blob: a2246359e6f422049caaaf775dc7a4f30e7ff811 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_transport.c
3 *
4 * This file contains the Generic Target Engine Core.
5 *
6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8 * Copyright (c) 2007-2010 Rising Tide Systems
9 * Copyright (c) 2008-2010 Linux-iSCSI.org
10 *
11 * Nicholas A. Bellinger <nab@kernel.org>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 *
27 ******************************************************************************/
28
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080029#include <linux/net.h>
30#include <linux/delay.h>
31#include <linux/string.h>
32#include <linux/timer.h>
33#include <linux/slab.h>
34#include <linux/blkdev.h>
35#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036#include <linux/kthread.h>
37#include <linux/in.h>
38#include <linux/cdrom.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040039#include <linux/module.h>
Roland Dreier015487b2012-02-13 16:18:17 -080040#include <linux/ratelimit.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041#include <asm/unaligned.h>
42#include <net/sock.h>
43#include <net/tcp.h>
44#include <scsi/scsi.h>
45#include <scsi/scsi_cmnd.h>
Nicholas Bellingere66ecd52011-05-19 20:19:14 -070046#include <scsi/scsi_tcq.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047
48#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050049#include <target/target_core_backend.h>
50#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080051#include <target/target_core_configfs.h>
52
Christoph Hellwige26d99a2011-11-14 12:30:30 -050053#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080054#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080055#include "target_core_pr.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080056#include "target_core_ua.h"
57
Andy Grovere3d6f902011-07-19 08:55:10 +000058static int sub_api_initialized;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080059
Christoph Hellwig35e0e752011-10-17 13:56:53 -040060static struct workqueue_struct *target_completion_wq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061static struct kmem_cache *se_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062struct kmem_cache *se_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063struct kmem_cache *t10_pr_reg_cache;
64struct kmem_cache *t10_alua_lu_gp_cache;
65struct kmem_cache *t10_alua_lu_gp_mem_cache;
66struct kmem_cache *t10_alua_tg_pt_gp_cache;
67struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
68
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080069static int transport_generic_write_pending(struct se_cmd *);
Andy Grover5951146d2011-07-19 10:26:37 +000070static int transport_processing_thread(void *param);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -080071static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080072static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070073static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040074 struct se_device *dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080075static void transport_free_dev_tasks(struct se_cmd *cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +000076static int transport_generic_get_mem(struct se_cmd *cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -070077static void transport_put_cmd(struct se_cmd *cmd);
Christoph Hellwig3df8d402011-10-17 13:56:43 -040078static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080079static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
Christoph Hellwig35e0e752011-10-17 13:56:53 -040080static void target_complete_ok_work(struct work_struct *work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080081
Andy Grovere3d6f902011-07-19 08:55:10 +000082int init_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080084 se_sess_cache = kmem_cache_create("se_sess_cache",
85 sizeof(struct se_session), __alignof__(struct se_session),
86 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070087 if (!se_sess_cache) {
88 pr_err("kmem_cache_create() for struct se_session"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080089 " failed\n");
Andy Groverc8e31f22012-01-19 13:39:17 -080090 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080091 }
92 se_ua_cache = kmem_cache_create("se_ua_cache",
93 sizeof(struct se_ua), __alignof__(struct se_ua),
94 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070095 if (!se_ua_cache) {
96 pr_err("kmem_cache_create() for struct se_ua failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -040097 goto out_free_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080098 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080099 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
100 sizeof(struct t10_pr_registration),
101 __alignof__(struct t10_pr_registration), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700102 if (!t10_pr_reg_cache) {
103 pr_err("kmem_cache_create() for struct t10_pr_registration"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800104 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400105 goto out_free_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800106 }
107 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
108 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
109 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700110 if (!t10_alua_lu_gp_cache) {
111 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800112 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400113 goto out_free_pr_reg_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800114 }
115 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
116 sizeof(struct t10_alua_lu_gp_member),
117 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700118 if (!t10_alua_lu_gp_mem_cache) {
119 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800120 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400121 goto out_free_lu_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800122 }
123 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
124 sizeof(struct t10_alua_tg_pt_gp),
125 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700126 if (!t10_alua_tg_pt_gp_cache) {
127 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800128 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400129 goto out_free_lu_gp_mem_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800130 }
131 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
132 "t10_alua_tg_pt_gp_mem_cache",
133 sizeof(struct t10_alua_tg_pt_gp_member),
134 __alignof__(struct t10_alua_tg_pt_gp_member),
135 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700136 if (!t10_alua_tg_pt_gp_mem_cache) {
137 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800138 "mem_t failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400139 goto out_free_tg_pt_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800140 }
141
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400142 target_completion_wq = alloc_workqueue("target_completion",
143 WQ_MEM_RECLAIM, 0);
144 if (!target_completion_wq)
145 goto out_free_tg_pt_gp_mem_cache;
146
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800147 return 0;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400148
149out_free_tg_pt_gp_mem_cache:
150 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
151out_free_tg_pt_gp_cache:
152 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
153out_free_lu_gp_mem_cache:
154 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
155out_free_lu_gp_cache:
156 kmem_cache_destroy(t10_alua_lu_gp_cache);
157out_free_pr_reg_cache:
158 kmem_cache_destroy(t10_pr_reg_cache);
159out_free_ua_cache:
160 kmem_cache_destroy(se_ua_cache);
161out_free_sess_cache:
162 kmem_cache_destroy(se_sess_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800163out:
Andy Grovere3d6f902011-07-19 08:55:10 +0000164 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800165}
166
Andy Grovere3d6f902011-07-19 08:55:10 +0000167void release_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800168{
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400169 destroy_workqueue(target_completion_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800170 kmem_cache_destroy(se_sess_cache);
171 kmem_cache_destroy(se_ua_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800172 kmem_cache_destroy(t10_pr_reg_cache);
173 kmem_cache_destroy(t10_alua_lu_gp_cache);
174 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
175 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
176 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800177}
178
Andy Grovere3d6f902011-07-19 08:55:10 +0000179/* This code ensures unique mib indexes are handed out. */
180static DEFINE_SPINLOCK(scsi_mib_index_lock);
181static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800182
183/*
184 * Allocate a new row index for the entry type specified
185 */
186u32 scsi_get_new_index(scsi_index_t type)
187{
188 u32 new_index;
189
Andy Grovere3d6f902011-07-19 08:55:10 +0000190 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800191
Andy Grovere3d6f902011-07-19 08:55:10 +0000192 spin_lock(&scsi_mib_index_lock);
193 new_index = ++scsi_mib_index[type];
194 spin_unlock(&scsi_mib_index_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800195
196 return new_index;
197}
198
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500199static void transport_init_queue_obj(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800200{
201 atomic_set(&qobj->queue_cnt, 0);
202 INIT_LIST_HEAD(&qobj->qobj_list);
203 init_waitqueue_head(&qobj->thread_wq);
204 spin_lock_init(&qobj->cmd_queue_lock);
205}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800206
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700207void transport_subsystem_check_init(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800208{
209 int ret;
210
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700211 if (sub_api_initialized)
212 return;
213
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214 ret = request_module("target_core_iblock");
215 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700216 pr_err("Unable to load target_core_iblock\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800217
218 ret = request_module("target_core_file");
219 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700220 pr_err("Unable to load target_core_file\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800221
222 ret = request_module("target_core_pscsi");
223 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700224 pr_err("Unable to load target_core_pscsi\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800225
226 ret = request_module("target_core_stgt");
227 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700228 pr_err("Unable to load target_core_stgt\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229
Andy Grovere3d6f902011-07-19 08:55:10 +0000230 sub_api_initialized = 1;
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700231 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800232}
233
234struct se_session *transport_init_session(void)
235{
236 struct se_session *se_sess;
237
238 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700239 if (!se_sess) {
240 pr_err("Unable to allocate struct se_session from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800241 " se_sess_cache\n");
242 return ERR_PTR(-ENOMEM);
243 }
244 INIT_LIST_HEAD(&se_sess->sess_list);
245 INIT_LIST_HEAD(&se_sess->sess_acl_list);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700246 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
247 INIT_LIST_HEAD(&se_sess->sess_wait_list);
248 spin_lock_init(&se_sess->sess_cmd_lock);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800249 kref_init(&se_sess->sess_kref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800250
251 return se_sess;
252}
253EXPORT_SYMBOL(transport_init_session);
254
255/*
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700256 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800257 */
258void __transport_register_session(
259 struct se_portal_group *se_tpg,
260 struct se_node_acl *se_nacl,
261 struct se_session *se_sess,
262 void *fabric_sess_ptr)
263{
264 unsigned char buf[PR_REG_ISID_LEN];
265
266 se_sess->se_tpg = se_tpg;
267 se_sess->fabric_sess_ptr = fabric_sess_ptr;
268 /*
269 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
270 *
271 * Only set for struct se_session's that will actually be moving I/O.
272 * eg: *NOT* discovery sessions.
273 */
274 if (se_nacl) {
275 /*
276 * If the fabric module supports an ISID based TransportID,
277 * save this value in binary from the fabric I_T Nexus now.
278 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000279 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800280 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000281 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800282 &buf[0], PR_REG_ISID_LEN);
283 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
284 }
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800285 kref_get(&se_nacl->acl_kref);
286
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800287 spin_lock_irq(&se_nacl->nacl_sess_lock);
288 /*
289 * The se_nacl->nacl_sess pointer will be set to the
290 * last active I_T Nexus for each struct se_node_acl.
291 */
292 se_nacl->nacl_sess = se_sess;
293
294 list_add_tail(&se_sess->sess_acl_list,
295 &se_nacl->acl_sess_list);
296 spin_unlock_irq(&se_nacl->nacl_sess_lock);
297 }
298 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
299
Andy Grover6708bb22011-06-08 10:36:43 -0700300 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000301 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800302}
303EXPORT_SYMBOL(__transport_register_session);
304
305void transport_register_session(
306 struct se_portal_group *se_tpg,
307 struct se_node_acl *se_nacl,
308 struct se_session *se_sess,
309 void *fabric_sess_ptr)
310{
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700311 unsigned long flags;
312
313 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800314 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700315 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800316}
317EXPORT_SYMBOL(transport_register_session);
318
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800319static void target_release_session(struct kref *kref)
320{
321 struct se_session *se_sess = container_of(kref,
322 struct se_session, sess_kref);
323 struct se_portal_group *se_tpg = se_sess->se_tpg;
324
325 se_tpg->se_tpg_tfo->close_session(se_sess);
326}
327
328void target_get_session(struct se_session *se_sess)
329{
330 kref_get(&se_sess->sess_kref);
331}
332EXPORT_SYMBOL(target_get_session);
333
334int target_put_session(struct se_session *se_sess)
335{
336 return kref_put(&se_sess->sess_kref, target_release_session);
337}
338EXPORT_SYMBOL(target_put_session);
339
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800340static void target_complete_nacl(struct kref *kref)
341{
342 struct se_node_acl *nacl = container_of(kref,
343 struct se_node_acl, acl_kref);
344
345 complete(&nacl->acl_free_comp);
346}
347
348void target_put_nacl(struct se_node_acl *nacl)
349{
350 kref_put(&nacl->acl_kref, target_complete_nacl);
351}
352
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800353void transport_deregister_session_configfs(struct se_session *se_sess)
354{
355 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700356 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800357 /*
358 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
359 */
360 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700361 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700362 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800363 if (se_nacl->acl_stop == 0)
364 list_del(&se_sess->sess_acl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800365 /*
366 * If the session list is empty, then clear the pointer.
367 * Otherwise, set the struct se_session pointer from the tail
368 * element of the per struct se_node_acl active session list.
369 */
370 if (list_empty(&se_nacl->acl_sess_list))
371 se_nacl->nacl_sess = NULL;
372 else {
373 se_nacl->nacl_sess = container_of(
374 se_nacl->acl_sess_list.prev,
375 struct se_session, sess_acl_list);
376 }
Roland Dreier23388862011-06-22 01:02:21 -0700377 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800378 }
379}
380EXPORT_SYMBOL(transport_deregister_session_configfs);
381
382void transport_free_session(struct se_session *se_sess)
383{
384 kmem_cache_free(se_sess_cache, se_sess);
385}
386EXPORT_SYMBOL(transport_free_session);
387
388void transport_deregister_session(struct se_session *se_sess)
389{
390 struct se_portal_group *se_tpg = se_sess->se_tpg;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800391 struct target_core_fabric_ops *se_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800392 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700393 unsigned long flags;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800394 bool comp_nacl = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395
Andy Grover6708bb22011-06-08 10:36:43 -0700396 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800397 transport_free_session(se_sess);
398 return;
399 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800400 se_tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800401
Roland Dreiere63a8e12011-08-12 16:01:02 -0700402 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800403 list_del(&se_sess->sess_list);
404 se_sess->se_tpg = NULL;
405 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700406 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800407
408 /*
409 * Determine if we need to do extra work for this initiator node's
410 * struct se_node_acl if it had been previously dynamically generated.
411 */
412 se_nacl = se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800413
Nicholas Bellinger01468342012-03-10 14:32:52 -0800414 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
415 if (se_nacl && se_nacl->dynamic_node_acl) {
416 if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
417 list_del(&se_nacl->acl_list);
418 se_tpg->num_node_acls--;
419 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
420 core_tpg_wait_for_nacl_pr_ref(se_nacl);
421 core_free_device_list_for_node(se_nacl, se_tpg);
422 se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
423
424 comp_nacl = false;
425 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800426 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800427 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800428 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800429
Andy Grover6708bb22011-06-08 10:36:43 -0700430 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000431 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellinger01468342012-03-10 14:32:52 -0800432 /*
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800433 * If last kref is dropping now for an explict NodeACL, awake sleeping
434 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
435 * removal context.
Nicholas Bellinger01468342012-03-10 14:32:52 -0800436 */
437 if (se_nacl && comp_nacl == true)
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800438 target_put_nacl(se_nacl);
Nicholas Bellinger01468342012-03-10 14:32:52 -0800439
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800440 transport_free_session(se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800441}
442EXPORT_SYMBOL(transport_deregister_session);
443
444/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700445 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800446 */
447static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
448{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400449 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450 struct se_task *task;
451 unsigned long flags;
452
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400453 if (!dev)
454 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800455
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400456 task = cmd->t_task;
457 if (task) {
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400458 if (task->task_flags & TF_ACTIVE)
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400459 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800461 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500462 if (task->t_state_active) {
463 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
464 cmd->se_tfo->get_task_tag(cmd), dev, task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800465
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500466 list_del(&task->t_state_list);
467 atomic_dec(&cmd->t_task_cdbs_ex_left);
468 task->t_state_active = false;
469 }
470 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471 }
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500472
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800473}
474
475/* transport_cmd_check_stop():
476 *
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500477 * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800478 * 'transport_off = 2' determines if task_dev_state should be removed.
479 *
480 * A non-zero u8 t_state sets cmd->t_state.
481 * Returns 1 when command is stopped, else 0.
482 */
483static int transport_cmd_check_stop(
484 struct se_cmd *cmd,
485 int transport_off,
486 u8 t_state)
487{
488 unsigned long flags;
489
Andy Grovera1d8b492011-05-02 17:12:10 -0700490 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800491 /*
492 * Determine if IOCTL context caller in requesting the stopping of this
493 * command for LUN shutdown purposes.
494 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500495 if (cmd->transport_state & CMD_T_LUN_STOP) {
496 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
497 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800498
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500499 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 if (transport_off == 2)
501 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700502 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800503
Andy Grovera1d8b492011-05-02 17:12:10 -0700504 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800505 return 1;
506 }
507 /*
508 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000509 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800510 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500511 if (cmd->transport_state & CMD_T_STOP) {
512 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
513 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000514 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800515
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800516 if (transport_off == 2)
517 transport_all_task_dev_remove_state(cmd);
518
519 /*
520 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
521 * to FE.
522 */
523 if (transport_off == 2)
524 cmd->se_lun = NULL;
Andy Grovera1d8b492011-05-02 17:12:10 -0700525 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800526
Andy Grovera1d8b492011-05-02 17:12:10 -0700527 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800528 return 1;
529 }
530 if (transport_off) {
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500531 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800532 if (transport_off == 2) {
533 transport_all_task_dev_remove_state(cmd);
534 /*
535 * Clear struct se_cmd->se_lun before the transport_off == 2
536 * handoff to fabric module.
537 */
538 cmd->se_lun = NULL;
539 /*
540 * Some fabric modules like tcm_loop can release
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300541 * their internally allocated I/O reference now and
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800542 * struct se_cmd now.
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700543 *
544 * Fabric modules are expected to return '1' here if the
545 * se_cmd being passed is released at this point,
546 * or zero if not being released.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000548 if (cmd->se_tfo->check_stop_free != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800549 spin_unlock_irqrestore(
Andy Grovera1d8b492011-05-02 17:12:10 -0700550 &cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800551
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700552 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553 }
554 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700555 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800556
557 return 0;
558 } else if (t_state)
559 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700560 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800561
562 return 0;
563}
564
565static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
566{
567 return transport_cmd_check_stop(cmd, 2, 0);
568}
569
570static void transport_lun_remove_cmd(struct se_cmd *cmd)
571{
Andy Grovere3d6f902011-07-19 08:55:10 +0000572 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800573 unsigned long flags;
574
575 if (!lun)
576 return;
577
Andy Grovera1d8b492011-05-02 17:12:10 -0700578 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500579 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
580 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
581 transport_all_task_dev_remove_state(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800582 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700583 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800585 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500586 if (!list_empty(&cmd->se_lun_node))
587 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
589}
590
591void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
592{
Andy Groverc8e31f22012-01-19 13:39:17 -0800593 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700594 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800595
596 if (transport_cmd_check_stop_to_fabric(cmd))
597 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700598 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400599 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200600 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700601 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800602}
603
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400604static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
605 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800606{
607 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000608 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609 unsigned long flags;
610
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800611 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700612 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800613 cmd->t_state = t_state;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500614 cmd->transport_state |= CMD_T_ACTIVE;
Andy Grovera1d8b492011-05-02 17:12:10 -0700615 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800616 }
617
618 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700619
620 /* If the cmd is already on the list, remove it before we add it */
621 if (!list_empty(&cmd->se_queue_node))
622 list_del(&cmd->se_queue_node);
623 else
624 atomic_inc(&qobj->queue_cnt);
625
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400626 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700627 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400628 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700629 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500630 cmd->transport_state |= CMD_T_QUEUED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
632
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800633 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800634}
635
Andy Grover5951146d2011-07-19 10:26:37 +0000636static struct se_cmd *
637transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800638{
Andy Grover5951146d2011-07-19 10:26:37 +0000639 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800640 unsigned long flags;
641
642 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
643 if (list_empty(&qobj->qobj_list)) {
644 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
645 return NULL;
646 }
Andy Grover5951146d2011-07-19 10:26:37 +0000647 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800648
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500649 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700650 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800651 atomic_dec(&qobj->queue_cnt);
652 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
653
Andy Grover5951146d2011-07-19 10:26:37 +0000654 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800655}
656
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400657static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800658{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400659 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800660 unsigned long flags;
661
662 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500663 if (!(cmd->transport_state & CMD_T_QUEUED)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800664 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
665 return;
666 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500667 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700668 atomic_dec(&qobj->queue_cnt);
669 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800670 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800671}
672
673/*
674 * Completion function used by TCM subsystem plugins (such as FILEIO)
675 * for queueing up response from struct se_subsystem_api->do_task()
676 */
677void transport_complete_sync_cache(struct se_cmd *cmd, int good)
678{
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400679 struct se_task *task = cmd->t_task;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800680
681 if (good) {
682 cmd->scsi_status = SAM_STAT_GOOD;
683 task->task_scsi_status = GOOD;
684 } else {
685 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700686 task->task_se_cmd->scsi_sense_reason =
687 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
688
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800689 }
690
691 transport_complete_task(task, good);
692}
693EXPORT_SYMBOL(transport_complete_sync_cache);
694
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400695static void target_complete_failure_work(struct work_struct *work)
696{
697 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
698
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700699 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400700}
701
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800702/* transport_complete_task():
703 *
704 * Called from interrupt and non interrupt context depending
705 * on the transport plugin.
706 */
707void transport_complete_task(struct se_task *task, int success)
708{
Andy Grovere3d6f902011-07-19 08:55:10 +0000709 struct se_cmd *cmd = task->task_se_cmd;
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400710 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800711 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800712
Andy Grovera1d8b492011-05-02 17:12:10 -0700713 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400714 task->task_flags &= ~TF_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800715
716 /*
717 * See if any sense data exists, if so set the TASK_SENSE flag.
718 * Also check for any other post completion work that needs to be
719 * done by the plugins.
720 */
721 if (dev && dev->transport->transport_complete) {
722 if (dev->transport->transport_complete(task) != 0) {
723 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
Christoph Hellwigef804a82011-11-23 06:53:58 -0500724 task->task_flags |= TF_HAS_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800725 success = 1;
726 }
727 }
728
729 /*
730 * See if we are waiting for outstanding struct se_task
731 * to complete for an exception condition
732 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400733 if (task->task_flags & TF_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700734 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800735 complete(&task->task_stop_comp);
736 return;
737 }
Christoph Hellwig2235007c2011-11-02 05:06:35 -0700738
739 if (!success)
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500740 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig2235007c2011-11-02 05:06:35 -0700741
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800742 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800743 * Decrement the outstanding t_task_cdbs_left count. The last
744 * struct se_task from struct se_cmd will complete itself into the
745 * device queue depending upon int success.
746 */
Andy Grover6708bb22011-06-08 10:36:43 -0700747 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700748 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800749 return;
750 }
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800751 /*
752 * Check for case where an explict ABORT_TASK has been received
753 * and transport_wait_for_tasks() will be waiting for completion..
754 */
755 if (cmd->transport_state & CMD_T_ABORTED &&
756 cmd->transport_state & CMD_T_STOP) {
757 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
758 complete(&cmd->t_transport_stop_comp);
759 return;
760 } else if (cmd->transport_state & CMD_T_FAILED) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500761 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400762 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800763 } else {
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400764 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800765 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400766
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400767 cmd->t_state = TRANSPORT_COMPLETE;
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800768 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
Andy Grovera1d8b492011-05-02 17:12:10 -0700769 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800770
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400771 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800772}
773EXPORT_SYMBOL(transport_complete_task);
774
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400775void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
776{
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400777 struct se_task *task = cmd->t_task;
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400778
779 task->task_scsi_status = scsi_status;
780 transport_complete_task(task, scsi_status == GOOD);
781}
782EXPORT_SYMBOL(target_complete_cmd);
783
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800784/*
785 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
786 * struct se_task list are ready to be added to the active execution list
787 * struct se_device
788
789 * Called with se_dev_t->execute_task_lock called.
790 */
791static inline int transport_add_task_check_sam_attr(
792 struct se_task *task,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800793 struct se_device *dev)
794{
795 /*
796 * No SAM Task attribute emulation enabled, add to tail of
797 * execution queue
798 */
799 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
800 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
801 return 0;
802 }
803 /*
804 * HEAD_OF_QUEUE attribute for received CDB, which means
805 * the first task that is associated with a struct se_cmd goes to
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400806 * head of the struct se_device->execute_task_list.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800807 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -0700808 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400809 list_add(&task->t_execute_list, &dev->execute_task_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800810
Andy Grover6708bb22011-06-08 10:36:43 -0700811 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800812 " in execution queue\n",
Andy Grover6708bb22011-06-08 10:36:43 -0700813 task->task_se_cmd->t_task_cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800814 return 1;
815 }
816 /*
817 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
818 * transitioned from Dermant -> Active state, and are added to the end
819 * of the struct se_device->execute_task_list
820 */
821 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
822 return 0;
823}
824
825/* __transport_add_task_to_execute_queue():
826 *
827 * Called with se_dev_t->execute_task_lock called.
828 */
829static void __transport_add_task_to_execute_queue(
830 struct se_task *task,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831 struct se_device *dev)
832{
833 int head_of_queue;
834
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400835 head_of_queue = transport_add_task_check_sam_attr(task, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800836 atomic_inc(&dev->execute_tasks);
837
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500838 if (task->t_state_active)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800839 return;
840 /*
841 * Determine if this task needs to go to HEAD_OF_QUEUE for the
842 * state list as well. Running with SAM Task Attribute emulation
843 * will always return head_of_queue == 0 here
844 */
845 if (head_of_queue)
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400846 list_add(&task->t_state_list, &dev->state_task_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800847 else
848 list_add_tail(&task->t_state_list, &dev->state_task_list);
849
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500850 task->t_state_active = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800851
Andy Grover6708bb22011-06-08 10:36:43 -0700852 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000853 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800854 task, dev);
855}
856
857static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
858{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400859 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800860 struct se_task *task;
861 unsigned long flags;
862
Andy Grovera1d8b492011-05-02 17:12:10 -0700863 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400864 task = cmd->t_task;
865 if (task) {
866 if (task->task_flags & TF_ACTIVE)
867 goto out;
868
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800869 spin_lock(&dev->execute_task_lock);
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500870 if (!task->t_state_active) {
871 list_add_tail(&task->t_state_list,
872 &dev->state_task_list);
873 task->t_state_active = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800874
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500875 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
876 task->task_se_cmd->se_tfo->get_task_tag(
877 task->task_se_cmd), task, dev);
878 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800879 spin_unlock(&dev->execute_task_lock);
880 }
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400881out:
Andy Grovera1d8b492011-05-02 17:12:10 -0700882 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800883}
884
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800885static void __transport_add_tasks_from_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800886{
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400887 struct se_task *task;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800888
Christoph Hellwig785fdf72012-04-24 00:25:04 -0400889 task = cmd->t_task;
890 if (task && list_empty(&task->t_execute_list))
891 __transport_add_task_to_execute_queue(task, cmd->se_dev);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800892}
893
894static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
895{
896 unsigned long flags;
897 struct se_device *dev = cmd->se_dev;
898
899 spin_lock_irqsave(&dev->execute_task_lock, flags);
900 __transport_add_tasks_from_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800901 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800902}
903
Christoph Hellwig04629b72011-10-12 11:07:04 -0400904void __transport_remove_task_from_execute_queue(struct se_task *task,
905 struct se_device *dev)
906{
907 list_del_init(&task->t_execute_list);
908 atomic_dec(&dev->execute_tasks);
909}
910
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500911static void transport_remove_task_from_execute_queue(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800912 struct se_task *task,
913 struct se_device *dev)
914{
915 unsigned long flags;
916
Christoph Hellwig04629b72011-10-12 11:07:04 -0400917 if (WARN_ON(list_empty(&task->t_execute_list)))
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700918 return;
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700919
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800920 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig04629b72011-10-12 11:07:04 -0400921 __transport_remove_task_from_execute_queue(task, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800922 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
923}
924
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700925/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700926 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700927 */
928
929static void target_qf_do_work(struct work_struct *work)
930{
931 struct se_device *dev = container_of(work, struct se_device,
932 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700933 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700934 struct se_cmd *cmd, *cmd_tmp;
935
936 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700937 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
938 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700939
Roland Dreierbcac3642011-08-27 21:33:16 -0700940 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700941 list_del(&cmd->se_qf_node);
942 atomic_dec(&dev->dev_qf_count);
943 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700944
Andy Grover6708bb22011-06-08 10:36:43 -0700945 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700946 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400947 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700948 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
949 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400950
951 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700952 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700953}
954
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800955unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
956{
957 switch (cmd->data_direction) {
958 case DMA_NONE:
959 return "NONE";
960 case DMA_FROM_DEVICE:
961 return "READ";
962 case DMA_TO_DEVICE:
963 return "WRITE";
964 case DMA_BIDIRECTIONAL:
965 return "BIDI";
966 default:
967 break;
968 }
969
970 return "UNKNOWN";
971}
972
973void transport_dump_dev_state(
974 struct se_device *dev,
975 char *b,
976 int *bl)
977{
978 *bl += sprintf(b + *bl, "Status: ");
979 switch (dev->dev_status) {
980 case TRANSPORT_DEVICE_ACTIVATED:
981 *bl += sprintf(b + *bl, "ACTIVATED");
982 break;
983 case TRANSPORT_DEVICE_DEACTIVATED:
984 *bl += sprintf(b + *bl, "DEACTIVATED");
985 break;
986 case TRANSPORT_DEVICE_SHUTDOWN:
987 *bl += sprintf(b + *bl, "SHUTDOWN");
988 break;
989 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
990 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
991 *bl += sprintf(b + *bl, "OFFLINE");
992 break;
993 default:
994 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
995 break;
996 }
997
Nicholas Bellinger65586d52011-11-30 01:25:21 -0800998 *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
999 atomic_read(&dev->execute_tasks), dev->queue_depth);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001000 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001001 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 -08001002 *bl += sprintf(b + *bl, " ");
1003}
1004
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001005void transport_dump_vpd_proto_id(
1006 struct t10_vpd *vpd,
1007 unsigned char *p_buf,
1008 int p_buf_len)
1009{
1010 unsigned char buf[VPD_TMP_BUF_SIZE];
1011 int len;
1012
1013 memset(buf, 0, VPD_TMP_BUF_SIZE);
1014 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1015
1016 switch (vpd->protocol_identifier) {
1017 case 0x00:
1018 sprintf(buf+len, "Fibre Channel\n");
1019 break;
1020 case 0x10:
1021 sprintf(buf+len, "Parallel SCSI\n");
1022 break;
1023 case 0x20:
1024 sprintf(buf+len, "SSA\n");
1025 break;
1026 case 0x30:
1027 sprintf(buf+len, "IEEE 1394\n");
1028 break;
1029 case 0x40:
1030 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1031 " Protocol\n");
1032 break;
1033 case 0x50:
1034 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1035 break;
1036 case 0x60:
1037 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1038 break;
1039 case 0x70:
1040 sprintf(buf+len, "Automation/Drive Interface Transport"
1041 " Protocol\n");
1042 break;
1043 case 0x80:
1044 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1045 break;
1046 default:
1047 sprintf(buf+len, "Unknown 0x%02x\n",
1048 vpd->protocol_identifier);
1049 break;
1050 }
1051
1052 if (p_buf)
1053 strncpy(p_buf, buf, p_buf_len);
1054 else
Andy Grover6708bb22011-06-08 10:36:43 -07001055 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001056}
1057
1058void
1059transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1060{
1061 /*
1062 * Check if the Protocol Identifier Valid (PIV) bit is set..
1063 *
1064 * from spc3r23.pdf section 7.5.1
1065 */
1066 if (page_83[1] & 0x80) {
1067 vpd->protocol_identifier = (page_83[0] & 0xf0);
1068 vpd->protocol_identifier_set = 1;
1069 transport_dump_vpd_proto_id(vpd, NULL, 0);
1070 }
1071}
1072EXPORT_SYMBOL(transport_set_vpd_proto_id);
1073
1074int transport_dump_vpd_assoc(
1075 struct t10_vpd *vpd,
1076 unsigned char *p_buf,
1077 int p_buf_len)
1078{
1079 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001080 int ret = 0;
1081 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082
1083 memset(buf, 0, VPD_TMP_BUF_SIZE);
1084 len = sprintf(buf, "T10 VPD Identifier Association: ");
1085
1086 switch (vpd->association) {
1087 case 0x00:
1088 sprintf(buf+len, "addressed logical unit\n");
1089 break;
1090 case 0x10:
1091 sprintf(buf+len, "target port\n");
1092 break;
1093 case 0x20:
1094 sprintf(buf+len, "SCSI target device\n");
1095 break;
1096 default:
1097 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +00001098 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001099 break;
1100 }
1101
1102 if (p_buf)
1103 strncpy(p_buf, buf, p_buf_len);
1104 else
Andy Grover6708bb22011-06-08 10:36:43 -07001105 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001106
1107 return ret;
1108}
1109
1110int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1111{
1112 /*
1113 * The VPD identification association..
1114 *
1115 * from spc3r23.pdf Section 7.6.3.1 Table 297
1116 */
1117 vpd->association = (page_83[1] & 0x30);
1118 return transport_dump_vpd_assoc(vpd, NULL, 0);
1119}
1120EXPORT_SYMBOL(transport_set_vpd_assoc);
1121
1122int transport_dump_vpd_ident_type(
1123 struct t10_vpd *vpd,
1124 unsigned char *p_buf,
1125 int p_buf_len)
1126{
1127 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001128 int ret = 0;
1129 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001130
1131 memset(buf, 0, VPD_TMP_BUF_SIZE);
1132 len = sprintf(buf, "T10 VPD Identifier Type: ");
1133
1134 switch (vpd->device_identifier_type) {
1135 case 0x00:
1136 sprintf(buf+len, "Vendor specific\n");
1137 break;
1138 case 0x01:
1139 sprintf(buf+len, "T10 Vendor ID based\n");
1140 break;
1141 case 0x02:
1142 sprintf(buf+len, "EUI-64 based\n");
1143 break;
1144 case 0x03:
1145 sprintf(buf+len, "NAA\n");
1146 break;
1147 case 0x04:
1148 sprintf(buf+len, "Relative target port identifier\n");
1149 break;
1150 case 0x08:
1151 sprintf(buf+len, "SCSI name string\n");
1152 break;
1153 default:
1154 sprintf(buf+len, "Unsupported: 0x%02x\n",
1155 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +00001156 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001157 break;
1158 }
1159
Andy Grovere3d6f902011-07-19 08:55:10 +00001160 if (p_buf) {
1161 if (p_buf_len < strlen(buf)+1)
1162 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +00001164 } else {
Andy Grover6708bb22011-06-08 10:36:43 -07001165 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +00001166 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001167
1168 return ret;
1169}
1170
1171int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1172{
1173 /*
1174 * The VPD identifier type..
1175 *
1176 * from spc3r23.pdf Section 7.6.3.1 Table 298
1177 */
1178 vpd->device_identifier_type = (page_83[1] & 0x0f);
1179 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1180}
1181EXPORT_SYMBOL(transport_set_vpd_ident_type);
1182
1183int transport_dump_vpd_ident(
1184 struct t10_vpd *vpd,
1185 unsigned char *p_buf,
1186 int p_buf_len)
1187{
1188 unsigned char buf[VPD_TMP_BUF_SIZE];
1189 int ret = 0;
1190
1191 memset(buf, 0, VPD_TMP_BUF_SIZE);
1192
1193 switch (vpd->device_identifier_code_set) {
1194 case 0x01: /* Binary */
1195 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1196 &vpd->device_identifier[0]);
1197 break;
1198 case 0x02: /* ASCII */
1199 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1200 &vpd->device_identifier[0]);
1201 break;
1202 case 0x03: /* UTF-8 */
1203 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1204 &vpd->device_identifier[0]);
1205 break;
1206 default:
1207 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1208 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +00001209 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001210 break;
1211 }
1212
1213 if (p_buf)
1214 strncpy(p_buf, buf, p_buf_len);
1215 else
Andy Grover6708bb22011-06-08 10:36:43 -07001216 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001217
1218 return ret;
1219}
1220
1221int
1222transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1223{
1224 static const char hex_str[] = "0123456789abcdef";
1225 int j = 0, i = 4; /* offset to start of the identifer */
1226
1227 /*
1228 * The VPD Code Set (encoding)
1229 *
1230 * from spc3r23.pdf Section 7.6.3.1 Table 296
1231 */
1232 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1233 switch (vpd->device_identifier_code_set) {
1234 case 0x01: /* Binary */
1235 vpd->device_identifier[j++] =
1236 hex_str[vpd->device_identifier_type];
1237 while (i < (4 + page_83[3])) {
1238 vpd->device_identifier[j++] =
1239 hex_str[(page_83[i] & 0xf0) >> 4];
1240 vpd->device_identifier[j++] =
1241 hex_str[page_83[i] & 0x0f];
1242 i++;
1243 }
1244 break;
1245 case 0x02: /* ASCII */
1246 case 0x03: /* UTF-8 */
1247 while (i < (4 + page_83[3]))
1248 vpd->device_identifier[j++] = page_83[i++];
1249 break;
1250 default:
1251 break;
1252 }
1253
1254 return transport_dump_vpd_ident(vpd, NULL, 0);
1255}
1256EXPORT_SYMBOL(transport_set_vpd_ident);
1257
1258static void core_setup_task_attr_emulation(struct se_device *dev)
1259{
1260 /*
1261 * If this device is from Target_Core_Mod/pSCSI, disable the
1262 * SAM Task Attribute emulation.
1263 *
1264 * This is currently not available in upsream Linux/SCSI Target
1265 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1266 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001267 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1269 return;
1270 }
1271
1272 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001273 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001274 " device\n", dev->transport->name,
1275 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001276}
1277
1278static void scsi_dump_inquiry(struct se_device *dev)
1279{
Andy Grovere3d6f902011-07-19 08:55:10 +00001280 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001281 char buf[17];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282 int i, device_type;
1283 /*
1284 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1285 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286 for (i = 0; i < 8; i++)
1287 if (wwn->vendor[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001288 buf[i] = wwn->vendor[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001289 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001290 buf[i] = ' ';
1291 buf[i] = '\0';
1292 pr_debug(" Vendor: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001293
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001294 for (i = 0; i < 16; i++)
1295 if (wwn->model[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001296 buf[i] = wwn->model[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001297 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001298 buf[i] = ' ';
1299 buf[i] = '\0';
1300 pr_debug(" Model: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001301
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001302 for (i = 0; i < 4; i++)
1303 if (wwn->revision[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001304 buf[i] = wwn->revision[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001305 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001306 buf[i] = ' ';
1307 buf[i] = '\0';
1308 pr_debug(" Revision: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001309
Andy Grovere3d6f902011-07-19 08:55:10 +00001310 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001311 pr_debug(" Type: %s ", scsi_device_type(device_type));
1312 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001313 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001314}
1315
1316struct se_device *transport_add_device_to_core_hba(
1317 struct se_hba *hba,
1318 struct se_subsystem_api *transport,
1319 struct se_subsystem_dev *se_dev,
1320 u32 device_flags,
1321 void *transport_dev,
1322 struct se_dev_limits *dev_limits,
1323 const char *inquiry_prod,
1324 const char *inquiry_rev)
1325{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001326 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001327 struct se_device *dev;
1328
1329 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001330 if (!dev) {
1331 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001332 return NULL;
1333 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001334
Andy Grovere3d6f902011-07-19 08:55:10 +00001335 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001336 dev->dev_flags = device_flags;
1337 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001338 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339 dev->se_hba = hba;
1340 dev->se_sub_dev = se_dev;
1341 dev->transport = transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001342 INIT_LIST_HEAD(&dev->dev_list);
1343 INIT_LIST_HEAD(&dev->dev_sep_list);
1344 INIT_LIST_HEAD(&dev->dev_tmr_list);
1345 INIT_LIST_HEAD(&dev->execute_task_list);
1346 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347 INIT_LIST_HEAD(&dev->state_task_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001348 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001349 spin_lock_init(&dev->execute_task_lock);
1350 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001351 spin_lock_init(&dev->dev_reservation_lock);
1352 spin_lock_init(&dev->dev_status_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001353 spin_lock_init(&dev->se_port_lock);
1354 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001355 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001356 atomic_set(&dev->dev_ordered_id, 0);
1357
1358 se_dev_set_default_attribs(dev, dev_limits);
1359
1360 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1361 dev->creation_time = get_jiffies_64();
1362 spin_lock_init(&dev->stats_lock);
1363
1364 spin_lock(&hba->device_lock);
1365 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1366 hba->dev_count++;
1367 spin_unlock(&hba->device_lock);
1368 /*
1369 * Setup the SAM Task Attribute emulation for struct se_device
1370 */
1371 core_setup_task_attr_emulation(dev);
1372 /*
1373 * Force PR and ALUA passthrough emulation with internal object use.
1374 */
1375 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1376 /*
1377 * Setup the Reservations infrastructure for struct se_device
1378 */
1379 core_setup_reservations(dev, force_pt);
1380 /*
1381 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1382 */
1383 if (core_setup_alua(dev, force_pt) < 0)
1384 goto out;
1385
1386 /*
1387 * Startup the struct se_device processing thread
1388 */
1389 dev->process_thread = kthread_run(transport_processing_thread, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001390 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001391 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001392 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001393 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001394 goto out;
1395 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001396 /*
1397 * Setup work_queue for QUEUE_FULL
1398 */
1399 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001400 /*
1401 * Preload the initial INQUIRY const values if we are doing
1402 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1403 * passthrough because this is being provided by the backend LLD.
1404 * This is required so that transport_get_inquiry() copies these
1405 * originals once back into DEV_T10_WWN(dev) for the virtual device
1406 * setup.
1407 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001408 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001409 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001410 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001411 " INQUIRY consts\n");
1412 goto out;
1413 }
1414
Andy Grovere3d6f902011-07-19 08:55:10 +00001415 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1416 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1417 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001418 }
1419 scsi_dump_inquiry(dev);
1420
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001421 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001422out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001423 kthread_stop(dev->process_thread);
1424
1425 spin_lock(&hba->device_lock);
1426 list_del(&dev->dev_list);
1427 hba->dev_count--;
1428 spin_unlock(&hba->device_lock);
1429
1430 se_release_vpd_for_dev(dev);
1431
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001432 kfree(dev);
1433
1434 return NULL;
1435}
1436EXPORT_SYMBOL(transport_add_device_to_core_hba);
1437
1438/* transport_generic_prepare_cdb():
1439 *
1440 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1441 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1442 * The point of this is since we are mapping iSCSI LUNs to
1443 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1444 * devices and HBAs for a loop.
1445 */
1446static inline void transport_generic_prepare_cdb(
1447 unsigned char *cdb)
1448{
1449 switch (cdb[0]) {
1450 case READ_10: /* SBC - RDProtect */
1451 case READ_12: /* SBC - RDProtect */
1452 case READ_16: /* SBC - RDProtect */
1453 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1454 case VERIFY: /* SBC - VRProtect */
1455 case VERIFY_16: /* SBC - VRProtect */
1456 case WRITE_VERIFY: /* SBC - VRProtect */
1457 case WRITE_VERIFY_12: /* SBC - VRProtect */
1458 break;
1459 default:
1460 cdb[1] &= 0x1f; /* clear logical unit number */
1461 break;
1462 }
1463}
1464
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001465static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1466
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001467/*
1468 * Used by fabric modules containing a local struct se_cmd within their
1469 * fabric dependent per I/O descriptor.
1470 */
1471void transport_init_se_cmd(
1472 struct se_cmd *cmd,
1473 struct target_core_fabric_ops *tfo,
1474 struct se_session *se_sess,
1475 u32 data_length,
1476 int data_direction,
1477 int task_attr,
1478 unsigned char *sense_buffer)
1479{
Andy Grover5951146d2011-07-19 10:26:37 +00001480 INIT_LIST_HEAD(&cmd->se_lun_node);
1481 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001482 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001483 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001484 INIT_LIST_HEAD(&cmd->se_cmd_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001485 init_completion(&cmd->transport_lun_fe_stop_comp);
1486 init_completion(&cmd->transport_lun_stop_comp);
1487 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001488 init_completion(&cmd->cmd_wait_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001489 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001490 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001491
1492 cmd->se_tfo = tfo;
1493 cmd->se_sess = se_sess;
1494 cmd->data_length = data_length;
1495 cmd->data_direction = data_direction;
1496 cmd->sam_task_attr = task_attr;
1497 cmd->sense_buffer = sense_buffer;
1498}
1499EXPORT_SYMBOL(transport_init_se_cmd);
1500
1501static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1502{
1503 /*
1504 * Check if SAM Task Attribute emulation is enabled for this
1505 * struct se_device storage object
1506 */
Andy Grover5951146d2011-07-19 10:26:37 +00001507 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001508 return 0;
1509
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001510 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001511 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001512 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001513 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001514 }
1515 /*
1516 * Used to determine when ORDERED commands should go from
1517 * Dormant to Active status.
1518 */
Andy Grover5951146d2011-07-19 10:26:37 +00001519 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001520 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001521 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001522 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001523 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001524 return 0;
1525}
1526
Andy Grovera12f41f2012-04-03 15:51:20 -07001527/* target_setup_cmd_from_cdb():
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001528 *
1529 * Called from fabric RX Thread.
1530 */
Andy Grovera12f41f2012-04-03 15:51:20 -07001531int target_setup_cmd_from_cdb(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001532 struct se_cmd *cmd,
1533 unsigned char *cdb)
1534{
1535 int ret;
1536
1537 transport_generic_prepare_cdb(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001538 /*
1539 * Ensure that the received CDB is less than the max (252 + 8) bytes
1540 * for VARIABLE_LENGTH_CMD
1541 */
1542 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001543 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001544 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1545 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001546 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1547 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001548 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001549 }
1550 /*
1551 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1552 * allocate the additional extended CDB buffer now.. Otherwise
1553 * setup the pointer from __t_task_cdb to t_task_cdb.
1554 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001555 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1556 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001557 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001558 if (!cmd->t_task_cdb) {
1559 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001560 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001561 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001562 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001563 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1564 cmd->scsi_sense_reason =
1565 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001566 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001567 }
1568 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001569 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001570 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001571 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001572 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001573 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001574 /*
1575 * Setup the received CDB based on SCSI defined opcodes and
1576 * perform unit attention, persistent reservations and ALUA
Andy Grovera1d8b492011-05-02 17:12:10 -07001577 * checks for virtual device backends. The cmd->t_task_cdb
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001578 * pointer is expected to be setup before we reach this point.
1579 */
1580 ret = transport_generic_cmd_sequencer(cmd, cdb);
1581 if (ret < 0)
1582 return ret;
1583 /*
1584 * Check for SAM Task Attribute Emulation
1585 */
1586 if (transport_check_alloc_task_attr(cmd) < 0) {
1587 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1588 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001589 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001590 }
1591 spin_lock(&cmd->se_lun->lun_sep_lock);
1592 if (cmd->se_lun->lun_sep)
1593 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1594 spin_unlock(&cmd->se_lun->lun_sep_lock);
1595 return 0;
1596}
Andy Grovera12f41f2012-04-03 15:51:20 -07001597EXPORT_SYMBOL(target_setup_cmd_from_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001598
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001599/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001600 * Used by fabric module frontends to queue tasks directly.
1601 * Many only be used from process context only
1602 */
1603int transport_handle_cdb_direct(
1604 struct se_cmd *cmd)
1605{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001606 int ret;
1607
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001608 if (!cmd->se_lun) {
1609 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001610 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001611 return -EINVAL;
1612 }
1613 if (in_interrupt()) {
1614 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001615 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001616 " from interrupt context\n");
1617 return -EINVAL;
1618 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001619 /*
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001620 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001621 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1622 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001623 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001624 *
1625 * Also, we don't take cmd->t_state_lock here as we only expect
1626 * this to be called for initial descriptor submission.
1627 */
1628 cmd->t_state = TRANSPORT_NEW_CMD;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001629 cmd->transport_state |= CMD_T_ACTIVE;
1630
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001631 /*
1632 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1633 * so follow TRANSPORT_NEW_CMD processing thread context usage
1634 * and call transport_generic_request_failure() if necessary..
1635 */
1636 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001637 if (ret < 0)
1638 transport_generic_request_failure(cmd);
1639
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001640 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001641}
1642EXPORT_SYMBOL(transport_handle_cdb_direct);
1643
Nicholas Bellingera6360782011-11-18 20:36:22 -08001644/**
1645 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1646 *
1647 * @se_cmd: command descriptor to submit
1648 * @se_sess: associated se_sess for endpoint
1649 * @cdb: pointer to SCSI CDB
1650 * @sense: pointer to SCSI sense buffer
1651 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1652 * @data_length: fabric expected data transfer length
1653 * @task_addr: SAM task attribute
1654 * @data_dir: DMA data direction
1655 * @flags: flags for command submission from target_sc_flags_tables
1656 *
1657 * This may only be called from process context, and also currently
1658 * assumes internal allocation of fabric payload buffer by target-core.
1659 **/
Andy Grover1edcdb42012-01-19 13:39:23 -08001660void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001661 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1662 u32 data_length, int task_attr, int data_dir, int flags)
1663{
1664 struct se_portal_group *se_tpg;
1665 int rc;
1666
1667 se_tpg = se_sess->se_tpg;
1668 BUG_ON(!se_tpg);
1669 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1670 BUG_ON(in_interrupt());
1671 /*
1672 * Initialize se_cmd for target operation. From this point
1673 * exceptions are handled by sending exception status via
1674 * target_core_fabric_ops->queue_status() callback
1675 */
1676 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1677 data_length, data_dir, task_attr, sense);
Sebastian Andrzej Siewiorb0d79942012-01-10 14:16:59 +01001678 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1679 se_cmd->unknown_data_length = 1;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001680 /*
1681 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1682 * se_sess->sess_cmd_list. A second kref_get here is necessary
1683 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1684 * kref_put() to happen during fabric packet acknowledgement.
1685 */
1686 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1687 /*
1688 * Signal bidirectional data payloads to target-core
1689 */
1690 if (flags & TARGET_SCF_BIDI_OP)
1691 se_cmd->se_cmd_flags |= SCF_BIDI;
1692 /*
1693 * Locate se_lun pointer and attach it to struct se_cmd
1694 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001695 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1696 transport_send_check_condition_and_sense(se_cmd,
1697 se_cmd->scsi_sense_reason, 0);
1698 target_put_sess_cmd(se_sess, se_cmd);
1699 return;
1700 }
Nicholas Bellingera6360782011-11-18 20:36:22 -08001701 /*
1702 * Sanitize CDBs via transport_generic_cmd_sequencer() and
1703 * allocate the necessary tasks to complete the received CDB+data
1704 */
Andy Grovera12f41f2012-04-03 15:51:20 -07001705 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001706 if (rc != 0) {
1707 transport_generic_request_failure(se_cmd);
1708 return;
1709 }
Andy Grover11e319e2012-04-03 15:51:28 -07001710
1711 /*
1712 * Check if we need to delay processing because of ALUA
1713 * Active/NonOptimized primary access state..
1714 */
1715 core_alua_check_nonop_delay(se_cmd);
1716
Nicholas Bellingera6360782011-11-18 20:36:22 -08001717 /*
1718 * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
1719 * for immediate execution of READs, otherwise wait for
1720 * transport_generic_handle_data() to be called for WRITEs
1721 * when fabric has filled the incoming buffer.
1722 */
1723 transport_handle_cdb_direct(se_cmd);
Andy Grover1edcdb42012-01-19 13:39:23 -08001724 return;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001725}
1726EXPORT_SYMBOL(target_submit_cmd);
1727
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001728static void target_complete_tmr_failure(struct work_struct *work)
1729{
1730 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1731
1732 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1733 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1734 transport_generic_free_cmd(se_cmd, 0);
1735}
1736
Andy Groverea98d7f2012-01-19 13:39:21 -08001737/**
1738 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1739 * for TMR CDBs
1740 *
1741 * @se_cmd: command descriptor to submit
1742 * @se_sess: associated se_sess for endpoint
1743 * @sense: pointer to SCSI sense buffer
1744 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1745 * @fabric_context: fabric context for TMR req
1746 * @tm_type: Type of TM request
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001747 * @gfp: gfp type for caller
1748 * @tag: referenced task tag for TMR_ABORT_TASK
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001749 * @flags: submit cmd flags
Andy Groverea98d7f2012-01-19 13:39:21 -08001750 *
1751 * Callable from all contexts.
1752 **/
1753
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001754int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
Andy Groverea98d7f2012-01-19 13:39:21 -08001755 unsigned char *sense, u32 unpacked_lun,
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001756 void *fabric_tmr_ptr, unsigned char tm_type,
1757 gfp_t gfp, unsigned int tag, int flags)
Andy Groverea98d7f2012-01-19 13:39:21 -08001758{
1759 struct se_portal_group *se_tpg;
1760 int ret;
1761
1762 se_tpg = se_sess->se_tpg;
1763 BUG_ON(!se_tpg);
1764
1765 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1766 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001767 /*
1768 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1769 * allocation failure.
1770 */
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001771 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001772 if (ret < 0)
1773 return -ENOMEM;
Andy Groverea98d7f2012-01-19 13:39:21 -08001774
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001775 if (tm_type == TMR_ABORT_TASK)
1776 se_cmd->se_tmr_req->ref_task_tag = tag;
1777
Andy Groverea98d7f2012-01-19 13:39:21 -08001778 /* See target_submit_cmd for commentary */
1779 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1780
Andy Groverea98d7f2012-01-19 13:39:21 -08001781 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1782 if (ret) {
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001783 /*
1784 * For callback during failure handling, push this work off
1785 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1786 */
1787 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1788 schedule_work(&se_cmd->work);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001789 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001790 }
1791 transport_generic_handle_tmr(se_cmd);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001792 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001793}
1794EXPORT_SYMBOL(target_submit_tmr);
1795
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001796/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001797 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1798 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1799 * complete setup in TCM process context w/ TFO->new_cmd_map().
1800 */
1801int transport_generic_handle_cdb_map(
1802 struct se_cmd *cmd)
1803{
Andy Grovere3d6f902011-07-19 08:55:10 +00001804 if (!cmd->se_lun) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001805 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001806 pr_err("cmd->se_lun is NULL\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001807 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001808 }
1809
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001810 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001811 return 0;
1812}
1813EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1814
1815/* transport_generic_handle_data():
1816 *
1817 *
1818 */
1819int transport_generic_handle_data(
1820 struct se_cmd *cmd)
1821{
1822 /*
1823 * For the software fabric case, then we assume the nexus is being
1824 * failed/shutdown when signals are pending from the kthread context
1825 * caller, so we return a failure. For the HW target mode case running
1826 * in interrupt code, the signal_pending() check is skipped.
1827 */
1828 if (!in_interrupt() && signal_pending(current))
Andy Grovere3d6f902011-07-19 08:55:10 +00001829 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001830 /*
1831 * If the received CDB has aleady been ABORTED by the generic
1832 * target engine, we now call transport_check_aborted_status()
1833 * to queue any delated TASK_ABORTED status for the received CDB to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001834 * fabric module as we are expecting no further incoming DATA OUT
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001835 * sequences at this point.
1836 */
1837 if (transport_check_aborted_status(cmd, 1) != 0)
1838 return 0;
1839
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001840 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001841 return 0;
1842}
1843EXPORT_SYMBOL(transport_generic_handle_data);
1844
1845/* transport_generic_handle_tmr():
1846 *
1847 *
1848 */
1849int transport_generic_handle_tmr(
1850 struct se_cmd *cmd)
1851{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001852 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001853 return 0;
1854}
1855EXPORT_SYMBOL(transport_generic_handle_tmr);
1856
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001857/*
1858 * If the task is active, request it to be stopped and sleep until it
1859 * has completed.
1860 */
1861bool target_stop_task(struct se_task *task, unsigned long *flags)
1862{
1863 struct se_cmd *cmd = task->task_se_cmd;
1864 bool was_active = false;
1865
1866 if (task->task_flags & TF_ACTIVE) {
1867 task->task_flags |= TF_REQUEST_STOP;
1868 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1869
1870 pr_debug("Task %p waiting to complete\n", task);
1871 wait_for_completion(&task->task_stop_comp);
1872 pr_debug("Task %p stopped successfully\n", task);
1873
1874 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1875 atomic_dec(&cmd->t_task_cdbs_left);
1876 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1877 was_active = true;
1878 }
1879
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001880 return was_active;
1881}
1882
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001883static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1884{
Christoph Hellwig785fdf72012-04-24 00:25:04 -04001885 struct se_task *task;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001886 unsigned long flags;
1887 int ret = 0;
1888
Andy Grover6708bb22011-06-08 10:36:43 -07001889 pr_debug("ITT[0x%08x] - Stopping tasks\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001890 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001891
1892 /*
1893 * No tasks remain in the execution queue
1894 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001895 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig785fdf72012-04-24 00:25:04 -04001896 task = cmd->t_task;
1897 if (task) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001898 pr_debug("Processing task %p\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001899 /*
1900 * If the struct se_task has not been sent and is not active,
1901 * remove the struct se_task from the execution queue.
1902 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04001903 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
Andy Grovera1d8b492011-05-02 17:12:10 -07001904 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001905 flags);
1906 transport_remove_task_from_execute_queue(task,
Christoph Hellwig42bf8292011-10-12 11:07:00 -04001907 cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001908
Christoph Hellwig04629b72011-10-12 11:07:04 -04001909 pr_debug("Task %p removed from execute queue\n", task);
Andy Grovera1d8b492011-05-02 17:12:10 -07001910 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig785fdf72012-04-24 00:25:04 -04001911 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001912 }
1913
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001914 if (!target_stop_task(task, &flags)) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001915 pr_debug("Task %p - did nothing\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001916 ret++;
1917 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001918 }
Christoph Hellwig785fdf72012-04-24 00:25:04 -04001919out:
Andy Grovera1d8b492011-05-02 17:12:10 -07001920 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001921 return ret;
1922}
1923
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001924/*
1925 * Handle SAM-esque emulation for generic transport request failures.
1926 */
Nicholas Bellinger2fbff1272012-02-10 16:18:11 -08001927void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001928{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001929 int ret = 0;
1930
Andy Grover6708bb22011-06-08 10:36:43 -07001931 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001932 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001933 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001934 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001935 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001936 cmd->t_state, cmd->scsi_sense_reason);
Christoph Hellwig785fdf72012-04-24 00:25:04 -04001937 pr_debug("-----[ t_task_cdbs_left: %d"
1938 " t_task_cdbs_ex_left: %d --"
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001939 " CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07001940 atomic_read(&cmd->t_task_cdbs_left),
Andy Grovera1d8b492011-05-02 17:12:10 -07001941 atomic_read(&cmd->t_task_cdbs_ex_left),
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001942 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1943 (cmd->transport_state & CMD_T_STOP) != 0,
1944 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001945
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001946 /*
1947 * For SAM Task Attribute emulation for failed struct se_cmd
1948 */
1949 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1950 transport_complete_task_attr(cmd);
1951
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001952 switch (cmd->scsi_sense_reason) {
1953 case TCM_NON_EXISTENT_LUN:
1954 case TCM_UNSUPPORTED_SCSI_OPCODE:
1955 case TCM_INVALID_CDB_FIELD:
1956 case TCM_INVALID_PARAMETER_LIST:
1957 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1958 case TCM_UNKNOWN_MODE_PAGE:
1959 case TCM_WRITE_PROTECTED:
1960 case TCM_CHECK_CONDITION_ABORT_CMD:
1961 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1962 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001963 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001964 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001965 /*
1966 * No SENSE Data payload for this case, set SCSI Status
1967 * and queue the response to $FABRIC_MOD.
1968 *
1969 * Uses linux/include/scsi/scsi.h SAM status codes defs
1970 */
1971 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1972 /*
1973 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1974 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1975 * CONFLICT STATUS.
1976 *
1977 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1978 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001979 if (cmd->se_sess &&
1980 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1981 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001982 cmd->orig_fe_lun, 0x2C,
1983 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1984
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001985 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001986 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001987 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001988 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001989 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001990 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001991 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001992 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1993 break;
1994 }
Nicholas Bellinger16ab8e62011-08-08 19:03:38 -07001995 /*
1996 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1997 * make the call to transport_send_check_condition_and_sense()
1998 * directly. Otherwise expect the fabric to make the call to
1999 * transport_send_check_condition_and_sense() after handling
2000 * possible unsoliticied write data payloads.
2001 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002002 ret = transport_send_check_condition_and_sense(cmd,
2003 cmd->scsi_sense_reason, 0);
2004 if (ret == -EAGAIN || ret == -ENOMEM)
2005 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002006
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007check_stop:
2008 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002009 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002010 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002011 return;
2012
2013queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04002014 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
2015 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002016}
Nicholas Bellinger2fbff1272012-02-10 16:18:11 -08002017EXPORT_SYMBOL(transport_generic_request_failure);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002018
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002019static inline u32 transport_lba_21(unsigned char *cdb)
2020{
2021 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
2022}
2023
2024static inline u32 transport_lba_32(unsigned char *cdb)
2025{
2026 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2027}
2028
2029static inline unsigned long long transport_lba_64(unsigned char *cdb)
2030{
2031 unsigned int __v1, __v2;
2032
2033 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2034 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2035
2036 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2037}
2038
2039/*
2040 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2041 */
2042static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
2043{
2044 unsigned int __v1, __v2;
2045
2046 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
2047 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
2048
2049 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2050}
2051
2052static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2053{
2054 unsigned long flags;
2055
Andy Grovera1d8b492011-05-02 17:12:10 -07002056 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002057 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
Andy Grovera1d8b492011-05-02 17:12:10 -07002058 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002059}
2060
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002061/*
2062 * Called from Fabric Module context from transport_execute_tasks()
2063 *
2064 * The return of this function determins if the tasks from struct se_cmd
2065 * get added to the execution queue in transport_execute_tasks(),
2066 * or are added to the delayed or ordered lists here.
2067 */
2068static inline int transport_execute_task_attr(struct se_cmd *cmd)
2069{
Andy Grover5951146d2011-07-19 10:26:37 +00002070 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002071 return 1;
2072 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002073 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002074 * to allow the passed struct se_cmd list of tasks to the front of the list.
2075 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002076 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07002077 pr_debug("Added HEAD_OF_QUEUE for CDB:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002078 " 0x%02x, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07002079 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002080 cmd->se_ordered_id);
2081 return 1;
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002082 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Andy Grover5951146d2011-07-19 10:26:37 +00002083 atomic_inc(&cmd->se_dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002084 smp_mb__after_atomic_inc();
2085
Andy Grover6708bb22011-06-08 10:36:43 -07002086 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002087 " list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002088 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002089 cmd->se_ordered_id);
2090 /*
2091 * Add ORDERED command to tail of execution queue if
2092 * no other older commands exist that need to be
2093 * completed first.
2094 */
Andy Grover6708bb22011-06-08 10:36:43 -07002095 if (!atomic_read(&cmd->se_dev->simple_cmds))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002096 return 1;
2097 } else {
2098 /*
2099 * For SIMPLE and UNTAGGED Task Attribute commands
2100 */
Andy Grover5951146d2011-07-19 10:26:37 +00002101 atomic_inc(&cmd->se_dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002102 smp_mb__after_atomic_inc();
2103 }
2104 /*
2105 * Otherwise if one or more outstanding ORDERED task attribute exist,
2106 * add the dormant task(s) built for the passed struct se_cmd to the
2107 * execution queue and become in Active state for this struct se_device.
2108 */
Andy Grover5951146d2011-07-19 10:26:37 +00002109 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002110 /*
2111 * Otherwise, add cmd w/ tasks to delayed cmd queue that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002112 * will be drained upon completion of HEAD_OF_QUEUE task.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002113 */
Andy Grover5951146d2011-07-19 10:26:37 +00002114 spin_lock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002115 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
Andy Grover5951146d2011-07-19 10:26:37 +00002116 list_add_tail(&cmd->se_delayed_node,
2117 &cmd->se_dev->delayed_cmd_list);
2118 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002119
Andy Grover6708bb22011-06-08 10:36:43 -07002120 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002121 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002122 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002123 cmd->se_ordered_id);
2124 /*
2125 * Return zero to let transport_execute_tasks() know
2126 * not to add the delayed tasks to the execution list.
2127 */
2128 return 0;
2129 }
2130 /*
2131 * Otherwise, no ORDERED task attributes exist..
2132 */
2133 return 1;
2134}
2135
2136/*
2137 * Called from fabric module context in transport_generic_new_cmd() and
2138 * transport_generic_process_write()
2139 */
2140static int transport_execute_tasks(struct se_cmd *cmd)
2141{
2142 int add_tasks;
Nicholas Bellinger40be67f2011-11-30 00:41:20 -08002143 struct se_device *se_dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002144 /*
2145 * Call transport_cmd_check_stop() to see if a fabric exception
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002146 * has occurred that prevents execution.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002147 */
Andy Grover6708bb22011-06-08 10:36:43 -07002148 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002149 /*
2150 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2151 * attribute for the tasks of the received struct se_cmd CDB
2152 */
2153 add_tasks = transport_execute_task_attr(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00002154 if (!add_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002155 goto execute_tasks;
2156 /*
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002157 * __transport_execute_tasks() -> __transport_add_tasks_from_cmd()
2158 * adds associated se_tasks while holding dev->execute_task_lock
2159 * before I/O dispath to avoid a double spinlock access.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002160 */
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002161 __transport_execute_tasks(se_dev, cmd);
2162 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002163 }
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002164
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002165execute_tasks:
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002166 __transport_execute_tasks(se_dev, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002167 return 0;
2168}
2169
2170/*
2171 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2172 * from struct se_device->execute_task_list and
2173 *
2174 * Called from transport_processing_thread()
2175 */
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002176static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002177{
2178 int error;
2179 struct se_cmd *cmd = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002180 struct se_task *task = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002181 unsigned long flags;
2182
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002183check_depth:
Andy Grovere3d6f902011-07-19 08:55:10 +00002184 spin_lock_irq(&dev->execute_task_lock);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002185 if (new_cmd != NULL)
2186 __transport_add_tasks_from_cmd(new_cmd);
2187
Andy Grovere3d6f902011-07-19 08:55:10 +00002188 if (list_empty(&dev->execute_task_list)) {
2189 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002190 return 0;
2191 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002192 task = list_first_entry(&dev->execute_task_list,
2193 struct se_task, t_execute_list);
Christoph Hellwig04629b72011-10-12 11:07:04 -04002194 __transport_remove_task_from_execute_queue(task, dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002195 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002196
Andy Grovere3d6f902011-07-19 08:55:10 +00002197 cmd = task->task_se_cmd;
Andy Grovera1d8b492011-05-02 17:12:10 -07002198 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04002199 task->task_flags |= (TF_ACTIVE | TF_SENT);
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002200 cmd->transport_state |= CMD_T_SENT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002201
Andy Grovera1d8b492011-05-02 17:12:10 -07002202 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002203
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002204 if (cmd->execute_cmd)
2205 error = cmd->execute_cmd(cmd);
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002206 else
2207 error = dev->transport->do_task(task);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002208 if (error != 0) {
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002209 spin_lock_irqsave(&cmd->t_state_lock, flags);
2210 task->task_flags &= ~TF_ACTIVE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002211 cmd->transport_state &= ~CMD_T_SENT;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002212 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002213
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002214 transport_stop_tasks_for_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002215 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002216 }
2217
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002218 new_cmd = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002219 goto check_depth;
2220
2221 return 0;
2222}
2223
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002224static inline u32 transport_get_sectors_6(
2225 unsigned char *cdb,
2226 struct se_cmd *cmd,
2227 int *ret)
2228{
Andy Grover5951146d2011-07-19 10:26:37 +00002229 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002230
2231 /*
2232 * Assume TYPE_DISK for non struct se_device objects.
2233 * Use 8-bit sector value.
2234 */
2235 if (!dev)
2236 goto type_disk;
2237
2238 /*
2239 * Use 24-bit allocation length for TYPE_TAPE.
2240 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002241 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002242 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2243
2244 /*
2245 * Everything else assume TYPE_DISK Sector CDB location.
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002246 * Use 8-bit sector value. SBC-3 says:
2247 *
2248 * A TRANSFER LENGTH field set to zero specifies that 256
2249 * logical blocks shall be written. Any other value
2250 * specifies the number of logical blocks that shall be
2251 * written.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002252 */
2253type_disk:
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002254 return cdb[4] ? : 256;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002255}
2256
2257static inline u32 transport_get_sectors_10(
2258 unsigned char *cdb,
2259 struct se_cmd *cmd,
2260 int *ret)
2261{
Andy Grover5951146d2011-07-19 10:26:37 +00002262 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002263
2264 /*
2265 * Assume TYPE_DISK for non struct se_device objects.
2266 * Use 16-bit sector value.
2267 */
2268 if (!dev)
2269 goto type_disk;
2270
2271 /*
2272 * XXX_10 is not defined in SSC, throw an exception
2273 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002274 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2275 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002276 return 0;
2277 }
2278
2279 /*
2280 * Everything else assume TYPE_DISK Sector CDB location.
2281 * Use 16-bit sector value.
2282 */
2283type_disk:
2284 return (u32)(cdb[7] << 8) + cdb[8];
2285}
2286
2287static inline u32 transport_get_sectors_12(
2288 unsigned char *cdb,
2289 struct se_cmd *cmd,
2290 int *ret)
2291{
Andy Grover5951146d2011-07-19 10:26:37 +00002292 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002293
2294 /*
2295 * Assume TYPE_DISK for non struct se_device objects.
2296 * Use 32-bit sector value.
2297 */
2298 if (!dev)
2299 goto type_disk;
2300
2301 /*
2302 * XXX_12 is not defined in SSC, throw an exception
2303 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002304 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2305 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002306 return 0;
2307 }
2308
2309 /*
2310 * Everything else assume TYPE_DISK Sector CDB location.
2311 * Use 32-bit sector value.
2312 */
2313type_disk:
2314 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2315}
2316
2317static inline u32 transport_get_sectors_16(
2318 unsigned char *cdb,
2319 struct se_cmd *cmd,
2320 int *ret)
2321{
Andy Grover5951146d2011-07-19 10:26:37 +00002322 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002323
2324 /*
2325 * Assume TYPE_DISK for non struct se_device objects.
2326 * Use 32-bit sector value.
2327 */
2328 if (!dev)
2329 goto type_disk;
2330
2331 /*
2332 * Use 24-bit allocation length for TYPE_TAPE.
2333 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002334 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002335 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2336
2337type_disk:
2338 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2339 (cdb[12] << 8) + cdb[13];
2340}
2341
2342/*
2343 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2344 */
2345static inline u32 transport_get_sectors_32(
2346 unsigned char *cdb,
2347 struct se_cmd *cmd,
2348 int *ret)
2349{
2350 /*
2351 * Assume TYPE_DISK for non struct se_device objects.
2352 * Use 32-bit sector value.
2353 */
2354 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2355 (cdb[30] << 8) + cdb[31];
2356
2357}
2358
2359static inline u32 transport_get_size(
2360 u32 sectors,
2361 unsigned char *cdb,
2362 struct se_cmd *cmd)
2363{
Andy Grover5951146d2011-07-19 10:26:37 +00002364 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002365
Andy Grovere3d6f902011-07-19 08:55:10 +00002366 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002367 if (cdb[1] & 1) { /* sectors */
Andy Grovere3d6f902011-07-19 08:55:10 +00002368 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002369 } else /* bytes */
2370 return sectors;
2371 }
Andy Grover8b1e1242012-04-03 15:51:12 -07002372
Andy Grover6708bb22011-06-08 10:36:43 -07002373 pr_debug("Returning block_size: %u, sectors: %u == %u for"
Andy Grover8b1e1242012-04-03 15:51:12 -07002374 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size,
2375 sectors, dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2376 dev->transport->name);
2377
Andy Grovere3d6f902011-07-19 08:55:10 +00002378 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002379}
2380
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002381static void transport_xor_callback(struct se_cmd *cmd)
2382{
2383 unsigned char *buf, *addr;
Andy Groverec98f782011-07-20 19:28:46 +00002384 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002385 unsigned int offset;
2386 int i;
Andy Groverec98f782011-07-20 19:28:46 +00002387 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002388 /*
2389 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2390 *
2391 * 1) read the specified logical block(s);
2392 * 2) transfer logical blocks from the data-out buffer;
2393 * 3) XOR the logical blocks transferred from the data-out buffer with
2394 * the logical blocks read, storing the resulting XOR data in a buffer;
2395 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2396 * blocks transferred from the data-out buffer; and
2397 * 5) transfer the resulting XOR data to the data-in buffer.
2398 */
2399 buf = kmalloc(cmd->data_length, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002400 if (!buf) {
2401 pr_err("Unable to allocate xor_callback buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002402 return;
2403 }
2404 /*
Andy Groverec98f782011-07-20 19:28:46 +00002405 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002406 * into the locally allocated *buf
2407 */
Andy Groverec98f782011-07-20 19:28:46 +00002408 sg_copy_to_buffer(cmd->t_data_sg,
2409 cmd->t_data_nents,
2410 buf,
2411 cmd->data_length);
2412
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002413 /*
2414 * Now perform the XOR against the BIDI read memory located at
Andy Grovera1d8b492011-05-02 17:12:10 -07002415 * cmd->t_mem_bidi_list
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002416 */
2417
2418 offset = 0;
Andy Groverec98f782011-07-20 19:28:46 +00002419 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
Cong Wangca747d62011-11-25 23:14:25 +08002420 addr = kmap_atomic(sg_page(sg));
Andy Groverec98f782011-07-20 19:28:46 +00002421 if (!addr)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002422 goto out;
2423
Andy Groverec98f782011-07-20 19:28:46 +00002424 for (i = 0; i < sg->length; i++)
2425 *(addr + sg->offset + i) ^= *(buf + offset + i);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002426
Andy Groverec98f782011-07-20 19:28:46 +00002427 offset += sg->length;
Cong Wangca747d62011-11-25 23:14:25 +08002428 kunmap_atomic(addr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002429 }
Andy Groverec98f782011-07-20 19:28:46 +00002430
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002431out:
2432 kfree(buf);
2433}
2434
2435/*
2436 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2437 */
2438static int transport_get_sense_data(struct se_cmd *cmd)
2439{
2440 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002441 struct se_device *dev = cmd->se_dev;
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002442 struct se_task *task = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002443 unsigned long flags;
2444 u32 offset = 0;
2445
Andy Grovere3d6f902011-07-19 08:55:10 +00002446 WARN_ON(!cmd->se_lun);
2447
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002448 if (!dev)
2449 return 0;
2450
Andy Grovera1d8b492011-05-02 17:12:10 -07002451 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002452 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002453 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002454 return 0;
2455 }
2456
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002457 task = cmd->t_task;
2458 if (task) {
Christoph Hellwigef804a82011-11-23 06:53:58 -05002459 if (!(task->task_flags & TF_HAS_SENSE))
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002460 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002461
Andy Grovere3d6f902011-07-19 08:55:10 +00002462 if (!dev->transport->get_sense_buffer) {
Andy Grover6708bb22011-06-08 10:36:43 -07002463 pr_err("dev->transport->get_sense_buffer"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002464 " is NULL\n");
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002465 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002466 }
2467
Andy Grovere3d6f902011-07-19 08:55:10 +00002468 sense_buffer = dev->transport->get_sense_buffer(task);
Andy Grover6708bb22011-06-08 10:36:43 -07002469 if (!sense_buffer) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04002470 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002471 " sense buffer for task with sense\n",
Christoph Hellwig04629b72011-10-12 11:07:04 -04002472 cmd->se_tfo->get_task_tag(cmd), task);
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002473 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002474 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002475 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002476
Andy Grovere3d6f902011-07-19 08:55:10 +00002477 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002478 TRANSPORT_SENSE_BUFFER);
2479
Andy Grover5951146d2011-07-19 10:26:37 +00002480 memcpy(&buffer[offset], sense_buffer,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002481 TRANSPORT_SENSE_BUFFER);
2482 cmd->scsi_status = task->task_scsi_status;
2483 /* Automatically padded */
2484 cmd->scsi_sense_length =
2485 (TRANSPORT_SENSE_BUFFER + offset);
2486
Andy Grover6708bb22011-06-08 10:36:43 -07002487 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002488 " and sense\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002489 dev->se_hba->hba_id, dev->transport->name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002490 cmd->scsi_status);
2491 return 0;
2492 }
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002493out:
Andy Grovera1d8b492011-05-02 17:12:10 -07002494 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002495 return -1;
2496}
2497
Andy Groverec98f782011-07-20 19:28:46 +00002498static inline long long transport_dev_end_lba(struct se_device *dev)
2499{
2500 return dev->transport->get_blocks(dev) + 1;
2501}
2502
2503static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2504{
2505 struct se_device *dev = cmd->se_dev;
2506 u32 sectors;
2507
2508 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2509 return 0;
2510
2511 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2512
Andy Grover6708bb22011-06-08 10:36:43 -07002513 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2514 pr_err("LBA: %llu Sectors: %u exceeds"
Andy Groverec98f782011-07-20 19:28:46 +00002515 " transport_dev_end_lba(): %llu\n",
2516 cmd->t_task_lba, sectors,
2517 transport_dev_end_lba(dev));
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002518 return -EINVAL;
Andy Groverec98f782011-07-20 19:28:46 +00002519 }
2520
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002521 return 0;
Andy Groverec98f782011-07-20 19:28:46 +00002522}
2523
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002524static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2525{
2526 /*
2527 * Determine if the received WRITE_SAME is used to for direct
2528 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2529 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2530 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2531 */
2532 int passthrough = (dev->transport->transport_type ==
2533 TRANSPORT_PLUGIN_PHBA_PDEV);
2534
2535 if (!passthrough) {
2536 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2537 pr_err("WRITE_SAME PBDATA and LBDATA"
2538 " bits not supported for Block Discard"
2539 " Emulation\n");
2540 return -ENOSYS;
2541 }
2542 /*
2543 * Currently for the emulated case we only accept
2544 * tpws with the UNMAP=1 bit set.
2545 */
2546 if (!(flags[0] & 0x08)) {
2547 pr_err("WRITE_SAME w/o UNMAP bit not"
2548 " supported for Block Discard Emulation\n");
2549 return -ENOSYS;
2550 }
2551 }
2552
2553 return 0;
2554}
2555
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002556/* transport_generic_cmd_sequencer():
2557 *
2558 * Generic Command Sequencer that should work for most DAS transport
2559 * drivers.
2560 *
Andy Grovera12f41f2012-04-03 15:51:20 -07002561 * Called from target_setup_cmd_from_cdb() in the $FABRIC_MOD
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002562 * RX Thread.
2563 *
2564 * FIXME: Need to support other SCSI OPCODES where as well.
2565 */
2566static int transport_generic_cmd_sequencer(
2567 struct se_cmd *cmd,
2568 unsigned char *cdb)
2569{
Andy Grover5951146d2011-07-19 10:26:37 +00002570 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002571 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2572 int ret = 0, sector_ret = 0, passthrough;
2573 u32 sectors = 0, size = 0, pr_reg_type = 0;
2574 u16 service_action;
2575 u8 alua_ascq = 0;
2576 /*
2577 * Check for an existing UNIT ATTENTION condition
2578 */
2579 if (core_scsi3_ua_check(cmd, cdb) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002580 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2581 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
Andy Grover5951146d2011-07-19 10:26:37 +00002582 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002583 }
2584 /*
2585 * Check status of Asymmetric Logical Unit Assignment port
2586 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002587 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002588 if (ret != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002589 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002590 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002591 * The ALUA additional sense code qualifier (ASCQ) is determined
2592 * by the ALUA primary or secondary access state..
2593 */
2594 if (ret > 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002595 pr_debug("[%s]: ALUA TG Port not available,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002596 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002597 cmd->se_tfo->get_fabric_name(), alua_ascq);
Andy Grover8b1e1242012-04-03 15:51:12 -07002598
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002599 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2600 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2601 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
Andy Grover5951146d2011-07-19 10:26:37 +00002602 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002603 }
2604 goto out_invalid_cdb_field;
2605 }
2606 /*
2607 * Check status for SPC-3 Persistent Reservations
2608 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002609 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2610 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002611 cmd, cdb, pr_reg_type) != 0) {
2612 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2613 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
Nicholas Bellinger087a03b2012-03-13 21:29:06 -07002614 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002615 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2616 return -EBUSY;
2617 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002618 /*
2619 * This means the CDB is allowed for the SCSI Initiator port
2620 * when said port is *NOT* holding the legacy SPC-2 or
2621 * SPC-3 Persistent Reservation.
2622 */
2623 }
2624
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002625 /*
2626 * If we operate in passthrough mode we skip most CDB emulation and
2627 * instead hand the commands down to the physical SCSI device.
2628 */
2629 passthrough =
2630 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2631
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002632 switch (cdb[0]) {
2633 case READ_6:
2634 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2635 if (sector_ret)
2636 goto out_unsupported_cdb;
2637 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002638 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002639 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2640 break;
2641 case READ_10:
2642 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2643 if (sector_ret)
2644 goto out_unsupported_cdb;
2645 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002646 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002647 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2648 break;
2649 case READ_12:
2650 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2651 if (sector_ret)
2652 goto out_unsupported_cdb;
2653 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002654 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002655 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2656 break;
2657 case READ_16:
2658 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2659 if (sector_ret)
2660 goto out_unsupported_cdb;
2661 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002662 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002663 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2664 break;
2665 case WRITE_6:
2666 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2667 if (sector_ret)
2668 goto out_unsupported_cdb;
2669 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002670 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002671 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2672 break;
2673 case WRITE_10:
2674 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2675 if (sector_ret)
2676 goto out_unsupported_cdb;
2677 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002678 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002679 if (cdb[1] & 0x8)
2680 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002681 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2682 break;
2683 case WRITE_12:
2684 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2685 if (sector_ret)
2686 goto out_unsupported_cdb;
2687 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002688 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002689 if (cdb[1] & 0x8)
2690 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002691 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2692 break;
2693 case WRITE_16:
2694 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2695 if (sector_ret)
2696 goto out_unsupported_cdb;
2697 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002698 cmd->t_task_lba = transport_lba_64(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002699 if (cdb[1] & 0x8)
2700 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002701 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2702 break;
2703 case XDWRITEREAD_10:
2704 if ((cmd->data_direction != DMA_TO_DEVICE) ||
Christoph Hellwig33c3faf2011-11-14 11:36:30 -05002705 !(cmd->se_cmd_flags & SCF_BIDI))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002706 goto out_invalid_cdb_field;
2707 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2708 if (sector_ret)
2709 goto out_unsupported_cdb;
2710 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002711 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002712 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002713
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002714 /*
2715 * Do now allow BIDI commands for passthrough mode.
2716 */
2717 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002718 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002719
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002720 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002721 * Setup BIDI XOR callback to be run after I/O completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002722 */
2723 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002724 if (cdb[1] & 0x8)
2725 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002726 break;
2727 case VARIABLE_LENGTH_CMD:
2728 service_action = get_unaligned_be16(&cdb[8]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002729 switch (service_action) {
2730 case XDWRITEREAD_32:
2731 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2732 if (sector_ret)
2733 goto out_unsupported_cdb;
2734 size = transport_get_size(sectors, cdb, cmd);
2735 /*
2736 * Use WRITE_32 and READ_32 opcodes for the emulated
2737 * XDWRITE_READ_32 logic.
2738 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002739 cmd->t_task_lba = transport_lba_64_ext(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002740 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2741
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002742 /*
2743 * Do now allow BIDI commands for passthrough mode.
2744 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002745 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002746 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002747
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002748 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002749 * Setup BIDI XOR callback to be run during after I/O
2750 * completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002751 */
2752 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002753 if (cdb[1] & 0x8)
2754 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002755 break;
2756 case WRITE_SAME_32:
2757 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2758 if (sector_ret)
2759 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002760
Andy Grover6708bb22011-06-08 10:36:43 -07002761 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002762 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002763 else {
2764 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2765 " supported\n");
2766 goto out_invalid_cdb_field;
2767 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002768
Andy Grovera1d8b492011-05-02 17:12:10 -07002769 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002770 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2771
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002772 if (target_check_write_same_discard(&cdb[10], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08002773 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002774 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002775 cmd->execute_cmd = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002776 break;
2777 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002778 pr_err("VARIABLE_LENGTH_CMD service action"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002779 " 0x%04x not supported\n", service_action);
2780 goto out_unsupported_cdb;
2781 }
2782 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002783 case MAINTENANCE_IN:
Andy Grovere3d6f902011-07-19 08:55:10 +00002784 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002785 /* MAINTENANCE_IN from SCC-2 */
2786 /*
2787 * Check for emulated MI_REPORT_TARGET_PGS.
2788 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002789 if (cdb[1] == MI_REPORT_TARGET_PGS &&
2790 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002791 cmd->execute_cmd =
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002792 target_emulate_report_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002793 }
2794 size = (cdb[6] << 24) | (cdb[7] << 16) |
2795 (cdb[8] << 8) | cdb[9];
2796 } else {
2797 /* GPCMD_SEND_KEY from multi media commands */
2798 size = (cdb[8] << 8) + cdb[9];
2799 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002800 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002801 break;
2802 case MODE_SELECT:
2803 size = cdb[4];
2804 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2805 break;
2806 case MODE_SELECT_10:
2807 size = (cdb[7] << 8) + cdb[8];
2808 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2809 break;
2810 case MODE_SENSE:
2811 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002812 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002813 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002814 cmd->execute_cmd = target_emulate_modesense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002815 break;
2816 case MODE_SENSE_10:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002817 size = (cdb[7] << 8) + cdb[8];
2818 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2819 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002820 cmd->execute_cmd = target_emulate_modesense;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002821 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002822 case GPCMD_READ_BUFFER_CAPACITY:
2823 case GPCMD_SEND_OPC:
2824 case LOG_SELECT:
2825 case LOG_SENSE:
2826 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002827 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002828 break;
2829 case READ_BLOCK_LIMITS:
2830 size = READ_BLOCK_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002831 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002832 break;
2833 case GPCMD_GET_CONFIGURATION:
2834 case GPCMD_READ_FORMAT_CAPACITIES:
2835 case GPCMD_READ_DISC_INFO:
2836 case GPCMD_READ_TRACK_RZONE_INFO:
2837 size = (cdb[7] << 8) + cdb[8];
2838 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2839 break;
2840 case PERSISTENT_RESERVE_IN:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002841 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002842 cmd->execute_cmd = target_scsi3_emulate_pr_in;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002843 size = (cdb[7] << 8) + cdb[8];
2844 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2845 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002846 case PERSISTENT_RESERVE_OUT:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002847 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002848 cmd->execute_cmd = target_scsi3_emulate_pr_out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002849 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002850 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002851 break;
2852 case GPCMD_MECHANISM_STATUS:
2853 case GPCMD_READ_DVD_STRUCTURE:
2854 size = (cdb[8] << 8) + cdb[9];
2855 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2856 break;
2857 case READ_POSITION:
2858 size = READ_POSITION_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002859 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002860 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002861 case MAINTENANCE_OUT:
Andy Grovere3d6f902011-07-19 08:55:10 +00002862 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002863 /* MAINTENANCE_OUT from SCC-2
2864 *
2865 * Check for emulated MO_SET_TARGET_PGS.
2866 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002867 if (cdb[1] == MO_SET_TARGET_PGS &&
2868 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002869 cmd->execute_cmd =
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002870 target_emulate_set_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002871 }
2872
2873 size = (cdb[6] << 24) | (cdb[7] << 16) |
2874 (cdb[8] << 8) | cdb[9];
2875 } else {
2876 /* GPCMD_REPORT_KEY from multi media commands */
2877 size = (cdb[8] << 8) + cdb[9];
2878 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002879 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002880 break;
2881 case INQUIRY:
2882 size = (cdb[3] << 8) + cdb[4];
2883 /*
2884 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2885 * See spc4r17 section 5.3
2886 */
Andy Grover5951146d2011-07-19 10:26:37 +00002887 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002888 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002889 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002890 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002891 cmd->execute_cmd = target_emulate_inquiry;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002892 break;
2893 case READ_BUFFER:
2894 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002895 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002896 break;
2897 case READ_CAPACITY:
2898 size = READ_CAP_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002899 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002900 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002901 cmd->execute_cmd = target_emulate_readcapacity;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002902 break;
2903 case READ_MEDIA_SERIAL_NUMBER:
2904 case SECURITY_PROTOCOL_IN:
2905 case SECURITY_PROTOCOL_OUT:
2906 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002907 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002908 break;
2909 case SERVICE_ACTION_IN:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002910 switch (cmd->t_task_cdb[1] & 0x1f) {
2911 case SAI_READ_CAPACITY_16:
2912 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002913 cmd->execute_cmd =
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002914 target_emulate_readcapacity_16;
2915 break;
2916 default:
2917 if (passthrough)
2918 break;
2919
2920 pr_err("Unsupported SA: 0x%02x\n",
2921 cmd->t_task_cdb[1] & 0x1f);
Roland Dreierb168fe82012-03-14 10:40:43 -07002922 goto out_invalid_cdb_field;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002923 }
2924 /*FALLTHROUGH*/
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002925 case ACCESS_CONTROL_IN:
2926 case ACCESS_CONTROL_OUT:
2927 case EXTENDED_COPY:
2928 case READ_ATTRIBUTE:
2929 case RECEIVE_COPY_RESULTS:
2930 case WRITE_ATTRIBUTE:
2931 size = (cdb[10] << 24) | (cdb[11] << 16) |
2932 (cdb[12] << 8) | cdb[13];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002933 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002934 break;
2935 case RECEIVE_DIAGNOSTIC:
2936 case SEND_DIAGNOSTIC:
2937 size = (cdb[3] << 8) | cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002938 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002939 break;
2940/* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2941#if 0
2942 case GPCMD_READ_CD:
2943 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2944 size = (2336 * sectors);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002945 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002946 break;
2947#endif
2948 case READ_TOC:
2949 size = cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002950 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002951 break;
2952 case REQUEST_SENSE:
2953 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002954 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002955 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002956 cmd->execute_cmd = target_emulate_request_sense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002957 break;
2958 case READ_ELEMENT_STATUS:
2959 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002960 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002961 break;
2962 case WRITE_BUFFER:
2963 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002964 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002965 break;
2966 case RESERVE:
2967 case RESERVE_10:
2968 /*
2969 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2970 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2971 */
2972 if (cdb[0] == RESERVE_10)
2973 size = (cdb[7] << 8) | cdb[8];
2974 else
2975 size = cmd->data_length;
2976
2977 /*
2978 * Setup the legacy emulated handler for SPC-2 and
2979 * >= SPC-3 compatible reservation handling (CRH=1)
2980 * Otherwise, we assume the underlying SCSI logic is
2981 * is running in SPC_PASSTHROUGH, and wants reservations
2982 * emulation disabled.
2983 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002984 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04002985 cmd->execute_cmd = target_scsi2_reservation_reserve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002986 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2987 break;
2988 case RELEASE:
2989 case RELEASE_10:
2990 /*
2991 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2992 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2993 */
2994 if (cdb[0] == RELEASE_10)
2995 size = (cdb[7] << 8) | cdb[8];
2996 else
2997 size = cmd->data_length;
2998
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002999 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003000 cmd->execute_cmd = target_scsi2_reservation_release;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003001 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3002 break;
3003 case SYNCHRONIZE_CACHE:
Andy Grover8e94b8d2012-01-16 16:57:07 -08003004 case SYNCHRONIZE_CACHE_16:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003005 /*
3006 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
3007 */
3008 if (cdb[0] == SYNCHRONIZE_CACHE) {
3009 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07003010 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003011 } else {
3012 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07003013 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003014 }
3015 if (sector_ret)
3016 goto out_unsupported_cdb;
3017
3018 size = transport_get_size(sectors, cdb, cmd);
3019 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3020
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003021 if (passthrough)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003022 break;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003023
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003024 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003025 * Check to ensure that LBA + Range does not exceed past end of
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07003026 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003027 */
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07003028 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
3029 if (transport_cmd_get_valid_sectors(cmd) < 0)
3030 goto out_invalid_cdb_field;
3031 }
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003032 cmd->execute_cmd = target_emulate_synchronize_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003033 break;
3034 case UNMAP:
3035 size = get_unaligned_be16(&cdb[7]);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003036 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003037 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003038 cmd->execute_cmd = target_emulate_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003039 break;
3040 case WRITE_SAME_16:
3041 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3042 if (sector_ret)
3043 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07003044
Andy Grover6708bb22011-06-08 10:36:43 -07003045 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07003046 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07003047 else {
3048 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3049 goto out_invalid_cdb_field;
3050 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07003051
Nicholas Bellinger5db07532011-07-27 22:18:52 -07003052 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003053 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003054
3055 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08003056 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003057 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003058 cmd->execute_cmd = target_emulate_write_same;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003059 break;
3060 case WRITE_SAME:
3061 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3062 if (sector_ret)
3063 goto out_unsupported_cdb;
3064
3065 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07003066 size = transport_get_size(1, cdb, cmd);
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003067 else {
3068 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3069 goto out_invalid_cdb_field;
3070 }
3071
3072 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
3073 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3074 /*
3075 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3076 * of byte 1 bit 3 UNMAP instead of original reserved field
3077 */
3078 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08003079 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003080 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003081 cmd->execute_cmd = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003082 break;
3083 case ALLOW_MEDIUM_REMOVAL:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003084 case ERASE:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003085 case REZERO_UNIT:
3086 case SEEK_10:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003087 case SPACE:
3088 case START_STOP:
3089 case TEST_UNIT_READY:
3090 case VERIFY:
3091 case WRITE_FILEMARKS:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003092 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3093 if (!passthrough)
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003094 cmd->execute_cmd = target_emulate_noop;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003095 break;
3096 case GPCMD_CLOSE_TRACK:
3097 case INITIALIZE_ELEMENT_STATUS:
3098 case GPCMD_LOAD_UNLOAD:
3099 case GPCMD_SET_SPEED:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003100 case MOVE_MEDIUM:
3101 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3102 break;
3103 case REPORT_LUNS:
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003104 cmd->execute_cmd = target_report_luns;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003105 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3106 /*
3107 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3108 * See spc4r17 section 5.3
3109 */
Andy Grover5951146d2011-07-19 10:26:37 +00003110 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003111 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003112 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003113 break;
Stefan Hajnoczied0b2142012-04-30 16:35:13 +01003114 case GET_EVENT_STATUS_NOTIFICATION:
3115 size = (cdb[7] << 8) | cdb[8];
3116 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3117 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003118 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003119 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003120 " 0x%02x, sending CHECK_CONDITION.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003121 cmd->se_tfo->get_fabric_name(), cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003122 goto out_unsupported_cdb;
3123 }
3124
Sebastian Andrzej Siewiorb0d79942012-01-10 14:16:59 +01003125 if (cmd->unknown_data_length)
3126 cmd->data_length = size;
3127
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128 if (size != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003129 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003130 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
Andy Grovere3d6f902011-07-19 08:55:10 +00003131 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003132 cmd->data_length, size, cdb[0]);
3133
3134 cmd->cmd_spdtl = size;
3135
3136 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grover6708bb22011-06-08 10:36:43 -07003137 pr_err("Rejecting underflow/overflow"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003138 " WRITE data\n");
3139 goto out_invalid_cdb_field;
3140 }
3141 /*
3142 * Reject READ_* or WRITE_* with overflow/underflow for
3143 * type SCF_SCSI_DATA_SG_IO_CDB.
3144 */
Andy Grover6708bb22011-06-08 10:36:43 -07003145 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3146 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147 " CDB on non 512-byte sector setup subsystem"
Andy Grovere3d6f902011-07-19 08:55:10 +00003148 " plugin: %s\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003149 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3150 goto out_invalid_cdb_field;
3151 }
3152
3153 if (size > cmd->data_length) {
3154 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3155 cmd->residual_count = (size - cmd->data_length);
3156 } else {
3157 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3158 cmd->residual_count = (cmd->data_length - size);
3159 }
3160 cmd->data_length = size;
3161 }
3162
Roland Dreier015487b2012-02-13 16:18:17 -08003163 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB &&
Christoph Hellwigbebe2fd2012-03-26 17:53:17 -04003164 (sectors > dev->se_sub_dev->se_dev_attrib.fabric_max_sectors ||
3165 sectors > dev->se_sub_dev->se_dev_attrib.max_sectors)) {
Roland Dreier015487b2012-02-13 16:18:17 -08003166 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too big sectors %u\n",
3167 cdb[0], sectors);
3168 goto out_invalid_cdb_field;
3169 }
3170
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003171 /* reject any command that we don't have a handler for */
Christoph Hellwig6bb35e02012-04-23 11:35:33 -04003172 if (!(passthrough || cmd->execute_cmd ||
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003173 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3174 goto out_unsupported_cdb;
3175
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003176 transport_set_supported_SAM_opcode(cmd);
3177 return ret;
3178
3179out_unsupported_cdb:
3180 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3181 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover5951146d2011-07-19 10:26:37 +00003182 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003183out_invalid_cdb_field:
3184 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3185 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00003186 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003187}
3188
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003189/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003190 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003191 * and ordered cmds need to have their tasks added to the execution queue.
3192 */
3193static void transport_complete_task_attr(struct se_cmd *cmd)
3194{
Andy Grover5951146d2011-07-19 10:26:37 +00003195 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003196 struct se_cmd *cmd_p, *cmd_tmp;
3197 int new_active_tasks = 0;
3198
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003199 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003200 atomic_dec(&dev->simple_cmds);
3201 smp_mb__after_atomic_dec();
3202 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003203 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003204 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3205 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003206 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003207 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003208 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003209 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3210 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003211 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003212 atomic_dec(&dev->dev_ordered_sync);
3213 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003214
3215 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003216 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003217 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3218 }
3219 /*
3220 * Process all commands up to the last received
3221 * ORDERED task attribute which requires another blocking
3222 * boundary
3223 */
3224 spin_lock(&dev->delayed_cmd_lock);
3225 list_for_each_entry_safe(cmd_p, cmd_tmp,
Andy Grover5951146d2011-07-19 10:26:37 +00003226 &dev->delayed_cmd_list, se_delayed_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003227
Andy Grover5951146d2011-07-19 10:26:37 +00003228 list_del(&cmd_p->se_delayed_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003229 spin_unlock(&dev->delayed_cmd_lock);
3230
Andy Grover6708bb22011-06-08 10:36:43 -07003231 pr_debug("Calling add_tasks() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003232 " cmd_p: 0x%02x Task Attr: 0x%02x"
3233 " Dormant -> Active, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07003234 cmd_p->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003235 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3236
3237 transport_add_tasks_from_cmd(cmd_p);
3238 new_active_tasks++;
3239
3240 spin_lock(&dev->delayed_cmd_lock);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003241 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003242 break;
3243 }
3244 spin_unlock(&dev->delayed_cmd_lock);
3245 /*
3246 * If new tasks have become active, wake up the transport thread
3247 * to do the processing of the Active tasks.
3248 */
3249 if (new_active_tasks != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003250 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003251}
3252
Christoph Hellwige057f532011-10-17 13:56:41 -04003253static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003254{
3255 int ret = 0;
3256
Christoph Hellwige057f532011-10-17 13:56:41 -04003257 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3258 transport_complete_task_attr(cmd);
3259
3260 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3261 ret = cmd->se_tfo->queue_status(cmd);
3262 if (ret)
3263 goto out;
3264 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003265
3266 switch (cmd->data_direction) {
3267 case DMA_FROM_DEVICE:
3268 ret = cmd->se_tfo->queue_data_in(cmd);
3269 break;
3270 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00003271 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003272 ret = cmd->se_tfo->queue_data_in(cmd);
3273 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04003274 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003275 }
3276 /* Fall through for DMA_TO_DEVICE */
3277 case DMA_NONE:
3278 ret = cmd->se_tfo->queue_status(cmd);
3279 break;
3280 default:
3281 break;
3282 }
3283
Christoph Hellwige057f532011-10-17 13:56:41 -04003284out:
3285 if (ret < 0) {
3286 transport_handle_queue_full(cmd, cmd->se_dev);
3287 return;
3288 }
3289 transport_lun_remove_cmd(cmd);
3290 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003291}
3292
3293static void transport_handle_queue_full(
3294 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04003295 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003296{
3297 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003298 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3299 atomic_inc(&dev->dev_qf_count);
3300 smp_mb__after_atomic_inc();
3301 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3302
3303 schedule_work(&cmd->se_dev->qf_work_queue);
3304}
3305
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003306static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003307{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003308 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003309 int reason = 0, ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003310
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003311 /*
3312 * Check if we need to move delayed/dormant tasks from cmds on the
3313 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3314 * Attribute.
3315 */
Andy Grover5951146d2011-07-19 10:26:37 +00003316 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003317 transport_complete_task_attr(cmd);
3318 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003319 * Check to schedule QUEUE_FULL work, or execute an existing
3320 * cmd->transport_qf_callback()
3321 */
3322 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3323 schedule_work(&cmd->se_dev->qf_work_queue);
3324
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003325 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003326 * Check if we need to retrieve a sense buffer from
3327 * the struct se_cmd in question.
3328 */
3329 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3330 if (transport_get_sense_data(cmd) < 0)
3331 reason = TCM_NON_EXISTENT_LUN;
3332
3333 /*
3334 * Only set when an struct se_task->task_scsi_status returned
3335 * a non GOOD status.
3336 */
3337 if (cmd->scsi_status) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003338 ret = transport_send_check_condition_and_sense(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003339 cmd, reason, 1);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003340 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003341 goto queue_full;
3342
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003343 transport_lun_remove_cmd(cmd);
3344 transport_cmd_check_stop_to_fabric(cmd);
3345 return;
3346 }
3347 }
3348 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003349 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003350 * XDWRITE_READ_10 emulation.
3351 */
3352 if (cmd->transport_complete_callback)
3353 cmd->transport_complete_callback(cmd);
3354
3355 switch (cmd->data_direction) {
3356 case DMA_FROM_DEVICE:
3357 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003358 if (cmd->se_lun->lun_sep) {
3359 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003360 cmd->data_length;
3361 }
3362 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003363
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003364 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003365 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003366 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003367 break;
3368 case DMA_TO_DEVICE:
3369 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003370 if (cmd->se_lun->lun_sep) {
3371 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003372 cmd->data_length;
3373 }
3374 spin_unlock(&cmd->se_lun->lun_sep_lock);
3375 /*
3376 * Check if we need to send READ payload for BIDI-COMMAND
3377 */
Andy Groverec98f782011-07-20 19:28:46 +00003378 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003379 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003380 if (cmd->se_lun->lun_sep) {
3381 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003382 cmd->data_length;
3383 }
3384 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003385 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003386 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003387 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003388 break;
3389 }
3390 /* Fall through for DMA_TO_DEVICE */
3391 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003392 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003393 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003394 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003395 break;
3396 default:
3397 break;
3398 }
3399
3400 transport_lun_remove_cmd(cmd);
3401 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003402 return;
3403
3404queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003405 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003406 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04003407 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3408 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003409}
3410
3411static void transport_free_dev_tasks(struct se_cmd *cmd)
3412{
Christoph Hellwig785fdf72012-04-24 00:25:04 -04003413 struct se_task *task;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003414
Christoph Hellwig785fdf72012-04-24 00:25:04 -04003415 task = cmd->t_task;
3416 if (task && !(task->task_flags & TF_ACTIVE))
Christoph Hellwig42bf8292011-10-12 11:07:00 -04003417 cmd->se_dev->transport->free_task(task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003418}
3419
Andy Grover6708bb22011-06-08 10:36:43 -07003420static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003421{
Andy Groverec98f782011-07-20 19:28:46 +00003422 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00003423 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003424
Andy Grover6708bb22011-06-08 10:36:43 -07003425 for_each_sg(sgl, sg, nents, count)
3426 __free_page(sg_page(sg));
3427
3428 kfree(sgl);
3429}
3430
3431static inline void transport_free_pages(struct se_cmd *cmd)
3432{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003433 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07003434 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003435
Andy Grover6708bb22011-06-08 10:36:43 -07003436 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003437 cmd->t_data_sg = NULL;
3438 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003439
Andy Grover6708bb22011-06-08 10:36:43 -07003440 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003441 cmd->t_bidi_data_sg = NULL;
3442 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003443}
3444
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003445/**
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003446 * transport_release_cmd - free a command
3447 * @cmd: command to free
3448 *
3449 * This routine unconditionally frees a command, and reference counting
3450 * or list removal must be done in the caller.
3451 */
3452static void transport_release_cmd(struct se_cmd *cmd)
3453{
3454 BUG_ON(!cmd->se_tfo);
3455
Andy Groverc8e31f22012-01-19 13:39:17 -08003456 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003457 core_tmr_release_req(cmd->se_tmr_req);
3458 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3459 kfree(cmd->t_task_cdb);
3460 /*
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003461 * If this cmd has been setup with target_get_sess_cmd(), drop
3462 * the kref and call ->release_cmd() in kref callback.
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003463 */
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003464 if (cmd->check_release != 0) {
3465 target_put_sess_cmd(cmd->se_sess, cmd);
3466 return;
3467 }
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003468 cmd->se_tfo->release_cmd(cmd);
3469}
3470
3471/**
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003472 * transport_put_cmd - release a reference to a command
3473 * @cmd: command to release
3474 *
3475 * This routine releases our reference to the command and frees it if possible.
3476 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003477static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003478{
3479 unsigned long flags;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003480 int free_tasks = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003481
Andy Grovera1d8b492011-05-02 17:12:10 -07003482 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003483 if (atomic_read(&cmd->t_fe_count)) {
3484 if (!atomic_dec_and_test(&cmd->t_fe_count))
3485 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003486 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003487
3488 if (atomic_read(&cmd->t_se_count)) {
3489 if (!atomic_dec_and_test(&cmd->t_se_count))
3490 goto out_busy;
3491 }
3492
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003493 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
3494 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003495 transport_all_task_dev_remove_state(cmd);
3496 free_tasks = 1;
3497 }
Andy Grovera1d8b492011-05-02 17:12:10 -07003498 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003499
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003500 if (free_tasks != 0)
3501 transport_free_dev_tasks(cmd);
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003502
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003503 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02003504 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003505 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003506out_busy:
3507 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003508}
3509
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003510/*
Andy Groverec98f782011-07-20 19:28:46 +00003511 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3512 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003513 * @cmd: Associated se_cmd descriptor
3514 * @mem: SGL style memory for TCM WRITE / READ
3515 * @sg_mem_num: Number of SGL elements
3516 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3517 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3518 *
3519 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3520 * of parameters.
3521 */
3522int transport_generic_map_mem_to_cmd(
3523 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00003524 struct scatterlist *sgl,
3525 u32 sgl_count,
3526 struct scatterlist *sgl_bidi,
3527 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003528{
Andy Grover5951146d2011-07-19 10:26:37 +00003529 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003530 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003531
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003532 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3533 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
Nicholas Bellingerfef58a62011-11-15 22:13:24 -08003534 /*
3535 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3536 * scatterlists already have been set to follow what the fabric
3537 * passes for the original expected data transfer length.
3538 */
3539 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3540 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3541 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3542 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3543 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3544 return -EINVAL;
3545 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003546
Andy Groverec98f782011-07-20 19:28:46 +00003547 cmd->t_data_sg = sgl;
3548 cmd->t_data_nents = sgl_count;
3549
Andy Grover5951146d2011-07-19 10:26:37 +00003550 if (sgl_bidi && sgl_bidi_count) {
Andy Groverec98f782011-07-20 19:28:46 +00003551 cmd->t_bidi_data_sg = sgl_bidi;
3552 cmd->t_bidi_data_nents = sgl_bidi_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003553 }
3554 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003555 }
3556
3557 return 0;
3558}
3559EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3560
Andy Grover49493142012-01-16 16:57:08 -08003561void *transport_kmap_data_sg(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003562{
Andy Groverec98f782011-07-20 19:28:46 +00003563 struct scatterlist *sg = cmd->t_data_sg;
Andy Grover49493142012-01-16 16:57:08 -08003564 struct page **pages;
3565 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003566
Andy Groverec98f782011-07-20 19:28:46 +00003567 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003568 /*
Andy Groverec98f782011-07-20 19:28:46 +00003569 * We need to take into account a possible offset here for fabrics like
3570 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3571 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00003572 */
Andy Grover49493142012-01-16 16:57:08 -08003573 if (!cmd->t_data_nents)
3574 return NULL;
3575 else if (cmd->t_data_nents == 1)
3576 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003577
Andy Grover49493142012-01-16 16:57:08 -08003578 /* >1 page. use vmap */
3579 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
3580 if (!pages)
3581 return NULL;
3582
3583 /* convert sg[] to pages[] */
3584 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
3585 pages[i] = sg_page(sg);
3586 }
3587
3588 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
3589 kfree(pages);
3590 if (!cmd->t_data_vmap)
3591 return NULL;
3592
3593 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003594}
Andy Grover49493142012-01-16 16:57:08 -08003595EXPORT_SYMBOL(transport_kmap_data_sg);
3596
3597void transport_kunmap_data_sg(struct se_cmd *cmd)
3598{
Andy Grovera1edf9c2012-02-09 12:18:06 -08003599 if (!cmd->t_data_nents) {
Andy Grover49493142012-01-16 16:57:08 -08003600 return;
Andy Grovera1edf9c2012-02-09 12:18:06 -08003601 } else if (cmd->t_data_nents == 1) {
Andy Grover49493142012-01-16 16:57:08 -08003602 kunmap(sg_page(cmd->t_data_sg));
Andy Grovera1edf9c2012-02-09 12:18:06 -08003603 return;
3604 }
Andy Grover49493142012-01-16 16:57:08 -08003605
3606 vunmap(cmd->t_data_vmap);
3607 cmd->t_data_vmap = NULL;
3608}
3609EXPORT_SYMBOL(transport_kunmap_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003610
3611static int
3612transport_generic_get_mem(struct se_cmd *cmd)
3613{
Andy Groverec98f782011-07-20 19:28:46 +00003614 u32 length = cmd->data_length;
3615 unsigned int nents;
3616 struct page *page;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003617 gfp_t zero_flag;
Andy Groverec98f782011-07-20 19:28:46 +00003618 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003619
Andy Groverec98f782011-07-20 19:28:46 +00003620 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3621 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3622 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00003623 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003624
Andy Groverec98f782011-07-20 19:28:46 +00003625 cmd->t_data_nents = nents;
3626 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003627
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003628 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
3629
Andy Groverec98f782011-07-20 19:28:46 +00003630 while (length) {
3631 u32 page_len = min_t(u32, length, PAGE_SIZE);
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003632 page = alloc_page(GFP_KERNEL | zero_flag);
Andy Groverec98f782011-07-20 19:28:46 +00003633 if (!page)
3634 goto out;
3635
3636 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3637 length -= page_len;
3638 i++;
3639 }
3640 return 0;
3641
3642out:
3643 while (i >= 0) {
3644 __free_page(sg_page(&cmd->t_data_sg[i]));
3645 i--;
3646 }
3647 kfree(cmd->t_data_sg);
3648 cmd->t_data_sg = NULL;
3649 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003650}
3651
Andy Grovera1d8b492011-05-02 17:12:10 -07003652/*
Andy Groverb16a35b2012-04-03 15:51:21 -07003653 * Allocate any required resources to execute the command. For writes we
3654 * might not have the payload yet, so notify the fabric via a call to
3655 * ->write_pending instead. Otherwise place it on the execution queue.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003656 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003657int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003658{
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003659 struct se_device *dev = cmd->se_dev;
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003660 struct se_task *task;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003661 int ret = 0;
3662
3663 /*
3664 * Determine is the TCM fabric module has already allocated physical
3665 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00003666 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003667 */
Andy Groverec98f782011-07-20 19:28:46 +00003668 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3669 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00003670 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003671 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003672 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003673 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003674
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003675 /* Workaround for handling zero-length control CDBs */
3676 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3677 !cmd->data_length) {
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003678 spin_lock_irq(&cmd->t_state_lock);
Roland Dreier410f6702011-11-22 13:51:32 -08003679 cmd->t_state = TRANSPORT_COMPLETE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003680 cmd->transport_state |= CMD_T_ACTIVE;
3681 spin_unlock_irq(&cmd->t_state_lock);
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08003682
3683 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
3684 u8 ua_asc = 0, ua_ascq = 0;
3685
3686 core_scsi3_ua_clear_for_request_sense(cmd,
3687 &ua_asc, &ua_ascq);
3688 }
3689
Roland Dreier410f6702011-11-22 13:51:32 -08003690 INIT_WORK(&cmd->work, target_complete_ok_work);
3691 queue_work(target_completion_wq, &cmd->work);
3692 return 0;
3693 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003694
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003695 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3696 struct se_dev_attrib *attr = &dev->se_sub_dev->se_dev_attrib;
3697
3698 if (transport_cmd_get_valid_sectors(cmd) < 0)
3699 return -EINVAL;
3700
3701 BUG_ON(cmd->data_length % attr->block_size);
3702 BUG_ON(DIV_ROUND_UP(cmd->data_length, attr->block_size) >
3703 attr->max_sectors);
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003704 }
3705
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003706 task = dev->transport->alloc_task(cmd->t_task_cdb);
3707 if (!task) {
3708 pr_err("Unable to allocate struct se_task\n");
3709 goto out_fail;
3710 }
3711
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003712 INIT_LIST_HEAD(&task->t_execute_list);
3713 INIT_LIST_HEAD(&task->t_state_list);
3714 init_completion(&task->task_stop_comp);
3715 task->task_se_cmd = cmd;
3716 task->task_data_direction = cmd->data_direction;
3717 task->task_sg = cmd->t_data_sg;
3718 task->task_sg_nents = cmd->t_data_nents;
3719
Christoph Hellwig785fdf72012-04-24 00:25:04 -04003720 cmd->t_task = task;
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04003721
3722 atomic_inc(&cmd->t_fe_count);
3723 atomic_inc(&cmd->t_se_count);
3724
Christoph Hellwig785fdf72012-04-24 00:25:04 -04003725 atomic_set(&cmd->t_task_cdbs_left, 1);
3726 atomic_set(&cmd->t_task_cdbs_ex_left, 1);
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003727
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003728 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07003729 * For WRITEs, let the fabric know its buffer is ready..
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003730 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3731 * will be added to the struct se_device execution queue after its WRITE
3732 * data has arrived. (ie: It gets handled by the transport processing
3733 * thread a second time)
3734 */
3735 if (cmd->data_direction == DMA_TO_DEVICE) {
3736 transport_add_tasks_to_state_queue(cmd);
3737 return transport_generic_write_pending(cmd);
3738 }
3739 /*
3740 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3741 * to the execution queue.
3742 */
3743 transport_execute_tasks(cmd);
3744 return 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003745
3746out_fail:
3747 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3748 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3749 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003750}
Andy Grovera1d8b492011-05-02 17:12:10 -07003751EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003752
3753/* transport_generic_process_write():
3754 *
3755 *
3756 */
3757void transport_generic_process_write(struct se_cmd *cmd)
3758{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003759 transport_execute_tasks(cmd);
3760}
3761EXPORT_SYMBOL(transport_generic_process_write);
3762
Christoph Hellwige057f532011-10-17 13:56:41 -04003763static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003764{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003765 int ret;
3766
3767 ret = cmd->se_tfo->write_pending(cmd);
3768 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04003769 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3770 cmd);
3771 transport_handle_queue_full(cmd, cmd->se_dev);
3772 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003773}
3774
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003775static int transport_generic_write_pending(struct se_cmd *cmd)
3776{
3777 unsigned long flags;
3778 int ret;
3779
Andy Grovera1d8b492011-05-02 17:12:10 -07003780 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003781 cmd->t_state = TRANSPORT_WRITE_PENDING;
Andy Grovera1d8b492011-05-02 17:12:10 -07003782 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003783
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003784 /*
3785 * Clear the se_cmd for WRITE_PENDING status in order to set
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003786 * CMD_T_ACTIVE so that transport_generic_handle_data can be called
3787 * from HW target mode interrupt code. This is safe to be called
3788 * with transport_off=1 before the cmd->se_tfo->write_pending
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003789 * because the se_cmd->se_lun pointer is not being cleared.
3790 */
3791 transport_cmd_check_stop(cmd, 1, 0);
3792
3793 /*
3794 * Call the fabric write_pending function here to let the
3795 * frontend know that WRITE buffers are ready.
3796 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003797 ret = cmd->se_tfo->write_pending(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003798 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003799 goto queue_full;
3800 else if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003801 return ret;
3802
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003803 return 1;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003804
3805queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003806 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003807 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
Christoph Hellwige057f532011-10-17 13:56:41 -04003808 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003809 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003810}
3811
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003812void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003813{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003814 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
Andy Groverc8e31f22012-01-19 13:39:17 -08003815 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003816 transport_wait_for_tasks(cmd);
3817
Christoph Hellwig35462972011-05-31 23:56:57 -04003818 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003819 } else {
3820 if (wait_for_tasks)
3821 transport_wait_for_tasks(cmd);
3822
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003823 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
3824
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02003825 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003826 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003827
Nicholas Bellingerf4366772011-05-19 20:19:11 -07003828 transport_free_dev_tasks(cmd);
3829
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003830 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003831 }
3832}
3833EXPORT_SYMBOL(transport_generic_free_cmd);
3834
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003835/* target_get_sess_cmd - Add command to active ->sess_cmd_list
3836 * @se_sess: session to reference
3837 * @se_cmd: command descriptor to add
Nicholas Bellingera6360782011-11-18 20:36:22 -08003838 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003839 */
Nicholas Bellingera6360782011-11-18 20:36:22 -08003840void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
3841 bool ack_kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003842{
3843 unsigned long flags;
3844
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003845 kref_init(&se_cmd->cmd_kref);
Nicholas Bellingera6360782011-11-18 20:36:22 -08003846 /*
3847 * Add a second kref if the fabric caller is expecting to handle
3848 * fabric acknowledgement that requires two target_put_sess_cmd()
3849 * invocations before se_cmd descriptor release.
3850 */
Nicholas Bellinger86715562012-02-13 01:07:22 -08003851 if (ack_kref == true) {
Nicholas Bellingera6360782011-11-18 20:36:22 -08003852 kref_get(&se_cmd->cmd_kref);
Nicholas Bellinger86715562012-02-13 01:07:22 -08003853 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
3854 }
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003855
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003856 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3857 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
3858 se_cmd->check_release = 1;
3859 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3860}
3861EXPORT_SYMBOL(target_get_sess_cmd);
3862
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003863static void target_release_cmd_kref(struct kref *kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003864{
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003865 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
3866 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003867 unsigned long flags;
3868
3869 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3870 if (list_empty(&se_cmd->se_cmd_list)) {
3871 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Nicholas Bellingerffc32d52012-02-13 02:35:01 -08003872 se_cmd->se_tfo->release_cmd(se_cmd);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003873 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003874 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003875 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
3876 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3877 complete(&se_cmd->cmd_wait_comp);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003878 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003879 }
3880 list_del(&se_cmd->se_cmd_list);
3881 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3882
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003883 se_cmd->se_tfo->release_cmd(se_cmd);
3884}
3885
3886/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
3887 * @se_sess: session to reference
3888 * @se_cmd: command descriptor to drop
3889 */
3890int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3891{
3892 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003893}
3894EXPORT_SYMBOL(target_put_sess_cmd);
3895
3896/* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
3897 * @se_sess: session to split
3898 */
3899void target_splice_sess_cmd_list(struct se_session *se_sess)
3900{
3901 struct se_cmd *se_cmd;
3902 unsigned long flags;
3903
3904 WARN_ON(!list_empty(&se_sess->sess_wait_list));
3905 INIT_LIST_HEAD(&se_sess->sess_wait_list);
3906
3907 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3908 se_sess->sess_tearing_down = 1;
3909
3910 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
3911
3912 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
3913 se_cmd->cmd_wait_set = 1;
3914
3915 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3916}
3917EXPORT_SYMBOL(target_splice_sess_cmd_list);
3918
3919/* target_wait_for_sess_cmds - Wait for outstanding descriptors
3920 * @se_sess: session to wait for active I/O
3921 * @wait_for_tasks: Make extra transport_wait_for_tasks call
3922 */
3923void target_wait_for_sess_cmds(
3924 struct se_session *se_sess,
3925 int wait_for_tasks)
3926{
3927 struct se_cmd *se_cmd, *tmp_cmd;
3928 bool rc = false;
3929
3930 list_for_each_entry_safe(se_cmd, tmp_cmd,
3931 &se_sess->sess_wait_list, se_cmd_list) {
3932 list_del(&se_cmd->se_cmd_list);
3933
3934 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
3935 " %d\n", se_cmd, se_cmd->t_state,
3936 se_cmd->se_tfo->get_cmd_state(se_cmd));
3937
3938 if (wait_for_tasks) {
3939 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
3940 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3941 se_cmd->se_tfo->get_cmd_state(se_cmd));
3942
3943 rc = transport_wait_for_tasks(se_cmd);
3944
3945 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
3946 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3947 se_cmd->se_tfo->get_cmd_state(se_cmd));
3948 }
3949
3950 if (!rc) {
3951 wait_for_completion(&se_cmd->cmd_wait_comp);
3952 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
3953 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3954 se_cmd->se_tfo->get_cmd_state(se_cmd));
3955 }
3956
3957 se_cmd->se_tfo->release_cmd(se_cmd);
3958 }
3959}
3960EXPORT_SYMBOL(target_wait_for_sess_cmds);
3961
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003962/* transport_lun_wait_for_tasks():
3963 *
3964 * Called from ConfigFS context to stop the passed struct se_cmd to allow
3965 * an struct se_lun to be successfully shutdown.
3966 */
3967static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
3968{
3969 unsigned long flags;
3970 int ret;
3971 /*
3972 * If the frontend has already requested this struct se_cmd to
3973 * be stopped, we can safely ignore this struct se_cmd.
3974 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003975 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003976 if (cmd->transport_state & CMD_T_STOP) {
3977 cmd->transport_state &= ~CMD_T_LUN_STOP;
3978
3979 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
3980 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07003981 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003982 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovere3d6f902011-07-19 08:55:10 +00003983 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003984 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003985 cmd->transport_state |= CMD_T_LUN_FE_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07003986 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003987
Andy Grover5951146d2011-07-19 10:26:37 +00003988 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003989
3990 ret = transport_stop_tasks_for_cmd(cmd);
3991
Christoph Hellwig785fdf72012-04-24 00:25:04 -04003992 pr_debug("ConfigFS: cmd: %p stop tasks ret:"
3993 " %d\n", cmd, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003994 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07003995 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003996 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07003997 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07003998 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003999 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004000 }
Christoph Hellwig3df8d402011-10-17 13:56:43 -04004001 transport_remove_cmd_from_queue(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004002
4003 return 0;
4004}
4005
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004006static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4007{
4008 struct se_cmd *cmd = NULL;
4009 unsigned long lun_flags, cmd_flags;
4010 /*
4011 * Do exception processing and return CHECK_CONDITION status to the
4012 * Initiator Port.
4013 */
4014 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00004015 while (!list_empty(&lun->lun_cmd_list)) {
4016 cmd = list_first_entry(&lun->lun_cmd_list,
4017 struct se_cmd, se_lun_node);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -05004018 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004019
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004020 /*
4021 * This will notify iscsi_target_transport.c:
4022 * transport_cmd_check_stop() that a LUN shutdown is in
4023 * progress for the iscsi_cmd_t.
4024 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004025 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07004026 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004027 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004028 cmd->se_lun->unpacked_lun,
4029 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004030 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07004031 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004032
4033 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4034
Andy Grover6708bb22011-06-08 10:36:43 -07004035 if (!cmd->se_lun) {
4036 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004037 cmd->se_tfo->get_task_tag(cmd),
4038 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004039 BUG();
4040 }
4041 /*
4042 * If the Storage engine still owns the iscsi_cmd_t, determine
4043 * and/or stop its context.
4044 */
Andy Grover6708bb22011-06-08 10:36:43 -07004045 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00004046 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4047 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004048
Andy Grovere3d6f902011-07-19 08:55:10 +00004049 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004050 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4051 continue;
4052 }
4053
Andy Grover6708bb22011-06-08 10:36:43 -07004054 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004055 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004056 cmd->se_lun->unpacked_lun,
4057 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004058
Andy Grovera1d8b492011-05-02 17:12:10 -07004059 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004060 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004061 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004062 goto check_cond;
4063 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004064 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004065 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07004066 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004067
4068 transport_free_dev_tasks(cmd);
4069 /*
4070 * The Storage engine stopped this struct se_cmd before it was
4071 * send to the fabric frontend for delivery back to the
4072 * Initiator Node. Return this SCSI CDB back with an
4073 * CHECK_CONDITION status.
4074 */
4075check_cond:
4076 transport_send_check_condition_and_sense(cmd,
4077 TCM_NON_EXISTENT_LUN, 0);
4078 /*
4079 * If the fabric frontend is waiting for this iscsi_cmd_t to
4080 * be released, notify the waiting thread now that LU has
4081 * finished accessing it.
4082 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004083 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004084 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07004085 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004086 " struct se_cmd: %p ITT: 0x%08x\n",
4087 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00004088 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004089
Andy Grovera1d8b492011-05-02 17:12:10 -07004090 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004091 cmd_flags);
4092 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -07004093 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004094 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4095 continue;
4096 }
Andy Grover6708bb22011-06-08 10:36:43 -07004097 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004098 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004099
Andy Grovera1d8b492011-05-02 17:12:10 -07004100 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004101 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4102 }
4103 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4104}
4105
4106static int transport_clear_lun_thread(void *p)
4107{
Jörn Engel8359cf42011-11-24 02:05:51 +01004108 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004109
4110 __transport_clear_lun_from_sessions(lun);
4111 complete(&lun->lun_shutdown_comp);
4112
4113 return 0;
4114}
4115
4116int transport_clear_lun_from_sessions(struct se_lun *lun)
4117{
4118 struct task_struct *kt;
4119
Andy Grover5951146d2011-07-19 10:26:37 +00004120 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004121 "tcm_cl_%u", lun->unpacked_lun);
4122 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07004123 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00004124 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004125 }
4126 wait_for_completion(&lun->lun_shutdown_comp);
4127
4128 return 0;
4129}
4130
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004131/**
4132 * transport_wait_for_tasks - wait for completion to occur
4133 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004134 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004135 * Called from frontend fabric context to wait for storage engine
4136 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004137 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004138bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004139{
4140 unsigned long flags;
4141
Andy Grovera1d8b492011-05-02 17:12:10 -07004142 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08004143 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
4144 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004145 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004146 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004147 }
4148 /*
4149 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4150 * has been set in transport_set_supported_SAM_opcode().
4151 */
Andy Groverc8e31f22012-01-19 13:39:17 -08004152 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
4153 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004154 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004155 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004156 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004157 /*
4158 * If we are already stopped due to an external event (ie: LUN shutdown)
4159 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07004160 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004161 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4162 * has completed its operation on the struct se_cmd.
4163 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004164 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07004165 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00004166 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004167 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004168 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004169 /*
4170 * There is a special case for WRITES where a FE exception +
4171 * LUN shutdown means ConfigFS context is still sleeping on
4172 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4173 * We go ahead and up transport_lun_stop_comp just to be sure
4174 * here.
4175 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004176 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4177 complete(&cmd->transport_lun_stop_comp);
4178 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4179 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004180
4181 transport_all_task_dev_remove_state(cmd);
4182 /*
4183 * At this point, the frontend who was the originator of this
4184 * struct se_cmd, now owns the structure and can be released through
4185 * normal means below.
4186 */
Andy Grover6708bb22011-06-08 10:36:43 -07004187 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00004188 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004189 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004190 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004191
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004192 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004193 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004194
Nicholas Bellinger3d289342012-02-13 02:38:14 -08004195 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004196 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004197 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004198 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004199
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004200 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004201
Andy Grover6708bb22011-06-08 10:36:43 -07004202 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004203 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004204 cmd, cmd->se_tfo->get_task_tag(cmd),
4205 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004206
Andy Grovera1d8b492011-05-02 17:12:10 -07004207 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004208
Andy Grover5951146d2011-07-19 10:26:37 +00004209 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004210
Andy Grovera1d8b492011-05-02 17:12:10 -07004211 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004212
Andy Grovera1d8b492011-05-02 17:12:10 -07004213 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004214 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004215
Andy Grover6708bb22011-06-08 10:36:43 -07004216 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07004217 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004218 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004219
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004220 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004221
4222 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004223}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004224EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004225
4226static int transport_get_sense_codes(
4227 struct se_cmd *cmd,
4228 u8 *asc,
4229 u8 *ascq)
4230{
4231 *asc = cmd->scsi_asc;
4232 *ascq = cmd->scsi_ascq;
4233
4234 return 0;
4235}
4236
4237static int transport_set_sense_codes(
4238 struct se_cmd *cmd,
4239 u8 asc,
4240 u8 ascq)
4241{
4242 cmd->scsi_asc = asc;
4243 cmd->scsi_ascq = ascq;
4244
4245 return 0;
4246}
4247
4248int transport_send_check_condition_and_sense(
4249 struct se_cmd *cmd,
4250 u8 reason,
4251 int from_transport)
4252{
4253 unsigned char *buffer = cmd->sense_buffer;
4254 unsigned long flags;
4255 int offset;
4256 u8 asc = 0, ascq = 0;
4257
Andy Grovera1d8b492011-05-02 17:12:10 -07004258 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004259 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004260 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004261 return 0;
4262 }
4263 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07004264 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004265
4266 if (!reason && from_transport)
4267 goto after_reason;
4268
4269 if (!from_transport)
4270 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4271 /*
4272 * Data Segment and SenseLength of the fabric response PDU.
4273 *
4274 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4275 * from include/scsi/scsi_cmnd.h
4276 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004277 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004278 TRANSPORT_SENSE_BUFFER);
4279 /*
4280 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4281 * SENSE KEY values from include/scsi/scsi.h
4282 */
4283 switch (reason) {
4284 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004285 /* CURRENT ERROR */
4286 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004287 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004288 /* ILLEGAL REQUEST */
4289 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4290 /* LOGICAL UNIT NOT SUPPORTED */
4291 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4292 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004293 case TCM_UNSUPPORTED_SCSI_OPCODE:
4294 case TCM_SECTOR_COUNT_TOO_MANY:
4295 /* CURRENT ERROR */
4296 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004297 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004298 /* ILLEGAL REQUEST */
4299 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4300 /* INVALID COMMAND OPERATION CODE */
4301 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4302 break;
4303 case TCM_UNKNOWN_MODE_PAGE:
4304 /* CURRENT ERROR */
4305 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004306 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004307 /* ILLEGAL REQUEST */
4308 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4309 /* INVALID FIELD IN CDB */
4310 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4311 break;
4312 case TCM_CHECK_CONDITION_ABORT_CMD:
4313 /* CURRENT ERROR */
4314 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004315 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004316 /* ABORTED COMMAND */
4317 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4318 /* BUS DEVICE RESET FUNCTION OCCURRED */
4319 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4320 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4321 break;
4322 case TCM_INCORRECT_AMOUNT_OF_DATA:
4323 /* CURRENT ERROR */
4324 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004325 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004326 /* ABORTED COMMAND */
4327 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4328 /* WRITE ERROR */
4329 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4330 /* NOT ENOUGH UNSOLICITED DATA */
4331 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4332 break;
4333 case TCM_INVALID_CDB_FIELD:
4334 /* CURRENT ERROR */
4335 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004336 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004337 /* ILLEGAL REQUEST */
4338 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004339 /* INVALID FIELD IN CDB */
4340 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4341 break;
4342 case TCM_INVALID_PARAMETER_LIST:
4343 /* CURRENT ERROR */
4344 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004345 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004346 /* ILLEGAL REQUEST */
4347 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004348 /* INVALID FIELD IN PARAMETER LIST */
4349 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4350 break;
4351 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4352 /* CURRENT ERROR */
4353 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004354 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004355 /* ABORTED COMMAND */
4356 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4357 /* WRITE ERROR */
4358 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4359 /* UNEXPECTED_UNSOLICITED_DATA */
4360 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4361 break;
4362 case TCM_SERVICE_CRC_ERROR:
4363 /* CURRENT ERROR */
4364 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004365 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004366 /* ABORTED COMMAND */
4367 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4368 /* PROTOCOL SERVICE CRC ERROR */
4369 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4370 /* N/A */
4371 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4372 break;
4373 case TCM_SNACK_REJECTED:
4374 /* CURRENT ERROR */
4375 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004376 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004377 /* ABORTED COMMAND */
4378 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4379 /* READ ERROR */
4380 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4381 /* FAILED RETRANSMISSION REQUEST */
4382 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4383 break;
4384 case TCM_WRITE_PROTECTED:
4385 /* CURRENT ERROR */
4386 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004387 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004388 /* DATA PROTECT */
4389 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4390 /* WRITE PROTECTED */
4391 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4392 break;
4393 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4394 /* CURRENT ERROR */
4395 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004396 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004397 /* UNIT ATTENTION */
4398 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4399 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4400 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4401 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4402 break;
4403 case TCM_CHECK_CONDITION_NOT_READY:
4404 /* CURRENT ERROR */
4405 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004406 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004407 /* Not Ready */
4408 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4409 transport_get_sense_codes(cmd, &asc, &ascq);
4410 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4411 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4412 break;
4413 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4414 default:
4415 /* CURRENT ERROR */
4416 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004417 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004418 /* ILLEGAL REQUEST */
4419 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4420 /* LOGICAL UNIT COMMUNICATION FAILURE */
4421 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4422 break;
4423 }
4424 /*
4425 * This code uses linux/include/scsi/scsi.h SAM status codes!
4426 */
4427 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4428 /*
4429 * Automatically padded, this value is encoded in the fabric's
4430 * data_length response PDU containing the SCSI defined sense data.
4431 */
4432 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4433
4434after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004435 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004436}
4437EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4438
4439int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4440{
4441 int ret = 0;
4442
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004443 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07004444 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004445 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4446 return 1;
Andy Grover8b1e1242012-04-03 15:51:12 -07004447
Andy Grover6708bb22011-06-08 10:36:43 -07004448 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004449 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07004450 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004451 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07004452
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004453 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00004454 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004455 ret = 1;
4456 }
4457 return ret;
4458}
4459EXPORT_SYMBOL(transport_check_aborted_status);
4460
4461void transport_send_task_abort(struct se_cmd *cmd)
4462{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07004463 unsigned long flags;
4464
4465 spin_lock_irqsave(&cmd->t_state_lock, flags);
4466 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4467 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4468 return;
4469 }
4470 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4471
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004472 /*
4473 * If there are still expected incoming fabric WRITEs, we wait
4474 * until until they have completed before sending a TASK_ABORTED
4475 * response. This response with TASK_ABORTED status will be
4476 * queued back to fabric module by transport_check_aborted_status().
4477 */
4478 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004479 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004480 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004481 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004482 }
4483 }
4484 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
Andy Grover8b1e1242012-04-03 15:51:12 -07004485
Andy Grover6708bb22011-06-08 10:36:43 -07004486 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07004487 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004488 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07004489
Andy Grovere3d6f902011-07-19 08:55:10 +00004490 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004491}
4492
Christoph Hellwige26d99a2011-11-14 12:30:30 -05004493static int transport_generic_do_tmr(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004494{
Andy Grover5951146d2011-07-19 10:26:37 +00004495 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004496 struct se_tmr_req *tmr = cmd->se_tmr_req;
4497 int ret;
4498
4499 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004500 case TMR_ABORT_TASK:
Nicholas Bellinger3d289342012-02-13 02:38:14 -08004501 core_tmr_abort_task(dev, tmr, cmd->se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004502 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004503 case TMR_ABORT_TASK_SET:
4504 case TMR_CLEAR_ACA:
4505 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004506 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4507 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004508 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004509 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4510 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4511 TMR_FUNCTION_REJECTED;
4512 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004513 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004514 tmr->response = TMR_FUNCTION_REJECTED;
4515 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004516 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004517 tmr->response = TMR_FUNCTION_REJECTED;
4518 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004519 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004520 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004521 tmr->function);
4522 tmr->response = TMR_FUNCTION_REJECTED;
4523 break;
4524 }
4525
4526 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00004527 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004528
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04004529 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004530 return 0;
4531}
4532
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004533/* transport_processing_thread():
4534 *
4535 *
4536 */
4537static int transport_processing_thread(void *param)
4538{
Andy Grover5951146d2011-07-19 10:26:37 +00004539 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004540 struct se_cmd *cmd;
Jörn Engel8359cf42011-11-24 02:05:51 +01004541 struct se_device *dev = param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004542
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004543 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004544 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4545 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004546 kthread_should_stop());
4547 if (ret < 0)
4548 goto out;
4549
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004550get_cmd:
Andy Grover5951146d2011-07-19 10:26:37 +00004551 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4552 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004553 continue;
4554
Andy Grover5951146d2011-07-19 10:26:37 +00004555 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02004556 case TRANSPORT_NEW_CMD:
4557 BUG();
4558 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004559 case TRANSPORT_NEW_CMD_MAP:
Andy Grover6708bb22011-06-08 10:36:43 -07004560 if (!cmd->se_tfo->new_cmd_map) {
4561 pr_err("cmd->se_tfo->new_cmd_map is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004562 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4563 BUG();
4564 }
Andy Grovere3d6f902011-07-19 08:55:10 +00004565 ret = cmd->se_tfo->new_cmd_map(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004566 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004567 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004568 break;
4569 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004570 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004571 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004572 transport_generic_request_failure(cmd);
4573 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004574 }
4575 break;
4576 case TRANSPORT_PROCESS_WRITE:
4577 transport_generic_process_write(cmd);
4578 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004579 case TRANSPORT_PROCESS_TMR:
4580 transport_generic_do_tmr(cmd);
4581 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004582 case TRANSPORT_COMPLETE_QF_WP:
Christoph Hellwige057f532011-10-17 13:56:41 -04004583 transport_write_pending_qf(cmd);
4584 break;
4585 case TRANSPORT_COMPLETE_QF_OK:
4586 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004587 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004588 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004589 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4590 "i_state: %d on SE LUN: %u\n",
4591 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00004592 cmd->se_tfo->get_task_tag(cmd),
4593 cmd->se_tfo->get_cmd_state(cmd),
4594 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004595 BUG();
4596 }
4597
4598 goto get_cmd;
4599 }
4600
4601out:
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07004602 WARN_ON(!list_empty(&dev->state_task_list));
4603 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004604 dev->process_thread = NULL;
4605 return 0;
4606}