blob: 42da7a30072943a5c2a1b706b8f8150b5c44f053 [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>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040#include <asm/unaligned.h>
41#include <net/sock.h>
42#include <net/tcp.h>
43#include <scsi/scsi.h>
44#include <scsi/scsi_cmnd.h>
Nicholas Bellingere66ecd52011-05-19 20:19:14 -070045#include <scsi/scsi_tcq.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080046
47#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050048#include <target/target_core_backend.h>
49#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080050#include <target/target_core_configfs.h>
51
Christoph Hellwige26d99a2011-11-14 12:30:30 -050052#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080053#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080054#include "target_core_pr.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080055#include "target_core_ua.h"
56
Andy Grovere3d6f902011-07-19 08:55:10 +000057static int sub_api_initialized;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080058
Christoph Hellwig35e0e752011-10-17 13:56:53 -040059static struct workqueue_struct *target_completion_wq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080060static struct kmem_cache *se_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061struct kmem_cache *se_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062struct kmem_cache *t10_pr_reg_cache;
63struct kmem_cache *t10_alua_lu_gp_cache;
64struct kmem_cache *t10_alua_lu_gp_mem_cache;
65struct kmem_cache *t10_alua_tg_pt_gp_cache;
66struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
67
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080068static int transport_generic_write_pending(struct se_cmd *);
Andy Grover5951146d2011-07-19 10:26:37 +000069static int transport_processing_thread(void *param);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -080070static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080071static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070072static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040073 struct se_device *dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080074static void transport_free_dev_tasks(struct se_cmd *cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +000075static int transport_generic_get_mem(struct se_cmd *cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -070076static void transport_put_cmd(struct se_cmd *cmd);
Christoph Hellwig3df8d402011-10-17 13:56:43 -040077static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -070079static void transport_generic_request_failure(struct se_cmd *);
Christoph Hellwig35e0e752011-10-17 13:56:53 -040080static void target_complete_ok_work(struct work_struct *work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080081
Andy Grovere3d6f902011-07-19 08:55:10 +000082int init_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080084 se_sess_cache = kmem_cache_create("se_sess_cache",
85 sizeof(struct se_session), __alignof__(struct se_session),
86 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070087 if (!se_sess_cache) {
88 pr_err("kmem_cache_create() for struct se_session"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080089 " failed\n");
Andy Groverc8e31f22012-01-19 13:39:17 -080090 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080091 }
92 se_ua_cache = kmem_cache_create("se_ua_cache",
93 sizeof(struct se_ua), __alignof__(struct se_ua),
94 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070095 if (!se_ua_cache) {
96 pr_err("kmem_cache_create() for struct se_ua failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -040097 goto out_free_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080098 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080099 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
100 sizeof(struct t10_pr_registration),
101 __alignof__(struct t10_pr_registration), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700102 if (!t10_pr_reg_cache) {
103 pr_err("kmem_cache_create() for struct t10_pr_registration"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800104 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400105 goto out_free_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800106 }
107 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
108 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
109 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700110 if (!t10_alua_lu_gp_cache) {
111 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800112 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400113 goto out_free_pr_reg_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800114 }
115 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
116 sizeof(struct t10_alua_lu_gp_member),
117 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700118 if (!t10_alua_lu_gp_mem_cache) {
119 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800120 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400121 goto out_free_lu_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800122 }
123 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
124 sizeof(struct t10_alua_tg_pt_gp),
125 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700126 if (!t10_alua_tg_pt_gp_cache) {
127 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800128 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400129 goto out_free_lu_gp_mem_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800130 }
131 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
132 "t10_alua_tg_pt_gp_mem_cache",
133 sizeof(struct t10_alua_tg_pt_gp_member),
134 __alignof__(struct t10_alua_tg_pt_gp_member),
135 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700136 if (!t10_alua_tg_pt_gp_mem_cache) {
137 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800138 "mem_t failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400139 goto out_free_tg_pt_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800140 }
141
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400142 target_completion_wq = alloc_workqueue("target_completion",
143 WQ_MEM_RECLAIM, 0);
144 if (!target_completion_wq)
145 goto out_free_tg_pt_gp_mem_cache;
146
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800147 return 0;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400148
149out_free_tg_pt_gp_mem_cache:
150 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
151out_free_tg_pt_gp_cache:
152 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
153out_free_lu_gp_mem_cache:
154 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
155out_free_lu_gp_cache:
156 kmem_cache_destroy(t10_alua_lu_gp_cache);
157out_free_pr_reg_cache:
158 kmem_cache_destroy(t10_pr_reg_cache);
159out_free_ua_cache:
160 kmem_cache_destroy(se_ua_cache);
161out_free_sess_cache:
162 kmem_cache_destroy(se_sess_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800163out:
Andy Grovere3d6f902011-07-19 08:55:10 +0000164 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800165}
166
Andy Grovere3d6f902011-07-19 08:55:10 +0000167void release_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800168{
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400169 destroy_workqueue(target_completion_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800170 kmem_cache_destroy(se_sess_cache);
171 kmem_cache_destroy(se_ua_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800172 kmem_cache_destroy(t10_pr_reg_cache);
173 kmem_cache_destroy(t10_alua_lu_gp_cache);
174 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
175 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
176 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800177}
178
Andy Grovere3d6f902011-07-19 08:55:10 +0000179/* This code ensures unique mib indexes are handed out. */
180static DEFINE_SPINLOCK(scsi_mib_index_lock);
181static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800182
183/*
184 * Allocate a new row index for the entry type specified
185 */
186u32 scsi_get_new_index(scsi_index_t type)
187{
188 u32 new_index;
189
Andy Grovere3d6f902011-07-19 08:55:10 +0000190 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800191
Andy Grovere3d6f902011-07-19 08:55:10 +0000192 spin_lock(&scsi_mib_index_lock);
193 new_index = ++scsi_mib_index[type];
194 spin_unlock(&scsi_mib_index_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800195
196 return new_index;
197}
198
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500199static void transport_init_queue_obj(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800200{
201 atomic_set(&qobj->queue_cnt, 0);
202 INIT_LIST_HEAD(&qobj->qobj_list);
203 init_waitqueue_head(&qobj->thread_wq);
204 spin_lock_init(&qobj->cmd_queue_lock);
205}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800206
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700207void transport_subsystem_check_init(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800208{
209 int ret;
210
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700211 if (sub_api_initialized)
212 return;
213
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214 ret = request_module("target_core_iblock");
215 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700216 pr_err("Unable to load target_core_iblock\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800217
218 ret = request_module("target_core_file");
219 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700220 pr_err("Unable to load target_core_file\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800221
222 ret = request_module("target_core_pscsi");
223 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700224 pr_err("Unable to load target_core_pscsi\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800225
226 ret = request_module("target_core_stgt");
227 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700228 pr_err("Unable to load target_core_stgt\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229
Andy Grovere3d6f902011-07-19 08:55:10 +0000230 sub_api_initialized = 1;
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700231 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800232}
233
234struct se_session *transport_init_session(void)
235{
236 struct se_session *se_sess;
237
238 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700239 if (!se_sess) {
240 pr_err("Unable to allocate struct se_session from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800241 " se_sess_cache\n");
242 return ERR_PTR(-ENOMEM);
243 }
244 INIT_LIST_HEAD(&se_sess->sess_list);
245 INIT_LIST_HEAD(&se_sess->sess_acl_list);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700246 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
247 INIT_LIST_HEAD(&se_sess->sess_wait_list);
248 spin_lock_init(&se_sess->sess_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800249
250 return se_sess;
251}
252EXPORT_SYMBOL(transport_init_session);
253
254/*
255 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
256 */
257void __transport_register_session(
258 struct se_portal_group *se_tpg,
259 struct se_node_acl *se_nacl,
260 struct se_session *se_sess,
261 void *fabric_sess_ptr)
262{
263 unsigned char buf[PR_REG_ISID_LEN];
264
265 se_sess->se_tpg = se_tpg;
266 se_sess->fabric_sess_ptr = fabric_sess_ptr;
267 /*
268 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
269 *
270 * Only set for struct se_session's that will actually be moving I/O.
271 * eg: *NOT* discovery sessions.
272 */
273 if (se_nacl) {
274 /*
275 * If the fabric module supports an ISID based TransportID,
276 * save this value in binary from the fabric I_T Nexus now.
277 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000278 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800279 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000280 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800281 &buf[0], PR_REG_ISID_LEN);
282 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
283 }
284 spin_lock_irq(&se_nacl->nacl_sess_lock);
285 /*
286 * The se_nacl->nacl_sess pointer will be set to the
287 * last active I_T Nexus for each struct se_node_acl.
288 */
289 se_nacl->nacl_sess = se_sess;
290
291 list_add_tail(&se_sess->sess_acl_list,
292 &se_nacl->acl_sess_list);
293 spin_unlock_irq(&se_nacl->nacl_sess_lock);
294 }
295 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
296
Andy Grover6708bb22011-06-08 10:36:43 -0700297 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000298 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800299}
300EXPORT_SYMBOL(__transport_register_session);
301
302void transport_register_session(
303 struct se_portal_group *se_tpg,
304 struct se_node_acl *se_nacl,
305 struct se_session *se_sess,
306 void *fabric_sess_ptr)
307{
308 spin_lock_bh(&se_tpg->session_lock);
309 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
310 spin_unlock_bh(&se_tpg->session_lock);
311}
312EXPORT_SYMBOL(transport_register_session);
313
314void transport_deregister_session_configfs(struct se_session *se_sess)
315{
316 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700317 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800318 /*
319 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
320 */
321 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700322 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700323 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800324 list_del(&se_sess->sess_acl_list);
325 /*
326 * If the session list is empty, then clear the pointer.
327 * Otherwise, set the struct se_session pointer from the tail
328 * element of the per struct se_node_acl active session list.
329 */
330 if (list_empty(&se_nacl->acl_sess_list))
331 se_nacl->nacl_sess = NULL;
332 else {
333 se_nacl->nacl_sess = container_of(
334 se_nacl->acl_sess_list.prev,
335 struct se_session, sess_acl_list);
336 }
Roland Dreier23388862011-06-22 01:02:21 -0700337 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800338 }
339}
340EXPORT_SYMBOL(transport_deregister_session_configfs);
341
342void transport_free_session(struct se_session *se_sess)
343{
344 kmem_cache_free(se_sess_cache, se_sess);
345}
346EXPORT_SYMBOL(transport_free_session);
347
348void transport_deregister_session(struct se_session *se_sess)
349{
350 struct se_portal_group *se_tpg = se_sess->se_tpg;
351 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700352 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800353
Andy Grover6708bb22011-06-08 10:36:43 -0700354 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800355 transport_free_session(se_sess);
356 return;
357 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800358
Roland Dreiere63a8e12011-08-12 16:01:02 -0700359 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800360 list_del(&se_sess->sess_list);
361 se_sess->se_tpg = NULL;
362 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700363 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800364
365 /*
366 * Determine if we need to do extra work for this initiator node's
367 * struct se_node_acl if it had been previously dynamically generated.
368 */
369 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700370 if (se_nacl) {
Roland Dreiere63a8e12011-08-12 16:01:02 -0700371 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800372 if (se_nacl->dynamic_node_acl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700373 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
374 se_tpg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800375 list_del(&se_nacl->acl_list);
376 se_tpg->num_node_acls--;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700377 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800378
379 core_tpg_wait_for_nacl_pr_ref(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380 core_free_device_list_for_node(se_nacl, se_tpg);
Andy Grovere3d6f902011-07-19 08:55:10 +0000381 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800382 se_nacl);
Roland Dreiere63a8e12011-08-12 16:01:02 -0700383 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800384 }
385 }
Roland Dreiere63a8e12011-08-12 16:01:02 -0700386 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800387 }
388
389 transport_free_session(se_sess);
390
Andy Grover6708bb22011-06-08 10:36:43 -0700391 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000392 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800393}
394EXPORT_SYMBOL(transport_deregister_session);
395
396/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700397 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398 */
399static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
400{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400401 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 struct se_task *task;
403 unsigned long flags;
404
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400405 if (!dev)
406 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800407
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400408 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400409 if (task->task_flags & TF_ACTIVE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800410 continue;
411
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500413 if (task->t_state_active) {
414 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
415 cmd->se_tfo->get_task_tag(cmd), dev, task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800416
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500417 list_del(&task->t_state_list);
418 atomic_dec(&cmd->t_task_cdbs_ex_left);
419 task->t_state_active = false;
420 }
421 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800422 }
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500423
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800424}
425
426/* transport_cmd_check_stop():
427 *
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500428 * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800429 * 'transport_off = 2' determines if task_dev_state should be removed.
430 *
431 * A non-zero u8 t_state sets cmd->t_state.
432 * Returns 1 when command is stopped, else 0.
433 */
434static int transport_cmd_check_stop(
435 struct se_cmd *cmd,
436 int transport_off,
437 u8 t_state)
438{
439 unsigned long flags;
440
Andy Grovera1d8b492011-05-02 17:12:10 -0700441 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800442 /*
443 * Determine if IOCTL context caller in requesting the stopping of this
444 * command for LUN shutdown purposes.
445 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500446 if (cmd->transport_state & CMD_T_LUN_STOP) {
447 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
448 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800449
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500450 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451 if (transport_off == 2)
452 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700453 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454
Andy Grovera1d8b492011-05-02 17:12:10 -0700455 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800456 return 1;
457 }
458 /*
459 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000460 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800461 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500462 if (cmd->transport_state & CMD_T_STOP) {
463 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
464 __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000465 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800466
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800467 if (transport_off == 2)
468 transport_all_task_dev_remove_state(cmd);
469
470 /*
471 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
472 * to FE.
473 */
474 if (transport_off == 2)
475 cmd->se_lun = NULL;
Andy Grovera1d8b492011-05-02 17:12:10 -0700476 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800477
Andy Grovera1d8b492011-05-02 17:12:10 -0700478 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479 return 1;
480 }
481 if (transport_off) {
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500482 cmd->transport_state &= ~CMD_T_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800483 if (transport_off == 2) {
484 transport_all_task_dev_remove_state(cmd);
485 /*
486 * Clear struct se_cmd->se_lun before the transport_off == 2
487 * handoff to fabric module.
488 */
489 cmd->se_lun = NULL;
490 /*
491 * Some fabric modules like tcm_loop can release
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300492 * their internally allocated I/O reference now and
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800493 * struct se_cmd now.
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700494 *
495 * Fabric modules are expected to return '1' here if the
496 * se_cmd being passed is released at this point,
497 * or zero if not being released.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800498 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000499 if (cmd->se_tfo->check_stop_free != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800500 spin_unlock_irqrestore(
Andy Grovera1d8b492011-05-02 17:12:10 -0700501 &cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800502
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700503 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800504 }
505 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700506 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800507
508 return 0;
509 } else if (t_state)
510 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700511 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800512
513 return 0;
514}
515
516static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
517{
518 return transport_cmd_check_stop(cmd, 2, 0);
519}
520
521static void transport_lun_remove_cmd(struct se_cmd *cmd)
522{
Andy Grovere3d6f902011-07-19 08:55:10 +0000523 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800524 unsigned long flags;
525
526 if (!lun)
527 return;
528
Andy Grovera1d8b492011-05-02 17:12:10 -0700529 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500530 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
531 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
532 transport_all_task_dev_remove_state(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800533 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700534 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800536 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -0500537 if (!list_empty(&cmd->se_lun_node))
538 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800539 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
540}
541
542void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
543{
Andy Groverc8e31f22012-01-19 13:39:17 -0800544 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700545 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800546
547 if (transport_cmd_check_stop_to_fabric(cmd))
548 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700549 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400550 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200551 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700552 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553}
554
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400555static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
556 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557{
558 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000559 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800560 unsigned long flags;
561
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800562 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700563 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800564 cmd->t_state = t_state;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500565 cmd->transport_state |= CMD_T_ACTIVE;
Andy Grovera1d8b492011-05-02 17:12:10 -0700566 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800567 }
568
569 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700570
571 /* If the cmd is already on the list, remove it before we add it */
572 if (!list_empty(&cmd->se_queue_node))
573 list_del(&cmd->se_queue_node);
574 else
575 atomic_inc(&qobj->queue_cnt);
576
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400577 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700578 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400579 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700580 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500581 cmd->transport_state |= CMD_T_QUEUED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800582 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
583
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800584 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800585}
586
Andy Grover5951146d2011-07-19 10:26:37 +0000587static struct se_cmd *
588transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800589{
Andy Grover5951146d2011-07-19 10:26:37 +0000590 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800591 unsigned long flags;
592
593 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
594 if (list_empty(&qobj->qobj_list)) {
595 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
596 return NULL;
597 }
Andy Grover5951146d2011-07-19 10:26:37 +0000598 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800599
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500600 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700601 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800602 atomic_dec(&qobj->queue_cnt);
603 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
604
Andy Grover5951146d2011-07-19 10:26:37 +0000605 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800606}
607
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400608static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800609{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400610 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800611 unsigned long flags;
612
613 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500614 if (!(cmd->transport_state & CMD_T_QUEUED)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800615 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
616 return;
617 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500618 cmd->transport_state &= ~CMD_T_QUEUED;
Roland Dreier79a7fef2011-09-28 22:12:07 -0700619 atomic_dec(&qobj->queue_cnt);
620 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800621 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800622}
623
624/*
625 * Completion function used by TCM subsystem plugins (such as FILEIO)
626 * for queueing up response from struct se_subsystem_api->do_task()
627 */
628void transport_complete_sync_cache(struct se_cmd *cmd, int good)
629{
Andy Grovera1d8b492011-05-02 17:12:10 -0700630 struct se_task *task = list_entry(cmd->t_task_list.next,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631 struct se_task, t_list);
632
633 if (good) {
634 cmd->scsi_status = SAM_STAT_GOOD;
635 task->task_scsi_status = GOOD;
636 } else {
637 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700638 task->task_se_cmd->scsi_sense_reason =
639 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
640
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800641 }
642
643 transport_complete_task(task, good);
644}
645EXPORT_SYMBOL(transport_complete_sync_cache);
646
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400647static void target_complete_failure_work(struct work_struct *work)
648{
649 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
650
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700651 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400652}
653
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800654/* transport_complete_task():
655 *
656 * Called from interrupt and non interrupt context depending
657 * on the transport plugin.
658 */
659void transport_complete_task(struct se_task *task, int success)
660{
Andy Grovere3d6f902011-07-19 08:55:10 +0000661 struct se_cmd *cmd = task->task_se_cmd;
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400662 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800663 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800664
Andy Grovera1d8b492011-05-02 17:12:10 -0700665 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400666 task->task_flags &= ~TF_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800667
668 /*
669 * See if any sense data exists, if so set the TASK_SENSE flag.
670 * Also check for any other post completion work that needs to be
671 * done by the plugins.
672 */
673 if (dev && dev->transport->transport_complete) {
674 if (dev->transport->transport_complete(task) != 0) {
675 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
Christoph Hellwigef804a82011-11-23 06:53:58 -0500676 task->task_flags |= TF_HAS_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800677 success = 1;
678 }
679 }
680
681 /*
682 * See if we are waiting for outstanding struct se_task
683 * to complete for an exception condition
684 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400685 if (task->task_flags & TF_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700686 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800687 complete(&task->task_stop_comp);
688 return;
689 }
Christoph Hellwig2235007c2011-11-02 05:06:35 -0700690
691 if (!success)
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500692 cmd->transport_state |= CMD_T_FAILED;
Christoph Hellwig2235007c2011-11-02 05:06:35 -0700693
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800694 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800695 * Decrement the outstanding t_task_cdbs_left count. The last
696 * struct se_task from struct se_cmd will complete itself into the
697 * device queue depending upon int success.
698 */
Andy Grover6708bb22011-06-08 10:36:43 -0700699 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700700 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800701 return;
702 }
703
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500704 if (cmd->transport_state & CMD_T_FAILED) {
Christoph Hellwig41e16e92011-11-23 06:54:15 -0500705 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400706 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800707 } else {
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500708 cmd->transport_state |= CMD_T_COMPLETE;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400709 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800710 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400711
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400712 cmd->t_state = TRANSPORT_COMPLETE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -0500713 cmd->transport_state |= CMD_T_ACTIVE;
Andy Grovera1d8b492011-05-02 17:12:10 -0700714 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800715
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400716 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800717}
718EXPORT_SYMBOL(transport_complete_task);
719
720/*
721 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
722 * struct se_task list are ready to be added to the active execution list
723 * struct se_device
724
725 * Called with se_dev_t->execute_task_lock called.
726 */
727static inline int transport_add_task_check_sam_attr(
728 struct se_task *task,
729 struct se_task *task_prev,
730 struct se_device *dev)
731{
732 /*
733 * No SAM Task attribute emulation enabled, add to tail of
734 * execution queue
735 */
736 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
737 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
738 return 0;
739 }
740 /*
741 * HEAD_OF_QUEUE attribute for received CDB, which means
742 * the first task that is associated with a struct se_cmd goes to
743 * head of the struct se_device->execute_task_list, and task_prev
744 * after that for each subsequent task
745 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -0700746 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800747 list_add(&task->t_execute_list,
748 (task_prev != NULL) ?
749 &task_prev->t_execute_list :
750 &dev->execute_task_list);
751
Andy Grover6708bb22011-06-08 10:36:43 -0700752 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800753 " in execution queue\n",
Andy Grover6708bb22011-06-08 10:36:43 -0700754 task->task_se_cmd->t_task_cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800755 return 1;
756 }
757 /*
758 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
759 * transitioned from Dermant -> Active state, and are added to the end
760 * of the struct se_device->execute_task_list
761 */
762 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
763 return 0;
764}
765
766/* __transport_add_task_to_execute_queue():
767 *
768 * Called with se_dev_t->execute_task_lock called.
769 */
770static void __transport_add_task_to_execute_queue(
771 struct se_task *task,
772 struct se_task *task_prev,
773 struct se_device *dev)
774{
775 int head_of_queue;
776
777 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
778 atomic_inc(&dev->execute_tasks);
779
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500780 if (task->t_state_active)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800781 return;
782 /*
783 * Determine if this task needs to go to HEAD_OF_QUEUE for the
784 * state list as well. Running with SAM Task Attribute emulation
785 * will always return head_of_queue == 0 here
786 */
787 if (head_of_queue)
788 list_add(&task->t_state_list, (task_prev) ?
789 &task_prev->t_state_list :
790 &dev->state_task_list);
791 else
792 list_add_tail(&task->t_state_list, &dev->state_task_list);
793
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500794 task->t_state_active = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800795
Andy Grover6708bb22011-06-08 10:36:43 -0700796 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000797 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800798 task, dev);
799}
800
801static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
802{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400803 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800804 struct se_task *task;
805 unsigned long flags;
806
Andy Grovera1d8b492011-05-02 17:12:10 -0700807 spin_lock_irqsave(&cmd->t_state_lock, flags);
808 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800809 spin_lock(&dev->execute_task_lock);
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500810 if (!task->t_state_active) {
811 list_add_tail(&task->t_state_list,
812 &dev->state_task_list);
813 task->t_state_active = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800814
Christoph Hellwig1880807a2011-11-23 06:54:36 -0500815 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
816 task->task_se_cmd->se_tfo->get_task_tag(
817 task->task_se_cmd), task, dev);
818 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800819 spin_unlock(&dev->execute_task_lock);
820 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700821 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800822}
823
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800824static void __transport_add_tasks_from_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800825{
Andy Grover5951146d2011-07-19 10:26:37 +0000826 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800827 struct se_task *task, *task_prev = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800828
Andy Grovera1d8b492011-05-02 17:12:10 -0700829 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -0400830 if (!list_empty(&task->t_execute_list))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831 continue;
832 /*
833 * __transport_add_task_to_execute_queue() handles the
834 * SAM Task Attribute emulation if enabled
835 */
836 __transport_add_task_to_execute_queue(task, task_prev, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800837 task_prev = task;
838 }
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -0800839}
840
841static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
842{
843 unsigned long flags;
844 struct se_device *dev = cmd->se_dev;
845
846 spin_lock_irqsave(&dev->execute_task_lock, flags);
847 __transport_add_tasks_from_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800848 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800849}
850
Christoph Hellwig04629b72011-10-12 11:07:04 -0400851void __transport_remove_task_from_execute_queue(struct se_task *task,
852 struct se_device *dev)
853{
854 list_del_init(&task->t_execute_list);
855 atomic_dec(&dev->execute_tasks);
856}
857
Christoph Hellwige26d99a2011-11-14 12:30:30 -0500858static void transport_remove_task_from_execute_queue(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800859 struct se_task *task,
860 struct se_device *dev)
861{
862 unsigned long flags;
863
Christoph Hellwig04629b72011-10-12 11:07:04 -0400864 if (WARN_ON(list_empty(&task->t_execute_list)))
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700865 return;
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700866
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800867 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig04629b72011-10-12 11:07:04 -0400868 __transport_remove_task_from_execute_queue(task, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800869 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
870}
871
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700872/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700873 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700874 */
875
876static void target_qf_do_work(struct work_struct *work)
877{
878 struct se_device *dev = container_of(work, struct se_device,
879 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700880 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700881 struct se_cmd *cmd, *cmd_tmp;
882
883 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700884 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
885 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700886
Roland Dreierbcac3642011-08-27 21:33:16 -0700887 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700888 list_del(&cmd->se_qf_node);
889 atomic_dec(&dev->dev_qf_count);
890 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700891
Andy Grover6708bb22011-06-08 10:36:43 -0700892 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700893 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400894 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700895 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
896 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400897
898 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700899 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700900}
901
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800902unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
903{
904 switch (cmd->data_direction) {
905 case DMA_NONE:
906 return "NONE";
907 case DMA_FROM_DEVICE:
908 return "READ";
909 case DMA_TO_DEVICE:
910 return "WRITE";
911 case DMA_BIDIRECTIONAL:
912 return "BIDI";
913 default:
914 break;
915 }
916
917 return "UNKNOWN";
918}
919
920void transport_dump_dev_state(
921 struct se_device *dev,
922 char *b,
923 int *bl)
924{
925 *bl += sprintf(b + *bl, "Status: ");
926 switch (dev->dev_status) {
927 case TRANSPORT_DEVICE_ACTIVATED:
928 *bl += sprintf(b + *bl, "ACTIVATED");
929 break;
930 case TRANSPORT_DEVICE_DEACTIVATED:
931 *bl += sprintf(b + *bl, "DEACTIVATED");
932 break;
933 case TRANSPORT_DEVICE_SHUTDOWN:
934 *bl += sprintf(b + *bl, "SHUTDOWN");
935 break;
936 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
937 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
938 *bl += sprintf(b + *bl, "OFFLINE");
939 break;
940 default:
941 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
942 break;
943 }
944
Nicholas Bellinger65586d52011-11-30 01:25:21 -0800945 *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
946 atomic_read(&dev->execute_tasks), dev->queue_depth);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800947 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000948 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 -0800949 *bl += sprintf(b + *bl, " ");
950}
951
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800952void transport_dump_vpd_proto_id(
953 struct t10_vpd *vpd,
954 unsigned char *p_buf,
955 int p_buf_len)
956{
957 unsigned char buf[VPD_TMP_BUF_SIZE];
958 int len;
959
960 memset(buf, 0, VPD_TMP_BUF_SIZE);
961 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
962
963 switch (vpd->protocol_identifier) {
964 case 0x00:
965 sprintf(buf+len, "Fibre Channel\n");
966 break;
967 case 0x10:
968 sprintf(buf+len, "Parallel SCSI\n");
969 break;
970 case 0x20:
971 sprintf(buf+len, "SSA\n");
972 break;
973 case 0x30:
974 sprintf(buf+len, "IEEE 1394\n");
975 break;
976 case 0x40:
977 sprintf(buf+len, "SCSI Remote Direct Memory Access"
978 " Protocol\n");
979 break;
980 case 0x50:
981 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
982 break;
983 case 0x60:
984 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
985 break;
986 case 0x70:
987 sprintf(buf+len, "Automation/Drive Interface Transport"
988 " Protocol\n");
989 break;
990 case 0x80:
991 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
992 break;
993 default:
994 sprintf(buf+len, "Unknown 0x%02x\n",
995 vpd->protocol_identifier);
996 break;
997 }
998
999 if (p_buf)
1000 strncpy(p_buf, buf, p_buf_len);
1001 else
Andy Grover6708bb22011-06-08 10:36:43 -07001002 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001003}
1004
1005void
1006transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1007{
1008 /*
1009 * Check if the Protocol Identifier Valid (PIV) bit is set..
1010 *
1011 * from spc3r23.pdf section 7.5.1
1012 */
1013 if (page_83[1] & 0x80) {
1014 vpd->protocol_identifier = (page_83[0] & 0xf0);
1015 vpd->protocol_identifier_set = 1;
1016 transport_dump_vpd_proto_id(vpd, NULL, 0);
1017 }
1018}
1019EXPORT_SYMBOL(transport_set_vpd_proto_id);
1020
1021int transport_dump_vpd_assoc(
1022 struct t10_vpd *vpd,
1023 unsigned char *p_buf,
1024 int p_buf_len)
1025{
1026 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001027 int ret = 0;
1028 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001029
1030 memset(buf, 0, VPD_TMP_BUF_SIZE);
1031 len = sprintf(buf, "T10 VPD Identifier Association: ");
1032
1033 switch (vpd->association) {
1034 case 0x00:
1035 sprintf(buf+len, "addressed logical unit\n");
1036 break;
1037 case 0x10:
1038 sprintf(buf+len, "target port\n");
1039 break;
1040 case 0x20:
1041 sprintf(buf+len, "SCSI target device\n");
1042 break;
1043 default:
1044 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +00001045 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001046 break;
1047 }
1048
1049 if (p_buf)
1050 strncpy(p_buf, buf, p_buf_len);
1051 else
Andy Grover6708bb22011-06-08 10:36:43 -07001052 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001053
1054 return ret;
1055}
1056
1057int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1058{
1059 /*
1060 * The VPD identification association..
1061 *
1062 * from spc3r23.pdf Section 7.6.3.1 Table 297
1063 */
1064 vpd->association = (page_83[1] & 0x30);
1065 return transport_dump_vpd_assoc(vpd, NULL, 0);
1066}
1067EXPORT_SYMBOL(transport_set_vpd_assoc);
1068
1069int transport_dump_vpd_ident_type(
1070 struct t10_vpd *vpd,
1071 unsigned char *p_buf,
1072 int p_buf_len)
1073{
1074 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001075 int ret = 0;
1076 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001077
1078 memset(buf, 0, VPD_TMP_BUF_SIZE);
1079 len = sprintf(buf, "T10 VPD Identifier Type: ");
1080
1081 switch (vpd->device_identifier_type) {
1082 case 0x00:
1083 sprintf(buf+len, "Vendor specific\n");
1084 break;
1085 case 0x01:
1086 sprintf(buf+len, "T10 Vendor ID based\n");
1087 break;
1088 case 0x02:
1089 sprintf(buf+len, "EUI-64 based\n");
1090 break;
1091 case 0x03:
1092 sprintf(buf+len, "NAA\n");
1093 break;
1094 case 0x04:
1095 sprintf(buf+len, "Relative target port identifier\n");
1096 break;
1097 case 0x08:
1098 sprintf(buf+len, "SCSI name string\n");
1099 break;
1100 default:
1101 sprintf(buf+len, "Unsupported: 0x%02x\n",
1102 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +00001103 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001104 break;
1105 }
1106
Andy Grovere3d6f902011-07-19 08:55:10 +00001107 if (p_buf) {
1108 if (p_buf_len < strlen(buf)+1)
1109 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001110 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +00001111 } else {
Andy Grover6708bb22011-06-08 10:36:43 -07001112 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +00001113 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001114
1115 return ret;
1116}
1117
1118int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1119{
1120 /*
1121 * The VPD identifier type..
1122 *
1123 * from spc3r23.pdf Section 7.6.3.1 Table 298
1124 */
1125 vpd->device_identifier_type = (page_83[1] & 0x0f);
1126 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1127}
1128EXPORT_SYMBOL(transport_set_vpd_ident_type);
1129
1130int transport_dump_vpd_ident(
1131 struct t10_vpd *vpd,
1132 unsigned char *p_buf,
1133 int p_buf_len)
1134{
1135 unsigned char buf[VPD_TMP_BUF_SIZE];
1136 int ret = 0;
1137
1138 memset(buf, 0, VPD_TMP_BUF_SIZE);
1139
1140 switch (vpd->device_identifier_code_set) {
1141 case 0x01: /* Binary */
1142 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1143 &vpd->device_identifier[0]);
1144 break;
1145 case 0x02: /* ASCII */
1146 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1147 &vpd->device_identifier[0]);
1148 break;
1149 case 0x03: /* UTF-8 */
1150 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1151 &vpd->device_identifier[0]);
1152 break;
1153 default:
1154 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1155 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +00001156 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001157 break;
1158 }
1159
1160 if (p_buf)
1161 strncpy(p_buf, buf, p_buf_len);
1162 else
Andy Grover6708bb22011-06-08 10:36:43 -07001163 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001164
1165 return ret;
1166}
1167
1168int
1169transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1170{
1171 static const char hex_str[] = "0123456789abcdef";
1172 int j = 0, i = 4; /* offset to start of the identifer */
1173
1174 /*
1175 * The VPD Code Set (encoding)
1176 *
1177 * from spc3r23.pdf Section 7.6.3.1 Table 296
1178 */
1179 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1180 switch (vpd->device_identifier_code_set) {
1181 case 0x01: /* Binary */
1182 vpd->device_identifier[j++] =
1183 hex_str[vpd->device_identifier_type];
1184 while (i < (4 + page_83[3])) {
1185 vpd->device_identifier[j++] =
1186 hex_str[(page_83[i] & 0xf0) >> 4];
1187 vpd->device_identifier[j++] =
1188 hex_str[page_83[i] & 0x0f];
1189 i++;
1190 }
1191 break;
1192 case 0x02: /* ASCII */
1193 case 0x03: /* UTF-8 */
1194 while (i < (4 + page_83[3]))
1195 vpd->device_identifier[j++] = page_83[i++];
1196 break;
1197 default:
1198 break;
1199 }
1200
1201 return transport_dump_vpd_ident(vpd, NULL, 0);
1202}
1203EXPORT_SYMBOL(transport_set_vpd_ident);
1204
1205static void core_setup_task_attr_emulation(struct se_device *dev)
1206{
1207 /*
1208 * If this device is from Target_Core_Mod/pSCSI, disable the
1209 * SAM Task Attribute emulation.
1210 *
1211 * This is currently not available in upsream Linux/SCSI Target
1212 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1213 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001214 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001215 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1216 return;
1217 }
1218
1219 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001220 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001221 " device\n", dev->transport->name,
1222 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001223}
1224
1225static void scsi_dump_inquiry(struct se_device *dev)
1226{
Andy Grovere3d6f902011-07-19 08:55:10 +00001227 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001228 char buf[17];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229 int i, device_type;
1230 /*
1231 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1232 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001233 for (i = 0; i < 8; i++)
1234 if (wwn->vendor[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001235 buf[i] = wwn->vendor[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001236 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001237 buf[i] = ' ';
1238 buf[i] = '\0';
1239 pr_debug(" Vendor: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001240
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001241 for (i = 0; i < 16; i++)
1242 if (wwn->model[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001243 buf[i] = wwn->model[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001244 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001245 buf[i] = ' ';
1246 buf[i] = '\0';
1247 pr_debug(" Model: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001248
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001249 for (i = 0; i < 4; i++)
1250 if (wwn->revision[i] >= 0x20)
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001251 buf[i] = wwn->revision[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001252 else
Sebastian Andrzej Siewiore59a41b2012-01-10 14:16:57 +01001253 buf[i] = ' ';
1254 buf[i] = '\0';
1255 pr_debug(" Revision: %s\n", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001256
Andy Grovere3d6f902011-07-19 08:55:10 +00001257 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001258 pr_debug(" Type: %s ", scsi_device_type(device_type));
1259 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001260 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001261}
1262
1263struct se_device *transport_add_device_to_core_hba(
1264 struct se_hba *hba,
1265 struct se_subsystem_api *transport,
1266 struct se_subsystem_dev *se_dev,
1267 u32 device_flags,
1268 void *transport_dev,
1269 struct se_dev_limits *dev_limits,
1270 const char *inquiry_prod,
1271 const char *inquiry_rev)
1272{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001273 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001274 struct se_device *dev;
1275
1276 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001277 if (!dev) {
1278 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001279 return NULL;
1280 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001281
Andy Grovere3d6f902011-07-19 08:55:10 +00001282 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001283 dev->dev_flags = device_flags;
1284 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001285 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286 dev->se_hba = hba;
1287 dev->se_sub_dev = se_dev;
1288 dev->transport = transport;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001289 INIT_LIST_HEAD(&dev->dev_list);
1290 INIT_LIST_HEAD(&dev->dev_sep_list);
1291 INIT_LIST_HEAD(&dev->dev_tmr_list);
1292 INIT_LIST_HEAD(&dev->execute_task_list);
1293 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001294 INIT_LIST_HEAD(&dev->state_task_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001295 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001296 spin_lock_init(&dev->execute_task_lock);
1297 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001298 spin_lock_init(&dev->dev_reservation_lock);
1299 spin_lock_init(&dev->dev_status_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300 spin_lock_init(&dev->se_port_lock);
1301 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001302 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001303 atomic_set(&dev->dev_ordered_id, 0);
1304
1305 se_dev_set_default_attribs(dev, dev_limits);
1306
1307 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1308 dev->creation_time = get_jiffies_64();
1309 spin_lock_init(&dev->stats_lock);
1310
1311 spin_lock(&hba->device_lock);
1312 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1313 hba->dev_count++;
1314 spin_unlock(&hba->device_lock);
1315 /*
1316 * Setup the SAM Task Attribute emulation for struct se_device
1317 */
1318 core_setup_task_attr_emulation(dev);
1319 /*
1320 * Force PR and ALUA passthrough emulation with internal object use.
1321 */
1322 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1323 /*
1324 * Setup the Reservations infrastructure for struct se_device
1325 */
1326 core_setup_reservations(dev, force_pt);
1327 /*
1328 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1329 */
1330 if (core_setup_alua(dev, force_pt) < 0)
1331 goto out;
1332
1333 /*
1334 * Startup the struct se_device processing thread
1335 */
1336 dev->process_thread = kthread_run(transport_processing_thread, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001337 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001338 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001339 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001340 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001341 goto out;
1342 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001343 /*
1344 * Setup work_queue for QUEUE_FULL
1345 */
1346 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347 /*
1348 * Preload the initial INQUIRY const values if we are doing
1349 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1350 * passthrough because this is being provided by the backend LLD.
1351 * This is required so that transport_get_inquiry() copies these
1352 * originals once back into DEV_T10_WWN(dev) for the virtual device
1353 * setup.
1354 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001355 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001356 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001357 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001358 " INQUIRY consts\n");
1359 goto out;
1360 }
1361
Andy Grovere3d6f902011-07-19 08:55:10 +00001362 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1363 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1364 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001365 }
1366 scsi_dump_inquiry(dev);
1367
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001368 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001369out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001370 kthread_stop(dev->process_thread);
1371
1372 spin_lock(&hba->device_lock);
1373 list_del(&dev->dev_list);
1374 hba->dev_count--;
1375 spin_unlock(&hba->device_lock);
1376
1377 se_release_vpd_for_dev(dev);
1378
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001379 kfree(dev);
1380
1381 return NULL;
1382}
1383EXPORT_SYMBOL(transport_add_device_to_core_hba);
1384
1385/* transport_generic_prepare_cdb():
1386 *
1387 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1388 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1389 * The point of this is since we are mapping iSCSI LUNs to
1390 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1391 * devices and HBAs for a loop.
1392 */
1393static inline void transport_generic_prepare_cdb(
1394 unsigned char *cdb)
1395{
1396 switch (cdb[0]) {
1397 case READ_10: /* SBC - RDProtect */
1398 case READ_12: /* SBC - RDProtect */
1399 case READ_16: /* SBC - RDProtect */
1400 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1401 case VERIFY: /* SBC - VRProtect */
1402 case VERIFY_16: /* SBC - VRProtect */
1403 case WRITE_VERIFY: /* SBC - VRProtect */
1404 case WRITE_VERIFY_12: /* SBC - VRProtect */
1405 break;
1406 default:
1407 cdb[1] &= 0x1f; /* clear logical unit number */
1408 break;
1409 }
1410}
1411
1412static struct se_task *
1413transport_generic_get_task(struct se_cmd *cmd,
1414 enum dma_data_direction data_direction)
1415{
1416 struct se_task *task;
Andy Grover5951146d2011-07-19 10:26:37 +00001417 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001418
Andy Grover6708bb22011-06-08 10:36:43 -07001419 task = dev->transport->alloc_task(cmd->t_task_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001420 if (!task) {
Andy Grover6708bb22011-06-08 10:36:43 -07001421 pr_err("Unable to allocate struct se_task\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001422 return NULL;
1423 }
1424
1425 INIT_LIST_HEAD(&task->t_list);
1426 INIT_LIST_HEAD(&task->t_execute_list);
1427 INIT_LIST_HEAD(&task->t_state_list);
1428 init_completion(&task->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001429 task->task_se_cmd = cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001430 task->task_data_direction = data_direction;
1431
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001432 return task;
1433}
1434
1435static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1436
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001437/*
1438 * Used by fabric modules containing a local struct se_cmd within their
1439 * fabric dependent per I/O descriptor.
1440 */
1441void transport_init_se_cmd(
1442 struct se_cmd *cmd,
1443 struct target_core_fabric_ops *tfo,
1444 struct se_session *se_sess,
1445 u32 data_length,
1446 int data_direction,
1447 int task_attr,
1448 unsigned char *sense_buffer)
1449{
Andy Grover5951146d2011-07-19 10:26:37 +00001450 INIT_LIST_HEAD(&cmd->se_lun_node);
1451 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001452 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001453 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001454 INIT_LIST_HEAD(&cmd->se_cmd_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001455 INIT_LIST_HEAD(&cmd->t_task_list);
1456 init_completion(&cmd->transport_lun_fe_stop_comp);
1457 init_completion(&cmd->transport_lun_stop_comp);
1458 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001459 init_completion(&cmd->cmd_wait_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001460 spin_lock_init(&cmd->t_state_lock);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001461 cmd->transport_state = CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001462
1463 cmd->se_tfo = tfo;
1464 cmd->se_sess = se_sess;
1465 cmd->data_length = data_length;
1466 cmd->data_direction = data_direction;
1467 cmd->sam_task_attr = task_attr;
1468 cmd->sense_buffer = sense_buffer;
1469}
1470EXPORT_SYMBOL(transport_init_se_cmd);
1471
1472static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1473{
1474 /*
1475 * Check if SAM Task Attribute emulation is enabled for this
1476 * struct se_device storage object
1477 */
Andy Grover5951146d2011-07-19 10:26:37 +00001478 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001479 return 0;
1480
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001481 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001482 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001483 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001484 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485 }
1486 /*
1487 * Used to determine when ORDERED commands should go from
1488 * Dormant to Active status.
1489 */
Andy Grover5951146d2011-07-19 10:26:37 +00001490 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001491 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001492 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001493 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001494 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001495 return 0;
1496}
1497
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001498/* transport_generic_allocate_tasks():
1499 *
1500 * Called from fabric RX Thread.
1501 */
1502int transport_generic_allocate_tasks(
1503 struct se_cmd *cmd,
1504 unsigned char *cdb)
1505{
1506 int ret;
1507
1508 transport_generic_prepare_cdb(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001509 /*
1510 * Ensure that the received CDB is less than the max (252 + 8) bytes
1511 * for VARIABLE_LENGTH_CMD
1512 */
1513 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001514 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001515 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1516 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001517 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1518 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001519 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001520 }
1521 /*
1522 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1523 * allocate the additional extended CDB buffer now.. Otherwise
1524 * setup the pointer from __t_task_cdb to t_task_cdb.
1525 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001526 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1527 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001528 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001529 if (!cmd->t_task_cdb) {
1530 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001531 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001532 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001533 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001534 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1535 cmd->scsi_sense_reason =
1536 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001537 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001538 }
1539 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001540 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001541 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001542 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001543 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001544 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001545 /*
1546 * Setup the received CDB based on SCSI defined opcodes and
1547 * perform unit attention, persistent reservations and ALUA
Andy Grovera1d8b492011-05-02 17:12:10 -07001548 * checks for virtual device backends. The cmd->t_task_cdb
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001549 * pointer is expected to be setup before we reach this point.
1550 */
1551 ret = transport_generic_cmd_sequencer(cmd, cdb);
1552 if (ret < 0)
1553 return ret;
1554 /*
1555 * Check for SAM Task Attribute Emulation
1556 */
1557 if (transport_check_alloc_task_attr(cmd) < 0) {
1558 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1559 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001560 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001561 }
1562 spin_lock(&cmd->se_lun->lun_sep_lock);
1563 if (cmd->se_lun->lun_sep)
1564 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1565 spin_unlock(&cmd->se_lun->lun_sep_lock);
1566 return 0;
1567}
1568EXPORT_SYMBOL(transport_generic_allocate_tasks);
1569
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001570/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001571 * Used by fabric module frontends to queue tasks directly.
1572 * Many only be used from process context only
1573 */
1574int transport_handle_cdb_direct(
1575 struct se_cmd *cmd)
1576{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001577 int ret;
1578
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001579 if (!cmd->se_lun) {
1580 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001581 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001582 return -EINVAL;
1583 }
1584 if (in_interrupt()) {
1585 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001586 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001587 " from interrupt context\n");
1588 return -EINVAL;
1589 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001590 /*
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001591 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001592 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1593 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001594 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001595 *
1596 * Also, we don't take cmd->t_state_lock here as we only expect
1597 * this to be called for initial descriptor submission.
1598 */
1599 cmd->t_state = TRANSPORT_NEW_CMD;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001600 cmd->transport_state |= CMD_T_ACTIVE;
1601
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001602 /*
1603 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1604 * so follow TRANSPORT_NEW_CMD processing thread context usage
1605 * and call transport_generic_request_failure() if necessary..
1606 */
1607 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001608 if (ret < 0)
1609 transport_generic_request_failure(cmd);
1610
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001611 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001612}
1613EXPORT_SYMBOL(transport_handle_cdb_direct);
1614
Nicholas Bellingera6360782011-11-18 20:36:22 -08001615/**
1616 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1617 *
1618 * @se_cmd: command descriptor to submit
1619 * @se_sess: associated se_sess for endpoint
1620 * @cdb: pointer to SCSI CDB
1621 * @sense: pointer to SCSI sense buffer
1622 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1623 * @data_length: fabric expected data transfer length
1624 * @task_addr: SAM task attribute
1625 * @data_dir: DMA data direction
1626 * @flags: flags for command submission from target_sc_flags_tables
1627 *
1628 * This may only be called from process context, and also currently
1629 * assumes internal allocation of fabric payload buffer by target-core.
1630 **/
Andy Grover1edcdb42012-01-19 13:39:23 -08001631void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
Nicholas Bellingera6360782011-11-18 20:36:22 -08001632 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1633 u32 data_length, int task_attr, int data_dir, int flags)
1634{
1635 struct se_portal_group *se_tpg;
1636 int rc;
1637
1638 se_tpg = se_sess->se_tpg;
1639 BUG_ON(!se_tpg);
1640 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1641 BUG_ON(in_interrupt());
1642 /*
1643 * Initialize se_cmd for target operation. From this point
1644 * exceptions are handled by sending exception status via
1645 * target_core_fabric_ops->queue_status() callback
1646 */
1647 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1648 data_length, data_dir, task_attr, sense);
1649 /*
1650 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1651 * se_sess->sess_cmd_list. A second kref_get here is necessary
1652 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1653 * kref_put() to happen during fabric packet acknowledgement.
1654 */
1655 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1656 /*
1657 * Signal bidirectional data payloads to target-core
1658 */
1659 if (flags & TARGET_SCF_BIDI_OP)
1660 se_cmd->se_cmd_flags |= SCF_BIDI;
1661 /*
1662 * Locate se_lun pointer and attach it to struct se_cmd
1663 */
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001664 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1665 transport_send_check_condition_and_sense(se_cmd,
1666 se_cmd->scsi_sense_reason, 0);
1667 target_put_sess_cmd(se_sess, se_cmd);
1668 return;
1669 }
Nicholas Bellingera6360782011-11-18 20:36:22 -08001670 /*
1671 * Sanitize CDBs via transport_generic_cmd_sequencer() and
1672 * allocate the necessary tasks to complete the received CDB+data
1673 */
1674 rc = transport_generic_allocate_tasks(se_cmd, cdb);
Nicholas Bellinger735703c2012-01-20 19:02:56 -08001675 if (rc != 0) {
1676 transport_generic_request_failure(se_cmd);
1677 return;
1678 }
Nicholas Bellingera6360782011-11-18 20:36:22 -08001679 /*
1680 * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
1681 * for immediate execution of READs, otherwise wait for
1682 * transport_generic_handle_data() to be called for WRITEs
1683 * when fabric has filled the incoming buffer.
1684 */
1685 transport_handle_cdb_direct(se_cmd);
Andy Grover1edcdb42012-01-19 13:39:23 -08001686 return;
Nicholas Bellingera6360782011-11-18 20:36:22 -08001687}
1688EXPORT_SYMBOL(target_submit_cmd);
1689
Andy Groverea98d7f2012-01-19 13:39:21 -08001690/**
1691 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1692 * for TMR CDBs
1693 *
1694 * @se_cmd: command descriptor to submit
1695 * @se_sess: associated se_sess for endpoint
1696 * @sense: pointer to SCSI sense buffer
1697 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1698 * @fabric_context: fabric context for TMR req
1699 * @tm_type: Type of TM request
1700 *
1701 * Callable from all contexts.
1702 **/
1703
1704void target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
1705 unsigned char *sense, u32 unpacked_lun,
1706 void *fabric_tmr_ptr, unsigned char tm_type, int flags)
1707{
1708 struct se_portal_group *se_tpg;
1709 int ret;
1710
1711 se_tpg = se_sess->se_tpg;
1712 BUG_ON(!se_tpg);
1713
1714 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1715 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
1716
1717 /* See target_submit_cmd for commentary */
1718 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1719
1720 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, GFP_KERNEL);
1721 if (ret < 0) {
1722 dump_stack();
1723 /* FIXME XXX */
1724 return;
1725 }
1726
1727 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1728 if (ret) {
1729 transport_send_check_condition_and_sense(se_cmd,
1730 se_cmd->scsi_sense_reason, 0);
1731 transport_generic_free_cmd(se_cmd, 0);
1732 return;
1733 }
1734 transport_generic_handle_tmr(se_cmd);
1735}
1736EXPORT_SYMBOL(target_submit_tmr);
1737
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001738/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001739 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1740 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1741 * complete setup in TCM process context w/ TFO->new_cmd_map().
1742 */
1743int transport_generic_handle_cdb_map(
1744 struct se_cmd *cmd)
1745{
Andy Grovere3d6f902011-07-19 08:55:10 +00001746 if (!cmd->se_lun) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001747 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001748 pr_err("cmd->se_lun is NULL\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001749 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001750 }
1751
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001752 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001753 return 0;
1754}
1755EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1756
1757/* transport_generic_handle_data():
1758 *
1759 *
1760 */
1761int transport_generic_handle_data(
1762 struct se_cmd *cmd)
1763{
1764 /*
1765 * For the software fabric case, then we assume the nexus is being
1766 * failed/shutdown when signals are pending from the kthread context
1767 * caller, so we return a failure. For the HW target mode case running
1768 * in interrupt code, the signal_pending() check is skipped.
1769 */
1770 if (!in_interrupt() && signal_pending(current))
Andy Grovere3d6f902011-07-19 08:55:10 +00001771 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001772 /*
1773 * If the received CDB has aleady been ABORTED by the generic
1774 * target engine, we now call transport_check_aborted_status()
1775 * to queue any delated TASK_ABORTED status for the received CDB to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001776 * fabric module as we are expecting no further incoming DATA OUT
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001777 * sequences at this point.
1778 */
1779 if (transport_check_aborted_status(cmd, 1) != 0)
1780 return 0;
1781
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001782 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001783 return 0;
1784}
1785EXPORT_SYMBOL(transport_generic_handle_data);
1786
1787/* transport_generic_handle_tmr():
1788 *
1789 *
1790 */
1791int transport_generic_handle_tmr(
1792 struct se_cmd *cmd)
1793{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001794 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001795 return 0;
1796}
1797EXPORT_SYMBOL(transport_generic_handle_tmr);
1798
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001799/*
1800 * If the task is active, request it to be stopped and sleep until it
1801 * has completed.
1802 */
1803bool target_stop_task(struct se_task *task, unsigned long *flags)
1804{
1805 struct se_cmd *cmd = task->task_se_cmd;
1806 bool was_active = false;
1807
1808 if (task->task_flags & TF_ACTIVE) {
1809 task->task_flags |= TF_REQUEST_STOP;
1810 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1811
1812 pr_debug("Task %p waiting to complete\n", task);
1813 wait_for_completion(&task->task_stop_comp);
1814 pr_debug("Task %p stopped successfully\n", task);
1815
1816 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1817 atomic_dec(&cmd->t_task_cdbs_left);
1818 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1819 was_active = true;
1820 }
1821
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001822 return was_active;
1823}
1824
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001825static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1826{
1827 struct se_task *task, *task_tmp;
1828 unsigned long flags;
1829 int ret = 0;
1830
Andy Grover6708bb22011-06-08 10:36:43 -07001831 pr_debug("ITT[0x%08x] - Stopping tasks\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001832 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001833
1834 /*
1835 * No tasks remain in the execution queue
1836 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001837 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001838 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07001839 &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001840 pr_debug("Processing task %p\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001841 /*
1842 * If the struct se_task has not been sent and is not active,
1843 * remove the struct se_task from the execution queue.
1844 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04001845 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
Andy Grovera1d8b492011-05-02 17:12:10 -07001846 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001847 flags);
1848 transport_remove_task_from_execute_queue(task,
Christoph Hellwig42bf8292011-10-12 11:07:00 -04001849 cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001850
Christoph Hellwig04629b72011-10-12 11:07:04 -04001851 pr_debug("Task %p removed from execute queue\n", task);
Andy Grovera1d8b492011-05-02 17:12:10 -07001852 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001853 continue;
1854 }
1855
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001856 if (!target_stop_task(task, &flags)) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001857 pr_debug("Task %p - did nothing\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001858 ret++;
1859 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001860 }
Andy Grovera1d8b492011-05-02 17:12:10 -07001861 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001862
1863 return ret;
1864}
1865
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001866/*
1867 * Handle SAM-esque emulation for generic transport request failures.
1868 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001869static void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001870{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001871 int ret = 0;
1872
Andy Grover6708bb22011-06-08 10:36:43 -07001873 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001874 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001875 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001876 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001877 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001878 cmd->t_state, cmd->scsi_sense_reason);
Andy Grover6708bb22011-06-08 10:36:43 -07001879 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001880 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001881 " CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1882 cmd->t_task_list_num,
Andy Grovera1d8b492011-05-02 17:12:10 -07001883 atomic_read(&cmd->t_task_cdbs_left),
1884 atomic_read(&cmd->t_task_cdbs_sent),
1885 atomic_read(&cmd->t_task_cdbs_ex_left),
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001886 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1887 (cmd->transport_state & CMD_T_STOP) != 0,
1888 (cmd->transport_state & CMD_T_SENT) != 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001889
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001890 /*
1891 * For SAM Task Attribute emulation for failed struct se_cmd
1892 */
1893 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1894 transport_complete_task_attr(cmd);
1895
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001896 switch (cmd->scsi_sense_reason) {
1897 case TCM_NON_EXISTENT_LUN:
1898 case TCM_UNSUPPORTED_SCSI_OPCODE:
1899 case TCM_INVALID_CDB_FIELD:
1900 case TCM_INVALID_PARAMETER_LIST:
1901 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1902 case TCM_UNKNOWN_MODE_PAGE:
1903 case TCM_WRITE_PROTECTED:
1904 case TCM_CHECK_CONDITION_ABORT_CMD:
1905 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1906 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001907 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001908 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001909 /*
1910 * No SENSE Data payload for this case, set SCSI Status
1911 * and queue the response to $FABRIC_MOD.
1912 *
1913 * Uses linux/include/scsi/scsi.h SAM status codes defs
1914 */
1915 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1916 /*
1917 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1918 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1919 * CONFLICT STATUS.
1920 *
1921 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1922 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001923 if (cmd->se_sess &&
1924 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1925 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001926 cmd->orig_fe_lun, 0x2C,
1927 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1928
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001929 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001930 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001931 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001932 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001933 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001934 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001935 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001936 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1937 break;
1938 }
Nicholas Bellinger16ab8e62011-08-08 19:03:38 -07001939 /*
1940 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1941 * make the call to transport_send_check_condition_and_sense()
1942 * directly. Otherwise expect the fabric to make the call to
1943 * transport_send_check_condition_and_sense() after handling
1944 * possible unsoliticied write data payloads.
1945 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001946 ret = transport_send_check_condition_and_sense(cmd,
1947 cmd->scsi_sense_reason, 0);
1948 if (ret == -EAGAIN || ret == -ENOMEM)
1949 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001950
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001951check_stop:
1952 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001953 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001954 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001955 return;
1956
1957queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001958 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1959 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001960}
1961
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001962static inline u32 transport_lba_21(unsigned char *cdb)
1963{
1964 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1965}
1966
1967static inline u32 transport_lba_32(unsigned char *cdb)
1968{
1969 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1970}
1971
1972static inline unsigned long long transport_lba_64(unsigned char *cdb)
1973{
1974 unsigned int __v1, __v2;
1975
1976 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1977 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1978
1979 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1980}
1981
1982/*
1983 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1984 */
1985static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1986{
1987 unsigned int __v1, __v2;
1988
1989 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1990 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1991
1992 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1993}
1994
1995static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
1996{
1997 unsigned long flags;
1998
Andy Grovera1d8b492011-05-02 17:12:10 -07001999 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002000 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
Andy Grovera1d8b492011-05-02 17:12:10 -07002001 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002002}
2003
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002004/*
2005 * Called from Fabric Module context from transport_execute_tasks()
2006 *
2007 * The return of this function determins if the tasks from struct se_cmd
2008 * get added to the execution queue in transport_execute_tasks(),
2009 * or are added to the delayed or ordered lists here.
2010 */
2011static inline int transport_execute_task_attr(struct se_cmd *cmd)
2012{
Andy Grover5951146d2011-07-19 10:26:37 +00002013 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002014 return 1;
2015 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002016 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002017 * to allow the passed struct se_cmd list of tasks to the front of the list.
2018 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002019 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07002020 pr_debug("Added HEAD_OF_QUEUE for CDB:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002021 " 0x%02x, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07002022 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002023 cmd->se_ordered_id);
2024 return 1;
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002025 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Andy Grover5951146d2011-07-19 10:26:37 +00002026 atomic_inc(&cmd->se_dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002027 smp_mb__after_atomic_inc();
2028
Andy Grover6708bb22011-06-08 10:36:43 -07002029 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002030 " list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002031 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002032 cmd->se_ordered_id);
2033 /*
2034 * Add ORDERED command to tail of execution queue if
2035 * no other older commands exist that need to be
2036 * completed first.
2037 */
Andy Grover6708bb22011-06-08 10:36:43 -07002038 if (!atomic_read(&cmd->se_dev->simple_cmds))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002039 return 1;
2040 } else {
2041 /*
2042 * For SIMPLE and UNTAGGED Task Attribute commands
2043 */
Andy Grover5951146d2011-07-19 10:26:37 +00002044 atomic_inc(&cmd->se_dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002045 smp_mb__after_atomic_inc();
2046 }
2047 /*
2048 * Otherwise if one or more outstanding ORDERED task attribute exist,
2049 * add the dormant task(s) built for the passed struct se_cmd to the
2050 * execution queue and become in Active state for this struct se_device.
2051 */
Andy Grover5951146d2011-07-19 10:26:37 +00002052 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002053 /*
2054 * Otherwise, add cmd w/ tasks to delayed cmd queue that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002055 * will be drained upon completion of HEAD_OF_QUEUE task.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002056 */
Andy Grover5951146d2011-07-19 10:26:37 +00002057 spin_lock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002058 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
Andy Grover5951146d2011-07-19 10:26:37 +00002059 list_add_tail(&cmd->se_delayed_node,
2060 &cmd->se_dev->delayed_cmd_list);
2061 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002062
Andy Grover6708bb22011-06-08 10:36:43 -07002063 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002064 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002065 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002066 cmd->se_ordered_id);
2067 /*
2068 * Return zero to let transport_execute_tasks() know
2069 * not to add the delayed tasks to the execution list.
2070 */
2071 return 0;
2072 }
2073 /*
2074 * Otherwise, no ORDERED task attributes exist..
2075 */
2076 return 1;
2077}
2078
2079/*
2080 * Called from fabric module context in transport_generic_new_cmd() and
2081 * transport_generic_process_write()
2082 */
2083static int transport_execute_tasks(struct se_cmd *cmd)
2084{
2085 int add_tasks;
Nicholas Bellinger40be67f2011-11-30 00:41:20 -08002086 struct se_device *se_dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002087 /*
2088 * Call transport_cmd_check_stop() to see if a fabric exception
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002089 * has occurred that prevents execution.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002090 */
Andy Grover6708bb22011-06-08 10:36:43 -07002091 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002092 /*
2093 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2094 * attribute for the tasks of the received struct se_cmd CDB
2095 */
2096 add_tasks = transport_execute_task_attr(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00002097 if (!add_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002098 goto execute_tasks;
2099 /*
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002100 * __transport_execute_tasks() -> __transport_add_tasks_from_cmd()
2101 * adds associated se_tasks while holding dev->execute_task_lock
2102 * before I/O dispath to avoid a double spinlock access.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002103 */
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002104 __transport_execute_tasks(se_dev, cmd);
2105 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002106 }
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002107
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002108execute_tasks:
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002109 __transport_execute_tasks(se_dev, NULL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002110 return 0;
2111}
2112
2113/*
2114 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2115 * from struct se_device->execute_task_list and
2116 *
2117 * Called from transport_processing_thread()
2118 */
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002119static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002120{
2121 int error;
2122 struct se_cmd *cmd = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002123 struct se_task *task = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002124 unsigned long flags;
2125
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002126check_depth:
Andy Grovere3d6f902011-07-19 08:55:10 +00002127 spin_lock_irq(&dev->execute_task_lock);
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002128 if (new_cmd != NULL)
2129 __transport_add_tasks_from_cmd(new_cmd);
2130
Andy Grovere3d6f902011-07-19 08:55:10 +00002131 if (list_empty(&dev->execute_task_list)) {
2132 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002133 return 0;
2134 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002135 task = list_first_entry(&dev->execute_task_list,
2136 struct se_task, t_execute_list);
Christoph Hellwig04629b72011-10-12 11:07:04 -04002137 __transport_remove_task_from_execute_queue(task, dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002138 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002139
Andy Grovere3d6f902011-07-19 08:55:10 +00002140 cmd = task->task_se_cmd;
Andy Grovera1d8b492011-05-02 17:12:10 -07002141 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04002142 task->task_flags |= (TF_ACTIVE | TF_SENT);
Andy Grovera1d8b492011-05-02 17:12:10 -07002143 atomic_inc(&cmd->t_task_cdbs_sent);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002144
Andy Grovera1d8b492011-05-02 17:12:10 -07002145 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2146 cmd->t_task_list_num)
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002147 cmd->transport_state |= CMD_T_SENT;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002148
Andy Grovera1d8b492011-05-02 17:12:10 -07002149 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002150
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002151 if (cmd->execute_task)
2152 error = cmd->execute_task(task);
2153 else
2154 error = dev->transport->do_task(task);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002155 if (error != 0) {
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002156 spin_lock_irqsave(&cmd->t_state_lock, flags);
2157 task->task_flags &= ~TF_ACTIVE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002158 cmd->transport_state &= ~CMD_T_SENT;
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002159 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05002160
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002161 transport_stop_tasks_for_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002162 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002163 }
2164
Nicholas Bellinger4d2300c2011-11-30 18:18:33 -08002165 new_cmd = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002166 goto check_depth;
2167
2168 return 0;
2169}
2170
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002171static inline u32 transport_get_sectors_6(
2172 unsigned char *cdb,
2173 struct se_cmd *cmd,
2174 int *ret)
2175{
Andy Grover5951146d2011-07-19 10:26:37 +00002176 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002177
2178 /*
2179 * Assume TYPE_DISK for non struct se_device objects.
2180 * Use 8-bit sector value.
2181 */
2182 if (!dev)
2183 goto type_disk;
2184
2185 /*
2186 * Use 24-bit allocation length for TYPE_TAPE.
2187 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002188 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002189 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2190
2191 /*
2192 * Everything else assume TYPE_DISK Sector CDB location.
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002193 * Use 8-bit sector value. SBC-3 says:
2194 *
2195 * A TRANSFER LENGTH field set to zero specifies that 256
2196 * logical blocks shall be written. Any other value
2197 * specifies the number of logical blocks that shall be
2198 * written.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002199 */
2200type_disk:
Roland Dreier9b5cd7f2011-11-22 13:51:33 -08002201 return cdb[4] ? : 256;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002202}
2203
2204static inline u32 transport_get_sectors_10(
2205 unsigned char *cdb,
2206 struct se_cmd *cmd,
2207 int *ret)
2208{
Andy Grover5951146d2011-07-19 10:26:37 +00002209 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002210
2211 /*
2212 * Assume TYPE_DISK for non struct se_device objects.
2213 * Use 16-bit sector value.
2214 */
2215 if (!dev)
2216 goto type_disk;
2217
2218 /*
2219 * XXX_10 is not defined in SSC, throw an exception
2220 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002221 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2222 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002223 return 0;
2224 }
2225
2226 /*
2227 * Everything else assume TYPE_DISK Sector CDB location.
2228 * Use 16-bit sector value.
2229 */
2230type_disk:
2231 return (u32)(cdb[7] << 8) + cdb[8];
2232}
2233
2234static inline u32 transport_get_sectors_12(
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 32-bit sector value.
2244 */
2245 if (!dev)
2246 goto type_disk;
2247
2248 /*
2249 * XXX_12 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 32-bit sector value.
2259 */
2260type_disk:
2261 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2262}
2263
2264static inline u32 transport_get_sectors_16(
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 * Use 24-bit allocation length for TYPE_TAPE.
2280 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002281 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002282 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2283
2284type_disk:
2285 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2286 (cdb[12] << 8) + cdb[13];
2287}
2288
2289/*
2290 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2291 */
2292static inline u32 transport_get_sectors_32(
2293 unsigned char *cdb,
2294 struct se_cmd *cmd,
2295 int *ret)
2296{
2297 /*
2298 * Assume TYPE_DISK for non struct se_device objects.
2299 * Use 32-bit sector value.
2300 */
2301 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2302 (cdb[30] << 8) + cdb[31];
2303
2304}
2305
2306static inline u32 transport_get_size(
2307 u32 sectors,
2308 unsigned char *cdb,
2309 struct se_cmd *cmd)
2310{
Andy Grover5951146d2011-07-19 10:26:37 +00002311 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002312
Andy Grovere3d6f902011-07-19 08:55:10 +00002313 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002314 if (cdb[1] & 1) { /* sectors */
Andy Grovere3d6f902011-07-19 08:55:10 +00002315 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002316 } else /* bytes */
2317 return sectors;
2318 }
2319#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002320 pr_debug("Returning block_size: %u, sectors: %u == %u for"
Andy Grovere3d6f902011-07-19 08:55:10 +00002321 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2322 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2323 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002324#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00002325 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002326}
2327
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002328static void transport_xor_callback(struct se_cmd *cmd)
2329{
2330 unsigned char *buf, *addr;
Andy Groverec98f782011-07-20 19:28:46 +00002331 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002332 unsigned int offset;
2333 int i;
Andy Groverec98f782011-07-20 19:28:46 +00002334 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002335 /*
2336 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2337 *
2338 * 1) read the specified logical block(s);
2339 * 2) transfer logical blocks from the data-out buffer;
2340 * 3) XOR the logical blocks transferred from the data-out buffer with
2341 * the logical blocks read, storing the resulting XOR data in a buffer;
2342 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2343 * blocks transferred from the data-out buffer; and
2344 * 5) transfer the resulting XOR data to the data-in buffer.
2345 */
2346 buf = kmalloc(cmd->data_length, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002347 if (!buf) {
2348 pr_err("Unable to allocate xor_callback buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002349 return;
2350 }
2351 /*
Andy Groverec98f782011-07-20 19:28:46 +00002352 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002353 * into the locally allocated *buf
2354 */
Andy Groverec98f782011-07-20 19:28:46 +00002355 sg_copy_to_buffer(cmd->t_data_sg,
2356 cmd->t_data_nents,
2357 buf,
2358 cmd->data_length);
2359
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002360 /*
2361 * Now perform the XOR against the BIDI read memory located at
Andy Grovera1d8b492011-05-02 17:12:10 -07002362 * cmd->t_mem_bidi_list
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002363 */
2364
2365 offset = 0;
Andy Groverec98f782011-07-20 19:28:46 +00002366 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2367 addr = kmap_atomic(sg_page(sg), KM_USER0);
2368 if (!addr)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002369 goto out;
2370
Andy Groverec98f782011-07-20 19:28:46 +00002371 for (i = 0; i < sg->length; i++)
2372 *(addr + sg->offset + i) ^= *(buf + offset + i);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002373
Andy Groverec98f782011-07-20 19:28:46 +00002374 offset += sg->length;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002375 kunmap_atomic(addr, KM_USER0);
2376 }
Andy Groverec98f782011-07-20 19:28:46 +00002377
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002378out:
2379 kfree(buf);
2380}
2381
2382/*
2383 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2384 */
2385static int transport_get_sense_data(struct se_cmd *cmd)
2386{
2387 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002388 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002389 struct se_task *task = NULL, *task_tmp;
2390 unsigned long flags;
2391 u32 offset = 0;
2392
Andy Grovere3d6f902011-07-19 08:55:10 +00002393 WARN_ON(!cmd->se_lun);
2394
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002395 if (!dev)
2396 return 0;
2397
Andy Grovera1d8b492011-05-02 17:12:10 -07002398 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002399 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002400 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002401 return 0;
2402 }
2403
2404 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07002405 &cmd->t_task_list, t_list) {
Christoph Hellwigef804a82011-11-23 06:53:58 -05002406 if (!(task->task_flags & TF_HAS_SENSE))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002407 continue;
2408
Andy Grovere3d6f902011-07-19 08:55:10 +00002409 if (!dev->transport->get_sense_buffer) {
Andy Grover6708bb22011-06-08 10:36:43 -07002410 pr_err("dev->transport->get_sense_buffer"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002411 " is NULL\n");
2412 continue;
2413 }
2414
Andy Grovere3d6f902011-07-19 08:55:10 +00002415 sense_buffer = dev->transport->get_sense_buffer(task);
Andy Grover6708bb22011-06-08 10:36:43 -07002416 if (!sense_buffer) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04002417 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002418 " sense buffer for task with sense\n",
Christoph Hellwig04629b72011-10-12 11:07:04 -04002419 cmd->se_tfo->get_task_tag(cmd), task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002420 continue;
2421 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002422 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002423
Andy Grovere3d6f902011-07-19 08:55:10 +00002424 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002425 TRANSPORT_SENSE_BUFFER);
2426
Andy Grover5951146d2011-07-19 10:26:37 +00002427 memcpy(&buffer[offset], sense_buffer,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002428 TRANSPORT_SENSE_BUFFER);
2429 cmd->scsi_status = task->task_scsi_status;
2430 /* Automatically padded */
2431 cmd->scsi_sense_length =
2432 (TRANSPORT_SENSE_BUFFER + offset);
2433
Andy Grover6708bb22011-06-08 10:36:43 -07002434 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002435 " and sense\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002436 dev->se_hba->hba_id, dev->transport->name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002437 cmd->scsi_status);
2438 return 0;
2439 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002440 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002441
2442 return -1;
2443}
2444
Andy Groverec98f782011-07-20 19:28:46 +00002445static inline long long transport_dev_end_lba(struct se_device *dev)
2446{
2447 return dev->transport->get_blocks(dev) + 1;
2448}
2449
2450static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2451{
2452 struct se_device *dev = cmd->se_dev;
2453 u32 sectors;
2454
2455 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2456 return 0;
2457
2458 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2459
Andy Grover6708bb22011-06-08 10:36:43 -07002460 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2461 pr_err("LBA: %llu Sectors: %u exceeds"
Andy Groverec98f782011-07-20 19:28:46 +00002462 " transport_dev_end_lba(): %llu\n",
2463 cmd->t_task_lba, sectors,
2464 transport_dev_end_lba(dev));
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002465 return -EINVAL;
Andy Groverec98f782011-07-20 19:28:46 +00002466 }
2467
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002468 return 0;
Andy Groverec98f782011-07-20 19:28:46 +00002469}
2470
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002471static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2472{
2473 /*
2474 * Determine if the received WRITE_SAME is used to for direct
2475 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2476 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2477 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2478 */
2479 int passthrough = (dev->transport->transport_type ==
2480 TRANSPORT_PLUGIN_PHBA_PDEV);
2481
2482 if (!passthrough) {
2483 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2484 pr_err("WRITE_SAME PBDATA and LBDATA"
2485 " bits not supported for Block Discard"
2486 " Emulation\n");
2487 return -ENOSYS;
2488 }
2489 /*
2490 * Currently for the emulated case we only accept
2491 * tpws with the UNMAP=1 bit set.
2492 */
2493 if (!(flags[0] & 0x08)) {
2494 pr_err("WRITE_SAME w/o UNMAP bit not"
2495 " supported for Block Discard Emulation\n");
2496 return -ENOSYS;
2497 }
2498 }
2499
2500 return 0;
2501}
2502
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002503/* transport_generic_cmd_sequencer():
2504 *
2505 * Generic Command Sequencer that should work for most DAS transport
2506 * drivers.
2507 *
2508 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2509 * RX Thread.
2510 *
2511 * FIXME: Need to support other SCSI OPCODES where as well.
2512 */
2513static int transport_generic_cmd_sequencer(
2514 struct se_cmd *cmd,
2515 unsigned char *cdb)
2516{
Andy Grover5951146d2011-07-19 10:26:37 +00002517 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002518 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2519 int ret = 0, sector_ret = 0, passthrough;
2520 u32 sectors = 0, size = 0, pr_reg_type = 0;
2521 u16 service_action;
2522 u8 alua_ascq = 0;
2523 /*
2524 * Check for an existing UNIT ATTENTION condition
2525 */
2526 if (core_scsi3_ua_check(cmd, cdb) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002527 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2528 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
Andy Grover5951146d2011-07-19 10:26:37 +00002529 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002530 }
2531 /*
2532 * Check status of Asymmetric Logical Unit Assignment port
2533 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002534 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002535 if (ret != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002536 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002537 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002538 * The ALUA additional sense code qualifier (ASCQ) is determined
2539 * by the ALUA primary or secondary access state..
2540 */
2541 if (ret > 0) {
2542#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002543 pr_debug("[%s]: ALUA TG Port not available,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002544 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002545 cmd->se_tfo->get_fabric_name(), alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002546#endif
2547 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2548 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2549 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
Andy Grover5951146d2011-07-19 10:26:37 +00002550 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002551 }
2552 goto out_invalid_cdb_field;
2553 }
2554 /*
2555 * Check status for SPC-3 Persistent Reservations
2556 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002557 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2558 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002559 cmd, cdb, pr_reg_type) != 0) {
2560 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2561 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2562 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2563 return -EBUSY;
2564 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002565 /*
2566 * This means the CDB is allowed for the SCSI Initiator port
2567 * when said port is *NOT* holding the legacy SPC-2 or
2568 * SPC-3 Persistent Reservation.
2569 */
2570 }
2571
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002572 /*
2573 * If we operate in passthrough mode we skip most CDB emulation and
2574 * instead hand the commands down to the physical SCSI device.
2575 */
2576 passthrough =
2577 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2578
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002579 switch (cdb[0]) {
2580 case READ_6:
2581 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2582 if (sector_ret)
2583 goto out_unsupported_cdb;
2584 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002585 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002586 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2587 break;
2588 case READ_10:
2589 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2590 if (sector_ret)
2591 goto out_unsupported_cdb;
2592 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002593 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002594 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2595 break;
2596 case READ_12:
2597 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2598 if (sector_ret)
2599 goto out_unsupported_cdb;
2600 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002601 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002602 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2603 break;
2604 case READ_16:
2605 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2606 if (sector_ret)
2607 goto out_unsupported_cdb;
2608 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002609 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002610 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2611 break;
2612 case WRITE_6:
2613 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2614 if (sector_ret)
2615 goto out_unsupported_cdb;
2616 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002617 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002618 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2619 break;
2620 case WRITE_10:
2621 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2622 if (sector_ret)
2623 goto out_unsupported_cdb;
2624 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002625 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002626 if (cdb[1] & 0x8)
2627 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002628 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2629 break;
2630 case WRITE_12:
2631 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2632 if (sector_ret)
2633 goto out_unsupported_cdb;
2634 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002635 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002636 if (cdb[1] & 0x8)
2637 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002638 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2639 break;
2640 case WRITE_16:
2641 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2642 if (sector_ret)
2643 goto out_unsupported_cdb;
2644 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002645 cmd->t_task_lba = transport_lba_64(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002646 if (cdb[1] & 0x8)
2647 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002648 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2649 break;
2650 case XDWRITEREAD_10:
2651 if ((cmd->data_direction != DMA_TO_DEVICE) ||
Christoph Hellwig33c3faf2011-11-14 11:36:30 -05002652 !(cmd->se_cmd_flags & SCF_BIDI))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002653 goto out_invalid_cdb_field;
2654 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2655 if (sector_ret)
2656 goto out_unsupported_cdb;
2657 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002658 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002659 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002660
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002661 /*
2662 * Do now allow BIDI commands for passthrough mode.
2663 */
2664 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002665 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002666
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002667 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002668 * Setup BIDI XOR callback to be run after I/O completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002669 */
2670 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002671 if (cdb[1] & 0x8)
2672 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002673 break;
2674 case VARIABLE_LENGTH_CMD:
2675 service_action = get_unaligned_be16(&cdb[8]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002676 switch (service_action) {
2677 case XDWRITEREAD_32:
2678 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2679 if (sector_ret)
2680 goto out_unsupported_cdb;
2681 size = transport_get_size(sectors, cdb, cmd);
2682 /*
2683 * Use WRITE_32 and READ_32 opcodes for the emulated
2684 * XDWRITE_READ_32 logic.
2685 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002686 cmd->t_task_lba = transport_lba_64_ext(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002687 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2688
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002689 /*
2690 * Do now allow BIDI commands for passthrough mode.
2691 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002692 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002693 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002694
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002695 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002696 * Setup BIDI XOR callback to be run during after I/O
2697 * completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002698 */
2699 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002700 if (cdb[1] & 0x8)
2701 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002702 break;
2703 case WRITE_SAME_32:
2704 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2705 if (sector_ret)
2706 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002707
Andy Grover6708bb22011-06-08 10:36:43 -07002708 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002709 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002710 else {
2711 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2712 " supported\n");
2713 goto out_invalid_cdb_field;
2714 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002715
Andy Grovera1d8b492011-05-02 17:12:10 -07002716 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002717 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2718
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002719 if (target_check_write_same_discard(&cdb[10], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08002720 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002721 if (!passthrough)
2722 cmd->execute_task = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002723 break;
2724 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002725 pr_err("VARIABLE_LENGTH_CMD service action"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002726 " 0x%04x not supported\n", service_action);
2727 goto out_unsupported_cdb;
2728 }
2729 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002730 case MAINTENANCE_IN:
Andy Grovere3d6f902011-07-19 08:55:10 +00002731 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002732 /* MAINTENANCE_IN from SCC-2 */
2733 /*
2734 * Check for emulated MI_REPORT_TARGET_PGS.
2735 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002736 if (cdb[1] == MI_REPORT_TARGET_PGS &&
2737 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2738 cmd->execute_task =
2739 target_emulate_report_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002740 }
2741 size = (cdb[6] << 24) | (cdb[7] << 16) |
2742 (cdb[8] << 8) | cdb[9];
2743 } else {
2744 /* GPCMD_SEND_KEY from multi media commands */
2745 size = (cdb[8] << 8) + cdb[9];
2746 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002747 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002748 break;
2749 case MODE_SELECT:
2750 size = cdb[4];
2751 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2752 break;
2753 case MODE_SELECT_10:
2754 size = (cdb[7] << 8) + cdb[8];
2755 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2756 break;
2757 case MODE_SENSE:
2758 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002759 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002760 if (!passthrough)
2761 cmd->execute_task = target_emulate_modesense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002762 break;
2763 case MODE_SENSE_10:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002764 size = (cdb[7] << 8) + cdb[8];
2765 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2766 if (!passthrough)
2767 cmd->execute_task = target_emulate_modesense;
2768 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002769 case GPCMD_READ_BUFFER_CAPACITY:
2770 case GPCMD_SEND_OPC:
2771 case LOG_SELECT:
2772 case LOG_SENSE:
2773 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002774 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002775 break;
2776 case READ_BLOCK_LIMITS:
2777 size = READ_BLOCK_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002778 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002779 break;
2780 case GPCMD_GET_CONFIGURATION:
2781 case GPCMD_READ_FORMAT_CAPACITIES:
2782 case GPCMD_READ_DISC_INFO:
2783 case GPCMD_READ_TRACK_RZONE_INFO:
2784 size = (cdb[7] << 8) + cdb[8];
2785 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2786 break;
2787 case PERSISTENT_RESERVE_IN:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002788 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002789 cmd->execute_task = target_scsi3_emulate_pr_in;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002790 size = (cdb[7] << 8) + cdb[8];
2791 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2792 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002793 case PERSISTENT_RESERVE_OUT:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002794 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002795 cmd->execute_task = target_scsi3_emulate_pr_out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002796 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002797 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002798 break;
2799 case GPCMD_MECHANISM_STATUS:
2800 case GPCMD_READ_DVD_STRUCTURE:
2801 size = (cdb[8] << 8) + cdb[9];
2802 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2803 break;
2804 case READ_POSITION:
2805 size = READ_POSITION_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002806 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002807 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002808 case MAINTENANCE_OUT:
Andy Grovere3d6f902011-07-19 08:55:10 +00002809 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002810 /* MAINTENANCE_OUT from SCC-2
2811 *
2812 * Check for emulated MO_SET_TARGET_PGS.
2813 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002814 if (cdb[1] == MO_SET_TARGET_PGS &&
2815 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2816 cmd->execute_task =
2817 target_emulate_set_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002818 }
2819
2820 size = (cdb[6] << 24) | (cdb[7] << 16) |
2821 (cdb[8] << 8) | cdb[9];
2822 } else {
2823 /* GPCMD_REPORT_KEY from multi media commands */
2824 size = (cdb[8] << 8) + cdb[9];
2825 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002826 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002827 break;
2828 case INQUIRY:
2829 size = (cdb[3] << 8) + cdb[4];
2830 /*
2831 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2832 * See spc4r17 section 5.3
2833 */
Andy Grover5951146d2011-07-19 10:26:37 +00002834 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002835 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002836 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002837 if (!passthrough)
2838 cmd->execute_task = target_emulate_inquiry;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002839 break;
2840 case READ_BUFFER:
2841 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002842 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002843 break;
2844 case READ_CAPACITY:
2845 size = READ_CAP_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002846 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002847 if (!passthrough)
2848 cmd->execute_task = target_emulate_readcapacity;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002849 break;
2850 case READ_MEDIA_SERIAL_NUMBER:
2851 case SECURITY_PROTOCOL_IN:
2852 case SECURITY_PROTOCOL_OUT:
2853 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002854 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002855 break;
2856 case SERVICE_ACTION_IN:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002857 switch (cmd->t_task_cdb[1] & 0x1f) {
2858 case SAI_READ_CAPACITY_16:
2859 if (!passthrough)
2860 cmd->execute_task =
2861 target_emulate_readcapacity_16;
2862 break;
2863 default:
2864 if (passthrough)
2865 break;
2866
2867 pr_err("Unsupported SA: 0x%02x\n",
2868 cmd->t_task_cdb[1] & 0x1f);
2869 goto out_unsupported_cdb;
2870 }
2871 /*FALLTHROUGH*/
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002872 case ACCESS_CONTROL_IN:
2873 case ACCESS_CONTROL_OUT:
2874 case EXTENDED_COPY:
2875 case READ_ATTRIBUTE:
2876 case RECEIVE_COPY_RESULTS:
2877 case WRITE_ATTRIBUTE:
2878 size = (cdb[10] << 24) | (cdb[11] << 16) |
2879 (cdb[12] << 8) | cdb[13];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002880 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002881 break;
2882 case RECEIVE_DIAGNOSTIC:
2883 case SEND_DIAGNOSTIC:
2884 size = (cdb[3] << 8) | cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002885 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002886 break;
2887/* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2888#if 0
2889 case GPCMD_READ_CD:
2890 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2891 size = (2336 * sectors);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002892 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002893 break;
2894#endif
2895 case READ_TOC:
2896 size = cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002897 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002898 break;
2899 case REQUEST_SENSE:
2900 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002901 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002902 if (!passthrough)
2903 cmd->execute_task = target_emulate_request_sense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002904 break;
2905 case READ_ELEMENT_STATUS:
2906 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002907 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002908 break;
2909 case WRITE_BUFFER:
2910 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002911 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002912 break;
2913 case RESERVE:
2914 case RESERVE_10:
2915 /*
2916 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2917 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2918 */
2919 if (cdb[0] == RESERVE_10)
2920 size = (cdb[7] << 8) | cdb[8];
2921 else
2922 size = cmd->data_length;
2923
2924 /*
2925 * Setup the legacy emulated handler for SPC-2 and
2926 * >= SPC-3 compatible reservation handling (CRH=1)
2927 * Otherwise, we assume the underlying SCSI logic is
2928 * is running in SPC_PASSTHROUGH, and wants reservations
2929 * emulation disabled.
2930 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002931 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2932 cmd->execute_task = target_scsi2_reservation_reserve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002933 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2934 break;
2935 case RELEASE:
2936 case RELEASE_10:
2937 /*
2938 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2939 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2940 */
2941 if (cdb[0] == RELEASE_10)
2942 size = (cdb[7] << 8) | cdb[8];
2943 else
2944 size = cmd->data_length;
2945
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002946 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2947 cmd->execute_task = target_scsi2_reservation_release;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002948 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2949 break;
2950 case SYNCHRONIZE_CACHE:
Andy Grover8e94b8d2012-01-16 16:57:07 -08002951 case SYNCHRONIZE_CACHE_16:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002952 /*
2953 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2954 */
2955 if (cdb[0] == SYNCHRONIZE_CACHE) {
2956 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002957 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002958 } else {
2959 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002960 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002961 }
2962 if (sector_ret)
2963 goto out_unsupported_cdb;
2964
2965 size = transport_get_size(sectors, cdb, cmd);
2966 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2967
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002968 if (passthrough)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002969 break;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002970
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002971 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002972 * Check to ensure that LBA + Range does not exceed past end of
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002973 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002974 */
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002975 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
2976 if (transport_cmd_get_valid_sectors(cmd) < 0)
2977 goto out_invalid_cdb_field;
2978 }
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002979 cmd->execute_task = target_emulate_synchronize_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002980 break;
2981 case UNMAP:
2982 size = get_unaligned_be16(&cdb[7]);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002983 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002984 if (!passthrough)
2985 cmd->execute_task = target_emulate_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002986 break;
2987 case WRITE_SAME_16:
2988 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2989 if (sector_ret)
2990 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002991
Andy Grover6708bb22011-06-08 10:36:43 -07002992 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002993 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002994 else {
2995 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2996 goto out_invalid_cdb_field;
2997 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002998
Nicholas Bellinger5db07532011-07-27 22:18:52 -07002999 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003000 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003001
3002 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08003003 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003004 if (!passthrough)
3005 cmd->execute_task = target_emulate_write_same;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003006 break;
3007 case WRITE_SAME:
3008 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3009 if (sector_ret)
3010 goto out_unsupported_cdb;
3011
3012 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07003013 size = transport_get_size(1, cdb, cmd);
Nicholas Bellinger706d5862011-07-28 00:07:03 -07003014 else {
3015 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3016 goto out_invalid_cdb_field;
3017 }
3018
3019 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
3020 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3021 /*
3022 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3023 * of byte 1 bit 3 UNMAP instead of original reserved field
3024 */
3025 if (target_check_write_same_discard(&cdb[1], dev) < 0)
Martin Svec67236c42012-02-06 22:13:25 -08003026 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003027 if (!passthrough)
3028 cmd->execute_task = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003029 break;
3030 case ALLOW_MEDIUM_REMOVAL:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003031 case ERASE:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003032 case REZERO_UNIT:
3033 case SEEK_10:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003034 case SPACE:
3035 case START_STOP:
3036 case TEST_UNIT_READY:
3037 case VERIFY:
3038 case WRITE_FILEMARKS:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003039 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3040 if (!passthrough)
3041 cmd->execute_task = target_emulate_noop;
3042 break;
3043 case GPCMD_CLOSE_TRACK:
3044 case INITIALIZE_ELEMENT_STATUS:
3045 case GPCMD_LOAD_UNLOAD:
3046 case GPCMD_SET_SPEED:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003047 case MOVE_MEDIUM:
3048 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3049 break;
3050 case REPORT_LUNS:
Christoph Hellwige76a35d2011-11-03 17:50:42 -04003051 cmd->execute_task = target_report_luns;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003052 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3053 /*
3054 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3055 * See spc4r17 section 5.3
3056 */
Andy Grover5951146d2011-07-19 10:26:37 +00003057 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003058 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003059 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003060 break;
3061 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003062 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003063 " 0x%02x, sending CHECK_CONDITION.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003064 cmd->se_tfo->get_fabric_name(), cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003065 goto out_unsupported_cdb;
3066 }
3067
3068 if (size != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003069 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003070 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
Andy Grovere3d6f902011-07-19 08:55:10 +00003071 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003072 cmd->data_length, size, cdb[0]);
3073
3074 cmd->cmd_spdtl = size;
3075
3076 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grover6708bb22011-06-08 10:36:43 -07003077 pr_err("Rejecting underflow/overflow"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003078 " WRITE data\n");
3079 goto out_invalid_cdb_field;
3080 }
3081 /*
3082 * Reject READ_* or WRITE_* with overflow/underflow for
3083 * type SCF_SCSI_DATA_SG_IO_CDB.
3084 */
Andy Grover6708bb22011-06-08 10:36:43 -07003085 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3086 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003087 " CDB on non 512-byte sector setup subsystem"
Andy Grovere3d6f902011-07-19 08:55:10 +00003088 " plugin: %s\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003089 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3090 goto out_invalid_cdb_field;
3091 }
3092
3093 if (size > cmd->data_length) {
3094 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3095 cmd->residual_count = (size - cmd->data_length);
3096 } else {
3097 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3098 cmd->residual_count = (cmd->data_length - size);
3099 }
3100 cmd->data_length = size;
3101 }
3102
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003103 /* reject any command that we don't have a handler for */
3104 if (!(passthrough || cmd->execute_task ||
3105 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3106 goto out_unsupported_cdb;
3107
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003108 transport_set_supported_SAM_opcode(cmd);
3109 return ret;
3110
3111out_unsupported_cdb:
3112 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3113 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover5951146d2011-07-19 10:26:37 +00003114 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003115out_invalid_cdb_field:
3116 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3117 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00003118 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003119}
3120
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003121/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003122 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003123 * and ordered cmds need to have their tasks added to the execution queue.
3124 */
3125static void transport_complete_task_attr(struct se_cmd *cmd)
3126{
Andy Grover5951146d2011-07-19 10:26:37 +00003127 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003128 struct se_cmd *cmd_p, *cmd_tmp;
3129 int new_active_tasks = 0;
3130
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003131 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003132 atomic_dec(&dev->simple_cmds);
3133 smp_mb__after_atomic_dec();
3134 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003135 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003136 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3137 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003138 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003139 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003140 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003141 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3142 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003143 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003144 atomic_dec(&dev->dev_ordered_sync);
3145 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003146
3147 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003148 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003149 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3150 }
3151 /*
3152 * Process all commands up to the last received
3153 * ORDERED task attribute which requires another blocking
3154 * boundary
3155 */
3156 spin_lock(&dev->delayed_cmd_lock);
3157 list_for_each_entry_safe(cmd_p, cmd_tmp,
Andy Grover5951146d2011-07-19 10:26:37 +00003158 &dev->delayed_cmd_list, se_delayed_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003159
Andy Grover5951146d2011-07-19 10:26:37 +00003160 list_del(&cmd_p->se_delayed_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003161 spin_unlock(&dev->delayed_cmd_lock);
3162
Andy Grover6708bb22011-06-08 10:36:43 -07003163 pr_debug("Calling add_tasks() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003164 " cmd_p: 0x%02x Task Attr: 0x%02x"
3165 " Dormant -> Active, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07003166 cmd_p->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003167 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3168
3169 transport_add_tasks_from_cmd(cmd_p);
3170 new_active_tasks++;
3171
3172 spin_lock(&dev->delayed_cmd_lock);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003173 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003174 break;
3175 }
3176 spin_unlock(&dev->delayed_cmd_lock);
3177 /*
3178 * If new tasks have become active, wake up the transport thread
3179 * to do the processing of the Active tasks.
3180 */
3181 if (new_active_tasks != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003182 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003183}
3184
Christoph Hellwige057f532011-10-17 13:56:41 -04003185static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003186{
3187 int ret = 0;
3188
Christoph Hellwige057f532011-10-17 13:56:41 -04003189 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3190 transport_complete_task_attr(cmd);
3191
3192 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3193 ret = cmd->se_tfo->queue_status(cmd);
3194 if (ret)
3195 goto out;
3196 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003197
3198 switch (cmd->data_direction) {
3199 case DMA_FROM_DEVICE:
3200 ret = cmd->se_tfo->queue_data_in(cmd);
3201 break;
3202 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00003203 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003204 ret = cmd->se_tfo->queue_data_in(cmd);
3205 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04003206 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003207 }
3208 /* Fall through for DMA_TO_DEVICE */
3209 case DMA_NONE:
3210 ret = cmd->se_tfo->queue_status(cmd);
3211 break;
3212 default:
3213 break;
3214 }
3215
Christoph Hellwige057f532011-10-17 13:56:41 -04003216out:
3217 if (ret < 0) {
3218 transport_handle_queue_full(cmd, cmd->se_dev);
3219 return;
3220 }
3221 transport_lun_remove_cmd(cmd);
3222 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003223}
3224
3225static void transport_handle_queue_full(
3226 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04003227 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003228{
3229 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003230 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3231 atomic_inc(&dev->dev_qf_count);
3232 smp_mb__after_atomic_inc();
3233 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3234
3235 schedule_work(&cmd->se_dev->qf_work_queue);
3236}
3237
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003238static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003239{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003240 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003241 int reason = 0, ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003242
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003243 /*
3244 * Check if we need to move delayed/dormant tasks from cmds on the
3245 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3246 * Attribute.
3247 */
Andy Grover5951146d2011-07-19 10:26:37 +00003248 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003249 transport_complete_task_attr(cmd);
3250 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003251 * Check to schedule QUEUE_FULL work, or execute an existing
3252 * cmd->transport_qf_callback()
3253 */
3254 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3255 schedule_work(&cmd->se_dev->qf_work_queue);
3256
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003257 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003258 * Check if we need to retrieve a sense buffer from
3259 * the struct se_cmd in question.
3260 */
3261 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3262 if (transport_get_sense_data(cmd) < 0)
3263 reason = TCM_NON_EXISTENT_LUN;
3264
3265 /*
3266 * Only set when an struct se_task->task_scsi_status returned
3267 * a non GOOD status.
3268 */
3269 if (cmd->scsi_status) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003270 ret = transport_send_check_condition_and_sense(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003271 cmd, reason, 1);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003272 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003273 goto queue_full;
3274
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003275 transport_lun_remove_cmd(cmd);
3276 transport_cmd_check_stop_to_fabric(cmd);
3277 return;
3278 }
3279 }
3280 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003281 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003282 * XDWRITE_READ_10 emulation.
3283 */
3284 if (cmd->transport_complete_callback)
3285 cmd->transport_complete_callback(cmd);
3286
3287 switch (cmd->data_direction) {
3288 case DMA_FROM_DEVICE:
3289 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003290 if (cmd->se_lun->lun_sep) {
3291 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003292 cmd->data_length;
3293 }
3294 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003295
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003296 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003297 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003298 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003299 break;
3300 case DMA_TO_DEVICE:
3301 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003302 if (cmd->se_lun->lun_sep) {
3303 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003304 cmd->data_length;
3305 }
3306 spin_unlock(&cmd->se_lun->lun_sep_lock);
3307 /*
3308 * Check if we need to send READ payload for BIDI-COMMAND
3309 */
Andy Groverec98f782011-07-20 19:28:46 +00003310 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003311 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003312 if (cmd->se_lun->lun_sep) {
3313 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003314 cmd->data_length;
3315 }
3316 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003317 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003318 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003319 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003320 break;
3321 }
3322 /* Fall through for DMA_TO_DEVICE */
3323 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003324 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003325 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003326 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003327 break;
3328 default:
3329 break;
3330 }
3331
3332 transport_lun_remove_cmd(cmd);
3333 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003334 return;
3335
3336queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003337 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003338 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04003339 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3340 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003341}
3342
3343static void transport_free_dev_tasks(struct se_cmd *cmd)
3344{
3345 struct se_task *task, *task_tmp;
3346 unsigned long flags;
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003347 LIST_HEAD(dispose_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003348
Andy Grovera1d8b492011-05-02 17:12:10 -07003349 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003350 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07003351 &cmd->t_task_list, t_list) {
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003352 if (!(task->task_flags & TF_ACTIVE))
3353 list_move_tail(&task->t_list, &dispose_list);
3354 }
3355 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3356
3357 while (!list_empty(&dispose_list)) {
3358 task = list_first_entry(&dispose_list, struct se_task, t_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003359
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003360 if (task->task_sg != cmd->t_data_sg &&
3361 task->task_sg != cmd->t_bidi_data_sg)
3362 kfree(task->task_sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003363
3364 list_del(&task->t_list);
3365
Christoph Hellwig42bf8292011-10-12 11:07:00 -04003366 cmd->se_dev->transport->free_task(task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003367 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003368}
3369
Andy Grover6708bb22011-06-08 10:36:43 -07003370static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003371{
Andy Groverec98f782011-07-20 19:28:46 +00003372 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00003373 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003374
Andy Grover6708bb22011-06-08 10:36:43 -07003375 for_each_sg(sgl, sg, nents, count)
3376 __free_page(sg_page(sg));
3377
3378 kfree(sgl);
3379}
3380
3381static inline void transport_free_pages(struct se_cmd *cmd)
3382{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003383 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07003384 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003385
Andy Grover6708bb22011-06-08 10:36:43 -07003386 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003387 cmd->t_data_sg = NULL;
3388 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003389
Andy Grover6708bb22011-06-08 10:36:43 -07003390 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003391 cmd->t_bidi_data_sg = NULL;
3392 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003393}
3394
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003395/**
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003396 * transport_release_cmd - free a command
3397 * @cmd: command to free
3398 *
3399 * This routine unconditionally frees a command, and reference counting
3400 * or list removal must be done in the caller.
3401 */
3402static void transport_release_cmd(struct se_cmd *cmd)
3403{
3404 BUG_ON(!cmd->se_tfo);
3405
Andy Groverc8e31f22012-01-19 13:39:17 -08003406 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003407 core_tmr_release_req(cmd->se_tmr_req);
3408 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3409 kfree(cmd->t_task_cdb);
3410 /*
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003411 * If this cmd has been setup with target_get_sess_cmd(), drop
3412 * the kref and call ->release_cmd() in kref callback.
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003413 */
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08003414 if (cmd->check_release != 0) {
3415 target_put_sess_cmd(cmd->se_sess, cmd);
3416 return;
3417 }
Christoph Hellwige26d99a2011-11-14 12:30:30 -05003418 cmd->se_tfo->release_cmd(cmd);
3419}
3420
3421/**
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003422 * transport_put_cmd - release a reference to a command
3423 * @cmd: command to release
3424 *
3425 * This routine releases our reference to the command and frees it if possible.
3426 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003427static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003428{
3429 unsigned long flags;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003430 int free_tasks = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003431
Andy Grovera1d8b492011-05-02 17:12:10 -07003432 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003433 if (atomic_read(&cmd->t_fe_count)) {
3434 if (!atomic_dec_and_test(&cmd->t_fe_count))
3435 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003436 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003437
3438 if (atomic_read(&cmd->t_se_count)) {
3439 if (!atomic_dec_and_test(&cmd->t_se_count))
3440 goto out_busy;
3441 }
3442
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003443 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
3444 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003445 transport_all_task_dev_remove_state(cmd);
3446 free_tasks = 1;
3447 }
Andy Grovera1d8b492011-05-02 17:12:10 -07003448 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003449
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003450 if (free_tasks != 0)
3451 transport_free_dev_tasks(cmd);
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003452
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003453 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02003454 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003455 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003456out_busy:
3457 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003458}
3459
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003460/*
Andy Groverec98f782011-07-20 19:28:46 +00003461 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3462 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003463 * @cmd: Associated se_cmd descriptor
3464 * @mem: SGL style memory for TCM WRITE / READ
3465 * @sg_mem_num: Number of SGL elements
3466 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3467 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3468 *
3469 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3470 * of parameters.
3471 */
3472int transport_generic_map_mem_to_cmd(
3473 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00003474 struct scatterlist *sgl,
3475 u32 sgl_count,
3476 struct scatterlist *sgl_bidi,
3477 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003478{
Andy Grover5951146d2011-07-19 10:26:37 +00003479 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003480 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003481
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003482 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3483 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
Nicholas Bellingerfef58a62011-11-15 22:13:24 -08003484 /*
3485 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3486 * scatterlists already have been set to follow what the fabric
3487 * passes for the original expected data transfer length.
3488 */
3489 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3490 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3491 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3492 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3493 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3494 return -EINVAL;
3495 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003496
Andy Groverec98f782011-07-20 19:28:46 +00003497 cmd->t_data_sg = sgl;
3498 cmd->t_data_nents = sgl_count;
3499
Andy Grover5951146d2011-07-19 10:26:37 +00003500 if (sgl_bidi && sgl_bidi_count) {
Andy Groverec98f782011-07-20 19:28:46 +00003501 cmd->t_bidi_data_sg = sgl_bidi;
3502 cmd->t_bidi_data_nents = sgl_bidi_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003503 }
3504 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003505 }
3506
3507 return 0;
3508}
3509EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3510
Andy Grover49493142012-01-16 16:57:08 -08003511void *transport_kmap_data_sg(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003512{
Andy Groverec98f782011-07-20 19:28:46 +00003513 struct scatterlist *sg = cmd->t_data_sg;
Andy Grover49493142012-01-16 16:57:08 -08003514 struct page **pages;
3515 int i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003516
Andy Groverec98f782011-07-20 19:28:46 +00003517 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003518 /*
Andy Groverec98f782011-07-20 19:28:46 +00003519 * We need to take into account a possible offset here for fabrics like
3520 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3521 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00003522 */
Andy Grover49493142012-01-16 16:57:08 -08003523 if (!cmd->t_data_nents)
3524 return NULL;
3525 else if (cmd->t_data_nents == 1)
3526 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003527
Andy Grover49493142012-01-16 16:57:08 -08003528 /* >1 page. use vmap */
3529 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
3530 if (!pages)
3531 return NULL;
3532
3533 /* convert sg[] to pages[] */
3534 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
3535 pages[i] = sg_page(sg);
3536 }
3537
3538 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
3539 kfree(pages);
3540 if (!cmd->t_data_vmap)
3541 return NULL;
3542
3543 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003544}
Andy Grover49493142012-01-16 16:57:08 -08003545EXPORT_SYMBOL(transport_kmap_data_sg);
3546
3547void transport_kunmap_data_sg(struct se_cmd *cmd)
3548{
3549 if (!cmd->t_data_nents)
3550 return;
3551 else if (cmd->t_data_nents == 1)
3552 kunmap(sg_page(cmd->t_data_sg));
3553
3554 vunmap(cmd->t_data_vmap);
3555 cmd->t_data_vmap = NULL;
3556}
3557EXPORT_SYMBOL(transport_kunmap_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003558
3559static int
3560transport_generic_get_mem(struct se_cmd *cmd)
3561{
Andy Groverec98f782011-07-20 19:28:46 +00003562 u32 length = cmd->data_length;
3563 unsigned int nents;
3564 struct page *page;
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003565 gfp_t zero_flag;
Andy Groverec98f782011-07-20 19:28:46 +00003566 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003567
Andy Groverec98f782011-07-20 19:28:46 +00003568 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3569 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3570 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00003571 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003572
Andy Groverec98f782011-07-20 19:28:46 +00003573 cmd->t_data_nents = nents;
3574 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003575
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003576 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
3577
Andy Groverec98f782011-07-20 19:28:46 +00003578 while (length) {
3579 u32 page_len = min_t(u32, length, PAGE_SIZE);
roland@purestorage.com9db9da32012-01-04 15:59:58 -08003580 page = alloc_page(GFP_KERNEL | zero_flag);
Andy Groverec98f782011-07-20 19:28:46 +00003581 if (!page)
3582 goto out;
3583
3584 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3585 length -= page_len;
3586 i++;
3587 }
3588 return 0;
3589
3590out:
3591 while (i >= 0) {
3592 __free_page(sg_page(&cmd->t_data_sg[i]));
3593 i--;
3594 }
3595 kfree(cmd->t_data_sg);
3596 cmd->t_data_sg = NULL;
3597 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003598}
3599
Andy Grovera1d8b492011-05-02 17:12:10 -07003600/* Reduce sectors if they are too long for the device */
3601static inline sector_t transport_limit_task_sectors(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003602 struct se_device *dev,
3603 unsigned long long lba,
Andy Grovera1d8b492011-05-02 17:12:10 -07003604 sector_t sectors)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003605{
Andy Grovera1d8b492011-05-02 17:12:10 -07003606 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003607
Andy Grovera1d8b492011-05-02 17:12:10 -07003608 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3609 if ((lba + sectors) > transport_dev_end_lba(dev))
3610 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003611
Andy Grovera1d8b492011-05-02 17:12:10 -07003612 return sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003613}
3614
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003615
3616/*
3617 * This function can be used by HW target mode drivers to create a linked
3618 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3619 * This is intended to be called during the completion path by TCM Core
3620 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3621 */
3622void transport_do_task_sg_chain(struct se_cmd *cmd)
3623{
Andy Groverec98f782011-07-20 19:28:46 +00003624 struct scatterlist *sg_first = NULL;
3625 struct scatterlist *sg_prev = NULL;
3626 int sg_prev_nents = 0;
3627 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003628 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003629 u32 chained_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003630 int i;
3631
Andy Groverec98f782011-07-20 19:28:46 +00003632 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3633
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003634 /*
3635 * Walk the struct se_task list and setup scatterlist chains
Andy Grovera1d8b492011-05-02 17:12:10 -07003636 * for each contiguously allocated struct se_task->task_sg[].
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003637 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003638 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Andy Groverec98f782011-07-20 19:28:46 +00003639 if (!task->task_sg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003640 continue;
3641
Andy Groverec98f782011-07-20 19:28:46 +00003642 if (!sg_first) {
3643 sg_first = task->task_sg;
Andy Grover6708bb22011-06-08 10:36:43 -07003644 chained_nents = task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003645 } else {
Andy Groverec98f782011-07-20 19:28:46 +00003646 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
Andy Grover6708bb22011-06-08 10:36:43 -07003647 chained_nents += task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003648 }
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003649 /*
3650 * For the padded tasks, use the extra SGL vector allocated
3651 * in transport_allocate_data_tasks() for the sg_prev_nents
Christoph Hellwig04629b72011-10-12 11:07:04 -04003652 * offset into sg_chain() above.
3653 *
3654 * We do not need the padding for the last task (or a single
3655 * task), but in that case we will never use the sg_prev_nents
3656 * value below which would be incorrect.
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003657 */
Christoph Hellwig04629b72011-10-12 11:07:04 -04003658 sg_prev_nents = (task->task_sg_nents + 1);
Andy Groverec98f782011-07-20 19:28:46 +00003659 sg_prev = task->task_sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003660 }
3661 /*
3662 * Setup the starting pointer and total t_tasks_sg_linked_no including
3663 * padding SGs for linking and to mark the end.
3664 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003665 cmd->t_tasks_sg_chained = sg_first;
Andy Groverec98f782011-07-20 19:28:46 +00003666 cmd->t_tasks_sg_chained_no = chained_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003667
Andy Grover6708bb22011-06-08 10:36:43 -07003668 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
Andy Grovera1d8b492011-05-02 17:12:10 -07003669 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3670 cmd->t_tasks_sg_chained_no);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003671
Andy Grovera1d8b492011-05-02 17:12:10 -07003672 for_each_sg(cmd->t_tasks_sg_chained, sg,
3673 cmd->t_tasks_sg_chained_no, i) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003674
Andy Grover6708bb22011-06-08 10:36:43 -07003675 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
Andy Grover5951146d2011-07-19 10:26:37 +00003676 i, sg, sg_page(sg), sg->length, sg->offset);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003677 if (sg_is_chain(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003678 pr_debug("SG: %p sg_is_chain=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003679 if (sg_is_last(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003680 pr_debug("SG: %p sg_is_last=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003681 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003682}
3683EXPORT_SYMBOL(transport_do_task_sg_chain);
3684
Andy Grovera1d8b492011-05-02 17:12:10 -07003685/*
3686 * Break up cmd into chunks transport can handle
3687 */
Christoph Hellwig38b400672011-10-18 06:57:02 -04003688static int
3689transport_allocate_data_tasks(struct se_cmd *cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003690 enum dma_data_direction data_direction,
Christoph Hellwig38b400672011-10-18 06:57:02 -04003691 struct scatterlist *cmd_sg, unsigned int sgl_nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003692{
Andy Grover5951146d2011-07-19 10:26:37 +00003693 struct se_device *dev = cmd->se_dev;
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003694 int task_count, i;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003695 unsigned long long lba;
3696 sector_t sectors, dev_max_sectors;
3697 u32 sector_size;
3698
3699 if (transport_cmd_get_valid_sectors(cmd) < 0)
3700 return -EINVAL;
3701
3702 dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3703 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003704
Andy Groverec98f782011-07-20 19:28:46 +00003705 WARN_ON(cmd->data_length % sector_size);
Christoph Hellwig38b400672011-10-18 06:57:02 -04003706
3707 lba = cmd->t_task_lba;
Andy Groverec98f782011-07-20 19:28:46 +00003708 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
Nicholas Bellinger277c5f22011-07-26 00:38:40 -07003709 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003710
3711 /*
3712 * If we need just a single task reuse the SG list in the command
3713 * and avoid a lot of work.
3714 */
3715 if (task_count == 1) {
3716 struct se_task *task;
3717 unsigned long flags;
3718
3719 task = transport_generic_get_task(cmd, data_direction);
3720 if (!task)
3721 return -ENOMEM;
3722
3723 task->task_sg = cmd_sg;
3724 task->task_sg_nents = sgl_nents;
3725
3726 task->task_lba = lba;
3727 task->task_sectors = sectors;
3728 task->task_size = task->task_sectors * sector_size;
3729
3730 spin_lock_irqsave(&cmd->t_state_lock, flags);
3731 list_add_tail(&task->t_list, &cmd->t_task_list);
3732 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3733
3734 return task_count;
3735 }
3736
Andy Groverec98f782011-07-20 19:28:46 +00003737 for (i = 0; i < task_count; i++) {
Christoph Hellwig38b400672011-10-18 06:57:02 -04003738 struct se_task *task;
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003739 unsigned int task_size, task_sg_nents_padded;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003740 struct scatterlist *sg;
3741 unsigned long flags;
Andy Groverec98f782011-07-20 19:28:46 +00003742 int count;
Andy Grovera1d8b492011-05-02 17:12:10 -07003743
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003744 task = transport_generic_get_task(cmd, data_direction);
Andy Grovera1d8b492011-05-02 17:12:10 -07003745 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003746 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003747
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003748 task->task_lba = lba;
Andy Groverec98f782011-07-20 19:28:46 +00003749 task->task_sectors = min(sectors, dev_max_sectors);
3750 task->task_size = task->task_sectors * sector_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003751
Nicholas Bellinger525a48a2011-08-13 02:11:38 -07003752 /*
3753 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3754 * in order to calculate the number per task SGL entries
3755 */
3756 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003757 /*
Andy Groverec98f782011-07-20 19:28:46 +00003758 * Check if the fabric module driver is requesting that all
3759 * struct se_task->task_sg[] be chained together.. If so,
3760 * then allocate an extra padding SG entry for linking and
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003761 * marking the end of the chained SGL for every task except
3762 * the last one for (task_count > 1) operation, or skipping
3763 * the extra padding for the (task_count == 1) case.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003764 */
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003765 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3766 task_sg_nents_padded = (task->task_sg_nents + 1);
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003767 } else
3768 task_sg_nents_padded = task->task_sg_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003769
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003770 task->task_sg = kmalloc(sizeof(struct scatterlist) *
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003771 task_sg_nents_padded, GFP_KERNEL);
Andy Groverec98f782011-07-20 19:28:46 +00003772 if (!task->task_sg) {
3773 cmd->se_dev->transport->free_task(task);
3774 return -ENOMEM;
3775 }
3776
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003777 sg_init_table(task->task_sg, task_sg_nents_padded);
Andy Groverec98f782011-07-20 19:28:46 +00003778
3779 task_size = task->task_size;
3780
3781 /* Build new sgl, only up to task_size */
Andy Grover6708bb22011-06-08 10:36:43 -07003782 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
Andy Groverec98f782011-07-20 19:28:46 +00003783 if (cmd_sg->length > task_size)
3784 break;
3785
3786 *sg = *cmd_sg;
3787 task_size -= cmd_sg->length;
3788 cmd_sg = sg_next(cmd_sg);
3789 }
3790
3791 lba += task->task_sectors;
3792 sectors -= task->task_sectors;
3793
3794 spin_lock_irqsave(&cmd->t_state_lock, flags);
3795 list_add_tail(&task->t_list, &cmd->t_task_list);
3796 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003797 }
3798
Andy Groverec98f782011-07-20 19:28:46 +00003799 return task_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003800}
3801
3802static int
Andy Groverec98f782011-07-20 19:28:46 +00003803transport_allocate_control_task(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003804{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003805 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003806 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003807
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08003808 /* Workaround for handling zero-length control CDBs */
3809 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3810 !cmd->data_length)
3811 return 0;
3812
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003813 task = transport_generic_get_task(cmd, cmd->data_direction);
3814 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003815 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003816
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003817 task->task_sg = cmd->t_data_sg;
Andy Groverec98f782011-07-20 19:28:46 +00003818 task->task_size = cmd->data_length;
Andy Grover6708bb22011-06-08 10:36:43 -07003819 task->task_sg_nents = cmd->t_data_nents;
Andy Groverec98f782011-07-20 19:28:46 +00003820
3821 spin_lock_irqsave(&cmd->t_state_lock, flags);
3822 list_add_tail(&task->t_list, &cmd->t_task_list);
3823 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003824
Andy Grover6708bb22011-06-08 10:36:43 -07003825 /* Success! Return number of tasks allocated */
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003826 return 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003827}
3828
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003829/*
3830 * Allocate any required ressources to execute the command, and either place
3831 * it on the execution queue if possible. For writes we might not have the
3832 * payload yet, thus notify the fabric via a call to ->write_pending instead.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003833 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003834int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003835{
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003836 struct se_device *dev = cmd->se_dev;
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003837 int task_cdbs, task_cdbs_bidi = 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003838 int set_counts = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003839 int ret = 0;
3840
3841 /*
3842 * Determine is the TCM fabric module has already allocated physical
3843 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00003844 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003845 */
Andy Groverec98f782011-07-20 19:28:46 +00003846 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3847 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00003848 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003849 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003850 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003851 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003852
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003853 /*
Christoph Hellwig38b400672011-10-18 06:57:02 -04003854 * For BIDI command set up the read tasks first.
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003855 */
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003856 if (cmd->t_bidi_data_sg &&
Christoph Hellwig38b400672011-10-18 06:57:02 -04003857 dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
3858 BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
3859
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003860 task_cdbs_bidi = transport_allocate_data_tasks(cmd,
3861 DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
3862 cmd->t_bidi_data_nents);
3863 if (task_cdbs_bidi <= 0)
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003864 goto out_fail;
3865
3866 atomic_inc(&cmd->t_fe_count);
3867 atomic_inc(&cmd->t_se_count);
3868 set_counts = 0;
3869 }
Christoph Hellwig38b400672011-10-18 06:57:02 -04003870
3871 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3872 task_cdbs = transport_allocate_data_tasks(cmd,
3873 cmd->data_direction, cmd->t_data_sg,
3874 cmd->t_data_nents);
3875 } else {
3876 task_cdbs = transport_allocate_control_task(cmd);
3877 }
3878
Roland Dreier410f6702011-11-22 13:51:32 -08003879 if (task_cdbs < 0)
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003880 goto out_fail;
Roland Dreier410f6702011-11-22 13:51:32 -08003881 else if (!task_cdbs && (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003882 spin_lock_irq(&cmd->t_state_lock);
Roland Dreier410f6702011-11-22 13:51:32 -08003883 cmd->t_state = TRANSPORT_COMPLETE;
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003884 cmd->transport_state |= CMD_T_ACTIVE;
3885 spin_unlock_irq(&cmd->t_state_lock);
Nicholas Bellinger91ec1d32012-01-13 12:01:34 -08003886
3887 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
3888 u8 ua_asc = 0, ua_ascq = 0;
3889
3890 core_scsi3_ua_clear_for_request_sense(cmd,
3891 &ua_asc, &ua_ascq);
3892 }
3893
Roland Dreier410f6702011-11-22 13:51:32 -08003894 INIT_WORK(&cmd->work, target_complete_ok_work);
3895 queue_work(target_completion_wq, &cmd->work);
3896 return 0;
3897 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003898
3899 if (set_counts) {
3900 atomic_inc(&cmd->t_fe_count);
3901 atomic_inc(&cmd->t_se_count);
3902 }
3903
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003904 cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
3905 atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
3906 atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003907
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003908 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07003909 * For WRITEs, let the fabric know its buffer is ready..
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003910 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3911 * will be added to the struct se_device execution queue after its WRITE
3912 * data has arrived. (ie: It gets handled by the transport processing
3913 * thread a second time)
3914 */
3915 if (cmd->data_direction == DMA_TO_DEVICE) {
3916 transport_add_tasks_to_state_queue(cmd);
3917 return transport_generic_write_pending(cmd);
3918 }
3919 /*
3920 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3921 * to the execution queue.
3922 */
3923 transport_execute_tasks(cmd);
3924 return 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003925
3926out_fail:
3927 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3928 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3929 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003930}
Andy Grovera1d8b492011-05-02 17:12:10 -07003931EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003932
3933/* transport_generic_process_write():
3934 *
3935 *
3936 */
3937void transport_generic_process_write(struct se_cmd *cmd)
3938{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003939 transport_execute_tasks(cmd);
3940}
3941EXPORT_SYMBOL(transport_generic_process_write);
3942
Christoph Hellwige057f532011-10-17 13:56:41 -04003943static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003944{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003945 int ret;
3946
3947 ret = cmd->se_tfo->write_pending(cmd);
3948 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04003949 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3950 cmd);
3951 transport_handle_queue_full(cmd, cmd->se_dev);
3952 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003953}
3954
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003955static int transport_generic_write_pending(struct se_cmd *cmd)
3956{
3957 unsigned long flags;
3958 int ret;
3959
Andy Grovera1d8b492011-05-02 17:12:10 -07003960 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003961 cmd->t_state = TRANSPORT_WRITE_PENDING;
Andy Grovera1d8b492011-05-02 17:12:10 -07003962 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003963
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003964 /*
3965 * Clear the se_cmd for WRITE_PENDING status in order to set
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05003966 * CMD_T_ACTIVE so that transport_generic_handle_data can be called
3967 * from HW target mode interrupt code. This is safe to be called
3968 * with transport_off=1 before the cmd->se_tfo->write_pending
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003969 * because the se_cmd->se_lun pointer is not being cleared.
3970 */
3971 transport_cmd_check_stop(cmd, 1, 0);
3972
3973 /*
3974 * Call the fabric write_pending function here to let the
3975 * frontend know that WRITE buffers are ready.
3976 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003977 ret = cmd->se_tfo->write_pending(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003978 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003979 goto queue_full;
3980 else if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003981 return ret;
3982
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003983 return 1;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003984
3985queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003986 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003987 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
Christoph Hellwige057f532011-10-17 13:56:41 -04003988 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003989 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003990}
3991
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003992void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003993{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003994 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
Andy Groverc8e31f22012-01-19 13:39:17 -08003995 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003996 transport_wait_for_tasks(cmd);
3997
Christoph Hellwig35462972011-05-31 23:56:57 -04003998 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003999 } else {
4000 if (wait_for_tasks)
4001 transport_wait_for_tasks(cmd);
4002
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004003 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
4004
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02004005 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004006 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004007
Nicholas Bellingerf4366772011-05-19 20:19:11 -07004008 transport_free_dev_tasks(cmd);
4009
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07004010 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004011 }
4012}
4013EXPORT_SYMBOL(transport_generic_free_cmd);
4014
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004015/* target_get_sess_cmd - Add command to active ->sess_cmd_list
4016 * @se_sess: session to reference
4017 * @se_cmd: command descriptor to add
Nicholas Bellingera6360782011-11-18 20:36:22 -08004018 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004019 */
Nicholas Bellingera6360782011-11-18 20:36:22 -08004020void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
4021 bool ack_kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004022{
4023 unsigned long flags;
4024
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004025 kref_init(&se_cmd->cmd_kref);
Nicholas Bellingera6360782011-11-18 20:36:22 -08004026 /*
4027 * Add a second kref if the fabric caller is expecting to handle
4028 * fabric acknowledgement that requires two target_put_sess_cmd()
4029 * invocations before se_cmd descriptor release.
4030 */
4031 if (ack_kref == true)
4032 kref_get(&se_cmd->cmd_kref);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004033
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004034 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4035 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
4036 se_cmd->check_release = 1;
4037 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4038}
4039EXPORT_SYMBOL(target_get_sess_cmd);
4040
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004041static void target_release_cmd_kref(struct kref *kref)
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004042{
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004043 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
4044 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004045 unsigned long flags;
4046
4047 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4048 if (list_empty(&se_cmd->se_cmd_list)) {
4049 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4050 WARN_ON(1);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004051 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004052 }
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004053 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
4054 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4055 complete(&se_cmd->cmd_wait_comp);
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004056 return;
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004057 }
4058 list_del(&se_cmd->se_cmd_list);
4059 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4060
Nicholas Bellinger7481deb2011-11-12 00:32:17 -08004061 se_cmd->se_tfo->release_cmd(se_cmd);
4062}
4063
4064/* target_put_sess_cmd - Check for active I/O shutdown via kref_put
4065 * @se_sess: session to reference
4066 * @se_cmd: command descriptor to drop
4067 */
4068int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
4069{
4070 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004071}
4072EXPORT_SYMBOL(target_put_sess_cmd);
4073
4074/* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
4075 * @se_sess: session to split
4076 */
4077void target_splice_sess_cmd_list(struct se_session *se_sess)
4078{
4079 struct se_cmd *se_cmd;
4080 unsigned long flags;
4081
4082 WARN_ON(!list_empty(&se_sess->sess_wait_list));
4083 INIT_LIST_HEAD(&se_sess->sess_wait_list);
4084
4085 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4086 se_sess->sess_tearing_down = 1;
4087
4088 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
4089
4090 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
4091 se_cmd->cmd_wait_set = 1;
4092
4093 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4094}
4095EXPORT_SYMBOL(target_splice_sess_cmd_list);
4096
4097/* target_wait_for_sess_cmds - Wait for outstanding descriptors
4098 * @se_sess: session to wait for active I/O
4099 * @wait_for_tasks: Make extra transport_wait_for_tasks call
4100 */
4101void target_wait_for_sess_cmds(
4102 struct se_session *se_sess,
4103 int wait_for_tasks)
4104{
4105 struct se_cmd *se_cmd, *tmp_cmd;
4106 bool rc = false;
4107
4108 list_for_each_entry_safe(se_cmd, tmp_cmd,
4109 &se_sess->sess_wait_list, se_cmd_list) {
4110 list_del(&se_cmd->se_cmd_list);
4111
4112 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
4113 " %d\n", se_cmd, se_cmd->t_state,
4114 se_cmd->se_tfo->get_cmd_state(se_cmd));
4115
4116 if (wait_for_tasks) {
4117 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
4118 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4119 se_cmd->se_tfo->get_cmd_state(se_cmd));
4120
4121 rc = transport_wait_for_tasks(se_cmd);
4122
4123 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
4124 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4125 se_cmd->se_tfo->get_cmd_state(se_cmd));
4126 }
4127
4128 if (!rc) {
4129 wait_for_completion(&se_cmd->cmd_wait_comp);
4130 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
4131 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4132 se_cmd->se_tfo->get_cmd_state(se_cmd));
4133 }
4134
4135 se_cmd->se_tfo->release_cmd(se_cmd);
4136 }
4137}
4138EXPORT_SYMBOL(target_wait_for_sess_cmds);
4139
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004140/* transport_lun_wait_for_tasks():
4141 *
4142 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4143 * an struct se_lun to be successfully shutdown.
4144 */
4145static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4146{
4147 unsigned long flags;
4148 int ret;
4149 /*
4150 * If the frontend has already requested this struct se_cmd to
4151 * be stopped, we can safely ignore this struct se_cmd.
4152 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004153 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004154 if (cmd->transport_state & CMD_T_STOP) {
4155 cmd->transport_state &= ~CMD_T_LUN_STOP;
4156
4157 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
4158 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004159 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004160 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovere3d6f902011-07-19 08:55:10 +00004161 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004162 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004163 cmd->transport_state |= CMD_T_LUN_FE_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07004164 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004165
Andy Grover5951146d2011-07-19 10:26:37 +00004166 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004167
4168 ret = transport_stop_tasks_for_cmd(cmd);
4169
Andy Grover6708bb22011-06-08 10:36:43 -07004170 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4171 " %d\n", cmd, cmd->t_task_list_num, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004172 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07004173 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004174 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004175 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07004176 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004177 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004178 }
Christoph Hellwig3df8d402011-10-17 13:56:43 -04004179 transport_remove_cmd_from_queue(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004180
4181 return 0;
4182}
4183
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004184static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4185{
4186 struct se_cmd *cmd = NULL;
4187 unsigned long lun_flags, cmd_flags;
4188 /*
4189 * Do exception processing and return CHECK_CONDITION status to the
4190 * Initiator Port.
4191 */
4192 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00004193 while (!list_empty(&lun->lun_cmd_list)) {
4194 cmd = list_first_entry(&lun->lun_cmd_list,
4195 struct se_cmd, se_lun_node);
Christoph Hellwig3d26fea2011-12-21 14:14:05 -05004196 list_del_init(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004197
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004198 /*
4199 * This will notify iscsi_target_transport.c:
4200 * transport_cmd_check_stop() that a LUN shutdown is in
4201 * progress for the iscsi_cmd_t.
4202 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004203 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07004204 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004205 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004206 cmd->se_lun->unpacked_lun,
4207 cmd->se_tfo->get_task_tag(cmd));
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004208 cmd->transport_state |= CMD_T_LUN_STOP;
Andy Grovera1d8b492011-05-02 17:12:10 -07004209 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004210
4211 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4212
Andy Grover6708bb22011-06-08 10:36:43 -07004213 if (!cmd->se_lun) {
4214 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004215 cmd->se_tfo->get_task_tag(cmd),
4216 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004217 BUG();
4218 }
4219 /*
4220 * If the Storage engine still owns the iscsi_cmd_t, determine
4221 * and/or stop its context.
4222 */
Andy Grover6708bb22011-06-08 10:36:43 -07004223 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00004224 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4225 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004226
Andy Grovere3d6f902011-07-19 08:55:10 +00004227 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004228 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4229 continue;
4230 }
4231
Andy Grover6708bb22011-06-08 10:36:43 -07004232 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004233 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004234 cmd->se_lun->unpacked_lun,
4235 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004236
Andy Grovera1d8b492011-05-02 17:12:10 -07004237 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004238 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004239 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004240 goto check_cond;
4241 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004242 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004243 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07004244 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004245
4246 transport_free_dev_tasks(cmd);
4247 /*
4248 * The Storage engine stopped this struct se_cmd before it was
4249 * send to the fabric frontend for delivery back to the
4250 * Initiator Node. Return this SCSI CDB back with an
4251 * CHECK_CONDITION status.
4252 */
4253check_cond:
4254 transport_send_check_condition_and_sense(cmd,
4255 TCM_NON_EXISTENT_LUN, 0);
4256 /*
4257 * If the fabric frontend is waiting for this iscsi_cmd_t to
4258 * be released, notify the waiting thread now that LU has
4259 * finished accessing it.
4260 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004261 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004262 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07004263 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004264 " struct se_cmd: %p ITT: 0x%08x\n",
4265 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00004266 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004267
Andy Grovera1d8b492011-05-02 17:12:10 -07004268 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004269 cmd_flags);
4270 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -07004271 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004272 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4273 continue;
4274 }
Andy Grover6708bb22011-06-08 10:36:43 -07004275 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004276 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004277
Andy Grovera1d8b492011-05-02 17:12:10 -07004278 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004279 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4280 }
4281 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4282}
4283
4284static int transport_clear_lun_thread(void *p)
4285{
Jörn Engel8359cf42011-11-24 02:05:51 +01004286 struct se_lun *lun = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004287
4288 __transport_clear_lun_from_sessions(lun);
4289 complete(&lun->lun_shutdown_comp);
4290
4291 return 0;
4292}
4293
4294int transport_clear_lun_from_sessions(struct se_lun *lun)
4295{
4296 struct task_struct *kt;
4297
Andy Grover5951146d2011-07-19 10:26:37 +00004298 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004299 "tcm_cl_%u", lun->unpacked_lun);
4300 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07004301 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00004302 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004303 }
4304 wait_for_completion(&lun->lun_shutdown_comp);
4305
4306 return 0;
4307}
4308
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004309/**
4310 * transport_wait_for_tasks - wait for completion to occur
4311 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004312 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004313 * Called from frontend fabric context to wait for storage engine
4314 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004315 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004316bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004317{
4318 unsigned long flags;
4319
Andy Grovera1d8b492011-05-02 17:12:10 -07004320 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Groverc8e31f22012-01-19 13:39:17 -08004321 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
4322 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004323 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004324 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004325 }
4326 /*
4327 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4328 * has been set in transport_set_supported_SAM_opcode().
4329 */
Andy Groverc8e31f22012-01-19 13:39:17 -08004330 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
4331 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004332 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004333 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004334 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004335 /*
4336 * If we are already stopped due to an external event (ie: LUN shutdown)
4337 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07004338 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004339 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4340 * has completed its operation on the struct se_cmd.
4341 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004342 if (cmd->transport_state & CMD_T_LUN_STOP) {
Andy Grover6708bb22011-06-08 10:36:43 -07004343 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00004344 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004345 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004346 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004347 /*
4348 * There is a special case for WRITES where a FE exception +
4349 * LUN shutdown means ConfigFS context is still sleeping on
4350 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4351 * We go ahead and up transport_lun_stop_comp just to be sure
4352 * here.
4353 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004354 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4355 complete(&cmd->transport_lun_stop_comp);
4356 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4357 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004358
4359 transport_all_task_dev_remove_state(cmd);
4360 /*
4361 * At this point, the frontend who was the originator of this
4362 * struct se_cmd, now owns the structure and can be released through
4363 * normal means below.
4364 */
Andy Grover6708bb22011-06-08 10:36:43 -07004365 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00004366 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004367 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004368 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004369
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004370 cmd->transport_state &= ~CMD_T_LUN_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004371 }
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004372
4373 if (!(cmd->transport_state & CMD_T_ACTIVE) ||
4374 (cmd->transport_state & CMD_T_ABORTED)) {
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004375 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004376 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004377 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004378
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004379 cmd->transport_state |= CMD_T_STOP;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004380
Andy Grover6708bb22011-06-08 10:36:43 -07004381 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004382 " i_state: %d, t_state: %d, CMD_T_STOP\n",
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004383 cmd, cmd->se_tfo->get_task_tag(cmd),
4384 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004385
Andy Grovera1d8b492011-05-02 17:12:10 -07004386 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004387
Andy Grover5951146d2011-07-19 10:26:37 +00004388 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004389
Andy Grovera1d8b492011-05-02 17:12:10 -07004390 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004391
Andy Grovera1d8b492011-05-02 17:12:10 -07004392 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004393 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004394
Andy Grover6708bb22011-06-08 10:36:43 -07004395 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07004396 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004397 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004398
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004399 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004400
4401 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004402}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004403EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004404
4405static int transport_get_sense_codes(
4406 struct se_cmd *cmd,
4407 u8 *asc,
4408 u8 *ascq)
4409{
4410 *asc = cmd->scsi_asc;
4411 *ascq = cmd->scsi_ascq;
4412
4413 return 0;
4414}
4415
4416static int transport_set_sense_codes(
4417 struct se_cmd *cmd,
4418 u8 asc,
4419 u8 ascq)
4420{
4421 cmd->scsi_asc = asc;
4422 cmd->scsi_ascq = ascq;
4423
4424 return 0;
4425}
4426
4427int transport_send_check_condition_and_sense(
4428 struct se_cmd *cmd,
4429 u8 reason,
4430 int from_transport)
4431{
4432 unsigned char *buffer = cmd->sense_buffer;
4433 unsigned long flags;
4434 int offset;
4435 u8 asc = 0, ascq = 0;
4436
Andy Grovera1d8b492011-05-02 17:12:10 -07004437 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004438 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004439 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004440 return 0;
4441 }
4442 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07004443 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004444
4445 if (!reason && from_transport)
4446 goto after_reason;
4447
4448 if (!from_transport)
4449 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4450 /*
4451 * Data Segment and SenseLength of the fabric response PDU.
4452 *
4453 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4454 * from include/scsi/scsi_cmnd.h
4455 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004456 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004457 TRANSPORT_SENSE_BUFFER);
4458 /*
4459 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4460 * SENSE KEY values from include/scsi/scsi.h
4461 */
4462 switch (reason) {
4463 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004464 /* CURRENT ERROR */
4465 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004466 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004467 /* ILLEGAL REQUEST */
4468 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4469 /* LOGICAL UNIT NOT SUPPORTED */
4470 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4471 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004472 case TCM_UNSUPPORTED_SCSI_OPCODE:
4473 case TCM_SECTOR_COUNT_TOO_MANY:
4474 /* CURRENT ERROR */
4475 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004476 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004477 /* ILLEGAL REQUEST */
4478 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4479 /* INVALID COMMAND OPERATION CODE */
4480 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4481 break;
4482 case TCM_UNKNOWN_MODE_PAGE:
4483 /* CURRENT ERROR */
4484 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004485 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004486 /* ILLEGAL REQUEST */
4487 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4488 /* INVALID FIELD IN CDB */
4489 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4490 break;
4491 case TCM_CHECK_CONDITION_ABORT_CMD:
4492 /* CURRENT ERROR */
4493 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004494 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004495 /* ABORTED COMMAND */
4496 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4497 /* BUS DEVICE RESET FUNCTION OCCURRED */
4498 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4499 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4500 break;
4501 case TCM_INCORRECT_AMOUNT_OF_DATA:
4502 /* CURRENT ERROR */
4503 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004504 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004505 /* ABORTED COMMAND */
4506 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4507 /* WRITE ERROR */
4508 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4509 /* NOT ENOUGH UNSOLICITED DATA */
4510 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4511 break;
4512 case TCM_INVALID_CDB_FIELD:
4513 /* CURRENT ERROR */
4514 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004515 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004516 /* ILLEGAL REQUEST */
4517 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004518 /* INVALID FIELD IN CDB */
4519 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4520 break;
4521 case TCM_INVALID_PARAMETER_LIST:
4522 /* CURRENT ERROR */
4523 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004524 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Roland Dreier9fbc8902012-01-09 17:54:00 -08004525 /* ILLEGAL REQUEST */
4526 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004527 /* INVALID FIELD IN PARAMETER LIST */
4528 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4529 break;
4530 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4531 /* CURRENT ERROR */
4532 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004533 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004534 /* ABORTED COMMAND */
4535 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4536 /* WRITE ERROR */
4537 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4538 /* UNEXPECTED_UNSOLICITED_DATA */
4539 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4540 break;
4541 case TCM_SERVICE_CRC_ERROR:
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 /* PROTOCOL SERVICE CRC ERROR */
4548 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4549 /* N/A */
4550 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4551 break;
4552 case TCM_SNACK_REJECTED:
4553 /* CURRENT ERROR */
4554 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004555 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004556 /* ABORTED COMMAND */
4557 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4558 /* READ ERROR */
4559 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4560 /* FAILED RETRANSMISSION REQUEST */
4561 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4562 break;
4563 case TCM_WRITE_PROTECTED:
4564 /* CURRENT ERROR */
4565 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004566 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004567 /* DATA PROTECT */
4568 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4569 /* WRITE PROTECTED */
4570 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4571 break;
4572 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4573 /* CURRENT ERROR */
4574 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004575 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004576 /* UNIT ATTENTION */
4577 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4578 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4579 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4580 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4581 break;
4582 case TCM_CHECK_CONDITION_NOT_READY:
4583 /* CURRENT ERROR */
4584 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004585 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004586 /* Not Ready */
4587 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4588 transport_get_sense_codes(cmd, &asc, &ascq);
4589 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4590 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4591 break;
4592 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4593 default:
4594 /* CURRENT ERROR */
4595 buffer[offset] = 0x70;
Roland Dreier895f3022011-12-13 14:55:33 -08004596 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004597 /* ILLEGAL REQUEST */
4598 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4599 /* LOGICAL UNIT COMMUNICATION FAILURE */
4600 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4601 break;
4602 }
4603 /*
4604 * This code uses linux/include/scsi/scsi.h SAM status codes!
4605 */
4606 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4607 /*
4608 * Automatically padded, this value is encoded in the fabric's
4609 * data_length response PDU containing the SCSI defined sense data.
4610 */
4611 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4612
4613after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004614 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004615}
4616EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4617
4618int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4619{
4620 int ret = 0;
4621
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004622 if (cmd->transport_state & CMD_T_ABORTED) {
Andy Grover6708bb22011-06-08 10:36:43 -07004623 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004624 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4625 return 1;
4626#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004627 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004628 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07004629 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004630 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004631#endif
4632 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00004633 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004634 ret = 1;
4635 }
4636 return ret;
4637}
4638EXPORT_SYMBOL(transport_check_aborted_status);
4639
4640void transport_send_task_abort(struct se_cmd *cmd)
4641{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07004642 unsigned long flags;
4643
4644 spin_lock_irqsave(&cmd->t_state_lock, flags);
4645 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4646 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4647 return;
4648 }
4649 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4650
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004651 /*
4652 * If there are still expected incoming fabric WRITEs, we wait
4653 * until until they have completed before sending a TASK_ABORTED
4654 * response. This response with TASK_ABORTED status will be
4655 * queued back to fabric module by transport_check_aborted_status().
4656 */
4657 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004658 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05004659 cmd->transport_state |= CMD_T_ABORTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004660 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004661 }
4662 }
4663 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4664#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004665 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07004666 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004667 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004668#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00004669 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004670}
4671
Christoph Hellwige26d99a2011-11-14 12:30:30 -05004672static int transport_generic_do_tmr(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004673{
Andy Grover5951146d2011-07-19 10:26:37 +00004674 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004675 struct se_tmr_req *tmr = cmd->se_tmr_req;
4676 int ret;
4677
4678 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004679 case TMR_ABORT_TASK:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004680 tmr->response = TMR_FUNCTION_REJECTED;
4681 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004682 case TMR_ABORT_TASK_SET:
4683 case TMR_CLEAR_ACA:
4684 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004685 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4686 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004687 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004688 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4689 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4690 TMR_FUNCTION_REJECTED;
4691 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004692 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004693 tmr->response = TMR_FUNCTION_REJECTED;
4694 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004695 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004696 tmr->response = TMR_FUNCTION_REJECTED;
4697 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004698 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004699 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004700 tmr->function);
4701 tmr->response = TMR_FUNCTION_REJECTED;
4702 break;
4703 }
4704
4705 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00004706 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004707
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04004708 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004709 return 0;
4710}
4711
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004712/* transport_processing_thread():
4713 *
4714 *
4715 */
4716static int transport_processing_thread(void *param)
4717{
Andy Grover5951146d2011-07-19 10:26:37 +00004718 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004719 struct se_cmd *cmd;
Jörn Engel8359cf42011-11-24 02:05:51 +01004720 struct se_device *dev = param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004721
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004722 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004723 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4724 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004725 kthread_should_stop());
4726 if (ret < 0)
4727 goto out;
4728
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004729get_cmd:
Andy Grover5951146d2011-07-19 10:26:37 +00004730 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4731 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004732 continue;
4733
Andy Grover5951146d2011-07-19 10:26:37 +00004734 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02004735 case TRANSPORT_NEW_CMD:
4736 BUG();
4737 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004738 case TRANSPORT_NEW_CMD_MAP:
Andy Grover6708bb22011-06-08 10:36:43 -07004739 if (!cmd->se_tfo->new_cmd_map) {
4740 pr_err("cmd->se_tfo->new_cmd_map is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004741 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4742 BUG();
4743 }
Andy Grovere3d6f902011-07-19 08:55:10 +00004744 ret = cmd->se_tfo->new_cmd_map(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004745 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004746 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004747 break;
4748 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004749 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004750 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004751 transport_generic_request_failure(cmd);
4752 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004753 }
4754 break;
4755 case TRANSPORT_PROCESS_WRITE:
4756 transport_generic_process_write(cmd);
4757 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004758 case TRANSPORT_PROCESS_TMR:
4759 transport_generic_do_tmr(cmd);
4760 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004761 case TRANSPORT_COMPLETE_QF_WP:
Christoph Hellwige057f532011-10-17 13:56:41 -04004762 transport_write_pending_qf(cmd);
4763 break;
4764 case TRANSPORT_COMPLETE_QF_OK:
4765 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004766 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004767 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004768 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4769 "i_state: %d on SE LUN: %u\n",
4770 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00004771 cmd->se_tfo->get_task_tag(cmd),
4772 cmd->se_tfo->get_cmd_state(cmd),
4773 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004774 BUG();
4775 }
4776
4777 goto get_cmd;
4778 }
4779
4780out:
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07004781 WARN_ON(!list_empty(&dev->state_task_list));
4782 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004783 dev->process_thread = NULL;
4784 return 0;
4785}