blob: b79c6a2824ee027459d261e31cb2b03e06bf10df [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_transport.c
3 *
4 * This file contains the Generic Target Engine Core.
5 *
6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8 * Copyright (c) 2007-2010 Rising Tide Systems
9 * Copyright (c) 2008-2010 Linux-iSCSI.org
10 *
11 * Nicholas A. Bellinger <nab@kernel.org>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 *
27 ******************************************************************************/
28
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080029#include <linux/net.h>
30#include <linux/delay.h>
31#include <linux/string.h>
32#include <linux/timer.h>
33#include <linux/slab.h>
34#include <linux/blkdev.h>
35#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036#include <linux/kthread.h>
37#include <linux/in.h>
38#include <linux/cdrom.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040039#include <linux/module.h>
Roland Dreier015487b2012-02-13 16:18:17 -080040#include <linux/ratelimit.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041#include <asm/unaligned.h>
42#include <net/sock.h>
43#include <net/tcp.h>
44#include <scsi/scsi.h>
45#include <scsi/scsi_cmnd.h>
Nicholas Bellingere66ecd52011-05-19 20:19:14 -070046#include <scsi/scsi_tcq.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047
48#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050049#include <target/target_core_backend.h>
50#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080051#include <target/target_core_configfs.h>
52
Christoph Hellwige26d99a2011-11-14 12:30:30 -050053#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080054#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080055#include "target_core_pr.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080056#include "target_core_ua.h"
57
Andy Grovere3d6f902011-07-19 08:55:10 +000058static int sub_api_initialized;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080059
Christoph Hellwig35e0e752011-10-17 13:56:53 -040060static struct workqueue_struct *target_completion_wq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061static struct kmem_cache *se_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062struct kmem_cache *se_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063struct kmem_cache *t10_pr_reg_cache;
64struct kmem_cache *t10_alua_lu_gp_cache;
65struct kmem_cache *t10_alua_lu_gp_mem_cache;
66struct kmem_cache *t10_alua_tg_pt_gp_cache;
67struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
68
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080069static int transport_generic_write_pending(struct se_cmd *);
Andy Grover5951146d2011-07-19 10:26:37 +000070static int transport_processing_thread(void *param);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -080071static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080072static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070073static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040074 struct se_device *dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080075static void transport_free_dev_tasks(struct se_cmd *cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +000076static int transport_generic_get_mem(struct se_cmd *cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -070077static void transport_put_cmd(struct se_cmd *cmd);
Christoph Hellwig3df8d402011-10-17 13:56:43 -040078static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080079static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
Christoph Hellwig35e0e752011-10-17 13:56:53 -040080static void target_complete_ok_work(struct work_struct *work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080081
Andy Grovere3d6f902011-07-19 08:55:10 +000082int init_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080084 se_sess_cache = kmem_cache_create("se_sess_cache",
85 sizeof(struct se_session), __alignof__(struct se_session),
86 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070087 if (!se_sess_cache) {
88 pr_err("kmem_cache_create() for struct se_session"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080089 " failed\n");
Andy Groverc8e31f22012-01-19 13:39:17 -080090 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080091 }
92 se_ua_cache = kmem_cache_create("se_ua_cache",
93 sizeof(struct se_ua), __alignof__(struct se_ua),
94 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070095 if (!se_ua_cache) {
96 pr_err("kmem_cache_create() for struct se_ua failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -040097 goto out_free_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080098 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080099 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
100 sizeof(struct t10_pr_registration),
101 __alignof__(struct t10_pr_registration), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700102 if (!t10_pr_reg_cache) {
103 pr_err("kmem_cache_create() for struct t10_pr_registration"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800104 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400105 goto out_free_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800106 }
107 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
108 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
109 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700110 if (!t10_alua_lu_gp_cache) {
111 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800112 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400113 goto out_free_pr_reg_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800114 }
115 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
116 sizeof(struct t10_alua_lu_gp_member),
117 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700118 if (!t10_alua_lu_gp_mem_cache) {
119 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800120 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400121 goto out_free_lu_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800122 }
123 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
124 sizeof(struct t10_alua_tg_pt_gp),
125 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700126 if (!t10_alua_tg_pt_gp_cache) {
127 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800128 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400129 goto out_free_lu_gp_mem_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800130 }
131 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
132 "t10_alua_tg_pt_gp_mem_cache",
133 sizeof(struct t10_alua_tg_pt_gp_member),
134 __alignof__(struct t10_alua_tg_pt_gp_member),
135 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700136 if (!t10_alua_tg_pt_gp_mem_cache) {
137 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800138 "mem_t failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400139 goto out_free_tg_pt_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800140 }
141
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400142 target_completion_wq = alloc_workqueue("target_completion",
143 WQ_MEM_RECLAIM, 0);
144 if (!target_completion_wq)
145 goto out_free_tg_pt_gp_mem_cache;
146
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800147 return 0;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400148
149out_free_tg_pt_gp_mem_cache:
150 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
151out_free_tg_pt_gp_cache:
152 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
153out_free_lu_gp_mem_cache:
154 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
155out_free_lu_gp_cache:
156 kmem_cache_destroy(t10_alua_lu_gp_cache);
157out_free_pr_reg_cache:
158 kmem_cache_destroy(t10_pr_reg_cache);
159out_free_ua_cache:
160 kmem_cache_destroy(se_ua_cache);
161out_free_sess_cache:
162 kmem_cache_destroy(se_sess_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800163out:
Andy Grovere3d6f902011-07-19 08:55:10 +0000164 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800165}
166
Andy Grovere3d6f902011-07-19 08:55:10 +0000167void release_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800168{
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400169 destroy_workqueue(target_completion_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800170 kmem_cache_destroy(se_sess_cache);
171 kmem_cache_destroy(se_ua_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800172 kmem_cache_destroy(t10_pr_reg_cache);
173 kmem_cache_destroy(t10_alua_lu_gp_cache);
174 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
175 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
176 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800177}
178
Andy Grovere3d6f902011-07-19 08:55:10 +0000179/* This code ensures unique mib indexes are handed out. */
180static DEFINE_SPINLOCK(scsi_mib_index_lock);
181static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800182
183/*
184 * Allocate a new row index for the entry type specified
185 */
186u32 scsi_get_new_index(scsi_index_t type)
187{
188 u32 new_index;
189
Andy Grovere3d6f902011-07-19 08:55:10 +0000190 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800191
Andy Grovere3d6f902011-07-19 08:55:10 +0000192 spin_lock(&scsi_mib_index_lock);
193 new_index = ++scsi_mib_index[type];
194 spin_unlock(&scsi_mib_index_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800195
196 return new_index;
197}
198
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500199static void transport_init_queue_obj(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800200{
201 atomic_set(&qobj->queue_cnt, 0);
202 INIT_LIST_HEAD(&qobj->qobj_list);
203 init_waitqueue_head(&qobj->thread_wq);
204 spin_lock_init(&qobj->cmd_queue_lock);
205}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800206
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700207void transport_subsystem_check_init(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800208{
209 int ret;
210
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700211 if (sub_api_initialized)
212 return;
213
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214 ret = request_module("target_core_iblock");
215 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700216 pr_err("Unable to load target_core_iblock\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800217
218 ret = request_module("target_core_file");
219 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700220 pr_err("Unable to load target_core_file\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800221
222 ret = request_module("target_core_pscsi");
223 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700224 pr_err("Unable to load target_core_pscsi\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800225
226 ret = request_module("target_core_stgt");
227 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700228 pr_err("Unable to load target_core_stgt\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229
Andy Grovere3d6f902011-07-19 08:55:10 +0000230 sub_api_initialized = 1;
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700231 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800232}
233
234struct se_session *transport_init_session(void)
235{
236 struct se_session *se_sess;
237
238 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700239 if (!se_sess) {
240 pr_err("Unable to allocate struct se_session from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800241 " se_sess_cache\n");
242 return ERR_PTR(-ENOMEM);
243 }
244 INIT_LIST_HEAD(&se_sess->sess_list);
245 INIT_LIST_HEAD(&se_sess->sess_acl_list);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700246 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
247 INIT_LIST_HEAD(&se_sess->sess_wait_list);
248 spin_lock_init(&se_sess->sess_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800249
250 return se_sess;
251}
252EXPORT_SYMBOL(transport_init_session);
253
254/*
255 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
256 */
257void __transport_register_session(
258 struct se_portal_group *se_tpg,
259 struct se_node_acl *se_nacl,
260 struct se_session *se_sess,
261 void *fabric_sess_ptr)
262{
263 unsigned char buf[PR_REG_ISID_LEN];
264
265 se_sess->se_tpg = se_tpg;
266 se_sess->fabric_sess_ptr = fabric_sess_ptr;
267 /*
268 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
269 *
270 * Only set for struct se_session's that will actually be moving I/O.
271 * eg: *NOT* discovery sessions.
272 */
273 if (se_nacl) {
274 /*
275 * If the fabric module supports an ISID based TransportID,
276 * save this value in binary from the fabric I_T Nexus now.
277 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000278 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800279 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000280 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800281 &buf[0], PR_REG_ISID_LEN);
282 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
283 }
284 spin_lock_irq(&se_nacl->nacl_sess_lock);
285 /*
286 * The se_nacl->nacl_sess pointer will be set to the
287 * last active I_T Nexus for each struct se_node_acl.
288 */
289 se_nacl->nacl_sess = se_sess;
290
291 list_add_tail(&se_sess->sess_acl_list,
292 &se_nacl->acl_sess_list);
293 spin_unlock_irq(&se_nacl->nacl_sess_lock);
294 }
295 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
296
Andy Grover6708bb22011-06-08 10:36:43 -0700297 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000298 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800299}
300EXPORT_SYMBOL(__transport_register_session);
301
302void transport_register_session(
303 struct se_portal_group *se_tpg,
304 struct se_node_acl *se_nacl,
305 struct se_session *se_sess,
306 void *fabric_sess_ptr)
307{
308 spin_lock_bh(&se_tpg->session_lock);
309 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
310 spin_unlock_bh(&se_tpg->session_lock);
311}
312EXPORT_SYMBOL(transport_register_session);
313
314void transport_deregister_session_configfs(struct se_session *se_sess)
315{
316 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700317 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800318 /*
319 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
320 */
321 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700322 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700323 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800324 list_del(&se_sess->sess_acl_list);
325 /*
326 * If the session list is empty, then clear the pointer.
327 * Otherwise, set the struct se_session pointer from the tail
328 * element of the per struct se_node_acl active session list.
329 */
330 if (list_empty(&se_nacl->acl_sess_list))
331 se_nacl->nacl_sess = NULL;
332 else {
333 se_nacl->nacl_sess = container_of(
334 se_nacl->acl_sess_list.prev,
335 struct se_session, sess_acl_list);
336 }
Roland Dreier23388862011-06-22 01:02:21 -0700337 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800338 }
339}
340EXPORT_SYMBOL(transport_deregister_session_configfs);
341
342void transport_free_session(struct se_session *se_sess)
343{
344 kmem_cache_free(se_sess_cache, se_sess);
345}
346EXPORT_SYMBOL(transport_free_session);
347
348void transport_deregister_session(struct se_session *se_sess)
349{
350 struct se_portal_group *se_tpg = se_sess->se_tpg;
351 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700352 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800353
Andy Grover6708bb22011-06-08 10:36:43 -0700354 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800355 transport_free_session(se_sess);
356 return;
357 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358
Roland Dreiere63a8e12011-08-12 16:01:02 -0700359 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800360 list_del(&se_sess->sess_list);
361 se_sess->se_tpg = NULL;
362 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700363 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800364
365 /*
366 * Determine if we need to do extra work for this initiator node's
367 * struct se_node_acl if it had been previously dynamically generated.
368 */
369 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700370 if (se_nacl) {
Roland Dreiere63a8e12011-08-12 16:01:02 -0700371 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800372 if (se_nacl->dynamic_node_acl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700373 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
374 se_tpg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800375 list_del(&se_nacl->acl_list);
376 se_tpg->num_node_acls--;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700377 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800378
379 core_tpg_wait_for_nacl_pr_ref(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380 core_free_device_list_for_node(se_nacl, se_tpg);
Andy Grovere3d6f902011-07-19 08:55:10 +0000381 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800382 se_nacl);
Roland Dreiere63a8e12011-08-12 16:01:02 -0700383 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800384 }
385 }
Roland Dreiere63a8e12011-08-12 16:01:02 -0700386 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800387 }
388
389 transport_free_session(se_sess);
390
Andy Grover6708bb22011-06-08 10:36:43 -0700391 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000392 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800393}
394EXPORT_SYMBOL(transport_deregister_session);
395
396/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700397 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398 */
399static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
400{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400401 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 struct se_task *task;
403 unsigned long flags;
404
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400405 if (!dev)
406 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800407
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400408 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400409 if (task->task_flags & TF_ACTIVE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800410 continue;
411
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500413 if (task->t_state_active) {
414 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
415 cmd->se_tfo->get_task_tag(cmd), dev, task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800416
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500417 list_del(&task->t_state_list);
418 atomic_dec(&cmd->t_task_cdbs_ex_left);
419 task->t_state_active = false;
420 }
421 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800422 }
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500423
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800424}
425
426/* transport_cmd_check_stop():
427 *
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500428 * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800429 * 'transport_off = 2' determines if task_dev_state should be removed.
430 *
431 * A non-zero u8 t_state sets cmd->t_state.
432 * Returns 1 when command is stopped, else 0.
433 */
434static int transport_cmd_check_stop(
435 struct se_cmd *cmd,
436 int transport_off,
437 u8 t_state)
438{
439 unsigned long flags;
440
Andy Grovera1d8b492011-05-02 17:12:10 -0700441 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800442 /*
443 * Determine if IOCTL context caller in requesting the stopping of this
444 * command for LUN shutdown purposes.
445 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500446 if (cmd->transport_state & CMD_T_LUN_STOP) {
447 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
448 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800449
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500450 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451 if (transport_off == 2)
452 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700453 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454
Andy Grovera1d8b492011-05-02 17:12:10 -0700455 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800456 return 1;
457 }
458 /*
459 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000460 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800461 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500462 if (cmd->transport_state & CMD_T_STOP) {
463 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
464 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000465 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800466
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800467 if (transport_off == 2)
468 transport_all_task_dev_remove_state(cmd);
469
470 /*
471 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
472 * to FE.
473 */
474 if (transport_off == 2)
475 cmd->se_lun = NULL;
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->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479 return 1;
480 }
481 if (transport_off) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500482 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800483 if (transport_off == 2) {
484 transport_all_task_dev_remove_state(cmd);
485 /*
486 * Clear struct se_cmd->se_lun before the transport_off == 2
487 * handoff to fabric module.
488 */
489 cmd->se_lun = NULL;
490 /*
491 * Some fabric modules like tcm_loop can release
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300492 * their internally allocated I/O reference now and
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800493 * struct se_cmd now.
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700494 *
495 * Fabric modules are expected to return '1' here if the
496 * se_cmd being passed is released at this point,
497 * or zero if not being released.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800498 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000499 if (cmd->se_tfo->check_stop_free != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 spin_unlock_irqrestore(
Andy Grovera1d8b492011-05-02 17:12:10 -0700501 &cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800502
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700503 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800504 }
505 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700506 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800507
508 return 0;
509 } else if (t_state)
510 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700511 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800512
513 return 0;
514}
515
516static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
517{
518 return transport_cmd_check_stop(cmd, 2, 0);
519}
520
521static void transport_lun_remove_cmd(struct se_cmd *cmd)
522{
Andy Grovere3d6f902011-07-19 08:55:10 +0000523 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800524 unsigned long flags;
525
526 if (!lun)
527 return;
528
Andy Grovera1d8b492011-05-02 17:12:10 -0700529 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500530 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
531 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
532 transport_all_task_dev_remove_state(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800533 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700534 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800536 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500537 if (!list_empty(&cmd->se_lun_node))
538 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800539 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
540}
541
542void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
543{
Andy Groverc8e31f22012-01-19 13:39:17 -0800544 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700545 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800546
547 if (transport_cmd_check_stop_to_fabric(cmd))
548 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700549 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400550 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200551 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700552 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553}
554
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400555static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
556 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557{
558 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000559 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800560 unsigned long flags;
561
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800562 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700563 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800564 cmd->t_state = t_state;
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500565 cmd->transport_state |= CMD_T_ACTIVE;
Andy Grovera1d8b492011-05-02 17:12:10 -0700566 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800567 }
568
569 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700570
571 /* If the cmd is already on the list, remove it before we add it */
572 if (!list_empty(&cmd->se_queue_node))
573 list_del(&cmd->se_queue_node);
574 else
575 atomic_inc(&qobj->queue_cnt);
576
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400577 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700578 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400579 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700580 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500581 cmd->transport_state |= CMD_T_QUEUED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800582 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
583
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800585}
586
Andy Grover5951146d2011-07-19 10:26:37 +0000587static struct se_cmd *
588transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800589{
Andy Grover5951146d2011-07-19 10:26:37 +0000590 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800591 unsigned long flags;
592
593 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
594 if (list_empty(&qobj->qobj_list)) {
595 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
596 return NULL;
597 }
Andy Grover5951146d2011-07-19 10:26:37 +0000598 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800599
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500600 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700601 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800602 atomic_dec(&qobj->queue_cnt);
603 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
604
Andy Grover5951146d2011-07-19 10:26:37 +0000605 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800606}
607
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400608static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400610 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800611 unsigned long flags;
612
613 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500614 if (!(cmd->transport_state & CMD_T_QUEUED)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800615 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
616 return;
617 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500618 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700619 atomic_dec(&qobj->queue_cnt);
620 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800621 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800622}
623
624/*
625 * Completion function used by TCM subsystem plugins (such as FILEIO)
626 * for queueing up response from struct se_subsystem_api->do_task()
627 */
628void transport_complete_sync_cache(struct se_cmd *cmd, int good)
629{
Andy Grovera1d8b492011-05-02 17:12:10 -0700630 struct se_task *task = list_entry(cmd->t_task_list.next,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631 struct se_task, t_list);
632
633 if (good) {
634 cmd->scsi_status = SAM_STAT_GOOD;
635 task->task_scsi_status = GOOD;
636 } else {
637 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700638 task->task_se_cmd->scsi_sense_reason =
639 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
640
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800641 }
642
643 transport_complete_task(task, good);
644}
645EXPORT_SYMBOL(transport_complete_sync_cache);
646
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400647static void target_complete_failure_work(struct work_struct *work)
648{
649 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
650
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700651 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400652}
653
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800654/* transport_complete_task():
655 *
656 * Called from interrupt and non interrupt context depending
657 * on the transport plugin.
658 */
659void transport_complete_task(struct se_task *task, int success)
660{
Andy Grovere3d6f902011-07-19 08:55:10 +0000661 struct se_cmd *cmd = task->task_se_cmd;
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400662 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800663 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800664
Andy Grovera1d8b492011-05-02 17:12:10 -0700665 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400666 task->task_flags &= ~TF_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800667
668 /*
669 * See if any sense data exists, if so set the TASK_SENSE flag.
670 * Also check for any other post completion work that needs to be
671 * done by the plugins.
672 */
673 if (dev && dev->transport->transport_complete) {
674 if (dev->transport->transport_complete(task) != 0) {
675 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
Christoph Hellwigef804a82011-11-23 06:53:58 -0500676 task->task_flags |= TF_HAS_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800677 success = 1;
678 }
679 }
680
681 /*
682 * See if we are waiting for outstanding struct se_task
683 * to complete for an exception condition
684 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400685 if (task->task_flags & TF_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700686 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800687 complete(&task->task_stop_comp);
688 return;
689 }
Christoph Hellwig22350072011-11-02 05:06:35 -0700690
691 if (!success)
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500692 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig22350072011-11-02 05:06:35 -0700693
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800694 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800695 * Decrement the outstanding t_task_cdbs_left count. The last
696 * struct se_task from struct se_cmd will complete itself into the
697 * device queue depending upon int success.
698 */
Andy Grover6708bb22011-06-08 10:36:43 -0700699 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700700 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800701 return;
702 }
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800703 /*
704 * Check for case where an explict ABORT_TASK has been received
705 * and transport_wait_for_tasks() will be waiting for completion..
706 */
707 if (cmd->transport_state & CMD_T_ABORTED &&
708 cmd->transport_state & CMD_T_STOP) {
709 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
710 complete(&cmd->t_transport_stop_comp);
711 return;
712 } else if (cmd->transport_state & CMD_T_FAILED) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500713 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400714 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800715 } else {
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400716 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800717 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400718
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400719 cmd->t_state = TRANSPORT_COMPLETE;
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800720 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
Andy Grovera1d8b492011-05-02 17:12:10 -0700721 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800722
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400723 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800724}
725EXPORT_SYMBOL(transport_complete_task);
726
727/*
728 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
729 * struct se_task list are ready to be added to the active execution list
730 * struct se_device
731
732 * Called with se_dev_t->execute_task_lock called.
733 */
734static inline int transport_add_task_check_sam_attr(
735 struct se_task *task,
736 struct se_task *task_prev,
737 struct se_device *dev)
738{
739 /*
740 * No SAM Task attribute emulation enabled, add to tail of
741 * execution queue
742 */
743 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
744 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
745 return 0;
746 }
747 /*
748 * HEAD_OF_QUEUE attribute for received CDB, which means
749 * the first task that is associated with a struct se_cmd goes to
750 * head of the struct se_device->execute_task_list, and task_prev
751 * after that for each subsequent task
752 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -0700753 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800754 list_add(&task->t_execute_list,
755 (task_prev != NULL) ?
756 &task_prev->t_execute_list :
757 &dev->execute_task_list);
758
Andy Grover6708bb22011-06-08 10:36:43 -0700759 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800760 " in execution queue\n",
Andy Grover6708bb22011-06-08 10:36:43 -0700761 task->task_se_cmd->t_task_cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800762 return 1;
763 }
764 /*
765 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
766 * transitioned from Dermant -> Active state, and are added to the end
767 * of the struct se_device->execute_task_list
768 */
769 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
770 return 0;
771}
772
773/* __transport_add_task_to_execute_queue():
774 *
775 * Called with se_dev_t->execute_task_lock called.
776 */
777static void __transport_add_task_to_execute_queue(
778 struct se_task *task,
779 struct se_task *task_prev,
780 struct se_device *dev)
781{
782 int head_of_queue;
783
784 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
785 atomic_inc(&dev->execute_tasks);
786
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500787 if (task->t_state_active)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800788 return;
789 /*
790 * Determine if this task needs to go to HEAD_OF_QUEUE for the
791 * state list as well. Running with SAM Task Attribute emulation
792 * will always return head_of_queue == 0 here
793 */
794 if (head_of_queue)
795 list_add(&task->t_state_list, (task_prev) ?
796 &task_prev->t_state_list :
797 &dev->state_task_list);
798 else
799 list_add_tail(&task->t_state_list, &dev->state_task_list);
800
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500801 task->t_state_active = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800802
Andy Grover6708bb22011-06-08 10:36:43 -0700803 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000804 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800805 task, dev);
806}
807
808static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
809{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400810 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800811 struct se_task *task;
812 unsigned long flags;
813
Andy Grovera1d8b492011-05-02 17:12:10 -0700814 spin_lock_irqsave(&cmd->t_state_lock, flags);
815 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800816 spin_lock(&dev->execute_task_lock);
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500817 if (!task->t_state_active) {
818 list_add_tail(&task->t_state_list,
819 &dev->state_task_list);
820 task->t_state_active = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800821
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500822 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
823 task->task_se_cmd->se_tfo->get_task_tag(
824 task->task_se_cmd), task, dev);
825 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800826 spin_unlock(&dev->execute_task_lock);
827 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700828 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800829}
830
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800831static void __transport_add_tasks_from_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800832{
Andy Grover5951146d2011-07-19 10:26:37 +0000833 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800834 struct se_task *task, *task_prev = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800835
Andy Grovera1d8b492011-05-02 17:12:10 -0700836 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -0400837 if (!list_empty(&task->t_execute_list))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800838 continue;
839 /*
840 * __transport_add_task_to_execute_queue() handles the
841 * SAM Task Attribute emulation if enabled
842 */
843 __transport_add_task_to_execute_queue(task, task_prev, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800844 task_prev = task;
845 }
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800846}
847
848static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
849{
850 unsigned long flags;
851 struct se_device *dev = cmd->se_dev;
852
853 spin_lock_irqsave(&dev->execute_task_lock, flags);
854 __transport_add_tasks_from_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800855 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800856}
857
Christoph Hellwig04629b72011-10-12 11:07:04 -0400858void __transport_remove_task_from_execute_queue(struct se_task *task,
859 struct se_device *dev)
860{
861 list_del_init(&task->t_execute_list);
862 atomic_dec(&dev->execute_tasks);
863}
864
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500865static void transport_remove_task_from_execute_queue(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800866 struct se_task *task,
867 struct se_device *dev)
868{
869 unsigned long flags;
870
Christoph Hellwig04629b72011-10-12 11:07:04 -0400871 if (WARN_ON(list_empty(&task->t_execute_list)))
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700872 return;
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700873
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800874 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig04629b72011-10-12 11:07:04 -0400875 __transport_remove_task_from_execute_queue(task, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800876 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
877}
878
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700879/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700880 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700881 */
882
883static void target_qf_do_work(struct work_struct *work)
884{
885 struct se_device *dev = container_of(work, struct se_device,
886 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700887 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700888 struct se_cmd *cmd, *cmd_tmp;
889
890 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700891 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
892 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700893
Roland Dreierbcac3642011-08-27 21:33:16 -0700894 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700895 list_del(&cmd->se_qf_node);
896 atomic_dec(&dev->dev_qf_count);
897 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700898
Andy Grover6708bb22011-06-08 10:36:43 -0700899 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700900 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400901 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700902 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
903 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400904
905 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700906 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700907}
908
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800909unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
910{
911 switch (cmd->data_direction) {
912 case DMA_NONE:
913 return "NONE";
914 case DMA_FROM_DEVICE:
915 return "READ";
916 case DMA_TO_DEVICE:
917 return "WRITE";
918 case DMA_BIDIRECTIONAL:
919 return "BIDI";
920 default:
921 break;
922 }
923
924 return "UNKNOWN";
925}
926
927void transport_dump_dev_state(
928 struct se_device *dev,
929 char *b,
930 int *bl)
931{
932 *bl += sprintf(b + *bl, "Status: ");
933 switch (dev->dev_status) {
934 case TRANSPORT_DEVICE_ACTIVATED:
935 *bl += sprintf(b + *bl, "ACTIVATED");
936 break;
937 case TRANSPORT_DEVICE_DEACTIVATED:
938 *bl += sprintf(b + *bl, "DEACTIVATED");
939 break;
940 case TRANSPORT_DEVICE_SHUTDOWN:
941 *bl += sprintf(b + *bl, "SHUTDOWN");
942 break;
943 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
944 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
945 *bl += sprintf(b + *bl, "OFFLINE");
946 break;
947 default:
948 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
949 break;
950 }
951
Nicholas Bellinger65586d52011-11-30 01:25:21 -0800952 *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
953 atomic_read(&dev->execute_tasks), dev->queue_depth);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800954 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000955 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 -0800956 *bl += sprintf(b + *bl, " ");
957}
958
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800959void transport_dump_vpd_proto_id(
960 struct t10_vpd *vpd,
961 unsigned char *p_buf,
962 int p_buf_len)
963{
964 unsigned char buf[VPD_TMP_BUF_SIZE];
965 int len;
966
967 memset(buf, 0, VPD_TMP_BUF_SIZE);
968 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
969
970 switch (vpd->protocol_identifier) {
971 case 0x00:
972 sprintf(buf+len, "Fibre Channel\n");
973 break;
974 case 0x10:
975 sprintf(buf+len, "Parallel SCSI\n");
976 break;
977 case 0x20:
978 sprintf(buf+len, "SSA\n");
979 break;
980 case 0x30:
981 sprintf(buf+len, "IEEE 1394\n");
982 break;
983 case 0x40:
984 sprintf(buf+len, "SCSI Remote Direct Memory Access"
985 " Protocol\n");
986 break;
987 case 0x50:
988 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
989 break;
990 case 0x60:
991 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
992 break;
993 case 0x70:
994 sprintf(buf+len, "Automation/Drive Interface Transport"
995 " Protocol\n");
996 break;
997 case 0x80:
998 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
999 break;
1000 default:
1001 sprintf(buf+len, "Unknown 0x%02x\n",
1002 vpd->protocol_identifier);
1003 break;
1004 }
1005
1006 if (p_buf)
1007 strncpy(p_buf, buf, p_buf_len);
1008 else
Andy Grover6708bb22011-06-08 10:36:43 -07001009 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001010}
1011
1012void
1013transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1014{
1015 /*
1016 * Check if the Protocol Identifier Valid (PIV) bit is set..
1017 *
1018 * from spc3r23.pdf section 7.5.1
1019 */
1020 if (page_83[1] & 0x80) {
1021 vpd->protocol_identifier = (page_83[0] & 0xf0);
1022 vpd->protocol_identifier_set = 1;
1023 transport_dump_vpd_proto_id(vpd, NULL, 0);
1024 }
1025}
1026EXPORT_SYMBOL(transport_set_vpd_proto_id);
1027
1028int transport_dump_vpd_assoc(
1029 struct t10_vpd *vpd,
1030 unsigned char *p_buf,
1031 int p_buf_len)
1032{
1033 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001034 int ret = 0;
1035 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001036
1037 memset(buf, 0, VPD_TMP_BUF_SIZE);
1038 len = sprintf(buf, "T10 VPD Identifier Association: ");
1039
1040 switch (vpd->association) {
1041 case 0x00:
1042 sprintf(buf+len, "addressed logical unit\n");
1043 break;
1044 case 0x10:
1045 sprintf(buf+len, "target port\n");
1046 break;
1047 case 0x20:
1048 sprintf(buf+len, "SCSI target device\n");
1049 break;
1050 default:
1051 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +00001052 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001053 break;
1054 }
1055
1056 if (p_buf)
1057 strncpy(p_buf, buf, p_buf_len);
1058 else
Andy Grover6708bb22011-06-08 10:36:43 -07001059 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001060
1061 return ret;
1062}
1063
1064int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1065{
1066 /*
1067 * The VPD identification association..
1068 *
1069 * from spc3r23.pdf Section 7.6.3.1 Table 297
1070 */
1071 vpd->association = (page_83[1] & 0x30);
1072 return transport_dump_vpd_assoc(vpd, NULL, 0);
1073}
1074EXPORT_SYMBOL(transport_set_vpd_assoc);
1075
1076int transport_dump_vpd_ident_type(
1077 struct t10_vpd *vpd,
1078 unsigned char *p_buf,
1079 int p_buf_len)
1080{
1081 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001082 int ret = 0;
1083 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001084
1085 memset(buf, 0, VPD_TMP_BUF_SIZE);
1086 len = sprintf(buf, "T10 VPD Identifier Type: ");
1087
1088 switch (vpd->device_identifier_type) {
1089 case 0x00:
1090 sprintf(buf+len, "Vendor specific\n");
1091 break;
1092 case 0x01:
1093 sprintf(buf+len, "T10 Vendor ID based\n");
1094 break;
1095 case 0x02:
1096 sprintf(buf+len, "EUI-64 based\n");
1097 break;
1098 case 0x03:
1099 sprintf(buf+len, "NAA\n");
1100 break;
1101 case 0x04:
1102 sprintf(buf+len, "Relative target port identifier\n");
1103 break;
1104 case 0x08:
1105 sprintf(buf+len, "SCSI name string\n");
1106 break;
1107 default:
1108 sprintf(buf+len, "Unsupported: 0x%02x\n",
1109 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +00001110 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001111 break;
1112 }
1113
Andy Grovere3d6f902011-07-19 08:55:10 +00001114 if (p_buf) {
1115 if (p_buf_len < strlen(buf)+1)
1116 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001117 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +00001118 } else {
Andy Grover6708bb22011-06-08 10:36:43 -07001119 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +00001120 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001121
1122 return ret;
1123}
1124
1125int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1126{
1127 /*
1128 * The VPD identifier type..
1129 *
1130 * from spc3r23.pdf Section 7.6.3.1 Table 298
1131 */
1132 vpd->device_identifier_type = (page_83[1] & 0x0f);
1133 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1134}
1135EXPORT_SYMBOL(transport_set_vpd_ident_type);
1136
1137int transport_dump_vpd_ident(
1138 struct t10_vpd *vpd,
1139 unsigned char *p_buf,
1140 int p_buf_len)
1141{
1142 unsigned char buf[VPD_TMP_BUF_SIZE];
1143 int ret = 0;
1144
1145 memset(buf, 0, VPD_TMP_BUF_SIZE);
1146
1147 switch (vpd->device_identifier_code_set) {
1148 case 0x01: /* Binary */
1149 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1150 &vpd->device_identifier[0]);
1151 break;
1152 case 0x02: /* ASCII */
1153 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1154 &vpd->device_identifier[0]);
1155 break;
1156 case 0x03: /* UTF-8 */
1157 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1158 &vpd->device_identifier[0]);
1159 break;
1160 default:
1161 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1162 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +00001163 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001164 break;
1165 }
1166
1167 if (p_buf)
1168 strncpy(p_buf, buf, p_buf_len);
1169 else
Andy Grover6708bb22011-06-08 10:36:43 -07001170 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001171
1172 return ret;
1173}
1174
1175int
1176transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1177{
1178 static const char hex_str[] = "0123456789abcdef";
1179 int j = 0, i = 4; /* offset to start of the identifer */
1180
1181 /*
1182 * The VPD Code Set (encoding)
1183 *
1184 * from spc3r23.pdf Section 7.6.3.1 Table 296
1185 */
1186 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1187 switch (vpd->device_identifier_code_set) {
1188 case 0x01: /* Binary */
1189 vpd->device_identifier[j++] =
1190 hex_str[vpd->device_identifier_type];
1191 while (i < (4 + page_83[3])) {
1192 vpd->device_identifier[j++] =
1193 hex_str[(page_83[i] & 0xf0) >> 4];
1194 vpd->device_identifier[j++] =
1195 hex_str[page_83[i] & 0x0f];
1196 i++;
1197 }
1198 break;
1199 case 0x02: /* ASCII */
1200 case 0x03: /* UTF-8 */
1201 while (i < (4 + page_83[3]))
1202 vpd->device_identifier[j++] = page_83[i++];
1203 break;
1204 default:
1205 break;
1206 }
1207
1208 return transport_dump_vpd_ident(vpd, NULL, 0);
1209}
1210EXPORT_SYMBOL(transport_set_vpd_ident);
1211
1212static void core_setup_task_attr_emulation(struct se_device *dev)
1213{
1214 /*
1215 * If this device is from Target_Core_Mod/pSCSI, disable the
1216 * SAM Task Attribute emulation.
1217 *
1218 * This is currently not available in upsream Linux/SCSI Target
1219 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1220 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001221 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001222 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1223 return;
1224 }
1225
1226 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001227 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001228 " device\n", dev->transport->name,
1229 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001230}
1231
1232static void scsi_dump_inquiry(struct se_device *dev)
1233{
Andy Grovere3d6f902011-07-19 08:55:10 +00001234 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001235 char buf[17];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001236 int i, device_type;
1237 /*
1238 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1239 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001240 for (i = 0; i < 8; i++)
1241 if (wwn->vendor[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001242 buf[i] = wwn->vendor[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001243 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001244 buf[i] = ' ';
1245 buf[i] = '\0';
1246 pr_debug(" Vendor: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001247
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001248 for (i = 0; i < 16; i++)
1249 if (wwn->model[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001250 buf[i] = wwn->model[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001251 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001252 buf[i] = ' ';
1253 buf[i] = '\0';
1254 pr_debug(" Model: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001255
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001256 for (i = 0; i < 4; i++)
1257 if (wwn->revision[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001258 buf[i] = wwn->revision[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001259 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001260 buf[i] = ' ';
1261 buf[i] = '\0';
1262 pr_debug(" Revision: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001263
Andy Grovere3d6f902011-07-19 08:55:10 +00001264 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001265 pr_debug(" Type: %s ", scsi_device_type(device_type));
1266 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001267 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268}
1269
1270struct se_device *transport_add_device_to_core_hba(
1271 struct se_hba *hba,
1272 struct se_subsystem_api *transport,
1273 struct se_subsystem_dev *se_dev,
1274 u32 device_flags,
1275 void *transport_dev,
1276 struct se_dev_limits *dev_limits,
1277 const char *inquiry_prod,
1278 const char *inquiry_rev)
1279{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001280 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001281 struct se_device *dev;
1282
1283 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001284 if (!dev) {
1285 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286 return NULL;
1287 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001288
Andy Grovere3d6f902011-07-19 08:55:10 +00001289 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001290 dev->dev_flags = device_flags;
1291 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001292 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001293 dev->se_hba = hba;
1294 dev->se_sub_dev = se_dev;
1295 dev->transport = transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001296 INIT_LIST_HEAD(&dev->dev_list);
1297 INIT_LIST_HEAD(&dev->dev_sep_list);
1298 INIT_LIST_HEAD(&dev->dev_tmr_list);
1299 INIT_LIST_HEAD(&dev->execute_task_list);
1300 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001301 INIT_LIST_HEAD(&dev->state_task_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001302 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001303 spin_lock_init(&dev->execute_task_lock);
1304 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001305 spin_lock_init(&dev->dev_reservation_lock);
1306 spin_lock_init(&dev->dev_status_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001307 spin_lock_init(&dev->se_port_lock);
1308 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001309 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001310 atomic_set(&dev->dev_ordered_id, 0);
1311
1312 se_dev_set_default_attribs(dev, dev_limits);
1313
1314 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1315 dev->creation_time = get_jiffies_64();
1316 spin_lock_init(&dev->stats_lock);
1317
1318 spin_lock(&hba->device_lock);
1319 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1320 hba->dev_count++;
1321 spin_unlock(&hba->device_lock);
1322 /*
1323 * Setup the SAM Task Attribute emulation for struct se_device
1324 */
1325 core_setup_task_attr_emulation(dev);
1326 /*
1327 * Force PR and ALUA passthrough emulation with internal object use.
1328 */
1329 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1330 /*
1331 * Setup the Reservations infrastructure for struct se_device
1332 */
1333 core_setup_reservations(dev, force_pt);
1334 /*
1335 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1336 */
1337 if (core_setup_alua(dev, force_pt) < 0)
1338 goto out;
1339
1340 /*
1341 * Startup the struct se_device processing thread
1342 */
1343 dev->process_thread = kthread_run(transport_processing_thread, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001344 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001345 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001346 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001347 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001348 goto out;
1349 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001350 /*
1351 * Setup work_queue for QUEUE_FULL
1352 */
1353 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001354 /*
1355 * Preload the initial INQUIRY const values if we are doing
1356 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1357 * passthrough because this is being provided by the backend LLD.
1358 * This is required so that transport_get_inquiry() copies these
1359 * originals once back into DEV_T10_WWN(dev) for the virtual device
1360 * setup.
1361 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001362 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001363 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001364 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001365 " INQUIRY consts\n");
1366 goto out;
1367 }
1368
Andy Grovere3d6f902011-07-19 08:55:10 +00001369 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1370 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1371 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001372 }
1373 scsi_dump_inquiry(dev);
1374
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001375 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001376out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001377 kthread_stop(dev->process_thread);
1378
1379 spin_lock(&hba->device_lock);
1380 list_del(&dev->dev_list);
1381 hba->dev_count--;
1382 spin_unlock(&hba->device_lock);
1383
1384 se_release_vpd_for_dev(dev);
1385
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001386 kfree(dev);
1387
1388 return NULL;
1389}
1390EXPORT_SYMBOL(transport_add_device_to_core_hba);
1391
1392/* transport_generic_prepare_cdb():
1393 *
1394 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1395 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1396 * The point of this is since we are mapping iSCSI LUNs to
1397 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1398 * devices and HBAs for a loop.
1399 */
1400static inline void transport_generic_prepare_cdb(
1401 unsigned char *cdb)
1402{
1403 switch (cdb[0]) {
1404 case READ_10: /* SBC - RDProtect */
1405 case READ_12: /* SBC - RDProtect */
1406 case READ_16: /* SBC - RDProtect */
1407 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1408 case VERIFY: /* SBC - VRProtect */
1409 case VERIFY_16: /* SBC - VRProtect */
1410 case WRITE_VERIFY: /* SBC - VRProtect */
1411 case WRITE_VERIFY_12: /* SBC - VRProtect */
1412 break;
1413 default:
1414 cdb[1] &= 0x1f; /* clear logical unit number */
1415 break;
1416 }
1417}
1418
1419static struct se_task *
1420transport_generic_get_task(struct se_cmd *cmd,
1421 enum dma_data_direction data_direction)
1422{
1423 struct se_task *task;
Andy Grover5951146d2011-07-19 10:26:37 +00001424 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001425
Andy Grover6708bb22011-06-08 10:36:43 -07001426 task = dev->transport->alloc_task(cmd->t_task_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001427 if (!task) {
Andy Grover6708bb22011-06-08 10:36:43 -07001428 pr_err("Unable to allocate struct se_task\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001429 return NULL;
1430 }
1431
1432 INIT_LIST_HEAD(&task->t_list);
1433 INIT_LIST_HEAD(&task->t_execute_list);
1434 INIT_LIST_HEAD(&task->t_state_list);
1435 init_completion(&task->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001436 task->task_se_cmd = cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001437 task->task_data_direction = data_direction;
1438
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001439 return task;
1440}
1441
1442static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1443
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001444/*
1445 * Used by fabric modules containing a local struct se_cmd within their
1446 * fabric dependent per I/O descriptor.
1447 */
1448void transport_init_se_cmd(
1449 struct se_cmd *cmd,
1450 struct target_core_fabric_ops *tfo,
1451 struct se_session *se_sess,
1452 u32 data_length,
1453 int data_direction,
1454 int task_attr,
1455 unsigned char *sense_buffer)
1456{
Andy Grover5951146d2011-07-19 10:26:37 +00001457 INIT_LIST_HEAD(&cmd->se_lun_node);
1458 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001459 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001460 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001461 INIT_LIST_HEAD(&cmd->se_cmd_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001462 INIT_LIST_HEAD(&cmd->t_task_list);
1463 init_completion(&cmd->transport_lun_fe_stop_comp);
1464 init_completion(&cmd->transport_lun_stop_comp);
1465 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001466 init_completion(&cmd->cmd_wait_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001467 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001468 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001469
1470 cmd->se_tfo = tfo;
1471 cmd->se_sess = se_sess;
1472 cmd->data_length = data_length;
1473 cmd->data_direction = data_direction;
1474 cmd->sam_task_attr = task_attr;
1475 cmd->sense_buffer = sense_buffer;
1476}
1477EXPORT_SYMBOL(transport_init_se_cmd);
1478
1479static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1480{
1481 /*
1482 * Check if SAM Task Attribute emulation is enabled for this
1483 * struct se_device storage object
1484 */
Andy Grover5951146d2011-07-19 10:26:37 +00001485 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001486 return 0;
1487
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001488 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001489 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001490 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001491 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001492 }
1493 /*
1494 * Used to determine when ORDERED commands should go from
1495 * Dormant to Active status.
1496 */
Andy Grover5951146d2011-07-19 10:26:37 +00001497 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001498 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001499 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001500 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001501 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001502 return 0;
1503}
1504
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001505/* transport_generic_allocate_tasks():
1506 *
1507 * Called from fabric RX Thread.
1508 */
1509int transport_generic_allocate_tasks(
1510 struct se_cmd *cmd,
1511 unsigned char *cdb)
1512{
1513 int ret;
1514
1515 transport_generic_prepare_cdb(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001516 /*
1517 * Ensure that the received CDB is less than the max (252 + 8) bytes
1518 * for VARIABLE_LENGTH_CMD
1519 */
1520 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001521 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001522 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1523 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001524 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1525 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001526 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001527 }
1528 /*
1529 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1530 * allocate the additional extended CDB buffer now.. Otherwise
1531 * setup the pointer from __t_task_cdb to t_task_cdb.
1532 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001533 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1534 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001535 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001536 if (!cmd->t_task_cdb) {
1537 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001538 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001539 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001540 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001541 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1542 cmd->scsi_sense_reason =
1543 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001544 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001545 }
1546 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001547 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001548 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001549 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001550 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001551 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001552 /*
1553 * Setup the received CDB based on SCSI defined opcodes and
1554 * perform unit attention, persistent reservations and ALUA
Andy Grovera1d8b492011-05-02 17:12:10 -07001555 * checks for virtual device backends. The cmd->t_task_cdb
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001556 * pointer is expected to be setup before we reach this point.
1557 */
1558 ret = transport_generic_cmd_sequencer(cmd, cdb);
1559 if (ret < 0)
1560 return ret;
1561 /*
1562 * Check for SAM Task Attribute Emulation
1563 */
1564 if (transport_check_alloc_task_attr(cmd) < 0) {
1565 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1566 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001567 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001568 }
1569 spin_lock(&cmd->se_lun->lun_sep_lock);
1570 if (cmd->se_lun->lun_sep)
1571 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1572 spin_unlock(&cmd->se_lun->lun_sep_lock);
1573 return 0;
1574}
1575EXPORT_SYMBOL(transport_generic_allocate_tasks);
1576
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001577/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001578 * Used by fabric module frontends to queue tasks directly.
1579 * Many only be used from process context only
1580 */
1581int transport_handle_cdb_direct(
1582 struct se_cmd *cmd)
1583{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001584 int ret;
1585
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001586 if (!cmd->se_lun) {
1587 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001588 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001589 return -EINVAL;
1590 }
1591 if (in_interrupt()) {
1592 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001593 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001594 " from interrupt context\n");
1595 return -EINVAL;
1596 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001597 /*
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001598 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001599 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1600 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001601 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001602 *
1603 * Also, we don't take cmd->t_state_lock here as we only expect
1604 * this to be called for initial descriptor submission.
1605 */
1606 cmd->t_state = TRANSPORT_NEW_CMD;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001607 cmd->transport_state |= CMD_T_ACTIVE;
1608
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001609 /*
1610 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1611 * so follow TRANSPORT_NEW_CMD processing thread context usage
1612 * and call transport_generic_request_failure() if necessary..
1613 */
1614 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001615 if (ret < 0)
1616 transport_generic_request_failure(cmd);
1617
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001618 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001619}
1620EXPORT_SYMBOL(transport_handle_cdb_direct);
1621
Nicholas Bellingera6360782011-11-18 20:36:22 -08001622/**
1623 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1624 *
1625 * @se_cmd: command descriptor to submit
1626 * @se_sess: associated se_sess for endpoint
1627 * @cdb: pointer to SCSI CDB
1628 * @sense: pointer to SCSI sense buffer
1629 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1630 * @data_length: fabric expected data transfer length
1631 * @task_addr: SAM task attribute
1632 * @data_dir: DMA data direction
1633 * @flags: flags for command submission from target_sc_flags_tables
1634 *
1635 * This may only be called from process context, and also currently
1636 * assumes internal allocation of fabric payload buffer by target-core.
1637 **/
Andy Grover1edcdb42012-01-19 13:39:23 -08001638void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001639 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1640 u32 data_length, int task_attr, int data_dir, int flags)
1641{
1642 struct se_portal_group *se_tpg;
1643 int rc;
1644
1645 se_tpg = se_sess->se_tpg;
1646 BUG_ON(!se_tpg);
1647 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1648 BUG_ON(in_interrupt());
1649 /*
1650 * Initialize se_cmd for target operation. From this point
1651 * exceptions are handled by sending exception status via
1652 * target_core_fabric_ops->queue_status() callback
1653 */
1654 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1655 data_length, data_dir, task_attr, sense);
1656 /*
1657 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1658 * se_sess->sess_cmd_list. A second kref_get here is necessary
1659 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1660 * kref_put() to happen during fabric packet acknowledgement.
1661 */
1662 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1663 /*
1664 * Signal bidirectional data payloads to target-core
1665 */
1666 if (flags & TARGET_SCF_BIDI_OP)
1667 se_cmd->se_cmd_flags |= SCF_BIDI;
1668 /*
1669 * Locate se_lun pointer and attach it to struct se_cmd
1670 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001671 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1672 transport_send_check_condition_and_sense(se_cmd,
1673 se_cmd->scsi_sense_reason, 0);
1674 target_put_sess_cmd(se_sess, se_cmd);
1675 return;
1676 }
Nicholas Bellingera6360782011-11-18 20:36:22 -08001677 /*
1678 * Sanitize CDBs via transport_generic_cmd_sequencer() and
1679 * allocate the necessary tasks to complete the received CDB+data
1680 */
1681 rc = transport_generic_allocate_tasks(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001682 if (rc != 0) {
1683 transport_generic_request_failure(se_cmd);
1684 return;
1685 }
Nicholas Bellingera6360782011-11-18 20:36:22 -08001686 /*
1687 * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
1688 * for immediate execution of READs, otherwise wait for
1689 * transport_generic_handle_data() to be called for WRITEs
1690 * when fabric has filled the incoming buffer.
1691 */
1692 transport_handle_cdb_direct(se_cmd);
Andy Grover1edcdb42012-01-19 13:39:23 -08001693 return;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001694}
1695EXPORT_SYMBOL(target_submit_cmd);
1696
Andy Groverea98d7f2012-01-19 13:39:21 -08001697/**
1698 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1699 * for TMR CDBs
1700 *
1701 * @se_cmd: command descriptor to submit
1702 * @se_sess: associated se_sess for endpoint
1703 * @sense: pointer to SCSI sense buffer
1704 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1705 * @fabric_context: fabric context for TMR req
1706 * @tm_type: Type of TM request
1707 *
1708 * Callable from all contexts.
1709 **/
1710
1711void target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
1712 unsigned char *sense, u32 unpacked_lun,
1713 void *fabric_tmr_ptr, unsigned char tm_type, int flags)
1714{
1715 struct se_portal_group *se_tpg;
1716 int ret;
1717
1718 se_tpg = se_sess->se_tpg;
1719 BUG_ON(!se_tpg);
1720
1721 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1722 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
1723
1724 /* See target_submit_cmd for commentary */
1725 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1726
1727 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, GFP_KERNEL);
1728 if (ret < 0) {
1729 dump_stack();
1730 /* FIXME XXX */
1731 return;
1732 }
1733
1734 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1735 if (ret) {
1736 transport_send_check_condition_and_sense(se_cmd,
1737 se_cmd->scsi_sense_reason, 0);
1738 transport_generic_free_cmd(se_cmd, 0);
1739 return;
1740 }
1741 transport_generic_handle_tmr(se_cmd);
1742}
1743EXPORT_SYMBOL(target_submit_tmr);
1744
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001745/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001746 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1747 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1748 * complete setup in TCM process context w/ TFO->new_cmd_map().
1749 */
1750int transport_generic_handle_cdb_map(
1751 struct se_cmd *cmd)
1752{
Andy Grovere3d6f902011-07-19 08:55:10 +00001753 if (!cmd->se_lun) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001754 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001755 pr_err("cmd->se_lun is NULL\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001756 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001757 }
1758
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001759 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001760 return 0;
1761}
1762EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1763
1764/* transport_generic_handle_data():
1765 *
1766 *
1767 */
1768int transport_generic_handle_data(
1769 struct se_cmd *cmd)
1770{
1771 /*
1772 * For the software fabric case, then we assume the nexus is being
1773 * failed/shutdown when signals are pending from the kthread context
1774 * caller, so we return a failure. For the HW target mode case running
1775 * in interrupt code, the signal_pending() check is skipped.
1776 */
1777 if (!in_interrupt() && signal_pending(current))
Andy Grovere3d6f902011-07-19 08:55:10 +00001778 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001779 /*
1780 * If the received CDB has aleady been ABORTED by the generic
1781 * target engine, we now call transport_check_aborted_status()
1782 * to queue any delated TASK_ABORTED status for the received CDB to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001783 * fabric module as we are expecting no further incoming DATA OUT
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001784 * sequences at this point.
1785 */
1786 if (transport_check_aborted_status(cmd, 1) != 0)
1787 return 0;
1788
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001789 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001790 return 0;
1791}
1792EXPORT_SYMBOL(transport_generic_handle_data);
1793
1794/* transport_generic_handle_tmr():
1795 *
1796 *
1797 */
1798int transport_generic_handle_tmr(
1799 struct se_cmd *cmd)
1800{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001801 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001802 return 0;
1803}
1804EXPORT_SYMBOL(transport_generic_handle_tmr);
1805
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001806/*
1807 * If the task is active, request it to be stopped and sleep until it
1808 * has completed.
1809 */
1810bool target_stop_task(struct se_task *task, unsigned long *flags)
1811{
1812 struct se_cmd *cmd = task->task_se_cmd;
1813 bool was_active = false;
1814
1815 if (task->task_flags & TF_ACTIVE) {
1816 task->task_flags |= TF_REQUEST_STOP;
1817 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1818
1819 pr_debug("Task %p waiting to complete\n", task);
1820 wait_for_completion(&task->task_stop_comp);
1821 pr_debug("Task %p stopped successfully\n", task);
1822
1823 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1824 atomic_dec(&cmd->t_task_cdbs_left);
1825 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1826 was_active = true;
1827 }
1828
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001829 return was_active;
1830}
1831
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001832static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1833{
1834 struct se_task *task, *task_tmp;
1835 unsigned long flags;
1836 int ret = 0;
1837
Andy Grover6708bb22011-06-08 10:36:43 -07001838 pr_debug("ITT[0x%08x] - Stopping tasks\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001839 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001840
1841 /*
1842 * No tasks remain in the execution queue
1843 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001844 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001845 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07001846 &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001847 pr_debug("Processing task %p\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001848 /*
1849 * If the struct se_task has not been sent and is not active,
1850 * remove the struct se_task from the execution queue.
1851 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04001852 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
Andy Grovera1d8b492011-05-02 17:12:10 -07001853 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001854 flags);
1855 transport_remove_task_from_execute_queue(task,
Christoph Hellwig42bf8292011-10-12 11:07:00 -04001856 cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001857
Christoph Hellwig04629b72011-10-12 11:07:04 -04001858 pr_debug("Task %p removed from execute queue\n", task);
Andy Grovera1d8b492011-05-02 17:12:10 -07001859 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001860 continue;
1861 }
1862
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001863 if (!target_stop_task(task, &flags)) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001864 pr_debug("Task %p - did nothing\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001865 ret++;
1866 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001867 }
Andy Grovera1d8b492011-05-02 17:12:10 -07001868 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001869
1870 return ret;
1871}
1872
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001873/*
1874 * Handle SAM-esque emulation for generic transport request failures.
1875 */
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001876void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001877{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001878 int ret = 0;
1879
Andy Grover6708bb22011-06-08 10:36:43 -07001880 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001881 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001882 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001883 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001884 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001885 cmd->t_state, cmd->scsi_sense_reason);
Andy Grover6708bb22011-06-08 10:36:43 -07001886 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001887 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001888 " CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1889 cmd->t_task_list_num,
Andy Grovera1d8b492011-05-02 17:12:10 -07001890 atomic_read(&cmd->t_task_cdbs_left),
1891 atomic_read(&cmd->t_task_cdbs_sent),
1892 atomic_read(&cmd->t_task_cdbs_ex_left),
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001893 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1894 (cmd->transport_state & CMD_T_STOP) != 0,
1895 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001896
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001897 /*
1898 * For SAM Task Attribute emulation for failed struct se_cmd
1899 */
1900 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1901 transport_complete_task_attr(cmd);
1902
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001903 switch (cmd->scsi_sense_reason) {
1904 case TCM_NON_EXISTENT_LUN:
1905 case TCM_UNSUPPORTED_SCSI_OPCODE:
1906 case TCM_INVALID_CDB_FIELD:
1907 case TCM_INVALID_PARAMETER_LIST:
1908 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1909 case TCM_UNKNOWN_MODE_PAGE:
1910 case TCM_WRITE_PROTECTED:
1911 case TCM_CHECK_CONDITION_ABORT_CMD:
1912 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1913 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001914 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001915 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001916 /*
1917 * No SENSE Data payload for this case, set SCSI Status
1918 * and queue the response to $FABRIC_MOD.
1919 *
1920 * Uses linux/include/scsi/scsi.h SAM status codes defs
1921 */
1922 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1923 /*
1924 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1925 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1926 * CONFLICT STATUS.
1927 *
1928 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1929 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001930 if (cmd->se_sess &&
1931 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1932 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001933 cmd->orig_fe_lun, 0x2C,
1934 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1935
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001936 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001937 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001938 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001939 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001940 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001941 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001942 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001943 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1944 break;
1945 }
Nicholas Bellinger16ab8e62011-08-08 19:03:38 -07001946 /*
1947 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1948 * make the call to transport_send_check_condition_and_sense()
1949 * directly. Otherwise expect the fabric to make the call to
1950 * transport_send_check_condition_and_sense() after handling
1951 * possible unsoliticied write data payloads.
1952 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001953 ret = transport_send_check_condition_and_sense(cmd,
1954 cmd->scsi_sense_reason, 0);
1955 if (ret == -EAGAIN || ret == -ENOMEM)
1956 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001957
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001958check_stop:
1959 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001960 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001961 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001962 return;
1963
1964queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001965 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1966 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001967}
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001968EXPORT_SYMBOL(transport_generic_request_failure);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001969
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001970static inline u32 transport_lba_21(unsigned char *cdb)
1971{
1972 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1973}
1974
1975static inline u32 transport_lba_32(unsigned char *cdb)
1976{
1977 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1978}
1979
1980static inline unsigned long long transport_lba_64(unsigned char *cdb)
1981{
1982 unsigned int __v1, __v2;
1983
1984 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1985 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1986
1987 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1988}
1989
1990/*
1991 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1992 */
1993static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1994{
1995 unsigned int __v1, __v2;
1996
1997 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1998 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1999
2000 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2001}
2002
2003static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2004{
2005 unsigned long flags;
2006
Andy Grovera1d8b492011-05-02 17:12:10 -07002007 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002008 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
Andy Grovera1d8b492011-05-02 17:12:10 -07002009 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002010}
2011
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002012/*
2013 * Called from Fabric Module context from transport_execute_tasks()
2014 *
2015 * The return of this function determins if the tasks from struct se_cmd
2016 * get added to the execution queue in transport_execute_tasks(),
2017 * or are added to the delayed or ordered lists here.
2018 */
2019static inline int transport_execute_task_attr(struct se_cmd *cmd)
2020{
Andy Grover5951146d2011-07-19 10:26:37 +00002021 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002022 return 1;
2023 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002024 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002025 * to allow the passed struct se_cmd list of tasks to the front of the list.
2026 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002027 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07002028 pr_debug("Added HEAD_OF_QUEUE for CDB:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002029 " 0x%02x, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07002030 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002031 cmd->se_ordered_id);
2032 return 1;
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002033 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Andy Grover5951146d2011-07-19 10:26:37 +00002034 atomic_inc(&cmd->se_dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002035 smp_mb__after_atomic_inc();
2036
Andy Grover6708bb22011-06-08 10:36:43 -07002037 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002038 " list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002039 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002040 cmd->se_ordered_id);
2041 /*
2042 * Add ORDERED command to tail of execution queue if
2043 * no other older commands exist that need to be
2044 * completed first.
2045 */
Andy Grover6708bb22011-06-08 10:36:43 -07002046 if (!atomic_read(&cmd->se_dev->simple_cmds))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002047 return 1;
2048 } else {
2049 /*
2050 * For SIMPLE and UNTAGGED Task Attribute commands
2051 */
Andy Grover5951146d2011-07-19 10:26:37 +00002052 atomic_inc(&cmd->se_dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002053 smp_mb__after_atomic_inc();
2054 }
2055 /*
2056 * Otherwise if one or more outstanding ORDERED task attribute exist,
2057 * add the dormant task(s) built for the passed struct se_cmd to the
2058 * execution queue and become in Active state for this struct se_device.
2059 */
Andy Grover5951146d2011-07-19 10:26:37 +00002060 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002061 /*
2062 * Otherwise, add cmd w/ tasks to delayed cmd queue that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002063 * will be drained upon completion of HEAD_OF_QUEUE task.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002064 */
Andy Grover5951146d2011-07-19 10:26:37 +00002065 spin_lock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002066 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
Andy Grover5951146d2011-07-19 10:26:37 +00002067 list_add_tail(&cmd->se_delayed_node,
2068 &cmd->se_dev->delayed_cmd_list);
2069 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002070
Andy Grover6708bb22011-06-08 10:36:43 -07002071 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002072 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002073 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002074 cmd->se_ordered_id);
2075 /*
2076 * Return zero to let transport_execute_tasks() know
2077 * not to add the delayed tasks to the execution list.
2078 */
2079 return 0;
2080 }
2081 /*
2082 * Otherwise, no ORDERED task attributes exist..
2083 */
2084 return 1;
2085}
2086
2087/*
2088 * Called from fabric module context in transport_generic_new_cmd() and
2089 * transport_generic_process_write()
2090 */
2091static int transport_execute_tasks(struct se_cmd *cmd)
2092{
2093 int add_tasks;
Nicholas Bellinger40be67f2011-11-30 00:41:20 -08002094 struct se_device *se_dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002095 /*
2096 * Call transport_cmd_check_stop() to see if a fabric exception
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002097 * has occurred that prevents execution.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002098 */
Andy Grover6708bb22011-06-08 10:36:43 -07002099 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002100 /*
2101 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2102 * attribute for the tasks of the received struct se_cmd CDB
2103 */
2104 add_tasks = transport_execute_task_attr(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00002105 if (!add_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002106 goto execute_tasks;
2107 /*
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002108 * __transport_execute_tasks() -> __transport_add_tasks_from_cmd()
2109 * adds associated se_tasks while holding dev->execute_task_lock
2110 * before I/O dispath to avoid a double spinlock access.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002111 */
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002112 __transport_execute_tasks(se_dev, cmd);
2113 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002114 }
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002115
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002116execute_tasks:
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002117 __transport_execute_tasks(se_dev, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002118 return 0;
2119}
2120
2121/*
2122 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2123 * from struct se_device->execute_task_list and
2124 *
2125 * Called from transport_processing_thread()
2126 */
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002127static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002128{
2129 int error;
2130 struct se_cmd *cmd = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002131 struct se_task *task = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002132 unsigned long flags;
2133
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002134check_depth:
Andy Grovere3d6f902011-07-19 08:55:10 +00002135 spin_lock_irq(&dev->execute_task_lock);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002136 if (new_cmd != NULL)
2137 __transport_add_tasks_from_cmd(new_cmd);
2138
Andy Grovere3d6f902011-07-19 08:55:10 +00002139 if (list_empty(&dev->execute_task_list)) {
2140 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002141 return 0;
2142 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002143 task = list_first_entry(&dev->execute_task_list,
2144 struct se_task, t_execute_list);
Christoph Hellwig04629b72011-10-12 11:07:04 -04002145 __transport_remove_task_from_execute_queue(task, dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002146 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002147
Andy Grovere3d6f902011-07-19 08:55:10 +00002148 cmd = task->task_se_cmd;
Andy Grovera1d8b492011-05-02 17:12:10 -07002149 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04002150 task->task_flags |= (TF_ACTIVE | TF_SENT);
Andy Grovera1d8b492011-05-02 17:12:10 -07002151 atomic_inc(&cmd->t_task_cdbs_sent);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002152
Andy Grovera1d8b492011-05-02 17:12:10 -07002153 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2154 cmd->t_task_list_num)
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002155 cmd->transport_state |= CMD_T_SENT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002156
Andy Grovera1d8b492011-05-02 17:12:10 -07002157 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002158
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002159 if (cmd->execute_task)
2160 error = cmd->execute_task(task);
2161 else
2162 error = dev->transport->do_task(task);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002163 if (error != 0) {
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002164 spin_lock_irqsave(&cmd->t_state_lock, flags);
2165 task->task_flags &= ~TF_ACTIVE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002166 cmd->transport_state &= ~CMD_T_SENT;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002167 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002168
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002169 transport_stop_tasks_for_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002170 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002171 }
2172
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002173 new_cmd = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002174 goto check_depth;
2175
2176 return 0;
2177}
2178
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002179static inline u32 transport_get_sectors_6(
2180 unsigned char *cdb,
2181 struct se_cmd *cmd,
2182 int *ret)
2183{
Andy Grover5951146d2011-07-19 10:26:37 +00002184 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002185
2186 /*
2187 * Assume TYPE_DISK for non struct se_device objects.
2188 * Use 8-bit sector value.
2189 */
2190 if (!dev)
2191 goto type_disk;
2192
2193 /*
2194 * Use 24-bit allocation length for TYPE_TAPE.
2195 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002196 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002197 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2198
2199 /*
2200 * Everything else assume TYPE_DISK Sector CDB location.
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002201 * Use 8-bit sector value. SBC-3 says:
2202 *
2203 * A TRANSFER LENGTH field set to zero specifies that 256
2204 * logical blocks shall be written. Any other value
2205 * specifies the number of logical blocks that shall be
2206 * written.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002207 */
2208type_disk:
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002209 return cdb[4] ? : 256;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002210}
2211
2212static inline u32 transport_get_sectors_10(
2213 unsigned char *cdb,
2214 struct se_cmd *cmd,
2215 int *ret)
2216{
Andy Grover5951146d2011-07-19 10:26:37 +00002217 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002218
2219 /*
2220 * Assume TYPE_DISK for non struct se_device objects.
2221 * Use 16-bit sector value.
2222 */
2223 if (!dev)
2224 goto type_disk;
2225
2226 /*
2227 * XXX_10 is not defined in SSC, throw an exception
2228 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002229 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2230 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002231 return 0;
2232 }
2233
2234 /*
2235 * Everything else assume TYPE_DISK Sector CDB location.
2236 * Use 16-bit sector value.
2237 */
2238type_disk:
2239 return (u32)(cdb[7] << 8) + cdb[8];
2240}
2241
2242static inline u32 transport_get_sectors_12(
2243 unsigned char *cdb,
2244 struct se_cmd *cmd,
2245 int *ret)
2246{
Andy Grover5951146d2011-07-19 10:26:37 +00002247 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002248
2249 /*
2250 * Assume TYPE_DISK for non struct se_device objects.
2251 * Use 32-bit sector value.
2252 */
2253 if (!dev)
2254 goto type_disk;
2255
2256 /*
2257 * XXX_12 is not defined in SSC, throw an exception
2258 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002259 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2260 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002261 return 0;
2262 }
2263
2264 /*
2265 * Everything else assume TYPE_DISK Sector CDB location.
2266 * Use 32-bit sector value.
2267 */
2268type_disk:
2269 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2270}
2271
2272static inline u32 transport_get_sectors_16(
2273 unsigned char *cdb,
2274 struct se_cmd *cmd,
2275 int *ret)
2276{
Andy Grover5951146d2011-07-19 10:26:37 +00002277 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002278
2279 /*
2280 * Assume TYPE_DISK for non struct se_device objects.
2281 * Use 32-bit sector value.
2282 */
2283 if (!dev)
2284 goto type_disk;
2285
2286 /*
2287 * Use 24-bit allocation length for TYPE_TAPE.
2288 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002289 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002290 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2291
2292type_disk:
2293 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2294 (cdb[12] << 8) + cdb[13];
2295}
2296
2297/*
2298 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2299 */
2300static inline u32 transport_get_sectors_32(
2301 unsigned char *cdb,
2302 struct se_cmd *cmd,
2303 int *ret)
2304{
2305 /*
2306 * Assume TYPE_DISK for non struct se_device objects.
2307 * Use 32-bit sector value.
2308 */
2309 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2310 (cdb[30] << 8) + cdb[31];
2311
2312}
2313
2314static inline u32 transport_get_size(
2315 u32 sectors,
2316 unsigned char *cdb,
2317 struct se_cmd *cmd)
2318{
Andy Grover5951146d2011-07-19 10:26:37 +00002319 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002320
Andy Grovere3d6f902011-07-19 08:55:10 +00002321 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002322 if (cdb[1] & 1) { /* sectors */
Andy Grovere3d6f902011-07-19 08:55:10 +00002323 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002324 } else /* bytes */
2325 return sectors;
2326 }
2327#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002328 pr_debug("Returning block_size: %u, sectors: %u == %u for"
Andy Grovere3d6f902011-07-19 08:55:10 +00002329 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2330 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2331 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002332#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00002333 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002334}
2335
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002336static void transport_xor_callback(struct se_cmd *cmd)
2337{
2338 unsigned char *buf, *addr;
Andy Groverec98f782011-07-20 19:28:46 +00002339 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002340 unsigned int offset;
2341 int i;
Andy Groverec98f782011-07-20 19:28:46 +00002342 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002343 /*
2344 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2345 *
2346 * 1) read the specified logical block(s);
2347 * 2) transfer logical blocks from the data-out buffer;
2348 * 3) XOR the logical blocks transferred from the data-out buffer with
2349 * the logical blocks read, storing the resulting XOR data in a buffer;
2350 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2351 * blocks transferred from the data-out buffer; and
2352 * 5) transfer the resulting XOR data to the data-in buffer.
2353 */
2354 buf = kmalloc(cmd->data_length, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002355 if (!buf) {
2356 pr_err("Unable to allocate xor_callback buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002357 return;
2358 }
2359 /*
Andy Groverec98f782011-07-20 19:28:46 +00002360 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002361 * into the locally allocated *buf
2362 */
Andy Groverec98f782011-07-20 19:28:46 +00002363 sg_copy_to_buffer(cmd->t_data_sg,
2364 cmd->t_data_nents,
2365 buf,
2366 cmd->data_length);
2367
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002368 /*
2369 * Now perform the XOR against the BIDI read memory located at
Andy Grovera1d8b492011-05-02 17:12:10 -07002370 * cmd->t_mem_bidi_list
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002371 */
2372
2373 offset = 0;
Andy Groverec98f782011-07-20 19:28:46 +00002374 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2375 addr = kmap_atomic(sg_page(sg), KM_USER0);
2376 if (!addr)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002377 goto out;
2378
Andy Groverec98f782011-07-20 19:28:46 +00002379 for (i = 0; i < sg->length; i++)
2380 *(addr + sg->offset + i) ^= *(buf + offset + i);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002381
Andy Groverec98f782011-07-20 19:28:46 +00002382 offset += sg->length;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002383 kunmap_atomic(addr, KM_USER0);
2384 }
Andy Groverec98f782011-07-20 19:28:46 +00002385
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002386out:
2387 kfree(buf);
2388}
2389
2390/*
2391 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2392 */
2393static int transport_get_sense_data(struct se_cmd *cmd)
2394{
2395 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002396 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002397 struct se_task *task = NULL, *task_tmp;
2398 unsigned long flags;
2399 u32 offset = 0;
2400
Andy Grovere3d6f902011-07-19 08:55:10 +00002401 WARN_ON(!cmd->se_lun);
2402
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002403 if (!dev)
2404 return 0;
2405
Andy Grovera1d8b492011-05-02 17:12:10 -07002406 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002407 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002408 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002409 return 0;
2410 }
2411
2412 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07002413 &cmd->t_task_list, t_list) {
Christoph Hellwigef804a82011-11-23 06:53:58 -05002414 if (!(task->task_flags & TF_HAS_SENSE))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002415 continue;
2416
Andy Grovere3d6f902011-07-19 08:55:10 +00002417 if (!dev->transport->get_sense_buffer) {
Andy Grover6708bb22011-06-08 10:36:43 -07002418 pr_err("dev->transport->get_sense_buffer"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002419 " is NULL\n");
2420 continue;
2421 }
2422
Andy Grovere3d6f902011-07-19 08:55:10 +00002423 sense_buffer = dev->transport->get_sense_buffer(task);
Andy Grover6708bb22011-06-08 10:36:43 -07002424 if (!sense_buffer) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04002425 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002426 " sense buffer for task with sense\n",
Christoph Hellwig04629b72011-10-12 11:07:04 -04002427 cmd->se_tfo->get_task_tag(cmd), task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002428 continue;
2429 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002430 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002431
Andy Grovere3d6f902011-07-19 08:55:10 +00002432 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002433 TRANSPORT_SENSE_BUFFER);
2434
Andy Grover5951146d2011-07-19 10:26:37 +00002435 memcpy(&buffer[offset], sense_buffer,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002436 TRANSPORT_SENSE_BUFFER);
2437 cmd->scsi_status = task->task_scsi_status;
2438 /* Automatically padded */
2439 cmd->scsi_sense_length =
2440 (TRANSPORT_SENSE_BUFFER + offset);
2441
Andy Grover6708bb22011-06-08 10:36:43 -07002442 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002443 " and sense\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002444 dev->se_hba->hba_id, dev->transport->name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002445 cmd->scsi_status);
2446 return 0;
2447 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002448 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002449
2450 return -1;
2451}
2452
Andy Groverec98f782011-07-20 19:28:46 +00002453static inline long long transport_dev_end_lba(struct se_device *dev)
2454{
2455 return dev->transport->get_blocks(dev) + 1;
2456}
2457
2458static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2459{
2460 struct se_device *dev = cmd->se_dev;
2461 u32 sectors;
2462
2463 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2464 return 0;
2465
2466 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2467
Andy Grover6708bb22011-06-08 10:36:43 -07002468 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2469 pr_err("LBA: %llu Sectors: %u exceeds"
Andy Groverec98f782011-07-20 19:28:46 +00002470 " transport_dev_end_lba(): %llu\n",
2471 cmd->t_task_lba, sectors,
2472 transport_dev_end_lba(dev));
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002473 return -EINVAL;
Andy Groverec98f782011-07-20 19:28:46 +00002474 }
2475
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002476 return 0;
Andy Groverec98f782011-07-20 19:28:46 +00002477}
2478
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002479static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2480{
2481 /*
2482 * Determine if the received WRITE_SAME is used to for direct
2483 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2484 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2485 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2486 */
2487 int passthrough = (dev->transport->transport_type ==
2488 TRANSPORT_PLUGIN_PHBA_PDEV);
2489
2490 if (!passthrough) {
2491 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2492 pr_err("WRITE_SAME PBDATA and LBDATA"
2493 " bits not supported for Block Discard"
2494 " Emulation\n");
2495 return -ENOSYS;
2496 }
2497 /*
2498 * Currently for the emulated case we only accept
2499 * tpws with the UNMAP=1 bit set.
2500 */
2501 if (!(flags[0] & 0x08)) {
2502 pr_err("WRITE_SAME w/o UNMAP bit not"
2503 " supported for Block Discard Emulation\n");
2504 return -ENOSYS;
2505 }
2506 }
2507
2508 return 0;
2509}
2510
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002511/* transport_generic_cmd_sequencer():
2512 *
2513 * Generic Command Sequencer that should work for most DAS transport
2514 * drivers.
2515 *
2516 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2517 * RX Thread.
2518 *
2519 * FIXME: Need to support other SCSI OPCODES where as well.
2520 */
2521static int transport_generic_cmd_sequencer(
2522 struct se_cmd *cmd,
2523 unsigned char *cdb)
2524{
Andy Grover5951146d2011-07-19 10:26:37 +00002525 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002526 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2527 int ret = 0, sector_ret = 0, passthrough;
2528 u32 sectors = 0, size = 0, pr_reg_type = 0;
2529 u16 service_action;
2530 u8 alua_ascq = 0;
2531 /*
2532 * Check for an existing UNIT ATTENTION condition
2533 */
2534 if (core_scsi3_ua_check(cmd, cdb) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002535 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2536 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
Andy Grover5951146d2011-07-19 10:26:37 +00002537 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538 }
2539 /*
2540 * Check status of Asymmetric Logical Unit Assignment port
2541 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002542 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002543 if (ret != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002544 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002545 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002546 * The ALUA additional sense code qualifier (ASCQ) is determined
2547 * by the ALUA primary or secondary access state..
2548 */
2549 if (ret > 0) {
2550#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002551 pr_debug("[%s]: ALUA TG Port not available,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002552 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002553 cmd->se_tfo->get_fabric_name(), alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002554#endif
2555 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2556 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2557 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
Andy Grover5951146d2011-07-19 10:26:37 +00002558 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002559 }
2560 goto out_invalid_cdb_field;
2561 }
2562 /*
2563 * Check status for SPC-3 Persistent Reservations
2564 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002565 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2566 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002567 cmd, cdb, pr_reg_type) != 0) {
2568 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2569 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2570 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2571 return -EBUSY;
2572 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002573 /*
2574 * This means the CDB is allowed for the SCSI Initiator port
2575 * when said port is *NOT* holding the legacy SPC-2 or
2576 * SPC-3 Persistent Reservation.
2577 */
2578 }
2579
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002580 /*
2581 * If we operate in passthrough mode we skip most CDB emulation and
2582 * instead hand the commands down to the physical SCSI device.
2583 */
2584 passthrough =
2585 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2586
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002587 switch (cdb[0]) {
2588 case READ_6:
2589 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2590 if (sector_ret)
2591 goto out_unsupported_cdb;
2592 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002593 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002594 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2595 break;
2596 case READ_10:
2597 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2598 if (sector_ret)
2599 goto out_unsupported_cdb;
2600 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002601 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002602 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2603 break;
2604 case READ_12:
2605 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2606 if (sector_ret)
2607 goto out_unsupported_cdb;
2608 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002609 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002610 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2611 break;
2612 case READ_16:
2613 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2614 if (sector_ret)
2615 goto out_unsupported_cdb;
2616 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002617 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002618 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2619 break;
2620 case WRITE_6:
2621 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2622 if (sector_ret)
2623 goto out_unsupported_cdb;
2624 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002625 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002626 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2627 break;
2628 case WRITE_10:
2629 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2630 if (sector_ret)
2631 goto out_unsupported_cdb;
2632 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002633 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002634 if (cdb[1] & 0x8)
2635 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002636 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2637 break;
2638 case WRITE_12:
2639 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2640 if (sector_ret)
2641 goto out_unsupported_cdb;
2642 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002643 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002644 if (cdb[1] & 0x8)
2645 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002646 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2647 break;
2648 case WRITE_16:
2649 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2650 if (sector_ret)
2651 goto out_unsupported_cdb;
2652 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002653 cmd->t_task_lba = transport_lba_64(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002654 if (cdb[1] & 0x8)
2655 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002656 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2657 break;
2658 case XDWRITEREAD_10:
2659 if ((cmd->data_direction != DMA_TO_DEVICE) ||
Christoph Hellwig33c3faf2011-11-14 11:36:30 -05002660 !(cmd->se_cmd_flags & SCF_BIDI))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002661 goto out_invalid_cdb_field;
2662 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2663 if (sector_ret)
2664 goto out_unsupported_cdb;
2665 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002666 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002667 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002668
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002669 /*
2670 * Do now allow BIDI commands for passthrough mode.
2671 */
2672 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002673 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002674
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002675 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002676 * Setup BIDI XOR callback to be run after I/O completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002677 */
2678 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002679 if (cdb[1] & 0x8)
2680 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002681 break;
2682 case VARIABLE_LENGTH_CMD:
2683 service_action = get_unaligned_be16(&cdb[8]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002684 switch (service_action) {
2685 case XDWRITEREAD_32:
2686 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2687 if (sector_ret)
2688 goto out_unsupported_cdb;
2689 size = transport_get_size(sectors, cdb, cmd);
2690 /*
2691 * Use WRITE_32 and READ_32 opcodes for the emulated
2692 * XDWRITE_READ_32 logic.
2693 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002694 cmd->t_task_lba = transport_lba_64_ext(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002695 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2696
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002697 /*
2698 * Do now allow BIDI commands for passthrough mode.
2699 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002700 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002701 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002702
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002703 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002704 * Setup BIDI XOR callback to be run during after I/O
2705 * completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002706 */
2707 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002708 if (cdb[1] & 0x8)
2709 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002710 break;
2711 case WRITE_SAME_32:
2712 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2713 if (sector_ret)
2714 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002715
Andy Grover6708bb22011-06-08 10:36:43 -07002716 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002717 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002718 else {
2719 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2720 " supported\n");
2721 goto out_invalid_cdb_field;
2722 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002723
Andy Grovera1d8b492011-05-02 17:12:10 -07002724 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002725 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2726
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002727 if (target_check_write_same_discard(&cdb[10], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08002728 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002729 if (!passthrough)
2730 cmd->execute_task = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002731 break;
2732 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002733 pr_err("VARIABLE_LENGTH_CMD service action"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002734 " 0x%04x not supported\n", service_action);
2735 goto out_unsupported_cdb;
2736 }
2737 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002738 case MAINTENANCE_IN:
Andy Grovere3d6f902011-07-19 08:55:10 +00002739 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002740 /* MAINTENANCE_IN from SCC-2 */
2741 /*
2742 * Check for emulated MI_REPORT_TARGET_PGS.
2743 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002744 if (cdb[1] == MI_REPORT_TARGET_PGS &&
2745 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2746 cmd->execute_task =
2747 target_emulate_report_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002748 }
2749 size = (cdb[6] << 24) | (cdb[7] << 16) |
2750 (cdb[8] << 8) | cdb[9];
2751 } else {
2752 /* GPCMD_SEND_KEY from multi media commands */
2753 size = (cdb[8] << 8) + cdb[9];
2754 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002755 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002756 break;
2757 case MODE_SELECT:
2758 size = cdb[4];
2759 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2760 break;
2761 case MODE_SELECT_10:
2762 size = (cdb[7] << 8) + cdb[8];
2763 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2764 break;
2765 case MODE_SENSE:
2766 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002767 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002768 if (!passthrough)
2769 cmd->execute_task = target_emulate_modesense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002770 break;
2771 case MODE_SENSE_10:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002772 size = (cdb[7] << 8) + cdb[8];
2773 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2774 if (!passthrough)
2775 cmd->execute_task = target_emulate_modesense;
2776 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002777 case GPCMD_READ_BUFFER_CAPACITY:
2778 case GPCMD_SEND_OPC:
2779 case LOG_SELECT:
2780 case LOG_SENSE:
2781 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002782 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002783 break;
2784 case READ_BLOCK_LIMITS:
2785 size = READ_BLOCK_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002786 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002787 break;
2788 case GPCMD_GET_CONFIGURATION:
2789 case GPCMD_READ_FORMAT_CAPACITIES:
2790 case GPCMD_READ_DISC_INFO:
2791 case GPCMD_READ_TRACK_RZONE_INFO:
2792 size = (cdb[7] << 8) + cdb[8];
2793 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2794 break;
2795 case PERSISTENT_RESERVE_IN:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002796 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002797 cmd->execute_task = target_scsi3_emulate_pr_in;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002798 size = (cdb[7] << 8) + cdb[8];
2799 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2800 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002801 case PERSISTENT_RESERVE_OUT:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002802 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002803 cmd->execute_task = target_scsi3_emulate_pr_out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002804 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002805 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002806 break;
2807 case GPCMD_MECHANISM_STATUS:
2808 case GPCMD_READ_DVD_STRUCTURE:
2809 size = (cdb[8] << 8) + cdb[9];
2810 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2811 break;
2812 case READ_POSITION:
2813 size = READ_POSITION_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002814 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002815 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002816 case MAINTENANCE_OUT:
Andy Grovere3d6f902011-07-19 08:55:10 +00002817 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002818 /* MAINTENANCE_OUT from SCC-2
2819 *
2820 * Check for emulated MO_SET_TARGET_PGS.
2821 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002822 if (cdb[1] == MO_SET_TARGET_PGS &&
2823 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2824 cmd->execute_task =
2825 target_emulate_set_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002826 }
2827
2828 size = (cdb[6] << 24) | (cdb[7] << 16) |
2829 (cdb[8] << 8) | cdb[9];
2830 } else {
2831 /* GPCMD_REPORT_KEY from multi media commands */
2832 size = (cdb[8] << 8) + cdb[9];
2833 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002834 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002835 break;
2836 case INQUIRY:
2837 size = (cdb[3] << 8) + cdb[4];
2838 /*
2839 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2840 * See spc4r17 section 5.3
2841 */
Andy Grover5951146d2011-07-19 10:26:37 +00002842 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002843 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002844 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002845 if (!passthrough)
2846 cmd->execute_task = target_emulate_inquiry;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002847 break;
2848 case READ_BUFFER:
2849 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002850 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002851 break;
2852 case READ_CAPACITY:
2853 size = READ_CAP_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002854 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002855 if (!passthrough)
2856 cmd->execute_task = target_emulate_readcapacity;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002857 break;
2858 case READ_MEDIA_SERIAL_NUMBER:
2859 case SECURITY_PROTOCOL_IN:
2860 case SECURITY_PROTOCOL_OUT:
2861 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002862 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002863 break;
2864 case SERVICE_ACTION_IN:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002865 switch (cmd->t_task_cdb[1] & 0x1f) {
2866 case SAI_READ_CAPACITY_16:
2867 if (!passthrough)
2868 cmd->execute_task =
2869 target_emulate_readcapacity_16;
2870 break;
2871 default:
2872 if (passthrough)
2873 break;
2874
2875 pr_err("Unsupported SA: 0x%02x\n",
2876 cmd->t_task_cdb[1] & 0x1f);
2877 goto out_unsupported_cdb;
2878 }
2879 /*FALLTHROUGH*/
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002880 case ACCESS_CONTROL_IN:
2881 case ACCESS_CONTROL_OUT:
2882 case EXTENDED_COPY:
2883 case READ_ATTRIBUTE:
2884 case RECEIVE_COPY_RESULTS:
2885 case WRITE_ATTRIBUTE:
2886 size = (cdb[10] << 24) | (cdb[11] << 16) |
2887 (cdb[12] << 8) | cdb[13];
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 RECEIVE_DIAGNOSTIC:
2891 case SEND_DIAGNOSTIC:
2892 size = (cdb[3] << 8) | cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002893 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002894 break;
2895/* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2896#if 0
2897 case GPCMD_READ_CD:
2898 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2899 size = (2336 * sectors);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002900 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002901 break;
2902#endif
2903 case READ_TOC:
2904 size = cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002905 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002906 break;
2907 case REQUEST_SENSE:
2908 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002909 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002910 if (!passthrough)
2911 cmd->execute_task = target_emulate_request_sense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002912 break;
2913 case READ_ELEMENT_STATUS:
2914 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002915 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002916 break;
2917 case WRITE_BUFFER:
2918 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002919 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002920 break;
2921 case RESERVE:
2922 case RESERVE_10:
2923 /*
2924 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2925 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2926 */
2927 if (cdb[0] == RESERVE_10)
2928 size = (cdb[7] << 8) | cdb[8];
2929 else
2930 size = cmd->data_length;
2931
2932 /*
2933 * Setup the legacy emulated handler for SPC-2 and
2934 * >= SPC-3 compatible reservation handling (CRH=1)
2935 * Otherwise, we assume the underlying SCSI logic is
2936 * is running in SPC_PASSTHROUGH, and wants reservations
2937 * emulation disabled.
2938 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002939 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2940 cmd->execute_task = target_scsi2_reservation_reserve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002941 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2942 break;
2943 case RELEASE:
2944 case RELEASE_10:
2945 /*
2946 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2947 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2948 */
2949 if (cdb[0] == RELEASE_10)
2950 size = (cdb[7] << 8) | cdb[8];
2951 else
2952 size = cmd->data_length;
2953
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002954 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2955 cmd->execute_task = target_scsi2_reservation_release;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002956 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2957 break;
2958 case SYNCHRONIZE_CACHE:
Andy Grover8e94b8d2012-01-16 16:57:07 -08002959 case SYNCHRONIZE_CACHE_16:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002960 /*
2961 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2962 */
2963 if (cdb[0] == SYNCHRONIZE_CACHE) {
2964 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002965 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002966 } else {
2967 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002968 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002969 }
2970 if (sector_ret)
2971 goto out_unsupported_cdb;
2972
2973 size = transport_get_size(sectors, cdb, cmd);
2974 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2975
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002976 if (passthrough)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002977 break;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002978
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002979 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002980 * Check to ensure that LBA + Range does not exceed past end of
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002981 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002982 */
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002983 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
2984 if (transport_cmd_get_valid_sectors(cmd) < 0)
2985 goto out_invalid_cdb_field;
2986 }
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002987 cmd->execute_task = target_emulate_synchronize_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002988 break;
2989 case UNMAP:
2990 size = get_unaligned_be16(&cdb[7]);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002991 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002992 if (!passthrough)
2993 cmd->execute_task = target_emulate_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002994 break;
2995 case WRITE_SAME_16:
2996 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2997 if (sector_ret)
2998 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002999
Andy Grover6708bb22011-06-08 10:36:43 -07003000 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07003001 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07003002 else {
3003 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3004 goto out_invalid_cdb_field;
3005 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07003006
Nicholas Bellinger5db07532011-07-27 22:18:52 -07003007 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003008 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003009
3010 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08003011 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003012 if (!passthrough)
3013 cmd->execute_task = target_emulate_write_same;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003014 break;
3015 case WRITE_SAME:
3016 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3017 if (sector_ret)
3018 goto out_unsupported_cdb;
3019
3020 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07003021 size = transport_get_size(1, cdb, cmd);
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003022 else {
3023 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3024 goto out_invalid_cdb_field;
3025 }
3026
3027 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
3028 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3029 /*
3030 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3031 * of byte 1 bit 3 UNMAP instead of original reserved field
3032 */
3033 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08003034 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003035 if (!passthrough)
3036 cmd->execute_task = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003037 break;
3038 case ALLOW_MEDIUM_REMOVAL:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003039 case ERASE:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003040 case REZERO_UNIT:
3041 case SEEK_10:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003042 case SPACE:
3043 case START_STOP:
3044 case TEST_UNIT_READY:
3045 case VERIFY:
3046 case WRITE_FILEMARKS:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003047 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3048 if (!passthrough)
3049 cmd->execute_task = target_emulate_noop;
3050 break;
3051 case GPCMD_CLOSE_TRACK:
3052 case INITIALIZE_ELEMENT_STATUS:
3053 case GPCMD_LOAD_UNLOAD:
3054 case GPCMD_SET_SPEED:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003055 case MOVE_MEDIUM:
3056 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3057 break;
3058 case REPORT_LUNS:
Christoph Hellwige76a35d2011-11-03 17:50:42 -04003059 cmd->execute_task = target_report_luns;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003060 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3061 /*
3062 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3063 * See spc4r17 section 5.3
3064 */
Andy Grover5951146d2011-07-19 10:26:37 +00003065 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003066 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003067 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003068 break;
3069 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003070 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003071 " 0x%02x, sending CHECK_CONDITION.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003072 cmd->se_tfo->get_fabric_name(), cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003073 goto out_unsupported_cdb;
3074 }
3075
3076 if (size != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003077 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003078 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
Andy Grovere3d6f902011-07-19 08:55:10 +00003079 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003080 cmd->data_length, size, cdb[0]);
3081
3082 cmd->cmd_spdtl = size;
3083
3084 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grover6708bb22011-06-08 10:36:43 -07003085 pr_err("Rejecting underflow/overflow"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003086 " WRITE data\n");
3087 goto out_invalid_cdb_field;
3088 }
3089 /*
3090 * Reject READ_* or WRITE_* with overflow/underflow for
3091 * type SCF_SCSI_DATA_SG_IO_CDB.
3092 */
Andy Grover6708bb22011-06-08 10:36:43 -07003093 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3094 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003095 " CDB on non 512-byte sector setup subsystem"
Andy Grovere3d6f902011-07-19 08:55:10 +00003096 " plugin: %s\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003097 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3098 goto out_invalid_cdb_field;
3099 }
3100
3101 if (size > cmd->data_length) {
3102 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3103 cmd->residual_count = (size - cmd->data_length);
3104 } else {
3105 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3106 cmd->residual_count = (cmd->data_length - size);
3107 }
3108 cmd->data_length = size;
3109 }
3110
Roland Dreier015487b2012-02-13 16:18:17 -08003111 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB &&
3112 sectors > dev->se_sub_dev->se_dev_attrib.fabric_max_sectors) {
3113 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too big sectors %u\n",
3114 cdb[0], sectors);
3115 goto out_invalid_cdb_field;
3116 }
3117
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003118 /* reject any command that we don't have a handler for */
3119 if (!(passthrough || cmd->execute_task ||
3120 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3121 goto out_unsupported_cdb;
3122
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003123 transport_set_supported_SAM_opcode(cmd);
3124 return ret;
3125
3126out_unsupported_cdb:
3127 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3128 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover5951146d2011-07-19 10:26:37 +00003129 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003130out_invalid_cdb_field:
3131 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3132 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00003133 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003134}
3135
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003136/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003137 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003138 * and ordered cmds need to have their tasks added to the execution queue.
3139 */
3140static void transport_complete_task_attr(struct se_cmd *cmd)
3141{
Andy Grover5951146d2011-07-19 10:26:37 +00003142 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003143 struct se_cmd *cmd_p, *cmd_tmp;
3144 int new_active_tasks = 0;
3145
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003146 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003147 atomic_dec(&dev->simple_cmds);
3148 smp_mb__after_atomic_dec();
3149 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003150 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003151 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3152 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003153 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003154 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003155 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003156 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3157 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003158 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003159 atomic_dec(&dev->dev_ordered_sync);
3160 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003161
3162 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003163 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003164 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3165 }
3166 /*
3167 * Process all commands up to the last received
3168 * ORDERED task attribute which requires another blocking
3169 * boundary
3170 */
3171 spin_lock(&dev->delayed_cmd_lock);
3172 list_for_each_entry_safe(cmd_p, cmd_tmp,
Andy Grover5951146d2011-07-19 10:26:37 +00003173 &dev->delayed_cmd_list, se_delayed_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003174
Andy Grover5951146d2011-07-19 10:26:37 +00003175 list_del(&cmd_p->se_delayed_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003176 spin_unlock(&dev->delayed_cmd_lock);
3177
Andy Grover6708bb22011-06-08 10:36:43 -07003178 pr_debug("Calling add_tasks() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003179 " cmd_p: 0x%02x Task Attr: 0x%02x"
3180 " Dormant -> Active, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07003181 cmd_p->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003182 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3183
3184 transport_add_tasks_from_cmd(cmd_p);
3185 new_active_tasks++;
3186
3187 spin_lock(&dev->delayed_cmd_lock);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003188 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003189 break;
3190 }
3191 spin_unlock(&dev->delayed_cmd_lock);
3192 /*
3193 * If new tasks have become active, wake up the transport thread
3194 * to do the processing of the Active tasks.
3195 */
3196 if (new_active_tasks != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003197 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198}
3199
Christoph Hellwige057f532011-10-17 13:56:41 -04003200static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003201{
3202 int ret = 0;
3203
Christoph Hellwige057f532011-10-17 13:56:41 -04003204 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3205 transport_complete_task_attr(cmd);
3206
3207 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3208 ret = cmd->se_tfo->queue_status(cmd);
3209 if (ret)
3210 goto out;
3211 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003212
3213 switch (cmd->data_direction) {
3214 case DMA_FROM_DEVICE:
3215 ret = cmd->se_tfo->queue_data_in(cmd);
3216 break;
3217 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00003218 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003219 ret = cmd->se_tfo->queue_data_in(cmd);
3220 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04003221 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003222 }
3223 /* Fall through for DMA_TO_DEVICE */
3224 case DMA_NONE:
3225 ret = cmd->se_tfo->queue_status(cmd);
3226 break;
3227 default:
3228 break;
3229 }
3230
Christoph Hellwige057f532011-10-17 13:56:41 -04003231out:
3232 if (ret < 0) {
3233 transport_handle_queue_full(cmd, cmd->se_dev);
3234 return;
3235 }
3236 transport_lun_remove_cmd(cmd);
3237 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003238}
3239
3240static void transport_handle_queue_full(
3241 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04003242 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003243{
3244 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003245 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3246 atomic_inc(&dev->dev_qf_count);
3247 smp_mb__after_atomic_inc();
3248 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3249
3250 schedule_work(&cmd->se_dev->qf_work_queue);
3251}
3252
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003253static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003254{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003255 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003256 int reason = 0, ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003257
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003258 /*
3259 * Check if we need to move delayed/dormant tasks from cmds on the
3260 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3261 * Attribute.
3262 */
Andy Grover5951146d2011-07-19 10:26:37 +00003263 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003264 transport_complete_task_attr(cmd);
3265 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003266 * Check to schedule QUEUE_FULL work, or execute an existing
3267 * cmd->transport_qf_callback()
3268 */
3269 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3270 schedule_work(&cmd->se_dev->qf_work_queue);
3271
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003272 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003273 * Check if we need to retrieve a sense buffer from
3274 * the struct se_cmd in question.
3275 */
3276 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3277 if (transport_get_sense_data(cmd) < 0)
3278 reason = TCM_NON_EXISTENT_LUN;
3279
3280 /*
3281 * Only set when an struct se_task->task_scsi_status returned
3282 * a non GOOD status.
3283 */
3284 if (cmd->scsi_status) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003285 ret = transport_send_check_condition_and_sense(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003286 cmd, reason, 1);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003287 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003288 goto queue_full;
3289
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003290 transport_lun_remove_cmd(cmd);
3291 transport_cmd_check_stop_to_fabric(cmd);
3292 return;
3293 }
3294 }
3295 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003296 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003297 * XDWRITE_READ_10 emulation.
3298 */
3299 if (cmd->transport_complete_callback)
3300 cmd->transport_complete_callback(cmd);
3301
3302 switch (cmd->data_direction) {
3303 case DMA_FROM_DEVICE:
3304 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003305 if (cmd->se_lun->lun_sep) {
3306 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003307 cmd->data_length;
3308 }
3309 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003310
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003311 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003312 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003313 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003314 break;
3315 case DMA_TO_DEVICE:
3316 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003317 if (cmd->se_lun->lun_sep) {
3318 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003319 cmd->data_length;
3320 }
3321 spin_unlock(&cmd->se_lun->lun_sep_lock);
3322 /*
3323 * Check if we need to send READ payload for BIDI-COMMAND
3324 */
Andy Groverec98f782011-07-20 19:28:46 +00003325 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003326 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 Bellinger07bde792011-06-13 14:46:09 -07003332 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003333 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003334 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003335 break;
3336 }
3337 /* Fall through for DMA_TO_DEVICE */
3338 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003339 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003340 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003341 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003342 break;
3343 default:
3344 break;
3345 }
3346
3347 transport_lun_remove_cmd(cmd);
3348 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003349 return;
3350
3351queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003352 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003353 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04003354 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3355 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003356}
3357
3358static void transport_free_dev_tasks(struct se_cmd *cmd)
3359{
3360 struct se_task *task, *task_tmp;
3361 unsigned long flags;
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003362 LIST_HEAD(dispose_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003363
Andy Grovera1d8b492011-05-02 17:12:10 -07003364 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003365 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07003366 &cmd->t_task_list, t_list) {
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003367 if (!(task->task_flags & TF_ACTIVE))
3368 list_move_tail(&task->t_list, &dispose_list);
3369 }
3370 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3371
3372 while (!list_empty(&dispose_list)) {
3373 task = list_first_entry(&dispose_list, struct se_task, t_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003374
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003375 if (task->task_sg != cmd->t_data_sg &&
3376 task->task_sg != cmd->t_bidi_data_sg)
3377 kfree(task->task_sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003378
3379 list_del(&task->t_list);
3380
Christoph Hellwig42bf8292011-10-12 11:07:00 -04003381 cmd->se_dev->transport->free_task(task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003382 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003383}
3384
Andy Grover6708bb22011-06-08 10:36:43 -07003385static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003386{
Andy Groverec98f782011-07-20 19:28:46 +00003387 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00003388 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003389
Andy Grover6708bb22011-06-08 10:36:43 -07003390 for_each_sg(sgl, sg, nents, count)
3391 __free_page(sg_page(sg));
3392
3393 kfree(sgl);
3394}
3395
3396static inline void transport_free_pages(struct se_cmd *cmd)
3397{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003398 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07003399 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003400
Andy Grover6708bb22011-06-08 10:36:43 -07003401 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003402 cmd->t_data_sg = NULL;
3403 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003404
Andy Grover6708bb22011-06-08 10:36:43 -07003405 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003406 cmd->t_bidi_data_sg = NULL;
3407 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003408}
3409
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003410/**
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003411 * transport_release_cmd - free a command
3412 * @cmd: command to free
3413 *
3414 * This routine unconditionally frees a command, and reference counting
3415 * or list removal must be done in the caller.
3416 */
3417static void transport_release_cmd(struct se_cmd *cmd)
3418{
3419 BUG_ON(!cmd->se_tfo);
3420
Andy Groverc8e31f22012-01-19 13:39:17 -08003421 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003422 core_tmr_release_req(cmd->se_tmr_req);
3423 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3424 kfree(cmd->t_task_cdb);
3425 /*
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003426 * If this cmd has been setup with target_get_sess_cmd(), drop
3427 * the kref and call ->release_cmd() in kref callback.
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003428 */
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003429 if (cmd->check_release != 0) {
3430 target_put_sess_cmd(cmd->se_sess, cmd);
3431 return;
3432 }
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003433 cmd->se_tfo->release_cmd(cmd);
3434}
3435
3436/**
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003437 * transport_put_cmd - release a reference to a command
3438 * @cmd: command to release
3439 *
3440 * This routine releases our reference to the command and frees it if possible.
3441 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003442static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003443{
3444 unsigned long flags;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003445 int free_tasks = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003446
Andy Grovera1d8b492011-05-02 17:12:10 -07003447 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003448 if (atomic_read(&cmd->t_fe_count)) {
3449 if (!atomic_dec_and_test(&cmd->t_fe_count))
3450 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003451 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003452
3453 if (atomic_read(&cmd->t_se_count)) {
3454 if (!atomic_dec_and_test(&cmd->t_se_count))
3455 goto out_busy;
3456 }
3457
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003458 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
3459 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003460 transport_all_task_dev_remove_state(cmd);
3461 free_tasks = 1;
3462 }
Andy Grovera1d8b492011-05-02 17:12:10 -07003463 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003464
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003465 if (free_tasks != 0)
3466 transport_free_dev_tasks(cmd);
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003467
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003468 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02003469 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003470 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003471out_busy:
3472 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003473}
3474
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003475/*
Andy Groverec98f782011-07-20 19:28:46 +00003476 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3477 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003478 * @cmd: Associated se_cmd descriptor
3479 * @mem: SGL style memory for TCM WRITE / READ
3480 * @sg_mem_num: Number of SGL elements
3481 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3482 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3483 *
3484 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3485 * of parameters.
3486 */
3487int transport_generic_map_mem_to_cmd(
3488 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00003489 struct scatterlist *sgl,
3490 u32 sgl_count,
3491 struct scatterlist *sgl_bidi,
3492 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003493{
Andy Grover5951146d2011-07-19 10:26:37 +00003494 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003495 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003496
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003497 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3498 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
Nicholas Bellingerfef58a62011-11-15 22:13:24 -08003499 /*
3500 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3501 * scatterlists already have been set to follow what the fabric
3502 * passes for the original expected data transfer length.
3503 */
3504 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3505 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3506 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3507 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3508 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3509 return -EINVAL;
3510 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003511
Andy Groverec98f782011-07-20 19:28:46 +00003512 cmd->t_data_sg = sgl;
3513 cmd->t_data_nents = sgl_count;
3514
Andy Grover5951146d2011-07-19 10:26:37 +00003515 if (sgl_bidi && sgl_bidi_count) {
Andy Groverec98f782011-07-20 19:28:46 +00003516 cmd->t_bidi_data_sg = sgl_bidi;
3517 cmd->t_bidi_data_nents = sgl_bidi_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003518 }
3519 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003520 }
3521
3522 return 0;
3523}
3524EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3525
Andy Grover49493142012-01-16 16:57:08 -08003526void *transport_kmap_data_sg(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003527{
Andy Groverec98f782011-07-20 19:28:46 +00003528 struct scatterlist *sg = cmd->t_data_sg;
Andy Grover49493142012-01-16 16:57:08 -08003529 struct page **pages;
3530 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003531
Andy Groverec98f782011-07-20 19:28:46 +00003532 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003533 /*
Andy Groverec98f782011-07-20 19:28:46 +00003534 * We need to take into account a possible offset here for fabrics like
3535 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3536 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00003537 */
Andy Grover49493142012-01-16 16:57:08 -08003538 if (!cmd->t_data_nents)
3539 return NULL;
3540 else if (cmd->t_data_nents == 1)
3541 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003542
Andy Grover49493142012-01-16 16:57:08 -08003543 /* >1 page. use vmap */
3544 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
3545 if (!pages)
3546 return NULL;
3547
3548 /* convert sg[] to pages[] */
3549 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
3550 pages[i] = sg_page(sg);
3551 }
3552
3553 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
3554 kfree(pages);
3555 if (!cmd->t_data_vmap)
3556 return NULL;
3557
3558 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003559}
Andy Grover49493142012-01-16 16:57:08 -08003560EXPORT_SYMBOL(transport_kmap_data_sg);
3561
3562void transport_kunmap_data_sg(struct se_cmd *cmd)
3563{
Andy Grovera1edf9c2012-02-09 12:18:06 -08003564 if (!cmd->t_data_nents) {
Andy Grover49493142012-01-16 16:57:08 -08003565 return;
Andy Grovera1edf9c2012-02-09 12:18:06 -08003566 } else if (cmd->t_data_nents == 1) {
Andy Grover49493142012-01-16 16:57:08 -08003567 kunmap(sg_page(cmd->t_data_sg));
Andy Grovera1edf9c2012-02-09 12:18:06 -08003568 return;
3569 }
Andy Grover49493142012-01-16 16:57:08 -08003570
3571 vunmap(cmd->t_data_vmap);
3572 cmd->t_data_vmap = NULL;
3573}
3574EXPORT_SYMBOL(transport_kunmap_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003575
3576static int
3577transport_generic_get_mem(struct se_cmd *cmd)
3578{
Andy Groverec98f782011-07-20 19:28:46 +00003579 u32 length = cmd->data_length;
3580 unsigned int nents;
3581 struct page *page;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003582 gfp_t zero_flag;
Andy Groverec98f782011-07-20 19:28:46 +00003583 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003584
Andy Groverec98f782011-07-20 19:28:46 +00003585 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3586 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3587 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00003588 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003589
Andy Groverec98f782011-07-20 19:28:46 +00003590 cmd->t_data_nents = nents;
3591 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003592
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003593 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
3594
Andy Groverec98f782011-07-20 19:28:46 +00003595 while (length) {
3596 u32 page_len = min_t(u32, length, PAGE_SIZE);
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003597 page = alloc_page(GFP_KERNEL | zero_flag);
Andy Groverec98f782011-07-20 19:28:46 +00003598 if (!page)
3599 goto out;
3600
3601 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3602 length -= page_len;
3603 i++;
3604 }
3605 return 0;
3606
3607out:
3608 while (i >= 0) {
3609 __free_page(sg_page(&cmd->t_data_sg[i]));
3610 i--;
3611 }
3612 kfree(cmd->t_data_sg);
3613 cmd->t_data_sg = NULL;
3614 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003615}
3616
Andy Grovera1d8b492011-05-02 17:12:10 -07003617/* Reduce sectors if they are too long for the device */
3618static inline sector_t transport_limit_task_sectors(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003619 struct se_device *dev,
3620 unsigned long long lba,
Andy Grovera1d8b492011-05-02 17:12:10 -07003621 sector_t sectors)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003622{
Andy Grovera1d8b492011-05-02 17:12:10 -07003623 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003624
Andy Grovera1d8b492011-05-02 17:12:10 -07003625 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3626 if ((lba + sectors) > transport_dev_end_lba(dev))
3627 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003628
Andy Grovera1d8b492011-05-02 17:12:10 -07003629 return sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003630}
3631
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003632
3633/*
3634 * This function can be used by HW target mode drivers to create a linked
3635 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3636 * This is intended to be called during the completion path by TCM Core
3637 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3638 */
3639void transport_do_task_sg_chain(struct se_cmd *cmd)
3640{
Andy Groverec98f782011-07-20 19:28:46 +00003641 struct scatterlist *sg_first = NULL;
3642 struct scatterlist *sg_prev = NULL;
3643 int sg_prev_nents = 0;
3644 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003645 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003646 u32 chained_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003647 int i;
3648
Andy Groverec98f782011-07-20 19:28:46 +00003649 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3650
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003651 /*
3652 * Walk the struct se_task list and setup scatterlist chains
Andy Grovera1d8b492011-05-02 17:12:10 -07003653 * for each contiguously allocated struct se_task->task_sg[].
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003654 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003655 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Andy Groverec98f782011-07-20 19:28:46 +00003656 if (!task->task_sg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003657 continue;
3658
Andy Groverec98f782011-07-20 19:28:46 +00003659 if (!sg_first) {
3660 sg_first = task->task_sg;
Andy Grover6708bb22011-06-08 10:36:43 -07003661 chained_nents = task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003662 } else {
Andy Groverec98f782011-07-20 19:28:46 +00003663 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
Andy Grover6708bb22011-06-08 10:36:43 -07003664 chained_nents += task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003665 }
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003666 /*
3667 * For the padded tasks, use the extra SGL vector allocated
3668 * in transport_allocate_data_tasks() for the sg_prev_nents
Christoph Hellwig04629b72011-10-12 11:07:04 -04003669 * offset into sg_chain() above.
3670 *
3671 * We do not need the padding for the last task (or a single
3672 * task), but in that case we will never use the sg_prev_nents
3673 * value below which would be incorrect.
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003674 */
Christoph Hellwig04629b72011-10-12 11:07:04 -04003675 sg_prev_nents = (task->task_sg_nents + 1);
Andy Groverec98f782011-07-20 19:28:46 +00003676 sg_prev = task->task_sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003677 }
3678 /*
3679 * Setup the starting pointer and total t_tasks_sg_linked_no including
3680 * padding SGs for linking and to mark the end.
3681 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003682 cmd->t_tasks_sg_chained = sg_first;
Andy Groverec98f782011-07-20 19:28:46 +00003683 cmd->t_tasks_sg_chained_no = chained_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003684
Andy Grover6708bb22011-06-08 10:36:43 -07003685 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
Andy Grovera1d8b492011-05-02 17:12:10 -07003686 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3687 cmd->t_tasks_sg_chained_no);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003688
Andy Grovera1d8b492011-05-02 17:12:10 -07003689 for_each_sg(cmd->t_tasks_sg_chained, sg,
3690 cmd->t_tasks_sg_chained_no, i) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003691
Andy Grover6708bb22011-06-08 10:36:43 -07003692 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
Andy Grover5951146d2011-07-19 10:26:37 +00003693 i, sg, sg_page(sg), sg->length, sg->offset);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003694 if (sg_is_chain(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003695 pr_debug("SG: %p sg_is_chain=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003696 if (sg_is_last(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003697 pr_debug("SG: %p sg_is_last=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003698 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003699}
3700EXPORT_SYMBOL(transport_do_task_sg_chain);
3701
Andy Grovera1d8b492011-05-02 17:12:10 -07003702/*
3703 * Break up cmd into chunks transport can handle
3704 */
Christoph Hellwig38b400672011-10-18 06:57:02 -04003705static int
3706transport_allocate_data_tasks(struct se_cmd *cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003707 enum dma_data_direction data_direction,
Christoph Hellwig38b400672011-10-18 06:57:02 -04003708 struct scatterlist *cmd_sg, unsigned int sgl_nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003709{
Andy Grover5951146d2011-07-19 10:26:37 +00003710 struct se_device *dev = cmd->se_dev;
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003711 int task_count, i;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003712 unsigned long long lba;
3713 sector_t sectors, dev_max_sectors;
3714 u32 sector_size;
3715
3716 if (transport_cmd_get_valid_sectors(cmd) < 0)
3717 return -EINVAL;
3718
3719 dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3720 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003721
Andy Groverec98f782011-07-20 19:28:46 +00003722 WARN_ON(cmd->data_length % sector_size);
Christoph Hellwig38b400672011-10-18 06:57:02 -04003723
3724 lba = cmd->t_task_lba;
Andy Groverec98f782011-07-20 19:28:46 +00003725 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
Nicholas Bellinger277c5f22011-07-26 00:38:40 -07003726 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003727
3728 /*
3729 * If we need just a single task reuse the SG list in the command
3730 * and avoid a lot of work.
3731 */
3732 if (task_count == 1) {
3733 struct se_task *task;
3734 unsigned long flags;
3735
3736 task = transport_generic_get_task(cmd, data_direction);
3737 if (!task)
3738 return -ENOMEM;
3739
3740 task->task_sg = cmd_sg;
3741 task->task_sg_nents = sgl_nents;
3742
3743 task->task_lba = lba;
3744 task->task_sectors = sectors;
3745 task->task_size = task->task_sectors * sector_size;
3746
3747 spin_lock_irqsave(&cmd->t_state_lock, flags);
3748 list_add_tail(&task->t_list, &cmd->t_task_list);
3749 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3750
3751 return task_count;
3752 }
3753
Andy Groverec98f782011-07-20 19:28:46 +00003754 for (i = 0; i < task_count; i++) {
Christoph Hellwig38b400672011-10-18 06:57:02 -04003755 struct se_task *task;
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003756 unsigned int task_size, task_sg_nents_padded;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003757 struct scatterlist *sg;
3758 unsigned long flags;
Andy Groverec98f782011-07-20 19:28:46 +00003759 int count;
Andy Grovera1d8b492011-05-02 17:12:10 -07003760
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003761 task = transport_generic_get_task(cmd, data_direction);
Andy Grovera1d8b492011-05-02 17:12:10 -07003762 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003763 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003764
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003765 task->task_lba = lba;
Andy Groverec98f782011-07-20 19:28:46 +00003766 task->task_sectors = min(sectors, dev_max_sectors);
3767 task->task_size = task->task_sectors * sector_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003768
Nicholas Bellinger525a48a2011-08-13 02:11:38 -07003769 /*
3770 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3771 * in order to calculate the number per task SGL entries
3772 */
3773 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003774 /*
Andy Groverec98f782011-07-20 19:28:46 +00003775 * Check if the fabric module driver is requesting that all
3776 * struct se_task->task_sg[] be chained together.. If so,
3777 * then allocate an extra padding SG entry for linking and
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003778 * marking the end of the chained SGL for every task except
3779 * the last one for (task_count > 1) operation, or skipping
3780 * the extra padding for the (task_count == 1) case.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003781 */
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003782 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3783 task_sg_nents_padded = (task->task_sg_nents + 1);
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003784 } else
3785 task_sg_nents_padded = task->task_sg_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003786
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003787 task->task_sg = kmalloc(sizeof(struct scatterlist) *
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003788 task_sg_nents_padded, GFP_KERNEL);
Andy Groverec98f782011-07-20 19:28:46 +00003789 if (!task->task_sg) {
3790 cmd->se_dev->transport->free_task(task);
3791 return -ENOMEM;
3792 }
3793
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003794 sg_init_table(task->task_sg, task_sg_nents_padded);
Andy Groverec98f782011-07-20 19:28:46 +00003795
3796 task_size = task->task_size;
3797
3798 /* Build new sgl, only up to task_size */
Andy Grover6708bb22011-06-08 10:36:43 -07003799 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
Andy Groverec98f782011-07-20 19:28:46 +00003800 if (cmd_sg->length > task_size)
3801 break;
3802
3803 *sg = *cmd_sg;
3804 task_size -= cmd_sg->length;
3805 cmd_sg = sg_next(cmd_sg);
3806 }
3807
3808 lba += task->task_sectors;
3809 sectors -= task->task_sectors;
3810
3811 spin_lock_irqsave(&cmd->t_state_lock, flags);
3812 list_add_tail(&task->t_list, &cmd->t_task_list);
3813 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003814 }
3815
Andy Groverec98f782011-07-20 19:28:46 +00003816 return task_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003817}
3818
3819static int
Andy Groverec98f782011-07-20 19:28:46 +00003820transport_allocate_control_task(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003821{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003822 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003823 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003824
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08003825 /* Workaround for handling zero-length control CDBs */
3826 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3827 !cmd->data_length)
3828 return 0;
3829
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003830 task = transport_generic_get_task(cmd, cmd->data_direction);
3831 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003832 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003833
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003834 task->task_sg = cmd->t_data_sg;
Andy Groverec98f782011-07-20 19:28:46 +00003835 task->task_size = cmd->data_length;
Andy Grover6708bb22011-06-08 10:36:43 -07003836 task->task_sg_nents = cmd->t_data_nents;
Andy Groverec98f782011-07-20 19:28:46 +00003837
3838 spin_lock_irqsave(&cmd->t_state_lock, flags);
3839 list_add_tail(&task->t_list, &cmd->t_task_list);
3840 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003841
Andy Grover6708bb22011-06-08 10:36:43 -07003842 /* Success! Return number of tasks allocated */
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003843 return 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003844}
3845
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003846/*
3847 * Allocate any required ressources to execute the command, and either place
3848 * it on the execution queue if possible. For writes we might not have the
3849 * payload yet, thus notify the fabric via a call to ->write_pending instead.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003850 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003851int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003852{
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003853 struct se_device *dev = cmd->se_dev;
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003854 int task_cdbs, task_cdbs_bidi = 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003855 int set_counts = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003856 int ret = 0;
3857
3858 /*
3859 * Determine is the TCM fabric module has already allocated physical
3860 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00003861 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003862 */
Andy Groverec98f782011-07-20 19:28:46 +00003863 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3864 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00003865 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003866 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003867 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003868 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003869
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003870 /*
Christoph Hellwig38b400672011-10-18 06:57:02 -04003871 * For BIDI command set up the read tasks first.
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003872 */
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003873 if (cmd->t_bidi_data_sg &&
Christoph Hellwig38b400672011-10-18 06:57:02 -04003874 dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
3875 BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
3876
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003877 task_cdbs_bidi = transport_allocate_data_tasks(cmd,
3878 DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
3879 cmd->t_bidi_data_nents);
3880 if (task_cdbs_bidi <= 0)
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003881 goto out_fail;
3882
3883 atomic_inc(&cmd->t_fe_count);
3884 atomic_inc(&cmd->t_se_count);
3885 set_counts = 0;
3886 }
Christoph Hellwig38b400672011-10-18 06:57:02 -04003887
3888 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3889 task_cdbs = transport_allocate_data_tasks(cmd,
3890 cmd->data_direction, cmd->t_data_sg,
3891 cmd->t_data_nents);
3892 } else {
3893 task_cdbs = transport_allocate_control_task(cmd);
3894 }
3895
Roland Dreier410f6702011-11-22 13:51:32 -08003896 if (task_cdbs < 0)
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003897 goto out_fail;
Roland Dreier410f6702011-11-22 13:51:32 -08003898 else if (!task_cdbs && (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003899 spin_lock_irq(&cmd->t_state_lock);
Roland Dreier410f6702011-11-22 13:51:32 -08003900 cmd->t_state = TRANSPORT_COMPLETE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003901 cmd->transport_state |= CMD_T_ACTIVE;
3902 spin_unlock_irq(&cmd->t_state_lock);
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08003903
3904 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
3905 u8 ua_asc = 0, ua_ascq = 0;
3906
3907 core_scsi3_ua_clear_for_request_sense(cmd,
3908 &ua_asc, &ua_ascq);
3909 }
3910
Roland Dreier410f6702011-11-22 13:51:32 -08003911 INIT_WORK(&cmd->work, target_complete_ok_work);
3912 queue_work(target_completion_wq, &cmd->work);
3913 return 0;
3914 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003915
3916 if (set_counts) {
3917 atomic_inc(&cmd->t_fe_count);
3918 atomic_inc(&cmd->t_se_count);
3919 }
3920
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003921 cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
3922 atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
3923 atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003924
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003925 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07003926 * For WRITEs, let the fabric know its buffer is ready..
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003927 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3928 * will be added to the struct se_device execution queue after its WRITE
3929 * data has arrived. (ie: It gets handled by the transport processing
3930 * thread a second time)
3931 */
3932 if (cmd->data_direction == DMA_TO_DEVICE) {
3933 transport_add_tasks_to_state_queue(cmd);
3934 return transport_generic_write_pending(cmd);
3935 }
3936 /*
3937 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3938 * to the execution queue.
3939 */
3940 transport_execute_tasks(cmd);
3941 return 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003942
3943out_fail:
3944 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3945 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3946 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003947}
Andy Grovera1d8b492011-05-02 17:12:10 -07003948EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003949
3950/* transport_generic_process_write():
3951 *
3952 *
3953 */
3954void transport_generic_process_write(struct se_cmd *cmd)
3955{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003956 transport_execute_tasks(cmd);
3957}
3958EXPORT_SYMBOL(transport_generic_process_write);
3959
Christoph Hellwige057f532011-10-17 13:56:41 -04003960static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003961{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003962 int ret;
3963
3964 ret = cmd->se_tfo->write_pending(cmd);
3965 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04003966 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3967 cmd);
3968 transport_handle_queue_full(cmd, cmd->se_dev);
3969 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003970}
3971
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003972static int transport_generic_write_pending(struct se_cmd *cmd)
3973{
3974 unsigned long flags;
3975 int ret;
3976
Andy Grovera1d8b492011-05-02 17:12:10 -07003977 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003978 cmd->t_state = TRANSPORT_WRITE_PENDING;
Andy Grovera1d8b492011-05-02 17:12:10 -07003979 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003980
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003981 /*
3982 * Clear the se_cmd for WRITE_PENDING status in order to set
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003983 * CMD_T_ACTIVE so that transport_generic_handle_data can be called
3984 * from HW target mode interrupt code. This is safe to be called
3985 * with transport_off=1 before the cmd->se_tfo->write_pending
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003986 * because the se_cmd->se_lun pointer is not being cleared.
3987 */
3988 transport_cmd_check_stop(cmd, 1, 0);
3989
3990 /*
3991 * Call the fabric write_pending function here to let the
3992 * frontend know that WRITE buffers are ready.
3993 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003994 ret = cmd->se_tfo->write_pending(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003995 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003996 goto queue_full;
3997 else if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003998 return ret;
3999
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004000 return 1;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004001
4002queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07004003 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004004 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
Christoph Hellwige057f532011-10-17 13:56:41 -04004005 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004006 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004007}
4008
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07004009void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004010{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004011 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
Andy Groverc8e31f22012-01-19 13:39:17 -08004012 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004013 transport_wait_for_tasks(cmd);
4014
Christoph Hellwig35462972011-05-31 23:56:57 -04004015 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004016 } else {
4017 if (wait_for_tasks)
4018 transport_wait_for_tasks(cmd);
4019
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004020 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
4021
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02004022 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004023 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004024
Nicholas Bellingerf4366772011-05-19 20:19:11 -07004025 transport_free_dev_tasks(cmd);
4026
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07004027 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004028 }
4029}
4030EXPORT_SYMBOL(transport_generic_free_cmd);
4031
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004032/* target_get_sess_cmd - Add command to active ->sess_cmd_list
4033 * @se_sess: session to reference
4034 * @se_cmd: command descriptor to add
Nicholas Bellingera6360782011-11-18 20:36:22 -08004035 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004036 */
Nicholas Bellingera6360782011-11-18 20:36:22 -08004037void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
4038 bool ack_kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004039{
4040 unsigned long flags;
4041
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004042 kref_init(&se_cmd->cmd_kref);
Nicholas Bellingera6360782011-11-18 20:36:22 -08004043 /*
4044 * Add a second kref if the fabric caller is expecting to handle
4045 * fabric acknowledgement that requires two target_put_sess_cmd()
4046 * invocations before se_cmd descriptor release.
4047 */
Nicholas Bellinger86715562012-02-13 01:07:22 -08004048 if (ack_kref == true) {
Nicholas Bellingera6360782011-11-18 20:36:22 -08004049 kref_get(&se_cmd->cmd_kref);
Nicholas Bellinger86715562012-02-13 01:07:22 -08004050 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
4051 }
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004052
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004053 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4054 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
4055 se_cmd->check_release = 1;
4056 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4057}
4058EXPORT_SYMBOL(target_get_sess_cmd);
4059
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004060static void target_release_cmd_kref(struct kref *kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004061{
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004062 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
4063 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004064 unsigned long flags;
4065
4066 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4067 if (list_empty(&se_cmd->se_cmd_list)) {
4068 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Nicholas Bellingerffc32d52012-02-13 02:35:01 -08004069 se_cmd->se_tfo->release_cmd(se_cmd);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004070 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004071 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004072 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
4073 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4074 complete(&se_cmd->cmd_wait_comp);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004075 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004076 }
4077 list_del(&se_cmd->se_cmd_list);
4078 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4079
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004080 se_cmd->se_tfo->release_cmd(se_cmd);
4081}
4082
4083/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
4084 * @se_sess: session to reference
4085 * @se_cmd: command descriptor to drop
4086 */
4087int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
4088{
4089 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004090}
4091EXPORT_SYMBOL(target_put_sess_cmd);
4092
4093/* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
4094 * @se_sess: session to split
4095 */
4096void target_splice_sess_cmd_list(struct se_session *se_sess)
4097{
4098 struct se_cmd *se_cmd;
4099 unsigned long flags;
4100
4101 WARN_ON(!list_empty(&se_sess->sess_wait_list));
4102 INIT_LIST_HEAD(&se_sess->sess_wait_list);
4103
4104 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4105 se_sess->sess_tearing_down = 1;
4106
4107 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
4108
4109 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
4110 se_cmd->cmd_wait_set = 1;
4111
4112 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4113}
4114EXPORT_SYMBOL(target_splice_sess_cmd_list);
4115
4116/* target_wait_for_sess_cmds - Wait for outstanding descriptors
4117 * @se_sess: session to wait for active I/O
4118 * @wait_for_tasks: Make extra transport_wait_for_tasks call
4119 */
4120void target_wait_for_sess_cmds(
4121 struct se_session *se_sess,
4122 int wait_for_tasks)
4123{
4124 struct se_cmd *se_cmd, *tmp_cmd;
4125 bool rc = false;
4126
4127 list_for_each_entry_safe(se_cmd, tmp_cmd,
4128 &se_sess->sess_wait_list, se_cmd_list) {
4129 list_del(&se_cmd->se_cmd_list);
4130
4131 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
4132 " %d\n", se_cmd, se_cmd->t_state,
4133 se_cmd->se_tfo->get_cmd_state(se_cmd));
4134
4135 if (wait_for_tasks) {
4136 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
4137 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4138 se_cmd->se_tfo->get_cmd_state(se_cmd));
4139
4140 rc = transport_wait_for_tasks(se_cmd);
4141
4142 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
4143 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4144 se_cmd->se_tfo->get_cmd_state(se_cmd));
4145 }
4146
4147 if (!rc) {
4148 wait_for_completion(&se_cmd->cmd_wait_comp);
4149 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
4150 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4151 se_cmd->se_tfo->get_cmd_state(se_cmd));
4152 }
4153
4154 se_cmd->se_tfo->release_cmd(se_cmd);
4155 }
4156}
4157EXPORT_SYMBOL(target_wait_for_sess_cmds);
4158
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004159/* transport_lun_wait_for_tasks():
4160 *
4161 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4162 * an struct se_lun to be successfully shutdown.
4163 */
4164static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4165{
4166 unsigned long flags;
4167 int ret;
4168 /*
4169 * If the frontend has already requested this struct se_cmd to
4170 * be stopped, we can safely ignore this struct se_cmd.
4171 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004172 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004173 if (cmd->transport_state & CMD_T_STOP) {
4174 cmd->transport_state &= ~CMD_T_LUN_STOP;
4175
4176 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
4177 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004178 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004179 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovere3d6f902011-07-19 08:55:10 +00004180 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004181 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004182 cmd->transport_state |= CMD_T_LUN_FE_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07004183 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004184
Andy Grover5951146d2011-07-19 10:26:37 +00004185 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004186
4187 ret = transport_stop_tasks_for_cmd(cmd);
4188
Andy Grover6708bb22011-06-08 10:36:43 -07004189 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4190 " %d\n", cmd, cmd->t_task_list_num, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004191 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07004192 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004193 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004194 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07004195 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004196 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004197 }
Christoph Hellwig3df8d402011-10-17 13:56:43 -04004198 transport_remove_cmd_from_queue(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004199
4200 return 0;
4201}
4202
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004203static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4204{
4205 struct se_cmd *cmd = NULL;
4206 unsigned long lun_flags, cmd_flags;
4207 /*
4208 * Do exception processing and return CHECK_CONDITION status to the
4209 * Initiator Port.
4210 */
4211 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00004212 while (!list_empty(&lun->lun_cmd_list)) {
4213 cmd = list_first_entry(&lun->lun_cmd_list,
4214 struct se_cmd, se_lun_node);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -05004215 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004216
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004217 /*
4218 * This will notify iscsi_target_transport.c:
4219 * transport_cmd_check_stop() that a LUN shutdown is in
4220 * progress for the iscsi_cmd_t.
4221 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004222 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07004223 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004224 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004225 cmd->se_lun->unpacked_lun,
4226 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004227 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07004228 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004229
4230 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4231
Andy Grover6708bb22011-06-08 10:36:43 -07004232 if (!cmd->se_lun) {
4233 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004234 cmd->se_tfo->get_task_tag(cmd),
4235 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004236 BUG();
4237 }
4238 /*
4239 * If the Storage engine still owns the iscsi_cmd_t, determine
4240 * and/or stop its context.
4241 */
Andy Grover6708bb22011-06-08 10:36:43 -07004242 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00004243 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4244 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004245
Andy Grovere3d6f902011-07-19 08:55:10 +00004246 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004247 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4248 continue;
4249 }
4250
Andy Grover6708bb22011-06-08 10:36:43 -07004251 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004252 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004253 cmd->se_lun->unpacked_lun,
4254 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004255
Andy Grovera1d8b492011-05-02 17:12:10 -07004256 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004257 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004258 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004259 goto check_cond;
4260 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004261 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004262 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07004263 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004264
4265 transport_free_dev_tasks(cmd);
4266 /*
4267 * The Storage engine stopped this struct se_cmd before it was
4268 * send to the fabric frontend for delivery back to the
4269 * Initiator Node. Return this SCSI CDB back with an
4270 * CHECK_CONDITION status.
4271 */
4272check_cond:
4273 transport_send_check_condition_and_sense(cmd,
4274 TCM_NON_EXISTENT_LUN, 0);
4275 /*
4276 * If the fabric frontend is waiting for this iscsi_cmd_t to
4277 * be released, notify the waiting thread now that LU has
4278 * finished accessing it.
4279 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004280 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004281 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07004282 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004283 " struct se_cmd: %p ITT: 0x%08x\n",
4284 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00004285 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004286
Andy Grovera1d8b492011-05-02 17:12:10 -07004287 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004288 cmd_flags);
4289 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -07004290 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004291 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4292 continue;
4293 }
Andy Grover6708bb22011-06-08 10:36:43 -07004294 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004295 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004296
Andy Grovera1d8b492011-05-02 17:12:10 -07004297 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004298 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4299 }
4300 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4301}
4302
4303static int transport_clear_lun_thread(void *p)
4304{
Jörn Engel8359cf42011-11-24 02:05:51 +01004305 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004306
4307 __transport_clear_lun_from_sessions(lun);
4308 complete(&lun->lun_shutdown_comp);
4309
4310 return 0;
4311}
4312
4313int transport_clear_lun_from_sessions(struct se_lun *lun)
4314{
4315 struct task_struct *kt;
4316
Andy Grover5951146d2011-07-19 10:26:37 +00004317 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004318 "tcm_cl_%u", lun->unpacked_lun);
4319 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07004320 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00004321 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004322 }
4323 wait_for_completion(&lun->lun_shutdown_comp);
4324
4325 return 0;
4326}
4327
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004328/**
4329 * transport_wait_for_tasks - wait for completion to occur
4330 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004331 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004332 * Called from frontend fabric context to wait for storage engine
4333 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004334 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004335bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004336{
4337 unsigned long flags;
4338
Andy Grovera1d8b492011-05-02 17:12:10 -07004339 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08004340 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
4341 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004342 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004343 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004344 }
4345 /*
4346 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4347 * has been set in transport_set_supported_SAM_opcode().
4348 */
Andy Groverc8e31f22012-01-19 13:39:17 -08004349 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
4350 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004351 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004352 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004353 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004354 /*
4355 * If we are already stopped due to an external event (ie: LUN shutdown)
4356 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07004357 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004358 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4359 * has completed its operation on the struct se_cmd.
4360 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004361 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07004362 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00004363 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004364 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004365 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004366 /*
4367 * There is a special case for WRITES where a FE exception +
4368 * LUN shutdown means ConfigFS context is still sleeping on
4369 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4370 * We go ahead and up transport_lun_stop_comp just to be sure
4371 * here.
4372 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004373 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4374 complete(&cmd->transport_lun_stop_comp);
4375 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4376 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004377
4378 transport_all_task_dev_remove_state(cmd);
4379 /*
4380 * At this point, the frontend who was the originator of this
4381 * struct se_cmd, now owns the structure and can be released through
4382 * normal means below.
4383 */
Andy Grover6708bb22011-06-08 10:36:43 -07004384 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00004385 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004386 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004387 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004388
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004389 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004390 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004391
Nicholas Bellinger3d289342012-02-13 02:38:14 -08004392 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004393 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004394 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004395 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004396
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004397 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004398
Andy Grover6708bb22011-06-08 10:36:43 -07004399 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004400 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004401 cmd, cmd->se_tfo->get_task_tag(cmd),
4402 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004403
Andy Grovera1d8b492011-05-02 17:12:10 -07004404 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004405
Andy Grover5951146d2011-07-19 10:26:37 +00004406 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004407
Andy Grovera1d8b492011-05-02 17:12:10 -07004408 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004409
Andy Grovera1d8b492011-05-02 17:12:10 -07004410 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004411 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004412
Andy Grover6708bb22011-06-08 10:36:43 -07004413 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07004414 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004415 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004416
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004417 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004418
4419 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004420}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004421EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004422
4423static int transport_get_sense_codes(
4424 struct se_cmd *cmd,
4425 u8 *asc,
4426 u8 *ascq)
4427{
4428 *asc = cmd->scsi_asc;
4429 *ascq = cmd->scsi_ascq;
4430
4431 return 0;
4432}
4433
4434static int transport_set_sense_codes(
4435 struct se_cmd *cmd,
4436 u8 asc,
4437 u8 ascq)
4438{
4439 cmd->scsi_asc = asc;
4440 cmd->scsi_ascq = ascq;
4441
4442 return 0;
4443}
4444
4445int transport_send_check_condition_and_sense(
4446 struct se_cmd *cmd,
4447 u8 reason,
4448 int from_transport)
4449{
4450 unsigned char *buffer = cmd->sense_buffer;
4451 unsigned long flags;
4452 int offset;
4453 u8 asc = 0, ascq = 0;
4454
Andy Grovera1d8b492011-05-02 17:12:10 -07004455 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004456 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004457 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004458 return 0;
4459 }
4460 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07004461 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004462
4463 if (!reason && from_transport)
4464 goto after_reason;
4465
4466 if (!from_transport)
4467 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4468 /*
4469 * Data Segment and SenseLength of the fabric response PDU.
4470 *
4471 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4472 * from include/scsi/scsi_cmnd.h
4473 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004474 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004475 TRANSPORT_SENSE_BUFFER);
4476 /*
4477 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4478 * SENSE KEY values from include/scsi/scsi.h
4479 */
4480 switch (reason) {
4481 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004482 /* CURRENT ERROR */
4483 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004484 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004485 /* ILLEGAL REQUEST */
4486 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4487 /* LOGICAL UNIT NOT SUPPORTED */
4488 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4489 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004490 case TCM_UNSUPPORTED_SCSI_OPCODE:
4491 case TCM_SECTOR_COUNT_TOO_MANY:
4492 /* CURRENT ERROR */
4493 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004494 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004495 /* ILLEGAL REQUEST */
4496 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4497 /* INVALID COMMAND OPERATION CODE */
4498 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4499 break;
4500 case TCM_UNKNOWN_MODE_PAGE:
4501 /* CURRENT ERROR */
4502 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004503 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004504 /* ILLEGAL REQUEST */
4505 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4506 /* INVALID FIELD IN CDB */
4507 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4508 break;
4509 case TCM_CHECK_CONDITION_ABORT_CMD:
4510 /* CURRENT ERROR */
4511 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004512 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004513 /* ABORTED COMMAND */
4514 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4515 /* BUS DEVICE RESET FUNCTION OCCURRED */
4516 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4517 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4518 break;
4519 case TCM_INCORRECT_AMOUNT_OF_DATA:
4520 /* CURRENT ERROR */
4521 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004522 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004523 /* ABORTED COMMAND */
4524 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4525 /* WRITE ERROR */
4526 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4527 /* NOT ENOUGH UNSOLICITED DATA */
4528 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4529 break;
4530 case TCM_INVALID_CDB_FIELD:
4531 /* CURRENT ERROR */
4532 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004533 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004534 /* ILLEGAL REQUEST */
4535 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004536 /* INVALID FIELD IN CDB */
4537 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4538 break;
4539 case TCM_INVALID_PARAMETER_LIST:
4540 /* CURRENT ERROR */
4541 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004542 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004543 /* ILLEGAL REQUEST */
4544 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004545 /* INVALID FIELD IN PARAMETER LIST */
4546 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4547 break;
4548 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4549 /* CURRENT ERROR */
4550 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004551 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004552 /* ABORTED COMMAND */
4553 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4554 /* WRITE ERROR */
4555 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4556 /* UNEXPECTED_UNSOLICITED_DATA */
4557 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4558 break;
4559 case TCM_SERVICE_CRC_ERROR:
4560 /* CURRENT ERROR */
4561 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004562 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004563 /* ABORTED COMMAND */
4564 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4565 /* PROTOCOL SERVICE CRC ERROR */
4566 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4567 /* N/A */
4568 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4569 break;
4570 case TCM_SNACK_REJECTED:
4571 /* CURRENT ERROR */
4572 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004573 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004574 /* ABORTED COMMAND */
4575 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4576 /* READ ERROR */
4577 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4578 /* FAILED RETRANSMISSION REQUEST */
4579 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4580 break;
4581 case TCM_WRITE_PROTECTED:
4582 /* CURRENT ERROR */
4583 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004584 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004585 /* DATA PROTECT */
4586 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4587 /* WRITE PROTECTED */
4588 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4589 break;
4590 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4591 /* CURRENT ERROR */
4592 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004593 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004594 /* UNIT ATTENTION */
4595 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4596 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4597 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4598 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4599 break;
4600 case TCM_CHECK_CONDITION_NOT_READY:
4601 /* CURRENT ERROR */
4602 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004603 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004604 /* Not Ready */
4605 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4606 transport_get_sense_codes(cmd, &asc, &ascq);
4607 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4608 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4609 break;
4610 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4611 default:
4612 /* CURRENT ERROR */
4613 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004614 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004615 /* ILLEGAL REQUEST */
4616 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4617 /* LOGICAL UNIT COMMUNICATION FAILURE */
4618 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4619 break;
4620 }
4621 /*
4622 * This code uses linux/include/scsi/scsi.h SAM status codes!
4623 */
4624 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4625 /*
4626 * Automatically padded, this value is encoded in the fabric's
4627 * data_length response PDU containing the SCSI defined sense data.
4628 */
4629 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4630
4631after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004632 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004633}
4634EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4635
4636int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4637{
4638 int ret = 0;
4639
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004640 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07004641 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004642 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4643 return 1;
4644#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004645 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004646 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07004647 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004648 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004649#endif
4650 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00004651 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004652 ret = 1;
4653 }
4654 return ret;
4655}
4656EXPORT_SYMBOL(transport_check_aborted_status);
4657
4658void transport_send_task_abort(struct se_cmd *cmd)
4659{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07004660 unsigned long flags;
4661
4662 spin_lock_irqsave(&cmd->t_state_lock, flags);
4663 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4664 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4665 return;
4666 }
4667 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4668
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004669 /*
4670 * If there are still expected incoming fabric WRITEs, we wait
4671 * until until they have completed before sending a TASK_ABORTED
4672 * response. This response with TASK_ABORTED status will be
4673 * queued back to fabric module by transport_check_aborted_status().
4674 */
4675 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004676 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004677 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004678 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004679 }
4680 }
4681 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4682#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004683 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07004684 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004685 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004686#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00004687 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004688}
4689
Christoph Hellwige26d99a2011-11-14 12:30:30 -05004690static int transport_generic_do_tmr(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004691{
Andy Grover5951146d2011-07-19 10:26:37 +00004692 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004693 struct se_tmr_req *tmr = cmd->se_tmr_req;
4694 int ret;
4695
4696 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004697 case TMR_ABORT_TASK:
Nicholas Bellinger3d289342012-02-13 02:38:14 -08004698 core_tmr_abort_task(dev, tmr, cmd->se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004699 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004700 case TMR_ABORT_TASK_SET:
4701 case TMR_CLEAR_ACA:
4702 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004703 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4704 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004705 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004706 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4707 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4708 TMR_FUNCTION_REJECTED;
4709 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004710 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004711 tmr->response = TMR_FUNCTION_REJECTED;
4712 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004713 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004714 tmr->response = TMR_FUNCTION_REJECTED;
4715 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004716 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004717 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004718 tmr->function);
4719 tmr->response = TMR_FUNCTION_REJECTED;
4720 break;
4721 }
4722
4723 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00004724 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004725
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04004726 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004727 return 0;
4728}
4729
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004730/* transport_processing_thread():
4731 *
4732 *
4733 */
4734static int transport_processing_thread(void *param)
4735{
Andy Grover5951146d2011-07-19 10:26:37 +00004736 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004737 struct se_cmd *cmd;
Jörn Engel8359cf42011-11-24 02:05:51 +01004738 struct se_device *dev = param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004739
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004740 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004741 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4742 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004743 kthread_should_stop());
4744 if (ret < 0)
4745 goto out;
4746
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004747get_cmd:
Andy Grover5951146d2011-07-19 10:26:37 +00004748 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4749 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004750 continue;
4751
Andy Grover5951146d2011-07-19 10:26:37 +00004752 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02004753 case TRANSPORT_NEW_CMD:
4754 BUG();
4755 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004756 case TRANSPORT_NEW_CMD_MAP:
Andy Grover6708bb22011-06-08 10:36:43 -07004757 if (!cmd->se_tfo->new_cmd_map) {
4758 pr_err("cmd->se_tfo->new_cmd_map is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004759 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4760 BUG();
4761 }
Andy Grovere3d6f902011-07-19 08:55:10 +00004762 ret = cmd->se_tfo->new_cmd_map(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004763 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004764 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004765 break;
4766 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004767 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004768 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004769 transport_generic_request_failure(cmd);
4770 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004771 }
4772 break;
4773 case TRANSPORT_PROCESS_WRITE:
4774 transport_generic_process_write(cmd);
4775 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004776 case TRANSPORT_PROCESS_TMR:
4777 transport_generic_do_tmr(cmd);
4778 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004779 case TRANSPORT_COMPLETE_QF_WP:
Christoph Hellwige057f532011-10-17 13:56:41 -04004780 transport_write_pending_qf(cmd);
4781 break;
4782 case TRANSPORT_COMPLETE_QF_OK:
4783 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004784 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004785 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004786 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4787 "i_state: %d on SE LUN: %u\n",
4788 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00004789 cmd->se_tfo->get_task_tag(cmd),
4790 cmd->se_tfo->get_cmd_state(cmd),
4791 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004792 BUG();
4793 }
4794
4795 goto get_cmd;
4796 }
4797
4798out:
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07004799 WARN_ON(!list_empty(&dev->state_task_list));
4800 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004801 dev->process_thread = NULL;
4802 return 0;
4803}