blob: d931edf11c2b231f2a0939ed4d9dc959ef36ce57 [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/*
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700255 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800256 */
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{
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700308 unsigned long flags;
309
310 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800311 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
Nicholas Bellinger140854c2011-08-31 12:34:39 -0700312 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800313}
314EXPORT_SYMBOL(transport_register_session);
315
316void transport_deregister_session_configfs(struct se_session *se_sess)
317{
318 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700319 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800320 /*
321 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
322 */
323 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700324 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700325 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800326 list_del(&se_sess->sess_acl_list);
327 /*
328 * If the session list is empty, then clear the pointer.
329 * Otherwise, set the struct se_session pointer from the tail
330 * element of the per struct se_node_acl active session list.
331 */
332 if (list_empty(&se_nacl->acl_sess_list))
333 se_nacl->nacl_sess = NULL;
334 else {
335 se_nacl->nacl_sess = container_of(
336 se_nacl->acl_sess_list.prev,
337 struct se_session, sess_acl_list);
338 }
Roland Dreier23388862011-06-22 01:02:21 -0700339 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800340 }
341}
342EXPORT_SYMBOL(transport_deregister_session_configfs);
343
344void transport_free_session(struct se_session *se_sess)
345{
346 kmem_cache_free(se_sess_cache, se_sess);
347}
348EXPORT_SYMBOL(transport_free_session);
349
350void transport_deregister_session(struct se_session *se_sess)
351{
352 struct se_portal_group *se_tpg = se_sess->se_tpg;
353 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700354 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800355
Andy Grover6708bb22011-06-08 10:36:43 -0700356 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800357 transport_free_session(se_sess);
358 return;
359 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800360
Roland Dreiere63a8e12011-08-12 16:01:02 -0700361 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800362 list_del(&se_sess->sess_list);
363 se_sess->se_tpg = NULL;
364 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700365 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800366
367 /*
368 * Determine if we need to do extra work for this initiator node's
369 * struct se_node_acl if it had been previously dynamically generated.
370 */
371 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700372 if (se_nacl) {
Roland Dreiere63a8e12011-08-12 16:01:02 -0700373 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800374 if (se_nacl->dynamic_node_acl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700375 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
376 se_tpg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800377 list_del(&se_nacl->acl_list);
378 se_tpg->num_node_acls--;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700379 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380
381 core_tpg_wait_for_nacl_pr_ref(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800382 core_free_device_list_for_node(se_nacl, se_tpg);
Andy Grovere3d6f902011-07-19 08:55:10 +0000383 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800384 se_nacl);
Roland Dreiere63a8e12011-08-12 16:01:02 -0700385 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800386 }
387 }
Roland Dreiere63a8e12011-08-12 16:01:02 -0700388 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800389 }
390
391 transport_free_session(se_sess);
392
Andy Grover6708bb22011-06-08 10:36:43 -0700393 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000394 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395}
396EXPORT_SYMBOL(transport_deregister_session);
397
398/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700399 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800400 */
401static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
402{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400403 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800404 struct se_task *task;
405 unsigned long flags;
406
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400407 if (!dev)
408 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800409
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400410 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400411 if (task->task_flags & TF_ACTIVE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412 continue;
413
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800414 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500415 if (task->t_state_active) {
416 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
417 cmd->se_tfo->get_task_tag(cmd), dev, task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500419 list_del(&task->t_state_list);
420 atomic_dec(&cmd->t_task_cdbs_ex_left);
421 task->t_state_active = false;
422 }
423 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800424 }
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500425
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800426}
427
428/* transport_cmd_check_stop():
429 *
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500430 * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800431 * 'transport_off = 2' determines if task_dev_state should be removed.
432 *
433 * A non-zero u8 t_state sets cmd->t_state.
434 * Returns 1 when command is stopped, else 0.
435 */
436static int transport_cmd_check_stop(
437 struct se_cmd *cmd,
438 int transport_off,
439 u8 t_state)
440{
441 unsigned long flags;
442
Andy Grovera1d8b492011-05-02 17:12:10 -0700443 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800444 /*
445 * Determine if IOCTL context caller in requesting the stopping of this
446 * command for LUN shutdown purposes.
447 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500448 if (cmd->transport_state & CMD_T_LUN_STOP) {
449 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
450 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500452 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800453 if (transport_off == 2)
454 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700455 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800456
Andy Grovera1d8b492011-05-02 17:12:10 -0700457 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800458 return 1;
459 }
460 /*
461 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000462 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800463 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500464 if (cmd->transport_state & CMD_T_STOP) {
465 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
466 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000467 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800468
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800469 if (transport_off == 2)
470 transport_all_task_dev_remove_state(cmd);
471
472 /*
473 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
474 * to FE.
475 */
476 if (transport_off == 2)
477 cmd->se_lun = NULL;
Andy Grovera1d8b492011-05-02 17:12:10 -0700478 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479
Andy Grovera1d8b492011-05-02 17:12:10 -0700480 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800481 return 1;
482 }
483 if (transport_off) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500484 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800485 if (transport_off == 2) {
486 transport_all_task_dev_remove_state(cmd);
487 /*
488 * Clear struct se_cmd->se_lun before the transport_off == 2
489 * handoff to fabric module.
490 */
491 cmd->se_lun = NULL;
492 /*
493 * Some fabric modules like tcm_loop can release
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300494 * their internally allocated I/O reference now and
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800495 * struct se_cmd now.
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700496 *
497 * Fabric modules are expected to return '1' here if the
498 * se_cmd being passed is released at this point,
499 * or zero if not being released.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000501 if (cmd->se_tfo->check_stop_free != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800502 spin_unlock_irqrestore(
Andy Grovera1d8b492011-05-02 17:12:10 -0700503 &cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800504
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700505 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800506 }
507 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700508 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800509
510 return 0;
511 } else if (t_state)
512 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700513 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800514
515 return 0;
516}
517
518static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
519{
520 return transport_cmd_check_stop(cmd, 2, 0);
521}
522
523static void transport_lun_remove_cmd(struct se_cmd *cmd)
524{
Andy Grovere3d6f902011-07-19 08:55:10 +0000525 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800526 unsigned long flags;
527
528 if (!lun)
529 return;
530
Andy Grovera1d8b492011-05-02 17:12:10 -0700531 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500532 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
533 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
534 transport_all_task_dev_remove_state(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700536 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800537
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800538 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500539 if (!list_empty(&cmd->se_lun_node))
540 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800541 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
542}
543
544void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
545{
Andy Groverc8e31f22012-01-19 13:39:17 -0800546 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700547 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800548
549 if (transport_cmd_check_stop_to_fabric(cmd))
550 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700551 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400552 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200553 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700554 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800555}
556
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400557static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
558 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800559{
560 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000561 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800562 unsigned long flags;
563
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800564 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700565 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800566 cmd->t_state = t_state;
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500567 cmd->transport_state |= CMD_T_ACTIVE;
Andy Grovera1d8b492011-05-02 17:12:10 -0700568 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800569 }
570
571 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700572
573 /* If the cmd is already on the list, remove it before we add it */
574 if (!list_empty(&cmd->se_queue_node))
575 list_del(&cmd->se_queue_node);
576 else
577 atomic_inc(&qobj->queue_cnt);
578
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400579 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700580 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400581 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700582 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500583 cmd->transport_state |= CMD_T_QUEUED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
585
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800586 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800587}
588
Andy Grover5951146d2011-07-19 10:26:37 +0000589static struct se_cmd *
590transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800591{
Andy Grover5951146d2011-07-19 10:26:37 +0000592 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800593 unsigned long flags;
594
595 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
596 if (list_empty(&qobj->qobj_list)) {
597 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
598 return NULL;
599 }
Andy Grover5951146d2011-07-19 10:26:37 +0000600 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800601
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500602 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700603 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800604 atomic_dec(&qobj->queue_cnt);
605 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
606
Andy Grover5951146d2011-07-19 10:26:37 +0000607 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800608}
609
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400610static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800611{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400612 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800613 unsigned long flags;
614
615 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500616 if (!(cmd->transport_state & CMD_T_QUEUED)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800617 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
618 return;
619 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500620 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700621 atomic_dec(&qobj->queue_cnt);
622 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800623 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800624}
625
626/*
627 * Completion function used by TCM subsystem plugins (such as FILEIO)
628 * for queueing up response from struct se_subsystem_api->do_task()
629 */
630void transport_complete_sync_cache(struct se_cmd *cmd, int good)
631{
Andy Grovera1d8b492011-05-02 17:12:10 -0700632 struct se_task *task = list_entry(cmd->t_task_list.next,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800633 struct se_task, t_list);
634
635 if (good) {
636 cmd->scsi_status = SAM_STAT_GOOD;
637 task->task_scsi_status = GOOD;
638 } else {
639 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700640 task->task_se_cmd->scsi_sense_reason =
641 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
642
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800643 }
644
645 transport_complete_task(task, good);
646}
647EXPORT_SYMBOL(transport_complete_sync_cache);
648
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400649static void target_complete_failure_work(struct work_struct *work)
650{
651 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
652
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700653 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400654}
655
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800656/* transport_complete_task():
657 *
658 * Called from interrupt and non interrupt context depending
659 * on the transport plugin.
660 */
661void transport_complete_task(struct se_task *task, int success)
662{
Andy Grovere3d6f902011-07-19 08:55:10 +0000663 struct se_cmd *cmd = task->task_se_cmd;
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400664 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800665 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800666
Andy Grovera1d8b492011-05-02 17:12:10 -0700667 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400668 task->task_flags &= ~TF_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800669
670 /*
671 * See if any sense data exists, if so set the TASK_SENSE flag.
672 * Also check for any other post completion work that needs to be
673 * done by the plugins.
674 */
675 if (dev && dev->transport->transport_complete) {
676 if (dev->transport->transport_complete(task) != 0) {
677 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
Christoph Hellwigef804a82011-11-23 06:53:58 -0500678 task->task_flags |= TF_HAS_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800679 success = 1;
680 }
681 }
682
683 /*
684 * See if we are waiting for outstanding struct se_task
685 * to complete for an exception condition
686 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400687 if (task->task_flags & TF_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700688 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800689 complete(&task->task_stop_comp);
690 return;
691 }
Christoph Hellwig22350072011-11-02 05:06:35 -0700692
693 if (!success)
Christoph Hellwig7d680f32011-12-21 14:13:47 -0500694 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig22350072011-11-02 05:06:35 -0700695
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800696 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800697 * Decrement the outstanding t_task_cdbs_left count. The last
698 * struct se_task from struct se_cmd will complete itself into the
699 * device queue depending upon int success.
700 */
Andy Grover6708bb22011-06-08 10:36:43 -0700701 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700702 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800703 return;
704 }
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800705 /*
706 * Check for case where an explict ABORT_TASK has been received
707 * and transport_wait_for_tasks() will be waiting for completion..
708 */
709 if (cmd->transport_state & CMD_T_ABORTED &&
710 cmd->transport_state & CMD_T_STOP) {
711 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
712 complete(&cmd->t_transport_stop_comp);
713 return;
714 } else if (cmd->transport_state & CMD_T_FAILED) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500715 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400716 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800717 } else {
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400718 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800719 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400720
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400721 cmd->t_state = TRANSPORT_COMPLETE;
Nicholas Bellinger3d289342012-02-13 02:38:14 -0800722 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
Andy Grovera1d8b492011-05-02 17:12:10 -0700723 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800724
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400725 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800726}
727EXPORT_SYMBOL(transport_complete_task);
728
729/*
730 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
731 * struct se_task list are ready to be added to the active execution list
732 * struct se_device
733
734 * Called with se_dev_t->execute_task_lock called.
735 */
736static inline int transport_add_task_check_sam_attr(
737 struct se_task *task,
738 struct se_task *task_prev,
739 struct se_device *dev)
740{
741 /*
742 * No SAM Task attribute emulation enabled, add to tail of
743 * execution queue
744 */
745 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
746 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
747 return 0;
748 }
749 /*
750 * HEAD_OF_QUEUE attribute for received CDB, which means
751 * the first task that is associated with a struct se_cmd goes to
752 * head of the struct se_device->execute_task_list, and task_prev
753 * after that for each subsequent task
754 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -0700755 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800756 list_add(&task->t_execute_list,
757 (task_prev != NULL) ?
758 &task_prev->t_execute_list :
759 &dev->execute_task_list);
760
Andy Grover6708bb22011-06-08 10:36:43 -0700761 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800762 " in execution queue\n",
Andy Grover6708bb22011-06-08 10:36:43 -0700763 task->task_se_cmd->t_task_cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800764 return 1;
765 }
766 /*
767 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
768 * transitioned from Dermant -> Active state, and are added to the end
769 * of the struct se_device->execute_task_list
770 */
771 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
772 return 0;
773}
774
775/* __transport_add_task_to_execute_queue():
776 *
777 * Called with se_dev_t->execute_task_lock called.
778 */
779static void __transport_add_task_to_execute_queue(
780 struct se_task *task,
781 struct se_task *task_prev,
782 struct se_device *dev)
783{
784 int head_of_queue;
785
786 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
787 atomic_inc(&dev->execute_tasks);
788
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500789 if (task->t_state_active)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800790 return;
791 /*
792 * Determine if this task needs to go to HEAD_OF_QUEUE for the
793 * state list as well. Running with SAM Task Attribute emulation
794 * will always return head_of_queue == 0 here
795 */
796 if (head_of_queue)
797 list_add(&task->t_state_list, (task_prev) ?
798 &task_prev->t_state_list :
799 &dev->state_task_list);
800 else
801 list_add_tail(&task->t_state_list, &dev->state_task_list);
802
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500803 task->t_state_active = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800804
Andy Grover6708bb22011-06-08 10:36:43 -0700805 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000806 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800807 task, dev);
808}
809
810static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
811{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400812 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800813 struct se_task *task;
814 unsigned long flags;
815
Andy Grovera1d8b492011-05-02 17:12:10 -0700816 spin_lock_irqsave(&cmd->t_state_lock, flags);
817 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800818 spin_lock(&dev->execute_task_lock);
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500819 if (!task->t_state_active) {
820 list_add_tail(&task->t_state_list,
821 &dev->state_task_list);
822 task->t_state_active = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800823
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500824 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
825 task->task_se_cmd->se_tfo->get_task_tag(
826 task->task_se_cmd), task, dev);
827 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800828 spin_unlock(&dev->execute_task_lock);
829 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700830 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831}
832
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800833static void __transport_add_tasks_from_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800834{
Andy Grover5951146d2011-07-19 10:26:37 +0000835 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800836 struct se_task *task, *task_prev = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800837
Andy Grovera1d8b492011-05-02 17:12:10 -0700838 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -0400839 if (!list_empty(&task->t_execute_list))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800840 continue;
841 /*
842 * __transport_add_task_to_execute_queue() handles the
843 * SAM Task Attribute emulation if enabled
844 */
845 __transport_add_task_to_execute_queue(task, task_prev, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800846 task_prev = task;
847 }
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800848}
849
850static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
851{
852 unsigned long flags;
853 struct se_device *dev = cmd->se_dev;
854
855 spin_lock_irqsave(&dev->execute_task_lock, flags);
856 __transport_add_tasks_from_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800857 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800858}
859
Christoph Hellwig04629b72011-10-12 11:07:04 -0400860void __transport_remove_task_from_execute_queue(struct se_task *task,
861 struct se_device *dev)
862{
863 list_del_init(&task->t_execute_list);
864 atomic_dec(&dev->execute_tasks);
865}
866
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500867static void transport_remove_task_from_execute_queue(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800868 struct se_task *task,
869 struct se_device *dev)
870{
871 unsigned long flags;
872
Christoph Hellwig04629b72011-10-12 11:07:04 -0400873 if (WARN_ON(list_empty(&task->t_execute_list)))
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700874 return;
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700875
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800876 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig04629b72011-10-12 11:07:04 -0400877 __transport_remove_task_from_execute_queue(task, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800878 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
879}
880
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700881/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700882 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700883 */
884
885static void target_qf_do_work(struct work_struct *work)
886{
887 struct se_device *dev = container_of(work, struct se_device,
888 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700889 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700890 struct se_cmd *cmd, *cmd_tmp;
891
892 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700893 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
894 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700895
Roland Dreierbcac3642011-08-27 21:33:16 -0700896 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700897 list_del(&cmd->se_qf_node);
898 atomic_dec(&dev->dev_qf_count);
899 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700900
Andy Grover6708bb22011-06-08 10:36:43 -0700901 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700902 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400903 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700904 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
905 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400906
907 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700908 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700909}
910
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800911unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
912{
913 switch (cmd->data_direction) {
914 case DMA_NONE:
915 return "NONE";
916 case DMA_FROM_DEVICE:
917 return "READ";
918 case DMA_TO_DEVICE:
919 return "WRITE";
920 case DMA_BIDIRECTIONAL:
921 return "BIDI";
922 default:
923 break;
924 }
925
926 return "UNKNOWN";
927}
928
929void transport_dump_dev_state(
930 struct se_device *dev,
931 char *b,
932 int *bl)
933{
934 *bl += sprintf(b + *bl, "Status: ");
935 switch (dev->dev_status) {
936 case TRANSPORT_DEVICE_ACTIVATED:
937 *bl += sprintf(b + *bl, "ACTIVATED");
938 break;
939 case TRANSPORT_DEVICE_DEACTIVATED:
940 *bl += sprintf(b + *bl, "DEACTIVATED");
941 break;
942 case TRANSPORT_DEVICE_SHUTDOWN:
943 *bl += sprintf(b + *bl, "SHUTDOWN");
944 break;
945 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
946 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
947 *bl += sprintf(b + *bl, "OFFLINE");
948 break;
949 default:
950 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
951 break;
952 }
953
Nicholas Bellinger65586d52011-11-30 01:25:21 -0800954 *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
955 atomic_read(&dev->execute_tasks), dev->queue_depth);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800956 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000957 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 -0800958 *bl += sprintf(b + *bl, " ");
959}
960
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800961void transport_dump_vpd_proto_id(
962 struct t10_vpd *vpd,
963 unsigned char *p_buf,
964 int p_buf_len)
965{
966 unsigned char buf[VPD_TMP_BUF_SIZE];
967 int len;
968
969 memset(buf, 0, VPD_TMP_BUF_SIZE);
970 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
971
972 switch (vpd->protocol_identifier) {
973 case 0x00:
974 sprintf(buf+len, "Fibre Channel\n");
975 break;
976 case 0x10:
977 sprintf(buf+len, "Parallel SCSI\n");
978 break;
979 case 0x20:
980 sprintf(buf+len, "SSA\n");
981 break;
982 case 0x30:
983 sprintf(buf+len, "IEEE 1394\n");
984 break;
985 case 0x40:
986 sprintf(buf+len, "SCSI Remote Direct Memory Access"
987 " Protocol\n");
988 break;
989 case 0x50:
990 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
991 break;
992 case 0x60:
993 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
994 break;
995 case 0x70:
996 sprintf(buf+len, "Automation/Drive Interface Transport"
997 " Protocol\n");
998 break;
999 case 0x80:
1000 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1001 break;
1002 default:
1003 sprintf(buf+len, "Unknown 0x%02x\n",
1004 vpd->protocol_identifier);
1005 break;
1006 }
1007
1008 if (p_buf)
1009 strncpy(p_buf, buf, p_buf_len);
1010 else
Andy Grover6708bb22011-06-08 10:36:43 -07001011 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001012}
1013
1014void
1015transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1016{
1017 /*
1018 * Check if the Protocol Identifier Valid (PIV) bit is set..
1019 *
1020 * from spc3r23.pdf section 7.5.1
1021 */
1022 if (page_83[1] & 0x80) {
1023 vpd->protocol_identifier = (page_83[0] & 0xf0);
1024 vpd->protocol_identifier_set = 1;
1025 transport_dump_vpd_proto_id(vpd, NULL, 0);
1026 }
1027}
1028EXPORT_SYMBOL(transport_set_vpd_proto_id);
1029
1030int transport_dump_vpd_assoc(
1031 struct t10_vpd *vpd,
1032 unsigned char *p_buf,
1033 int p_buf_len)
1034{
1035 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001036 int ret = 0;
1037 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001038
1039 memset(buf, 0, VPD_TMP_BUF_SIZE);
1040 len = sprintf(buf, "T10 VPD Identifier Association: ");
1041
1042 switch (vpd->association) {
1043 case 0x00:
1044 sprintf(buf+len, "addressed logical unit\n");
1045 break;
1046 case 0x10:
1047 sprintf(buf+len, "target port\n");
1048 break;
1049 case 0x20:
1050 sprintf(buf+len, "SCSI target device\n");
1051 break;
1052 default:
1053 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +00001054 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001055 break;
1056 }
1057
1058 if (p_buf)
1059 strncpy(p_buf, buf, p_buf_len);
1060 else
Andy Grover6708bb22011-06-08 10:36:43 -07001061 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001062
1063 return ret;
1064}
1065
1066int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1067{
1068 /*
1069 * The VPD identification association..
1070 *
1071 * from spc3r23.pdf Section 7.6.3.1 Table 297
1072 */
1073 vpd->association = (page_83[1] & 0x30);
1074 return transport_dump_vpd_assoc(vpd, NULL, 0);
1075}
1076EXPORT_SYMBOL(transport_set_vpd_assoc);
1077
1078int transport_dump_vpd_ident_type(
1079 struct t10_vpd *vpd,
1080 unsigned char *p_buf,
1081 int p_buf_len)
1082{
1083 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001084 int ret = 0;
1085 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001086
1087 memset(buf, 0, VPD_TMP_BUF_SIZE);
1088 len = sprintf(buf, "T10 VPD Identifier Type: ");
1089
1090 switch (vpd->device_identifier_type) {
1091 case 0x00:
1092 sprintf(buf+len, "Vendor specific\n");
1093 break;
1094 case 0x01:
1095 sprintf(buf+len, "T10 Vendor ID based\n");
1096 break;
1097 case 0x02:
1098 sprintf(buf+len, "EUI-64 based\n");
1099 break;
1100 case 0x03:
1101 sprintf(buf+len, "NAA\n");
1102 break;
1103 case 0x04:
1104 sprintf(buf+len, "Relative target port identifier\n");
1105 break;
1106 case 0x08:
1107 sprintf(buf+len, "SCSI name string\n");
1108 break;
1109 default:
1110 sprintf(buf+len, "Unsupported: 0x%02x\n",
1111 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +00001112 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001113 break;
1114 }
1115
Andy Grovere3d6f902011-07-19 08:55:10 +00001116 if (p_buf) {
1117 if (p_buf_len < strlen(buf)+1)
1118 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001119 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +00001120 } else {
Andy Grover6708bb22011-06-08 10:36:43 -07001121 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +00001122 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001123
1124 return ret;
1125}
1126
1127int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1128{
1129 /*
1130 * The VPD identifier type..
1131 *
1132 * from spc3r23.pdf Section 7.6.3.1 Table 298
1133 */
1134 vpd->device_identifier_type = (page_83[1] & 0x0f);
1135 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1136}
1137EXPORT_SYMBOL(transport_set_vpd_ident_type);
1138
1139int transport_dump_vpd_ident(
1140 struct t10_vpd *vpd,
1141 unsigned char *p_buf,
1142 int p_buf_len)
1143{
1144 unsigned char buf[VPD_TMP_BUF_SIZE];
1145 int ret = 0;
1146
1147 memset(buf, 0, VPD_TMP_BUF_SIZE);
1148
1149 switch (vpd->device_identifier_code_set) {
1150 case 0x01: /* Binary */
1151 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1152 &vpd->device_identifier[0]);
1153 break;
1154 case 0x02: /* ASCII */
1155 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1156 &vpd->device_identifier[0]);
1157 break;
1158 case 0x03: /* UTF-8 */
1159 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1160 &vpd->device_identifier[0]);
1161 break;
1162 default:
1163 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1164 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +00001165 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001166 break;
1167 }
1168
1169 if (p_buf)
1170 strncpy(p_buf, buf, p_buf_len);
1171 else
Andy Grover6708bb22011-06-08 10:36:43 -07001172 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001173
1174 return ret;
1175}
1176
1177int
1178transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1179{
1180 static const char hex_str[] = "0123456789abcdef";
1181 int j = 0, i = 4; /* offset to start of the identifer */
1182
1183 /*
1184 * The VPD Code Set (encoding)
1185 *
1186 * from spc3r23.pdf Section 7.6.3.1 Table 296
1187 */
1188 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1189 switch (vpd->device_identifier_code_set) {
1190 case 0x01: /* Binary */
1191 vpd->device_identifier[j++] =
1192 hex_str[vpd->device_identifier_type];
1193 while (i < (4 + page_83[3])) {
1194 vpd->device_identifier[j++] =
1195 hex_str[(page_83[i] & 0xf0) >> 4];
1196 vpd->device_identifier[j++] =
1197 hex_str[page_83[i] & 0x0f];
1198 i++;
1199 }
1200 break;
1201 case 0x02: /* ASCII */
1202 case 0x03: /* UTF-8 */
1203 while (i < (4 + page_83[3]))
1204 vpd->device_identifier[j++] = page_83[i++];
1205 break;
1206 default:
1207 break;
1208 }
1209
1210 return transport_dump_vpd_ident(vpd, NULL, 0);
1211}
1212EXPORT_SYMBOL(transport_set_vpd_ident);
1213
1214static void core_setup_task_attr_emulation(struct se_device *dev)
1215{
1216 /*
1217 * If this device is from Target_Core_Mod/pSCSI, disable the
1218 * SAM Task Attribute emulation.
1219 *
1220 * This is currently not available in upsream Linux/SCSI Target
1221 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1222 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001223 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001224 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1225 return;
1226 }
1227
1228 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001229 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001230 " device\n", dev->transport->name,
1231 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001232}
1233
1234static void scsi_dump_inquiry(struct se_device *dev)
1235{
Andy Grovere3d6f902011-07-19 08:55:10 +00001236 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001237 char buf[17];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001238 int i, device_type;
1239 /*
1240 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1241 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001242 for (i = 0; i < 8; i++)
1243 if (wwn->vendor[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001244 buf[i] = wwn->vendor[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001245 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001246 buf[i] = ' ';
1247 buf[i] = '\0';
1248 pr_debug(" Vendor: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001249
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001250 for (i = 0; i < 16; i++)
1251 if (wwn->model[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001252 buf[i] = wwn->model[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001253 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001254 buf[i] = ' ';
1255 buf[i] = '\0';
1256 pr_debug(" Model: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001257
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001258 for (i = 0; i < 4; i++)
1259 if (wwn->revision[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001260 buf[i] = wwn->revision[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001261 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001262 buf[i] = ' ';
1263 buf[i] = '\0';
1264 pr_debug(" Revision: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001265
Andy Grovere3d6f902011-07-19 08:55:10 +00001266 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001267 pr_debug(" Type: %s ", scsi_device_type(device_type));
1268 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001269 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001270}
1271
1272struct se_device *transport_add_device_to_core_hba(
1273 struct se_hba *hba,
1274 struct se_subsystem_api *transport,
1275 struct se_subsystem_dev *se_dev,
1276 u32 device_flags,
1277 void *transport_dev,
1278 struct se_dev_limits *dev_limits,
1279 const char *inquiry_prod,
1280 const char *inquiry_rev)
1281{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001282 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001283 struct se_device *dev;
1284
1285 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001286 if (!dev) {
1287 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001288 return NULL;
1289 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001290
Andy Grovere3d6f902011-07-19 08:55:10 +00001291 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001292 dev->dev_flags = device_flags;
1293 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001294 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001295 dev->se_hba = hba;
1296 dev->se_sub_dev = se_dev;
1297 dev->transport = transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001298 INIT_LIST_HEAD(&dev->dev_list);
1299 INIT_LIST_HEAD(&dev->dev_sep_list);
1300 INIT_LIST_HEAD(&dev->dev_tmr_list);
1301 INIT_LIST_HEAD(&dev->execute_task_list);
1302 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001303 INIT_LIST_HEAD(&dev->state_task_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001304 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001305 spin_lock_init(&dev->execute_task_lock);
1306 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001307 spin_lock_init(&dev->dev_reservation_lock);
1308 spin_lock_init(&dev->dev_status_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001309 spin_lock_init(&dev->se_port_lock);
1310 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001311 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312 atomic_set(&dev->dev_ordered_id, 0);
1313
1314 se_dev_set_default_attribs(dev, dev_limits);
1315
1316 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1317 dev->creation_time = get_jiffies_64();
1318 spin_lock_init(&dev->stats_lock);
1319
1320 spin_lock(&hba->device_lock);
1321 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1322 hba->dev_count++;
1323 spin_unlock(&hba->device_lock);
1324 /*
1325 * Setup the SAM Task Attribute emulation for struct se_device
1326 */
1327 core_setup_task_attr_emulation(dev);
1328 /*
1329 * Force PR and ALUA passthrough emulation with internal object use.
1330 */
1331 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1332 /*
1333 * Setup the Reservations infrastructure for struct se_device
1334 */
1335 core_setup_reservations(dev, force_pt);
1336 /*
1337 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1338 */
1339 if (core_setup_alua(dev, force_pt) < 0)
1340 goto out;
1341
1342 /*
1343 * Startup the struct se_device processing thread
1344 */
1345 dev->process_thread = kthread_run(transport_processing_thread, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001346 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001348 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001349 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001350 goto out;
1351 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001352 /*
1353 * Setup work_queue for QUEUE_FULL
1354 */
1355 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001356 /*
1357 * Preload the initial INQUIRY const values if we are doing
1358 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1359 * passthrough because this is being provided by the backend LLD.
1360 * This is required so that transport_get_inquiry() copies these
1361 * originals once back into DEV_T10_WWN(dev) for the virtual device
1362 * setup.
1363 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001364 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001365 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001366 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001367 " INQUIRY consts\n");
1368 goto out;
1369 }
1370
Andy Grovere3d6f902011-07-19 08:55:10 +00001371 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1372 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1373 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001374 }
1375 scsi_dump_inquiry(dev);
1376
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001377 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001378out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001379 kthread_stop(dev->process_thread);
1380
1381 spin_lock(&hba->device_lock);
1382 list_del(&dev->dev_list);
1383 hba->dev_count--;
1384 spin_unlock(&hba->device_lock);
1385
1386 se_release_vpd_for_dev(dev);
1387
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001388 kfree(dev);
1389
1390 return NULL;
1391}
1392EXPORT_SYMBOL(transport_add_device_to_core_hba);
1393
1394/* transport_generic_prepare_cdb():
1395 *
1396 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1397 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1398 * The point of this is since we are mapping iSCSI LUNs to
1399 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1400 * devices and HBAs for a loop.
1401 */
1402static inline void transport_generic_prepare_cdb(
1403 unsigned char *cdb)
1404{
1405 switch (cdb[0]) {
1406 case READ_10: /* SBC - RDProtect */
1407 case READ_12: /* SBC - RDProtect */
1408 case READ_16: /* SBC - RDProtect */
1409 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1410 case VERIFY: /* SBC - VRProtect */
1411 case VERIFY_16: /* SBC - VRProtect */
1412 case WRITE_VERIFY: /* SBC - VRProtect */
1413 case WRITE_VERIFY_12: /* SBC - VRProtect */
1414 break;
1415 default:
1416 cdb[1] &= 0x1f; /* clear logical unit number */
1417 break;
1418 }
1419}
1420
1421static struct se_task *
1422transport_generic_get_task(struct se_cmd *cmd,
1423 enum dma_data_direction data_direction)
1424{
1425 struct se_task *task;
Andy Grover5951146d2011-07-19 10:26:37 +00001426 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001427
Andy Grover6708bb22011-06-08 10:36:43 -07001428 task = dev->transport->alloc_task(cmd->t_task_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001429 if (!task) {
Andy Grover6708bb22011-06-08 10:36:43 -07001430 pr_err("Unable to allocate struct se_task\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001431 return NULL;
1432 }
1433
1434 INIT_LIST_HEAD(&task->t_list);
1435 INIT_LIST_HEAD(&task->t_execute_list);
1436 INIT_LIST_HEAD(&task->t_state_list);
1437 init_completion(&task->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001438 task->task_se_cmd = cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001439 task->task_data_direction = data_direction;
1440
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001441 return task;
1442}
1443
1444static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1445
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001446/*
1447 * Used by fabric modules containing a local struct se_cmd within their
1448 * fabric dependent per I/O descriptor.
1449 */
1450void transport_init_se_cmd(
1451 struct se_cmd *cmd,
1452 struct target_core_fabric_ops *tfo,
1453 struct se_session *se_sess,
1454 u32 data_length,
1455 int data_direction,
1456 int task_attr,
1457 unsigned char *sense_buffer)
1458{
Andy Grover5951146d2011-07-19 10:26:37 +00001459 INIT_LIST_HEAD(&cmd->se_lun_node);
1460 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001461 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001462 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001463 INIT_LIST_HEAD(&cmd->se_cmd_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001464 INIT_LIST_HEAD(&cmd->t_task_list);
1465 init_completion(&cmd->transport_lun_fe_stop_comp);
1466 init_completion(&cmd->transport_lun_stop_comp);
1467 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001468 init_completion(&cmd->cmd_wait_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001469 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001470 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001471
1472 cmd->se_tfo = tfo;
1473 cmd->se_sess = se_sess;
1474 cmd->data_length = data_length;
1475 cmd->data_direction = data_direction;
1476 cmd->sam_task_attr = task_attr;
1477 cmd->sense_buffer = sense_buffer;
1478}
1479EXPORT_SYMBOL(transport_init_se_cmd);
1480
1481static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1482{
1483 /*
1484 * Check if SAM Task Attribute emulation is enabled for this
1485 * struct se_device storage object
1486 */
Andy Grover5951146d2011-07-19 10:26:37 +00001487 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001488 return 0;
1489
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001490 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001491 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001492 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001493 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001494 }
1495 /*
1496 * Used to determine when ORDERED commands should go from
1497 * Dormant to Active status.
1498 */
Andy Grover5951146d2011-07-19 10:26:37 +00001499 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001500 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001501 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001502 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001503 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001504 return 0;
1505}
1506
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001507/* transport_generic_allocate_tasks():
1508 *
1509 * Called from fabric RX Thread.
1510 */
1511int transport_generic_allocate_tasks(
1512 struct se_cmd *cmd,
1513 unsigned char *cdb)
1514{
1515 int ret;
1516
1517 transport_generic_prepare_cdb(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001518 /*
1519 * Ensure that the received CDB is less than the max (252 + 8) bytes
1520 * for VARIABLE_LENGTH_CMD
1521 */
1522 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001523 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001524 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1525 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001526 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1527 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001528 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001529 }
1530 /*
1531 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1532 * allocate the additional extended CDB buffer now.. Otherwise
1533 * setup the pointer from __t_task_cdb to t_task_cdb.
1534 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001535 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1536 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001537 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001538 if (!cmd->t_task_cdb) {
1539 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001540 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001541 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001542 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001543 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1544 cmd->scsi_sense_reason =
1545 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001546 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001547 }
1548 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001549 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001550 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001551 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001552 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001553 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001554 /*
1555 * Setup the received CDB based on SCSI defined opcodes and
1556 * perform unit attention, persistent reservations and ALUA
Andy Grovera1d8b492011-05-02 17:12:10 -07001557 * checks for virtual device backends. The cmd->t_task_cdb
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001558 * pointer is expected to be setup before we reach this point.
1559 */
1560 ret = transport_generic_cmd_sequencer(cmd, cdb);
1561 if (ret < 0)
1562 return ret;
1563 /*
1564 * Check for SAM Task Attribute Emulation
1565 */
1566 if (transport_check_alloc_task_attr(cmd) < 0) {
1567 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1568 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001569 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001570 }
1571 spin_lock(&cmd->se_lun->lun_sep_lock);
1572 if (cmd->se_lun->lun_sep)
1573 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1574 spin_unlock(&cmd->se_lun->lun_sep_lock);
1575 return 0;
1576}
1577EXPORT_SYMBOL(transport_generic_allocate_tasks);
1578
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001580 * Used by fabric module frontends to queue tasks directly.
1581 * Many only be used from process context only
1582 */
1583int transport_handle_cdb_direct(
1584 struct se_cmd *cmd)
1585{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001586 int ret;
1587
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001588 if (!cmd->se_lun) {
1589 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001590 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001591 return -EINVAL;
1592 }
1593 if (in_interrupt()) {
1594 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001595 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001596 " from interrupt context\n");
1597 return -EINVAL;
1598 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001599 /*
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001600 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001601 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1602 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001603 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001604 *
1605 * Also, we don't take cmd->t_state_lock here as we only expect
1606 * this to be called for initial descriptor submission.
1607 */
1608 cmd->t_state = TRANSPORT_NEW_CMD;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001609 cmd->transport_state |= CMD_T_ACTIVE;
1610
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001611 /*
1612 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1613 * so follow TRANSPORT_NEW_CMD processing thread context usage
1614 * and call transport_generic_request_failure() if necessary..
1615 */
1616 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001617 if (ret < 0)
1618 transport_generic_request_failure(cmd);
1619
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001620 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001621}
1622EXPORT_SYMBOL(transport_handle_cdb_direct);
1623
Nicholas Bellingera6360782011-11-18 20:36:22 -08001624/**
1625 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1626 *
1627 * @se_cmd: command descriptor to submit
1628 * @se_sess: associated se_sess for endpoint
1629 * @cdb: pointer to SCSI CDB
1630 * @sense: pointer to SCSI sense buffer
1631 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1632 * @data_length: fabric expected data transfer length
1633 * @task_addr: SAM task attribute
1634 * @data_dir: DMA data direction
1635 * @flags: flags for command submission from target_sc_flags_tables
1636 *
1637 * This may only be called from process context, and also currently
1638 * assumes internal allocation of fabric payload buffer by target-core.
1639 **/
Andy Grover1edcdb42012-01-19 13:39:23 -08001640void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001641 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1642 u32 data_length, int task_attr, int data_dir, int flags)
1643{
1644 struct se_portal_group *se_tpg;
1645 int rc;
1646
1647 se_tpg = se_sess->se_tpg;
1648 BUG_ON(!se_tpg);
1649 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1650 BUG_ON(in_interrupt());
1651 /*
1652 * Initialize se_cmd for target operation. From this point
1653 * exceptions are handled by sending exception status via
1654 * target_core_fabric_ops->queue_status() callback
1655 */
1656 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1657 data_length, data_dir, task_attr, sense);
1658 /*
1659 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1660 * se_sess->sess_cmd_list. A second kref_get here is necessary
1661 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1662 * kref_put() to happen during fabric packet acknowledgement.
1663 */
1664 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1665 /*
1666 * Signal bidirectional data payloads to target-core
1667 */
1668 if (flags & TARGET_SCF_BIDI_OP)
1669 se_cmd->se_cmd_flags |= SCF_BIDI;
1670 /*
1671 * Locate se_lun pointer and attach it to struct se_cmd
1672 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001673 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1674 transport_send_check_condition_and_sense(se_cmd,
1675 se_cmd->scsi_sense_reason, 0);
1676 target_put_sess_cmd(se_sess, se_cmd);
1677 return;
1678 }
Nicholas Bellingera6360782011-11-18 20:36:22 -08001679 /*
1680 * Sanitize CDBs via transport_generic_cmd_sequencer() and
1681 * allocate the necessary tasks to complete the received CDB+data
1682 */
1683 rc = transport_generic_allocate_tasks(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001684 if (rc != 0) {
1685 transport_generic_request_failure(se_cmd);
1686 return;
1687 }
Nicholas Bellingera6360782011-11-18 20:36:22 -08001688 /*
1689 * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
1690 * for immediate execution of READs, otherwise wait for
1691 * transport_generic_handle_data() to be called for WRITEs
1692 * when fabric has filled the incoming buffer.
1693 */
1694 transport_handle_cdb_direct(se_cmd);
Andy Grover1edcdb42012-01-19 13:39:23 -08001695 return;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001696}
1697EXPORT_SYMBOL(target_submit_cmd);
1698
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001699static void target_complete_tmr_failure(struct work_struct *work)
1700{
1701 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1702
1703 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1704 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1705 transport_generic_free_cmd(se_cmd, 0);
1706}
1707
Andy Groverea98d7f2012-01-19 13:39:21 -08001708/**
1709 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1710 * for TMR CDBs
1711 *
1712 * @se_cmd: command descriptor to submit
1713 * @se_sess: associated se_sess for endpoint
1714 * @sense: pointer to SCSI sense buffer
1715 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1716 * @fabric_context: fabric context for TMR req
1717 * @tm_type: Type of TM request
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001718 * @gfp: gfp type for caller
1719 * @tag: referenced task tag for TMR_ABORT_TASK
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001720 * @flags: submit cmd flags
Andy Groverea98d7f2012-01-19 13:39:21 -08001721 *
1722 * Callable from all contexts.
1723 **/
1724
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001725int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
Andy Groverea98d7f2012-01-19 13:39:21 -08001726 unsigned char *sense, u32 unpacked_lun,
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001727 void *fabric_tmr_ptr, unsigned char tm_type,
1728 gfp_t gfp, unsigned int tag, int flags)
Andy Groverea98d7f2012-01-19 13:39:21 -08001729{
1730 struct se_portal_group *se_tpg;
1731 int ret;
1732
1733 se_tpg = se_sess->se_tpg;
1734 BUG_ON(!se_tpg);
1735
1736 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1737 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001738 /*
1739 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1740 * allocation failure.
1741 */
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001742 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001743 if (ret < 0)
1744 return -ENOMEM;
Andy Groverea98d7f2012-01-19 13:39:21 -08001745
Nicholas Bellingerc0974f82012-02-25 05:10:04 -08001746 if (tm_type == TMR_ABORT_TASK)
1747 se_cmd->se_tmr_req->ref_task_tag = tag;
1748
Andy Groverea98d7f2012-01-19 13:39:21 -08001749 /* See target_submit_cmd for commentary */
1750 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1751
Andy Groverea98d7f2012-01-19 13:39:21 -08001752 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1753 if (ret) {
Nicholas Bellinger9f0d05c2012-02-25 05:02:48 -08001754 /*
1755 * For callback during failure handling, push this work off
1756 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1757 */
1758 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1759 schedule_work(&se_cmd->work);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001760 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001761 }
1762 transport_generic_handle_tmr(se_cmd);
Nicholas Bellingerc7042ca2012-02-25 01:40:24 -08001763 return 0;
Andy Groverea98d7f2012-01-19 13:39:21 -08001764}
1765EXPORT_SYMBOL(target_submit_tmr);
1766
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001767/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001768 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1769 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1770 * complete setup in TCM process context w/ TFO->new_cmd_map().
1771 */
1772int transport_generic_handle_cdb_map(
1773 struct se_cmd *cmd)
1774{
Andy Grovere3d6f902011-07-19 08:55:10 +00001775 if (!cmd->se_lun) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001776 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001777 pr_err("cmd->se_lun is NULL\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001778 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001779 }
1780
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001781 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001782 return 0;
1783}
1784EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1785
1786/* transport_generic_handle_data():
1787 *
1788 *
1789 */
1790int transport_generic_handle_data(
1791 struct se_cmd *cmd)
1792{
1793 /*
1794 * For the software fabric case, then we assume the nexus is being
1795 * failed/shutdown when signals are pending from the kthread context
1796 * caller, so we return a failure. For the HW target mode case running
1797 * in interrupt code, the signal_pending() check is skipped.
1798 */
1799 if (!in_interrupt() && signal_pending(current))
Andy Grovere3d6f902011-07-19 08:55:10 +00001800 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001801 /*
1802 * If the received CDB has aleady been ABORTED by the generic
1803 * target engine, we now call transport_check_aborted_status()
1804 * to queue any delated TASK_ABORTED status for the received CDB to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001805 * fabric module as we are expecting no further incoming DATA OUT
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001806 * sequences at this point.
1807 */
1808 if (transport_check_aborted_status(cmd, 1) != 0)
1809 return 0;
1810
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001811 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001812 return 0;
1813}
1814EXPORT_SYMBOL(transport_generic_handle_data);
1815
1816/* transport_generic_handle_tmr():
1817 *
1818 *
1819 */
1820int transport_generic_handle_tmr(
1821 struct se_cmd *cmd)
1822{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001823 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001824 return 0;
1825}
1826EXPORT_SYMBOL(transport_generic_handle_tmr);
1827
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001828/*
1829 * If the task is active, request it to be stopped and sleep until it
1830 * has completed.
1831 */
1832bool target_stop_task(struct se_task *task, unsigned long *flags)
1833{
1834 struct se_cmd *cmd = task->task_se_cmd;
1835 bool was_active = false;
1836
1837 if (task->task_flags & TF_ACTIVE) {
1838 task->task_flags |= TF_REQUEST_STOP;
1839 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1840
1841 pr_debug("Task %p waiting to complete\n", task);
1842 wait_for_completion(&task->task_stop_comp);
1843 pr_debug("Task %p stopped successfully\n", task);
1844
1845 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1846 atomic_dec(&cmd->t_task_cdbs_left);
1847 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1848 was_active = true;
1849 }
1850
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001851 return was_active;
1852}
1853
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001854static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1855{
1856 struct se_task *task, *task_tmp;
1857 unsigned long flags;
1858 int ret = 0;
1859
Andy Grover6708bb22011-06-08 10:36:43 -07001860 pr_debug("ITT[0x%08x] - Stopping tasks\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001861 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001862
1863 /*
1864 * No tasks remain in the execution queue
1865 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001866 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001867 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07001868 &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001869 pr_debug("Processing task %p\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001870 /*
1871 * If the struct se_task has not been sent and is not active,
1872 * remove the struct se_task from the execution queue.
1873 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04001874 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
Andy Grovera1d8b492011-05-02 17:12:10 -07001875 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001876 flags);
1877 transport_remove_task_from_execute_queue(task,
Christoph Hellwig42bf8292011-10-12 11:07:00 -04001878 cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001879
Christoph Hellwig04629b72011-10-12 11:07:04 -04001880 pr_debug("Task %p removed from execute queue\n", task);
Andy Grovera1d8b492011-05-02 17:12:10 -07001881 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001882 continue;
1883 }
1884
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001885 if (!target_stop_task(task, &flags)) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001886 pr_debug("Task %p - did nothing\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001887 ret++;
1888 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001889 }
Andy Grovera1d8b492011-05-02 17:12:10 -07001890 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001891
1892 return ret;
1893}
1894
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001895/*
1896 * Handle SAM-esque emulation for generic transport request failures.
1897 */
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001898void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001899{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001900 int ret = 0;
1901
Andy Grover6708bb22011-06-08 10:36:43 -07001902 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001903 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001904 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001905 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001906 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001907 cmd->t_state, cmd->scsi_sense_reason);
Andy Grover6708bb22011-06-08 10:36:43 -07001908 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001909 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001910 " CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1911 cmd->t_task_list_num,
Andy Grovera1d8b492011-05-02 17:12:10 -07001912 atomic_read(&cmd->t_task_cdbs_left),
1913 atomic_read(&cmd->t_task_cdbs_sent),
1914 atomic_read(&cmd->t_task_cdbs_ex_left),
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001915 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1916 (cmd->transport_state & CMD_T_STOP) != 0,
1917 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001918
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001919 /*
1920 * For SAM Task Attribute emulation for failed struct se_cmd
1921 */
1922 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1923 transport_complete_task_attr(cmd);
1924
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001925 switch (cmd->scsi_sense_reason) {
1926 case TCM_NON_EXISTENT_LUN:
1927 case TCM_UNSUPPORTED_SCSI_OPCODE:
1928 case TCM_INVALID_CDB_FIELD:
1929 case TCM_INVALID_PARAMETER_LIST:
1930 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1931 case TCM_UNKNOWN_MODE_PAGE:
1932 case TCM_WRITE_PROTECTED:
1933 case TCM_CHECK_CONDITION_ABORT_CMD:
1934 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1935 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001936 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001937 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001938 /*
1939 * No SENSE Data payload for this case, set SCSI Status
1940 * and queue the response to $FABRIC_MOD.
1941 *
1942 * Uses linux/include/scsi/scsi.h SAM status codes defs
1943 */
1944 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1945 /*
1946 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1947 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1948 * CONFLICT STATUS.
1949 *
1950 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1951 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001952 if (cmd->se_sess &&
1953 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1954 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001955 cmd->orig_fe_lun, 0x2C,
1956 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1957
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001958 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001959 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001960 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001961 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001962 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001963 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001964 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001965 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1966 break;
1967 }
Nicholas Bellinger16ab8e62011-08-08 19:03:38 -07001968 /*
1969 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1970 * make the call to transport_send_check_condition_and_sense()
1971 * directly. Otherwise expect the fabric to make the call to
1972 * transport_send_check_condition_and_sense() after handling
1973 * possible unsoliticied write data payloads.
1974 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001975 ret = transport_send_check_condition_and_sense(cmd,
1976 cmd->scsi_sense_reason, 0);
1977 if (ret == -EAGAIN || ret == -ENOMEM)
1978 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001979
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001980check_stop:
1981 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001982 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001983 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001984 return;
1985
1986queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001987 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1988 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001989}
Nicholas Bellinger2fbff122012-02-10 16:18:11 -08001990EXPORT_SYMBOL(transport_generic_request_failure);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001991
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001992static inline u32 transport_lba_21(unsigned char *cdb)
1993{
1994 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1995}
1996
1997static inline u32 transport_lba_32(unsigned char *cdb)
1998{
1999 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2000}
2001
2002static inline unsigned long long transport_lba_64(unsigned char *cdb)
2003{
2004 unsigned int __v1, __v2;
2005
2006 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2007 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2008
2009 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2010}
2011
2012/*
2013 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2014 */
2015static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
2016{
2017 unsigned int __v1, __v2;
2018
2019 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
2020 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
2021
2022 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2023}
2024
2025static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2026{
2027 unsigned long flags;
2028
Andy Grovera1d8b492011-05-02 17:12:10 -07002029 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002030 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
Andy Grovera1d8b492011-05-02 17:12:10 -07002031 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002032}
2033
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002034/*
2035 * Called from Fabric Module context from transport_execute_tasks()
2036 *
2037 * The return of this function determins if the tasks from struct se_cmd
2038 * get added to the execution queue in transport_execute_tasks(),
2039 * or are added to the delayed or ordered lists here.
2040 */
2041static inline int transport_execute_task_attr(struct se_cmd *cmd)
2042{
Andy Grover5951146d2011-07-19 10:26:37 +00002043 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002044 return 1;
2045 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002046 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002047 * to allow the passed struct se_cmd list of tasks to the front of the list.
2048 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002049 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07002050 pr_debug("Added HEAD_OF_QUEUE for CDB:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002051 " 0x%02x, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07002052 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002053 cmd->se_ordered_id);
2054 return 1;
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002055 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Andy Grover5951146d2011-07-19 10:26:37 +00002056 atomic_inc(&cmd->se_dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002057 smp_mb__after_atomic_inc();
2058
Andy Grover6708bb22011-06-08 10:36:43 -07002059 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002060 " list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002061 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002062 cmd->se_ordered_id);
2063 /*
2064 * Add ORDERED command to tail of execution queue if
2065 * no other older commands exist that need to be
2066 * completed first.
2067 */
Andy Grover6708bb22011-06-08 10:36:43 -07002068 if (!atomic_read(&cmd->se_dev->simple_cmds))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002069 return 1;
2070 } else {
2071 /*
2072 * For SIMPLE and UNTAGGED Task Attribute commands
2073 */
Andy Grover5951146d2011-07-19 10:26:37 +00002074 atomic_inc(&cmd->se_dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002075 smp_mb__after_atomic_inc();
2076 }
2077 /*
2078 * Otherwise if one or more outstanding ORDERED task attribute exist,
2079 * add the dormant task(s) built for the passed struct se_cmd to the
2080 * execution queue and become in Active state for this struct se_device.
2081 */
Andy Grover5951146d2011-07-19 10:26:37 +00002082 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002083 /*
2084 * Otherwise, add cmd w/ tasks to delayed cmd queue that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002085 * will be drained upon completion of HEAD_OF_QUEUE task.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002086 */
Andy Grover5951146d2011-07-19 10:26:37 +00002087 spin_lock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002088 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
Andy Grover5951146d2011-07-19 10:26:37 +00002089 list_add_tail(&cmd->se_delayed_node,
2090 &cmd->se_dev->delayed_cmd_list);
2091 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002092
Andy Grover6708bb22011-06-08 10:36:43 -07002093 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002094 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002095 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002096 cmd->se_ordered_id);
2097 /*
2098 * Return zero to let transport_execute_tasks() know
2099 * not to add the delayed tasks to the execution list.
2100 */
2101 return 0;
2102 }
2103 /*
2104 * Otherwise, no ORDERED task attributes exist..
2105 */
2106 return 1;
2107}
2108
2109/*
2110 * Called from fabric module context in transport_generic_new_cmd() and
2111 * transport_generic_process_write()
2112 */
2113static int transport_execute_tasks(struct se_cmd *cmd)
2114{
2115 int add_tasks;
Nicholas Bellinger40be67f2011-11-30 00:41:20 -08002116 struct se_device *se_dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002117 /*
2118 * Call transport_cmd_check_stop() to see if a fabric exception
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002119 * has occurred that prevents execution.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002120 */
Andy Grover6708bb22011-06-08 10:36:43 -07002121 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002122 /*
2123 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2124 * attribute for the tasks of the received struct se_cmd CDB
2125 */
2126 add_tasks = transport_execute_task_attr(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00002127 if (!add_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002128 goto execute_tasks;
2129 /*
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002130 * __transport_execute_tasks() -> __transport_add_tasks_from_cmd()
2131 * adds associated se_tasks while holding dev->execute_task_lock
2132 * before I/O dispath to avoid a double spinlock access.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002133 */
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002134 __transport_execute_tasks(se_dev, cmd);
2135 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002136 }
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002137
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002138execute_tasks:
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002139 __transport_execute_tasks(se_dev, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002140 return 0;
2141}
2142
2143/*
2144 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2145 * from struct se_device->execute_task_list and
2146 *
2147 * Called from transport_processing_thread()
2148 */
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002149static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002150{
2151 int error;
2152 struct se_cmd *cmd = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002153 struct se_task *task = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002154 unsigned long flags;
2155
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002156check_depth:
Andy Grovere3d6f902011-07-19 08:55:10 +00002157 spin_lock_irq(&dev->execute_task_lock);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002158 if (new_cmd != NULL)
2159 __transport_add_tasks_from_cmd(new_cmd);
2160
Andy Grovere3d6f902011-07-19 08:55:10 +00002161 if (list_empty(&dev->execute_task_list)) {
2162 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002163 return 0;
2164 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002165 task = list_first_entry(&dev->execute_task_list,
2166 struct se_task, t_execute_list);
Christoph Hellwig04629b72011-10-12 11:07:04 -04002167 __transport_remove_task_from_execute_queue(task, dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002168 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002169
Andy Grovere3d6f902011-07-19 08:55:10 +00002170 cmd = task->task_se_cmd;
Andy Grovera1d8b492011-05-02 17:12:10 -07002171 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04002172 task->task_flags |= (TF_ACTIVE | TF_SENT);
Andy Grovera1d8b492011-05-02 17:12:10 -07002173 atomic_inc(&cmd->t_task_cdbs_sent);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002174
Andy Grovera1d8b492011-05-02 17:12:10 -07002175 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2176 cmd->t_task_list_num)
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002177 cmd->transport_state |= CMD_T_SENT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002178
Andy Grovera1d8b492011-05-02 17:12:10 -07002179 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002180
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002181 if (cmd->execute_task)
2182 error = cmd->execute_task(task);
2183 else
2184 error = dev->transport->do_task(task);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002185 if (error != 0) {
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002186 spin_lock_irqsave(&cmd->t_state_lock, flags);
2187 task->task_flags &= ~TF_ACTIVE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002188 cmd->transport_state &= ~CMD_T_SENT;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002189 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05002190
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002191 transport_stop_tasks_for_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002192 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002193 }
2194
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002195 new_cmd = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002196 goto check_depth;
2197
2198 return 0;
2199}
2200
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002201static inline u32 transport_get_sectors_6(
2202 unsigned char *cdb,
2203 struct se_cmd *cmd,
2204 int *ret)
2205{
Andy Grover5951146d2011-07-19 10:26:37 +00002206 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002207
2208 /*
2209 * Assume TYPE_DISK for non struct se_device objects.
2210 * Use 8-bit sector value.
2211 */
2212 if (!dev)
2213 goto type_disk;
2214
2215 /*
2216 * Use 24-bit allocation length for TYPE_TAPE.
2217 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002218 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002219 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2220
2221 /*
2222 * Everything else assume TYPE_DISK Sector CDB location.
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002223 * Use 8-bit sector value. SBC-3 says:
2224 *
2225 * A TRANSFER LENGTH field set to zero specifies that 256
2226 * logical blocks shall be written. Any other value
2227 * specifies the number of logical blocks that shall be
2228 * written.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002229 */
2230type_disk:
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002231 return cdb[4] ? : 256;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002232}
2233
2234static inline u32 transport_get_sectors_10(
2235 unsigned char *cdb,
2236 struct se_cmd *cmd,
2237 int *ret)
2238{
Andy Grover5951146d2011-07-19 10:26:37 +00002239 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002240
2241 /*
2242 * Assume TYPE_DISK for non struct se_device objects.
2243 * Use 16-bit sector value.
2244 */
2245 if (!dev)
2246 goto type_disk;
2247
2248 /*
2249 * XXX_10 is not defined in SSC, throw an exception
2250 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002251 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2252 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002253 return 0;
2254 }
2255
2256 /*
2257 * Everything else assume TYPE_DISK Sector CDB location.
2258 * Use 16-bit sector value.
2259 */
2260type_disk:
2261 return (u32)(cdb[7] << 8) + cdb[8];
2262}
2263
2264static inline u32 transport_get_sectors_12(
2265 unsigned char *cdb,
2266 struct se_cmd *cmd,
2267 int *ret)
2268{
Andy Grover5951146d2011-07-19 10:26:37 +00002269 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002270
2271 /*
2272 * Assume TYPE_DISK for non struct se_device objects.
2273 * Use 32-bit sector value.
2274 */
2275 if (!dev)
2276 goto type_disk;
2277
2278 /*
2279 * XXX_12 is not defined in SSC, throw an exception
2280 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002281 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2282 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002283 return 0;
2284 }
2285
2286 /*
2287 * Everything else assume TYPE_DISK Sector CDB location.
2288 * Use 32-bit sector value.
2289 */
2290type_disk:
2291 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2292}
2293
2294static inline u32 transport_get_sectors_16(
2295 unsigned char *cdb,
2296 struct se_cmd *cmd,
2297 int *ret)
2298{
Andy Grover5951146d2011-07-19 10:26:37 +00002299 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002300
2301 /*
2302 * Assume TYPE_DISK for non struct se_device objects.
2303 * Use 32-bit sector value.
2304 */
2305 if (!dev)
2306 goto type_disk;
2307
2308 /*
2309 * Use 24-bit allocation length for TYPE_TAPE.
2310 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002311 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002312 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2313
2314type_disk:
2315 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2316 (cdb[12] << 8) + cdb[13];
2317}
2318
2319/*
2320 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2321 */
2322static inline u32 transport_get_sectors_32(
2323 unsigned char *cdb,
2324 struct se_cmd *cmd,
2325 int *ret)
2326{
2327 /*
2328 * Assume TYPE_DISK for non struct se_device objects.
2329 * Use 32-bit sector value.
2330 */
2331 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2332 (cdb[30] << 8) + cdb[31];
2333
2334}
2335
2336static inline u32 transport_get_size(
2337 u32 sectors,
2338 unsigned char *cdb,
2339 struct se_cmd *cmd)
2340{
Andy Grover5951146d2011-07-19 10:26:37 +00002341 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002342
Andy Grovere3d6f902011-07-19 08:55:10 +00002343 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002344 if (cdb[1] & 1) { /* sectors */
Andy Grovere3d6f902011-07-19 08:55:10 +00002345 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002346 } else /* bytes */
2347 return sectors;
2348 }
2349#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002350 pr_debug("Returning block_size: %u, sectors: %u == %u for"
Andy Grovere3d6f902011-07-19 08:55:10 +00002351 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2352 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2353 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002354#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00002355 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002356}
2357
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002358static void transport_xor_callback(struct se_cmd *cmd)
2359{
2360 unsigned char *buf, *addr;
Andy Groverec98f782011-07-20 19:28:46 +00002361 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002362 unsigned int offset;
2363 int i;
Andy Groverec98f782011-07-20 19:28:46 +00002364 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002365 /*
2366 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2367 *
2368 * 1) read the specified logical block(s);
2369 * 2) transfer logical blocks from the data-out buffer;
2370 * 3) XOR the logical blocks transferred from the data-out buffer with
2371 * the logical blocks read, storing the resulting XOR data in a buffer;
2372 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2373 * blocks transferred from the data-out buffer; and
2374 * 5) transfer the resulting XOR data to the data-in buffer.
2375 */
2376 buf = kmalloc(cmd->data_length, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002377 if (!buf) {
2378 pr_err("Unable to allocate xor_callback buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002379 return;
2380 }
2381 /*
Andy Groverec98f782011-07-20 19:28:46 +00002382 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002383 * into the locally allocated *buf
2384 */
Andy Groverec98f782011-07-20 19:28:46 +00002385 sg_copy_to_buffer(cmd->t_data_sg,
2386 cmd->t_data_nents,
2387 buf,
2388 cmd->data_length);
2389
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002390 /*
2391 * Now perform the XOR against the BIDI read memory located at
Andy Grovera1d8b492011-05-02 17:12:10 -07002392 * cmd->t_mem_bidi_list
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002393 */
2394
2395 offset = 0;
Andy Groverec98f782011-07-20 19:28:46 +00002396 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2397 addr = kmap_atomic(sg_page(sg), KM_USER0);
2398 if (!addr)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002399 goto out;
2400
Andy Groverec98f782011-07-20 19:28:46 +00002401 for (i = 0; i < sg->length; i++)
2402 *(addr + sg->offset + i) ^= *(buf + offset + i);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002403
Andy Groverec98f782011-07-20 19:28:46 +00002404 offset += sg->length;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002405 kunmap_atomic(addr, KM_USER0);
2406 }
Andy Groverec98f782011-07-20 19:28:46 +00002407
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002408out:
2409 kfree(buf);
2410}
2411
2412/*
2413 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2414 */
2415static int transport_get_sense_data(struct se_cmd *cmd)
2416{
2417 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002418 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002419 struct se_task *task = NULL, *task_tmp;
2420 unsigned long flags;
2421 u32 offset = 0;
2422
Andy Grovere3d6f902011-07-19 08:55:10 +00002423 WARN_ON(!cmd->se_lun);
2424
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002425 if (!dev)
2426 return 0;
2427
Andy Grovera1d8b492011-05-02 17:12:10 -07002428 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002429 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002430 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002431 return 0;
2432 }
2433
2434 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07002435 &cmd->t_task_list, t_list) {
Christoph Hellwigef804a82011-11-23 06:53:58 -05002436 if (!(task->task_flags & TF_HAS_SENSE))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002437 continue;
2438
Andy Grovere3d6f902011-07-19 08:55:10 +00002439 if (!dev->transport->get_sense_buffer) {
Andy Grover6708bb22011-06-08 10:36:43 -07002440 pr_err("dev->transport->get_sense_buffer"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002441 " is NULL\n");
2442 continue;
2443 }
2444
Andy Grovere3d6f902011-07-19 08:55:10 +00002445 sense_buffer = dev->transport->get_sense_buffer(task);
Andy Grover6708bb22011-06-08 10:36:43 -07002446 if (!sense_buffer) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04002447 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002448 " sense buffer for task with sense\n",
Christoph Hellwig04629b72011-10-12 11:07:04 -04002449 cmd->se_tfo->get_task_tag(cmd), task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002450 continue;
2451 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002452 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002453
Andy Grovere3d6f902011-07-19 08:55:10 +00002454 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002455 TRANSPORT_SENSE_BUFFER);
2456
Andy Grover5951146d2011-07-19 10:26:37 +00002457 memcpy(&buffer[offset], sense_buffer,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002458 TRANSPORT_SENSE_BUFFER);
2459 cmd->scsi_status = task->task_scsi_status;
2460 /* Automatically padded */
2461 cmd->scsi_sense_length =
2462 (TRANSPORT_SENSE_BUFFER + offset);
2463
Andy Grover6708bb22011-06-08 10:36:43 -07002464 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002465 " and sense\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002466 dev->se_hba->hba_id, dev->transport->name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002467 cmd->scsi_status);
2468 return 0;
2469 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002470 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002471
2472 return -1;
2473}
2474
Andy Groverec98f782011-07-20 19:28:46 +00002475static inline long long transport_dev_end_lba(struct se_device *dev)
2476{
2477 return dev->transport->get_blocks(dev) + 1;
2478}
2479
2480static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2481{
2482 struct se_device *dev = cmd->se_dev;
2483 u32 sectors;
2484
2485 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2486 return 0;
2487
2488 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2489
Andy Grover6708bb22011-06-08 10:36:43 -07002490 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2491 pr_err("LBA: %llu Sectors: %u exceeds"
Andy Groverec98f782011-07-20 19:28:46 +00002492 " transport_dev_end_lba(): %llu\n",
2493 cmd->t_task_lba, sectors,
2494 transport_dev_end_lba(dev));
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002495 return -EINVAL;
Andy Groverec98f782011-07-20 19:28:46 +00002496 }
2497
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002498 return 0;
Andy Groverec98f782011-07-20 19:28:46 +00002499}
2500
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002501static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2502{
2503 /*
2504 * Determine if the received WRITE_SAME is used to for direct
2505 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2506 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2507 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2508 */
2509 int passthrough = (dev->transport->transport_type ==
2510 TRANSPORT_PLUGIN_PHBA_PDEV);
2511
2512 if (!passthrough) {
2513 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2514 pr_err("WRITE_SAME PBDATA and LBDATA"
2515 " bits not supported for Block Discard"
2516 " Emulation\n");
2517 return -ENOSYS;
2518 }
2519 /*
2520 * Currently for the emulated case we only accept
2521 * tpws with the UNMAP=1 bit set.
2522 */
2523 if (!(flags[0] & 0x08)) {
2524 pr_err("WRITE_SAME w/o UNMAP bit not"
2525 " supported for Block Discard Emulation\n");
2526 return -ENOSYS;
2527 }
2528 }
2529
2530 return 0;
2531}
2532
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002533/* transport_generic_cmd_sequencer():
2534 *
2535 * Generic Command Sequencer that should work for most DAS transport
2536 * drivers.
2537 *
2538 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2539 * RX Thread.
2540 *
2541 * FIXME: Need to support other SCSI OPCODES where as well.
2542 */
2543static int transport_generic_cmd_sequencer(
2544 struct se_cmd *cmd,
2545 unsigned char *cdb)
2546{
Andy Grover5951146d2011-07-19 10:26:37 +00002547 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002548 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2549 int ret = 0, sector_ret = 0, passthrough;
2550 u32 sectors = 0, size = 0, pr_reg_type = 0;
2551 u16 service_action;
2552 u8 alua_ascq = 0;
2553 /*
2554 * Check for an existing UNIT ATTENTION condition
2555 */
2556 if (core_scsi3_ua_check(cmd, cdb) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002557 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2558 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
Andy Grover5951146d2011-07-19 10:26:37 +00002559 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002560 }
2561 /*
2562 * Check status of Asymmetric Logical Unit Assignment port
2563 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002564 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002565 if (ret != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002566 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002567 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002568 * The ALUA additional sense code qualifier (ASCQ) is determined
2569 * by the ALUA primary or secondary access state..
2570 */
2571 if (ret > 0) {
2572#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002573 pr_debug("[%s]: ALUA TG Port not available,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002574 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002575 cmd->se_tfo->get_fabric_name(), alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002576#endif
2577 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2578 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2579 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
Andy Grover5951146d2011-07-19 10:26:37 +00002580 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002581 }
2582 goto out_invalid_cdb_field;
2583 }
2584 /*
2585 * Check status for SPC-3 Persistent Reservations
2586 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002587 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2588 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002589 cmd, cdb, pr_reg_type) != 0) {
2590 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2591 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2592 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2593 return -EBUSY;
2594 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002595 /*
2596 * This means the CDB is allowed for the SCSI Initiator port
2597 * when said port is *NOT* holding the legacy SPC-2 or
2598 * SPC-3 Persistent Reservation.
2599 */
2600 }
2601
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002602 /*
2603 * If we operate in passthrough mode we skip most CDB emulation and
2604 * instead hand the commands down to the physical SCSI device.
2605 */
2606 passthrough =
2607 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2608
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002609 switch (cdb[0]) {
2610 case READ_6:
2611 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2612 if (sector_ret)
2613 goto out_unsupported_cdb;
2614 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002615 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002616 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2617 break;
2618 case READ_10:
2619 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2620 if (sector_ret)
2621 goto out_unsupported_cdb;
2622 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002623 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002624 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2625 break;
2626 case READ_12:
2627 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2628 if (sector_ret)
2629 goto out_unsupported_cdb;
2630 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002631 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002632 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2633 break;
2634 case READ_16:
2635 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2636 if (sector_ret)
2637 goto out_unsupported_cdb;
2638 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002639 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002640 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2641 break;
2642 case WRITE_6:
2643 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2644 if (sector_ret)
2645 goto out_unsupported_cdb;
2646 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002647 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002648 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2649 break;
2650 case WRITE_10:
2651 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2652 if (sector_ret)
2653 goto out_unsupported_cdb;
2654 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002655 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002656 if (cdb[1] & 0x8)
2657 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002658 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2659 break;
2660 case WRITE_12:
2661 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2662 if (sector_ret)
2663 goto out_unsupported_cdb;
2664 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002665 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002666 if (cdb[1] & 0x8)
2667 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002668 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2669 break;
2670 case WRITE_16:
2671 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2672 if (sector_ret)
2673 goto out_unsupported_cdb;
2674 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002675 cmd->t_task_lba = transport_lba_64(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002676 if (cdb[1] & 0x8)
2677 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002678 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2679 break;
2680 case XDWRITEREAD_10:
2681 if ((cmd->data_direction != DMA_TO_DEVICE) ||
Christoph Hellwig33c3faf2011-11-14 11:36:30 -05002682 !(cmd->se_cmd_flags & SCF_BIDI))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002683 goto out_invalid_cdb_field;
2684 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2685 if (sector_ret)
2686 goto out_unsupported_cdb;
2687 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002688 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002689 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002690
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002691 /*
2692 * Do now allow BIDI commands for passthrough mode.
2693 */
2694 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002695 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002696
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002697 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002698 * Setup BIDI XOR callback to be run after I/O completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002699 */
2700 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002701 if (cdb[1] & 0x8)
2702 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002703 break;
2704 case VARIABLE_LENGTH_CMD:
2705 service_action = get_unaligned_be16(&cdb[8]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002706 switch (service_action) {
2707 case XDWRITEREAD_32:
2708 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2709 if (sector_ret)
2710 goto out_unsupported_cdb;
2711 size = transport_get_size(sectors, cdb, cmd);
2712 /*
2713 * Use WRITE_32 and READ_32 opcodes for the emulated
2714 * XDWRITE_READ_32 logic.
2715 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002716 cmd->t_task_lba = transport_lba_64_ext(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002717 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2718
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002719 /*
2720 * Do now allow BIDI commands for passthrough mode.
2721 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002722 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002723 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002724
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002725 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002726 * Setup BIDI XOR callback to be run during after I/O
2727 * completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002728 */
2729 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002730 if (cdb[1] & 0x8)
2731 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002732 break;
2733 case WRITE_SAME_32:
2734 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2735 if (sector_ret)
2736 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002737
Andy Grover6708bb22011-06-08 10:36:43 -07002738 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002739 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002740 else {
2741 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2742 " supported\n");
2743 goto out_invalid_cdb_field;
2744 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002745
Andy Grovera1d8b492011-05-02 17:12:10 -07002746 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002747 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2748
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002749 if (target_check_write_same_discard(&cdb[10], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08002750 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002751 if (!passthrough)
2752 cmd->execute_task = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002753 break;
2754 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002755 pr_err("VARIABLE_LENGTH_CMD service action"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002756 " 0x%04x not supported\n", service_action);
2757 goto out_unsupported_cdb;
2758 }
2759 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002760 case MAINTENANCE_IN:
Andy Grovere3d6f902011-07-19 08:55:10 +00002761 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002762 /* MAINTENANCE_IN from SCC-2 */
2763 /*
2764 * Check for emulated MI_REPORT_TARGET_PGS.
2765 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002766 if (cdb[1] == MI_REPORT_TARGET_PGS &&
2767 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2768 cmd->execute_task =
2769 target_emulate_report_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002770 }
2771 size = (cdb[6] << 24) | (cdb[7] << 16) |
2772 (cdb[8] << 8) | cdb[9];
2773 } else {
2774 /* GPCMD_SEND_KEY from multi media commands */
2775 size = (cdb[8] << 8) + cdb[9];
2776 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002777 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002778 break;
2779 case MODE_SELECT:
2780 size = cdb[4];
2781 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2782 break;
2783 case MODE_SELECT_10:
2784 size = (cdb[7] << 8) + cdb[8];
2785 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2786 break;
2787 case MODE_SENSE:
2788 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002789 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002790 if (!passthrough)
2791 cmd->execute_task = target_emulate_modesense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002792 break;
2793 case MODE_SENSE_10:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002794 size = (cdb[7] << 8) + cdb[8];
2795 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2796 if (!passthrough)
2797 cmd->execute_task = target_emulate_modesense;
2798 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002799 case GPCMD_READ_BUFFER_CAPACITY:
2800 case GPCMD_SEND_OPC:
2801 case LOG_SELECT:
2802 case LOG_SENSE:
2803 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002804 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002805 break;
2806 case READ_BLOCK_LIMITS:
2807 size = READ_BLOCK_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002808 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002809 break;
2810 case GPCMD_GET_CONFIGURATION:
2811 case GPCMD_READ_FORMAT_CAPACITIES:
2812 case GPCMD_READ_DISC_INFO:
2813 case GPCMD_READ_TRACK_RZONE_INFO:
2814 size = (cdb[7] << 8) + cdb[8];
2815 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2816 break;
2817 case PERSISTENT_RESERVE_IN:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002818 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002819 cmd->execute_task = target_scsi3_emulate_pr_in;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002820 size = (cdb[7] << 8) + cdb[8];
2821 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2822 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002823 case PERSISTENT_RESERVE_OUT:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002824 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002825 cmd->execute_task = target_scsi3_emulate_pr_out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002826 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002827 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002828 break;
2829 case GPCMD_MECHANISM_STATUS:
2830 case GPCMD_READ_DVD_STRUCTURE:
2831 size = (cdb[8] << 8) + cdb[9];
2832 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2833 break;
2834 case READ_POSITION:
2835 size = READ_POSITION_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002836 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002837 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002838 case MAINTENANCE_OUT:
Andy Grovere3d6f902011-07-19 08:55:10 +00002839 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002840 /* MAINTENANCE_OUT from SCC-2
2841 *
2842 * Check for emulated MO_SET_TARGET_PGS.
2843 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002844 if (cdb[1] == MO_SET_TARGET_PGS &&
2845 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2846 cmd->execute_task =
2847 target_emulate_set_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002848 }
2849
2850 size = (cdb[6] << 24) | (cdb[7] << 16) |
2851 (cdb[8] << 8) | cdb[9];
2852 } else {
2853 /* GPCMD_REPORT_KEY from multi media commands */
2854 size = (cdb[8] << 8) + cdb[9];
2855 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002856 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002857 break;
2858 case INQUIRY:
2859 size = (cdb[3] << 8) + cdb[4];
2860 /*
2861 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2862 * See spc4r17 section 5.3
2863 */
Andy Grover5951146d2011-07-19 10:26:37 +00002864 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002865 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002866 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002867 if (!passthrough)
2868 cmd->execute_task = target_emulate_inquiry;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002869 break;
2870 case READ_BUFFER:
2871 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002872 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002873 break;
2874 case READ_CAPACITY:
2875 size = READ_CAP_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002876 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002877 if (!passthrough)
2878 cmd->execute_task = target_emulate_readcapacity;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002879 break;
2880 case READ_MEDIA_SERIAL_NUMBER:
2881 case SECURITY_PROTOCOL_IN:
2882 case SECURITY_PROTOCOL_OUT:
2883 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002884 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002885 break;
2886 case SERVICE_ACTION_IN:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002887 switch (cmd->t_task_cdb[1] & 0x1f) {
2888 case SAI_READ_CAPACITY_16:
2889 if (!passthrough)
2890 cmd->execute_task =
2891 target_emulate_readcapacity_16;
2892 break;
2893 default:
2894 if (passthrough)
2895 break;
2896
2897 pr_err("Unsupported SA: 0x%02x\n",
2898 cmd->t_task_cdb[1] & 0x1f);
2899 goto out_unsupported_cdb;
2900 }
2901 /*FALLTHROUGH*/
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002902 case ACCESS_CONTROL_IN:
2903 case ACCESS_CONTROL_OUT:
2904 case EXTENDED_COPY:
2905 case READ_ATTRIBUTE:
2906 case RECEIVE_COPY_RESULTS:
2907 case WRITE_ATTRIBUTE:
2908 size = (cdb[10] << 24) | (cdb[11] << 16) |
2909 (cdb[12] << 8) | cdb[13];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002910 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002911 break;
2912 case RECEIVE_DIAGNOSTIC:
2913 case SEND_DIAGNOSTIC:
2914 size = (cdb[3] << 8) | cdb[4];
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/* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2918#if 0
2919 case GPCMD_READ_CD:
2920 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2921 size = (2336 * sectors);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002922 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002923 break;
2924#endif
2925 case READ_TOC:
2926 size = cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002927 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002928 break;
2929 case REQUEST_SENSE:
2930 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002931 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002932 if (!passthrough)
2933 cmd->execute_task = target_emulate_request_sense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002934 break;
2935 case READ_ELEMENT_STATUS:
2936 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002937 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002938 break;
2939 case WRITE_BUFFER:
2940 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002941 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002942 break;
2943 case RESERVE:
2944 case RESERVE_10:
2945 /*
2946 * The SPC-2 RESERVE 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] == RESERVE_10)
2950 size = (cdb[7] << 8) | cdb[8];
2951 else
2952 size = cmd->data_length;
2953
2954 /*
2955 * Setup the legacy emulated handler for SPC-2 and
2956 * >= SPC-3 compatible reservation handling (CRH=1)
2957 * Otherwise, we assume the underlying SCSI logic is
2958 * is running in SPC_PASSTHROUGH, and wants reservations
2959 * emulation disabled.
2960 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002961 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2962 cmd->execute_task = target_scsi2_reservation_reserve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002963 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2964 break;
2965 case RELEASE:
2966 case RELEASE_10:
2967 /*
2968 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2969 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2970 */
2971 if (cdb[0] == RELEASE_10)
2972 size = (cdb[7] << 8) | cdb[8];
2973 else
2974 size = cmd->data_length;
2975
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002976 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2977 cmd->execute_task = target_scsi2_reservation_release;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002978 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2979 break;
2980 case SYNCHRONIZE_CACHE:
Andy Grover8e94b8d2012-01-16 16:57:07 -08002981 case SYNCHRONIZE_CACHE_16:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002982 /*
2983 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2984 */
2985 if (cdb[0] == SYNCHRONIZE_CACHE) {
2986 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002987 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002988 } else {
2989 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002990 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002991 }
2992 if (sector_ret)
2993 goto out_unsupported_cdb;
2994
2995 size = transport_get_size(sectors, cdb, cmd);
2996 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2997
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002998 if (passthrough)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002999 break;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003000
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003001 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003002 * Check to ensure that LBA + Range does not exceed past end of
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07003003 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003004 */
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07003005 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
3006 if (transport_cmd_get_valid_sectors(cmd) < 0)
3007 goto out_invalid_cdb_field;
3008 }
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003009 cmd->execute_task = target_emulate_synchronize_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003010 break;
3011 case UNMAP:
3012 size = get_unaligned_be16(&cdb[7]);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003013 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003014 if (!passthrough)
3015 cmd->execute_task = target_emulate_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003016 break;
3017 case WRITE_SAME_16:
3018 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3019 if (sector_ret)
3020 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07003021
Andy Grover6708bb22011-06-08 10:36:43 -07003022 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07003023 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07003024 else {
3025 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3026 goto out_invalid_cdb_field;
3027 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07003028
Nicholas Bellinger5db07532011-07-27 22:18:52 -07003029 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003030 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003031
3032 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08003033 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003034 if (!passthrough)
3035 cmd->execute_task = target_emulate_write_same;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003036 break;
3037 case WRITE_SAME:
3038 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3039 if (sector_ret)
3040 goto out_unsupported_cdb;
3041
3042 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07003043 size = transport_get_size(1, cdb, cmd);
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003044 else {
3045 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3046 goto out_invalid_cdb_field;
3047 }
3048
3049 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
3050 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3051 /*
3052 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3053 * of byte 1 bit 3 UNMAP instead of original reserved field
3054 */
3055 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08003056 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003057 if (!passthrough)
3058 cmd->execute_task = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003059 break;
3060 case ALLOW_MEDIUM_REMOVAL:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003061 case ERASE:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003062 case REZERO_UNIT:
3063 case SEEK_10:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003064 case SPACE:
3065 case START_STOP:
3066 case TEST_UNIT_READY:
3067 case VERIFY:
3068 case WRITE_FILEMARKS:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003069 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3070 if (!passthrough)
3071 cmd->execute_task = target_emulate_noop;
3072 break;
3073 case GPCMD_CLOSE_TRACK:
3074 case INITIALIZE_ELEMENT_STATUS:
3075 case GPCMD_LOAD_UNLOAD:
3076 case GPCMD_SET_SPEED:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003077 case MOVE_MEDIUM:
3078 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3079 break;
3080 case REPORT_LUNS:
Christoph Hellwige76a35d2011-11-03 17:50:42 -04003081 cmd->execute_task = target_report_luns;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003082 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3083 /*
3084 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3085 * See spc4r17 section 5.3
3086 */
Andy Grover5951146d2011-07-19 10:26:37 +00003087 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003088 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003089 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003090 break;
3091 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003092 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003093 " 0x%02x, sending CHECK_CONDITION.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003094 cmd->se_tfo->get_fabric_name(), cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003095 goto out_unsupported_cdb;
3096 }
3097
3098 if (size != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003099 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003100 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
Andy Grovere3d6f902011-07-19 08:55:10 +00003101 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003102 cmd->data_length, size, cdb[0]);
3103
3104 cmd->cmd_spdtl = size;
3105
3106 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grover6708bb22011-06-08 10:36:43 -07003107 pr_err("Rejecting underflow/overflow"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003108 " WRITE data\n");
3109 goto out_invalid_cdb_field;
3110 }
3111 /*
3112 * Reject READ_* or WRITE_* with overflow/underflow for
3113 * type SCF_SCSI_DATA_SG_IO_CDB.
3114 */
Andy Grover6708bb22011-06-08 10:36:43 -07003115 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3116 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003117 " CDB on non 512-byte sector setup subsystem"
Andy Grovere3d6f902011-07-19 08:55:10 +00003118 " plugin: %s\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003119 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3120 goto out_invalid_cdb_field;
3121 }
3122
3123 if (size > cmd->data_length) {
3124 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3125 cmd->residual_count = (size - cmd->data_length);
3126 } else {
3127 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3128 cmd->residual_count = (cmd->data_length - size);
3129 }
3130 cmd->data_length = size;
3131 }
3132
Roland Dreier015487b2012-02-13 16:18:17 -08003133 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB &&
3134 sectors > dev->se_sub_dev->se_dev_attrib.fabric_max_sectors) {
3135 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too big sectors %u\n",
3136 cdb[0], sectors);
3137 goto out_invalid_cdb_field;
3138 }
3139
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003140 /* reject any command that we don't have a handler for */
3141 if (!(passthrough || cmd->execute_task ||
3142 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3143 goto out_unsupported_cdb;
3144
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003145 transport_set_supported_SAM_opcode(cmd);
3146 return ret;
3147
3148out_unsupported_cdb:
3149 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3150 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover5951146d2011-07-19 10:26:37 +00003151 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003152out_invalid_cdb_field:
3153 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3154 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00003155 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003156}
3157
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003158/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003159 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003160 * and ordered cmds need to have their tasks added to the execution queue.
3161 */
3162static void transport_complete_task_attr(struct se_cmd *cmd)
3163{
Andy Grover5951146d2011-07-19 10:26:37 +00003164 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003165 struct se_cmd *cmd_p, *cmd_tmp;
3166 int new_active_tasks = 0;
3167
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003168 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003169 atomic_dec(&dev->simple_cmds);
3170 smp_mb__after_atomic_dec();
3171 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003172 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003173 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3174 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003175 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003176 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003177 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003178 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3179 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003180 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003181 atomic_dec(&dev->dev_ordered_sync);
3182 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003183
3184 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003185 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003186 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3187 }
3188 /*
3189 * Process all commands up to the last received
3190 * ORDERED task attribute which requires another blocking
3191 * boundary
3192 */
3193 spin_lock(&dev->delayed_cmd_lock);
3194 list_for_each_entry_safe(cmd_p, cmd_tmp,
Andy Grover5951146d2011-07-19 10:26:37 +00003195 &dev->delayed_cmd_list, se_delayed_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003196
Andy Grover5951146d2011-07-19 10:26:37 +00003197 list_del(&cmd_p->se_delayed_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003198 spin_unlock(&dev->delayed_cmd_lock);
3199
Andy Grover6708bb22011-06-08 10:36:43 -07003200 pr_debug("Calling add_tasks() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003201 " cmd_p: 0x%02x Task Attr: 0x%02x"
3202 " Dormant -> Active, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07003203 cmd_p->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003204 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3205
3206 transport_add_tasks_from_cmd(cmd_p);
3207 new_active_tasks++;
3208
3209 spin_lock(&dev->delayed_cmd_lock);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003210 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003211 break;
3212 }
3213 spin_unlock(&dev->delayed_cmd_lock);
3214 /*
3215 * If new tasks have become active, wake up the transport thread
3216 * to do the processing of the Active tasks.
3217 */
3218 if (new_active_tasks != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003219 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003220}
3221
Christoph Hellwige057f532011-10-17 13:56:41 -04003222static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003223{
3224 int ret = 0;
3225
Christoph Hellwige057f532011-10-17 13:56:41 -04003226 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3227 transport_complete_task_attr(cmd);
3228
3229 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3230 ret = cmd->se_tfo->queue_status(cmd);
3231 if (ret)
3232 goto out;
3233 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003234
3235 switch (cmd->data_direction) {
3236 case DMA_FROM_DEVICE:
3237 ret = cmd->se_tfo->queue_data_in(cmd);
3238 break;
3239 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00003240 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003241 ret = cmd->se_tfo->queue_data_in(cmd);
3242 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04003243 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003244 }
3245 /* Fall through for DMA_TO_DEVICE */
3246 case DMA_NONE:
3247 ret = cmd->se_tfo->queue_status(cmd);
3248 break;
3249 default:
3250 break;
3251 }
3252
Christoph Hellwige057f532011-10-17 13:56:41 -04003253out:
3254 if (ret < 0) {
3255 transport_handle_queue_full(cmd, cmd->se_dev);
3256 return;
3257 }
3258 transport_lun_remove_cmd(cmd);
3259 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003260}
3261
3262static void transport_handle_queue_full(
3263 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04003264 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003265{
3266 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003267 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3268 atomic_inc(&dev->dev_qf_count);
3269 smp_mb__after_atomic_inc();
3270 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3271
3272 schedule_work(&cmd->se_dev->qf_work_queue);
3273}
3274
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003275static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003276{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003277 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003278 int reason = 0, ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003279
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003280 /*
3281 * Check if we need to move delayed/dormant tasks from cmds on the
3282 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3283 * Attribute.
3284 */
Andy Grover5951146d2011-07-19 10:26:37 +00003285 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003286 transport_complete_task_attr(cmd);
3287 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003288 * Check to schedule QUEUE_FULL work, or execute an existing
3289 * cmd->transport_qf_callback()
3290 */
3291 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3292 schedule_work(&cmd->se_dev->qf_work_queue);
3293
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003294 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295 * Check if we need to retrieve a sense buffer from
3296 * the struct se_cmd in question.
3297 */
3298 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3299 if (transport_get_sense_data(cmd) < 0)
3300 reason = TCM_NON_EXISTENT_LUN;
3301
3302 /*
3303 * Only set when an struct se_task->task_scsi_status returned
3304 * a non GOOD status.
3305 */
3306 if (cmd->scsi_status) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003307 ret = transport_send_check_condition_and_sense(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003308 cmd, reason, 1);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003309 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003310 goto queue_full;
3311
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003312 transport_lun_remove_cmd(cmd);
3313 transport_cmd_check_stop_to_fabric(cmd);
3314 return;
3315 }
3316 }
3317 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003318 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003319 * XDWRITE_READ_10 emulation.
3320 */
3321 if (cmd->transport_complete_callback)
3322 cmd->transport_complete_callback(cmd);
3323
3324 switch (cmd->data_direction) {
3325 case DMA_FROM_DEVICE:
3326 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003327 if (cmd->se_lun->lun_sep) {
3328 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003329 cmd->data_length;
3330 }
3331 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003332
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003333 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003334 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003335 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003336 break;
3337 case DMA_TO_DEVICE:
3338 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003339 if (cmd->se_lun->lun_sep) {
3340 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003341 cmd->data_length;
3342 }
3343 spin_unlock(&cmd->se_lun->lun_sep_lock);
3344 /*
3345 * Check if we need to send READ payload for BIDI-COMMAND
3346 */
Andy Groverec98f782011-07-20 19:28:46 +00003347 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003348 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003349 if (cmd->se_lun->lun_sep) {
3350 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003351 cmd->data_length;
3352 }
3353 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003354 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003355 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003356 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003357 break;
3358 }
3359 /* Fall through for DMA_TO_DEVICE */
3360 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003361 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003362 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003363 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003364 break;
3365 default:
3366 break;
3367 }
3368
3369 transport_lun_remove_cmd(cmd);
3370 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003371 return;
3372
3373queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003374 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003375 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04003376 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3377 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003378}
3379
3380static void transport_free_dev_tasks(struct se_cmd *cmd)
3381{
3382 struct se_task *task, *task_tmp;
3383 unsigned long flags;
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003384 LIST_HEAD(dispose_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003385
Andy Grovera1d8b492011-05-02 17:12:10 -07003386 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003387 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07003388 &cmd->t_task_list, t_list) {
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003389 if (!(task->task_flags & TF_ACTIVE))
3390 list_move_tail(&task->t_list, &dispose_list);
3391 }
3392 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3393
3394 while (!list_empty(&dispose_list)) {
3395 task = list_first_entry(&dispose_list, struct se_task, t_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003396
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003397 if (task->task_sg != cmd->t_data_sg &&
3398 task->task_sg != cmd->t_bidi_data_sg)
3399 kfree(task->task_sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003400
3401 list_del(&task->t_list);
3402
Christoph Hellwig42bf8292011-10-12 11:07:00 -04003403 cmd->se_dev->transport->free_task(task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003404 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003405}
3406
Andy Grover6708bb22011-06-08 10:36:43 -07003407static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003408{
Andy Groverec98f782011-07-20 19:28:46 +00003409 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00003410 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003411
Andy Grover6708bb22011-06-08 10:36:43 -07003412 for_each_sg(sgl, sg, nents, count)
3413 __free_page(sg_page(sg));
3414
3415 kfree(sgl);
3416}
3417
3418static inline void transport_free_pages(struct se_cmd *cmd)
3419{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003420 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07003421 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003422
Andy Grover6708bb22011-06-08 10:36:43 -07003423 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003424 cmd->t_data_sg = NULL;
3425 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003426
Andy Grover6708bb22011-06-08 10:36:43 -07003427 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003428 cmd->t_bidi_data_sg = NULL;
3429 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003430}
3431
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003432/**
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003433 * transport_release_cmd - free a command
3434 * @cmd: command to free
3435 *
3436 * This routine unconditionally frees a command, and reference counting
3437 * or list removal must be done in the caller.
3438 */
3439static void transport_release_cmd(struct se_cmd *cmd)
3440{
3441 BUG_ON(!cmd->se_tfo);
3442
Andy Groverc8e31f22012-01-19 13:39:17 -08003443 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003444 core_tmr_release_req(cmd->se_tmr_req);
3445 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3446 kfree(cmd->t_task_cdb);
3447 /*
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003448 * If this cmd has been setup with target_get_sess_cmd(), drop
3449 * the kref and call ->release_cmd() in kref callback.
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003450 */
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003451 if (cmd->check_release != 0) {
3452 target_put_sess_cmd(cmd->se_sess, cmd);
3453 return;
3454 }
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003455 cmd->se_tfo->release_cmd(cmd);
3456}
3457
3458/**
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003459 * transport_put_cmd - release a reference to a command
3460 * @cmd: command to release
3461 *
3462 * This routine releases our reference to the command and frees it if possible.
3463 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003464static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003465{
3466 unsigned long flags;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003467 int free_tasks = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003468
Andy Grovera1d8b492011-05-02 17:12:10 -07003469 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003470 if (atomic_read(&cmd->t_fe_count)) {
3471 if (!atomic_dec_and_test(&cmd->t_fe_count))
3472 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003473 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003474
3475 if (atomic_read(&cmd->t_se_count)) {
3476 if (!atomic_dec_and_test(&cmd->t_se_count))
3477 goto out_busy;
3478 }
3479
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003480 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
3481 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003482 transport_all_task_dev_remove_state(cmd);
3483 free_tasks = 1;
3484 }
Andy Grovera1d8b492011-05-02 17:12:10 -07003485 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003486
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003487 if (free_tasks != 0)
3488 transport_free_dev_tasks(cmd);
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003489
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003490 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02003491 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003492 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003493out_busy:
3494 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003495}
3496
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003497/*
Andy Groverec98f782011-07-20 19:28:46 +00003498 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3499 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003500 * @cmd: Associated se_cmd descriptor
3501 * @mem: SGL style memory for TCM WRITE / READ
3502 * @sg_mem_num: Number of SGL elements
3503 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3504 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3505 *
3506 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3507 * of parameters.
3508 */
3509int transport_generic_map_mem_to_cmd(
3510 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00003511 struct scatterlist *sgl,
3512 u32 sgl_count,
3513 struct scatterlist *sgl_bidi,
3514 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003515{
Andy Grover5951146d2011-07-19 10:26:37 +00003516 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003517 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003518
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003519 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3520 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
Nicholas Bellingerfef58a62011-11-15 22:13:24 -08003521 /*
3522 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3523 * scatterlists already have been set to follow what the fabric
3524 * passes for the original expected data transfer length.
3525 */
3526 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3527 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3528 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3529 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3530 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3531 return -EINVAL;
3532 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003533
Andy Groverec98f782011-07-20 19:28:46 +00003534 cmd->t_data_sg = sgl;
3535 cmd->t_data_nents = sgl_count;
3536
Andy Grover5951146d2011-07-19 10:26:37 +00003537 if (sgl_bidi && sgl_bidi_count) {
Andy Groverec98f782011-07-20 19:28:46 +00003538 cmd->t_bidi_data_sg = sgl_bidi;
3539 cmd->t_bidi_data_nents = sgl_bidi_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003540 }
3541 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003542 }
3543
3544 return 0;
3545}
3546EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3547
Andy Grover49493142012-01-16 16:57:08 -08003548void *transport_kmap_data_sg(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003549{
Andy Groverec98f782011-07-20 19:28:46 +00003550 struct scatterlist *sg = cmd->t_data_sg;
Andy Grover49493142012-01-16 16:57:08 -08003551 struct page **pages;
3552 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003553
Andy Groverec98f782011-07-20 19:28:46 +00003554 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003555 /*
Andy Groverec98f782011-07-20 19:28:46 +00003556 * We need to take into account a possible offset here for fabrics like
3557 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3558 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00003559 */
Andy Grover49493142012-01-16 16:57:08 -08003560 if (!cmd->t_data_nents)
3561 return NULL;
3562 else if (cmd->t_data_nents == 1)
3563 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003564
Andy Grover49493142012-01-16 16:57:08 -08003565 /* >1 page. use vmap */
3566 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
3567 if (!pages)
3568 return NULL;
3569
3570 /* convert sg[] to pages[] */
3571 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
3572 pages[i] = sg_page(sg);
3573 }
3574
3575 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
3576 kfree(pages);
3577 if (!cmd->t_data_vmap)
3578 return NULL;
3579
3580 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003581}
Andy Grover49493142012-01-16 16:57:08 -08003582EXPORT_SYMBOL(transport_kmap_data_sg);
3583
3584void transport_kunmap_data_sg(struct se_cmd *cmd)
3585{
Andy Grovera1edf9c2012-02-09 12:18:06 -08003586 if (!cmd->t_data_nents) {
Andy Grover49493142012-01-16 16:57:08 -08003587 return;
Andy Grovera1edf9c2012-02-09 12:18:06 -08003588 } else if (cmd->t_data_nents == 1) {
Andy Grover49493142012-01-16 16:57:08 -08003589 kunmap(sg_page(cmd->t_data_sg));
Andy Grovera1edf9c2012-02-09 12:18:06 -08003590 return;
3591 }
Andy Grover49493142012-01-16 16:57:08 -08003592
3593 vunmap(cmd->t_data_vmap);
3594 cmd->t_data_vmap = NULL;
3595}
3596EXPORT_SYMBOL(transport_kunmap_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003597
3598static int
3599transport_generic_get_mem(struct se_cmd *cmd)
3600{
Andy Groverec98f782011-07-20 19:28:46 +00003601 u32 length = cmd->data_length;
3602 unsigned int nents;
3603 struct page *page;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003604 gfp_t zero_flag;
Andy Groverec98f782011-07-20 19:28:46 +00003605 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003606
Andy Groverec98f782011-07-20 19:28:46 +00003607 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3608 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3609 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00003610 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003611
Andy Groverec98f782011-07-20 19:28:46 +00003612 cmd->t_data_nents = nents;
3613 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003614
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003615 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
3616
Andy Groverec98f782011-07-20 19:28:46 +00003617 while (length) {
3618 u32 page_len = min_t(u32, length, PAGE_SIZE);
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003619 page = alloc_page(GFP_KERNEL | zero_flag);
Andy Groverec98f782011-07-20 19:28:46 +00003620 if (!page)
3621 goto out;
3622
3623 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3624 length -= page_len;
3625 i++;
3626 }
3627 return 0;
3628
3629out:
3630 while (i >= 0) {
3631 __free_page(sg_page(&cmd->t_data_sg[i]));
3632 i--;
3633 }
3634 kfree(cmd->t_data_sg);
3635 cmd->t_data_sg = NULL;
3636 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003637}
3638
Andy Grovera1d8b492011-05-02 17:12:10 -07003639/* Reduce sectors if they are too long for the device */
3640static inline sector_t transport_limit_task_sectors(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003641 struct se_device *dev,
3642 unsigned long long lba,
Andy Grovera1d8b492011-05-02 17:12:10 -07003643 sector_t sectors)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003644{
Andy Grovera1d8b492011-05-02 17:12:10 -07003645 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003646
Andy Grovera1d8b492011-05-02 17:12:10 -07003647 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3648 if ((lba + sectors) > transport_dev_end_lba(dev))
3649 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003650
Andy Grovera1d8b492011-05-02 17:12:10 -07003651 return sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003652}
3653
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003654
3655/*
3656 * This function can be used by HW target mode drivers to create a linked
3657 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3658 * This is intended to be called during the completion path by TCM Core
3659 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3660 */
3661void transport_do_task_sg_chain(struct se_cmd *cmd)
3662{
Andy Groverec98f782011-07-20 19:28:46 +00003663 struct scatterlist *sg_first = NULL;
3664 struct scatterlist *sg_prev = NULL;
3665 int sg_prev_nents = 0;
3666 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003667 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003668 u32 chained_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003669 int i;
3670
Andy Groverec98f782011-07-20 19:28:46 +00003671 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3672
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003673 /*
3674 * Walk the struct se_task list and setup scatterlist chains
Andy Grovera1d8b492011-05-02 17:12:10 -07003675 * for each contiguously allocated struct se_task->task_sg[].
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003676 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003677 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Andy Groverec98f782011-07-20 19:28:46 +00003678 if (!task->task_sg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003679 continue;
3680
Andy Groverec98f782011-07-20 19:28:46 +00003681 if (!sg_first) {
3682 sg_first = task->task_sg;
Andy Grover6708bb22011-06-08 10:36:43 -07003683 chained_nents = task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003684 } else {
Andy Groverec98f782011-07-20 19:28:46 +00003685 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
Andy Grover6708bb22011-06-08 10:36:43 -07003686 chained_nents += task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003687 }
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003688 /*
3689 * For the padded tasks, use the extra SGL vector allocated
3690 * in transport_allocate_data_tasks() for the sg_prev_nents
Christoph Hellwig04629b72011-10-12 11:07:04 -04003691 * offset into sg_chain() above.
3692 *
3693 * We do not need the padding for the last task (or a single
3694 * task), but in that case we will never use the sg_prev_nents
3695 * value below which would be incorrect.
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003696 */
Christoph Hellwig04629b72011-10-12 11:07:04 -04003697 sg_prev_nents = (task->task_sg_nents + 1);
Andy Groverec98f782011-07-20 19:28:46 +00003698 sg_prev = task->task_sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003699 }
3700 /*
3701 * Setup the starting pointer and total t_tasks_sg_linked_no including
3702 * padding SGs for linking and to mark the end.
3703 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003704 cmd->t_tasks_sg_chained = sg_first;
Andy Groverec98f782011-07-20 19:28:46 +00003705 cmd->t_tasks_sg_chained_no = chained_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003706
Andy Grover6708bb22011-06-08 10:36:43 -07003707 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
Andy Grovera1d8b492011-05-02 17:12:10 -07003708 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3709 cmd->t_tasks_sg_chained_no);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003710
Andy Grovera1d8b492011-05-02 17:12:10 -07003711 for_each_sg(cmd->t_tasks_sg_chained, sg,
3712 cmd->t_tasks_sg_chained_no, i) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003713
Andy Grover6708bb22011-06-08 10:36:43 -07003714 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
Andy Grover5951146d2011-07-19 10:26:37 +00003715 i, sg, sg_page(sg), sg->length, sg->offset);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003716 if (sg_is_chain(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003717 pr_debug("SG: %p sg_is_chain=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003718 if (sg_is_last(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003719 pr_debug("SG: %p sg_is_last=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003720 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003721}
3722EXPORT_SYMBOL(transport_do_task_sg_chain);
3723
Andy Grovera1d8b492011-05-02 17:12:10 -07003724/*
3725 * Break up cmd into chunks transport can handle
3726 */
Christoph Hellwig38b400672011-10-18 06:57:02 -04003727static int
3728transport_allocate_data_tasks(struct se_cmd *cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003729 enum dma_data_direction data_direction,
Christoph Hellwig38b400672011-10-18 06:57:02 -04003730 struct scatterlist *cmd_sg, unsigned int sgl_nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003731{
Andy Grover5951146d2011-07-19 10:26:37 +00003732 struct se_device *dev = cmd->se_dev;
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003733 int task_count, i;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003734 unsigned long long lba;
3735 sector_t sectors, dev_max_sectors;
3736 u32 sector_size;
3737
3738 if (transport_cmd_get_valid_sectors(cmd) < 0)
3739 return -EINVAL;
3740
3741 dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3742 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003743
Andy Groverec98f782011-07-20 19:28:46 +00003744 WARN_ON(cmd->data_length % sector_size);
Christoph Hellwig38b400672011-10-18 06:57:02 -04003745
3746 lba = cmd->t_task_lba;
Andy Groverec98f782011-07-20 19:28:46 +00003747 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
Nicholas Bellinger277c5f22011-07-26 00:38:40 -07003748 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003749
3750 /*
3751 * If we need just a single task reuse the SG list in the command
3752 * and avoid a lot of work.
3753 */
3754 if (task_count == 1) {
3755 struct se_task *task;
3756 unsigned long flags;
3757
3758 task = transport_generic_get_task(cmd, data_direction);
3759 if (!task)
3760 return -ENOMEM;
3761
3762 task->task_sg = cmd_sg;
3763 task->task_sg_nents = sgl_nents;
3764
3765 task->task_lba = lba;
3766 task->task_sectors = sectors;
3767 task->task_size = task->task_sectors * sector_size;
3768
3769 spin_lock_irqsave(&cmd->t_state_lock, flags);
3770 list_add_tail(&task->t_list, &cmd->t_task_list);
3771 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3772
3773 return task_count;
3774 }
3775
Andy Groverec98f782011-07-20 19:28:46 +00003776 for (i = 0; i < task_count; i++) {
Christoph Hellwig38b400672011-10-18 06:57:02 -04003777 struct se_task *task;
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003778 unsigned int task_size, task_sg_nents_padded;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003779 struct scatterlist *sg;
3780 unsigned long flags;
Andy Groverec98f782011-07-20 19:28:46 +00003781 int count;
Andy Grovera1d8b492011-05-02 17:12:10 -07003782
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003783 task = transport_generic_get_task(cmd, data_direction);
Andy Grovera1d8b492011-05-02 17:12:10 -07003784 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003785 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003786
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003787 task->task_lba = lba;
Andy Groverec98f782011-07-20 19:28:46 +00003788 task->task_sectors = min(sectors, dev_max_sectors);
3789 task->task_size = task->task_sectors * sector_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003790
Nicholas Bellinger525a48a2011-08-13 02:11:38 -07003791 /*
3792 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3793 * in order to calculate the number per task SGL entries
3794 */
3795 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003796 /*
Andy Groverec98f782011-07-20 19:28:46 +00003797 * Check if the fabric module driver is requesting that all
3798 * struct se_task->task_sg[] be chained together.. If so,
3799 * then allocate an extra padding SG entry for linking and
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003800 * marking the end of the chained SGL for every task except
3801 * the last one for (task_count > 1) operation, or skipping
3802 * the extra padding for the (task_count == 1) case.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003803 */
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003804 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3805 task_sg_nents_padded = (task->task_sg_nents + 1);
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003806 } else
3807 task_sg_nents_padded = task->task_sg_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003808
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003809 task->task_sg = kmalloc(sizeof(struct scatterlist) *
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003810 task_sg_nents_padded, GFP_KERNEL);
Andy Groverec98f782011-07-20 19:28:46 +00003811 if (!task->task_sg) {
3812 cmd->se_dev->transport->free_task(task);
3813 return -ENOMEM;
3814 }
3815
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003816 sg_init_table(task->task_sg, task_sg_nents_padded);
Andy Groverec98f782011-07-20 19:28:46 +00003817
3818 task_size = task->task_size;
3819
3820 /* Build new sgl, only up to task_size */
Andy Grover6708bb22011-06-08 10:36:43 -07003821 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
Andy Groverec98f782011-07-20 19:28:46 +00003822 if (cmd_sg->length > task_size)
3823 break;
3824
3825 *sg = *cmd_sg;
3826 task_size -= cmd_sg->length;
3827 cmd_sg = sg_next(cmd_sg);
3828 }
3829
3830 lba += task->task_sectors;
3831 sectors -= task->task_sectors;
3832
3833 spin_lock_irqsave(&cmd->t_state_lock, flags);
3834 list_add_tail(&task->t_list, &cmd->t_task_list);
3835 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003836 }
3837
Andy Groverec98f782011-07-20 19:28:46 +00003838 return task_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003839}
3840
3841static int
Andy Groverec98f782011-07-20 19:28:46 +00003842transport_allocate_control_task(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003843{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003844 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003845 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003846
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08003847 /* Workaround for handling zero-length control CDBs */
3848 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3849 !cmd->data_length)
3850 return 0;
3851
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003852 task = transport_generic_get_task(cmd, cmd->data_direction);
3853 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003854 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003855
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003856 task->task_sg = cmd->t_data_sg;
Andy Groverec98f782011-07-20 19:28:46 +00003857 task->task_size = cmd->data_length;
Andy Grover6708bb22011-06-08 10:36:43 -07003858 task->task_sg_nents = cmd->t_data_nents;
Andy Groverec98f782011-07-20 19:28:46 +00003859
3860 spin_lock_irqsave(&cmd->t_state_lock, flags);
3861 list_add_tail(&task->t_list, &cmd->t_task_list);
3862 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003863
Andy Grover6708bb22011-06-08 10:36:43 -07003864 /* Success! Return number of tasks allocated */
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003865 return 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003866}
3867
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003868/*
3869 * Allocate any required ressources to execute the command, and either place
3870 * it on the execution queue if possible. For writes we might not have the
3871 * payload yet, thus notify the fabric via a call to ->write_pending instead.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003872 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003873int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003874{
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003875 struct se_device *dev = cmd->se_dev;
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003876 int task_cdbs, task_cdbs_bidi = 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003877 int set_counts = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003878 int ret = 0;
3879
3880 /*
3881 * Determine is the TCM fabric module has already allocated physical
3882 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00003883 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003884 */
Andy Groverec98f782011-07-20 19:28:46 +00003885 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3886 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00003887 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003888 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003889 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003890 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003891
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003892 /*
Christoph Hellwig38b400672011-10-18 06:57:02 -04003893 * For BIDI command set up the read tasks first.
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003894 */
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003895 if (cmd->t_bidi_data_sg &&
Christoph Hellwig38b400672011-10-18 06:57:02 -04003896 dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
3897 BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
3898
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003899 task_cdbs_bidi = transport_allocate_data_tasks(cmd,
3900 DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
3901 cmd->t_bidi_data_nents);
3902 if (task_cdbs_bidi <= 0)
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003903 goto out_fail;
3904
3905 atomic_inc(&cmd->t_fe_count);
3906 atomic_inc(&cmd->t_se_count);
3907 set_counts = 0;
3908 }
Christoph Hellwig38b400672011-10-18 06:57:02 -04003909
3910 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3911 task_cdbs = transport_allocate_data_tasks(cmd,
3912 cmd->data_direction, cmd->t_data_sg,
3913 cmd->t_data_nents);
3914 } else {
3915 task_cdbs = transport_allocate_control_task(cmd);
3916 }
3917
Roland Dreier410f6702011-11-22 13:51:32 -08003918 if (task_cdbs < 0)
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003919 goto out_fail;
Roland Dreier410f6702011-11-22 13:51:32 -08003920 else if (!task_cdbs && (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003921 spin_lock_irq(&cmd->t_state_lock);
Roland Dreier410f6702011-11-22 13:51:32 -08003922 cmd->t_state = TRANSPORT_COMPLETE;
Christoph Hellwig7d680f32011-12-21 14:13:47 -05003923 cmd->transport_state |= CMD_T_ACTIVE;
3924 spin_unlock_irq(&cmd->t_state_lock);
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08003925
3926 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
3927 u8 ua_asc = 0, ua_ascq = 0;
3928
3929 core_scsi3_ua_clear_for_request_sense(cmd,
3930 &ua_asc, &ua_ascq);
3931 }
3932
Roland Dreier410f6702011-11-22 13:51:32 -08003933 INIT_WORK(&cmd->work, target_complete_ok_work);
3934 queue_work(target_completion_wq, &cmd->work);
3935 return 0;
3936 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003937
3938 if (set_counts) {
3939 atomic_inc(&cmd->t_fe_count);
3940 atomic_inc(&cmd->t_se_count);
3941 }
3942
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003943 cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
3944 atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
3945 atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003946
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003947 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07003948 * For WRITEs, let the fabric know its buffer is ready..
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003949 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3950 * will be added to the struct se_device execution queue after its WRITE
3951 * data has arrived. (ie: It gets handled by the transport processing
3952 * thread a second time)
3953 */
3954 if (cmd->data_direction == DMA_TO_DEVICE) {
3955 transport_add_tasks_to_state_queue(cmd);
3956 return transport_generic_write_pending(cmd);
3957 }
3958 /*
3959 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3960 * to the execution queue.
3961 */
3962 transport_execute_tasks(cmd);
3963 return 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003964
3965out_fail:
3966 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3967 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3968 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003969}
Andy Grovera1d8b492011-05-02 17:12:10 -07003970EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003971
3972/* transport_generic_process_write():
3973 *
3974 *
3975 */
3976void transport_generic_process_write(struct se_cmd *cmd)
3977{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003978 transport_execute_tasks(cmd);
3979}
3980EXPORT_SYMBOL(transport_generic_process_write);
3981
Christoph Hellwige057f532011-10-17 13:56:41 -04003982static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003983{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003984 int ret;
3985
3986 ret = cmd->se_tfo->write_pending(cmd);
3987 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04003988 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3989 cmd);
3990 transport_handle_queue_full(cmd, cmd->se_dev);
3991 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003992}
3993
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003994static int transport_generic_write_pending(struct se_cmd *cmd)
3995{
3996 unsigned long flags;
3997 int ret;
3998
Andy Grovera1d8b492011-05-02 17:12:10 -07003999 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004000 cmd->t_state = TRANSPORT_WRITE_PENDING;
Andy Grovera1d8b492011-05-02 17:12:10 -07004001 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004002
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004003 /*
4004 * Clear the se_cmd for WRITE_PENDING status in order to set
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004005 * CMD_T_ACTIVE so that transport_generic_handle_data can be called
4006 * from HW target mode interrupt code. This is safe to be called
4007 * with transport_off=1 before the cmd->se_tfo->write_pending
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004008 * because the se_cmd->se_lun pointer is not being cleared.
4009 */
4010 transport_cmd_check_stop(cmd, 1, 0);
4011
4012 /*
4013 * Call the fabric write_pending function here to let the
4014 * frontend know that WRITE buffers are ready.
4015 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004016 ret = cmd->se_tfo->write_pending(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004017 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004018 goto queue_full;
4019 else if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004020 return ret;
4021
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004022 return 1;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004023
4024queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07004025 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004026 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
Christoph Hellwige057f532011-10-17 13:56:41 -04004027 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004028 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004029}
4030
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07004031void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004032{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004033 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
Andy Groverc8e31f22012-01-19 13:39:17 -08004034 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004035 transport_wait_for_tasks(cmd);
4036
Christoph Hellwig35462972011-05-31 23:56:57 -04004037 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004038 } else {
4039 if (wait_for_tasks)
4040 transport_wait_for_tasks(cmd);
4041
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004042 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
4043
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02004044 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004045 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004046
Nicholas Bellingerf4366772011-05-19 20:19:11 -07004047 transport_free_dev_tasks(cmd);
4048
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07004049 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004050 }
4051}
4052EXPORT_SYMBOL(transport_generic_free_cmd);
4053
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004054/* target_get_sess_cmd - Add command to active ->sess_cmd_list
4055 * @se_sess: session to reference
4056 * @se_cmd: command descriptor to add
Nicholas Bellingera6360782011-11-18 20:36:22 -08004057 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004058 */
Nicholas Bellingera6360782011-11-18 20:36:22 -08004059void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
4060 bool ack_kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004061{
4062 unsigned long flags;
4063
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004064 kref_init(&se_cmd->cmd_kref);
Nicholas Bellingera6360782011-11-18 20:36:22 -08004065 /*
4066 * Add a second kref if the fabric caller is expecting to handle
4067 * fabric acknowledgement that requires two target_put_sess_cmd()
4068 * invocations before se_cmd descriptor release.
4069 */
Nicholas Bellinger86715562012-02-13 01:07:22 -08004070 if (ack_kref == true) {
Nicholas Bellingera6360782011-11-18 20:36:22 -08004071 kref_get(&se_cmd->cmd_kref);
Nicholas Bellinger86715562012-02-13 01:07:22 -08004072 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
4073 }
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004074
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004075 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4076 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
4077 se_cmd->check_release = 1;
4078 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4079}
4080EXPORT_SYMBOL(target_get_sess_cmd);
4081
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004082static void target_release_cmd_kref(struct kref *kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004083{
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004084 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
4085 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004086 unsigned long flags;
4087
4088 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4089 if (list_empty(&se_cmd->se_cmd_list)) {
4090 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
Nicholas Bellingerffc32d52012-02-13 02:35:01 -08004091 se_cmd->se_tfo->release_cmd(se_cmd);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004092 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004093 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004094 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
4095 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4096 complete(&se_cmd->cmd_wait_comp);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004097 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004098 }
4099 list_del(&se_cmd->se_cmd_list);
4100 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4101
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004102 se_cmd->se_tfo->release_cmd(se_cmd);
4103}
4104
4105/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
4106 * @se_sess: session to reference
4107 * @se_cmd: command descriptor to drop
4108 */
4109int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
4110{
4111 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004112}
4113EXPORT_SYMBOL(target_put_sess_cmd);
4114
4115/* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
4116 * @se_sess: session to split
4117 */
4118void target_splice_sess_cmd_list(struct se_session *se_sess)
4119{
4120 struct se_cmd *se_cmd;
4121 unsigned long flags;
4122
4123 WARN_ON(!list_empty(&se_sess->sess_wait_list));
4124 INIT_LIST_HEAD(&se_sess->sess_wait_list);
4125
4126 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4127 se_sess->sess_tearing_down = 1;
4128
4129 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
4130
4131 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
4132 se_cmd->cmd_wait_set = 1;
4133
4134 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4135}
4136EXPORT_SYMBOL(target_splice_sess_cmd_list);
4137
4138/* target_wait_for_sess_cmds - Wait for outstanding descriptors
4139 * @se_sess: session to wait for active I/O
4140 * @wait_for_tasks: Make extra transport_wait_for_tasks call
4141 */
4142void target_wait_for_sess_cmds(
4143 struct se_session *se_sess,
4144 int wait_for_tasks)
4145{
4146 struct se_cmd *se_cmd, *tmp_cmd;
4147 bool rc = false;
4148
4149 list_for_each_entry_safe(se_cmd, tmp_cmd,
4150 &se_sess->sess_wait_list, se_cmd_list) {
4151 list_del(&se_cmd->se_cmd_list);
4152
4153 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
4154 " %d\n", se_cmd, se_cmd->t_state,
4155 se_cmd->se_tfo->get_cmd_state(se_cmd));
4156
4157 if (wait_for_tasks) {
4158 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
4159 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4160 se_cmd->se_tfo->get_cmd_state(se_cmd));
4161
4162 rc = transport_wait_for_tasks(se_cmd);
4163
4164 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
4165 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4166 se_cmd->se_tfo->get_cmd_state(se_cmd));
4167 }
4168
4169 if (!rc) {
4170 wait_for_completion(&se_cmd->cmd_wait_comp);
4171 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
4172 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4173 se_cmd->se_tfo->get_cmd_state(se_cmd));
4174 }
4175
4176 se_cmd->se_tfo->release_cmd(se_cmd);
4177 }
4178}
4179EXPORT_SYMBOL(target_wait_for_sess_cmds);
4180
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004181/* transport_lun_wait_for_tasks():
4182 *
4183 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4184 * an struct se_lun to be successfully shutdown.
4185 */
4186static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4187{
4188 unsigned long flags;
4189 int ret;
4190 /*
4191 * If the frontend has already requested this struct se_cmd to
4192 * be stopped, we can safely ignore this struct se_cmd.
4193 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004194 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004195 if (cmd->transport_state & CMD_T_STOP) {
4196 cmd->transport_state &= ~CMD_T_LUN_STOP;
4197
4198 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
4199 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004200 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004201 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovere3d6f902011-07-19 08:55:10 +00004202 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004203 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004204 cmd->transport_state |= CMD_T_LUN_FE_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07004205 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004206
Andy Grover5951146d2011-07-19 10:26:37 +00004207 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004208
4209 ret = transport_stop_tasks_for_cmd(cmd);
4210
Andy Grover6708bb22011-06-08 10:36:43 -07004211 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4212 " %d\n", cmd, cmd->t_task_list_num, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004213 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07004214 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004215 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004216 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07004217 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004218 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004219 }
Christoph Hellwig3df8d402011-10-17 13:56:43 -04004220 transport_remove_cmd_from_queue(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004221
4222 return 0;
4223}
4224
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004225static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4226{
4227 struct se_cmd *cmd = NULL;
4228 unsigned long lun_flags, cmd_flags;
4229 /*
4230 * Do exception processing and return CHECK_CONDITION status to the
4231 * Initiator Port.
4232 */
4233 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00004234 while (!list_empty(&lun->lun_cmd_list)) {
4235 cmd = list_first_entry(&lun->lun_cmd_list,
4236 struct se_cmd, se_lun_node);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -05004237 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004238
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004239 /*
4240 * This will notify iscsi_target_transport.c:
4241 * transport_cmd_check_stop() that a LUN shutdown is in
4242 * progress for the iscsi_cmd_t.
4243 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004244 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07004245 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004246 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004247 cmd->se_lun->unpacked_lun,
4248 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004249 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07004250 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004251
4252 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4253
Andy Grover6708bb22011-06-08 10:36:43 -07004254 if (!cmd->se_lun) {
4255 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004256 cmd->se_tfo->get_task_tag(cmd),
4257 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004258 BUG();
4259 }
4260 /*
4261 * If the Storage engine still owns the iscsi_cmd_t, determine
4262 * and/or stop its context.
4263 */
Andy Grover6708bb22011-06-08 10:36:43 -07004264 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00004265 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4266 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004267
Andy Grovere3d6f902011-07-19 08:55:10 +00004268 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004269 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4270 continue;
4271 }
4272
Andy Grover6708bb22011-06-08 10:36:43 -07004273 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004274 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004275 cmd->se_lun->unpacked_lun,
4276 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004277
Andy Grovera1d8b492011-05-02 17:12:10 -07004278 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004279 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004280 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004281 goto check_cond;
4282 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004283 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004284 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07004285 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004286
4287 transport_free_dev_tasks(cmd);
4288 /*
4289 * The Storage engine stopped this struct se_cmd before it was
4290 * send to the fabric frontend for delivery back to the
4291 * Initiator Node. Return this SCSI CDB back with an
4292 * CHECK_CONDITION status.
4293 */
4294check_cond:
4295 transport_send_check_condition_and_sense(cmd,
4296 TCM_NON_EXISTENT_LUN, 0);
4297 /*
4298 * If the fabric frontend is waiting for this iscsi_cmd_t to
4299 * be released, notify the waiting thread now that LU has
4300 * finished accessing it.
4301 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004302 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004303 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07004304 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004305 " struct se_cmd: %p ITT: 0x%08x\n",
4306 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00004307 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004308
Andy Grovera1d8b492011-05-02 17:12:10 -07004309 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004310 cmd_flags);
4311 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -07004312 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004313 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4314 continue;
4315 }
Andy Grover6708bb22011-06-08 10:36:43 -07004316 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004317 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004318
Andy Grovera1d8b492011-05-02 17:12:10 -07004319 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004320 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4321 }
4322 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4323}
4324
4325static int transport_clear_lun_thread(void *p)
4326{
Jörn Engel8359cf42011-11-24 02:05:51 +01004327 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004328
4329 __transport_clear_lun_from_sessions(lun);
4330 complete(&lun->lun_shutdown_comp);
4331
4332 return 0;
4333}
4334
4335int transport_clear_lun_from_sessions(struct se_lun *lun)
4336{
4337 struct task_struct *kt;
4338
Andy Grover5951146d2011-07-19 10:26:37 +00004339 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004340 "tcm_cl_%u", lun->unpacked_lun);
4341 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07004342 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00004343 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004344 }
4345 wait_for_completion(&lun->lun_shutdown_comp);
4346
4347 return 0;
4348}
4349
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004350/**
4351 * transport_wait_for_tasks - wait for completion to occur
4352 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004353 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004354 * Called from frontend fabric context to wait for storage engine
4355 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004356 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004357bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004358{
4359 unsigned long flags;
4360
Andy Grovera1d8b492011-05-02 17:12:10 -07004361 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08004362 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
4363 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004364 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004365 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004366 }
4367 /*
4368 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4369 * has been set in transport_set_supported_SAM_opcode().
4370 */
Andy Groverc8e31f22012-01-19 13:39:17 -08004371 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
4372 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004373 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004374 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004375 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004376 /*
4377 * If we are already stopped due to an external event (ie: LUN shutdown)
4378 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07004379 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004380 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4381 * has completed its operation on the struct se_cmd.
4382 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004383 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07004384 pr_debug("wait_for_tasks: Stopping"
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 /*
4389 * There is a special case for WRITES where a FE exception +
4390 * LUN shutdown means ConfigFS context is still sleeping on
4391 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4392 * We go ahead and up transport_lun_stop_comp just to be sure
4393 * here.
4394 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004395 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4396 complete(&cmd->transport_lun_stop_comp);
4397 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4398 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004399
4400 transport_all_task_dev_remove_state(cmd);
4401 /*
4402 * At this point, the frontend who was the originator of this
4403 * struct se_cmd, now owns the structure and can be released through
4404 * normal means below.
4405 */
Andy Grover6708bb22011-06-08 10:36:43 -07004406 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00004407 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004408 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004409 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004410
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004411 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004412 }
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004413
Nicholas Bellinger3d289342012-02-13 02:38:14 -08004414 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004415 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004416 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004417 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004418
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004419 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004420
Andy Grover6708bb22011-06-08 10:36:43 -07004421 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004422 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004423 cmd, cmd->se_tfo->get_task_tag(cmd),
4424 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004425
Andy Grovera1d8b492011-05-02 17:12:10 -07004426 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004427
Andy Grover5951146d2011-07-19 10:26:37 +00004428 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004429
Andy Grovera1d8b492011-05-02 17:12:10 -07004430 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004431
Andy Grovera1d8b492011-05-02 17:12:10 -07004432 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004433 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004434
Andy Grover6708bb22011-06-08 10:36:43 -07004435 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07004436 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004437 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004438
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004439 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004440
4441 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004442}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004443EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004444
4445static int transport_get_sense_codes(
4446 struct se_cmd *cmd,
4447 u8 *asc,
4448 u8 *ascq)
4449{
4450 *asc = cmd->scsi_asc;
4451 *ascq = cmd->scsi_ascq;
4452
4453 return 0;
4454}
4455
4456static int transport_set_sense_codes(
4457 struct se_cmd *cmd,
4458 u8 asc,
4459 u8 ascq)
4460{
4461 cmd->scsi_asc = asc;
4462 cmd->scsi_ascq = ascq;
4463
4464 return 0;
4465}
4466
4467int transport_send_check_condition_and_sense(
4468 struct se_cmd *cmd,
4469 u8 reason,
4470 int from_transport)
4471{
4472 unsigned char *buffer = cmd->sense_buffer;
4473 unsigned long flags;
4474 int offset;
4475 u8 asc = 0, ascq = 0;
4476
Andy Grovera1d8b492011-05-02 17:12:10 -07004477 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004478 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004479 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004480 return 0;
4481 }
4482 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07004483 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004484
4485 if (!reason && from_transport)
4486 goto after_reason;
4487
4488 if (!from_transport)
4489 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4490 /*
4491 * Data Segment and SenseLength of the fabric response PDU.
4492 *
4493 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4494 * from include/scsi/scsi_cmnd.h
4495 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004496 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004497 TRANSPORT_SENSE_BUFFER);
4498 /*
4499 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4500 * SENSE KEY values from include/scsi/scsi.h
4501 */
4502 switch (reason) {
4503 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004504 /* CURRENT ERROR */
4505 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004506 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004507 /* ILLEGAL REQUEST */
4508 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4509 /* LOGICAL UNIT NOT SUPPORTED */
4510 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4511 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004512 case TCM_UNSUPPORTED_SCSI_OPCODE:
4513 case TCM_SECTOR_COUNT_TOO_MANY:
4514 /* CURRENT ERROR */
4515 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004516 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004517 /* ILLEGAL REQUEST */
4518 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4519 /* INVALID COMMAND OPERATION CODE */
4520 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4521 break;
4522 case TCM_UNKNOWN_MODE_PAGE:
4523 /* CURRENT ERROR */
4524 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004525 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004526 /* ILLEGAL REQUEST */
4527 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4528 /* INVALID FIELD IN CDB */
4529 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4530 break;
4531 case TCM_CHECK_CONDITION_ABORT_CMD:
4532 /* CURRENT ERROR */
4533 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004534 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004535 /* ABORTED COMMAND */
4536 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4537 /* BUS DEVICE RESET FUNCTION OCCURRED */
4538 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4539 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4540 break;
4541 case TCM_INCORRECT_AMOUNT_OF_DATA:
4542 /* CURRENT ERROR */
4543 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004544 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004545 /* ABORTED COMMAND */
4546 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4547 /* WRITE ERROR */
4548 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4549 /* NOT ENOUGH UNSOLICITED DATA */
4550 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4551 break;
4552 case TCM_INVALID_CDB_FIELD:
4553 /* CURRENT ERROR */
4554 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004555 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004556 /* ILLEGAL REQUEST */
4557 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004558 /* INVALID FIELD IN CDB */
4559 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4560 break;
4561 case TCM_INVALID_PARAMETER_LIST:
4562 /* CURRENT ERROR */
4563 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004564 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004565 /* ILLEGAL REQUEST */
4566 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004567 /* INVALID FIELD IN PARAMETER LIST */
4568 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4569 break;
4570 case TCM_UNEXPECTED_UNSOLICITED_DATA:
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 /* WRITE ERROR */
4577 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4578 /* UNEXPECTED_UNSOLICITED_DATA */
4579 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4580 break;
4581 case TCM_SERVICE_CRC_ERROR:
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 /* ABORTED COMMAND */
4586 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4587 /* PROTOCOL SERVICE CRC ERROR */
4588 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4589 /* N/A */
4590 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4591 break;
4592 case TCM_SNACK_REJECTED:
4593 /* CURRENT ERROR */
4594 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004595 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004596 /* ABORTED COMMAND */
4597 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4598 /* READ ERROR */
4599 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4600 /* FAILED RETRANSMISSION REQUEST */
4601 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4602 break;
4603 case TCM_WRITE_PROTECTED:
4604 /* CURRENT ERROR */
4605 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004606 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004607 /* DATA PROTECT */
4608 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4609 /* WRITE PROTECTED */
4610 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4611 break;
4612 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4613 /* CURRENT ERROR */
4614 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004615 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004616 /* UNIT ATTENTION */
4617 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4618 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4619 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4620 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4621 break;
4622 case TCM_CHECK_CONDITION_NOT_READY:
4623 /* CURRENT ERROR */
4624 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004625 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004626 /* Not Ready */
4627 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4628 transport_get_sense_codes(cmd, &asc, &ascq);
4629 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4630 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4631 break;
4632 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4633 default:
4634 /* CURRENT ERROR */
4635 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004636 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004637 /* ILLEGAL REQUEST */
4638 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4639 /* LOGICAL UNIT COMMUNICATION FAILURE */
4640 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4641 break;
4642 }
4643 /*
4644 * This code uses linux/include/scsi/scsi.h SAM status codes!
4645 */
4646 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4647 /*
4648 * Automatically padded, this value is encoded in the fabric's
4649 * data_length response PDU containing the SCSI defined sense data.
4650 */
4651 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4652
4653after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004654 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004655}
4656EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4657
4658int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4659{
4660 int ret = 0;
4661
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004662 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07004663 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004664 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4665 return 1;
4666#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004667 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004668 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07004669 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004670 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004671#endif
4672 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00004673 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004674 ret = 1;
4675 }
4676 return ret;
4677}
4678EXPORT_SYMBOL(transport_check_aborted_status);
4679
4680void transport_send_task_abort(struct se_cmd *cmd)
4681{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07004682 unsigned long flags;
4683
4684 spin_lock_irqsave(&cmd->t_state_lock, flags);
4685 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4686 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4687 return;
4688 }
4689 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4690
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004691 /*
4692 * If there are still expected incoming fabric WRITEs, we wait
4693 * until until they have completed before sending a TASK_ABORTED
4694 * response. This response with TASK_ABORTED status will be
4695 * queued back to fabric module by transport_check_aborted_status().
4696 */
4697 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004698 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f32011-12-21 14:13:47 -05004699 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004700 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004701 }
4702 }
4703 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4704#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004705 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07004706 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004707 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004708#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00004709 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004710}
4711
Christoph Hellwige26d99a2011-11-14 12:30:30 -05004712static int transport_generic_do_tmr(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004713{
Andy Grover5951146d2011-07-19 10:26:37 +00004714 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004715 struct se_tmr_req *tmr = cmd->se_tmr_req;
4716 int ret;
4717
4718 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004719 case TMR_ABORT_TASK:
Nicholas Bellinger3d289342012-02-13 02:38:14 -08004720 core_tmr_abort_task(dev, tmr, cmd->se_sess);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004721 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004722 case TMR_ABORT_TASK_SET:
4723 case TMR_CLEAR_ACA:
4724 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004725 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4726 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004727 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004728 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4729 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4730 TMR_FUNCTION_REJECTED;
4731 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004732 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004733 tmr->response = TMR_FUNCTION_REJECTED;
4734 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004735 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004736 tmr->response = TMR_FUNCTION_REJECTED;
4737 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004738 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004739 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004740 tmr->function);
4741 tmr->response = TMR_FUNCTION_REJECTED;
4742 break;
4743 }
4744
4745 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00004746 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004747
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04004748 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004749 return 0;
4750}
4751
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004752/* transport_processing_thread():
4753 *
4754 *
4755 */
4756static int transport_processing_thread(void *param)
4757{
Andy Grover5951146d2011-07-19 10:26:37 +00004758 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004759 struct se_cmd *cmd;
Jörn Engel8359cf42011-11-24 02:05:51 +01004760 struct se_device *dev = param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004761
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004762 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004763 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4764 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004765 kthread_should_stop());
4766 if (ret < 0)
4767 goto out;
4768
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004769get_cmd:
Andy Grover5951146d2011-07-19 10:26:37 +00004770 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4771 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004772 continue;
4773
Andy Grover5951146d2011-07-19 10:26:37 +00004774 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02004775 case TRANSPORT_NEW_CMD:
4776 BUG();
4777 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004778 case TRANSPORT_NEW_CMD_MAP:
Andy Grover6708bb22011-06-08 10:36:43 -07004779 if (!cmd->se_tfo->new_cmd_map) {
4780 pr_err("cmd->se_tfo->new_cmd_map is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004781 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4782 BUG();
4783 }
Andy Grovere3d6f902011-07-19 08:55:10 +00004784 ret = cmd->se_tfo->new_cmd_map(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004785 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004786 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004787 break;
4788 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004789 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004790 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004791 transport_generic_request_failure(cmd);
4792 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004793 }
4794 break;
4795 case TRANSPORT_PROCESS_WRITE:
4796 transport_generic_process_write(cmd);
4797 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004798 case TRANSPORT_PROCESS_TMR:
4799 transport_generic_do_tmr(cmd);
4800 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004801 case TRANSPORT_COMPLETE_QF_WP:
Christoph Hellwige057f532011-10-17 13:56:41 -04004802 transport_write_pending_qf(cmd);
4803 break;
4804 case TRANSPORT_COMPLETE_QF_OK:
4805 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004806 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004807 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004808 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4809 "i_state: %d on SE LUN: %u\n",
4810 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00004811 cmd->se_tfo->get_task_tag(cmd),
4812 cmd->se_tfo->get_cmd_state(cmd),
4813 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004814 BUG();
4815 }
4816
4817 goto get_cmd;
4818 }
4819
4820out:
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07004821 WARN_ON(!list_empty(&dev->state_task_list));
4822 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004823 dev->process_thread = NULL;
4824 return 0;
4825}