blob: 757e3777ce792bf9363957bf3daa6c0a2f826774 [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
Christoph Hellwig35e0e752011-10-17 13:56:53 -040058static struct workqueue_struct *target_completion_wq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080059static struct kmem_cache *se_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080060struct kmem_cache *se_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061struct kmem_cache *t10_pr_reg_cache;
62struct kmem_cache *t10_alua_lu_gp_cache;
63struct kmem_cache *t10_alua_lu_gp_mem_cache;
64struct kmem_cache *t10_alua_tg_pt_gp_cache;
65struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
66
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080067static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070068static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040069 struct se_device *dev);
Andy Grover05d1c7c2011-07-20 19:13:28 +000070static int transport_generic_get_mem(struct se_cmd *cmd);
Roland Dreierbc187ea2012-07-16 11:04:40 -070071static int target_get_sess_cmd(struct se_session *, struct se_cmd *, bool);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -070072static void transport_put_cmd(struct se_cmd *cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080073static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
Christoph Hellwig35e0e752011-10-17 13:56:53 -040074static void target_complete_ok_work(struct work_struct *work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080075
Andy Grovere3d6f902011-07-19 08:55:10 +000076int init_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080077{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078 se_sess_cache = kmem_cache_create("se_sess_cache",
79 sizeof(struct se_session), __alignof__(struct se_session),
80 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070081 if (!se_sess_cache) {
82 pr_err("kmem_cache_create() for struct se_session"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083 " failed\n");
Andy Groverc8e31f22012-01-19 13:39:17 -080084 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080085 }
86 se_ua_cache = kmem_cache_create("se_ua_cache",
87 sizeof(struct se_ua), __alignof__(struct se_ua),
88 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070089 if (!se_ua_cache) {
90 pr_err("kmem_cache_create() for struct se_ua failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -040091 goto out_free_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080092 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080093 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
94 sizeof(struct t10_pr_registration),
95 __alignof__(struct t10_pr_registration), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070096 if (!t10_pr_reg_cache) {
97 pr_err("kmem_cache_create() for struct t10_pr_registration"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080098 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -040099 goto out_free_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800100 }
101 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
102 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
103 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700104 if (!t10_alua_lu_gp_cache) {
105 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800106 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400107 goto out_free_pr_reg_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800108 }
109 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
110 sizeof(struct t10_alua_lu_gp_member),
111 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700112 if (!t10_alua_lu_gp_mem_cache) {
113 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800114 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400115 goto out_free_lu_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800116 }
117 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
118 sizeof(struct t10_alua_tg_pt_gp),
119 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700120 if (!t10_alua_tg_pt_gp_cache) {
121 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800122 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400123 goto out_free_lu_gp_mem_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800124 }
125 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
126 "t10_alua_tg_pt_gp_mem_cache",
127 sizeof(struct t10_alua_tg_pt_gp_member),
128 __alignof__(struct t10_alua_tg_pt_gp_member),
129 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700130 if (!t10_alua_tg_pt_gp_mem_cache) {
131 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800132 "mem_t failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400133 goto out_free_tg_pt_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800134 }
135
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400136 target_completion_wq = alloc_workqueue("target_completion",
137 WQ_MEM_RECLAIM, 0);
138 if (!target_completion_wq)
139 goto out_free_tg_pt_gp_mem_cache;
140
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800141 return 0;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400142
143out_free_tg_pt_gp_mem_cache:
144 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
145out_free_tg_pt_gp_cache:
146 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
147out_free_lu_gp_mem_cache:
148 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
149out_free_lu_gp_cache:
150 kmem_cache_destroy(t10_alua_lu_gp_cache);
151out_free_pr_reg_cache:
152 kmem_cache_destroy(t10_pr_reg_cache);
153out_free_ua_cache:
154 kmem_cache_destroy(se_ua_cache);
155out_free_sess_cache:
156 kmem_cache_destroy(se_sess_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800157out:
Andy Grovere3d6f902011-07-19 08:55:10 +0000158 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800159}
160
Andy Grovere3d6f902011-07-19 08:55:10 +0000161void release_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800162{
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400163 destroy_workqueue(target_completion_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800164 kmem_cache_destroy(se_sess_cache);
165 kmem_cache_destroy(se_ua_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800166 kmem_cache_destroy(t10_pr_reg_cache);
167 kmem_cache_destroy(t10_alua_lu_gp_cache);
168 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
169 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
170 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800171}
172
Andy Grovere3d6f902011-07-19 08:55:10 +0000173/* This code ensures unique mib indexes are handed out. */
174static DEFINE_SPINLOCK(scsi_mib_index_lock);
175static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800176
177/*
178 * Allocate a new row index for the entry type specified
179 */
180u32 scsi_get_new_index(scsi_index_t type)
181{
182 u32 new_index;
183
Andy Grovere3d6f902011-07-19 08:55:10 +0000184 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800185
Andy Grovere3d6f902011-07-19 08:55:10 +0000186 spin_lock(&scsi_mib_index_lock);
187 new_index = ++scsi_mib_index[type];
188 spin_unlock(&scsi_mib_index_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800189
190 return new_index;
191}
192
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700193void transport_subsystem_check_init(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800194{
195 int ret;
Andy Grover283669d2012-07-30 15:54:17 -0700196 static int sub_api_initialized;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800197
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700198 if (sub_api_initialized)
199 return;
200
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800201 ret = request_module("target_core_iblock");
202 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700203 pr_err("Unable to load target_core_iblock\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204
205 ret = request_module("target_core_file");
206 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700207 pr_err("Unable to load target_core_file\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800208
209 ret = request_module("target_core_pscsi");
210 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700211 pr_err("Unable to load target_core_pscsi\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800212
Andy Grovere3d6f902011-07-19 08:55:10 +0000213 sub_api_initialized = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214}
215
216struct se_session *transport_init_session(void)
217{
218 struct se_session *se_sess;
219
220 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700221 if (!se_sess) {
222 pr_err("Unable to allocate struct se_session from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800223 " se_sess_cache\n");
224 return ERR_PTR(-ENOMEM);
225 }
226 INIT_LIST_HEAD(&se_sess->sess_list);
227 INIT_LIST_HEAD(&se_sess->sess_acl_list);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700228 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700229 spin_lock_init(&se_sess->sess_cmd_lock);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800230 kref_init(&se_sess->sess_kref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800231
232 return se_sess;
233}
234EXPORT_SYMBOL(transport_init_session);
235
236/*
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700237 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800238 */
239void __transport_register_session(
240 struct se_portal_group *se_tpg,
241 struct se_node_acl *se_nacl,
242 struct se_session *se_sess,
243 void *fabric_sess_ptr)
244{
245 unsigned char buf[PR_REG_ISID_LEN];
246
247 se_sess->se_tpg = se_tpg;
248 se_sess->fabric_sess_ptr = fabric_sess_ptr;
249 /*
250 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
251 *
252 * Only set for struct se_session's that will actually be moving I/O.
253 * eg: *NOT* discovery sessions.
254 */
255 if (se_nacl) {
256 /*
257 * If the fabric module supports an ISID based TransportID,
258 * save this value in binary from the fabric I_T Nexus now.
259 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000260 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800261 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000262 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800263 &buf[0], PR_REG_ISID_LEN);
264 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
265 }
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800266 kref_get(&se_nacl->acl_kref);
267
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800268 spin_lock_irq(&se_nacl->nacl_sess_lock);
269 /*
270 * The se_nacl->nacl_sess pointer will be set to the
271 * last active I_T Nexus for each struct se_node_acl.
272 */
273 se_nacl->nacl_sess = se_sess;
274
275 list_add_tail(&se_sess->sess_acl_list,
276 &se_nacl->acl_sess_list);
277 spin_unlock_irq(&se_nacl->nacl_sess_lock);
278 }
279 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
280
Andy Grover6708bb22011-06-08 10:36:43 -0700281 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000282 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800283}
284EXPORT_SYMBOL(__transport_register_session);
285
286void transport_register_session(
287 struct se_portal_group *se_tpg,
288 struct se_node_acl *se_nacl,
289 struct se_session *se_sess,
290 void *fabric_sess_ptr)
291{
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700292 unsigned long flags;
293
294 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800295 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700296 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800297}
298EXPORT_SYMBOL(transport_register_session);
299
Fengguang Wuecf0dd62012-10-10 07:30:20 +0800300static void target_release_session(struct kref *kref)
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800301{
302 struct se_session *se_sess = container_of(kref,
303 struct se_session, sess_kref);
304 struct se_portal_group *se_tpg = se_sess->se_tpg;
305
306 se_tpg->se_tpg_tfo->close_session(se_sess);
307}
308
309void target_get_session(struct se_session *se_sess)
310{
311 kref_get(&se_sess->sess_kref);
312}
313EXPORT_SYMBOL(target_get_session);
314
Jörn Engel33933a02012-05-11 10:35:08 -0400315void target_put_session(struct se_session *se_sess)
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800316{
Joern Engel41492682012-05-18 13:57:19 -0700317 struct se_portal_group *tpg = se_sess->se_tpg;
318
319 if (tpg->se_tpg_tfo->put_session != NULL) {
320 tpg->se_tpg_tfo->put_session(se_sess);
321 return;
322 }
Jörn Engel33933a02012-05-11 10:35:08 -0400323 kref_put(&se_sess->sess_kref, target_release_session);
Nicholas Bellinger41ac82b2012-02-26 22:22:10 -0800324}
325EXPORT_SYMBOL(target_put_session);
326
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800327static void target_complete_nacl(struct kref *kref)
328{
329 struct se_node_acl *nacl = container_of(kref,
330 struct se_node_acl, acl_kref);
331
332 complete(&nacl->acl_free_comp);
333}
334
335void target_put_nacl(struct se_node_acl *nacl)
336{
337 kref_put(&nacl->acl_kref, target_complete_nacl);
338}
339
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800340void transport_deregister_session_configfs(struct se_session *se_sess)
341{
342 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700343 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800344 /*
345 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
346 */
347 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700348 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700349 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellinger337c0602012-03-10 14:36:21 -0800350 if (se_nacl->acl_stop == 0)
351 list_del(&se_sess->sess_acl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800352 /*
353 * If the session list is empty, then clear the pointer.
354 * Otherwise, set the struct se_session pointer from the tail
355 * element of the per struct se_node_acl active session list.
356 */
357 if (list_empty(&se_nacl->acl_sess_list))
358 se_nacl->nacl_sess = NULL;
359 else {
360 se_nacl->nacl_sess = container_of(
361 se_nacl->acl_sess_list.prev,
362 struct se_session, sess_acl_list);
363 }
Roland Dreier23388862011-06-22 01:02:21 -0700364 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800365 }
366}
367EXPORT_SYMBOL(transport_deregister_session_configfs);
368
369void transport_free_session(struct se_session *se_sess)
370{
371 kmem_cache_free(se_sess_cache, se_sess);
372}
373EXPORT_SYMBOL(transport_free_session);
374
375void transport_deregister_session(struct se_session *se_sess)
376{
377 struct se_portal_group *se_tpg = se_sess->se_tpg;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800378 struct target_core_fabric_ops *se_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800379 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700380 unsigned long flags;
Nicholas Bellinger01468342012-03-10 14:32:52 -0800381 bool comp_nacl = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800382
Andy Grover6708bb22011-06-08 10:36:43 -0700383 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800384 transport_free_session(se_sess);
385 return;
386 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800387 se_tfo = se_tpg->se_tpg_tfo;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800388
Roland Dreiere63a8e12011-08-12 16:01:02 -0700389 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800390 list_del(&se_sess->sess_list);
391 se_sess->se_tpg = NULL;
392 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700393 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800394
395 /*
396 * Determine if we need to do extra work for this initiator node's
397 * struct se_node_acl if it had been previously dynamically generated.
398 */
399 se_nacl = se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800400
Nicholas Bellinger01468342012-03-10 14:32:52 -0800401 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
402 if (se_nacl && se_nacl->dynamic_node_acl) {
403 if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
404 list_del(&se_nacl->acl_list);
405 se_tpg->num_node_acls--;
406 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
407 core_tpg_wait_for_nacl_pr_ref(se_nacl);
408 core_free_device_list_for_node(se_nacl, se_tpg);
409 se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
410
411 comp_nacl = false;
412 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800413 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800414 }
Nicholas Bellinger01468342012-03-10 14:32:52 -0800415 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800416
Andy Grover6708bb22011-06-08 10:36:43 -0700417 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000418 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellinger01468342012-03-10 14:32:52 -0800419 /*
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800420 * If last kref is dropping now for an explict NodeACL, awake sleeping
421 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
422 * removal context.
Nicholas Bellinger01468342012-03-10 14:32:52 -0800423 */
424 if (se_nacl && comp_nacl == true)
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800425 target_put_nacl(se_nacl);
Nicholas Bellinger01468342012-03-10 14:32:52 -0800426
Nicholas Bellingerafb999f2012-03-08 23:45:02 -0800427 transport_free_session(se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800428}
429EXPORT_SYMBOL(transport_deregister_session);
430
431/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700432 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800433 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400434static void target_remove_from_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800435{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400436 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800437 unsigned long flags;
438
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400439 if (!dev)
440 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800441
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400442 if (cmd->transport_state & CMD_T_BUSY)
443 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800444
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400445 spin_lock_irqsave(&dev->execute_task_lock, flags);
446 if (cmd->state_active) {
447 list_del(&cmd->state_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400448 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800449 }
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400450 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451}
452
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400453static int transport_cmd_check_stop(struct se_cmd *cmd, bool remove_from_lists)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454{
455 unsigned long flags;
456
Andy Grovera1d8b492011-05-02 17:12:10 -0700457 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800458 /*
459 * Determine if IOCTL context caller in requesting the stopping of this
460 * command for LUN shutdown purposes.
461 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500462 if (cmd->transport_state & CMD_T_LUN_STOP) {
463 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
464 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800465
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500466 cmd->transport_state &= ~CMD_T_ACTIVE;
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400467 if (remove_from_lists)
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400468 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700469 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800470
Andy Grovera1d8b492011-05-02 17:12:10 -0700471 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800472 return 1;
473 }
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400474
475 if (remove_from_lists) {
476 target_remove_from_state_list(cmd);
477
478 /*
479 * Clear struct se_cmd->se_lun before the handoff to FE.
480 */
481 cmd->se_lun = NULL;
482 }
483
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800484 /*
485 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000486 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800487 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500488 if (cmd->transport_state & CMD_T_STOP) {
489 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
490 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000491 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800492
Andy Grovera1d8b492011-05-02 17:12:10 -0700493 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800494
Andy Grovera1d8b492011-05-02 17:12:10 -0700495 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800496 return 1;
497 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800498
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400499 cmd->transport_state &= ~CMD_T_ACTIVE;
500 if (remove_from_lists) {
501 /*
502 * Some fabric modules like tcm_loop can release
503 * their internally allocated I/O reference now and
504 * struct se_cmd now.
505 *
506 * Fabric modules are expected to return '1' here if the
507 * se_cmd being passed is released at this point,
508 * or zero if not being released.
509 */
510 if (cmd->se_tfo->check_stop_free != NULL) {
511 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
512 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800513 }
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400514 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800515
Andy Grovera1d8b492011-05-02 17:12:10 -0700516 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800517 return 0;
518}
519
520static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
521{
Christoph Hellwigf7113a42012-07-08 15:58:38 -0400522 return transport_cmd_check_stop(cmd, true);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800523}
524
525static void transport_lun_remove_cmd(struct se_cmd *cmd)
526{
Andy Grovere3d6f902011-07-19 08:55:10 +0000527 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800528 unsigned long flags;
529
530 if (!lun)
531 return;
532
Andy Grovera1d8b492011-05-02 17:12:10 -0700533 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500534 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
535 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400536 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800537 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700538 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800539
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800540 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500541 if (!list_empty(&cmd->se_lun_node))
542 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800543 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
544}
545
546void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
547{
Andy Groverc8e31f22012-01-19 13:39:17 -0800548 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700549 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800550
551 if (transport_cmd_check_stop_to_fabric(cmd))
552 return;
Christoph Hellwigaf877292012-07-08 15:58:49 -0400553 if (remove)
Christoph Hellwige6a25732011-09-13 23:08:50 +0200554 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800555}
556
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400557static void target_complete_failure_work(struct work_struct *work)
558{
559 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
560
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700561 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400562}
563
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200564/*
Paolo Bonzinid5829ea2012-09-05 17:09:15 +0200565 * Used when asking transport to copy Sense Data from the underlying
566 * Linux/SCSI struct scsi_cmnd
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200567 */
Paolo Bonzinid5829ea2012-09-05 17:09:15 +0200568static unsigned char *transport_get_sense_buffer(struct se_cmd *cmd)
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200569{
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200570 struct se_device *dev = cmd->se_dev;
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200571
572 WARN_ON(!cmd->se_lun);
573
574 if (!dev)
Paolo Bonzinid5829ea2012-09-05 17:09:15 +0200575 return NULL;
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200576
Paolo Bonzinid5829ea2012-09-05 17:09:15 +0200577 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION)
578 return NULL;
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200579
Roland Dreier9c58b7d2012-08-15 14:35:25 -0700580 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200581
Paolo Bonzinid5829ea2012-09-05 17:09:15 +0200582 pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n",
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200583 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
Roland Dreier9c58b7d2012-08-15 14:35:25 -0700584 return cmd->sense_buffer;
Paolo Bonzini6138ed22012-09-05 17:09:13 +0200585}
586
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400587void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400589 struct se_device *dev = cmd->se_dev;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400590 int success = scsi_status == GOOD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800591 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800592
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400593 cmd->scsi_status = scsi_status;
594
595
Andy Grovera1d8b492011-05-02 17:12:10 -0700596 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400597 cmd->transport_state &= ~CMD_T_BUSY;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800598
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800599 if (dev && dev->transport->transport_complete) {
Paolo Bonzinid5829ea2012-09-05 17:09:15 +0200600 dev->transport->transport_complete(cmd,
601 cmd->t_data_sg,
602 transport_get_sense_buffer(cmd));
603 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800604 success = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800605 }
606
607 /*
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400608 * See if we are waiting to complete for an exception condition.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400610 if (cmd->transport_state & CMD_T_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700611 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400612 complete(&cmd->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800613 return;
614 }
Christoph Hellwig22350072011-11-02 05:06:35 -0700615
616 if (!success)
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500617 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig22350072011-11-02 05:06:35 -0700618
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800619 /*
620 * Check for case where an explict ABORT_TASK has been received
621 * and transport_wait_for_tasks() will be waiting for completion..
622 */
623 if (cmd->transport_state & CMD_T_ABORTED &&
624 cmd->transport_state & CMD_T_STOP) {
625 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
626 complete(&cmd->t_transport_stop_comp);
627 return;
628 } else if (cmd->transport_state & CMD_T_FAILED) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500629 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400630 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631 } else {
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400632 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800633 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400634
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400635 cmd->t_state = TRANSPORT_COMPLETE;
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800636 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
Andy Grovera1d8b492011-05-02 17:12:10 -0700637 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800638
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400639 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800640}
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400641EXPORT_SYMBOL(target_complete_cmd);
642
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400643static void target_add_to_state_list(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800644{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400645 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800646 unsigned long flags;
647
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800648 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -0400649 if (!cmd->state_active) {
650 list_add_tail(&cmd->state_list, &dev->state_list);
651 cmd->state_active = true;
652 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800653 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800654}
655
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700656/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700657 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700658 */
Christoph Hellwig7a6f0a12012-07-08 15:58:47 -0400659static void transport_write_pending_qf(struct se_cmd *cmd);
660static void transport_complete_qf(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700661
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400662void target_qf_do_work(struct work_struct *work)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700663{
664 struct se_device *dev = container_of(work, struct se_device,
665 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700666 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700667 struct se_cmd *cmd, *cmd_tmp;
668
669 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700670 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
671 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700672
Roland Dreierbcac3642011-08-27 21:33:16 -0700673 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700674 list_del(&cmd->se_qf_node);
675 atomic_dec(&dev->dev_qf_count);
676 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700677
Andy Grover6708bb22011-06-08 10:36:43 -0700678 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700679 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400680 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700681 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
682 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400683
Christoph Hellwig7a6f0a12012-07-08 15:58:47 -0400684 if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP)
685 transport_write_pending_qf(cmd);
686 else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK)
687 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700688 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700689}
690
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800691unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
692{
693 switch (cmd->data_direction) {
694 case DMA_NONE:
695 return "NONE";
696 case DMA_FROM_DEVICE:
697 return "READ";
698 case DMA_TO_DEVICE:
699 return "WRITE";
700 case DMA_BIDIRECTIONAL:
701 return "BIDI";
702 default:
703 break;
704 }
705
706 return "UNKNOWN";
707}
708
709void transport_dump_dev_state(
710 struct se_device *dev,
711 char *b,
712 int *bl)
713{
714 *bl += sprintf(b + *bl, "Status: ");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400715 if (dev->export_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800716 *bl += sprintf(b + *bl, "ACTIVATED");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400717 else
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800718 *bl += sprintf(b + *bl, "DEACTIVATED");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800719
Christoph Hellwig5f41a312012-05-20 14:34:44 -0400720 *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth);
Nicholas Bellinger11e764b2012-05-09 12:42:09 -0700721 *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400722 dev->dev_attrib.block_size,
723 dev->dev_attrib.hw_max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800724 *bl += sprintf(b + *bl, " ");
725}
726
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800727void transport_dump_vpd_proto_id(
728 struct t10_vpd *vpd,
729 unsigned char *p_buf,
730 int p_buf_len)
731{
732 unsigned char buf[VPD_TMP_BUF_SIZE];
733 int len;
734
735 memset(buf, 0, VPD_TMP_BUF_SIZE);
736 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
737
738 switch (vpd->protocol_identifier) {
739 case 0x00:
740 sprintf(buf+len, "Fibre Channel\n");
741 break;
742 case 0x10:
743 sprintf(buf+len, "Parallel SCSI\n");
744 break;
745 case 0x20:
746 sprintf(buf+len, "SSA\n");
747 break;
748 case 0x30:
749 sprintf(buf+len, "IEEE 1394\n");
750 break;
751 case 0x40:
752 sprintf(buf+len, "SCSI Remote Direct Memory Access"
753 " Protocol\n");
754 break;
755 case 0x50:
756 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
757 break;
758 case 0x60:
759 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
760 break;
761 case 0x70:
762 sprintf(buf+len, "Automation/Drive Interface Transport"
763 " Protocol\n");
764 break;
765 case 0x80:
766 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
767 break;
768 default:
769 sprintf(buf+len, "Unknown 0x%02x\n",
770 vpd->protocol_identifier);
771 break;
772 }
773
774 if (p_buf)
775 strncpy(p_buf, buf, p_buf_len);
776 else
Andy Grover6708bb22011-06-08 10:36:43 -0700777 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800778}
779
780void
781transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
782{
783 /*
784 * Check if the Protocol Identifier Valid (PIV) bit is set..
785 *
786 * from spc3r23.pdf section 7.5.1
787 */
788 if (page_83[1] & 0x80) {
789 vpd->protocol_identifier = (page_83[0] & 0xf0);
790 vpd->protocol_identifier_set = 1;
791 transport_dump_vpd_proto_id(vpd, NULL, 0);
792 }
793}
794EXPORT_SYMBOL(transport_set_vpd_proto_id);
795
796int transport_dump_vpd_assoc(
797 struct t10_vpd *vpd,
798 unsigned char *p_buf,
799 int p_buf_len)
800{
801 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +0000802 int ret = 0;
803 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800804
805 memset(buf, 0, VPD_TMP_BUF_SIZE);
806 len = sprintf(buf, "T10 VPD Identifier Association: ");
807
808 switch (vpd->association) {
809 case 0x00:
810 sprintf(buf+len, "addressed logical unit\n");
811 break;
812 case 0x10:
813 sprintf(buf+len, "target port\n");
814 break;
815 case 0x20:
816 sprintf(buf+len, "SCSI target device\n");
817 break;
818 default:
819 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +0000820 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800821 break;
822 }
823
824 if (p_buf)
825 strncpy(p_buf, buf, p_buf_len);
826 else
Andy Grover6708bb22011-06-08 10:36:43 -0700827 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800828
829 return ret;
830}
831
832int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
833{
834 /*
835 * The VPD identification association..
836 *
837 * from spc3r23.pdf Section 7.6.3.1 Table 297
838 */
839 vpd->association = (page_83[1] & 0x30);
840 return transport_dump_vpd_assoc(vpd, NULL, 0);
841}
842EXPORT_SYMBOL(transport_set_vpd_assoc);
843
844int transport_dump_vpd_ident_type(
845 struct t10_vpd *vpd,
846 unsigned char *p_buf,
847 int p_buf_len)
848{
849 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +0000850 int ret = 0;
851 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800852
853 memset(buf, 0, VPD_TMP_BUF_SIZE);
854 len = sprintf(buf, "T10 VPD Identifier Type: ");
855
856 switch (vpd->device_identifier_type) {
857 case 0x00:
858 sprintf(buf+len, "Vendor specific\n");
859 break;
860 case 0x01:
861 sprintf(buf+len, "T10 Vendor ID based\n");
862 break;
863 case 0x02:
864 sprintf(buf+len, "EUI-64 based\n");
865 break;
866 case 0x03:
867 sprintf(buf+len, "NAA\n");
868 break;
869 case 0x04:
870 sprintf(buf+len, "Relative target port identifier\n");
871 break;
872 case 0x08:
873 sprintf(buf+len, "SCSI name string\n");
874 break;
875 default:
876 sprintf(buf+len, "Unsupported: 0x%02x\n",
877 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +0000878 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800879 break;
880 }
881
Andy Grovere3d6f902011-07-19 08:55:10 +0000882 if (p_buf) {
883 if (p_buf_len < strlen(buf)+1)
884 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800885 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +0000886 } else {
Andy Grover6708bb22011-06-08 10:36:43 -0700887 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +0000888 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800889
890 return ret;
891}
892
893int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
894{
895 /*
896 * The VPD identifier type..
897 *
898 * from spc3r23.pdf Section 7.6.3.1 Table 298
899 */
900 vpd->device_identifier_type = (page_83[1] & 0x0f);
901 return transport_dump_vpd_ident_type(vpd, NULL, 0);
902}
903EXPORT_SYMBOL(transport_set_vpd_ident_type);
904
905int transport_dump_vpd_ident(
906 struct t10_vpd *vpd,
907 unsigned char *p_buf,
908 int p_buf_len)
909{
910 unsigned char buf[VPD_TMP_BUF_SIZE];
911 int ret = 0;
912
913 memset(buf, 0, VPD_TMP_BUF_SIZE);
914
915 switch (vpd->device_identifier_code_set) {
916 case 0x01: /* Binary */
917 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
918 &vpd->device_identifier[0]);
919 break;
920 case 0x02: /* ASCII */
921 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
922 &vpd->device_identifier[0]);
923 break;
924 case 0x03: /* UTF-8 */
925 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
926 &vpd->device_identifier[0]);
927 break;
928 default:
929 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
930 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +0000931 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800932 break;
933 }
934
935 if (p_buf)
936 strncpy(p_buf, buf, p_buf_len);
937 else
Andy Grover6708bb22011-06-08 10:36:43 -0700938 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800939
940 return ret;
941}
942
943int
944transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
945{
946 static const char hex_str[] = "0123456789abcdef";
Masanari Iida35d1efe2012-08-16 22:43:13 +0900947 int j = 0, i = 4; /* offset to start of the identifier */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800948
949 /*
950 * The VPD Code Set (encoding)
951 *
952 * from spc3r23.pdf Section 7.6.3.1 Table 296
953 */
954 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
955 switch (vpd->device_identifier_code_set) {
956 case 0x01: /* Binary */
957 vpd->device_identifier[j++] =
958 hex_str[vpd->device_identifier_type];
959 while (i < (4 + page_83[3])) {
960 vpd->device_identifier[j++] =
961 hex_str[(page_83[i] & 0xf0) >> 4];
962 vpd->device_identifier[j++] =
963 hex_str[page_83[i] & 0x0f];
964 i++;
965 }
966 break;
967 case 0x02: /* ASCII */
968 case 0x03: /* UTF-8 */
969 while (i < (4 + page_83[3]))
970 vpd->device_identifier[j++] = page_83[i++];
971 break;
972 default:
973 break;
974 }
975
976 return transport_dump_vpd_ident(vpd, NULL, 0);
977}
978EXPORT_SYMBOL(transport_set_vpd_ident);
979
Christoph Hellwig1fd032e2012-05-20 11:59:15 -0400980int target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
Christoph Hellwig9b3b8042012-05-20 11:59:12 -0400981{
982 struct se_device *dev = cmd->se_dev;
983
984 if (cmd->unknown_data_length) {
985 cmd->data_length = size;
986 } else if (size != cmd->data_length) {
987 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
988 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
989 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
990 cmd->data_length, size, cmd->t_task_cdb[0]);
991
Christoph Hellwig9b3b8042012-05-20 11:59:12 -0400992 if (cmd->data_direction == DMA_TO_DEVICE) {
993 pr_err("Rejecting underflow/overflow"
994 " WRITE data\n");
995 goto out_invalid_cdb_field;
996 }
997 /*
998 * Reject READ_* or WRITE_* with overflow/underflow for
999 * type SCF_SCSI_DATA_CDB.
1000 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001001 if (dev->dev_attrib.block_size != 512) {
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001002 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1003 " CDB on non 512-byte sector setup subsystem"
1004 " plugin: %s\n", dev->transport->name);
1005 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1006 goto out_invalid_cdb_field;
1007 }
Nicholas Bellinger4c054ba2012-08-16 15:33:10 -07001008 /*
1009 * For the overflow case keep the existing fabric provided
1010 * ->data_length. Otherwise for the underflow case, reset
1011 * ->data_length to the smaller SCSI expected data transfer
1012 * length.
1013 */
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001014 if (size > cmd->data_length) {
1015 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
1016 cmd->residual_count = (size - cmd->data_length);
1017 } else {
1018 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
1019 cmd->residual_count = (cmd->data_length - size);
Nicholas Bellinger4c054ba2012-08-16 15:33:10 -07001020 cmd->data_length = size;
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001021 }
Christoph Hellwig9b3b8042012-05-20 11:59:12 -04001022 }
1023
1024 return 0;
1025
1026out_invalid_cdb_field:
1027 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1028 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1029 return -EINVAL;
1030}
1031
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001032/*
1033 * Used by fabric modules containing a local struct se_cmd within their
1034 * fabric dependent per I/O descriptor.
1035 */
1036void transport_init_se_cmd(
1037 struct se_cmd *cmd,
1038 struct target_core_fabric_ops *tfo,
1039 struct se_session *se_sess,
1040 u32 data_length,
1041 int data_direction,
1042 int task_attr,
1043 unsigned char *sense_buffer)
1044{
Andy Grover5951146d2011-07-19 10:26:37 +00001045 INIT_LIST_HEAD(&cmd->se_lun_node);
1046 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001047 INIT_LIST_HEAD(&cmd->se_qf_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001048 INIT_LIST_HEAD(&cmd->se_cmd_list);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001049 INIT_LIST_HEAD(&cmd->state_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001050 init_completion(&cmd->transport_lun_fe_stop_comp);
1051 init_completion(&cmd->transport_lun_stop_comp);
1052 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001053 init_completion(&cmd->cmd_wait_comp);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001054 init_completion(&cmd->task_stop_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001055 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001056 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001057
1058 cmd->se_tfo = tfo;
1059 cmd->se_sess = se_sess;
1060 cmd->data_length = data_length;
1061 cmd->data_direction = data_direction;
1062 cmd->sam_task_attr = task_attr;
1063 cmd->sense_buffer = sense_buffer;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001064
1065 cmd->state_active = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001066}
1067EXPORT_SYMBOL(transport_init_se_cmd);
1068
1069static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1070{
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001071 struct se_device *dev = cmd->se_dev;
1072
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001073 /*
1074 * Check if SAM Task Attribute emulation is enabled for this
1075 * struct se_device storage object
1076 */
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001077 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001078 return 0;
1079
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001080 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001081 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001082 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001083 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001084 }
1085 /*
1086 * Used to determine when ORDERED commands should go from
1087 * Dormant to Active status.
1088 */
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001089 cmd->se_ordered_id = atomic_inc_return(&dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001090 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001091 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001092 cmd->se_ordered_id, cmd->sam_task_attr,
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001093 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001094 return 0;
1095}
1096
Andy Grovera12f41f2012-04-03 15:51:20 -07001097/* target_setup_cmd_from_cdb():
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001098 *
1099 * Called from fabric RX Thread.
1100 */
Andy Grovera12f41f2012-04-03 15:51:20 -07001101int target_setup_cmd_from_cdb(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001102 struct se_cmd *cmd,
1103 unsigned char *cdb)
1104{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001105 struct se_device *dev = cmd->se_dev;
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001106 u32 pr_reg_type = 0;
1107 u8 alua_ascq = 0;
1108 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001109 int ret;
1110
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001111 /*
1112 * Ensure that the received CDB is less than the max (252 + 8) bytes
1113 * for VARIABLE_LENGTH_CMD
1114 */
1115 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001116 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001117 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1118 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001119 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1120 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001121 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001122 }
1123 /*
1124 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1125 * allocate the additional extended CDB buffer now.. Otherwise
1126 * setup the pointer from __t_task_cdb to t_task_cdb.
1127 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001128 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1129 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001130 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001131 if (!cmd->t_task_cdb) {
1132 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001133 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001134 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001135 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001136 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1137 cmd->scsi_sense_reason =
1138 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001139 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001140 }
1141 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001142 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001143 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001144 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001145 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001146 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001147
1148 /*
1149 * Check for an existing UNIT ATTENTION condition
1150 */
1151 if (core_scsi3_ua_check(cmd, cdb) < 0) {
1152 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1153 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
1154 return -EINVAL;
1155 }
1156
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001157 ret = dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001158 if (ret != 0) {
1159 /*
1160 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
1161 * The ALUA additional sense code qualifier (ASCQ) is determined
1162 * by the ALUA primary or secondary access state..
1163 */
1164 if (ret > 0) {
1165 pr_debug("[%s]: ALUA TG Port not available, "
1166 "SenseKey: NOT_READY, ASC/ASCQ: "
1167 "0x04/0x%02x\n",
1168 cmd->se_tfo->get_fabric_name(), alua_ascq);
1169
1170 transport_set_sense_codes(cmd, 0x04, alua_ascq);
1171 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1172 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
1173 return -EINVAL;
1174 }
1175 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1176 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1177 return -EINVAL;
1178 }
1179
1180 /*
1181 * Check status for SPC-3 Persistent Reservations
1182 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001183 if (dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type)) {
1184 if (dev->t10_pr.pr_ops.t10_seq_non_holder(
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001185 cmd, cdb, pr_reg_type) != 0) {
1186 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1187 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
1188 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1189 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
1190 return -EBUSY;
1191 }
1192 /*
1193 * This means the CDB is allowed for the SCSI Initiator port
1194 * when said port is *NOT* holding the legacy SPC-2 or
1195 * SPC-3 Persistent Reservation.
1196 */
1197 }
1198
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001199 ret = dev->transport->parse_cdb(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001200 if (ret < 0)
1201 return ret;
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04001202
1203 spin_lock_irqsave(&cmd->t_state_lock, flags);
1204 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1205 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1206
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001207 /*
1208 * Check for SAM Task Attribute Emulation
1209 */
1210 if (transport_check_alloc_task_attr(cmd) < 0) {
1211 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1212 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001213 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001214 }
1215 spin_lock(&cmd->se_lun->lun_sep_lock);
1216 if (cmd->se_lun->lun_sep)
1217 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1218 spin_unlock(&cmd->se_lun->lun_sep_lock);
1219 return 0;
1220}
Andy Grovera12f41f2012-04-03 15:51:20 -07001221EXPORT_SYMBOL(target_setup_cmd_from_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001222
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001223/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001224 * Used by fabric module frontends to queue tasks directly.
1225 * Many only be used from process context only
1226 */
1227int transport_handle_cdb_direct(
1228 struct se_cmd *cmd)
1229{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001230 int ret;
1231
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001232 if (!cmd->se_lun) {
1233 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001234 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001235 return -EINVAL;
1236 }
1237 if (in_interrupt()) {
1238 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001239 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001240 " from interrupt context\n");
1241 return -EINVAL;
1242 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001243 /*
Christoph Hellwigaf877292012-07-08 15:58:49 -04001244 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
1245 * outstanding descriptors are handled correctly during shutdown via
1246 * transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001247 *
1248 * Also, we don't take cmd->t_state_lock here as we only expect
1249 * this to be called for initial descriptor submission.
1250 */
1251 cmd->t_state = TRANSPORT_NEW_CMD;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001252 cmd->transport_state |= CMD_T_ACTIVE;
1253
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001254 /*
1255 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1256 * so follow TRANSPORT_NEW_CMD processing thread context usage
1257 * and call transport_generic_request_failure() if necessary..
1258 */
1259 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001260 if (ret < 0)
1261 transport_generic_request_failure(cmd);
1262
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001263 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001264}
1265EXPORT_SYMBOL(transport_handle_cdb_direct);
1266
Nicholas Bellingera0267572012-10-01 17:23:22 -07001267/*
1268 * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized
1269 * se_cmd + use pre-allocated SGL memory.
Nicholas Bellingera6360782011-11-18 20:36:22 -08001270 *
1271 * @se_cmd: command descriptor to submit
1272 * @se_sess: associated se_sess for endpoint
1273 * @cdb: pointer to SCSI CDB
1274 * @sense: pointer to SCSI sense buffer
1275 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1276 * @data_length: fabric expected data transfer length
1277 * @task_addr: SAM task attribute
1278 * @data_dir: DMA data direction
1279 * @flags: flags for command submission from target_sc_flags_tables
Nicholas Bellingera0267572012-10-01 17:23:22 -07001280 * @sgl: struct scatterlist memory for unidirectional mapping
1281 * @sgl_count: scatterlist count for unidirectional mapping
1282 * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping
1283 * @sgl_bidi_count: scatterlist count for bidirectional READ mapping
Nicholas Bellingera6360782011-11-18 20:36:22 -08001284 *
Roland Dreierd6dfc862012-07-16 11:04:39 -07001285 * Returns non zero to signal active I/O shutdown failure. All other
1286 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1287 * but still return zero here.
1288 *
Nicholas Bellingera6360782011-11-18 20:36:22 -08001289 * This may only be called from process context, and also currently
1290 * assumes internal allocation of fabric payload buffer by target-core.
Nicholas Bellingera0267572012-10-01 17:23:22 -07001291 */
1292int target_submit_cmd_map_sgls(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001293 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
Nicholas Bellingera0267572012-10-01 17:23:22 -07001294 u32 data_length, int task_attr, int data_dir, int flags,
1295 struct scatterlist *sgl, u32 sgl_count,
1296 struct scatterlist *sgl_bidi, u32 sgl_bidi_count)
Nicholas Bellingera6360782011-11-18 20:36:22 -08001297{
1298 struct se_portal_group *se_tpg;
1299 int rc;
1300
1301 se_tpg = se_sess->se_tpg;
1302 BUG_ON(!se_tpg);
1303 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1304 BUG_ON(in_interrupt());
1305 /*
1306 * Initialize se_cmd for target operation. From this point
1307 * exceptions are handled by sending exception status via
1308 * target_core_fabric_ops->queue_status() callback
1309 */
1310 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1311 data_length, data_dir, task_attr, sense);
Sebastian Andrzej Siewiorb0d79942012-01-10 14:16:59 +01001312 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1313 se_cmd->unknown_data_length = 1;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001314 /*
1315 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1316 * se_sess->sess_cmd_list. A second kref_get here is necessary
1317 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1318 * kref_put() to happen during fabric packet acknowledgement.
1319 */
Roland Dreierbc187ea2012-07-16 11:04:40 -07001320 rc = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1321 if (rc)
Roland Dreierd6dfc862012-07-16 11:04:39 -07001322 return rc;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001323 /*
1324 * Signal bidirectional data payloads to target-core
1325 */
1326 if (flags & TARGET_SCF_BIDI_OP)
1327 se_cmd->se_cmd_flags |= SCF_BIDI;
1328 /*
1329 * Locate se_lun pointer and attach it to struct se_cmd
1330 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001331 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1332 transport_send_check_condition_and_sense(se_cmd,
1333 se_cmd->scsi_sense_reason, 0);
1334 target_put_sess_cmd(se_sess, se_cmd);
Roland Dreierd6dfc862012-07-16 11:04:39 -07001335 return 0;
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001336 }
Christoph Hellwigd6e01752012-05-20 11:59:14 -04001337
Andy Grovera12f41f2012-04-03 15:51:20 -07001338 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001339 if (rc != 0) {
1340 transport_generic_request_failure(se_cmd);
Roland Dreierd6dfc862012-07-16 11:04:39 -07001341 return 0;
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001342 }
Nicholas Bellingera0267572012-10-01 17:23:22 -07001343 /*
1344 * When a non zero sgl_count has been passed perform SGL passthrough
1345 * mapping for pre-allocated fabric memory instead of having target
1346 * core perform an internal SGL allocation..
1347 */
1348 if (sgl_count != 0) {
1349 BUG_ON(!sgl);
Andy Grover11e319e2012-04-03 15:51:28 -07001350
Nicholas Bellinger944981c2012-10-02 14:00:33 -07001351 /*
1352 * A work-around for tcm_loop as some userspace code via
1353 * scsi-generic do not memset their associated read buffers,
1354 * so go ahead and do that here for type non-data CDBs. Also
1355 * note that this is currently guaranteed to be a single SGL
1356 * for this case by target core in target_setup_cmd_from_cdb()
1357 * -> transport_generic_cmd_sequencer().
1358 */
1359 if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
1360 se_cmd->data_direction == DMA_FROM_DEVICE) {
1361 unsigned char *buf = NULL;
1362
1363 if (sgl)
1364 buf = kmap(sg_page(sgl)) + sgl->offset;
1365
1366 if (buf) {
1367 memset(buf, 0, sgl->length);
1368 kunmap(sg_page(sgl));
1369 }
1370 }
1371
Nicholas Bellingera0267572012-10-01 17:23:22 -07001372 rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count,
1373 sgl_bidi, sgl_bidi_count);
1374 if (rc != 0) {
1375 transport_generic_request_failure(se_cmd);
1376 return 0;
1377 }
1378 }
Andy Grover11e319e2012-04-03 15:51:28 -07001379 /*
1380 * Check if we need to delay processing because of ALUA
1381 * Active/NonOptimized primary access state..
1382 */
1383 core_alua_check_nonop_delay(se_cmd);
1384
Nicholas Bellingera6360782011-11-18 20:36:22 -08001385 transport_handle_cdb_direct(se_cmd);
Roland Dreierd6dfc862012-07-16 11:04:39 -07001386 return 0;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001387}
Nicholas Bellingera0267572012-10-01 17:23:22 -07001388EXPORT_SYMBOL(target_submit_cmd_map_sgls);
1389
1390/*
1391 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1392 *
1393 * @se_cmd: command descriptor to submit
1394 * @se_sess: associated se_sess for endpoint
1395 * @cdb: pointer to SCSI CDB
1396 * @sense: pointer to SCSI sense buffer
1397 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1398 * @data_length: fabric expected data transfer length
1399 * @task_addr: SAM task attribute
1400 * @data_dir: DMA data direction
1401 * @flags: flags for command submission from target_sc_flags_tables
1402 *
1403 * Returns non zero to signal active I/O shutdown failure. All other
1404 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1405 * but still return zero here.
1406 *
1407 * This may only be called from process context, and also currently
1408 * assumes internal allocation of fabric payload buffer by target-core.
1409 *
1410 * It also assumes interal target core SGL memory allocation.
1411 */
1412int target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
1413 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1414 u32 data_length, int task_attr, int data_dir, int flags)
1415{
1416 return target_submit_cmd_map_sgls(se_cmd, se_sess, cdb, sense,
1417 unpacked_lun, data_length, task_attr, data_dir,
1418 flags, NULL, 0, NULL, 0);
1419}
Nicholas Bellingera6360782011-11-18 20:36:22 -08001420EXPORT_SYMBOL(target_submit_cmd);
1421
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001422static void target_complete_tmr_failure(struct work_struct *work)
1423{
1424 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1425
1426 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1427 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001428}
1429
Andy Groverea98d7f2012-01-19 13:39:21 -08001430/**
1431 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1432 * for TMR CDBs
1433 *
1434 * @se_cmd: command descriptor to submit
1435 * @se_sess: associated se_sess for endpoint
1436 * @sense: pointer to SCSI sense buffer
1437 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1438 * @fabric_context: fabric context for TMR req
1439 * @tm_type: Type of TM request
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001440 * @gfp: gfp type for caller
1441 * @tag: referenced task tag for TMR_ABORT_TASK
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001442 * @flags: submit cmd flags
Andy Groverea98d7f2012-01-19 13:39:21 -08001443 *
1444 * Callable from all contexts.
1445 **/
1446
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001447int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
Andy Groverea98d7f2012-01-19 13:39:21 -08001448 unsigned char *sense, u32 unpacked_lun,
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001449 void *fabric_tmr_ptr, unsigned char tm_type,
1450 gfp_t gfp, unsigned int tag, int flags)
Andy Groverea98d7f2012-01-19 13:39:21 -08001451{
1452 struct se_portal_group *se_tpg;
1453 int ret;
1454
1455 se_tpg = se_sess->se_tpg;
1456 BUG_ON(!se_tpg);
1457
1458 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1459 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001460 /*
1461 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1462 * allocation failure.
1463 */
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001464 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001465 if (ret < 0)
1466 return -ENOMEM;
Andy Groverea98d7f2012-01-19 13:39:21 -08001467
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001468 if (tm_type == TMR_ABORT_TASK)
1469 se_cmd->se_tmr_req->ref_task_tag = tag;
1470
Andy Groverea98d7f2012-01-19 13:39:21 -08001471 /* See target_submit_cmd for commentary */
Roland Dreierbc187ea2012-07-16 11:04:40 -07001472 ret = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1473 if (ret) {
1474 core_tmr_release_req(se_cmd->se_tmr_req);
1475 return ret;
1476 }
Andy Groverea98d7f2012-01-19 13:39:21 -08001477
Andy Groverea98d7f2012-01-19 13:39:21 -08001478 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1479 if (ret) {
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001480 /*
1481 * For callback during failure handling, push this work off
1482 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1483 */
1484 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1485 schedule_work(&se_cmd->work);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001486 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001487 }
1488 transport_generic_handle_tmr(se_cmd);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001489 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001490}
1491EXPORT_SYMBOL(target_submit_tmr);
1492
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001493/*
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001494 * If the cmd is active, request it to be stopped and sleep until it
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001495 * has completed.
1496 */
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001497bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001498{
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001499 bool was_active = false;
1500
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001501 if (cmd->transport_state & CMD_T_BUSY) {
1502 cmd->transport_state |= CMD_T_REQUEST_STOP;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001503 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1504
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001505 pr_debug("cmd %p waiting to complete\n", cmd);
1506 wait_for_completion(&cmd->task_stop_comp);
1507 pr_debug("cmd %p stopped successfully\n", cmd);
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001508
1509 spin_lock_irqsave(&cmd->t_state_lock, *flags);
Christoph Hellwigcf572a92012-04-24 00:25:05 -04001510 cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1511 cmd->transport_state &= ~CMD_T_BUSY;
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001512 was_active = true;
1513 }
1514
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001515 return was_active;
1516}
1517
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001518/*
1519 * Handle SAM-esque emulation for generic transport request failures.
1520 */
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001521void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001522{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001523 int ret = 0;
1524
Andy Grover6708bb22011-06-08 10:36:43 -07001525 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001526 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001527 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001528 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001529 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001530 cmd->t_state, cmd->scsi_sense_reason);
Christoph Hellwigd43d6ae2012-04-24 00:25:08 -04001531 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001532 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1533 (cmd->transport_state & CMD_T_STOP) != 0,
1534 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001535
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001536 /*
1537 * For SAM Task Attribute emulation for failed struct se_cmd
1538 */
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001539 transport_complete_task_attr(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001540
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001541 switch (cmd->scsi_sense_reason) {
1542 case TCM_NON_EXISTENT_LUN:
1543 case TCM_UNSUPPORTED_SCSI_OPCODE:
1544 case TCM_INVALID_CDB_FIELD:
1545 case TCM_INVALID_PARAMETER_LIST:
1546 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1547 case TCM_UNKNOWN_MODE_PAGE:
1548 case TCM_WRITE_PROTECTED:
Roland Dreiere2397c72012-07-16 15:34:21 -07001549 case TCM_ADDRESS_OUT_OF_RANGE:
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001550 case TCM_CHECK_CONDITION_ABORT_CMD:
1551 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1552 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001553 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001554 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001555 /*
1556 * No SENSE Data payload for this case, set SCSI Status
1557 * and queue the response to $FABRIC_MOD.
1558 *
1559 * Uses linux/include/scsi/scsi.h SAM status codes defs
1560 */
1561 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1562 /*
1563 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1564 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1565 * CONFLICT STATUS.
1566 *
1567 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1568 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001569 if (cmd->se_sess &&
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001570 cmd->se_dev->dev_attrib.emulate_ua_intlck_ctrl == 2)
Andy Grovere3d6f902011-07-19 08:55:10 +00001571 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001572 cmd->orig_fe_lun, 0x2C,
1573 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1574
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001575 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001576 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001577 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001578 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001580 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001581 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001582 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1583 break;
1584 }
Christoph Hellwigf3146432012-07-08 15:58:48 -04001585
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001586 ret = transport_send_check_condition_and_sense(cmd,
1587 cmd->scsi_sense_reason, 0);
1588 if (ret == -EAGAIN || ret == -ENOMEM)
1589 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001590
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001591check_stop:
1592 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001593 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001594 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001595 return;
1596
1597queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001598 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1599 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001600}
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001601EXPORT_SYMBOL(transport_generic_request_failure);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001602
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001603static void __target_execute_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001604{
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -04001605 int error = 0;
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001606
1607 spin_lock_irq(&cmd->t_state_lock);
1608 cmd->transport_state |= (CMD_T_BUSY|CMD_T_SENT);
1609 spin_unlock_irq(&cmd->t_state_lock);
1610
1611 if (cmd->execute_cmd)
1612 error = cmd->execute_cmd(cmd);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001613
1614 if (error) {
1615 spin_lock_irq(&cmd->t_state_lock);
1616 cmd->transport_state &= ~(CMD_T_BUSY|CMD_T_SENT);
1617 spin_unlock_irq(&cmd->t_state_lock);
1618
1619 transport_generic_request_failure(cmd);
1620 }
1621}
1622
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001623static bool target_handle_task_attr(struct se_cmd *cmd)
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001624{
1625 struct se_device *dev = cmd->se_dev;
1626
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001627 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1628 return false;
1629
1630 /*
1631 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1632 * to allow the passed struct se_cmd list of tasks to the front of the list.
1633 */
1634 switch (cmd->sam_task_attr) {
1635 case MSG_HEAD_TAG:
1636 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1637 "se_ordered_id: %u\n",
1638 cmd->t_task_cdb[0], cmd->se_ordered_id);
1639 return false;
1640 case MSG_ORDERED_TAG:
1641 atomic_inc(&dev->dev_ordered_sync);
1642 smp_mb__after_atomic_inc();
1643
1644 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1645 " se_ordered_id: %u\n",
1646 cmd->t_task_cdb[0], cmd->se_ordered_id);
1647
1648 /*
1649 * Execute an ORDERED command if no other older commands
1650 * exist that need to be completed first.
1651 */
1652 if (!atomic_read(&dev->simple_cmds))
1653 return false;
1654 break;
1655 default:
1656 /*
1657 * For SIMPLE and UNTAGGED Task Attribute commands
1658 */
1659 atomic_inc(&dev->simple_cmds);
1660 smp_mb__after_atomic_inc();
1661 break;
1662 }
1663
1664 if (atomic_read(&dev->dev_ordered_sync) == 0)
1665 return false;
1666
1667 spin_lock(&dev->delayed_cmd_lock);
1668 list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list);
1669 spin_unlock(&dev->delayed_cmd_lock);
1670
1671 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1672 " delayed CMD list, se_ordered_id: %u\n",
1673 cmd->t_task_cdb[0], cmd->sam_task_attr,
1674 cmd->se_ordered_id);
1675 return true;
1676}
1677
1678void target_execute_cmd(struct se_cmd *cmd)
1679{
Christoph Hellwigf7113a42012-07-08 15:58:38 -04001680 /*
Christoph Hellwigd59a02b2012-07-08 15:58:40 -04001681 * If the received CDB has aleady been aborted stop processing it here.
1682 */
1683 if (transport_check_aborted_status(cmd, 1))
1684 return;
1685
1686 /*
Christoph Hellwigf7113a42012-07-08 15:58:38 -04001687 * Determine if IOCTL context caller in requesting the stopping of this
1688 * command for LUN shutdown purposes.
1689 */
1690 spin_lock_irq(&cmd->t_state_lock);
1691 if (cmd->transport_state & CMD_T_LUN_STOP) {
1692 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
1693 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
1694
1695 cmd->transport_state &= ~CMD_T_ACTIVE;
1696 spin_unlock_irq(&cmd->t_state_lock);
1697 complete(&cmd->transport_lun_stop_comp);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001698 return;
Christoph Hellwigf7113a42012-07-08 15:58:38 -04001699 }
1700 /*
1701 * Determine if frontend context caller is requesting the stopping of
1702 * this command for frontend exceptions.
1703 */
1704 if (cmd->transport_state & CMD_T_STOP) {
1705 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
1706 __func__, __LINE__,
1707 cmd->se_tfo->get_task_tag(cmd));
1708
1709 spin_unlock_irq(&cmd->t_state_lock);
1710 complete(&cmd->t_transport_stop_comp);
1711 return;
1712 }
1713
1714 cmd->t_state = TRANSPORT_PROCESSING;
1715 spin_unlock_irq(&cmd->t_state_lock);
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001716
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001717 if (!target_handle_task_attr(cmd))
1718 __target_execute_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001719}
Christoph Hellwig70baf0a2012-07-08 15:58:39 -04001720EXPORT_SYMBOL(target_execute_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001721
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001722/*
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001723 * Process all commands up to the last received ORDERED task attribute which
1724 * requires another blocking boundary
1725 */
1726static void target_restart_delayed_cmds(struct se_device *dev)
1727{
1728 for (;;) {
1729 struct se_cmd *cmd;
1730
1731 spin_lock(&dev->delayed_cmd_lock);
1732 if (list_empty(&dev->delayed_cmd_list)) {
1733 spin_unlock(&dev->delayed_cmd_lock);
1734 break;
1735 }
1736
1737 cmd = list_entry(dev->delayed_cmd_list.next,
1738 struct se_cmd, se_delayed_node);
1739 list_del(&cmd->se_delayed_node);
1740 spin_unlock(&dev->delayed_cmd_lock);
1741
1742 __target_execute_cmd(cmd);
1743
1744 if (cmd->sam_task_attr == MSG_ORDERED_TAG)
1745 break;
1746 }
1747}
1748
1749/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04001750 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001751 * and ordered cmds need to have their tasks added to the execution queue.
1752 */
1753static void transport_complete_task_attr(struct se_cmd *cmd)
1754{
Andy Grover5951146d2011-07-19 10:26:37 +00001755 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001756
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001757 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1758 return;
1759
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001760 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001761 atomic_dec(&dev->simple_cmds);
1762 smp_mb__after_atomic_dec();
1763 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07001764 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001765 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
1766 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001767 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001768 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07001769 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001770 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
1771 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001772 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001773 atomic_dec(&dev->dev_ordered_sync);
1774 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001775
1776 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07001777 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001778 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
1779 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001780
Christoph Hellwig5f41a312012-05-20 14:34:44 -04001781 target_restart_delayed_cmds(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001782}
1783
Christoph Hellwige057f532011-10-17 13:56:41 -04001784static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001785{
1786 int ret = 0;
1787
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001788 transport_complete_task_attr(cmd);
Christoph Hellwige057f532011-10-17 13:56:41 -04001789
1790 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
1791 ret = cmd->se_tfo->queue_status(cmd);
1792 if (ret)
1793 goto out;
1794 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001795
1796 switch (cmd->data_direction) {
1797 case DMA_FROM_DEVICE:
1798 ret = cmd->se_tfo->queue_data_in(cmd);
1799 break;
1800 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00001801 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001802 ret = cmd->se_tfo->queue_data_in(cmd);
1803 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04001804 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001805 }
1806 /* Fall through for DMA_TO_DEVICE */
1807 case DMA_NONE:
1808 ret = cmd->se_tfo->queue_status(cmd);
1809 break;
1810 default:
1811 break;
1812 }
1813
Christoph Hellwige057f532011-10-17 13:56:41 -04001814out:
1815 if (ret < 0) {
1816 transport_handle_queue_full(cmd, cmd->se_dev);
1817 return;
1818 }
1819 transport_lun_remove_cmd(cmd);
1820 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001821}
1822
1823static void transport_handle_queue_full(
1824 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04001825 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001826{
1827 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001828 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
1829 atomic_inc(&dev->dev_qf_count);
1830 smp_mb__after_atomic_inc();
1831 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
1832
1833 schedule_work(&cmd->se_dev->qf_work_queue);
1834}
1835
Christoph Hellwig35e0e752011-10-17 13:56:53 -04001836static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001837{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04001838 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Paolo Bonzini27a27092012-09-05 17:09:14 +02001839 int ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04001840
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001841 /*
1842 * Check if we need to move delayed/dormant tasks from cmds on the
1843 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
1844 * Attribute.
1845 */
Christoph Hellwig019c4ca2012-10-10 17:37:14 -04001846 transport_complete_task_attr(cmd);
1847
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001848 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001849 * Check to schedule QUEUE_FULL work, or execute an existing
1850 * cmd->transport_qf_callback()
1851 */
1852 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
1853 schedule_work(&cmd->se_dev->qf_work_queue);
1854
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001855 /*
Paolo Bonzinid5829ea2012-09-05 17:09:15 +02001856 * Check if we need to send a sense buffer from
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001857 * the struct se_cmd in question.
1858 */
1859 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
Paolo Bonzini27a27092012-09-05 17:09:14 +02001860 WARN_ON(!cmd->scsi_status);
Paolo Bonzini27a27092012-09-05 17:09:14 +02001861 ret = transport_send_check_condition_and_sense(
1862 cmd, 0, 1);
1863 if (ret == -EAGAIN || ret == -ENOMEM)
1864 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001865
Paolo Bonzini27a27092012-09-05 17:09:14 +02001866 transport_lun_remove_cmd(cmd);
1867 transport_cmd_check_stop_to_fabric(cmd);
1868 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001869 }
1870 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001871 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001872 * XDWRITE_READ_10 emulation.
1873 */
1874 if (cmd->transport_complete_callback)
1875 cmd->transport_complete_callback(cmd);
1876
1877 switch (cmd->data_direction) {
1878 case DMA_FROM_DEVICE:
1879 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001880 if (cmd->se_lun->lun_sep) {
1881 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001882 cmd->data_length;
1883 }
1884 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001885
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001886 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001887 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001888 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001889 break;
1890 case DMA_TO_DEVICE:
1891 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001892 if (cmd->se_lun->lun_sep) {
1893 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001894 cmd->data_length;
1895 }
1896 spin_unlock(&cmd->se_lun->lun_sep_lock);
1897 /*
1898 * Check if we need to send READ payload for BIDI-COMMAND
1899 */
Andy Groverec98f782011-07-20 19:28:46 +00001900 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001901 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001902 if (cmd->se_lun->lun_sep) {
1903 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001904 cmd->data_length;
1905 }
1906 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001907 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001908 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001909 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001910 break;
1911 }
1912 /* Fall through for DMA_TO_DEVICE */
1913 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001914 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001915 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001916 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001917 break;
1918 default:
1919 break;
1920 }
1921
1922 transport_lun_remove_cmd(cmd);
1923 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001924 return;
1925
1926queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07001927 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001928 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04001929 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1930 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001931}
1932
Andy Grover6708bb22011-06-08 10:36:43 -07001933static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001934{
Andy Groverec98f782011-07-20 19:28:46 +00001935 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00001936 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001937
Andy Grover6708bb22011-06-08 10:36:43 -07001938 for_each_sg(sgl, sg, nents, count)
1939 __free_page(sg_page(sg));
1940
1941 kfree(sgl);
1942}
1943
1944static inline void transport_free_pages(struct se_cmd *cmd)
1945{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001946 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07001947 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948
Andy Grover6708bb22011-06-08 10:36:43 -07001949 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00001950 cmd->t_data_sg = NULL;
1951 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001952
Andy Grover6708bb22011-06-08 10:36:43 -07001953 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00001954 cmd->t_bidi_data_sg = NULL;
1955 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956}
1957
Christoph Hellwigd3df7822011-09-13 23:08:32 +02001958/**
Christoph Hellwige26d99a2011-11-14 12:30:30 -05001959 * transport_release_cmd - free a command
1960 * @cmd: command to free
1961 *
1962 * This routine unconditionally frees a command, and reference counting
1963 * or list removal must be done in the caller.
1964 */
1965static void transport_release_cmd(struct se_cmd *cmd)
1966{
1967 BUG_ON(!cmd->se_tfo);
1968
Andy Groverc8e31f22012-01-19 13:39:17 -08001969 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Christoph Hellwige26d99a2011-11-14 12:30:30 -05001970 core_tmr_release_req(cmd->se_tmr_req);
1971 if (cmd->t_task_cdb != cmd->__t_task_cdb)
1972 kfree(cmd->t_task_cdb);
1973 /*
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08001974 * If this cmd has been setup with target_get_sess_cmd(), drop
1975 * the kref and call ->release_cmd() in kref callback.
Christoph Hellwige26d99a2011-11-14 12:30:30 -05001976 */
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08001977 if (cmd->check_release != 0) {
1978 target_put_sess_cmd(cmd->se_sess, cmd);
1979 return;
1980 }
Christoph Hellwige26d99a2011-11-14 12:30:30 -05001981 cmd->se_tfo->release_cmd(cmd);
1982}
1983
1984/**
Christoph Hellwigd3df7822011-09-13 23:08:32 +02001985 * transport_put_cmd - release a reference to a command
1986 * @cmd: command to release
1987 *
1988 * This routine releases our reference to the command and frees it if possible.
1989 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07001990static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001991{
1992 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001993
Andy Grovera1d8b492011-05-02 17:12:10 -07001994 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02001995 if (atomic_read(&cmd->t_fe_count)) {
1996 if (!atomic_dec_and_test(&cmd->t_fe_count))
1997 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001998 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02001999
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002000 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
2001 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002002 target_remove_from_state_list(cmd);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002003 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002004 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002005
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002006 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02002007 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002008 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02002009out_busy:
2010 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002011}
2012
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002013/*
Andy Groverec98f782011-07-20 19:28:46 +00002014 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
2015 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002016 * @cmd: Associated se_cmd descriptor
2017 * @mem: SGL style memory for TCM WRITE / READ
2018 * @sg_mem_num: Number of SGL elements
2019 * @mem_bidi_in: SGL style memory for TCM BIDI READ
2020 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
2021 *
2022 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
2023 * of parameters.
2024 */
2025int transport_generic_map_mem_to_cmd(
2026 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00002027 struct scatterlist *sgl,
2028 u32 sgl_count,
2029 struct scatterlist *sgl_bidi,
2030 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002031{
Andy Grover5951146d2011-07-19 10:26:37 +00002032 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002033 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002034
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002035 /*
2036 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
2037 * scatterlists already have been set to follow what the fabric
2038 * passes for the original expected data transfer length.
2039 */
2040 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2041 pr_warn("Rejecting SCSI DATA overflow for fabric using"
2042 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
2043 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2044 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2045 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002046 }
2047
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002048 cmd->t_data_sg = sgl;
2049 cmd->t_data_nents = sgl_count;
2050
2051 if (sgl_bidi && sgl_bidi_count) {
2052 cmd->t_bidi_data_sg = sgl_bidi;
2053 cmd->t_bidi_data_nents = sgl_bidi_count;
2054 }
2055 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002056 return 0;
2057}
2058EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
2059
Andy Grover49493142012-01-16 16:57:08 -08002060void *transport_kmap_data_sg(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002061{
Andy Groverec98f782011-07-20 19:28:46 +00002062 struct scatterlist *sg = cmd->t_data_sg;
Andy Grover49493142012-01-16 16:57:08 -08002063 struct page **pages;
2064 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002065
Andy Grover05d1c7c2011-07-20 19:13:28 +00002066 /*
Andy Groverec98f782011-07-20 19:28:46 +00002067 * We need to take into account a possible offset here for fabrics like
2068 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2069 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00002070 */
Andy Grover49493142012-01-16 16:57:08 -08002071 if (!cmd->t_data_nents)
2072 return NULL;
Paolo Bonzini3717ef02012-09-07 17:30:35 +02002073
2074 BUG_ON(!sg);
2075 if (cmd->t_data_nents == 1)
Andy Grover49493142012-01-16 16:57:08 -08002076 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002077
Andy Grover49493142012-01-16 16:57:08 -08002078 /* >1 page. use vmap */
2079 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
Paolo Bonzini3717ef02012-09-07 17:30:35 +02002080 if (!pages) {
2081 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover49493142012-01-16 16:57:08 -08002082 return NULL;
Paolo Bonzini3717ef02012-09-07 17:30:35 +02002083 }
Andy Grover49493142012-01-16 16:57:08 -08002084
2085 /* convert sg[] to pages[] */
2086 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
2087 pages[i] = sg_page(sg);
2088 }
2089
2090 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
2091 kfree(pages);
Paolo Bonzini3717ef02012-09-07 17:30:35 +02002092 if (!cmd->t_data_vmap) {
2093 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover49493142012-01-16 16:57:08 -08002094 return NULL;
Paolo Bonzini3717ef02012-09-07 17:30:35 +02002095 }
Andy Grover49493142012-01-16 16:57:08 -08002096
2097 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002098}
Andy Grover49493142012-01-16 16:57:08 -08002099EXPORT_SYMBOL(transport_kmap_data_sg);
2100
2101void transport_kunmap_data_sg(struct se_cmd *cmd)
2102{
Andy Grovera1edf9c2012-02-09 12:18:06 -08002103 if (!cmd->t_data_nents) {
Andy Grover49493142012-01-16 16:57:08 -08002104 return;
Andy Grovera1edf9c2012-02-09 12:18:06 -08002105 } else if (cmd->t_data_nents == 1) {
Andy Grover49493142012-01-16 16:57:08 -08002106 kunmap(sg_page(cmd->t_data_sg));
Andy Grovera1edf9c2012-02-09 12:18:06 -08002107 return;
2108 }
Andy Grover49493142012-01-16 16:57:08 -08002109
2110 vunmap(cmd->t_data_vmap);
2111 cmd->t_data_vmap = NULL;
2112}
2113EXPORT_SYMBOL(transport_kunmap_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002114
2115static int
2116transport_generic_get_mem(struct se_cmd *cmd)
2117{
Andy Groverec98f782011-07-20 19:28:46 +00002118 u32 length = cmd->data_length;
2119 unsigned int nents;
2120 struct page *page;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002121 gfp_t zero_flag;
Andy Groverec98f782011-07-20 19:28:46 +00002122 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002123
Andy Groverec98f782011-07-20 19:28:46 +00002124 nents = DIV_ROUND_UP(length, PAGE_SIZE);
2125 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
2126 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00002127 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002128
Andy Groverec98f782011-07-20 19:28:46 +00002129 cmd->t_data_nents = nents;
2130 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002131
Christoph Hellwig64f1db32012-05-20 11:59:11 -04002132 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_CDB ? 0 : __GFP_ZERO;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002133
Andy Groverec98f782011-07-20 19:28:46 +00002134 while (length) {
2135 u32 page_len = min_t(u32, length, PAGE_SIZE);
roland@purestorage.com9db9da32012-01-04 15:59:58 -08002136 page = alloc_page(GFP_KERNEL | zero_flag);
Andy Groverec98f782011-07-20 19:28:46 +00002137 if (!page)
2138 goto out;
2139
2140 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
2141 length -= page_len;
2142 i++;
2143 }
2144 return 0;
2145
2146out:
Yi Zoud0e27c82012-08-14 16:06:43 -07002147 while (i > 0) {
Andy Groverec98f782011-07-20 19:28:46 +00002148 i--;
Yi Zoud0e27c82012-08-14 16:06:43 -07002149 __free_page(sg_page(&cmd->t_data_sg[i]));
Andy Groverec98f782011-07-20 19:28:46 +00002150 }
2151 kfree(cmd->t_data_sg);
2152 cmd->t_data_sg = NULL;
2153 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002154}
2155
Andy Grovera1d8b492011-05-02 17:12:10 -07002156/*
Andy Groverb16a35b2012-04-03 15:51:21 -07002157 * Allocate any required resources to execute the command. For writes we
2158 * might not have the payload yet, so notify the fabric via a call to
2159 * ->write_pending instead. Otherwise place it on the execution queue.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002160 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002161int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002162{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002163 int ret = 0;
2164
2165 /*
2166 * Determine is the TCM fabric module has already allocated physical
2167 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00002168 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002169 */
Andy Groverec98f782011-07-20 19:28:46 +00002170 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
2171 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00002172 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002173 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002174 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002175 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04002176
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04002177 atomic_inc(&cmd->t_fe_count);
Christoph Hellwig4101f0a2012-04-24 00:25:03 -04002178
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002179 /*
Christoph Hellwigc3196f02012-07-08 15:58:41 -04002180 * If this command is not a write we can execute it right here,
2181 * for write buffers we need to notify the fabric driver first
2182 * and let it call back once the write buffers are ready.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002183 */
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002184 target_add_to_state_list(cmd);
Christoph Hellwigc3196f02012-07-08 15:58:41 -04002185 if (cmd->data_direction != DMA_TO_DEVICE) {
2186 target_execute_cmd(cmd);
2187 return 0;
2188 }
2189
2190 spin_lock_irq(&cmd->t_state_lock);
2191 cmd->t_state = TRANSPORT_WRITE_PENDING;
2192 spin_unlock_irq(&cmd->t_state_lock);
2193
2194 transport_cmd_check_stop(cmd, false);
2195
2196 ret = cmd->se_tfo->write_pending(cmd);
2197 if (ret == -EAGAIN || ret == -ENOMEM)
2198 goto queue_full;
2199
2200 if (ret < 0)
2201 return ret;
2202 return 1;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04002203
2204out_fail:
2205 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2206 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2207 return -EINVAL;
Christoph Hellwigc3196f02012-07-08 15:58:41 -04002208queue_full:
2209 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
2210 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
2211 transport_handle_queue_full(cmd, cmd->se_dev);
2212 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002213}
Andy Grovera1d8b492011-05-02 17:12:10 -07002214EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002215
Christoph Hellwige057f532011-10-17 13:56:41 -04002216static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002217{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07002218 int ret;
2219
2220 ret = cmd->se_tfo->write_pending(cmd);
2221 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04002222 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2223 cmd);
2224 transport_handle_queue_full(cmd, cmd->se_dev);
2225 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002226}
2227
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002228void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002229{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002230 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
Andy Groverc8e31f22012-01-19 13:39:17 -08002231 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002232 transport_wait_for_tasks(cmd);
2233
Christoph Hellwig35462972011-05-31 23:56:57 -04002234 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002235 } else {
2236 if (wait_for_tasks)
2237 transport_wait_for_tasks(cmd);
2238
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002239 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
2240
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02002241 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002242 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002243
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07002244 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002245 }
2246}
2247EXPORT_SYMBOL(transport_generic_free_cmd);
2248
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002249/* target_get_sess_cmd - Add command to active ->sess_cmd_list
2250 * @se_sess: session to reference
2251 * @se_cmd: command descriptor to add
Nicholas Bellingera6360782011-11-18 20:36:22 -08002252 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002253 */
Roland Dreierbc187ea2012-07-16 11:04:40 -07002254static int target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
2255 bool ack_kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002256{
2257 unsigned long flags;
Roland Dreierbc187ea2012-07-16 11:04:40 -07002258 int ret = 0;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002259
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002260 kref_init(&se_cmd->cmd_kref);
Nicholas Bellingera6360782011-11-18 20:36:22 -08002261 /*
2262 * Add a second kref if the fabric caller is expecting to handle
2263 * fabric acknowledgement that requires two target_put_sess_cmd()
2264 * invocations before se_cmd descriptor release.
2265 */
Nicholas Bellinger86715562012-02-13 01:07:22 -08002266 if (ack_kref == true) {
Nicholas Bellingera6360782011-11-18 20:36:22 -08002267 kref_get(&se_cmd->cmd_kref);
Nicholas Bellinger86715562012-02-13 01:07:22 -08002268 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
2269 }
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002270
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002271 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
Roland Dreierbc187ea2012-07-16 11:04:40 -07002272 if (se_sess->sess_tearing_down) {
2273 ret = -ESHUTDOWN;
2274 goto out;
2275 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002276 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
2277 se_cmd->check_release = 1;
Roland Dreierbc187ea2012-07-16 11:04:40 -07002278
2279out:
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002280 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Roland Dreierbc187ea2012-07-16 11:04:40 -07002281 return ret;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002282}
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002283
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002284static void target_release_cmd_kref(struct kref *kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002285{
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002286 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
2287 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002288 unsigned long flags;
2289
2290 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2291 if (list_empty(&se_cmd->se_cmd_list)) {
2292 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Nicholas Bellingerffc32d52012-02-13 02:35:01 -08002293 se_cmd->se_tfo->release_cmd(se_cmd);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002294 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002295 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002296 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
2297 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2298 complete(&se_cmd->cmd_wait_comp);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002299 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002300 }
2301 list_del(&se_cmd->se_cmd_list);
2302 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2303
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08002304 se_cmd->se_tfo->release_cmd(se_cmd);
2305}
2306
2307/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
2308 * @se_sess: session to reference
2309 * @se_cmd: command descriptor to drop
2310 */
2311int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
2312{
2313 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002314}
2315EXPORT_SYMBOL(target_put_sess_cmd);
2316
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002317/* target_sess_cmd_list_set_waiting - Flag all commands in
2318 * sess_cmd_list to complete cmd_wait_comp. Set
2319 * sess_tearing_down so no more commands are queued.
2320 * @se_sess: session to flag
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002321 */
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002322void target_sess_cmd_list_set_waiting(struct se_session *se_sess)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002323{
2324 struct se_cmd *se_cmd;
2325 unsigned long flags;
2326
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002327 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002328
2329 WARN_ON(se_sess->sess_tearing_down);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002330 se_sess->sess_tearing_down = 1;
2331
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002332 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002333 se_cmd->cmd_wait_set = 1;
2334
2335 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2336}
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002337EXPORT_SYMBOL(target_sess_cmd_list_set_waiting);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002338
2339/* target_wait_for_sess_cmds - Wait for outstanding descriptors
2340 * @se_sess: session to wait for active I/O
2341 * @wait_for_tasks: Make extra transport_wait_for_tasks call
2342 */
2343void target_wait_for_sess_cmds(
2344 struct se_session *se_sess,
2345 int wait_for_tasks)
2346{
2347 struct se_cmd *se_cmd, *tmp_cmd;
2348 bool rc = false;
2349
2350 list_for_each_entry_safe(se_cmd, tmp_cmd,
Roland Dreier1c7b13f2012-07-16 11:04:42 -07002351 &se_sess->sess_cmd_list, se_cmd_list) {
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002352 list_del(&se_cmd->se_cmd_list);
2353
2354 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
2355 " %d\n", se_cmd, se_cmd->t_state,
2356 se_cmd->se_tfo->get_cmd_state(se_cmd));
2357
2358 if (wait_for_tasks) {
2359 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
2360 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2361 se_cmd->se_tfo->get_cmd_state(se_cmd));
2362
2363 rc = transport_wait_for_tasks(se_cmd);
2364
2365 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
2366 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2367 se_cmd->se_tfo->get_cmd_state(se_cmd));
2368 }
2369
2370 if (!rc) {
2371 wait_for_completion(&se_cmd->cmd_wait_comp);
2372 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
2373 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2374 se_cmd->se_tfo->get_cmd_state(se_cmd));
2375 }
2376
2377 se_cmd->se_tfo->release_cmd(se_cmd);
2378 }
2379}
2380EXPORT_SYMBOL(target_wait_for_sess_cmds);
2381
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002382/* transport_lun_wait_for_tasks():
2383 *
2384 * Called from ConfigFS context to stop the passed struct se_cmd to allow
2385 * an struct se_lun to be successfully shutdown.
2386 */
2387static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
2388{
2389 unsigned long flags;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002390 int ret = 0;
2391
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002392 /*
2393 * If the frontend has already requested this struct se_cmd to
2394 * be stopped, we can safely ignore this struct se_cmd.
2395 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002396 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002397 if (cmd->transport_state & CMD_T_STOP) {
2398 cmd->transport_state &= ~CMD_T_LUN_STOP;
2399
2400 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
2401 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07002402 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwigf7113a42012-07-08 15:58:38 -04002403 transport_cmd_check_stop(cmd, false);
Andy Grovere3d6f902011-07-19 08:55:10 +00002404 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002405 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002406 cmd->transport_state |= CMD_T_LUN_FE_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07002407 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002408
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002409 // XXX: audit task_flags checks.
2410 spin_lock_irqsave(&cmd->t_state_lock, flags);
2411 if ((cmd->transport_state & CMD_T_BUSY) &&
2412 (cmd->transport_state & CMD_T_SENT)) {
2413 if (!target_stop_cmd(cmd, &flags))
2414 ret++;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002415 }
Christoph Hellwig5f41a312012-05-20 14:34:44 -04002416 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002417
Christoph Hellwig785fdf72012-04-24 00:25:04 -04002418 pr_debug("ConfigFS: cmd: %p stop tasks ret:"
2419 " %d\n", cmd, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002420 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07002421 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002422 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07002423 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07002424 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002425 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002426 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002427
2428 return 0;
2429}
2430
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002431static void __transport_clear_lun_from_sessions(struct se_lun *lun)
2432{
2433 struct se_cmd *cmd = NULL;
2434 unsigned long lun_flags, cmd_flags;
2435 /*
2436 * Do exception processing and return CHECK_CONDITION status to the
2437 * Initiator Port.
2438 */
2439 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00002440 while (!list_empty(&lun->lun_cmd_list)) {
2441 cmd = list_first_entry(&lun->lun_cmd_list,
2442 struct se_cmd, se_lun_node);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -05002443 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002444
Andy Grovera1d8b492011-05-02 17:12:10 -07002445 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07002446 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002447 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002448 cmd->se_lun->unpacked_lun,
2449 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002450 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07002451 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002452
2453 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2454
Andy Grover6708bb22011-06-08 10:36:43 -07002455 if (!cmd->se_lun) {
2456 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002457 cmd->se_tfo->get_task_tag(cmd),
2458 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002459 BUG();
2460 }
2461 /*
2462 * If the Storage engine still owns the iscsi_cmd_t, determine
2463 * and/or stop its context.
2464 */
Andy Grover6708bb22011-06-08 10:36:43 -07002465 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00002466 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
2467 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002468
Andy Grovere3d6f902011-07-19 08:55:10 +00002469 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002470 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2471 continue;
2472 }
2473
Andy Grover6708bb22011-06-08 10:36:43 -07002474 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002475 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002476 cmd->se_lun->unpacked_lun,
2477 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002478
Andy Grovera1d8b492011-05-02 17:12:10 -07002479 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002480 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002481 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002482 goto check_cond;
2483 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002484 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002485 target_remove_from_state_list(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002486 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002487
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002488 /*
2489 * The Storage engine stopped this struct se_cmd before it was
2490 * send to the fabric frontend for delivery back to the
2491 * Initiator Node. Return this SCSI CDB back with an
2492 * CHECK_CONDITION status.
2493 */
2494check_cond:
2495 transport_send_check_condition_and_sense(cmd,
2496 TCM_NON_EXISTENT_LUN, 0);
2497 /*
2498 * If the fabric frontend is waiting for this iscsi_cmd_t to
2499 * be released, notify the waiting thread now that LU has
2500 * finished accessing it.
2501 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002502 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002503 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07002504 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002505 " struct se_cmd: %p ITT: 0x%08x\n",
2506 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00002507 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002508
Andy Grovera1d8b492011-05-02 17:12:10 -07002509 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002510 cmd_flags);
Christoph Hellwigf7113a42012-07-08 15:58:38 -04002511 transport_cmd_check_stop(cmd, false);
Andy Grovera1d8b492011-05-02 17:12:10 -07002512 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002513 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2514 continue;
2515 }
Andy Grover6708bb22011-06-08 10:36:43 -07002516 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002517 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002518
Andy Grovera1d8b492011-05-02 17:12:10 -07002519 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002520 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2521 }
2522 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2523}
2524
2525static int transport_clear_lun_thread(void *p)
2526{
Jörn Engel8359cf42011-11-24 02:05:51 +01002527 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002528
2529 __transport_clear_lun_from_sessions(lun);
2530 complete(&lun->lun_shutdown_comp);
2531
2532 return 0;
2533}
2534
2535int transport_clear_lun_from_sessions(struct se_lun *lun)
2536{
2537 struct task_struct *kt;
2538
Andy Grover5951146d2011-07-19 10:26:37 +00002539 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002540 "tcm_cl_%u", lun->unpacked_lun);
2541 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002542 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00002543 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002544 }
2545 wait_for_completion(&lun->lun_shutdown_comp);
2546
2547 return 0;
2548}
2549
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002550/**
2551 * transport_wait_for_tasks - wait for completion to occur
2552 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002553 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002554 * Called from frontend fabric context to wait for storage engine
2555 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002556 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002557bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002558{
2559 unsigned long flags;
2560
Andy Grovera1d8b492011-05-02 17:12:10 -07002561 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08002562 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
2563 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002564 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002565 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002566 }
Christoph Hellwigcb4f4d32012-05-20 11:59:10 -04002567
Andy Groverc8e31f22012-01-19 13:39:17 -08002568 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
2569 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002570 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002571 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002572 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002573 /*
2574 * If we are already stopped due to an external event (ie: LUN shutdown)
2575 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07002576 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002577 * transport_clear_lun_from_sessions() once the ConfigFS context caller
2578 * has completed its operation on the struct se_cmd.
2579 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002580 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07002581 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00002582 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002583 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002584 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002585 /*
2586 * There is a special case for WRITES where a FE exception +
2587 * LUN shutdown means ConfigFS context is still sleeping on
2588 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
2589 * We go ahead and up transport_lun_stop_comp just to be sure
2590 * here.
2591 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002592 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2593 complete(&cmd->transport_lun_stop_comp);
2594 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
2595 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002596
Christoph Hellwigcf572a92012-04-24 00:25:05 -04002597 target_remove_from_state_list(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002598 /*
2599 * At this point, the frontend who was the originator of this
2600 * struct se_cmd, now owns the structure and can be released through
2601 * normal means below.
2602 */
Andy Grover6708bb22011-06-08 10:36:43 -07002603 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00002604 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002605 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002606 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002607
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002608 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002609 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002610
Nicholas Bellinger3d289342012-02-13 02:38:14 -08002611 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002612 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002613 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002614 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002615
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002616 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002617
Andy Grover6708bb22011-06-08 10:36:43 -07002618 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002619 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04002620 cmd, cmd->se_tfo->get_task_tag(cmd),
2621 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002622
Andy Grovera1d8b492011-05-02 17:12:10 -07002623 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002624
Andy Grovera1d8b492011-05-02 17:12:10 -07002625 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002626
Andy Grovera1d8b492011-05-02 17:12:10 -07002627 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002628 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002629
Masanari Iida35d1efe2012-08-16 22:43:13 +09002630 pr_debug("wait_for_tasks: Stopped wait_for_completion("
Andy Grovera1d8b492011-05-02 17:12:10 -07002631 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002632 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002633
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002634 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07002635
2636 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002637}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07002638EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002639
2640static int transport_get_sense_codes(
2641 struct se_cmd *cmd,
2642 u8 *asc,
2643 u8 *ascq)
2644{
2645 *asc = cmd->scsi_asc;
2646 *ascq = cmd->scsi_ascq;
2647
2648 return 0;
2649}
2650
2651static int transport_set_sense_codes(
2652 struct se_cmd *cmd,
2653 u8 asc,
2654 u8 ascq)
2655{
2656 cmd->scsi_asc = asc;
2657 cmd->scsi_ascq = ascq;
2658
2659 return 0;
2660}
2661
2662int transport_send_check_condition_and_sense(
2663 struct se_cmd *cmd,
2664 u8 reason,
2665 int from_transport)
2666{
2667 unsigned char *buffer = cmd->sense_buffer;
2668 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002669 u8 asc = 0, ascq = 0;
2670
Andy Grovera1d8b492011-05-02 17:12:10 -07002671 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002672 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002673 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002674 return 0;
2675 }
2676 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07002677 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002678
2679 if (!reason && from_transport)
2680 goto after_reason;
2681
2682 if (!from_transport)
2683 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002684
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002685 /*
2686 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
2687 * SENSE KEY values from include/scsi/scsi.h
2688 */
2689 switch (reason) {
2690 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07002691 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002692 buffer[0] = 0x70;
2693 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07002694 /* ILLEGAL REQUEST */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002695 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07002696 /* LOGICAL UNIT NOT SUPPORTED */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002697 buffer[SPC_ASC_KEY_OFFSET] = 0x25;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07002698 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002699 case TCM_UNSUPPORTED_SCSI_OPCODE:
2700 case TCM_SECTOR_COUNT_TOO_MANY:
2701 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002702 buffer[0] = 0x70;
2703 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002704 /* ILLEGAL REQUEST */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002705 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002706 /* INVALID COMMAND OPERATION CODE */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002707 buffer[SPC_ASC_KEY_OFFSET] = 0x20;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002708 break;
2709 case TCM_UNKNOWN_MODE_PAGE:
2710 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002711 buffer[0] = 0x70;
2712 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002713 /* ILLEGAL REQUEST */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002714 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002715 /* INVALID FIELD IN CDB */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002716 buffer[SPC_ASC_KEY_OFFSET] = 0x24;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002717 break;
2718 case TCM_CHECK_CONDITION_ABORT_CMD:
2719 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002720 buffer[0] = 0x70;
2721 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002722 /* ABORTED COMMAND */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002723 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002724 /* BUS DEVICE RESET FUNCTION OCCURRED */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002725 buffer[SPC_ASC_KEY_OFFSET] = 0x29;
2726 buffer[SPC_ASCQ_KEY_OFFSET] = 0x03;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002727 break;
2728 case TCM_INCORRECT_AMOUNT_OF_DATA:
2729 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002730 buffer[0] = 0x70;
2731 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002732 /* ABORTED COMMAND */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002733 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002734 /* WRITE ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002735 buffer[SPC_ASC_KEY_OFFSET] = 0x0c;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002736 /* NOT ENOUGH UNSOLICITED DATA */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002737 buffer[SPC_ASCQ_KEY_OFFSET] = 0x0d;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002738 break;
2739 case TCM_INVALID_CDB_FIELD:
2740 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002741 buffer[0] = 0x70;
2742 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08002743 /* ILLEGAL REQUEST */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002744 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002745 /* INVALID FIELD IN CDB */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002746 buffer[SPC_ASC_KEY_OFFSET] = 0x24;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002747 break;
2748 case TCM_INVALID_PARAMETER_LIST:
2749 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002750 buffer[0] = 0x70;
2751 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08002752 /* ILLEGAL REQUEST */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002753 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002754 /* INVALID FIELD IN PARAMETER LIST */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002755 buffer[SPC_ASC_KEY_OFFSET] = 0x26;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002756 break;
2757 case TCM_UNEXPECTED_UNSOLICITED_DATA:
2758 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002759 buffer[0] = 0x70;
2760 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002761 /* ABORTED COMMAND */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002762 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002763 /* WRITE ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002764 buffer[SPC_ASC_KEY_OFFSET] = 0x0c;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002765 /* UNEXPECTED_UNSOLICITED_DATA */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002766 buffer[SPC_ASCQ_KEY_OFFSET] = 0x0c;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002767 break;
2768 case TCM_SERVICE_CRC_ERROR:
2769 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002770 buffer[0] = 0x70;
2771 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002772 /* ABORTED COMMAND */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002773 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002774 /* PROTOCOL SERVICE CRC ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002775 buffer[SPC_ASC_KEY_OFFSET] = 0x47;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002776 /* N/A */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002777 buffer[SPC_ASCQ_KEY_OFFSET] = 0x05;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002778 break;
2779 case TCM_SNACK_REJECTED:
2780 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002781 buffer[0] = 0x70;
2782 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002783 /* ABORTED COMMAND */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002784 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002785 /* READ ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002786 buffer[SPC_ASC_KEY_OFFSET] = 0x11;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002787 /* FAILED RETRANSMISSION REQUEST */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002788 buffer[SPC_ASCQ_KEY_OFFSET] = 0x13;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002789 break;
2790 case TCM_WRITE_PROTECTED:
2791 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002792 buffer[0] = 0x70;
2793 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002794 /* DATA PROTECT */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002795 buffer[SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002796 /* WRITE PROTECTED */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002797 buffer[SPC_ASC_KEY_OFFSET] = 0x27;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002798 break;
Roland Dreiere2397c72012-07-16 15:34:21 -07002799 case TCM_ADDRESS_OUT_OF_RANGE:
2800 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002801 buffer[0] = 0x70;
2802 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreiere2397c72012-07-16 15:34:21 -07002803 /* ILLEGAL REQUEST */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002804 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Roland Dreiere2397c72012-07-16 15:34:21 -07002805 /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002806 buffer[SPC_ASC_KEY_OFFSET] = 0x21;
Roland Dreiere2397c72012-07-16 15:34:21 -07002807 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002808 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
2809 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002810 buffer[0] = 0x70;
2811 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002812 /* UNIT ATTENTION */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002813 buffer[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002814 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002815 buffer[SPC_ASC_KEY_OFFSET] = asc;
2816 buffer[SPC_ASCQ_KEY_OFFSET] = ascq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002817 break;
2818 case TCM_CHECK_CONDITION_NOT_READY:
2819 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002820 buffer[0] = 0x70;
2821 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002822 /* Not Ready */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002823 buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002824 transport_get_sense_codes(cmd, &asc, &ascq);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002825 buffer[SPC_ASC_KEY_OFFSET] = asc;
2826 buffer[SPC_ASCQ_KEY_OFFSET] = ascq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002827 break;
2828 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
2829 default:
2830 /* CURRENT ERROR */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002831 buffer[0] = 0x70;
2832 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002833 /* ILLEGAL REQUEST */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002834 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002835 /* LOGICAL UNIT COMMUNICATION FAILURE */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002836 buffer[SPC_ASC_KEY_OFFSET] = 0x80;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002837 break;
2838 }
2839 /*
2840 * This code uses linux/include/scsi/scsi.h SAM status codes!
2841 */
2842 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
2843 /*
2844 * Automatically padded, this value is encoded in the fabric's
2845 * data_length response PDU containing the SCSI defined sense data.
2846 */
Roland Dreier9c58b7d2012-08-15 14:35:25 -07002847 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002848
2849after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07002850 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002851}
2852EXPORT_SYMBOL(transport_send_check_condition_and_sense);
2853
2854int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
2855{
2856 int ret = 0;
2857
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002858 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07002859 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002860 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
2861 return 1;
Andy Grover8b1e1242012-04-03 15:51:12 -07002862
Andy Grover6708bb22011-06-08 10:36:43 -07002863 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002864 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002865 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00002866 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07002867
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002868 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00002869 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002870 ret = 1;
2871 }
2872 return ret;
2873}
2874EXPORT_SYMBOL(transport_check_aborted_status);
2875
2876void transport_send_task_abort(struct se_cmd *cmd)
2877{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07002878 unsigned long flags;
2879
2880 spin_lock_irqsave(&cmd->t_state_lock, flags);
2881 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2882 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2883 return;
2884 }
2885 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2886
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002887 /*
2888 * If there are still expected incoming fabric WRITEs, we wait
2889 * until until they have completed before sending a TASK_ABORTED
2890 * response. This response with TASK_ABORTED status will be
2891 * queued back to fabric module by transport_check_aborted_status().
2892 */
2893 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00002894 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002895 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002896 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002897 }
2898 }
2899 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
Andy Grover8b1e1242012-04-03 15:51:12 -07002900
Andy Grover6708bb22011-06-08 10:36:43 -07002901 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07002902 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00002903 cmd->se_tfo->get_task_tag(cmd));
Andy Grover8b1e1242012-04-03 15:51:12 -07002904
Andy Grovere3d6f902011-07-19 08:55:10 +00002905 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002906}
2907
Christoph Hellwigaf877292012-07-08 15:58:49 -04002908static void target_tmr_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002909{
Christoph Hellwigaf877292012-07-08 15:58:49 -04002910 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Andy Grover5951146d2011-07-19 10:26:37 +00002911 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002912 struct se_tmr_req *tmr = cmd->se_tmr_req;
2913 int ret;
2914
2915 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07002916 case TMR_ABORT_TASK:
Nicholas Bellinger3d289342012-02-13 02:38:14 -08002917 core_tmr_abort_task(dev, tmr, cmd->se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002918 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07002919 case TMR_ABORT_TASK_SET:
2920 case TMR_CLEAR_ACA:
2921 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002922 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
2923 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07002924 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002925 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
2926 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
2927 TMR_FUNCTION_REJECTED;
2928 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07002929 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002930 tmr->response = TMR_FUNCTION_REJECTED;
2931 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07002932 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002933 tmr->response = TMR_FUNCTION_REJECTED;
2934 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002935 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002936 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002937 tmr->function);
2938 tmr->response = TMR_FUNCTION_REJECTED;
2939 break;
2940 }
2941
2942 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00002943 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002944
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04002945 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002946}
2947
Christoph Hellwigaf877292012-07-08 15:58:49 -04002948int transport_generic_handle_tmr(
2949 struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002950{
Christoph Hellwigaf877292012-07-08 15:58:49 -04002951 INIT_WORK(&cmd->work, target_tmr_work);
2952 queue_work(cmd->se_dev->tmr_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002953 return 0;
2954}
Christoph Hellwigaf877292012-07-08 15:58:49 -04002955EXPORT_SYMBOL(transport_generic_handle_tmr);