blob: 5dee44639f8ffc4f29a7e074e3b9d6be3ad3179e [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>
39#include <asm/unaligned.h>
40#include <net/sock.h>
41#include <net/tcp.h>
42#include <scsi/scsi.h>
43#include <scsi/scsi_cmnd.h>
Nicholas Bellingere66ecd52011-05-19 20:19:14 -070044#include <scsi/scsi_tcq.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080045
46#include <target/target_core_base.h>
47#include <target/target_core_device.h>
48#include <target/target_core_tmr.h>
49#include <target/target_core_tpg.h>
50#include <target/target_core_transport.h>
51#include <target/target_core_fabric_ops.h>
52#include <target/target_core_configfs.h>
53
54#include "target_core_alua.h"
55#include "target_core_hba.h"
56#include "target_core_pr.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080057#include "target_core_ua.h"
58
Andy Grovere3d6f902011-07-19 08:55:10 +000059static int sub_api_initialized;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080060
Christoph Hellwig35e0e752011-10-17 13:56:53 -040061static struct workqueue_struct *target_completion_wq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062static struct kmem_cache *se_cmd_cache;
63static struct kmem_cache *se_sess_cache;
64struct kmem_cache *se_tmr_req_cache;
65struct kmem_cache *se_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080066struct kmem_cache *t10_pr_reg_cache;
67struct kmem_cache *t10_alua_lu_gp_cache;
68struct kmem_cache *t10_alua_lu_gp_mem_cache;
69struct kmem_cache *t10_alua_tg_pt_gp_cache;
70struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
71
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080072static int transport_generic_write_pending(struct se_cmd *);
Andy Grover5951146d2011-07-19 10:26:37 +000073static int transport_processing_thread(void *param);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080074static int __transport_execute_tasks(struct se_device *dev);
75static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070076static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040077 struct se_device *dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078static void transport_free_dev_tasks(struct se_cmd *cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +000079static int transport_generic_get_mem(struct se_cmd *cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -070080static void transport_put_cmd(struct se_cmd *cmd);
Christoph Hellwig3df8d402011-10-17 13:56:43 -040081static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080082static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
Christoph Hellwig35e0e752011-10-17 13:56:53 -040083static void transport_generic_request_failure(struct se_cmd *, int, int);
84static void target_complete_ok_work(struct work_struct *work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080085
Andy Grovere3d6f902011-07-19 08:55:10 +000086int init_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080087{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080088 se_cmd_cache = kmem_cache_create("se_cmd_cache",
89 sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070090 if (!se_cmd_cache) {
91 pr_err("kmem_cache_create for struct se_cmd failed\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080092 goto out;
93 }
94 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
95 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
96 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070097 if (!se_tmr_req_cache) {
98 pr_err("kmem_cache_create() for struct se_tmr_req"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080099 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400100 goto out_free_cmd_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800101 }
102 se_sess_cache = kmem_cache_create("se_sess_cache",
103 sizeof(struct se_session), __alignof__(struct se_session),
104 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700105 if (!se_sess_cache) {
106 pr_err("kmem_cache_create() for struct se_session"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800107 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400108 goto out_free_tmr_req_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800109 }
110 se_ua_cache = kmem_cache_create("se_ua_cache",
111 sizeof(struct se_ua), __alignof__(struct se_ua),
112 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700113 if (!se_ua_cache) {
114 pr_err("kmem_cache_create() for struct se_ua failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400115 goto out_free_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800116 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800117 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
118 sizeof(struct t10_pr_registration),
119 __alignof__(struct t10_pr_registration), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700120 if (!t10_pr_reg_cache) {
121 pr_err("kmem_cache_create() for struct t10_pr_registration"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800122 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400123 goto out_free_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800124 }
125 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
126 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
127 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700128 if (!t10_alua_lu_gp_cache) {
129 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800130 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400131 goto out_free_pr_reg_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800132 }
133 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
134 sizeof(struct t10_alua_lu_gp_member),
135 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700136 if (!t10_alua_lu_gp_mem_cache) {
137 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800138 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400139 goto out_free_lu_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800140 }
141 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
142 sizeof(struct t10_alua_tg_pt_gp),
143 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700144 if (!t10_alua_tg_pt_gp_cache) {
145 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800146 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400147 goto out_free_lu_gp_mem_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800148 }
149 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
150 "t10_alua_tg_pt_gp_mem_cache",
151 sizeof(struct t10_alua_tg_pt_gp_member),
152 __alignof__(struct t10_alua_tg_pt_gp_member),
153 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700154 if (!t10_alua_tg_pt_gp_mem_cache) {
155 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800156 "mem_t failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400157 goto out_free_tg_pt_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800158 }
159
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400160 target_completion_wq = alloc_workqueue("target_completion",
161 WQ_MEM_RECLAIM, 0);
162 if (!target_completion_wq)
163 goto out_free_tg_pt_gp_mem_cache;
164
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800165 return 0;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400166
167out_free_tg_pt_gp_mem_cache:
168 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
169out_free_tg_pt_gp_cache:
170 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
171out_free_lu_gp_mem_cache:
172 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
173out_free_lu_gp_cache:
174 kmem_cache_destroy(t10_alua_lu_gp_cache);
175out_free_pr_reg_cache:
176 kmem_cache_destroy(t10_pr_reg_cache);
177out_free_ua_cache:
178 kmem_cache_destroy(se_ua_cache);
179out_free_sess_cache:
180 kmem_cache_destroy(se_sess_cache);
181out_free_tmr_req_cache:
182 kmem_cache_destroy(se_tmr_req_cache);
183out_free_cmd_cache:
184 kmem_cache_destroy(se_cmd_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800185out:
Andy Grovere3d6f902011-07-19 08:55:10 +0000186 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800187}
188
Andy Grovere3d6f902011-07-19 08:55:10 +0000189void release_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190{
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400191 destroy_workqueue(target_completion_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800192 kmem_cache_destroy(se_cmd_cache);
193 kmem_cache_destroy(se_tmr_req_cache);
194 kmem_cache_destroy(se_sess_cache);
195 kmem_cache_destroy(se_ua_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800196 kmem_cache_destroy(t10_pr_reg_cache);
197 kmem_cache_destroy(t10_alua_lu_gp_cache);
198 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
199 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
200 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800201}
202
Andy Grovere3d6f902011-07-19 08:55:10 +0000203/* This code ensures unique mib indexes are handed out. */
204static DEFINE_SPINLOCK(scsi_mib_index_lock);
205static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800206
207/*
208 * Allocate a new row index for the entry type specified
209 */
210u32 scsi_get_new_index(scsi_index_t type)
211{
212 u32 new_index;
213
Andy Grovere3d6f902011-07-19 08:55:10 +0000214 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800215
Andy Grovere3d6f902011-07-19 08:55:10 +0000216 spin_lock(&scsi_mib_index_lock);
217 new_index = ++scsi_mib_index[type];
218 spin_unlock(&scsi_mib_index_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800219
220 return new_index;
221}
222
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800223void transport_init_queue_obj(struct se_queue_obj *qobj)
224{
225 atomic_set(&qobj->queue_cnt, 0);
226 INIT_LIST_HEAD(&qobj->qobj_list);
227 init_waitqueue_head(&qobj->thread_wq);
228 spin_lock_init(&qobj->cmd_queue_lock);
229}
230EXPORT_SYMBOL(transport_init_queue_obj);
231
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700232void transport_subsystem_check_init(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800233{
234 int ret;
235
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700236 if (sub_api_initialized)
237 return;
238
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800239 ret = request_module("target_core_iblock");
240 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700241 pr_err("Unable to load target_core_iblock\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800242
243 ret = request_module("target_core_file");
244 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700245 pr_err("Unable to load target_core_file\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800246
247 ret = request_module("target_core_pscsi");
248 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700249 pr_err("Unable to load target_core_pscsi\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800250
251 ret = request_module("target_core_stgt");
252 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700253 pr_err("Unable to load target_core_stgt\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800254
Andy Grovere3d6f902011-07-19 08:55:10 +0000255 sub_api_initialized = 1;
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700256 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800257}
258
259struct se_session *transport_init_session(void)
260{
261 struct se_session *se_sess;
262
263 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700264 if (!se_sess) {
265 pr_err("Unable to allocate struct se_session from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800266 " se_sess_cache\n");
267 return ERR_PTR(-ENOMEM);
268 }
269 INIT_LIST_HEAD(&se_sess->sess_list);
270 INIT_LIST_HEAD(&se_sess->sess_acl_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800271
272 return se_sess;
273}
274EXPORT_SYMBOL(transport_init_session);
275
276/*
277 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
278 */
279void __transport_register_session(
280 struct se_portal_group *se_tpg,
281 struct se_node_acl *se_nacl,
282 struct se_session *se_sess,
283 void *fabric_sess_ptr)
284{
285 unsigned char buf[PR_REG_ISID_LEN];
286
287 se_sess->se_tpg = se_tpg;
288 se_sess->fabric_sess_ptr = fabric_sess_ptr;
289 /*
290 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
291 *
292 * Only set for struct se_session's that will actually be moving I/O.
293 * eg: *NOT* discovery sessions.
294 */
295 if (se_nacl) {
296 /*
297 * If the fabric module supports an ISID based TransportID,
298 * save this value in binary from the fabric I_T Nexus now.
299 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000300 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000302 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 &buf[0], PR_REG_ISID_LEN);
304 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
305 }
306 spin_lock_irq(&se_nacl->nacl_sess_lock);
307 /*
308 * The se_nacl->nacl_sess pointer will be set to the
309 * last active I_T Nexus for each struct se_node_acl.
310 */
311 se_nacl->nacl_sess = se_sess;
312
313 list_add_tail(&se_sess->sess_acl_list,
314 &se_nacl->acl_sess_list);
315 spin_unlock_irq(&se_nacl->nacl_sess_lock);
316 }
317 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
318
Andy Grover6708bb22011-06-08 10:36:43 -0700319 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000320 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800321}
322EXPORT_SYMBOL(__transport_register_session);
323
324void transport_register_session(
325 struct se_portal_group *se_tpg,
326 struct se_node_acl *se_nacl,
327 struct se_session *se_sess,
328 void *fabric_sess_ptr)
329{
330 spin_lock_bh(&se_tpg->session_lock);
331 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
332 spin_unlock_bh(&se_tpg->session_lock);
333}
334EXPORT_SYMBOL(transport_register_session);
335
336void transport_deregister_session_configfs(struct se_session *se_sess)
337{
338 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700339 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800340 /*
341 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
342 */
343 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700344 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700345 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800346 list_del(&se_sess->sess_acl_list);
347 /*
348 * If the session list is empty, then clear the pointer.
349 * Otherwise, set the struct se_session pointer from the tail
350 * element of the per struct se_node_acl active session list.
351 */
352 if (list_empty(&se_nacl->acl_sess_list))
353 se_nacl->nacl_sess = NULL;
354 else {
355 se_nacl->nacl_sess = container_of(
356 se_nacl->acl_sess_list.prev,
357 struct se_session, sess_acl_list);
358 }
Roland Dreier23388862011-06-22 01:02:21 -0700359 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800360 }
361}
362EXPORT_SYMBOL(transport_deregister_session_configfs);
363
364void transport_free_session(struct se_session *se_sess)
365{
366 kmem_cache_free(se_sess_cache, se_sess);
367}
368EXPORT_SYMBOL(transport_free_session);
369
370void transport_deregister_session(struct se_session *se_sess)
371{
372 struct se_portal_group *se_tpg = se_sess->se_tpg;
373 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700374 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800375
Andy Grover6708bb22011-06-08 10:36:43 -0700376 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800377 transport_free_session(se_sess);
378 return;
379 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380
Roland Dreiere63a8e12011-08-12 16:01:02 -0700381 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800382 list_del(&se_sess->sess_list);
383 se_sess->se_tpg = NULL;
384 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700385 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800386
387 /*
388 * Determine if we need to do extra work for this initiator node's
389 * struct se_node_acl if it had been previously dynamically generated.
390 */
391 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700392 if (se_nacl) {
Roland Dreiere63a8e12011-08-12 16:01:02 -0700393 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800394 if (se_nacl->dynamic_node_acl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700395 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
396 se_tpg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800397 list_del(&se_nacl->acl_list);
398 se_tpg->num_node_acls--;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700399 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800400
401 core_tpg_wait_for_nacl_pr_ref(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 core_free_device_list_for_node(se_nacl, se_tpg);
Andy Grovere3d6f902011-07-19 08:55:10 +0000403 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800404 se_nacl);
Roland Dreiere63a8e12011-08-12 16:01:02 -0700405 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 }
407 }
Roland Dreiere63a8e12011-08-12 16:01:02 -0700408 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800409 }
410
411 transport_free_session(se_sess);
412
Andy Grover6708bb22011-06-08 10:36:43 -0700413 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000414 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800415}
416EXPORT_SYMBOL(transport_deregister_session);
417
418/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700419 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800420 */
421static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
422{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400423 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800424 struct se_task *task;
425 unsigned long flags;
426
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400427 if (!dev)
428 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800429
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400430 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400431 if (task->task_flags & TF_ACTIVE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800432 continue;
433
Andy Grover6708bb22011-06-08 10:36:43 -0700434 if (!atomic_read(&task->task_state_active))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800435 continue;
436
437 spin_lock_irqsave(&dev->execute_task_lock, flags);
438 list_del(&task->t_state_list);
Andy Grover6708bb22011-06-08 10:36:43 -0700439 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
440 cmd->se_tfo->get_task_tag(cmd), dev, task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800441 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
442
443 atomic_set(&task->task_state_active, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -0700444 atomic_dec(&cmd->t_task_cdbs_ex_left);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800445 }
446}
447
448/* transport_cmd_check_stop():
449 *
450 * 'transport_off = 1' determines if t_transport_active should be cleared.
451 * 'transport_off = 2' determines if task_dev_state should be removed.
452 *
453 * A non-zero u8 t_state sets cmd->t_state.
454 * Returns 1 when command is stopped, else 0.
455 */
456static int transport_cmd_check_stop(
457 struct se_cmd *cmd,
458 int transport_off,
459 u8 t_state)
460{
461 unsigned long flags;
462
Andy Grovera1d8b492011-05-02 17:12:10 -0700463 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800464 /*
465 * Determine if IOCTL context caller in requesting the stopping of this
466 * command for LUN shutdown purposes.
467 */
Andy Grovera1d8b492011-05-02 17:12:10 -0700468 if (atomic_read(&cmd->transport_lun_stop)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700469 pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800470 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000471 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800472
Andy Grovera1d8b492011-05-02 17:12:10 -0700473 atomic_set(&cmd->t_transport_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800474 if (transport_off == 2)
475 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700476 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800477
Andy Grovera1d8b492011-05-02 17:12:10 -0700478 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479 return 1;
480 }
481 /*
482 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000483 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800484 */
Andy Grovera1d8b492011-05-02 17:12:10 -0700485 if (atomic_read(&cmd->t_transport_stop)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700486 pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) =="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800487 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000488 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800489
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800490 if (transport_off == 2)
491 transport_all_task_dev_remove_state(cmd);
492
493 /*
494 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
495 * to FE.
496 */
497 if (transport_off == 2)
498 cmd->se_lun = NULL;
Andy Grovera1d8b492011-05-02 17:12:10 -0700499 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500
Andy Grovera1d8b492011-05-02 17:12:10 -0700501 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800502 return 1;
503 }
504 if (transport_off) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700505 atomic_set(&cmd->t_transport_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800506 if (transport_off == 2) {
507 transport_all_task_dev_remove_state(cmd);
508 /*
509 * Clear struct se_cmd->se_lun before the transport_off == 2
510 * handoff to fabric module.
511 */
512 cmd->se_lun = NULL;
513 /*
514 * Some fabric modules like tcm_loop can release
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300515 * their internally allocated I/O reference now and
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800516 * struct se_cmd now.
517 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000518 if (cmd->se_tfo->check_stop_free != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800519 spin_unlock_irqrestore(
Andy Grovera1d8b492011-05-02 17:12:10 -0700520 &cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800521
Andy Grovere3d6f902011-07-19 08:55:10 +0000522 cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800523 return 1;
524 }
525 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700526 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800527
528 return 0;
529 } else if (t_state)
530 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700531 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800532
533 return 0;
534}
535
536static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
537{
538 return transport_cmd_check_stop(cmd, 2, 0);
539}
540
541static void transport_lun_remove_cmd(struct se_cmd *cmd)
542{
Andy Grovere3d6f902011-07-19 08:55:10 +0000543 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800544 unsigned long flags;
545
546 if (!lun)
547 return;
548
Andy Grovera1d8b492011-05-02 17:12:10 -0700549 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Grover6708bb22011-06-08 10:36:43 -0700550 if (!atomic_read(&cmd->transport_dev_active)) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700551 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800552 goto check_lun;
553 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700554 atomic_set(&cmd->transport_dev_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800555 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700556 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800558
559check_lun:
560 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Andy Grovera1d8b492011-05-02 17:12:10 -0700561 if (atomic_read(&cmd->transport_lun_active)) {
Andy Grover5951146d2011-07-19 10:26:37 +0000562 list_del(&cmd->se_lun_node);
Andy Grovera1d8b492011-05-02 17:12:10 -0700563 atomic_set(&cmd->transport_lun_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800564#if 0
Andy Grover6708bb22011-06-08 10:36:43 -0700565 pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n"
Andy Grovere3d6f902011-07-19 08:55:10 +0000566 cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800567#endif
568 }
569 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
570}
571
572void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
573{
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700574 if (!cmd->se_tmr_req)
575 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800576
577 if (transport_cmd_check_stop_to_fabric(cmd))
578 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700579 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400580 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200581 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700582 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800583}
584
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400585static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
586 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800587{
588 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000589 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800590 unsigned long flags;
591
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800592 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700593 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800594 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700595 atomic_set(&cmd->t_transport_active, 1);
596 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800597 }
598
599 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700600
601 /* If the cmd is already on the list, remove it before we add it */
602 if (!list_empty(&cmd->se_queue_node))
603 list_del(&cmd->se_queue_node);
604 else
605 atomic_inc(&qobj->queue_cnt);
606
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400607 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700608 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400609 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700610 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700611 atomic_set(&cmd->t_transport_queue_active, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800612 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
613
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800614 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800615}
616
Andy Grover5951146d2011-07-19 10:26:37 +0000617static struct se_cmd *
618transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800619{
Andy Grover5951146d2011-07-19 10:26:37 +0000620 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800621 unsigned long flags;
622
623 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
624 if (list_empty(&qobj->qobj_list)) {
625 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
626 return NULL;
627 }
Andy Grover5951146d2011-07-19 10:26:37 +0000628 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800629
Roland Dreier79a7fef2011-09-28 22:12:07 -0700630 atomic_set(&cmd->t_transport_queue_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631
Roland Dreier79a7fef2011-09-28 22:12:07 -0700632 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800633 atomic_dec(&qobj->queue_cnt);
634 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
635
Andy Grover5951146d2011-07-19 10:26:37 +0000636 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800637}
638
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400639static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800640{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400641 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800642 unsigned long flags;
643
644 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Andy Grover6708bb22011-06-08 10:36:43 -0700645 if (!atomic_read(&cmd->t_transport_queue_active)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800646 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
647 return;
648 }
Roland Dreier79a7fef2011-09-28 22:12:07 -0700649 atomic_set(&cmd->t_transport_queue_active, 0);
650 atomic_dec(&qobj->queue_cnt);
651 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800652 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
653
Andy Grovera1d8b492011-05-02 17:12:10 -0700654 if (atomic_read(&cmd->t_transport_queue_active)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700655 pr_err("ITT: 0x%08x t_transport_queue_active: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000656 cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -0700657 atomic_read(&cmd->t_transport_queue_active));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800658 }
659}
660
661/*
662 * Completion function used by TCM subsystem plugins (such as FILEIO)
663 * for queueing up response from struct se_subsystem_api->do_task()
664 */
665void transport_complete_sync_cache(struct se_cmd *cmd, int good)
666{
Andy Grovera1d8b492011-05-02 17:12:10 -0700667 struct se_task *task = list_entry(cmd->t_task_list.next,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800668 struct se_task, t_list);
669
670 if (good) {
671 cmd->scsi_status = SAM_STAT_GOOD;
672 task->task_scsi_status = GOOD;
673 } else {
674 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
675 task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST;
Andy Grovere3d6f902011-07-19 08:55:10 +0000676 task->task_se_cmd->transport_error_status =
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800677 PYX_TRANSPORT_ILLEGAL_REQUEST;
678 }
679
680 transport_complete_task(task, good);
681}
682EXPORT_SYMBOL(transport_complete_sync_cache);
683
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400684static void target_complete_failure_work(struct work_struct *work)
685{
686 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
687
688 transport_generic_request_failure(cmd, 1, 1);
689}
690
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800691/* transport_complete_task():
692 *
693 * Called from interrupt and non interrupt context depending
694 * on the transport plugin.
695 */
696void transport_complete_task(struct se_task *task, int success)
697{
Andy Grovere3d6f902011-07-19 08:55:10 +0000698 struct se_cmd *cmd = task->task_se_cmd;
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400699 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800700 unsigned long flags;
701#if 0
Andy Grover6708bb22011-06-08 10:36:43 -0700702 pr_debug("task: %p CDB: 0x%02x obj_ptr: %p\n", task,
Andy Grovera1d8b492011-05-02 17:12:10 -0700703 cmd->t_task_cdb[0], dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800704#endif
Andy Grovere3d6f902011-07-19 08:55:10 +0000705 if (dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800706 atomic_inc(&dev->depth_left);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800707
Andy Grovera1d8b492011-05-02 17:12:10 -0700708 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400709 task->task_flags &= ~TF_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800710
711 /*
712 * See if any sense data exists, if so set the TASK_SENSE flag.
713 * Also check for any other post completion work that needs to be
714 * done by the plugins.
715 */
716 if (dev && dev->transport->transport_complete) {
717 if (dev->transport->transport_complete(task) != 0) {
718 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
719 task->task_sense = 1;
720 success = 1;
721 }
722 }
723
724 /*
725 * See if we are waiting for outstanding struct se_task
726 * to complete for an exception condition
727 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400728 if (task->task_flags & TF_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700729 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800730 complete(&task->task_stop_comp);
731 return;
732 }
733 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800734 * Decrement the outstanding t_task_cdbs_left count. The last
735 * struct se_task from struct se_cmd will complete itself into the
736 * device queue depending upon int success.
737 */
Andy Grover6708bb22011-06-08 10:36:43 -0700738 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700739 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800740 return;
741 }
742
Andy Grovera1d8b492011-05-02 17:12:10 -0700743 if (!success || cmd->t_tasks_failed) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800744 if (!task->task_error_status) {
745 task->task_error_status =
746 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
747 cmd->transport_error_status =
748 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
749 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400750 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800751 } else {
Andy Grovera1d8b492011-05-02 17:12:10 -0700752 atomic_set(&cmd->t_transport_complete, 1);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400753 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800754 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400755
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400756 cmd->t_state = TRANSPORT_COMPLETE;
757 atomic_set(&cmd->t_transport_active, 1);
Andy Grovera1d8b492011-05-02 17:12:10 -0700758 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800759
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400760 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800761}
762EXPORT_SYMBOL(transport_complete_task);
763
764/*
765 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
766 * struct se_task list are ready to be added to the active execution list
767 * struct se_device
768
769 * Called with se_dev_t->execute_task_lock called.
770 */
771static inline int transport_add_task_check_sam_attr(
772 struct se_task *task,
773 struct se_task *task_prev,
774 struct se_device *dev)
775{
776 /*
777 * No SAM Task attribute emulation enabled, add to tail of
778 * execution queue
779 */
780 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
781 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
782 return 0;
783 }
784 /*
785 * HEAD_OF_QUEUE attribute for received CDB, which means
786 * the first task that is associated with a struct se_cmd goes to
787 * head of the struct se_device->execute_task_list, and task_prev
788 * after that for each subsequent task
789 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -0700790 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800791 list_add(&task->t_execute_list,
792 (task_prev != NULL) ?
793 &task_prev->t_execute_list :
794 &dev->execute_task_list);
795
Andy Grover6708bb22011-06-08 10:36:43 -0700796 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800797 " in execution queue\n",
Andy Grover6708bb22011-06-08 10:36:43 -0700798 task->task_se_cmd->t_task_cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800799 return 1;
800 }
801 /*
802 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
803 * transitioned from Dermant -> Active state, and are added to the end
804 * of the struct se_device->execute_task_list
805 */
806 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
807 return 0;
808}
809
810/* __transport_add_task_to_execute_queue():
811 *
812 * Called with se_dev_t->execute_task_lock called.
813 */
814static void __transport_add_task_to_execute_queue(
815 struct se_task *task,
816 struct se_task *task_prev,
817 struct se_device *dev)
818{
819 int head_of_queue;
820
821 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
822 atomic_inc(&dev->execute_tasks);
823
824 if (atomic_read(&task->task_state_active))
825 return;
826 /*
827 * Determine if this task needs to go to HEAD_OF_QUEUE for the
828 * state list as well. Running with SAM Task Attribute emulation
829 * will always return head_of_queue == 0 here
830 */
831 if (head_of_queue)
832 list_add(&task->t_state_list, (task_prev) ?
833 &task_prev->t_state_list :
834 &dev->state_task_list);
835 else
836 list_add_tail(&task->t_state_list, &dev->state_task_list);
837
838 atomic_set(&task->task_state_active, 1);
839
Andy Grover6708bb22011-06-08 10:36:43 -0700840 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000841 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800842 task, dev);
843}
844
845static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
846{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400847 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800848 struct se_task *task;
849 unsigned long flags;
850
Andy Grovera1d8b492011-05-02 17:12:10 -0700851 spin_lock_irqsave(&cmd->t_state_lock, flags);
852 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800853 if (atomic_read(&task->task_state_active))
854 continue;
855
856 spin_lock(&dev->execute_task_lock);
857 list_add_tail(&task->t_state_list, &dev->state_task_list);
858 atomic_set(&task->task_state_active, 1);
859
Andy Grover6708bb22011-06-08 10:36:43 -0700860 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
861 task->task_se_cmd->se_tfo->get_task_tag(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800862 task->task_se_cmd), task, dev);
863
864 spin_unlock(&dev->execute_task_lock);
865 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700866 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800867}
868
869static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
870{
Andy Grover5951146d2011-07-19 10:26:37 +0000871 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800872 struct se_task *task, *task_prev = NULL;
873 unsigned long flags;
874
875 spin_lock_irqsave(&dev->execute_task_lock, flags);
Andy Grovera1d8b492011-05-02 17:12:10 -0700876 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -0400877 if (!list_empty(&task->t_execute_list))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800878 continue;
879 /*
880 * __transport_add_task_to_execute_queue() handles the
881 * SAM Task Attribute emulation if enabled
882 */
883 __transport_add_task_to_execute_queue(task, task_prev, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800884 task_prev = task;
885 }
886 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800887}
888
Christoph Hellwig04629b72011-10-12 11:07:04 -0400889void __transport_remove_task_from_execute_queue(struct se_task *task,
890 struct se_device *dev)
891{
892 list_del_init(&task->t_execute_list);
893 atomic_dec(&dev->execute_tasks);
894}
895
Nicholas Bellinger52208ae2011-02-24 16:58:20 -0800896void transport_remove_task_from_execute_queue(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800897 struct se_task *task,
898 struct se_device *dev)
899{
900 unsigned long flags;
901
Christoph Hellwig04629b72011-10-12 11:07:04 -0400902 if (WARN_ON(list_empty(&task->t_execute_list)))
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700903 return;
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700904
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800905 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig04629b72011-10-12 11:07:04 -0400906 __transport_remove_task_from_execute_queue(task, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800907 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
908}
909
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700910/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700911 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700912 */
913
914static void target_qf_do_work(struct work_struct *work)
915{
916 struct se_device *dev = container_of(work, struct se_device,
917 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700918 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700919 struct se_cmd *cmd, *cmd_tmp;
920
921 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700922 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
923 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700924
Roland Dreierbcac3642011-08-27 21:33:16 -0700925 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700926 list_del(&cmd->se_qf_node);
927 atomic_dec(&dev->dev_qf_count);
928 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700929
Andy Grover6708bb22011-06-08 10:36:43 -0700930 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700931 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400932 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700933 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
934 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400935
936 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700937 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700938}
939
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800940unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
941{
942 switch (cmd->data_direction) {
943 case DMA_NONE:
944 return "NONE";
945 case DMA_FROM_DEVICE:
946 return "READ";
947 case DMA_TO_DEVICE:
948 return "WRITE";
949 case DMA_BIDIRECTIONAL:
950 return "BIDI";
951 default:
952 break;
953 }
954
955 return "UNKNOWN";
956}
957
958void transport_dump_dev_state(
959 struct se_device *dev,
960 char *b,
961 int *bl)
962{
963 *bl += sprintf(b + *bl, "Status: ");
964 switch (dev->dev_status) {
965 case TRANSPORT_DEVICE_ACTIVATED:
966 *bl += sprintf(b + *bl, "ACTIVATED");
967 break;
968 case TRANSPORT_DEVICE_DEACTIVATED:
969 *bl += sprintf(b + *bl, "DEACTIVATED");
970 break;
971 case TRANSPORT_DEVICE_SHUTDOWN:
972 *bl += sprintf(b + *bl, "SHUTDOWN");
973 break;
974 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
975 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
976 *bl += sprintf(b + *bl, "OFFLINE");
977 break;
978 default:
979 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
980 break;
981 }
982
983 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
984 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
985 dev->queue_depth);
986 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000987 dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800988 *bl += sprintf(b + *bl, " ");
989}
990
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800991void transport_dump_vpd_proto_id(
992 struct t10_vpd *vpd,
993 unsigned char *p_buf,
994 int p_buf_len)
995{
996 unsigned char buf[VPD_TMP_BUF_SIZE];
997 int len;
998
999 memset(buf, 0, VPD_TMP_BUF_SIZE);
1000 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1001
1002 switch (vpd->protocol_identifier) {
1003 case 0x00:
1004 sprintf(buf+len, "Fibre Channel\n");
1005 break;
1006 case 0x10:
1007 sprintf(buf+len, "Parallel SCSI\n");
1008 break;
1009 case 0x20:
1010 sprintf(buf+len, "SSA\n");
1011 break;
1012 case 0x30:
1013 sprintf(buf+len, "IEEE 1394\n");
1014 break;
1015 case 0x40:
1016 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1017 " Protocol\n");
1018 break;
1019 case 0x50:
1020 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1021 break;
1022 case 0x60:
1023 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1024 break;
1025 case 0x70:
1026 sprintf(buf+len, "Automation/Drive Interface Transport"
1027 " Protocol\n");
1028 break;
1029 case 0x80:
1030 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1031 break;
1032 default:
1033 sprintf(buf+len, "Unknown 0x%02x\n",
1034 vpd->protocol_identifier);
1035 break;
1036 }
1037
1038 if (p_buf)
1039 strncpy(p_buf, buf, p_buf_len);
1040 else
Andy Grover6708bb22011-06-08 10:36:43 -07001041 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001042}
1043
1044void
1045transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1046{
1047 /*
1048 * Check if the Protocol Identifier Valid (PIV) bit is set..
1049 *
1050 * from spc3r23.pdf section 7.5.1
1051 */
1052 if (page_83[1] & 0x80) {
1053 vpd->protocol_identifier = (page_83[0] & 0xf0);
1054 vpd->protocol_identifier_set = 1;
1055 transport_dump_vpd_proto_id(vpd, NULL, 0);
1056 }
1057}
1058EXPORT_SYMBOL(transport_set_vpd_proto_id);
1059
1060int transport_dump_vpd_assoc(
1061 struct t10_vpd *vpd,
1062 unsigned char *p_buf,
1063 int p_buf_len)
1064{
1065 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001066 int ret = 0;
1067 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001068
1069 memset(buf, 0, VPD_TMP_BUF_SIZE);
1070 len = sprintf(buf, "T10 VPD Identifier Association: ");
1071
1072 switch (vpd->association) {
1073 case 0x00:
1074 sprintf(buf+len, "addressed logical unit\n");
1075 break;
1076 case 0x10:
1077 sprintf(buf+len, "target port\n");
1078 break;
1079 case 0x20:
1080 sprintf(buf+len, "SCSI target device\n");
1081 break;
1082 default:
1083 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +00001084 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001085 break;
1086 }
1087
1088 if (p_buf)
1089 strncpy(p_buf, buf, p_buf_len);
1090 else
Andy Grover6708bb22011-06-08 10:36:43 -07001091 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001092
1093 return ret;
1094}
1095
1096int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1097{
1098 /*
1099 * The VPD identification association..
1100 *
1101 * from spc3r23.pdf Section 7.6.3.1 Table 297
1102 */
1103 vpd->association = (page_83[1] & 0x30);
1104 return transport_dump_vpd_assoc(vpd, NULL, 0);
1105}
1106EXPORT_SYMBOL(transport_set_vpd_assoc);
1107
1108int transport_dump_vpd_ident_type(
1109 struct t10_vpd *vpd,
1110 unsigned char *p_buf,
1111 int p_buf_len)
1112{
1113 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001114 int ret = 0;
1115 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001116
1117 memset(buf, 0, VPD_TMP_BUF_SIZE);
1118 len = sprintf(buf, "T10 VPD Identifier Type: ");
1119
1120 switch (vpd->device_identifier_type) {
1121 case 0x00:
1122 sprintf(buf+len, "Vendor specific\n");
1123 break;
1124 case 0x01:
1125 sprintf(buf+len, "T10 Vendor ID based\n");
1126 break;
1127 case 0x02:
1128 sprintf(buf+len, "EUI-64 based\n");
1129 break;
1130 case 0x03:
1131 sprintf(buf+len, "NAA\n");
1132 break;
1133 case 0x04:
1134 sprintf(buf+len, "Relative target port identifier\n");
1135 break;
1136 case 0x08:
1137 sprintf(buf+len, "SCSI name string\n");
1138 break;
1139 default:
1140 sprintf(buf+len, "Unsupported: 0x%02x\n",
1141 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +00001142 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001143 break;
1144 }
1145
Andy Grovere3d6f902011-07-19 08:55:10 +00001146 if (p_buf) {
1147 if (p_buf_len < strlen(buf)+1)
1148 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001149 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +00001150 } else {
Andy Grover6708bb22011-06-08 10:36:43 -07001151 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +00001152 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153
1154 return ret;
1155}
1156
1157int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1158{
1159 /*
1160 * The VPD identifier type..
1161 *
1162 * from spc3r23.pdf Section 7.6.3.1 Table 298
1163 */
1164 vpd->device_identifier_type = (page_83[1] & 0x0f);
1165 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1166}
1167EXPORT_SYMBOL(transport_set_vpd_ident_type);
1168
1169int transport_dump_vpd_ident(
1170 struct t10_vpd *vpd,
1171 unsigned char *p_buf,
1172 int p_buf_len)
1173{
1174 unsigned char buf[VPD_TMP_BUF_SIZE];
1175 int ret = 0;
1176
1177 memset(buf, 0, VPD_TMP_BUF_SIZE);
1178
1179 switch (vpd->device_identifier_code_set) {
1180 case 0x01: /* Binary */
1181 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1182 &vpd->device_identifier[0]);
1183 break;
1184 case 0x02: /* ASCII */
1185 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1186 &vpd->device_identifier[0]);
1187 break;
1188 case 0x03: /* UTF-8 */
1189 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1190 &vpd->device_identifier[0]);
1191 break;
1192 default:
1193 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1194 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +00001195 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001196 break;
1197 }
1198
1199 if (p_buf)
1200 strncpy(p_buf, buf, p_buf_len);
1201 else
Andy Grover6708bb22011-06-08 10:36:43 -07001202 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001203
1204 return ret;
1205}
1206
1207int
1208transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1209{
1210 static const char hex_str[] = "0123456789abcdef";
1211 int j = 0, i = 4; /* offset to start of the identifer */
1212
1213 /*
1214 * The VPD Code Set (encoding)
1215 *
1216 * from spc3r23.pdf Section 7.6.3.1 Table 296
1217 */
1218 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1219 switch (vpd->device_identifier_code_set) {
1220 case 0x01: /* Binary */
1221 vpd->device_identifier[j++] =
1222 hex_str[vpd->device_identifier_type];
1223 while (i < (4 + page_83[3])) {
1224 vpd->device_identifier[j++] =
1225 hex_str[(page_83[i] & 0xf0) >> 4];
1226 vpd->device_identifier[j++] =
1227 hex_str[page_83[i] & 0x0f];
1228 i++;
1229 }
1230 break;
1231 case 0x02: /* ASCII */
1232 case 0x03: /* UTF-8 */
1233 while (i < (4 + page_83[3]))
1234 vpd->device_identifier[j++] = page_83[i++];
1235 break;
1236 default:
1237 break;
1238 }
1239
1240 return transport_dump_vpd_ident(vpd, NULL, 0);
1241}
1242EXPORT_SYMBOL(transport_set_vpd_ident);
1243
1244static void core_setup_task_attr_emulation(struct se_device *dev)
1245{
1246 /*
1247 * If this device is from Target_Core_Mod/pSCSI, disable the
1248 * SAM Task Attribute emulation.
1249 *
1250 * This is currently not available in upsream Linux/SCSI Target
1251 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1252 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001253 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001254 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1255 return;
1256 }
1257
1258 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001259 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001260 " device\n", dev->transport->name,
1261 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001262}
1263
1264static void scsi_dump_inquiry(struct se_device *dev)
1265{
Andy Grovere3d6f902011-07-19 08:55:10 +00001266 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001267 int i, device_type;
1268 /*
1269 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1270 */
Andy Grover6708bb22011-06-08 10:36:43 -07001271 pr_debug(" Vendor: ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001272 for (i = 0; i < 8; i++)
1273 if (wwn->vendor[i] >= 0x20)
Andy Grover6708bb22011-06-08 10:36:43 -07001274 pr_debug("%c", wwn->vendor[i]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001275 else
Andy Grover6708bb22011-06-08 10:36:43 -07001276 pr_debug(" ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001277
Andy Grover6708bb22011-06-08 10:36:43 -07001278 pr_debug(" Model: ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001279 for (i = 0; i < 16; i++)
1280 if (wwn->model[i] >= 0x20)
Andy Grover6708bb22011-06-08 10:36:43 -07001281 pr_debug("%c", wwn->model[i]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282 else
Andy Grover6708bb22011-06-08 10:36:43 -07001283 pr_debug(" ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001284
Andy Grover6708bb22011-06-08 10:36:43 -07001285 pr_debug(" Revision: ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286 for (i = 0; i < 4; i++)
1287 if (wwn->revision[i] >= 0x20)
Andy Grover6708bb22011-06-08 10:36:43 -07001288 pr_debug("%c", wwn->revision[i]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001289 else
Andy Grover6708bb22011-06-08 10:36:43 -07001290 pr_debug(" ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001291
Andy Grover6708bb22011-06-08 10:36:43 -07001292 pr_debug("\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001293
Andy Grovere3d6f902011-07-19 08:55:10 +00001294 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001295 pr_debug(" Type: %s ", scsi_device_type(device_type));
1296 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001297 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001298}
1299
1300struct se_device *transport_add_device_to_core_hba(
1301 struct se_hba *hba,
1302 struct se_subsystem_api *transport,
1303 struct se_subsystem_dev *se_dev,
1304 u32 device_flags,
1305 void *transport_dev,
1306 struct se_dev_limits *dev_limits,
1307 const char *inquiry_prod,
1308 const char *inquiry_rev)
1309{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001310 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001311 struct se_device *dev;
1312
1313 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001314 if (!dev) {
1315 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001316 return NULL;
1317 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001318
Andy Grovere3d6f902011-07-19 08:55:10 +00001319 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001320 dev->dev_flags = device_flags;
1321 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001322 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001323 dev->se_hba = hba;
1324 dev->se_sub_dev = se_dev;
1325 dev->transport = transport;
1326 atomic_set(&dev->active_cmds, 0);
1327 INIT_LIST_HEAD(&dev->dev_list);
1328 INIT_LIST_HEAD(&dev->dev_sep_list);
1329 INIT_LIST_HEAD(&dev->dev_tmr_list);
1330 INIT_LIST_HEAD(&dev->execute_task_list);
1331 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1332 INIT_LIST_HEAD(&dev->ordered_cmd_list);
1333 INIT_LIST_HEAD(&dev->state_task_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001334 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001335 spin_lock_init(&dev->execute_task_lock);
1336 spin_lock_init(&dev->delayed_cmd_lock);
1337 spin_lock_init(&dev->ordered_cmd_lock);
1338 spin_lock_init(&dev->state_task_lock);
1339 spin_lock_init(&dev->dev_alua_lock);
1340 spin_lock_init(&dev->dev_reservation_lock);
1341 spin_lock_init(&dev->dev_status_lock);
1342 spin_lock_init(&dev->dev_status_thr_lock);
1343 spin_lock_init(&dev->se_port_lock);
1344 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001345 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001346
1347 dev->queue_depth = dev_limits->queue_depth;
1348 atomic_set(&dev->depth_left, dev->queue_depth);
1349 atomic_set(&dev->dev_ordered_id, 0);
1350
1351 se_dev_set_default_attribs(dev, dev_limits);
1352
1353 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1354 dev->creation_time = get_jiffies_64();
1355 spin_lock_init(&dev->stats_lock);
1356
1357 spin_lock(&hba->device_lock);
1358 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1359 hba->dev_count++;
1360 spin_unlock(&hba->device_lock);
1361 /*
1362 * Setup the SAM Task Attribute emulation for struct se_device
1363 */
1364 core_setup_task_attr_emulation(dev);
1365 /*
1366 * Force PR and ALUA passthrough emulation with internal object use.
1367 */
1368 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1369 /*
1370 * Setup the Reservations infrastructure for struct se_device
1371 */
1372 core_setup_reservations(dev, force_pt);
1373 /*
1374 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1375 */
1376 if (core_setup_alua(dev, force_pt) < 0)
1377 goto out;
1378
1379 /*
1380 * Startup the struct se_device processing thread
1381 */
1382 dev->process_thread = kthread_run(transport_processing_thread, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001383 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001384 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001385 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001386 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001387 goto out;
1388 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001389 /*
1390 * Setup work_queue for QUEUE_FULL
1391 */
1392 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001393 /*
1394 * Preload the initial INQUIRY const values if we are doing
1395 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1396 * passthrough because this is being provided by the backend LLD.
1397 * This is required so that transport_get_inquiry() copies these
1398 * originals once back into DEV_T10_WWN(dev) for the virtual device
1399 * setup.
1400 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001401 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001402 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001403 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001404 " INQUIRY consts\n");
1405 goto out;
1406 }
1407
Andy Grovere3d6f902011-07-19 08:55:10 +00001408 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1409 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1410 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001411 }
1412 scsi_dump_inquiry(dev);
1413
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001414 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001415out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001416 kthread_stop(dev->process_thread);
1417
1418 spin_lock(&hba->device_lock);
1419 list_del(&dev->dev_list);
1420 hba->dev_count--;
1421 spin_unlock(&hba->device_lock);
1422
1423 se_release_vpd_for_dev(dev);
1424
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001425 kfree(dev);
1426
1427 return NULL;
1428}
1429EXPORT_SYMBOL(transport_add_device_to_core_hba);
1430
1431/* transport_generic_prepare_cdb():
1432 *
1433 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1434 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1435 * The point of this is since we are mapping iSCSI LUNs to
1436 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1437 * devices and HBAs for a loop.
1438 */
1439static inline void transport_generic_prepare_cdb(
1440 unsigned char *cdb)
1441{
1442 switch (cdb[0]) {
1443 case READ_10: /* SBC - RDProtect */
1444 case READ_12: /* SBC - RDProtect */
1445 case READ_16: /* SBC - RDProtect */
1446 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1447 case VERIFY: /* SBC - VRProtect */
1448 case VERIFY_16: /* SBC - VRProtect */
1449 case WRITE_VERIFY: /* SBC - VRProtect */
1450 case WRITE_VERIFY_12: /* SBC - VRProtect */
1451 break;
1452 default:
1453 cdb[1] &= 0x1f; /* clear logical unit number */
1454 break;
1455 }
1456}
1457
1458static struct se_task *
1459transport_generic_get_task(struct se_cmd *cmd,
1460 enum dma_data_direction data_direction)
1461{
1462 struct se_task *task;
Andy Grover5951146d2011-07-19 10:26:37 +00001463 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001464
Andy Grover6708bb22011-06-08 10:36:43 -07001465 task = dev->transport->alloc_task(cmd->t_task_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001466 if (!task) {
Andy Grover6708bb22011-06-08 10:36:43 -07001467 pr_err("Unable to allocate struct se_task\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001468 return NULL;
1469 }
1470
1471 INIT_LIST_HEAD(&task->t_list);
1472 INIT_LIST_HEAD(&task->t_execute_list);
1473 INIT_LIST_HEAD(&task->t_state_list);
1474 init_completion(&task->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001475 task->task_se_cmd = cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001476 task->task_data_direction = data_direction;
1477
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001478 return task;
1479}
1480
1481static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1482
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001483/*
1484 * Used by fabric modules containing a local struct se_cmd within their
1485 * fabric dependent per I/O descriptor.
1486 */
1487void transport_init_se_cmd(
1488 struct se_cmd *cmd,
1489 struct target_core_fabric_ops *tfo,
1490 struct se_session *se_sess,
1491 u32 data_length,
1492 int data_direction,
1493 int task_attr,
1494 unsigned char *sense_buffer)
1495{
Andy Grover5951146d2011-07-19 10:26:37 +00001496 INIT_LIST_HEAD(&cmd->se_lun_node);
1497 INIT_LIST_HEAD(&cmd->se_delayed_node);
1498 INIT_LIST_HEAD(&cmd->se_ordered_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001499 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001500 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001501
Andy Grovera1d8b492011-05-02 17:12:10 -07001502 INIT_LIST_HEAD(&cmd->t_task_list);
1503 init_completion(&cmd->transport_lun_fe_stop_comp);
1504 init_completion(&cmd->transport_lun_stop_comp);
1505 init_completion(&cmd->t_transport_stop_comp);
1506 spin_lock_init(&cmd->t_state_lock);
1507 atomic_set(&cmd->transport_dev_active, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001508
1509 cmd->se_tfo = tfo;
1510 cmd->se_sess = se_sess;
1511 cmd->data_length = data_length;
1512 cmd->data_direction = data_direction;
1513 cmd->sam_task_attr = task_attr;
1514 cmd->sense_buffer = sense_buffer;
1515}
1516EXPORT_SYMBOL(transport_init_se_cmd);
1517
1518static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1519{
1520 /*
1521 * Check if SAM Task Attribute emulation is enabled for this
1522 * struct se_device storage object
1523 */
Andy Grover5951146d2011-07-19 10:26:37 +00001524 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001525 return 0;
1526
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001527 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001528 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001529 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001530 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001531 }
1532 /*
1533 * Used to determine when ORDERED commands should go from
1534 * Dormant to Active status.
1535 */
Andy Grover5951146d2011-07-19 10:26:37 +00001536 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001537 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001538 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001539 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001540 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001541 return 0;
1542}
1543
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001544/* transport_generic_allocate_tasks():
1545 *
1546 * Called from fabric RX Thread.
1547 */
1548int transport_generic_allocate_tasks(
1549 struct se_cmd *cmd,
1550 unsigned char *cdb)
1551{
1552 int ret;
1553
1554 transport_generic_prepare_cdb(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001555 /*
1556 * Ensure that the received CDB is less than the max (252 + 8) bytes
1557 * for VARIABLE_LENGTH_CMD
1558 */
1559 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001560 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001561 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1562 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Andy Grovere3d6f902011-07-19 08:55:10 +00001563 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001564 }
1565 /*
1566 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1567 * allocate the additional extended CDB buffer now.. Otherwise
1568 * setup the pointer from __t_task_cdb to t_task_cdb.
1569 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001570 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1571 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001572 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001573 if (!cmd->t_task_cdb) {
1574 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001575 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001576 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001577 (unsigned long)sizeof(cmd->__t_task_cdb));
Andy Grovere3d6f902011-07-19 08:55:10 +00001578 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579 }
1580 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001581 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001582 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001583 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001584 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001585 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001586 /*
1587 * Setup the received CDB based on SCSI defined opcodes and
1588 * perform unit attention, persistent reservations and ALUA
Andy Grovera1d8b492011-05-02 17:12:10 -07001589 * checks for virtual device backends. The cmd->t_task_cdb
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001590 * pointer is expected to be setup before we reach this point.
1591 */
1592 ret = transport_generic_cmd_sequencer(cmd, cdb);
1593 if (ret < 0)
1594 return ret;
1595 /*
1596 * Check for SAM Task Attribute Emulation
1597 */
1598 if (transport_check_alloc_task_attr(cmd) < 0) {
1599 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1600 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001601 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001602 }
1603 spin_lock(&cmd->se_lun->lun_sep_lock);
1604 if (cmd->se_lun->lun_sep)
1605 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1606 spin_unlock(&cmd->se_lun->lun_sep_lock);
1607 return 0;
1608}
1609EXPORT_SYMBOL(transport_generic_allocate_tasks);
1610
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001611/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001612 * Used by fabric module frontends to queue tasks directly.
1613 * Many only be used from process context only
1614 */
1615int transport_handle_cdb_direct(
1616 struct se_cmd *cmd)
1617{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001618 int ret;
1619
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001620 if (!cmd->se_lun) {
1621 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001622 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001623 return -EINVAL;
1624 }
1625 if (in_interrupt()) {
1626 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001627 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001628 " from interrupt context\n");
1629 return -EINVAL;
1630 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001631 /*
1632 * Set TRANSPORT_NEW_CMD state and cmd->t_transport_active=1 following
1633 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1634 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001635 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001636 *
1637 * Also, we don't take cmd->t_state_lock here as we only expect
1638 * this to be called for initial descriptor submission.
1639 */
1640 cmd->t_state = TRANSPORT_NEW_CMD;
1641 atomic_set(&cmd->t_transport_active, 1);
1642 /*
1643 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1644 * so follow TRANSPORT_NEW_CMD processing thread context usage
1645 * and call transport_generic_request_failure() if necessary..
1646 */
1647 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001648 if (ret < 0) {
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001649 cmd->transport_error_status = ret;
Christoph Hellwig4499dda2011-10-17 13:56:49 -04001650 transport_generic_request_failure(cmd, 0,
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001651 (cmd->data_direction != DMA_TO_DEVICE));
1652 }
1653 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001654}
1655EXPORT_SYMBOL(transport_handle_cdb_direct);
1656
1657/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001658 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1659 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1660 * complete setup in TCM process context w/ TFO->new_cmd_map().
1661 */
1662int transport_generic_handle_cdb_map(
1663 struct se_cmd *cmd)
1664{
Andy Grovere3d6f902011-07-19 08:55:10 +00001665 if (!cmd->se_lun) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001666 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001667 pr_err("cmd->se_lun is NULL\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001668 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001669 }
1670
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001671 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001672 return 0;
1673}
1674EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1675
1676/* transport_generic_handle_data():
1677 *
1678 *
1679 */
1680int transport_generic_handle_data(
1681 struct se_cmd *cmd)
1682{
1683 /*
1684 * For the software fabric case, then we assume the nexus is being
1685 * failed/shutdown when signals are pending from the kthread context
1686 * caller, so we return a failure. For the HW target mode case running
1687 * in interrupt code, the signal_pending() check is skipped.
1688 */
1689 if (!in_interrupt() && signal_pending(current))
Andy Grovere3d6f902011-07-19 08:55:10 +00001690 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001691 /*
1692 * If the received CDB has aleady been ABORTED by the generic
1693 * target engine, we now call transport_check_aborted_status()
1694 * to queue any delated TASK_ABORTED status for the received CDB to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001695 * fabric module as we are expecting no further incoming DATA OUT
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001696 * sequences at this point.
1697 */
1698 if (transport_check_aborted_status(cmd, 1) != 0)
1699 return 0;
1700
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001701 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001702 return 0;
1703}
1704EXPORT_SYMBOL(transport_generic_handle_data);
1705
1706/* transport_generic_handle_tmr():
1707 *
1708 *
1709 */
1710int transport_generic_handle_tmr(
1711 struct se_cmd *cmd)
1712{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001713 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001714 return 0;
1715}
1716EXPORT_SYMBOL(transport_generic_handle_tmr);
1717
Nicholas Bellingerf4366772011-05-19 20:19:11 -07001718void transport_generic_free_cmd_intr(
1719 struct se_cmd *cmd)
1720{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001721 transport_add_cmd_to_queue(cmd, TRANSPORT_FREE_CMD_INTR, false);
Nicholas Bellingerf4366772011-05-19 20:19:11 -07001722}
1723EXPORT_SYMBOL(transport_generic_free_cmd_intr);
1724
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001725/*
1726 * If the task is active, request it to be stopped and sleep until it
1727 * has completed.
1728 */
1729bool target_stop_task(struct se_task *task, unsigned long *flags)
1730{
1731 struct se_cmd *cmd = task->task_se_cmd;
1732 bool was_active = false;
1733
1734 if (task->task_flags & TF_ACTIVE) {
1735 task->task_flags |= TF_REQUEST_STOP;
1736 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1737
1738 pr_debug("Task %p waiting to complete\n", task);
1739 wait_for_completion(&task->task_stop_comp);
1740 pr_debug("Task %p stopped successfully\n", task);
1741
1742 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1743 atomic_dec(&cmd->t_task_cdbs_left);
1744 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1745 was_active = true;
1746 }
1747
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001748 return was_active;
1749}
1750
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001751static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1752{
1753 struct se_task *task, *task_tmp;
1754 unsigned long flags;
1755 int ret = 0;
1756
Andy Grover6708bb22011-06-08 10:36:43 -07001757 pr_debug("ITT[0x%08x] - Stopping tasks\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001758 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001759
1760 /*
1761 * No tasks remain in the execution queue
1762 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001763 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001764 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07001765 &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001766 pr_debug("Processing task %p\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001767 /*
1768 * If the struct se_task has not been sent and is not active,
1769 * remove the struct se_task from the execution queue.
1770 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04001771 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
Andy Grovera1d8b492011-05-02 17:12:10 -07001772 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001773 flags);
1774 transport_remove_task_from_execute_queue(task,
Christoph Hellwig42bf8292011-10-12 11:07:00 -04001775 cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001776
Christoph Hellwig04629b72011-10-12 11:07:04 -04001777 pr_debug("Task %p removed from execute queue\n", task);
Andy Grovera1d8b492011-05-02 17:12:10 -07001778 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001779 continue;
1780 }
1781
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001782 if (!target_stop_task(task, &flags)) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001783 pr_debug("Task %p - did nothing\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001784 ret++;
1785 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001786 }
Andy Grovera1d8b492011-05-02 17:12:10 -07001787 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001788
1789 return ret;
1790}
1791
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001792/*
1793 * Handle SAM-esque emulation for generic transport request failures.
1794 */
1795static void transport_generic_request_failure(
1796 struct se_cmd *cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001797 int complete,
1798 int sc)
1799{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001800 int ret = 0;
1801
Andy Grover6708bb22011-06-08 10:36:43 -07001802 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001803 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001804 cmd->t_task_cdb[0]);
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04001805 pr_debug("-----[ i_state: %d t_state: %d transport_error_status: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001806 cmd->se_tfo->get_cmd_state(cmd),
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04001807 cmd->t_state,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001808 cmd->transport_error_status);
Andy Grover6708bb22011-06-08 10:36:43 -07001809 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001810 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
1811 " t_transport_active: %d t_transport_stop: %d"
Andy Grover6708bb22011-06-08 10:36:43 -07001812 " t_transport_sent: %d\n", cmd->t_task_list_num,
Andy Grovera1d8b492011-05-02 17:12:10 -07001813 atomic_read(&cmd->t_task_cdbs_left),
1814 atomic_read(&cmd->t_task_cdbs_sent),
1815 atomic_read(&cmd->t_task_cdbs_ex_left),
1816 atomic_read(&cmd->t_transport_active),
1817 atomic_read(&cmd->t_transport_stop),
1818 atomic_read(&cmd->t_transport_sent));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001819
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001820 /*
1821 * For SAM Task Attribute emulation for failed struct se_cmd
1822 */
1823 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1824 transport_complete_task_attr(cmd);
1825
1826 if (complete) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001827 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
1828 }
1829
1830 switch (cmd->transport_error_status) {
1831 case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
1832 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1833 break;
1834 case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
1835 cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
1836 break;
1837 case PYX_TRANSPORT_INVALID_CDB_FIELD:
1838 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1839 break;
1840 case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
1841 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1842 break;
1843 case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
1844 if (!sc)
1845 transport_new_cmd_failure(cmd);
1846 /*
1847 * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
1848 * we force this session to fall back to session
1849 * recovery.
1850 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001851 cmd->se_tfo->fall_back_to_erl0(cmd->se_sess);
1852 cmd->se_tfo->stop_session(cmd->se_sess, 0, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001853
1854 goto check_stop;
1855 case PYX_TRANSPORT_LU_COMM_FAILURE:
1856 case PYX_TRANSPORT_ILLEGAL_REQUEST:
1857 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1858 break;
1859 case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
1860 cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
1861 break;
1862 case PYX_TRANSPORT_WRITE_PROTECTED:
1863 cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
1864 break;
1865 case PYX_TRANSPORT_RESERVATION_CONFLICT:
1866 /*
1867 * No SENSE Data payload for this case, set SCSI Status
1868 * and queue the response to $FABRIC_MOD.
1869 *
1870 * Uses linux/include/scsi/scsi.h SAM status codes defs
1871 */
1872 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1873 /*
1874 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1875 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1876 * CONFLICT STATUS.
1877 *
1878 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1879 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001880 if (cmd->se_sess &&
1881 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1882 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001883 cmd->orig_fe_lun, 0x2C,
1884 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1885
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001886 ret = cmd->se_tfo->queue_status(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 goto check_stop;
1890 case PYX_TRANSPORT_USE_SENSE_REASON:
1891 /*
1892 * struct se_cmd->scsi_sense_reason already set
1893 */
1894 break;
1895 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001896 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07001897 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001898 cmd->transport_error_status);
1899 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1900 break;
1901 }
Nicholas Bellinger16ab8e62011-08-08 19:03:38 -07001902 /*
1903 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1904 * make the call to transport_send_check_condition_and_sense()
1905 * directly. Otherwise expect the fabric to make the call to
1906 * transport_send_check_condition_and_sense() after handling
1907 * possible unsoliticied write data payloads.
1908 */
1909 if (!sc && !cmd->se_tfo->new_cmd_map)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001910 transport_new_cmd_failure(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001911 else {
1912 ret = transport_send_check_condition_and_sense(cmd,
1913 cmd->scsi_sense_reason, 0);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001914 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001915 goto queue_full;
1916 }
1917
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001918check_stop:
1919 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001920 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001921 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001922 return;
1923
1924queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001925 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1926 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001927}
1928
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001929static inline u32 transport_lba_21(unsigned char *cdb)
1930{
1931 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1932}
1933
1934static inline u32 transport_lba_32(unsigned char *cdb)
1935{
1936 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1937}
1938
1939static inline unsigned long long transport_lba_64(unsigned char *cdb)
1940{
1941 unsigned int __v1, __v2;
1942
1943 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1944 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1945
1946 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1947}
1948
1949/*
1950 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1951 */
1952static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1953{
1954 unsigned int __v1, __v2;
1955
1956 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1957 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1958
1959 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1960}
1961
1962static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
1963{
1964 unsigned long flags;
1965
Andy Grovera1d8b492011-05-02 17:12:10 -07001966 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001967 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
Andy Grovera1d8b492011-05-02 17:12:10 -07001968 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001969}
1970
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001971static inline int transport_tcq_window_closed(struct se_device *dev)
1972{
1973 if (dev->dev_tcq_window_closed++ <
1974 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
1975 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
1976 } else
1977 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
1978
Andy Grovere3d6f902011-07-19 08:55:10 +00001979 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001980 return 0;
1981}
1982
1983/*
1984 * Called from Fabric Module context from transport_execute_tasks()
1985 *
1986 * The return of this function determins if the tasks from struct se_cmd
1987 * get added to the execution queue in transport_execute_tasks(),
1988 * or are added to the delayed or ordered lists here.
1989 */
1990static inline int transport_execute_task_attr(struct se_cmd *cmd)
1991{
Andy Grover5951146d2011-07-19 10:26:37 +00001992 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001993 return 1;
1994 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001995 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001996 * to allow the passed struct se_cmd list of tasks to the front of the list.
1997 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001998 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Andy Grover5951146d2011-07-19 10:26:37 +00001999 atomic_inc(&cmd->se_dev->dev_hoq_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002000 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07002001 pr_debug("Added HEAD_OF_QUEUE for CDB:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002002 " 0x%02x, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07002003 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002004 cmd->se_ordered_id);
2005 return 1;
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002006 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Andy Grover5951146d2011-07-19 10:26:37 +00002007 spin_lock(&cmd->se_dev->ordered_cmd_lock);
2008 list_add_tail(&cmd->se_ordered_node,
2009 &cmd->se_dev->ordered_cmd_list);
2010 spin_unlock(&cmd->se_dev->ordered_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002011
Andy Grover5951146d2011-07-19 10:26:37 +00002012 atomic_inc(&cmd->se_dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002013 smp_mb__after_atomic_inc();
2014
Andy Grover6708bb22011-06-08 10:36:43 -07002015 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002016 " list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002017 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002018 cmd->se_ordered_id);
2019 /*
2020 * Add ORDERED command to tail of execution queue if
2021 * no other older commands exist that need to be
2022 * completed first.
2023 */
Andy Grover6708bb22011-06-08 10:36:43 -07002024 if (!atomic_read(&cmd->se_dev->simple_cmds))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002025 return 1;
2026 } else {
2027 /*
2028 * For SIMPLE and UNTAGGED Task Attribute commands
2029 */
Andy Grover5951146d2011-07-19 10:26:37 +00002030 atomic_inc(&cmd->se_dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002031 smp_mb__after_atomic_inc();
2032 }
2033 /*
2034 * Otherwise if one or more outstanding ORDERED task attribute exist,
2035 * add the dormant task(s) built for the passed struct se_cmd to the
2036 * execution queue and become in Active state for this struct se_device.
2037 */
Andy Grover5951146d2011-07-19 10:26:37 +00002038 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002039 /*
2040 * Otherwise, add cmd w/ tasks to delayed cmd queue that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002041 * will be drained upon completion of HEAD_OF_QUEUE task.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002042 */
Andy Grover5951146d2011-07-19 10:26:37 +00002043 spin_lock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002044 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
Andy Grover5951146d2011-07-19 10:26:37 +00002045 list_add_tail(&cmd->se_delayed_node,
2046 &cmd->se_dev->delayed_cmd_list);
2047 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002048
Andy Grover6708bb22011-06-08 10:36:43 -07002049 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002050 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002051 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002052 cmd->se_ordered_id);
2053 /*
2054 * Return zero to let transport_execute_tasks() know
2055 * not to add the delayed tasks to the execution list.
2056 */
2057 return 0;
2058 }
2059 /*
2060 * Otherwise, no ORDERED task attributes exist..
2061 */
2062 return 1;
2063}
2064
2065/*
2066 * Called from fabric module context in transport_generic_new_cmd() and
2067 * transport_generic_process_write()
2068 */
2069static int transport_execute_tasks(struct se_cmd *cmd)
2070{
2071 int add_tasks;
2072
Christoph Hellwigdb1620a2011-05-31 17:06:43 -04002073 if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
2074 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
Christoph Hellwig4499dda2011-10-17 13:56:49 -04002075 transport_generic_request_failure(cmd, 0, 1);
Christoph Hellwigdb1620a2011-05-31 17:06:43 -04002076 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002077 }
Christoph Hellwigdb1620a2011-05-31 17:06:43 -04002078
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002079 /*
2080 * Call transport_cmd_check_stop() to see if a fabric exception
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002081 * has occurred that prevents execution.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002082 */
Andy Grover6708bb22011-06-08 10:36:43 -07002083 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002084 /*
2085 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2086 * attribute for the tasks of the received struct se_cmd CDB
2087 */
2088 add_tasks = transport_execute_task_attr(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00002089 if (!add_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002090 goto execute_tasks;
2091 /*
2092 * This calls transport_add_tasks_from_cmd() to handle
2093 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2094 * (if enabled) in __transport_add_task_to_execute_queue() and
2095 * transport_add_task_check_sam_attr().
2096 */
2097 transport_add_tasks_from_cmd(cmd);
2098 }
2099 /*
2100 * Kick the execution queue for the cmd associated struct se_device
2101 * storage object.
2102 */
2103execute_tasks:
Andy Grover5951146d2011-07-19 10:26:37 +00002104 __transport_execute_tasks(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002105 return 0;
2106}
2107
2108/*
2109 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2110 * from struct se_device->execute_task_list and
2111 *
2112 * Called from transport_processing_thread()
2113 */
2114static int __transport_execute_tasks(struct se_device *dev)
2115{
2116 int error;
2117 struct se_cmd *cmd = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002118 struct se_task *task = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002119 unsigned long flags;
2120
2121 /*
2122 * Check if there is enough room in the device and HBA queue to send
Andy Grovera1d8b492011-05-02 17:12:10 -07002123 * struct se_tasks to the selected transport.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002124 */
2125check_depth:
Andy Grovere3d6f902011-07-19 08:55:10 +00002126 if (!atomic_read(&dev->depth_left))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002127 return transport_tcq_window_closed(dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002128
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002129 dev->dev_tcq_window_closed = 0;
2130
Andy Grovere3d6f902011-07-19 08:55:10 +00002131 spin_lock_irq(&dev->execute_task_lock);
2132 if (list_empty(&dev->execute_task_list)) {
2133 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002134 return 0;
2135 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002136 task = list_first_entry(&dev->execute_task_list,
2137 struct se_task, t_execute_list);
Christoph Hellwig04629b72011-10-12 11:07:04 -04002138 __transport_remove_task_from_execute_queue(task, dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002139 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002140
2141 atomic_dec(&dev->depth_left);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002142
Andy Grovere3d6f902011-07-19 08:55:10 +00002143 cmd = task->task_se_cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002144
Andy Grovera1d8b492011-05-02 17:12:10 -07002145 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04002146 task->task_flags |= (TF_ACTIVE | TF_SENT);
Andy Grovera1d8b492011-05-02 17:12:10 -07002147 atomic_inc(&cmd->t_task_cdbs_sent);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002148
Andy Grovera1d8b492011-05-02 17:12:10 -07002149 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2150 cmd->t_task_list_num)
Nicholas Bellinger415a0902011-10-23 18:16:13 -07002151 atomic_set(&cmd->t_transport_sent, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002152
Andy Grovera1d8b492011-05-02 17:12:10 -07002153 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002154 /*
2155 * The struct se_cmd->transport_emulate_cdb() function pointer is used
Andy Grovere3d6f902011-07-19 08:55:10 +00002156 * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002157 * struct se_subsystem_api->do_task() caller below.
2158 */
2159 if (cmd->transport_emulate_cdb) {
2160 error = cmd->transport_emulate_cdb(cmd);
2161 if (error != 0) {
2162 cmd->transport_error_status = error;
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04002163 spin_lock_irqsave(&cmd->t_state_lock, flags);
2164 task->task_flags &= ~TF_ACTIVE;
2165 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger415a0902011-10-23 18:16:13 -07002166 atomic_set(&cmd->t_transport_sent, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002167 transport_stop_tasks_for_cmd(cmd);
Christoph Hellwig4499dda2011-10-17 13:56:49 -04002168 atomic_inc(&dev->depth_left);
2169 transport_generic_request_failure(cmd, 0, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002170 goto check_depth;
2171 }
2172 /*
2173 * Handle the successful completion for transport_emulate_cdb()
2174 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2175 * Otherwise the caller is expected to complete the task with
2176 * proper status.
2177 */
2178 if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
2179 cmd->scsi_status = SAM_STAT_GOOD;
2180 task->task_scsi_status = GOOD;
2181 transport_complete_task(task, 1);
2182 }
2183 } else {
2184 /*
2185 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2186 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2187 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2188 * LUN emulation code.
2189 *
2190 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2191 * call ->do_task() directly and let the underlying TCM subsystem plugin
2192 * code handle the CDB emulation.
2193 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002194 if ((dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
2195 (!(task->task_se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002196 error = transport_emulate_control_cdb(task);
2197 else
Andy Grovere3d6f902011-07-19 08:55:10 +00002198 error = dev->transport->do_task(task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002199
2200 if (error != 0) {
2201 cmd->transport_error_status = error;
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04002202 spin_lock_irqsave(&cmd->t_state_lock, flags);
2203 task->task_flags &= ~TF_ACTIVE;
2204 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger415a0902011-10-23 18:16:13 -07002205 atomic_set(&cmd->t_transport_sent, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002206 transport_stop_tasks_for_cmd(cmd);
Christoph Hellwig4499dda2011-10-17 13:56:49 -04002207 atomic_inc(&dev->depth_left);
2208 transport_generic_request_failure(cmd, 0, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002209 }
2210 }
2211
2212 goto check_depth;
2213
2214 return 0;
2215}
2216
2217void transport_new_cmd_failure(struct se_cmd *se_cmd)
2218{
2219 unsigned long flags;
2220 /*
2221 * Any unsolicited data will get dumped for failed command inside of
2222 * the fabric plugin
2223 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002224 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002225 se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
2226 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
Andy Grovera1d8b492011-05-02 17:12:10 -07002227 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002228}
2229
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002230static inline u32 transport_get_sectors_6(
2231 unsigned char *cdb,
2232 struct se_cmd *cmd,
2233 int *ret)
2234{
Andy Grover5951146d2011-07-19 10:26:37 +00002235 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002236
2237 /*
2238 * Assume TYPE_DISK for non struct se_device objects.
2239 * Use 8-bit sector value.
2240 */
2241 if (!dev)
2242 goto type_disk;
2243
2244 /*
2245 * Use 24-bit allocation length for TYPE_TAPE.
2246 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002247 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002248 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2249
2250 /*
2251 * Everything else assume TYPE_DISK Sector CDB location.
2252 * Use 8-bit sector value.
2253 */
2254type_disk:
2255 return (u32)cdb[4];
2256}
2257
2258static inline u32 transport_get_sectors_10(
2259 unsigned char *cdb,
2260 struct se_cmd *cmd,
2261 int *ret)
2262{
Andy Grover5951146d2011-07-19 10:26:37 +00002263 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002264
2265 /*
2266 * Assume TYPE_DISK for non struct se_device objects.
2267 * Use 16-bit sector value.
2268 */
2269 if (!dev)
2270 goto type_disk;
2271
2272 /*
2273 * XXX_10 is not defined in SSC, throw an exception
2274 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002275 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2276 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002277 return 0;
2278 }
2279
2280 /*
2281 * Everything else assume TYPE_DISK Sector CDB location.
2282 * Use 16-bit sector value.
2283 */
2284type_disk:
2285 return (u32)(cdb[7] << 8) + cdb[8];
2286}
2287
2288static inline u32 transport_get_sectors_12(
2289 unsigned char *cdb,
2290 struct se_cmd *cmd,
2291 int *ret)
2292{
Andy Grover5951146d2011-07-19 10:26:37 +00002293 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002294
2295 /*
2296 * Assume TYPE_DISK for non struct se_device objects.
2297 * Use 32-bit sector value.
2298 */
2299 if (!dev)
2300 goto type_disk;
2301
2302 /*
2303 * XXX_12 is not defined in SSC, throw an exception
2304 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002305 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2306 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002307 return 0;
2308 }
2309
2310 /*
2311 * Everything else assume TYPE_DISK Sector CDB location.
2312 * Use 32-bit sector value.
2313 */
2314type_disk:
2315 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2316}
2317
2318static inline u32 transport_get_sectors_16(
2319 unsigned char *cdb,
2320 struct se_cmd *cmd,
2321 int *ret)
2322{
Andy Grover5951146d2011-07-19 10:26:37 +00002323 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002324
2325 /*
2326 * Assume TYPE_DISK for non struct se_device objects.
2327 * Use 32-bit sector value.
2328 */
2329 if (!dev)
2330 goto type_disk;
2331
2332 /*
2333 * Use 24-bit allocation length for TYPE_TAPE.
2334 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002335 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002336 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2337
2338type_disk:
2339 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2340 (cdb[12] << 8) + cdb[13];
2341}
2342
2343/*
2344 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2345 */
2346static inline u32 transport_get_sectors_32(
2347 unsigned char *cdb,
2348 struct se_cmd *cmd,
2349 int *ret)
2350{
2351 /*
2352 * Assume TYPE_DISK for non struct se_device objects.
2353 * Use 32-bit sector value.
2354 */
2355 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2356 (cdb[30] << 8) + cdb[31];
2357
2358}
2359
2360static inline u32 transport_get_size(
2361 u32 sectors,
2362 unsigned char *cdb,
2363 struct se_cmd *cmd)
2364{
Andy Grover5951146d2011-07-19 10:26:37 +00002365 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002366
Andy Grovere3d6f902011-07-19 08:55:10 +00002367 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002368 if (cdb[1] & 1) { /* sectors */
Andy Grovere3d6f902011-07-19 08:55:10 +00002369 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002370 } else /* bytes */
2371 return sectors;
2372 }
2373#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002374 pr_debug("Returning block_size: %u, sectors: %u == %u for"
Andy Grovere3d6f902011-07-19 08:55:10 +00002375 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2376 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2377 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002378#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00002379 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002380}
2381
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002382static void transport_xor_callback(struct se_cmd *cmd)
2383{
2384 unsigned char *buf, *addr;
Andy Groverec98f782011-07-20 19:28:46 +00002385 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002386 unsigned int offset;
2387 int i;
Andy Groverec98f782011-07-20 19:28:46 +00002388 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002389 /*
2390 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2391 *
2392 * 1) read the specified logical block(s);
2393 * 2) transfer logical blocks from the data-out buffer;
2394 * 3) XOR the logical blocks transferred from the data-out buffer with
2395 * the logical blocks read, storing the resulting XOR data in a buffer;
2396 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2397 * blocks transferred from the data-out buffer; and
2398 * 5) transfer the resulting XOR data to the data-in buffer.
2399 */
2400 buf = kmalloc(cmd->data_length, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002401 if (!buf) {
2402 pr_err("Unable to allocate xor_callback buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002403 return;
2404 }
2405 /*
Andy Groverec98f782011-07-20 19:28:46 +00002406 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002407 * into the locally allocated *buf
2408 */
Andy Groverec98f782011-07-20 19:28:46 +00002409 sg_copy_to_buffer(cmd->t_data_sg,
2410 cmd->t_data_nents,
2411 buf,
2412 cmd->data_length);
2413
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002414 /*
2415 * Now perform the XOR against the BIDI read memory located at
Andy Grovera1d8b492011-05-02 17:12:10 -07002416 * cmd->t_mem_bidi_list
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002417 */
2418
2419 offset = 0;
Andy Groverec98f782011-07-20 19:28:46 +00002420 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2421 addr = kmap_atomic(sg_page(sg), KM_USER0);
2422 if (!addr)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002423 goto out;
2424
Andy Groverec98f782011-07-20 19:28:46 +00002425 for (i = 0; i < sg->length; i++)
2426 *(addr + sg->offset + i) ^= *(buf + offset + i);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002427
Andy Groverec98f782011-07-20 19:28:46 +00002428 offset += sg->length;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002429 kunmap_atomic(addr, KM_USER0);
2430 }
Andy Groverec98f782011-07-20 19:28:46 +00002431
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002432out:
2433 kfree(buf);
2434}
2435
2436/*
2437 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2438 */
2439static int transport_get_sense_data(struct se_cmd *cmd)
2440{
2441 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002442 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002443 struct se_task *task = NULL, *task_tmp;
2444 unsigned long flags;
2445 u32 offset = 0;
2446
Andy Grovere3d6f902011-07-19 08:55:10 +00002447 WARN_ON(!cmd->se_lun);
2448
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002449 if (!dev)
2450 return 0;
2451
Andy Grovera1d8b492011-05-02 17:12:10 -07002452 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002453 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002454 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002455 return 0;
2456 }
2457
2458 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07002459 &cmd->t_task_list, t_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002460 if (!task->task_sense)
2461 continue;
2462
Andy Grovere3d6f902011-07-19 08:55:10 +00002463 if (!dev->transport->get_sense_buffer) {
Andy Grover6708bb22011-06-08 10:36:43 -07002464 pr_err("dev->transport->get_sense_buffer"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002465 " is NULL\n");
2466 continue;
2467 }
2468
Andy Grovere3d6f902011-07-19 08:55:10 +00002469 sense_buffer = dev->transport->get_sense_buffer(task);
Andy Grover6708bb22011-06-08 10:36:43 -07002470 if (!sense_buffer) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04002471 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002472 " sense buffer for task with sense\n",
Christoph Hellwig04629b72011-10-12 11:07:04 -04002473 cmd->se_tfo->get_task_tag(cmd), task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002474 continue;
2475 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002476 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002477
Andy Grovere3d6f902011-07-19 08:55:10 +00002478 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002479 TRANSPORT_SENSE_BUFFER);
2480
Andy Grover5951146d2011-07-19 10:26:37 +00002481 memcpy(&buffer[offset], sense_buffer,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002482 TRANSPORT_SENSE_BUFFER);
2483 cmd->scsi_status = task->task_scsi_status;
2484 /* Automatically padded */
2485 cmd->scsi_sense_length =
2486 (TRANSPORT_SENSE_BUFFER + offset);
2487
Andy Grover6708bb22011-06-08 10:36:43 -07002488 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002489 " and sense\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002490 dev->se_hba->hba_id, dev->transport->name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002491 cmd->scsi_status);
2492 return 0;
2493 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002494 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002495
2496 return -1;
2497}
2498
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002499static int
2500transport_handle_reservation_conflict(struct se_cmd *cmd)
2501{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002502 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2503 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2504 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2505 /*
2506 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2507 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2508 * CONFLICT STATUS.
2509 *
2510 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2511 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002512 if (cmd->se_sess &&
2513 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
2514 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002515 cmd->orig_fe_lun, 0x2C,
2516 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
Andy Grover5951146d2011-07-19 10:26:37 +00002517 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002518}
2519
Andy Groverec98f782011-07-20 19:28:46 +00002520static inline long long transport_dev_end_lba(struct se_device *dev)
2521{
2522 return dev->transport->get_blocks(dev) + 1;
2523}
2524
2525static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2526{
2527 struct se_device *dev = cmd->se_dev;
2528 u32 sectors;
2529
2530 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2531 return 0;
2532
2533 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2534
Andy Grover6708bb22011-06-08 10:36:43 -07002535 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2536 pr_err("LBA: %llu Sectors: %u exceeds"
Andy Groverec98f782011-07-20 19:28:46 +00002537 " transport_dev_end_lba(): %llu\n",
2538 cmd->t_task_lba, sectors,
2539 transport_dev_end_lba(dev));
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002540 return -EINVAL;
Andy Groverec98f782011-07-20 19:28:46 +00002541 }
2542
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002543 return 0;
Andy Groverec98f782011-07-20 19:28:46 +00002544}
2545
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002546static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2547{
2548 /*
2549 * Determine if the received WRITE_SAME is used to for direct
2550 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2551 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2552 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2553 */
2554 int passthrough = (dev->transport->transport_type ==
2555 TRANSPORT_PLUGIN_PHBA_PDEV);
2556
2557 if (!passthrough) {
2558 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2559 pr_err("WRITE_SAME PBDATA and LBDATA"
2560 " bits not supported for Block Discard"
2561 " Emulation\n");
2562 return -ENOSYS;
2563 }
2564 /*
2565 * Currently for the emulated case we only accept
2566 * tpws with the UNMAP=1 bit set.
2567 */
2568 if (!(flags[0] & 0x08)) {
2569 pr_err("WRITE_SAME w/o UNMAP bit not"
2570 " supported for Block Discard Emulation\n");
2571 return -ENOSYS;
2572 }
2573 }
2574
2575 return 0;
2576}
2577
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002578/* transport_generic_cmd_sequencer():
2579 *
2580 * Generic Command Sequencer that should work for most DAS transport
2581 * drivers.
2582 *
2583 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2584 * RX Thread.
2585 *
2586 * FIXME: Need to support other SCSI OPCODES where as well.
2587 */
2588static int transport_generic_cmd_sequencer(
2589 struct se_cmd *cmd,
2590 unsigned char *cdb)
2591{
Andy Grover5951146d2011-07-19 10:26:37 +00002592 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002593 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2594 int ret = 0, sector_ret = 0, passthrough;
2595 u32 sectors = 0, size = 0, pr_reg_type = 0;
2596 u16 service_action;
2597 u8 alua_ascq = 0;
2598 /*
2599 * Check for an existing UNIT ATTENTION condition
2600 */
2601 if (core_scsi3_ua_check(cmd, cdb) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002602 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2603 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
Andy Grover5951146d2011-07-19 10:26:37 +00002604 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002605 }
2606 /*
2607 * Check status of Asymmetric Logical Unit Assignment port
2608 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002609 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002610 if (ret != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002611 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002612 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002613 * The ALUA additional sense code qualifier (ASCQ) is determined
2614 * by the ALUA primary or secondary access state..
2615 */
2616 if (ret > 0) {
2617#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002618 pr_debug("[%s]: ALUA TG Port not available,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002619 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002620 cmd->se_tfo->get_fabric_name(), alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002621#endif
2622 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2623 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2624 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
Andy Grover5951146d2011-07-19 10:26:37 +00002625 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002626 }
2627 goto out_invalid_cdb_field;
2628 }
2629 /*
2630 * Check status for SPC-3 Persistent Reservations
2631 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002632 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2633 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002634 cmd, cdb, pr_reg_type) != 0)
2635 return transport_handle_reservation_conflict(cmd);
2636 /*
2637 * This means the CDB is allowed for the SCSI Initiator port
2638 * when said port is *NOT* holding the legacy SPC-2 or
2639 * SPC-3 Persistent Reservation.
2640 */
2641 }
2642
2643 switch (cdb[0]) {
2644 case READ_6:
2645 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2646 if (sector_ret)
2647 goto out_unsupported_cdb;
2648 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002649 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002650 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2651 break;
2652 case READ_10:
2653 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2654 if (sector_ret)
2655 goto out_unsupported_cdb;
2656 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002657 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002658 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2659 break;
2660 case READ_12:
2661 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2662 if (sector_ret)
2663 goto out_unsupported_cdb;
2664 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002665 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002666 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2667 break;
2668 case READ_16:
2669 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2670 if (sector_ret)
2671 goto out_unsupported_cdb;
2672 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002673 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002674 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2675 break;
2676 case WRITE_6:
2677 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2678 if (sector_ret)
2679 goto out_unsupported_cdb;
2680 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002681 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002682 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2683 break;
2684 case WRITE_10:
2685 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2686 if (sector_ret)
2687 goto out_unsupported_cdb;
2688 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002689 cmd->t_task_lba = transport_lba_32(cdb);
2690 cmd->t_tasks_fua = (cdb[1] & 0x8);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002691 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2692 break;
2693 case WRITE_12:
2694 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2695 if (sector_ret)
2696 goto out_unsupported_cdb;
2697 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002698 cmd->t_task_lba = transport_lba_32(cdb);
2699 cmd->t_tasks_fua = (cdb[1] & 0x8);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002700 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2701 break;
2702 case WRITE_16:
2703 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2704 if (sector_ret)
2705 goto out_unsupported_cdb;
2706 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002707 cmd->t_task_lba = transport_lba_64(cdb);
2708 cmd->t_tasks_fua = (cdb[1] & 0x8);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002709 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2710 break;
2711 case XDWRITEREAD_10:
2712 if ((cmd->data_direction != DMA_TO_DEVICE) ||
Andy Grovera1d8b492011-05-02 17:12:10 -07002713 !(cmd->t_tasks_bidi))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002714 goto out_invalid_cdb_field;
2715 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2716 if (sector_ret)
2717 goto out_unsupported_cdb;
2718 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002719 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002720 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002721
2722 if (dev->transport->transport_type ==
2723 TRANSPORT_PLUGIN_PHBA_PDEV)
2724 goto out_unsupported_cdb;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002725 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002726 * Setup BIDI XOR callback to be run after I/O completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002727 */
2728 cmd->transport_complete_callback = &transport_xor_callback;
Andy Grovera1d8b492011-05-02 17:12:10 -07002729 cmd->t_tasks_fua = (cdb[1] & 0x8);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002730 break;
2731 case VARIABLE_LENGTH_CMD:
2732 service_action = get_unaligned_be16(&cdb[8]);
2733 /*
2734 * Determine if this is TCM/PSCSI device and we should disable
2735 * internal emulation for this CDB.
2736 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002737 passthrough = (dev->transport->transport_type ==
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002738 TRANSPORT_PLUGIN_PHBA_PDEV);
2739
2740 switch (service_action) {
2741 case XDWRITEREAD_32:
2742 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2743 if (sector_ret)
2744 goto out_unsupported_cdb;
2745 size = transport_get_size(sectors, cdb, cmd);
2746 /*
2747 * Use WRITE_32 and READ_32 opcodes for the emulated
2748 * XDWRITE_READ_32 logic.
2749 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002750 cmd->t_task_lba = transport_lba_64_ext(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002751 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2752
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002753 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002754 goto out_unsupported_cdb;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002755 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002756 * Setup BIDI XOR callback to be run during after I/O
2757 * completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002758 */
2759 cmd->transport_complete_callback = &transport_xor_callback;
Andy Grovera1d8b492011-05-02 17:12:10 -07002760 cmd->t_tasks_fua = (cdb[10] & 0x8);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002761 break;
2762 case WRITE_SAME_32:
2763 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2764 if (sector_ret)
2765 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002766
Andy Grover6708bb22011-06-08 10:36:43 -07002767 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002768 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002769 else {
2770 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2771 " supported\n");
2772 goto out_invalid_cdb_field;
2773 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002774
Andy Grovera1d8b492011-05-02 17:12:10 -07002775 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002776 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2777
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002778 if (target_check_write_same_discard(&cdb[10], dev) < 0)
2779 goto out_invalid_cdb_field;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002780
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002781 break;
2782 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002783 pr_err("VARIABLE_LENGTH_CMD service action"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002784 " 0x%04x not supported\n", service_action);
2785 goto out_unsupported_cdb;
2786 }
2787 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002788 case MAINTENANCE_IN:
Andy Grovere3d6f902011-07-19 08:55:10 +00002789 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002790 /* MAINTENANCE_IN from SCC-2 */
2791 /*
2792 * Check for emulated MI_REPORT_TARGET_PGS.
2793 */
2794 if (cdb[1] == MI_REPORT_TARGET_PGS) {
2795 cmd->transport_emulate_cdb =
Andy Grovere3d6f902011-07-19 08:55:10 +00002796 (su_dev->t10_alua.alua_type ==
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002797 SPC3_ALUA_EMULATED) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00002798 core_emulate_report_target_port_groups :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002799 NULL;
2800 }
2801 size = (cdb[6] << 24) | (cdb[7] << 16) |
2802 (cdb[8] << 8) | cdb[9];
2803 } else {
2804 /* GPCMD_SEND_KEY from multi media commands */
2805 size = (cdb[8] << 8) + cdb[9];
2806 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002807 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002808 break;
2809 case MODE_SELECT:
2810 size = cdb[4];
2811 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2812 break;
2813 case MODE_SELECT_10:
2814 size = (cdb[7] << 8) + cdb[8];
2815 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2816 break;
2817 case MODE_SENSE:
2818 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002819 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002820 break;
2821 case MODE_SENSE_10:
2822 case GPCMD_READ_BUFFER_CAPACITY:
2823 case GPCMD_SEND_OPC:
2824 case LOG_SELECT:
2825 case LOG_SENSE:
2826 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002827 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002828 break;
2829 case READ_BLOCK_LIMITS:
2830 size = READ_BLOCK_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002831 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002832 break;
2833 case GPCMD_GET_CONFIGURATION:
2834 case GPCMD_READ_FORMAT_CAPACITIES:
2835 case GPCMD_READ_DISC_INFO:
2836 case GPCMD_READ_TRACK_RZONE_INFO:
2837 size = (cdb[7] << 8) + cdb[8];
2838 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2839 break;
2840 case PERSISTENT_RESERVE_IN:
2841 case PERSISTENT_RESERVE_OUT:
2842 cmd->transport_emulate_cdb =
Andy Grovere3d6f902011-07-19 08:55:10 +00002843 (su_dev->t10_pr.res_type ==
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002844 SPC3_PERSISTENT_RESERVATIONS) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00002845 core_scsi3_emulate_pr : NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002846 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002847 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002848 break;
2849 case GPCMD_MECHANISM_STATUS:
2850 case GPCMD_READ_DVD_STRUCTURE:
2851 size = (cdb[8] << 8) + cdb[9];
2852 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2853 break;
2854 case READ_POSITION:
2855 size = READ_POSITION_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002856 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002857 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002858 case MAINTENANCE_OUT:
Andy Grovere3d6f902011-07-19 08:55:10 +00002859 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002860 /* MAINTENANCE_OUT from SCC-2
2861 *
2862 * Check for emulated MO_SET_TARGET_PGS.
2863 */
2864 if (cdb[1] == MO_SET_TARGET_PGS) {
2865 cmd->transport_emulate_cdb =
Andy Grovere3d6f902011-07-19 08:55:10 +00002866 (su_dev->t10_alua.alua_type ==
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002867 SPC3_ALUA_EMULATED) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00002868 core_emulate_set_target_port_groups :
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002869 NULL;
2870 }
2871
2872 size = (cdb[6] << 24) | (cdb[7] << 16) |
2873 (cdb[8] << 8) | cdb[9];
2874 } else {
2875 /* GPCMD_REPORT_KEY from multi media commands */
2876 size = (cdb[8] << 8) + cdb[9];
2877 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002878 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002879 break;
2880 case INQUIRY:
2881 size = (cdb[3] << 8) + cdb[4];
2882 /*
2883 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2884 * See spc4r17 section 5.3
2885 */
Andy Grover5951146d2011-07-19 10:26:37 +00002886 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002887 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002888 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002889 break;
2890 case READ_BUFFER:
2891 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002892 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002893 break;
2894 case READ_CAPACITY:
2895 size = READ_CAP_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002896 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002897 break;
2898 case READ_MEDIA_SERIAL_NUMBER:
2899 case SECURITY_PROTOCOL_IN:
2900 case SECURITY_PROTOCOL_OUT:
2901 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002902 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002903 break;
2904 case SERVICE_ACTION_IN:
2905 case ACCESS_CONTROL_IN:
2906 case ACCESS_CONTROL_OUT:
2907 case EXTENDED_COPY:
2908 case READ_ATTRIBUTE:
2909 case RECEIVE_COPY_RESULTS:
2910 case WRITE_ATTRIBUTE:
2911 size = (cdb[10] << 24) | (cdb[11] << 16) |
2912 (cdb[12] << 8) | cdb[13];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002913 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002914 break;
2915 case RECEIVE_DIAGNOSTIC:
2916 case SEND_DIAGNOSTIC:
2917 size = (cdb[3] << 8) | cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002918 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002919 break;
2920/* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2921#if 0
2922 case GPCMD_READ_CD:
2923 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2924 size = (2336 * sectors);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002925 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002926 break;
2927#endif
2928 case READ_TOC:
2929 size = cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002930 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002931 break;
2932 case REQUEST_SENSE:
2933 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002934 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002935 break;
2936 case READ_ELEMENT_STATUS:
2937 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002938 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002939 break;
2940 case WRITE_BUFFER:
2941 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002942 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002943 break;
2944 case RESERVE:
2945 case RESERVE_10:
2946 /*
2947 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2948 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2949 */
2950 if (cdb[0] == RESERVE_10)
2951 size = (cdb[7] << 8) | cdb[8];
2952 else
2953 size = cmd->data_length;
2954
2955 /*
2956 * Setup the legacy emulated handler for SPC-2 and
2957 * >= SPC-3 compatible reservation handling (CRH=1)
2958 * Otherwise, we assume the underlying SCSI logic is
2959 * is running in SPC_PASSTHROUGH, and wants reservations
2960 * emulation disabled.
2961 */
2962 cmd->transport_emulate_cdb =
Andy Grovere3d6f902011-07-19 08:55:10 +00002963 (su_dev->t10_pr.res_type !=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002964 SPC_PASSTHROUGH) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00002965 core_scsi2_emulate_crh : NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002966 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2967 break;
2968 case RELEASE:
2969 case RELEASE_10:
2970 /*
2971 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2972 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2973 */
2974 if (cdb[0] == RELEASE_10)
2975 size = (cdb[7] << 8) | cdb[8];
2976 else
2977 size = cmd->data_length;
2978
2979 cmd->transport_emulate_cdb =
Andy Grovere3d6f902011-07-19 08:55:10 +00002980 (su_dev->t10_pr.res_type !=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002981 SPC_PASSTHROUGH) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00002982 core_scsi2_emulate_crh : NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002983 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2984 break;
2985 case SYNCHRONIZE_CACHE:
2986 case 0x91: /* SYNCHRONIZE_CACHE_16: */
2987 /*
2988 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2989 */
2990 if (cdb[0] == SYNCHRONIZE_CACHE) {
2991 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002992 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002993 } else {
2994 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002995 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002996 }
2997 if (sector_ret)
2998 goto out_unsupported_cdb;
2999
3000 size = transport_get_size(sectors, cdb, cmd);
3001 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3002
3003 /*
3004 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3005 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003006 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003007 break;
3008 /*
3009 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3010 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3011 */
3012 cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
3013 /*
3014 * Check to ensure that LBA + Range does not exceed past end of
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07003015 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003016 */
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07003017 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
3018 if (transport_cmd_get_valid_sectors(cmd) < 0)
3019 goto out_invalid_cdb_field;
3020 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003021 break;
3022 case UNMAP:
3023 size = get_unaligned_be16(&cdb[7]);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003024 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003025 break;
3026 case WRITE_SAME_16:
3027 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3028 if (sector_ret)
3029 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07003030
Andy Grover6708bb22011-06-08 10:36:43 -07003031 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07003032 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07003033 else {
3034 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3035 goto out_invalid_cdb_field;
3036 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07003037
Nicholas Bellinger5db07532011-07-27 22:18:52 -07003038 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003039 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003040
3041 if (target_check_write_same_discard(&cdb[1], dev) < 0)
3042 goto out_invalid_cdb_field;
3043 break;
3044 case WRITE_SAME:
3045 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3046 if (sector_ret)
3047 goto out_unsupported_cdb;
3048
3049 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07003050 size = transport_get_size(1, cdb, cmd);
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003051 else {
3052 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3053 goto out_invalid_cdb_field;
3054 }
3055
3056 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
3057 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3058 /*
3059 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3060 * of byte 1 bit 3 UNMAP instead of original reserved field
3061 */
3062 if (target_check_write_same_discard(&cdb[1], dev) < 0)
3063 goto out_invalid_cdb_field;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003064 break;
3065 case ALLOW_MEDIUM_REMOVAL:
3066 case GPCMD_CLOSE_TRACK:
3067 case ERASE:
3068 case INITIALIZE_ELEMENT_STATUS:
3069 case GPCMD_LOAD_UNLOAD:
3070 case REZERO_UNIT:
3071 case SEEK_10:
3072 case GPCMD_SET_SPEED:
3073 case SPACE:
3074 case START_STOP:
3075 case TEST_UNIT_READY:
3076 case VERIFY:
3077 case WRITE_FILEMARKS:
3078 case MOVE_MEDIUM:
3079 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3080 break;
3081 case REPORT_LUNS:
3082 cmd->transport_emulate_cdb =
Andy Grovere3d6f902011-07-19 08:55:10 +00003083 transport_core_report_lun_response;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003084 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3085 /*
3086 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3087 * See spc4r17 section 5.3
3088 */
Andy Grover5951146d2011-07-19 10:26:37 +00003089 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003090 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003091 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003092 break;
3093 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003094 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003095 " 0x%02x, sending CHECK_CONDITION.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003096 cmd->se_tfo->get_fabric_name(), cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003097 goto out_unsupported_cdb;
3098 }
3099
3100 if (size != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003101 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003102 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
Andy Grovere3d6f902011-07-19 08:55:10 +00003103 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003104 cmd->data_length, size, cdb[0]);
3105
3106 cmd->cmd_spdtl = size;
3107
3108 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grover6708bb22011-06-08 10:36:43 -07003109 pr_err("Rejecting underflow/overflow"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003110 " WRITE data\n");
3111 goto out_invalid_cdb_field;
3112 }
3113 /*
3114 * Reject READ_* or WRITE_* with overflow/underflow for
3115 * type SCF_SCSI_DATA_SG_IO_CDB.
3116 */
Andy Grover6708bb22011-06-08 10:36:43 -07003117 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3118 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003119 " CDB on non 512-byte sector setup subsystem"
Andy Grovere3d6f902011-07-19 08:55:10 +00003120 " plugin: %s\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003121 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3122 goto out_invalid_cdb_field;
3123 }
3124
3125 if (size > cmd->data_length) {
3126 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3127 cmd->residual_count = (size - cmd->data_length);
3128 } else {
3129 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3130 cmd->residual_count = (cmd->data_length - size);
3131 }
3132 cmd->data_length = size;
3133 }
3134
Andy Groverd0229ae2011-07-08 17:04:53 -07003135 /* Let's limit control cdbs to a page, for simplicity's sake. */
3136 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3137 size > PAGE_SIZE)
3138 goto out_invalid_cdb_field;
3139
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003140 transport_set_supported_SAM_opcode(cmd);
3141 return ret;
3142
3143out_unsupported_cdb:
3144 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3145 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover5951146d2011-07-19 10:26:37 +00003146 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147out_invalid_cdb_field:
3148 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3149 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00003150 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003151}
3152
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003153/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003154 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003155 * and ordered cmds need to have their tasks added to the execution queue.
3156 */
3157static void transport_complete_task_attr(struct se_cmd *cmd)
3158{
Andy Grover5951146d2011-07-19 10:26:37 +00003159 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003160 struct se_cmd *cmd_p, *cmd_tmp;
3161 int new_active_tasks = 0;
3162
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003163 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003164 atomic_dec(&dev->simple_cmds);
3165 smp_mb__after_atomic_dec();
3166 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003167 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003168 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3169 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003170 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003171 atomic_dec(&dev->dev_hoq_count);
3172 smp_mb__after_atomic_dec();
3173 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003174 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003175 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3176 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003177 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003178 spin_lock(&dev->ordered_cmd_lock);
Andy Grover5951146d2011-07-19 10:26:37 +00003179 list_del(&cmd->se_ordered_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003180 atomic_dec(&dev->dev_ordered_sync);
3181 smp_mb__after_atomic_dec();
3182 spin_unlock(&dev->ordered_cmd_lock);
3183
3184 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003185 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003186 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3187 }
3188 /*
3189 * Process all commands up to the last received
3190 * ORDERED task attribute which requires another blocking
3191 * boundary
3192 */
3193 spin_lock(&dev->delayed_cmd_lock);
3194 list_for_each_entry_safe(cmd_p, cmd_tmp,
Andy Grover5951146d2011-07-19 10:26:37 +00003195 &dev->delayed_cmd_list, se_delayed_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003196
Andy Grover5951146d2011-07-19 10:26:37 +00003197 list_del(&cmd_p->se_delayed_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198 spin_unlock(&dev->delayed_cmd_lock);
3199
Andy Grover6708bb22011-06-08 10:36:43 -07003200 pr_debug("Calling add_tasks() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003201 " cmd_p: 0x%02x Task Attr: 0x%02x"
3202 " Dormant -> Active, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07003203 cmd_p->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003204 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3205
3206 transport_add_tasks_from_cmd(cmd_p);
3207 new_active_tasks++;
3208
3209 spin_lock(&dev->delayed_cmd_lock);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003210 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003211 break;
3212 }
3213 spin_unlock(&dev->delayed_cmd_lock);
3214 /*
3215 * If new tasks have become active, wake up the transport thread
3216 * to do the processing of the Active tasks.
3217 */
3218 if (new_active_tasks != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003219 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003220}
3221
Christoph Hellwige057f532011-10-17 13:56:41 -04003222static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003223{
3224 int ret = 0;
3225
Christoph Hellwige057f532011-10-17 13:56:41 -04003226 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3227 transport_complete_task_attr(cmd);
3228
3229 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3230 ret = cmd->se_tfo->queue_status(cmd);
3231 if (ret)
3232 goto out;
3233 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003234
3235 switch (cmd->data_direction) {
3236 case DMA_FROM_DEVICE:
3237 ret = cmd->se_tfo->queue_data_in(cmd);
3238 break;
3239 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00003240 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003241 ret = cmd->se_tfo->queue_data_in(cmd);
3242 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04003243 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003244 }
3245 /* Fall through for DMA_TO_DEVICE */
3246 case DMA_NONE:
3247 ret = cmd->se_tfo->queue_status(cmd);
3248 break;
3249 default:
3250 break;
3251 }
3252
Christoph Hellwige057f532011-10-17 13:56:41 -04003253out:
3254 if (ret < 0) {
3255 transport_handle_queue_full(cmd, cmd->se_dev);
3256 return;
3257 }
3258 transport_lun_remove_cmd(cmd);
3259 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003260}
3261
3262static void transport_handle_queue_full(
3263 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04003264 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003265{
3266 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003267 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3268 atomic_inc(&dev->dev_qf_count);
3269 smp_mb__after_atomic_inc();
3270 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3271
3272 schedule_work(&cmd->se_dev->qf_work_queue);
3273}
3274
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003275static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003276{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003277 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003278 int reason = 0, ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003279
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003280 /*
3281 * Check if we need to move delayed/dormant tasks from cmds on the
3282 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3283 * Attribute.
3284 */
Andy Grover5951146d2011-07-19 10:26:37 +00003285 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003286 transport_complete_task_attr(cmd);
3287 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003288 * Check to schedule QUEUE_FULL work, or execute an existing
3289 * cmd->transport_qf_callback()
3290 */
3291 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3292 schedule_work(&cmd->se_dev->qf_work_queue);
3293
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003294 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295 * Check if we need to retrieve a sense buffer from
3296 * the struct se_cmd in question.
3297 */
3298 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3299 if (transport_get_sense_data(cmd) < 0)
3300 reason = TCM_NON_EXISTENT_LUN;
3301
3302 /*
3303 * Only set when an struct se_task->task_scsi_status returned
3304 * a non GOOD status.
3305 */
3306 if (cmd->scsi_status) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003307 ret = transport_send_check_condition_and_sense(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003308 cmd, reason, 1);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003309 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003310 goto queue_full;
3311
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003312 transport_lun_remove_cmd(cmd);
3313 transport_cmd_check_stop_to_fabric(cmd);
3314 return;
3315 }
3316 }
3317 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003318 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003319 * XDWRITE_READ_10 emulation.
3320 */
3321 if (cmd->transport_complete_callback)
3322 cmd->transport_complete_callback(cmd);
3323
3324 switch (cmd->data_direction) {
3325 case DMA_FROM_DEVICE:
3326 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003327 if (cmd->se_lun->lun_sep) {
3328 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003329 cmd->data_length;
3330 }
3331 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003332
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003333 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003334 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003335 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003336 break;
3337 case DMA_TO_DEVICE:
3338 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003339 if (cmd->se_lun->lun_sep) {
3340 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003341 cmd->data_length;
3342 }
3343 spin_unlock(&cmd->se_lun->lun_sep_lock);
3344 /*
3345 * Check if we need to send READ payload for BIDI-COMMAND
3346 */
Andy Groverec98f782011-07-20 19:28:46 +00003347 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003348 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003349 if (cmd->se_lun->lun_sep) {
3350 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003351 cmd->data_length;
3352 }
3353 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003354 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003355 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003356 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003357 break;
3358 }
3359 /* Fall through for DMA_TO_DEVICE */
3360 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003361 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003362 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003363 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003364 break;
3365 default:
3366 break;
3367 }
3368
3369 transport_lun_remove_cmd(cmd);
3370 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003371 return;
3372
3373queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003374 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003375 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04003376 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3377 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003378}
3379
3380static void transport_free_dev_tasks(struct se_cmd *cmd)
3381{
3382 struct se_task *task, *task_tmp;
3383 unsigned long flags;
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003384 LIST_HEAD(dispose_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003385
Andy Grovera1d8b492011-05-02 17:12:10 -07003386 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003387 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07003388 &cmd->t_task_list, t_list) {
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003389 if (!(task->task_flags & TF_ACTIVE))
3390 list_move_tail(&task->t_list, &dispose_list);
3391 }
3392 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3393
3394 while (!list_empty(&dispose_list)) {
3395 task = list_first_entry(&dispose_list, struct se_task, t_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003396
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003397 if (task->task_sg != cmd->t_data_sg &&
3398 task->task_sg != cmd->t_bidi_data_sg)
3399 kfree(task->task_sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003400
3401 list_del(&task->t_list);
3402
Christoph Hellwig42bf8292011-10-12 11:07:00 -04003403 cmd->se_dev->transport->free_task(task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003404 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003405}
3406
Andy Grover6708bb22011-06-08 10:36:43 -07003407static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003408{
Andy Groverec98f782011-07-20 19:28:46 +00003409 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00003410 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003411
Andy Grover6708bb22011-06-08 10:36:43 -07003412 for_each_sg(sgl, sg, nents, count)
3413 __free_page(sg_page(sg));
3414
3415 kfree(sgl);
3416}
3417
3418static inline void transport_free_pages(struct se_cmd *cmd)
3419{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003420 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07003421 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003422
Andy Grover6708bb22011-06-08 10:36:43 -07003423 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003424 cmd->t_data_sg = NULL;
3425 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003426
Andy Grover6708bb22011-06-08 10:36:43 -07003427 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003428 cmd->t_bidi_data_sg = NULL;
3429 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003430}
3431
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003432/**
3433 * transport_put_cmd - release a reference to a command
3434 * @cmd: command to release
3435 *
3436 * This routine releases our reference to the command and frees it if possible.
3437 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003438static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003439{
3440 unsigned long flags;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003441 int free_tasks = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003442
Andy Grovera1d8b492011-05-02 17:12:10 -07003443 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003444 if (atomic_read(&cmd->t_fe_count)) {
3445 if (!atomic_dec_and_test(&cmd->t_fe_count))
3446 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003447 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003448
3449 if (atomic_read(&cmd->t_se_count)) {
3450 if (!atomic_dec_and_test(&cmd->t_se_count))
3451 goto out_busy;
3452 }
3453
3454 if (atomic_read(&cmd->transport_dev_active)) {
3455 atomic_set(&cmd->transport_dev_active, 0);
3456 transport_all_task_dev_remove_state(cmd);
3457 free_tasks = 1;
3458 }
Andy Grovera1d8b492011-05-02 17:12:10 -07003459 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003460
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003461 if (free_tasks != 0)
3462 transport_free_dev_tasks(cmd);
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003463
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003464 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02003465 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003466 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003467out_busy:
3468 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003469}
3470
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003471/*
Andy Groverec98f782011-07-20 19:28:46 +00003472 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3473 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003474 * @cmd: Associated se_cmd descriptor
3475 * @mem: SGL style memory for TCM WRITE / READ
3476 * @sg_mem_num: Number of SGL elements
3477 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3478 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3479 *
3480 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3481 * of parameters.
3482 */
3483int transport_generic_map_mem_to_cmd(
3484 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00003485 struct scatterlist *sgl,
3486 u32 sgl_count,
3487 struct scatterlist *sgl_bidi,
3488 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003489{
Andy Grover5951146d2011-07-19 10:26:37 +00003490 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003491 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003492
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003493 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3494 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003495
Andy Groverec98f782011-07-20 19:28:46 +00003496 cmd->t_data_sg = sgl;
3497 cmd->t_data_nents = sgl_count;
3498
Andy Grover5951146d2011-07-19 10:26:37 +00003499 if (sgl_bidi && sgl_bidi_count) {
Andy Groverec98f782011-07-20 19:28:46 +00003500 cmd->t_bidi_data_sg = sgl_bidi;
3501 cmd->t_bidi_data_nents = sgl_bidi_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003502 }
3503 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003504 }
3505
3506 return 0;
3507}
3508EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3509
Andy Grover05d1c7c2011-07-20 19:13:28 +00003510void *transport_kmap_first_data_page(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003511{
Andy Groverec98f782011-07-20 19:28:46 +00003512 struct scatterlist *sg = cmd->t_data_sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003513
Andy Groverec98f782011-07-20 19:28:46 +00003514 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003515 /*
Andy Groverec98f782011-07-20 19:28:46 +00003516 * We need to take into account a possible offset here for fabrics like
3517 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3518 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00003519 */
Andy Groverec98f782011-07-20 19:28:46 +00003520 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003521}
3522EXPORT_SYMBOL(transport_kmap_first_data_page);
3523
3524void transport_kunmap_first_data_page(struct se_cmd *cmd)
3525{
Andy Groverec98f782011-07-20 19:28:46 +00003526 kunmap(sg_page(cmd->t_data_sg));
Andy Grover05d1c7c2011-07-20 19:13:28 +00003527}
3528EXPORT_SYMBOL(transport_kunmap_first_data_page);
3529
3530static int
3531transport_generic_get_mem(struct se_cmd *cmd)
3532{
Andy Groverec98f782011-07-20 19:28:46 +00003533 u32 length = cmd->data_length;
3534 unsigned int nents;
3535 struct page *page;
3536 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003537
Andy Groverec98f782011-07-20 19:28:46 +00003538 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3539 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3540 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00003541 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003542
Andy Groverec98f782011-07-20 19:28:46 +00003543 cmd->t_data_nents = nents;
3544 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003545
Andy Groverec98f782011-07-20 19:28:46 +00003546 while (length) {
3547 u32 page_len = min_t(u32, length, PAGE_SIZE);
3548 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3549 if (!page)
3550 goto out;
3551
3552 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3553 length -= page_len;
3554 i++;
3555 }
3556 return 0;
3557
3558out:
3559 while (i >= 0) {
3560 __free_page(sg_page(&cmd->t_data_sg[i]));
3561 i--;
3562 }
3563 kfree(cmd->t_data_sg);
3564 cmd->t_data_sg = NULL;
3565 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003566}
3567
Andy Grovera1d8b492011-05-02 17:12:10 -07003568/* Reduce sectors if they are too long for the device */
3569static inline sector_t transport_limit_task_sectors(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003570 struct se_device *dev,
3571 unsigned long long lba,
Andy Grovera1d8b492011-05-02 17:12:10 -07003572 sector_t sectors)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003573{
Andy Grovera1d8b492011-05-02 17:12:10 -07003574 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003575
Andy Grovera1d8b492011-05-02 17:12:10 -07003576 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3577 if ((lba + sectors) > transport_dev_end_lba(dev))
3578 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003579
Andy Grovera1d8b492011-05-02 17:12:10 -07003580 return sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003581}
3582
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003583
3584/*
3585 * This function can be used by HW target mode drivers to create a linked
3586 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3587 * This is intended to be called during the completion path by TCM Core
3588 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3589 */
3590void transport_do_task_sg_chain(struct se_cmd *cmd)
3591{
Andy Groverec98f782011-07-20 19:28:46 +00003592 struct scatterlist *sg_first = NULL;
3593 struct scatterlist *sg_prev = NULL;
3594 int sg_prev_nents = 0;
3595 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003596 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003597 u32 chained_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003598 int i;
3599
Andy Groverec98f782011-07-20 19:28:46 +00003600 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3601
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003602 /*
3603 * Walk the struct se_task list and setup scatterlist chains
Andy Grovera1d8b492011-05-02 17:12:10 -07003604 * for each contiguously allocated struct se_task->task_sg[].
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003605 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003606 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Andy Groverec98f782011-07-20 19:28:46 +00003607 if (!task->task_sg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003608 continue;
3609
Andy Groverec98f782011-07-20 19:28:46 +00003610 if (!sg_first) {
3611 sg_first = task->task_sg;
Andy Grover6708bb22011-06-08 10:36:43 -07003612 chained_nents = task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003613 } else {
Andy Groverec98f782011-07-20 19:28:46 +00003614 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
Andy Grover6708bb22011-06-08 10:36:43 -07003615 chained_nents += task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003616 }
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003617 /*
3618 * For the padded tasks, use the extra SGL vector allocated
3619 * in transport_allocate_data_tasks() for the sg_prev_nents
Christoph Hellwig04629b72011-10-12 11:07:04 -04003620 * offset into sg_chain() above.
3621 *
3622 * We do not need the padding for the last task (or a single
3623 * task), but in that case we will never use the sg_prev_nents
3624 * value below which would be incorrect.
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003625 */
Christoph Hellwig04629b72011-10-12 11:07:04 -04003626 sg_prev_nents = (task->task_sg_nents + 1);
Andy Groverec98f782011-07-20 19:28:46 +00003627 sg_prev = task->task_sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003628 }
3629 /*
3630 * Setup the starting pointer and total t_tasks_sg_linked_no including
3631 * padding SGs for linking and to mark the end.
3632 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003633 cmd->t_tasks_sg_chained = sg_first;
Andy Groverec98f782011-07-20 19:28:46 +00003634 cmd->t_tasks_sg_chained_no = chained_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003635
Andy Grover6708bb22011-06-08 10:36:43 -07003636 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
Andy Grovera1d8b492011-05-02 17:12:10 -07003637 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3638 cmd->t_tasks_sg_chained_no);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003639
Andy Grovera1d8b492011-05-02 17:12:10 -07003640 for_each_sg(cmd->t_tasks_sg_chained, sg,
3641 cmd->t_tasks_sg_chained_no, i) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003642
Andy Grover6708bb22011-06-08 10:36:43 -07003643 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
Andy Grover5951146d2011-07-19 10:26:37 +00003644 i, sg, sg_page(sg), sg->length, sg->offset);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003645 if (sg_is_chain(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003646 pr_debug("SG: %p sg_is_chain=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003647 if (sg_is_last(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003648 pr_debug("SG: %p sg_is_last=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003649 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003650}
3651EXPORT_SYMBOL(transport_do_task_sg_chain);
3652
Andy Grovera1d8b492011-05-02 17:12:10 -07003653/*
3654 * Break up cmd into chunks transport can handle
3655 */
Christoph Hellwig38b400672011-10-18 06:57:02 -04003656static int
3657transport_allocate_data_tasks(struct se_cmd *cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003658 enum dma_data_direction data_direction,
Christoph Hellwig38b400672011-10-18 06:57:02 -04003659 struct scatterlist *cmd_sg, unsigned int sgl_nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003660{
Andy Grover5951146d2011-07-19 10:26:37 +00003661 struct se_device *dev = cmd->se_dev;
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003662 int task_count, i;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003663 unsigned long long lba;
3664 sector_t sectors, dev_max_sectors;
3665 u32 sector_size;
3666
3667 if (transport_cmd_get_valid_sectors(cmd) < 0)
3668 return -EINVAL;
3669
3670 dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3671 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003672
Andy Groverec98f782011-07-20 19:28:46 +00003673 WARN_ON(cmd->data_length % sector_size);
Christoph Hellwig38b400672011-10-18 06:57:02 -04003674
3675 lba = cmd->t_task_lba;
Andy Groverec98f782011-07-20 19:28:46 +00003676 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
Nicholas Bellinger277c5f22011-07-26 00:38:40 -07003677 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003678
3679 /*
3680 * If we need just a single task reuse the SG list in the command
3681 * and avoid a lot of work.
3682 */
3683 if (task_count == 1) {
3684 struct se_task *task;
3685 unsigned long flags;
3686
3687 task = transport_generic_get_task(cmd, data_direction);
3688 if (!task)
3689 return -ENOMEM;
3690
3691 task->task_sg = cmd_sg;
3692 task->task_sg_nents = sgl_nents;
3693
3694 task->task_lba = lba;
3695 task->task_sectors = sectors;
3696 task->task_size = task->task_sectors * sector_size;
3697
3698 spin_lock_irqsave(&cmd->t_state_lock, flags);
3699 list_add_tail(&task->t_list, &cmd->t_task_list);
3700 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3701
3702 return task_count;
3703 }
3704
Andy Groverec98f782011-07-20 19:28:46 +00003705 for (i = 0; i < task_count; i++) {
Christoph Hellwig38b400672011-10-18 06:57:02 -04003706 struct se_task *task;
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003707 unsigned int task_size, task_sg_nents_padded;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003708 struct scatterlist *sg;
3709 unsigned long flags;
Andy Groverec98f782011-07-20 19:28:46 +00003710 int count;
Andy Grovera1d8b492011-05-02 17:12:10 -07003711
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003712 task = transport_generic_get_task(cmd, data_direction);
Andy Grovera1d8b492011-05-02 17:12:10 -07003713 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003714 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003715
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003716 task->task_lba = lba;
Andy Groverec98f782011-07-20 19:28:46 +00003717 task->task_sectors = min(sectors, dev_max_sectors);
3718 task->task_size = task->task_sectors * sector_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003719
Nicholas Bellinger525a48a2011-08-13 02:11:38 -07003720 /*
3721 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3722 * in order to calculate the number per task SGL entries
3723 */
3724 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003725 /*
Andy Groverec98f782011-07-20 19:28:46 +00003726 * Check if the fabric module driver is requesting that all
3727 * struct se_task->task_sg[] be chained together.. If so,
3728 * then allocate an extra padding SG entry for linking and
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003729 * marking the end of the chained SGL for every task except
3730 * the last one for (task_count > 1) operation, or skipping
3731 * the extra padding for the (task_count == 1) case.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003732 */
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003733 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3734 task_sg_nents_padded = (task->task_sg_nents + 1);
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003735 } else
3736 task_sg_nents_padded = task->task_sg_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003737
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003738 task->task_sg = kmalloc(sizeof(struct scatterlist) *
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003739 task_sg_nents_padded, GFP_KERNEL);
Andy Groverec98f782011-07-20 19:28:46 +00003740 if (!task->task_sg) {
3741 cmd->se_dev->transport->free_task(task);
3742 return -ENOMEM;
3743 }
3744
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003745 sg_init_table(task->task_sg, task_sg_nents_padded);
Andy Groverec98f782011-07-20 19:28:46 +00003746
3747 task_size = task->task_size;
3748
3749 /* Build new sgl, only up to task_size */
Andy Grover6708bb22011-06-08 10:36:43 -07003750 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
Andy Groverec98f782011-07-20 19:28:46 +00003751 if (cmd_sg->length > task_size)
3752 break;
3753
3754 *sg = *cmd_sg;
3755 task_size -= cmd_sg->length;
3756 cmd_sg = sg_next(cmd_sg);
3757 }
3758
3759 lba += task->task_sectors;
3760 sectors -= task->task_sectors;
3761
3762 spin_lock_irqsave(&cmd->t_state_lock, flags);
3763 list_add_tail(&task->t_list, &cmd->t_task_list);
3764 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003765 }
3766
Andy Groverec98f782011-07-20 19:28:46 +00003767 return task_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003768}
3769
3770static int
Andy Groverec98f782011-07-20 19:28:46 +00003771transport_allocate_control_task(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003772{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003773 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003774 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003775
3776 task = transport_generic_get_task(cmd, cmd->data_direction);
3777 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003778 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003779
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003780 task->task_sg = cmd->t_data_sg;
Andy Groverec98f782011-07-20 19:28:46 +00003781 task->task_size = cmd->data_length;
Andy Grover6708bb22011-06-08 10:36:43 -07003782 task->task_sg_nents = cmd->t_data_nents;
Andy Groverec98f782011-07-20 19:28:46 +00003783
3784 spin_lock_irqsave(&cmd->t_state_lock, flags);
3785 list_add_tail(&task->t_list, &cmd->t_task_list);
3786 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003787
Andy Grover6708bb22011-06-08 10:36:43 -07003788 /* Success! Return number of tasks allocated */
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003789 return 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003790}
3791
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003792/*
3793 * Allocate any required ressources to execute the command, and either place
3794 * it on the execution queue if possible. For writes we might not have the
3795 * payload yet, thus notify the fabric via a call to ->write_pending instead.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003796 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003797int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003798{
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003799 struct se_device *dev = cmd->se_dev;
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003800 int task_cdbs, task_cdbs_bidi = 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003801 int set_counts = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003802 int ret = 0;
3803
3804 /*
3805 * Determine is the TCM fabric module has already allocated physical
3806 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00003807 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003808 */
Andy Groverec98f782011-07-20 19:28:46 +00003809 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3810 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00003811 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003812 if (ret < 0)
3813 return ret;
3814 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003815
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003816 /*
Christoph Hellwig38b400672011-10-18 06:57:02 -04003817 * For BIDI command set up the read tasks first.
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003818 */
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003819 if (cmd->t_bidi_data_sg &&
Christoph Hellwig38b400672011-10-18 06:57:02 -04003820 dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
3821 BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
3822
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003823 task_cdbs_bidi = transport_allocate_data_tasks(cmd,
3824 DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
3825 cmd->t_bidi_data_nents);
3826 if (task_cdbs_bidi <= 0)
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003827 goto out_fail;
3828
3829 atomic_inc(&cmd->t_fe_count);
3830 atomic_inc(&cmd->t_se_count);
3831 set_counts = 0;
3832 }
Christoph Hellwig38b400672011-10-18 06:57:02 -04003833
3834 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3835 task_cdbs = transport_allocate_data_tasks(cmd,
3836 cmd->data_direction, cmd->t_data_sg,
3837 cmd->t_data_nents);
3838 } else {
3839 task_cdbs = transport_allocate_control_task(cmd);
3840 }
3841
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003842 if (task_cdbs <= 0)
3843 goto out_fail;
3844
3845 if (set_counts) {
3846 atomic_inc(&cmd->t_fe_count);
3847 atomic_inc(&cmd->t_se_count);
3848 }
3849
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003850 cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
3851 atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
3852 atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003853
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003854 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07003855 * For WRITEs, let the fabric know its buffer is ready..
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003856 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3857 * will be added to the struct se_device execution queue after its WRITE
3858 * data has arrived. (ie: It gets handled by the transport processing
3859 * thread a second time)
3860 */
3861 if (cmd->data_direction == DMA_TO_DEVICE) {
3862 transport_add_tasks_to_state_queue(cmd);
3863 return transport_generic_write_pending(cmd);
3864 }
3865 /*
3866 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3867 * to the execution queue.
3868 */
3869 transport_execute_tasks(cmd);
3870 return 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003871
3872out_fail:
3873 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3874 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3875 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003876}
Andy Grovera1d8b492011-05-02 17:12:10 -07003877EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003878
3879/* transport_generic_process_write():
3880 *
3881 *
3882 */
3883void transport_generic_process_write(struct se_cmd *cmd)
3884{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003885 transport_execute_tasks(cmd);
3886}
3887EXPORT_SYMBOL(transport_generic_process_write);
3888
Christoph Hellwige057f532011-10-17 13:56:41 -04003889static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003890{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003891 int ret;
3892
3893 ret = cmd->se_tfo->write_pending(cmd);
3894 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04003895 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3896 cmd);
3897 transport_handle_queue_full(cmd, cmd->se_dev);
3898 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003899}
3900
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003901static int transport_generic_write_pending(struct se_cmd *cmd)
3902{
3903 unsigned long flags;
3904 int ret;
3905
Andy Grovera1d8b492011-05-02 17:12:10 -07003906 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003907 cmd->t_state = TRANSPORT_WRITE_PENDING;
Andy Grovera1d8b492011-05-02 17:12:10 -07003908 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003909
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003910 /*
3911 * Clear the se_cmd for WRITE_PENDING status in order to set
Andy Grovera1d8b492011-05-02 17:12:10 -07003912 * cmd->t_transport_active=0 so that transport_generic_handle_data
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003913 * can be called from HW target mode interrupt code. This is safe
Andy Grovere3d6f902011-07-19 08:55:10 +00003914 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003915 * because the se_cmd->se_lun pointer is not being cleared.
3916 */
3917 transport_cmd_check_stop(cmd, 1, 0);
3918
3919 /*
3920 * Call the fabric write_pending function here to let the
3921 * frontend know that WRITE buffers are ready.
3922 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003923 ret = cmd->se_tfo->write_pending(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003924 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003925 goto queue_full;
3926 else if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003927 return ret;
3928
3929 return PYX_TRANSPORT_WRITE_PENDING;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003930
3931queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003932 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003933 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
Christoph Hellwige057f532011-10-17 13:56:41 -04003934 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003935 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003936}
3937
Christoph Hellwig2dbc43d2011-09-13 23:08:19 +02003938/**
3939 * transport_release_cmd - free a command
3940 * @cmd: command to free
3941 *
3942 * This routine unconditionally frees a command, and reference counting
3943 * or list removal must be done in the caller.
3944 */
Christoph Hellwig35462972011-05-31 23:56:57 -04003945void transport_release_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003946{
Andy Grovere3d6f902011-07-19 08:55:10 +00003947 BUG_ON(!cmd->se_tfo);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003948
Christoph Hellwig2dbc43d2011-09-13 23:08:19 +02003949 if (cmd->se_tmr_req)
3950 core_tmr_release_req(cmd->se_tmr_req);
3951 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3952 kfree(cmd->t_task_cdb);
Christoph Hellwig35462972011-05-31 23:56:57 -04003953 cmd->se_tfo->release_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003954}
Christoph Hellwig35462972011-05-31 23:56:57 -04003955EXPORT_SYMBOL(transport_release_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003956
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003957void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003958{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003959 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
3960 if (wait_for_tasks && cmd->se_tmr_req)
3961 transport_wait_for_tasks(cmd);
3962
Christoph Hellwig35462972011-05-31 23:56:57 -04003963 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003964 } else {
3965 if (wait_for_tasks)
3966 transport_wait_for_tasks(cmd);
3967
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003968 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
3969
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02003970 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003971 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003972
Nicholas Bellingerf4366772011-05-19 20:19:11 -07003973 transport_free_dev_tasks(cmd);
3974
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003975 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003976 }
3977}
3978EXPORT_SYMBOL(transport_generic_free_cmd);
3979
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003980/* transport_lun_wait_for_tasks():
3981 *
3982 * Called from ConfigFS context to stop the passed struct se_cmd to allow
3983 * an struct se_lun to be successfully shutdown.
3984 */
3985static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
3986{
3987 unsigned long flags;
3988 int ret;
3989 /*
3990 * If the frontend has already requested this struct se_cmd to
3991 * be stopped, we can safely ignore this struct se_cmd.
3992 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003993 spin_lock_irqsave(&cmd->t_state_lock, flags);
3994 if (atomic_read(&cmd->t_transport_stop)) {
3995 atomic_set(&cmd->transport_lun_stop, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07003996 pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
Andy Grovere3d6f902011-07-19 08:55:10 +00003997 " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07003998 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003999 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovere3d6f902011-07-19 08:55:10 +00004000 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004001 }
Andy Grovera1d8b492011-05-02 17:12:10 -07004002 atomic_set(&cmd->transport_lun_fe_stop, 1);
4003 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004004
Andy Grover5951146d2011-07-19 10:26:37 +00004005 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004006
4007 ret = transport_stop_tasks_for_cmd(cmd);
4008
Andy Grover6708bb22011-06-08 10:36:43 -07004009 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4010 " %d\n", cmd, cmd->t_task_list_num, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004011 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07004012 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004013 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004014 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07004015 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004016 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004017 }
Christoph Hellwig3df8d402011-10-17 13:56:43 -04004018 transport_remove_cmd_from_queue(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004019
4020 return 0;
4021}
4022
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004023static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4024{
4025 struct se_cmd *cmd = NULL;
4026 unsigned long lun_flags, cmd_flags;
4027 /*
4028 * Do exception processing and return CHECK_CONDITION status to the
4029 * Initiator Port.
4030 */
4031 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00004032 while (!list_empty(&lun->lun_cmd_list)) {
4033 cmd = list_first_entry(&lun->lun_cmd_list,
4034 struct se_cmd, se_lun_node);
4035 list_del(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004036
Andy Grovera1d8b492011-05-02 17:12:10 -07004037 atomic_set(&cmd->transport_lun_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004038 /*
4039 * This will notify iscsi_target_transport.c:
4040 * transport_cmd_check_stop() that a LUN shutdown is in
4041 * progress for the iscsi_cmd_t.
4042 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004043 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07004044 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004045 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004046 cmd->se_lun->unpacked_lun,
4047 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004048 atomic_set(&cmd->transport_lun_stop, 1);
4049 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004050
4051 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4052
Andy Grover6708bb22011-06-08 10:36:43 -07004053 if (!cmd->se_lun) {
4054 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004055 cmd->se_tfo->get_task_tag(cmd),
4056 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004057 BUG();
4058 }
4059 /*
4060 * If the Storage engine still owns the iscsi_cmd_t, determine
4061 * and/or stop its context.
4062 */
Andy Grover6708bb22011-06-08 10:36:43 -07004063 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00004064 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4065 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004066
Andy Grovere3d6f902011-07-19 08:55:10 +00004067 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004068 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4069 continue;
4070 }
4071
Andy Grover6708bb22011-06-08 10:36:43 -07004072 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004073 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004074 cmd->se_lun->unpacked_lun,
4075 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004076
Andy Grovera1d8b492011-05-02 17:12:10 -07004077 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Andy Grover6708bb22011-06-08 10:36:43 -07004078 if (!atomic_read(&cmd->transport_dev_active)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004079 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004080 goto check_cond;
4081 }
Andy Grovera1d8b492011-05-02 17:12:10 -07004082 atomic_set(&cmd->transport_dev_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004083 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07004084 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004085
4086 transport_free_dev_tasks(cmd);
4087 /*
4088 * The Storage engine stopped this struct se_cmd before it was
4089 * send to the fabric frontend for delivery back to the
4090 * Initiator Node. Return this SCSI CDB back with an
4091 * CHECK_CONDITION status.
4092 */
4093check_cond:
4094 transport_send_check_condition_and_sense(cmd,
4095 TCM_NON_EXISTENT_LUN, 0);
4096 /*
4097 * If the fabric frontend is waiting for this iscsi_cmd_t to
4098 * be released, notify the waiting thread now that LU has
4099 * finished accessing it.
4100 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004101 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4102 if (atomic_read(&cmd->transport_lun_fe_stop)) {
Andy Grover6708bb22011-06-08 10:36:43 -07004103 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004104 " struct se_cmd: %p ITT: 0x%08x\n",
4105 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00004106 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004107
Andy Grovera1d8b492011-05-02 17:12:10 -07004108 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004109 cmd_flags);
4110 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -07004111 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004112 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4113 continue;
4114 }
Andy Grover6708bb22011-06-08 10:36:43 -07004115 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004116 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004117
Andy Grovera1d8b492011-05-02 17:12:10 -07004118 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004119 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4120 }
4121 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4122}
4123
4124static int transport_clear_lun_thread(void *p)
4125{
4126 struct se_lun *lun = (struct se_lun *)p;
4127
4128 __transport_clear_lun_from_sessions(lun);
4129 complete(&lun->lun_shutdown_comp);
4130
4131 return 0;
4132}
4133
4134int transport_clear_lun_from_sessions(struct se_lun *lun)
4135{
4136 struct task_struct *kt;
4137
Andy Grover5951146d2011-07-19 10:26:37 +00004138 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004139 "tcm_cl_%u", lun->unpacked_lun);
4140 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07004141 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00004142 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004143 }
4144 wait_for_completion(&lun->lun_shutdown_comp);
4145
4146 return 0;
4147}
4148
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004149/**
4150 * transport_wait_for_tasks - wait for completion to occur
4151 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004152 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004153 * Called from frontend fabric context to wait for storage engine
4154 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004155 */
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004156void transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004157{
4158 unsigned long flags;
4159
Andy Grovera1d8b492011-05-02 17:12:10 -07004160 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004161 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req)) {
4162 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4163 return;
4164 }
4165 /*
4166 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4167 * has been set in transport_set_supported_SAM_opcode().
4168 */
4169 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && !cmd->se_tmr_req) {
4170 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4171 return;
4172 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004173 /*
4174 * If we are already stopped due to an external event (ie: LUN shutdown)
4175 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07004176 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004177 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4178 * has completed its operation on the struct se_cmd.
4179 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004180 if (atomic_read(&cmd->transport_lun_stop)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004181
Andy Grover6708bb22011-06-08 10:36:43 -07004182 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00004183 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004184 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004185 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004186 /*
4187 * There is a special case for WRITES where a FE exception +
4188 * LUN shutdown means ConfigFS context is still sleeping on
4189 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4190 * We go ahead and up transport_lun_stop_comp just to be sure
4191 * here.
4192 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004193 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4194 complete(&cmd->transport_lun_stop_comp);
4195 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4196 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004197
4198 transport_all_task_dev_remove_state(cmd);
4199 /*
4200 * At this point, the frontend who was the originator of this
4201 * struct se_cmd, now owns the structure and can be released through
4202 * normal means below.
4203 */
Andy Grover6708bb22011-06-08 10:36:43 -07004204 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00004205 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004206 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004207 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004208
Andy Grovera1d8b492011-05-02 17:12:10 -07004209 atomic_set(&cmd->transport_lun_stop, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004210 }
Andy Grovera1d8b492011-05-02 17:12:10 -07004211 if (!atomic_read(&cmd->t_transport_active) ||
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004212 atomic_read(&cmd->t_transport_aborted)) {
4213 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4214 return;
4215 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004216
Andy Grovera1d8b492011-05-02 17:12:10 -07004217 atomic_set(&cmd->t_transport_stop, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004218
Andy Grover6708bb22011-06-08 10:36:43 -07004219 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004220 " i_state: %d, t_state: %d, t_transport_stop = TRUE\n",
4221 cmd, cmd->se_tfo->get_task_tag(cmd),
4222 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004223
Andy Grovera1d8b492011-05-02 17:12:10 -07004224 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004225
Andy Grover5951146d2011-07-19 10:26:37 +00004226 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004227
Andy Grovera1d8b492011-05-02 17:12:10 -07004228 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004229
Andy Grovera1d8b492011-05-02 17:12:10 -07004230 spin_lock_irqsave(&cmd->t_state_lock, flags);
4231 atomic_set(&cmd->t_transport_active, 0);
4232 atomic_set(&cmd->t_transport_stop, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004233
Andy Grover6708bb22011-06-08 10:36:43 -07004234 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07004235 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004236 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004237
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004238 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004239}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004240EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004241
4242static int transport_get_sense_codes(
4243 struct se_cmd *cmd,
4244 u8 *asc,
4245 u8 *ascq)
4246{
4247 *asc = cmd->scsi_asc;
4248 *ascq = cmd->scsi_ascq;
4249
4250 return 0;
4251}
4252
4253static int transport_set_sense_codes(
4254 struct se_cmd *cmd,
4255 u8 asc,
4256 u8 ascq)
4257{
4258 cmd->scsi_asc = asc;
4259 cmd->scsi_ascq = ascq;
4260
4261 return 0;
4262}
4263
4264int transport_send_check_condition_and_sense(
4265 struct se_cmd *cmd,
4266 u8 reason,
4267 int from_transport)
4268{
4269 unsigned char *buffer = cmd->sense_buffer;
4270 unsigned long flags;
4271 int offset;
4272 u8 asc = 0, ascq = 0;
4273
Andy Grovera1d8b492011-05-02 17:12:10 -07004274 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004275 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004276 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004277 return 0;
4278 }
4279 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07004280 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004281
4282 if (!reason && from_transport)
4283 goto after_reason;
4284
4285 if (!from_transport)
4286 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4287 /*
4288 * Data Segment and SenseLength of the fabric response PDU.
4289 *
4290 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4291 * from include/scsi/scsi_cmnd.h
4292 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004293 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004294 TRANSPORT_SENSE_BUFFER);
4295 /*
4296 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4297 * SENSE KEY values from include/scsi/scsi.h
4298 */
4299 switch (reason) {
4300 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004301 /* CURRENT ERROR */
4302 buffer[offset] = 0x70;
4303 /* ILLEGAL REQUEST */
4304 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4305 /* LOGICAL UNIT NOT SUPPORTED */
4306 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4307 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004308 case TCM_UNSUPPORTED_SCSI_OPCODE:
4309 case TCM_SECTOR_COUNT_TOO_MANY:
4310 /* CURRENT ERROR */
4311 buffer[offset] = 0x70;
4312 /* ILLEGAL REQUEST */
4313 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4314 /* INVALID COMMAND OPERATION CODE */
4315 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4316 break;
4317 case TCM_UNKNOWN_MODE_PAGE:
4318 /* CURRENT ERROR */
4319 buffer[offset] = 0x70;
4320 /* ILLEGAL REQUEST */
4321 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4322 /* INVALID FIELD IN CDB */
4323 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4324 break;
4325 case TCM_CHECK_CONDITION_ABORT_CMD:
4326 /* CURRENT ERROR */
4327 buffer[offset] = 0x70;
4328 /* ABORTED COMMAND */
4329 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4330 /* BUS DEVICE RESET FUNCTION OCCURRED */
4331 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4332 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4333 break;
4334 case TCM_INCORRECT_AMOUNT_OF_DATA:
4335 /* CURRENT ERROR */
4336 buffer[offset] = 0x70;
4337 /* ABORTED COMMAND */
4338 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4339 /* WRITE ERROR */
4340 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4341 /* NOT ENOUGH UNSOLICITED DATA */
4342 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4343 break;
4344 case TCM_INVALID_CDB_FIELD:
4345 /* CURRENT ERROR */
4346 buffer[offset] = 0x70;
4347 /* ABORTED COMMAND */
4348 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4349 /* INVALID FIELD IN CDB */
4350 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4351 break;
4352 case TCM_INVALID_PARAMETER_LIST:
4353 /* CURRENT ERROR */
4354 buffer[offset] = 0x70;
4355 /* ABORTED COMMAND */
4356 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4357 /* INVALID FIELD IN PARAMETER LIST */
4358 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4359 break;
4360 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4361 /* CURRENT ERROR */
4362 buffer[offset] = 0x70;
4363 /* ABORTED COMMAND */
4364 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4365 /* WRITE ERROR */
4366 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4367 /* UNEXPECTED_UNSOLICITED_DATA */
4368 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4369 break;
4370 case TCM_SERVICE_CRC_ERROR:
4371 /* CURRENT ERROR */
4372 buffer[offset] = 0x70;
4373 /* ABORTED COMMAND */
4374 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4375 /* PROTOCOL SERVICE CRC ERROR */
4376 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4377 /* N/A */
4378 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4379 break;
4380 case TCM_SNACK_REJECTED:
4381 /* CURRENT ERROR */
4382 buffer[offset] = 0x70;
4383 /* ABORTED COMMAND */
4384 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4385 /* READ ERROR */
4386 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4387 /* FAILED RETRANSMISSION REQUEST */
4388 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4389 break;
4390 case TCM_WRITE_PROTECTED:
4391 /* CURRENT ERROR */
4392 buffer[offset] = 0x70;
4393 /* DATA PROTECT */
4394 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4395 /* WRITE PROTECTED */
4396 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4397 break;
4398 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4399 /* CURRENT ERROR */
4400 buffer[offset] = 0x70;
4401 /* UNIT ATTENTION */
4402 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4403 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4404 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4405 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4406 break;
4407 case TCM_CHECK_CONDITION_NOT_READY:
4408 /* CURRENT ERROR */
4409 buffer[offset] = 0x70;
4410 /* Not Ready */
4411 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4412 transport_get_sense_codes(cmd, &asc, &ascq);
4413 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4414 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4415 break;
4416 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4417 default:
4418 /* CURRENT ERROR */
4419 buffer[offset] = 0x70;
4420 /* ILLEGAL REQUEST */
4421 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4422 /* LOGICAL UNIT COMMUNICATION FAILURE */
4423 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4424 break;
4425 }
4426 /*
4427 * This code uses linux/include/scsi/scsi.h SAM status codes!
4428 */
4429 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4430 /*
4431 * Automatically padded, this value is encoded in the fabric's
4432 * data_length response PDU containing the SCSI defined sense data.
4433 */
4434 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4435
4436after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004437 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004438}
4439EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4440
4441int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4442{
4443 int ret = 0;
4444
Andy Grovera1d8b492011-05-02 17:12:10 -07004445 if (atomic_read(&cmd->t_transport_aborted) != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07004446 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004447 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4448 return 1;
4449#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004450 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004451 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07004452 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004453 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004454#endif
4455 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00004456 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004457 ret = 1;
4458 }
4459 return ret;
4460}
4461EXPORT_SYMBOL(transport_check_aborted_status);
4462
4463void transport_send_task_abort(struct se_cmd *cmd)
4464{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07004465 unsigned long flags;
4466
4467 spin_lock_irqsave(&cmd->t_state_lock, flags);
4468 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4469 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4470 return;
4471 }
4472 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4473
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004474 /*
4475 * If there are still expected incoming fabric WRITEs, we wait
4476 * until until they have completed before sending a TASK_ABORTED
4477 * response. This response with TASK_ABORTED status will be
4478 * queued back to fabric module by transport_check_aborted_status().
4479 */
4480 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004481 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004482 atomic_inc(&cmd->t_transport_aborted);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004483 smp_mb__after_atomic_inc();
4484 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4485 transport_new_cmd_failure(cmd);
4486 return;
4487 }
4488 }
4489 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4490#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004491 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07004492 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004493 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004494#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00004495 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004496}
4497
4498/* transport_generic_do_tmr():
4499 *
4500 *
4501 */
4502int transport_generic_do_tmr(struct se_cmd *cmd)
4503{
Andy Grover5951146d2011-07-19 10:26:37 +00004504 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004505 struct se_tmr_req *tmr = cmd->se_tmr_req;
4506 int ret;
4507
4508 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004509 case TMR_ABORT_TASK:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004510 tmr->response = TMR_FUNCTION_REJECTED;
4511 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004512 case TMR_ABORT_TASK_SET:
4513 case TMR_CLEAR_ACA:
4514 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004515 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4516 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004517 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004518 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4519 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4520 TMR_FUNCTION_REJECTED;
4521 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004522 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004523 tmr->response = TMR_FUNCTION_REJECTED;
4524 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004525 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004526 tmr->response = TMR_FUNCTION_REJECTED;
4527 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004528 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004529 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004530 tmr->function);
4531 tmr->response = TMR_FUNCTION_REJECTED;
4532 break;
4533 }
4534
4535 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00004536 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004537
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04004538 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004539 return 0;
4540}
4541
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004542/* transport_processing_thread():
4543 *
4544 *
4545 */
4546static int transport_processing_thread(void *param)
4547{
Andy Grover5951146d2011-07-19 10:26:37 +00004548 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004549 struct se_cmd *cmd;
4550 struct se_device *dev = (struct se_device *) param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004551
4552 set_user_nice(current, -20);
4553
4554 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004555 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4556 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004557 kthread_should_stop());
4558 if (ret < 0)
4559 goto out;
4560
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004561get_cmd:
4562 __transport_execute_tasks(dev);
4563
Andy Grover5951146d2011-07-19 10:26:37 +00004564 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4565 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004566 continue;
4567
Andy Grover5951146d2011-07-19 10:26:37 +00004568 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02004569 case TRANSPORT_NEW_CMD:
4570 BUG();
4571 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004572 case TRANSPORT_NEW_CMD_MAP:
Andy Grover6708bb22011-06-08 10:36:43 -07004573 if (!cmd->se_tfo->new_cmd_map) {
4574 pr_err("cmd->se_tfo->new_cmd_map is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004575 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4576 BUG();
4577 }
Andy Grovere3d6f902011-07-19 08:55:10 +00004578 ret = cmd->se_tfo->new_cmd_map(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004579 if (ret < 0) {
4580 cmd->transport_error_status = ret;
Christoph Hellwig4499dda2011-10-17 13:56:49 -04004581 transport_generic_request_failure(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004582 0, (cmd->data_direction !=
4583 DMA_TO_DEVICE));
4584 break;
4585 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004586 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004587 if (ret < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004588 cmd->transport_error_status = ret;
Christoph Hellwig4499dda2011-10-17 13:56:49 -04004589 transport_generic_request_failure(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004590 0, (cmd->data_direction !=
4591 DMA_TO_DEVICE));
4592 }
4593 break;
4594 case TRANSPORT_PROCESS_WRITE:
4595 transport_generic_process_write(cmd);
4596 break;
Nicholas Bellingerf4366772011-05-19 20:19:11 -07004597 case TRANSPORT_FREE_CMD_INTR:
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02004598 transport_generic_free_cmd(cmd, 0);
Nicholas Bellingerf4366772011-05-19 20:19:11 -07004599 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004600 case TRANSPORT_PROCESS_TMR:
4601 transport_generic_do_tmr(cmd);
4602 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004603 case TRANSPORT_COMPLETE_QF_WP:
Christoph Hellwige057f532011-10-17 13:56:41 -04004604 transport_write_pending_qf(cmd);
4605 break;
4606 case TRANSPORT_COMPLETE_QF_OK:
4607 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004608 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004609 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004610 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4611 "i_state: %d on SE LUN: %u\n",
4612 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00004613 cmd->se_tfo->get_task_tag(cmd),
4614 cmd->se_tfo->get_cmd_state(cmd),
4615 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004616 BUG();
4617 }
4618
4619 goto get_cmd;
4620 }
4621
4622out:
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07004623 WARN_ON(!list_empty(&dev->state_task_list));
4624 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004625 dev->process_thread = NULL;
4626 return 0;
4627}