blob: 0bdb6badbf99689adb4ac8f0f4f849f6c0d1606d [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>
48#include <target/target_core_device.h>
49#include <target/target_core_tmr.h>
50#include <target/target_core_tpg.h>
51#include <target/target_core_transport.h>
52#include <target/target_core_fabric_ops.h>
53#include <target/target_core_configfs.h>
54
55#include "target_core_alua.h"
Christoph Hellwig5bda90c2011-11-03 17:50:45 -040056#include "target_core_cdb.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080057#include "target_core_hba.h"
58#include "target_core_pr.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080059#include "target_core_ua.h"
60
Andy Grovere3d6f902011-07-19 08:55:10 +000061static int sub_api_initialized;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062
Christoph Hellwig35e0e752011-10-17 13:56:53 -040063static struct workqueue_struct *target_completion_wq;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080064static struct kmem_cache *se_sess_cache;
65struct kmem_cache *se_tmr_req_cache;
66struct kmem_cache *se_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080067struct kmem_cache *t10_pr_reg_cache;
68struct kmem_cache *t10_alua_lu_gp_cache;
69struct kmem_cache *t10_alua_lu_gp_mem_cache;
70struct kmem_cache *t10_alua_tg_pt_gp_cache;
71struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
72
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080073static int transport_generic_write_pending(struct se_cmd *);
Andy Grover5951146d2011-07-19 10:26:37 +000074static int transport_processing_thread(void *param);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080075static int __transport_execute_tasks(struct se_device *dev);
76static void transport_complete_task_attr(struct se_cmd *cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -070077static void transport_handle_queue_full(struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -040078 struct se_device *dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080079static void transport_free_dev_tasks(struct se_cmd *cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +000080static int transport_generic_get_mem(struct se_cmd *cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -070081static void transport_put_cmd(struct se_cmd *cmd);
Christoph Hellwig3df8d402011-10-17 13:56:43 -040082static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -070084static void transport_generic_request_failure(struct se_cmd *);
Christoph Hellwig35e0e752011-10-17 13:56:53 -040085static void target_complete_ok_work(struct work_struct *work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080086
Andy Grovere3d6f902011-07-19 08:55:10 +000087int init_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080088{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080089 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
90 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
91 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -070092 if (!se_tmr_req_cache) {
93 pr_err("kmem_cache_create() for struct se_tmr_req"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080094 " failed\n");
Roland Dreier97c34f32011-11-10 11:22:47 -080095 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080096 }
97 se_sess_cache = kmem_cache_create("se_sess_cache",
98 sizeof(struct se_session), __alignof__(struct se_session),
99 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700100 if (!se_sess_cache) {
101 pr_err("kmem_cache_create() for struct se_session"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800102 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400103 goto out_free_tmr_req_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800104 }
105 se_ua_cache = kmem_cache_create("se_ua_cache",
106 sizeof(struct se_ua), __alignof__(struct se_ua),
107 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700108 if (!se_ua_cache) {
109 pr_err("kmem_cache_create() for struct se_ua failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400110 goto out_free_sess_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800111 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800112 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
113 sizeof(struct t10_pr_registration),
114 __alignof__(struct t10_pr_registration), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700115 if (!t10_pr_reg_cache) {
116 pr_err("kmem_cache_create() for struct t10_pr_registration"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800117 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400118 goto out_free_ua_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800119 }
120 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
121 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
122 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700123 if (!t10_alua_lu_gp_cache) {
124 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800125 " failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400126 goto out_free_pr_reg_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800127 }
128 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
129 sizeof(struct t10_alua_lu_gp_member),
130 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700131 if (!t10_alua_lu_gp_mem_cache) {
132 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800133 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400134 goto out_free_lu_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800135 }
136 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
137 sizeof(struct t10_alua_tg_pt_gp),
138 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700139 if (!t10_alua_tg_pt_gp_cache) {
140 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800141 "cache failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400142 goto out_free_lu_gp_mem_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800143 }
144 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
145 "t10_alua_tg_pt_gp_mem_cache",
146 sizeof(struct t10_alua_tg_pt_gp_member),
147 __alignof__(struct t10_alua_tg_pt_gp_member),
148 0, NULL);
Andy Grover6708bb22011-06-08 10:36:43 -0700149 if (!t10_alua_tg_pt_gp_mem_cache) {
150 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800151 "mem_t failed\n");
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400152 goto out_free_tg_pt_gp_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800153 }
154
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400155 target_completion_wq = alloc_workqueue("target_completion",
156 WQ_MEM_RECLAIM, 0);
157 if (!target_completion_wq)
158 goto out_free_tg_pt_gp_mem_cache;
159
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800160 return 0;
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400161
162out_free_tg_pt_gp_mem_cache:
163 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
164out_free_tg_pt_gp_cache:
165 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
166out_free_lu_gp_mem_cache:
167 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
168out_free_lu_gp_cache:
169 kmem_cache_destroy(t10_alua_lu_gp_cache);
170out_free_pr_reg_cache:
171 kmem_cache_destroy(t10_pr_reg_cache);
172out_free_ua_cache:
173 kmem_cache_destroy(se_ua_cache);
174out_free_sess_cache:
175 kmem_cache_destroy(se_sess_cache);
176out_free_tmr_req_cache:
177 kmem_cache_destroy(se_tmr_req_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800178out:
Andy Grovere3d6f902011-07-19 08:55:10 +0000179 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800180}
181
Andy Grovere3d6f902011-07-19 08:55:10 +0000182void release_se_kmem_caches(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800183{
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400184 destroy_workqueue(target_completion_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800185 kmem_cache_destroy(se_tmr_req_cache);
186 kmem_cache_destroy(se_sess_cache);
187 kmem_cache_destroy(se_ua_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800188 kmem_cache_destroy(t10_pr_reg_cache);
189 kmem_cache_destroy(t10_alua_lu_gp_cache);
190 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
191 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
192 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800193}
194
Andy Grovere3d6f902011-07-19 08:55:10 +0000195/* This code ensures unique mib indexes are handed out. */
196static DEFINE_SPINLOCK(scsi_mib_index_lock);
197static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800198
199/*
200 * Allocate a new row index for the entry type specified
201 */
202u32 scsi_get_new_index(scsi_index_t type)
203{
204 u32 new_index;
205
Andy Grovere3d6f902011-07-19 08:55:10 +0000206 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800207
Andy Grovere3d6f902011-07-19 08:55:10 +0000208 spin_lock(&scsi_mib_index_lock);
209 new_index = ++scsi_mib_index[type];
210 spin_unlock(&scsi_mib_index_lock);
Nicholas Bellingere89d15e2011-02-09 15:35:03 -0800211
212 return new_index;
213}
214
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800215void transport_init_queue_obj(struct se_queue_obj *qobj)
216{
217 atomic_set(&qobj->queue_cnt, 0);
218 INIT_LIST_HEAD(&qobj->qobj_list);
219 init_waitqueue_head(&qobj->thread_wq);
220 spin_lock_init(&qobj->cmd_queue_lock);
221}
222EXPORT_SYMBOL(transport_init_queue_obj);
223
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700224void transport_subsystem_check_init(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800225{
226 int ret;
227
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700228 if (sub_api_initialized)
229 return;
230
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800231 ret = request_module("target_core_iblock");
232 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700233 pr_err("Unable to load target_core_iblock\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800234
235 ret = request_module("target_core_file");
236 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700237 pr_err("Unable to load target_core_file\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800238
239 ret = request_module("target_core_pscsi");
240 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700241 pr_err("Unable to load target_core_pscsi\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800242
243 ret = request_module("target_core_stgt");
244 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700245 pr_err("Unable to load target_core_stgt\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800246
Andy Grovere3d6f902011-07-19 08:55:10 +0000247 sub_api_initialized = 1;
Nicholas Bellingerdbc56232011-10-22 01:03:54 -0700248 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800249}
250
251struct se_session *transport_init_session(void)
252{
253 struct se_session *se_sess;
254
255 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700256 if (!se_sess) {
257 pr_err("Unable to allocate struct se_session from"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800258 " se_sess_cache\n");
259 return ERR_PTR(-ENOMEM);
260 }
261 INIT_LIST_HEAD(&se_sess->sess_list);
262 INIT_LIST_HEAD(&se_sess->sess_acl_list);
Nicholas Bellingera17f0912011-11-02 21:52:08 -0700263 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
264 INIT_LIST_HEAD(&se_sess->sess_wait_list);
265 spin_lock_init(&se_sess->sess_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800266
267 return se_sess;
268}
269EXPORT_SYMBOL(transport_init_session);
270
271/*
272 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
273 */
274void __transport_register_session(
275 struct se_portal_group *se_tpg,
276 struct se_node_acl *se_nacl,
277 struct se_session *se_sess,
278 void *fabric_sess_ptr)
279{
280 unsigned char buf[PR_REG_ISID_LEN];
281
282 se_sess->se_tpg = se_tpg;
283 se_sess->fabric_sess_ptr = fabric_sess_ptr;
284 /*
285 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
286 *
287 * Only set for struct se_session's that will actually be moving I/O.
288 * eg: *NOT* discovery sessions.
289 */
290 if (se_nacl) {
291 /*
292 * If the fabric module supports an ISID based TransportID,
293 * save this value in binary from the fabric I_T Nexus now.
294 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000295 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800296 memset(&buf[0], 0, PR_REG_ISID_LEN);
Andy Grovere3d6f902011-07-19 08:55:10 +0000297 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800298 &buf[0], PR_REG_ISID_LEN);
299 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
300 }
301 spin_lock_irq(&se_nacl->nacl_sess_lock);
302 /*
303 * The se_nacl->nacl_sess pointer will be set to the
304 * last active I_T Nexus for each struct se_node_acl.
305 */
306 se_nacl->nacl_sess = se_sess;
307
308 list_add_tail(&se_sess->sess_acl_list,
309 &se_nacl->acl_sess_list);
310 spin_unlock_irq(&se_nacl->nacl_sess_lock);
311 }
312 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
313
Andy Grover6708bb22011-06-08 10:36:43 -0700314 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000315 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800316}
317EXPORT_SYMBOL(__transport_register_session);
318
319void transport_register_session(
320 struct se_portal_group *se_tpg,
321 struct se_node_acl *se_nacl,
322 struct se_session *se_sess,
323 void *fabric_sess_ptr)
324{
325 spin_lock_bh(&se_tpg->session_lock);
326 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
327 spin_unlock_bh(&se_tpg->session_lock);
328}
329EXPORT_SYMBOL(transport_register_session);
330
331void transport_deregister_session_configfs(struct se_session *se_sess)
332{
333 struct se_node_acl *se_nacl;
Roland Dreier23388862011-06-22 01:02:21 -0700334 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800335 /*
336 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
337 */
338 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700339 if (se_nacl) {
Roland Dreier23388862011-06-22 01:02:21 -0700340 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800341 list_del(&se_sess->sess_acl_list);
342 /*
343 * If the session list is empty, then clear the pointer.
344 * Otherwise, set the struct se_session pointer from the tail
345 * element of the per struct se_node_acl active session list.
346 */
347 if (list_empty(&se_nacl->acl_sess_list))
348 se_nacl->nacl_sess = NULL;
349 else {
350 se_nacl->nacl_sess = container_of(
351 se_nacl->acl_sess_list.prev,
352 struct se_session, sess_acl_list);
353 }
Roland Dreier23388862011-06-22 01:02:21 -0700354 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800355 }
356}
357EXPORT_SYMBOL(transport_deregister_session_configfs);
358
359void transport_free_session(struct se_session *se_sess)
360{
361 kmem_cache_free(se_sess_cache, se_sess);
362}
363EXPORT_SYMBOL(transport_free_session);
364
365void transport_deregister_session(struct se_session *se_sess)
366{
367 struct se_portal_group *se_tpg = se_sess->se_tpg;
368 struct se_node_acl *se_nacl;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700369 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800370
Andy Grover6708bb22011-06-08 10:36:43 -0700371 if (!se_tpg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800372 transport_free_session(se_sess);
373 return;
374 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800375
Roland Dreiere63a8e12011-08-12 16:01:02 -0700376 spin_lock_irqsave(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800377 list_del(&se_sess->sess_list);
378 se_sess->se_tpg = NULL;
379 se_sess->fabric_sess_ptr = NULL;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700380 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800381
382 /*
383 * Determine if we need to do extra work for this initiator node's
384 * struct se_node_acl if it had been previously dynamically generated.
385 */
386 se_nacl = se_sess->se_node_acl;
Andy Grover6708bb22011-06-08 10:36:43 -0700387 if (se_nacl) {
Roland Dreiere63a8e12011-08-12 16:01:02 -0700388 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800389 if (se_nacl->dynamic_node_acl) {
Andy Grover6708bb22011-06-08 10:36:43 -0700390 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
391 se_tpg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800392 list_del(&se_nacl->acl_list);
393 se_tpg->num_node_acls--;
Roland Dreiere63a8e12011-08-12 16:01:02 -0700394 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395
396 core_tpg_wait_for_nacl_pr_ref(se_nacl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800397 core_free_device_list_for_node(se_nacl, se_tpg);
Andy Grovere3d6f902011-07-19 08:55:10 +0000398 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800399 se_nacl);
Roland Dreiere63a8e12011-08-12 16:01:02 -0700400 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800401 }
402 }
Roland Dreiere63a8e12011-08-12 16:01:02 -0700403 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800404 }
405
406 transport_free_session(se_sess);
407
Andy Grover6708bb22011-06-08 10:36:43 -0700408 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000409 se_tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800410}
411EXPORT_SYMBOL(transport_deregister_session);
412
413/*
Andy Grovera1d8b492011-05-02 17:12:10 -0700414 * Called with cmd->t_state_lock held.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800415 */
416static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
417{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400418 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800419 struct se_task *task;
420 unsigned long flags;
421
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400422 if (!dev)
423 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800424
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400425 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400426 if (task->task_flags & TF_ACTIVE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800427 continue;
428
Andy Grover6708bb22011-06-08 10:36:43 -0700429 if (!atomic_read(&task->task_state_active))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 continue;
431
432 spin_lock_irqsave(&dev->execute_task_lock, flags);
433 list_del(&task->t_state_list);
Andy Grover6708bb22011-06-08 10:36:43 -0700434 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
435 cmd->se_tfo->get_task_tag(cmd), dev, task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800436 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
437
438 atomic_set(&task->task_state_active, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -0700439 atomic_dec(&cmd->t_task_cdbs_ex_left);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800440 }
441}
442
443/* transport_cmd_check_stop():
444 *
445 * 'transport_off = 1' determines if t_transport_active should be cleared.
446 * 'transport_off = 2' determines if task_dev_state should be removed.
447 *
448 * A non-zero u8 t_state sets cmd->t_state.
449 * Returns 1 when command is stopped, else 0.
450 */
451static int transport_cmd_check_stop(
452 struct se_cmd *cmd,
453 int transport_off,
454 u8 t_state)
455{
456 unsigned long flags;
457
Andy Grovera1d8b492011-05-02 17:12:10 -0700458 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800459 /*
460 * Determine if IOCTL context caller in requesting the stopping of this
461 * command for LUN shutdown purposes.
462 */
Andy Grovera1d8b492011-05-02 17:12:10 -0700463 if (atomic_read(&cmd->transport_lun_stop)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700464 pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800465 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000466 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800467
Andy Grovera1d8b492011-05-02 17:12:10 -0700468 atomic_set(&cmd->t_transport_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800469 if (transport_off == 2)
470 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700471 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800472
Andy Grovera1d8b492011-05-02 17:12:10 -0700473 complete(&cmd->transport_lun_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800474 return 1;
475 }
476 /*
477 * Determine if frontend context caller is requesting the stopping of
Andy Grovere3d6f902011-07-19 08:55:10 +0000478 * this command for frontend exceptions.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800479 */
Andy Grovera1d8b492011-05-02 17:12:10 -0700480 if (atomic_read(&cmd->t_transport_stop)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700481 pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) =="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800482 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
Andy Grovere3d6f902011-07-19 08:55:10 +0000483 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800484
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800485 if (transport_off == 2)
486 transport_all_task_dev_remove_state(cmd);
487
488 /*
489 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
490 * to FE.
491 */
492 if (transport_off == 2)
493 cmd->se_lun = NULL;
Andy Grovera1d8b492011-05-02 17:12:10 -0700494 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800495
Andy Grovera1d8b492011-05-02 17:12:10 -0700496 complete(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800497 return 1;
498 }
499 if (transport_off) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700500 atomic_set(&cmd->t_transport_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800501 if (transport_off == 2) {
502 transport_all_task_dev_remove_state(cmd);
503 /*
504 * Clear struct se_cmd->se_lun before the transport_off == 2
505 * handoff to fabric module.
506 */
507 cmd->se_lun = NULL;
508 /*
509 * Some fabric modules like tcm_loop can release
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300510 * their internally allocated I/O reference now and
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800511 * struct se_cmd now.
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700512 *
513 * Fabric modules are expected to return '1' here if the
514 * se_cmd being passed is released at this point,
515 * or zero if not being released.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800516 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000517 if (cmd->se_tfo->check_stop_free != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800518 spin_unlock_irqrestore(
Andy Grovera1d8b492011-05-02 17:12:10 -0700519 &cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800520
Nicholas Bellinger88dd9e22011-11-02 03:33:16 -0700521 return cmd->se_tfo->check_stop_free(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800522 }
523 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700524 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800525
526 return 0;
527 } else if (t_state)
528 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700529 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800530
531 return 0;
532}
533
534static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
535{
536 return transport_cmd_check_stop(cmd, 2, 0);
537}
538
539static void transport_lun_remove_cmd(struct se_cmd *cmd)
540{
Andy Grovere3d6f902011-07-19 08:55:10 +0000541 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800542 unsigned long flags;
543
544 if (!lun)
545 return;
546
Andy Grovera1d8b492011-05-02 17:12:10 -0700547 spin_lock_irqsave(&cmd->t_state_lock, flags);
Andy Grover6708bb22011-06-08 10:36:43 -0700548 if (!atomic_read(&cmd->transport_dev_active)) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700549 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800550 goto check_lun;
551 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700552 atomic_set(&cmd->transport_dev_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -0700554 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800555
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800556
557check_lun:
558 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
Andy Grovera1d8b492011-05-02 17:12:10 -0700559 if (atomic_read(&cmd->transport_lun_active)) {
Andy Grover5951146d2011-07-19 10:26:37 +0000560 list_del(&cmd->se_lun_node);
Andy Grovera1d8b492011-05-02 17:12:10 -0700561 atomic_set(&cmd->transport_lun_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800562#if 0
Andy Grover6708bb22011-06-08 10:36:43 -0700563 pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n"
Andy Grovere3d6f902011-07-19 08:55:10 +0000564 cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800565#endif
566 }
567 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
568}
569
570void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
571{
Nicholas Bellinger8dc52b52011-10-09 02:02:51 -0700572 if (!cmd->se_tmr_req)
573 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800574
575 if (transport_cmd_check_stop_to_fabric(cmd))
576 return;
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700577 if (remove) {
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400578 transport_remove_cmd_from_queue(cmd);
Christoph Hellwige6a25732011-09-13 23:08:50 +0200579 transport_put_cmd(cmd);
Nicholas Bellinger77039d12011-09-29 01:01:35 -0700580 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800581}
582
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400583static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
584 bool at_head)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800585{
586 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000587 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588 unsigned long flags;
589
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800590 if (t_state) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700591 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800592 cmd->t_state = t_state;
Andy Grovera1d8b492011-05-02 17:12:10 -0700593 atomic_set(&cmd->t_transport_active, 1);
594 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800595 }
596
597 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700598
599 /* If the cmd is already on the list, remove it before we add it */
600 if (!list_empty(&cmd->se_queue_node))
601 list_del(&cmd->se_queue_node);
602 else
603 atomic_inc(&qobj->queue_cnt);
604
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400605 if (at_head)
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700606 list_add(&cmd->se_queue_node, &qobj->qobj_list);
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400607 else
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700608 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
Roland Dreier79a7fef2011-09-28 22:12:07 -0700609 atomic_set(&cmd->t_transport_queue_active, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800610 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
611
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800612 wake_up_interruptible(&qobj->thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800613}
614
Andy Grover5951146d2011-07-19 10:26:37 +0000615static struct se_cmd *
616transport_get_cmd_from_queue(struct se_queue_obj *qobj)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800617{
Andy Grover5951146d2011-07-19 10:26:37 +0000618 struct se_cmd *cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800619 unsigned long flags;
620
621 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
622 if (list_empty(&qobj->qobj_list)) {
623 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
624 return NULL;
625 }
Andy Grover5951146d2011-07-19 10:26:37 +0000626 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800627
Roland Dreier79a7fef2011-09-28 22:12:07 -0700628 atomic_set(&cmd->t_transport_queue_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800629
Roland Dreier79a7fef2011-09-28 22:12:07 -0700630 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631 atomic_dec(&qobj->queue_cnt);
632 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
633
Andy Grover5951146d2011-07-19 10:26:37 +0000634 return cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800635}
636
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400637static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800638{
Christoph Hellwig3df8d402011-10-17 13:56:43 -0400639 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800640 unsigned long flags;
641
642 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
Andy Grover6708bb22011-06-08 10:36:43 -0700643 if (!atomic_read(&cmd->t_transport_queue_active)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800644 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
645 return;
646 }
Roland Dreier79a7fef2011-09-28 22:12:07 -0700647 atomic_set(&cmd->t_transport_queue_active, 0);
648 atomic_dec(&qobj->queue_cnt);
649 list_del_init(&cmd->se_queue_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800650 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
651
Andy Grovera1d8b492011-05-02 17:12:10 -0700652 if (atomic_read(&cmd->t_transport_queue_active)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700653 pr_err("ITT: 0x%08x t_transport_queue_active: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000654 cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -0700655 atomic_read(&cmd->t_transport_queue_active));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800656 }
657}
658
659/*
660 * Completion function used by TCM subsystem plugins (such as FILEIO)
661 * for queueing up response from struct se_subsystem_api->do_task()
662 */
663void transport_complete_sync_cache(struct se_cmd *cmd, int good)
664{
Andy Grovera1d8b492011-05-02 17:12:10 -0700665 struct se_task *task = list_entry(cmd->t_task_list.next,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800666 struct se_task, t_list);
667
668 if (good) {
669 cmd->scsi_status = SAM_STAT_GOOD;
670 task->task_scsi_status = GOOD;
671 } else {
672 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700673 task->task_se_cmd->scsi_sense_reason =
674 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
675
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800676 }
677
678 transport_complete_task(task, good);
679}
680EXPORT_SYMBOL(transport_complete_sync_cache);
681
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400682static void target_complete_failure_work(struct work_struct *work)
683{
684 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
685
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700686 transport_generic_request_failure(cmd);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400687}
688
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800689/* transport_complete_task():
690 *
691 * Called from interrupt and non interrupt context depending
692 * on the transport plugin.
693 */
694void transport_complete_task(struct se_task *task, int success)
695{
Andy Grovere3d6f902011-07-19 08:55:10 +0000696 struct se_cmd *cmd = task->task_se_cmd;
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400697 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800698 unsigned long flags;
699#if 0
Andy Grover6708bb22011-06-08 10:36:43 -0700700 pr_debug("task: %p CDB: 0x%02x obj_ptr: %p\n", task,
Andy Grovera1d8b492011-05-02 17:12:10 -0700701 cmd->t_task_cdb[0], dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800702#endif
Andy Grovere3d6f902011-07-19 08:55:10 +0000703 if (dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800704 atomic_inc(&dev->depth_left);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800705
Andy Grovera1d8b492011-05-02 17:12:10 -0700706 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400707 task->task_flags &= ~TF_ACTIVE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800708
709 /*
710 * See if any sense data exists, if so set the TASK_SENSE flag.
711 * Also check for any other post completion work that needs to be
712 * done by the plugins.
713 */
714 if (dev && dev->transport->transport_complete) {
715 if (dev->transport->transport_complete(task) != 0) {
716 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
717 task->task_sense = 1;
718 success = 1;
719 }
720 }
721
722 /*
723 * See if we are waiting for outstanding struct se_task
724 * to complete for an exception condition
725 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -0400726 if (task->task_flags & TF_REQUEST_STOP) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700727 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800728 complete(&task->task_stop_comp);
729 return;
730 }
Christoph Hellwig2235007c2011-11-02 05:06:35 -0700731
732 if (!success)
733 cmd->t_tasks_failed = 1;
734
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800735 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800736 * Decrement the outstanding t_task_cdbs_left count. The last
737 * struct se_task from struct se_cmd will complete itself into the
738 * device queue depending upon int success.
739 */
Andy Grover6708bb22011-06-08 10:36:43 -0700740 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
Andy Grovera1d8b492011-05-02 17:12:10 -0700741 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800742 return;
743 }
744
Christoph Hellwig2235007c2011-11-02 05:06:35 -0700745 if (cmd->t_tasks_failed) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800746 if (!task->task_error_status) {
747 task->task_error_status =
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700748 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
749 cmd->scsi_sense_reason =
750 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800751 }
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700752
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400753 INIT_WORK(&cmd->work, target_complete_failure_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800754 } else {
Andy Grovera1d8b492011-05-02 17:12:10 -0700755 atomic_set(&cmd->t_transport_complete, 1);
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400756 INIT_WORK(&cmd->work, target_complete_ok_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800757 }
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400758
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400759 cmd->t_state = TRANSPORT_COMPLETE;
760 atomic_set(&cmd->t_transport_active, 1);
Andy Grovera1d8b492011-05-02 17:12:10 -0700761 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800762
Christoph Hellwig35e0e752011-10-17 13:56:53 -0400763 queue_work(target_completion_wq, &cmd->work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800764}
765EXPORT_SYMBOL(transport_complete_task);
766
767/*
768 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
769 * struct se_task list are ready to be added to the active execution list
770 * struct se_device
771
772 * Called with se_dev_t->execute_task_lock called.
773 */
774static inline int transport_add_task_check_sam_attr(
775 struct se_task *task,
776 struct se_task *task_prev,
777 struct se_device *dev)
778{
779 /*
780 * No SAM Task attribute emulation enabled, add to tail of
781 * execution queue
782 */
783 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
784 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
785 return 0;
786 }
787 /*
788 * HEAD_OF_QUEUE attribute for received CDB, which means
789 * the first task that is associated with a struct se_cmd goes to
790 * head of the struct se_device->execute_task_list, and task_prev
791 * after that for each subsequent task
792 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -0700793 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800794 list_add(&task->t_execute_list,
795 (task_prev != NULL) ?
796 &task_prev->t_execute_list :
797 &dev->execute_task_list);
798
Andy Grover6708bb22011-06-08 10:36:43 -0700799 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800800 " in execution queue\n",
Andy Grover6708bb22011-06-08 10:36:43 -0700801 task->task_se_cmd->t_task_cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800802 return 1;
803 }
804 /*
805 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
806 * transitioned from Dermant -> Active state, and are added to the end
807 * of the struct se_device->execute_task_list
808 */
809 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
810 return 0;
811}
812
813/* __transport_add_task_to_execute_queue():
814 *
815 * Called with se_dev_t->execute_task_lock called.
816 */
817static void __transport_add_task_to_execute_queue(
818 struct se_task *task,
819 struct se_task *task_prev,
820 struct se_device *dev)
821{
822 int head_of_queue;
823
824 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
825 atomic_inc(&dev->execute_tasks);
826
827 if (atomic_read(&task->task_state_active))
828 return;
829 /*
830 * Determine if this task needs to go to HEAD_OF_QUEUE for the
831 * state list as well. Running with SAM Task Attribute emulation
832 * will always return head_of_queue == 0 here
833 */
834 if (head_of_queue)
835 list_add(&task->t_state_list, (task_prev) ?
836 &task_prev->t_state_list :
837 &dev->state_task_list);
838 else
839 list_add_tail(&task->t_state_list, &dev->state_task_list);
840
841 atomic_set(&task->task_state_active, 1);
842
Andy Grover6708bb22011-06-08 10:36:43 -0700843 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000844 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800845 task, dev);
846}
847
848static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
849{
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400850 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800851 struct se_task *task;
852 unsigned long flags;
853
Andy Grovera1d8b492011-05-02 17:12:10 -0700854 spin_lock_irqsave(&cmd->t_state_lock, flags);
855 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800856 if (atomic_read(&task->task_state_active))
857 continue;
858
859 spin_lock(&dev->execute_task_lock);
860 list_add_tail(&task->t_state_list, &dev->state_task_list);
861 atomic_set(&task->task_state_active, 1);
862
Andy Grover6708bb22011-06-08 10:36:43 -0700863 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
864 task->task_se_cmd->se_tfo->get_task_tag(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800865 task->task_se_cmd), task, dev);
866
867 spin_unlock(&dev->execute_task_lock);
868 }
Andy Grovera1d8b492011-05-02 17:12:10 -0700869 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800870}
871
872static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
873{
Andy Grover5951146d2011-07-19 10:26:37 +0000874 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800875 struct se_task *task, *task_prev = NULL;
876 unsigned long flags;
877
878 spin_lock_irqsave(&dev->execute_task_lock, flags);
Andy Grovera1d8b492011-05-02 17:12:10 -0700879 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -0400880 if (!list_empty(&task->t_execute_list))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800881 continue;
882 /*
883 * __transport_add_task_to_execute_queue() handles the
884 * SAM Task Attribute emulation if enabled
885 */
886 __transport_add_task_to_execute_queue(task, task_prev, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800887 task_prev = task;
888 }
889 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800890}
891
Christoph Hellwig04629b72011-10-12 11:07:04 -0400892void __transport_remove_task_from_execute_queue(struct se_task *task,
893 struct se_device *dev)
894{
895 list_del_init(&task->t_execute_list);
896 atomic_dec(&dev->execute_tasks);
897}
898
Nicholas Bellinger52208ae2011-02-24 16:58:20 -0800899void transport_remove_task_from_execute_queue(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800900 struct se_task *task,
901 struct se_device *dev)
902{
903 unsigned long flags;
904
Christoph Hellwig04629b72011-10-12 11:07:04 -0400905 if (WARN_ON(list_empty(&task->t_execute_list)))
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700906 return;
Nicholas Bellingeraf57c3a2011-05-19 20:19:12 -0700907
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800908 spin_lock_irqsave(&dev->execute_task_lock, flags);
Christoph Hellwig04629b72011-10-12 11:07:04 -0400909 __transport_remove_task_from_execute_queue(task, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800910 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
911}
912
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700913/*
Nicholas Bellingerf147abb2011-10-25 23:57:41 -0700914 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700915 */
916
917static void target_qf_do_work(struct work_struct *work)
918{
919 struct se_device *dev = container_of(work, struct se_device,
920 qf_work_queue);
Roland Dreierbcac3642011-08-27 21:33:16 -0700921 LIST_HEAD(qf_cmd_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700922 struct se_cmd *cmd, *cmd_tmp;
923
924 spin_lock_irq(&dev->qf_cmd_lock);
Roland Dreierbcac3642011-08-27 21:33:16 -0700925 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
926 spin_unlock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700927
Roland Dreierbcac3642011-08-27 21:33:16 -0700928 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700929 list_del(&cmd->se_qf_node);
930 atomic_dec(&dev->dev_qf_count);
931 smp_mb__after_atomic_dec();
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700932
Andy Grover6708bb22011-06-08 10:36:43 -0700933 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700934 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -0400935 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700936 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
937 : "UNKNOWN");
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -0400938
939 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700940 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -0700941}
942
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800943unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
944{
945 switch (cmd->data_direction) {
946 case DMA_NONE:
947 return "NONE";
948 case DMA_FROM_DEVICE:
949 return "READ";
950 case DMA_TO_DEVICE:
951 return "WRITE";
952 case DMA_BIDIRECTIONAL:
953 return "BIDI";
954 default:
955 break;
956 }
957
958 return "UNKNOWN";
959}
960
961void transport_dump_dev_state(
962 struct se_device *dev,
963 char *b,
964 int *bl)
965{
966 *bl += sprintf(b + *bl, "Status: ");
967 switch (dev->dev_status) {
968 case TRANSPORT_DEVICE_ACTIVATED:
969 *bl += sprintf(b + *bl, "ACTIVATED");
970 break;
971 case TRANSPORT_DEVICE_DEACTIVATED:
972 *bl += sprintf(b + *bl, "DEACTIVATED");
973 break;
974 case TRANSPORT_DEVICE_SHUTDOWN:
975 *bl += sprintf(b + *bl, "SHUTDOWN");
976 break;
977 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
978 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
979 *bl += sprintf(b + *bl, "OFFLINE");
980 break;
981 default:
982 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
983 break;
984 }
985
986 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
987 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
988 dev->queue_depth);
989 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000990 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 -0800991 *bl += sprintf(b + *bl, " ");
992}
993
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800994void transport_dump_vpd_proto_id(
995 struct t10_vpd *vpd,
996 unsigned char *p_buf,
997 int p_buf_len)
998{
999 unsigned char buf[VPD_TMP_BUF_SIZE];
1000 int len;
1001
1002 memset(buf, 0, VPD_TMP_BUF_SIZE);
1003 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1004
1005 switch (vpd->protocol_identifier) {
1006 case 0x00:
1007 sprintf(buf+len, "Fibre Channel\n");
1008 break;
1009 case 0x10:
1010 sprintf(buf+len, "Parallel SCSI\n");
1011 break;
1012 case 0x20:
1013 sprintf(buf+len, "SSA\n");
1014 break;
1015 case 0x30:
1016 sprintf(buf+len, "IEEE 1394\n");
1017 break;
1018 case 0x40:
1019 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1020 " Protocol\n");
1021 break;
1022 case 0x50:
1023 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1024 break;
1025 case 0x60:
1026 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1027 break;
1028 case 0x70:
1029 sprintf(buf+len, "Automation/Drive Interface Transport"
1030 " Protocol\n");
1031 break;
1032 case 0x80:
1033 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1034 break;
1035 default:
1036 sprintf(buf+len, "Unknown 0x%02x\n",
1037 vpd->protocol_identifier);
1038 break;
1039 }
1040
1041 if (p_buf)
1042 strncpy(p_buf, buf, p_buf_len);
1043 else
Andy Grover6708bb22011-06-08 10:36:43 -07001044 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001045}
1046
1047void
1048transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1049{
1050 /*
1051 * Check if the Protocol Identifier Valid (PIV) bit is set..
1052 *
1053 * from spc3r23.pdf section 7.5.1
1054 */
1055 if (page_83[1] & 0x80) {
1056 vpd->protocol_identifier = (page_83[0] & 0xf0);
1057 vpd->protocol_identifier_set = 1;
1058 transport_dump_vpd_proto_id(vpd, NULL, 0);
1059 }
1060}
1061EXPORT_SYMBOL(transport_set_vpd_proto_id);
1062
1063int transport_dump_vpd_assoc(
1064 struct t10_vpd *vpd,
1065 unsigned char *p_buf,
1066 int p_buf_len)
1067{
1068 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001069 int ret = 0;
1070 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001071
1072 memset(buf, 0, VPD_TMP_BUF_SIZE);
1073 len = sprintf(buf, "T10 VPD Identifier Association: ");
1074
1075 switch (vpd->association) {
1076 case 0x00:
1077 sprintf(buf+len, "addressed logical unit\n");
1078 break;
1079 case 0x10:
1080 sprintf(buf+len, "target port\n");
1081 break;
1082 case 0x20:
1083 sprintf(buf+len, "SCSI target device\n");
1084 break;
1085 default:
1086 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
Andy Grovere3d6f902011-07-19 08:55:10 +00001087 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001088 break;
1089 }
1090
1091 if (p_buf)
1092 strncpy(p_buf, buf, p_buf_len);
1093 else
Andy Grover6708bb22011-06-08 10:36:43 -07001094 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001095
1096 return ret;
1097}
1098
1099int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1100{
1101 /*
1102 * The VPD identification association..
1103 *
1104 * from spc3r23.pdf Section 7.6.3.1 Table 297
1105 */
1106 vpd->association = (page_83[1] & 0x30);
1107 return transport_dump_vpd_assoc(vpd, NULL, 0);
1108}
1109EXPORT_SYMBOL(transport_set_vpd_assoc);
1110
1111int transport_dump_vpd_ident_type(
1112 struct t10_vpd *vpd,
1113 unsigned char *p_buf,
1114 int p_buf_len)
1115{
1116 unsigned char buf[VPD_TMP_BUF_SIZE];
Andy Grovere3d6f902011-07-19 08:55:10 +00001117 int ret = 0;
1118 int len;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001119
1120 memset(buf, 0, VPD_TMP_BUF_SIZE);
1121 len = sprintf(buf, "T10 VPD Identifier Type: ");
1122
1123 switch (vpd->device_identifier_type) {
1124 case 0x00:
1125 sprintf(buf+len, "Vendor specific\n");
1126 break;
1127 case 0x01:
1128 sprintf(buf+len, "T10 Vendor ID based\n");
1129 break;
1130 case 0x02:
1131 sprintf(buf+len, "EUI-64 based\n");
1132 break;
1133 case 0x03:
1134 sprintf(buf+len, "NAA\n");
1135 break;
1136 case 0x04:
1137 sprintf(buf+len, "Relative target port identifier\n");
1138 break;
1139 case 0x08:
1140 sprintf(buf+len, "SCSI name string\n");
1141 break;
1142 default:
1143 sprintf(buf+len, "Unsupported: 0x%02x\n",
1144 vpd->device_identifier_type);
Andy Grovere3d6f902011-07-19 08:55:10 +00001145 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001146 break;
1147 }
1148
Andy Grovere3d6f902011-07-19 08:55:10 +00001149 if (p_buf) {
1150 if (p_buf_len < strlen(buf)+1)
1151 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001152 strncpy(p_buf, buf, p_buf_len);
Andy Grovere3d6f902011-07-19 08:55:10 +00001153 } else {
Andy Grover6708bb22011-06-08 10:36:43 -07001154 pr_debug("%s", buf);
Andy Grovere3d6f902011-07-19 08:55:10 +00001155 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001156
1157 return ret;
1158}
1159
1160int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1161{
1162 /*
1163 * The VPD identifier type..
1164 *
1165 * from spc3r23.pdf Section 7.6.3.1 Table 298
1166 */
1167 vpd->device_identifier_type = (page_83[1] & 0x0f);
1168 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1169}
1170EXPORT_SYMBOL(transport_set_vpd_ident_type);
1171
1172int transport_dump_vpd_ident(
1173 struct t10_vpd *vpd,
1174 unsigned char *p_buf,
1175 int p_buf_len)
1176{
1177 unsigned char buf[VPD_TMP_BUF_SIZE];
1178 int ret = 0;
1179
1180 memset(buf, 0, VPD_TMP_BUF_SIZE);
1181
1182 switch (vpd->device_identifier_code_set) {
1183 case 0x01: /* Binary */
1184 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1185 &vpd->device_identifier[0]);
1186 break;
1187 case 0x02: /* ASCII */
1188 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1189 &vpd->device_identifier[0]);
1190 break;
1191 case 0x03: /* UTF-8 */
1192 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1193 &vpd->device_identifier[0]);
1194 break;
1195 default:
1196 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1197 " 0x%02x", vpd->device_identifier_code_set);
Andy Grovere3d6f902011-07-19 08:55:10 +00001198 ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001199 break;
1200 }
1201
1202 if (p_buf)
1203 strncpy(p_buf, buf, p_buf_len);
1204 else
Andy Grover6708bb22011-06-08 10:36:43 -07001205 pr_debug("%s", buf);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001206
1207 return ret;
1208}
1209
1210int
1211transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1212{
1213 static const char hex_str[] = "0123456789abcdef";
1214 int j = 0, i = 4; /* offset to start of the identifer */
1215
1216 /*
1217 * The VPD Code Set (encoding)
1218 *
1219 * from spc3r23.pdf Section 7.6.3.1 Table 296
1220 */
1221 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1222 switch (vpd->device_identifier_code_set) {
1223 case 0x01: /* Binary */
1224 vpd->device_identifier[j++] =
1225 hex_str[vpd->device_identifier_type];
1226 while (i < (4 + page_83[3])) {
1227 vpd->device_identifier[j++] =
1228 hex_str[(page_83[i] & 0xf0) >> 4];
1229 vpd->device_identifier[j++] =
1230 hex_str[page_83[i] & 0x0f];
1231 i++;
1232 }
1233 break;
1234 case 0x02: /* ASCII */
1235 case 0x03: /* UTF-8 */
1236 while (i < (4 + page_83[3]))
1237 vpd->device_identifier[j++] = page_83[i++];
1238 break;
1239 default:
1240 break;
1241 }
1242
1243 return transport_dump_vpd_ident(vpd, NULL, 0);
1244}
1245EXPORT_SYMBOL(transport_set_vpd_ident);
1246
1247static void core_setup_task_attr_emulation(struct se_device *dev)
1248{
1249 /*
1250 * If this device is from Target_Core_Mod/pSCSI, disable the
1251 * SAM Task Attribute emulation.
1252 *
1253 * This is currently not available in upsream Linux/SCSI Target
1254 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1255 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001256 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001257 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1258 return;
1259 }
1260
1261 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
Andy Grover6708bb22011-06-08 10:36:43 -07001262 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001263 " device\n", dev->transport->name,
1264 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001265}
1266
1267static void scsi_dump_inquiry(struct se_device *dev)
1268{
Andy Grovere3d6f902011-07-19 08:55:10 +00001269 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001270 int i, device_type;
1271 /*
1272 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1273 */
Andy Grover6708bb22011-06-08 10:36:43 -07001274 pr_debug(" Vendor: ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001275 for (i = 0; i < 8; i++)
1276 if (wwn->vendor[i] >= 0x20)
Andy Grover6708bb22011-06-08 10:36:43 -07001277 pr_debug("%c", wwn->vendor[i]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001278 else
Andy Grover6708bb22011-06-08 10:36:43 -07001279 pr_debug(" ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001280
Andy Grover6708bb22011-06-08 10:36:43 -07001281 pr_debug(" Model: ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282 for (i = 0; i < 16; i++)
1283 if (wwn->model[i] >= 0x20)
Andy Grover6708bb22011-06-08 10:36:43 -07001284 pr_debug("%c", wwn->model[i]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001285 else
Andy Grover6708bb22011-06-08 10:36:43 -07001286 pr_debug(" ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001287
Andy Grover6708bb22011-06-08 10:36:43 -07001288 pr_debug(" Revision: ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001289 for (i = 0; i < 4; i++)
1290 if (wwn->revision[i] >= 0x20)
Andy Grover6708bb22011-06-08 10:36:43 -07001291 pr_debug("%c", wwn->revision[i]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001292 else
Andy Grover6708bb22011-06-08 10:36:43 -07001293 pr_debug(" ");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001294
Andy Grover6708bb22011-06-08 10:36:43 -07001295 pr_debug("\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001296
Andy Grovere3d6f902011-07-19 08:55:10 +00001297 device_type = dev->transport->get_device_type(dev);
Andy Grover6708bb22011-06-08 10:36:43 -07001298 pr_debug(" Type: %s ", scsi_device_type(device_type));
1299 pr_debug(" ANSI SCSI revision: %02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001300 dev->transport->get_device_rev(dev));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001301}
1302
1303struct se_device *transport_add_device_to_core_hba(
1304 struct se_hba *hba,
1305 struct se_subsystem_api *transport,
1306 struct se_subsystem_dev *se_dev,
1307 u32 device_flags,
1308 void *transport_dev,
1309 struct se_dev_limits *dev_limits,
1310 const char *inquiry_prod,
1311 const char *inquiry_rev)
1312{
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001313 int force_pt;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001314 struct se_device *dev;
1315
1316 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001317 if (!dev) {
1318 pr_err("Unable to allocate memory for se_dev_t\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001319 return NULL;
1320 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001321
Andy Grovere3d6f902011-07-19 08:55:10 +00001322 transport_init_queue_obj(&dev->dev_queue_obj);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001323 dev->dev_flags = device_flags;
1324 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
Andy Grover5951146d2011-07-19 10:26:37 +00001325 dev->dev_ptr = transport_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001326 dev->se_hba = hba;
1327 dev->se_sub_dev = se_dev;
1328 dev->transport = transport;
1329 atomic_set(&dev->active_cmds, 0);
1330 INIT_LIST_HEAD(&dev->dev_list);
1331 INIT_LIST_HEAD(&dev->dev_sep_list);
1332 INIT_LIST_HEAD(&dev->dev_tmr_list);
1333 INIT_LIST_HEAD(&dev->execute_task_list);
1334 INIT_LIST_HEAD(&dev->delayed_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001335 INIT_LIST_HEAD(&dev->state_task_list);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001336 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001337 spin_lock_init(&dev->execute_task_lock);
1338 spin_lock_init(&dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339 spin_lock_init(&dev->state_task_lock);
1340 spin_lock_init(&dev->dev_alua_lock);
1341 spin_lock_init(&dev->dev_reservation_lock);
1342 spin_lock_init(&dev->dev_status_lock);
1343 spin_lock_init(&dev->dev_status_thr_lock);
1344 spin_lock_init(&dev->se_port_lock);
1345 spin_lock_init(&dev->se_tmr_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001346 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001347
1348 dev->queue_depth = dev_limits->queue_depth;
1349 atomic_set(&dev->depth_left, dev->queue_depth);
1350 atomic_set(&dev->dev_ordered_id, 0);
1351
1352 se_dev_set_default_attribs(dev, dev_limits);
1353
1354 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1355 dev->creation_time = get_jiffies_64();
1356 spin_lock_init(&dev->stats_lock);
1357
1358 spin_lock(&hba->device_lock);
1359 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1360 hba->dev_count++;
1361 spin_unlock(&hba->device_lock);
1362 /*
1363 * Setup the SAM Task Attribute emulation for struct se_device
1364 */
1365 core_setup_task_attr_emulation(dev);
1366 /*
1367 * Force PR and ALUA passthrough emulation with internal object use.
1368 */
1369 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1370 /*
1371 * Setup the Reservations infrastructure for struct se_device
1372 */
1373 core_setup_reservations(dev, force_pt);
1374 /*
1375 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1376 */
1377 if (core_setup_alua(dev, force_pt) < 0)
1378 goto out;
1379
1380 /*
1381 * Startup the struct se_device processing thread
1382 */
1383 dev->process_thread = kthread_run(transport_processing_thread, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001384 "LIO_%s", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001385 if (IS_ERR(dev->process_thread)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001386 pr_err("Unable to create kthread: LIO_%s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001387 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001388 goto out;
1389 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001390 /*
1391 * Setup work_queue for QUEUE_FULL
1392 */
1393 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001394 /*
1395 * Preload the initial INQUIRY const values if we are doing
1396 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1397 * passthrough because this is being provided by the backend LLD.
1398 * This is required so that transport_get_inquiry() copies these
1399 * originals once back into DEV_T10_WWN(dev) for the virtual device
1400 * setup.
1401 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001402 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
Roland Dreierf22c1192011-05-02 22:15:37 -07001403 if (!inquiry_prod || !inquiry_rev) {
Andy Grover6708bb22011-06-08 10:36:43 -07001404 pr_err("All non TCM/pSCSI plugins require"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001405 " INQUIRY consts\n");
1406 goto out;
1407 }
1408
Andy Grovere3d6f902011-07-19 08:55:10 +00001409 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1410 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1411 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412 }
1413 scsi_dump_inquiry(dev);
1414
Nicholas Bellinger12a18bd2011-03-14 04:06:06 -07001415 return dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001416out:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001417 kthread_stop(dev->process_thread);
1418
1419 spin_lock(&hba->device_lock);
1420 list_del(&dev->dev_list);
1421 hba->dev_count--;
1422 spin_unlock(&hba->device_lock);
1423
1424 se_release_vpd_for_dev(dev);
1425
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001426 kfree(dev);
1427
1428 return NULL;
1429}
1430EXPORT_SYMBOL(transport_add_device_to_core_hba);
1431
1432/* transport_generic_prepare_cdb():
1433 *
1434 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1435 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1436 * The point of this is since we are mapping iSCSI LUNs to
1437 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1438 * devices and HBAs for a loop.
1439 */
1440static inline void transport_generic_prepare_cdb(
1441 unsigned char *cdb)
1442{
1443 switch (cdb[0]) {
1444 case READ_10: /* SBC - RDProtect */
1445 case READ_12: /* SBC - RDProtect */
1446 case READ_16: /* SBC - RDProtect */
1447 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1448 case VERIFY: /* SBC - VRProtect */
1449 case VERIFY_16: /* SBC - VRProtect */
1450 case WRITE_VERIFY: /* SBC - VRProtect */
1451 case WRITE_VERIFY_12: /* SBC - VRProtect */
1452 break;
1453 default:
1454 cdb[1] &= 0x1f; /* clear logical unit number */
1455 break;
1456 }
1457}
1458
1459static struct se_task *
1460transport_generic_get_task(struct se_cmd *cmd,
1461 enum dma_data_direction data_direction)
1462{
1463 struct se_task *task;
Andy Grover5951146d2011-07-19 10:26:37 +00001464 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001465
Andy Grover6708bb22011-06-08 10:36:43 -07001466 task = dev->transport->alloc_task(cmd->t_task_cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001467 if (!task) {
Andy Grover6708bb22011-06-08 10:36:43 -07001468 pr_err("Unable to allocate struct se_task\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001469 return NULL;
1470 }
1471
1472 INIT_LIST_HEAD(&task->t_list);
1473 INIT_LIST_HEAD(&task->t_execute_list);
1474 INIT_LIST_HEAD(&task->t_state_list);
1475 init_completion(&task->task_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001476 task->task_se_cmd = cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001477 task->task_data_direction = data_direction;
1478
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001479 return task;
1480}
1481
1482static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1483
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001484/*
1485 * Used by fabric modules containing a local struct se_cmd within their
1486 * fabric dependent per I/O descriptor.
1487 */
1488void transport_init_se_cmd(
1489 struct se_cmd *cmd,
1490 struct target_core_fabric_ops *tfo,
1491 struct se_session *se_sess,
1492 u32 data_length,
1493 int data_direction,
1494 int task_attr,
1495 unsigned char *sense_buffer)
1496{
Andy Grover5951146d2011-07-19 10:26:37 +00001497 INIT_LIST_HEAD(&cmd->se_lun_node);
1498 INIT_LIST_HEAD(&cmd->se_delayed_node);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001499 INIT_LIST_HEAD(&cmd->se_qf_node);
Roland Dreier79a7fef2011-09-28 22:12:07 -07001500 INIT_LIST_HEAD(&cmd->se_queue_node);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001501 INIT_LIST_HEAD(&cmd->se_cmd_list);
Andy Grovera1d8b492011-05-02 17:12:10 -07001502 INIT_LIST_HEAD(&cmd->t_task_list);
1503 init_completion(&cmd->transport_lun_fe_stop_comp);
1504 init_completion(&cmd->transport_lun_stop_comp);
1505 init_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07001506 init_completion(&cmd->cmd_wait_comp);
Andy Grovera1d8b492011-05-02 17:12:10 -07001507 spin_lock_init(&cmd->t_state_lock);
1508 atomic_set(&cmd->transport_dev_active, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001509
1510 cmd->se_tfo = tfo;
1511 cmd->se_sess = se_sess;
1512 cmd->data_length = data_length;
1513 cmd->data_direction = data_direction;
1514 cmd->sam_task_attr = task_attr;
1515 cmd->sense_buffer = sense_buffer;
1516}
1517EXPORT_SYMBOL(transport_init_se_cmd);
1518
1519static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1520{
1521 /*
1522 * Check if SAM Task Attribute emulation is enabled for this
1523 * struct se_device storage object
1524 */
Andy Grover5951146d2011-07-19 10:26:37 +00001525 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001526 return 0;
1527
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001528 if (cmd->sam_task_attr == MSG_ACA_TAG) {
Andy Grover6708bb22011-06-08 10:36:43 -07001529 pr_debug("SAM Task Attribute ACA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001530 " emulation is not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001531 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001532 }
1533 /*
1534 * Used to determine when ORDERED commands should go from
1535 * Dormant to Active status.
1536 */
Andy Grover5951146d2011-07-19 10:26:37 +00001537 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001538 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001539 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001540 cmd->se_ordered_id, cmd->sam_task_attr,
Andy Grover6708bb22011-06-08 10:36:43 -07001541 cmd->se_dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001542 return 0;
1543}
1544
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001545/* transport_generic_allocate_tasks():
1546 *
1547 * Called from fabric RX Thread.
1548 */
1549int transport_generic_allocate_tasks(
1550 struct se_cmd *cmd,
1551 unsigned char *cdb)
1552{
1553 int ret;
1554
1555 transport_generic_prepare_cdb(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001556 /*
1557 * Ensure that the received CDB is less than the max (252 + 8) bytes
1558 * for VARIABLE_LENGTH_CMD
1559 */
1560 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001561 pr_err("Received SCSI CDB with command_size: %d that"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001562 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1563 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001564 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1565 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grovere3d6f902011-07-19 08:55:10 +00001566 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001567 }
1568 /*
1569 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1570 * allocate the additional extended CDB buffer now.. Otherwise
1571 * setup the pointer from __t_task_cdb to t_task_cdb.
1572 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001573 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1574 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001575 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001576 if (!cmd->t_task_cdb) {
1577 pr_err("Unable to allocate cmd->t_task_cdb"
Andy Grovera1d8b492011-05-02 17:12:10 -07001578 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579 scsi_command_size(cdb),
Andy Grovera1d8b492011-05-02 17:12:10 -07001580 (unsigned long)sizeof(cmd->__t_task_cdb));
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001581 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1582 cmd->scsi_sense_reason =
1583 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grovere3d6f902011-07-19 08:55:10 +00001584 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001585 }
1586 } else
Andy Grovera1d8b492011-05-02 17:12:10 -07001587 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001588 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07001589 * Copy the original CDB into cmd->
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001590 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001591 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001592 /*
1593 * Setup the received CDB based on SCSI defined opcodes and
1594 * perform unit attention, persistent reservations and ALUA
Andy Grovera1d8b492011-05-02 17:12:10 -07001595 * checks for virtual device backends. The cmd->t_task_cdb
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001596 * pointer is expected to be setup before we reach this point.
1597 */
1598 ret = transport_generic_cmd_sequencer(cmd, cdb);
1599 if (ret < 0)
1600 return ret;
1601 /*
1602 * Check for SAM Task Attribute Emulation
1603 */
1604 if (transport_check_alloc_task_attr(cmd) < 0) {
1605 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1606 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00001607 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001608 }
1609 spin_lock(&cmd->se_lun->lun_sep_lock);
1610 if (cmd->se_lun->lun_sep)
1611 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1612 spin_unlock(&cmd->se_lun->lun_sep_lock);
1613 return 0;
1614}
1615EXPORT_SYMBOL(transport_generic_allocate_tasks);
1616
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001617/*
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001618 * Used by fabric module frontends to queue tasks directly.
1619 * Many only be used from process context only
1620 */
1621int transport_handle_cdb_direct(
1622 struct se_cmd *cmd)
1623{
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001624 int ret;
1625
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001626 if (!cmd->se_lun) {
1627 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001628 pr_err("cmd->se_lun is NULL\n");
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001629 return -EINVAL;
1630 }
1631 if (in_interrupt()) {
1632 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001633 pr_err("transport_generic_handle_cdb cannot be called"
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001634 " from interrupt context\n");
1635 return -EINVAL;
1636 }
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001637 /*
1638 * Set TRANSPORT_NEW_CMD state and cmd->t_transport_active=1 following
1639 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1640 * in existing usage to ensure that outstanding descriptors are handled
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07001641 * correctly during shutdown via transport_wait_for_tasks()
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001642 *
1643 * Also, we don't take cmd->t_state_lock here as we only expect
1644 * this to be called for initial descriptor submission.
1645 */
1646 cmd->t_state = TRANSPORT_NEW_CMD;
1647 atomic_set(&cmd->t_transport_active, 1);
1648 /*
1649 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1650 * so follow TRANSPORT_NEW_CMD processing thread context usage
1651 * and call transport_generic_request_failure() if necessary..
1652 */
1653 ret = transport_generic_new_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001654 if (ret < 0)
1655 transport_generic_request_failure(cmd);
1656
Nicholas Bellingerdd8ae592011-07-30 05:03:58 -07001657 return 0;
Nicholas Bellinger695434e12011-06-03 20:59:19 -07001658}
1659EXPORT_SYMBOL(transport_handle_cdb_direct);
1660
1661/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001662 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1663 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1664 * complete setup in TCM process context w/ TFO->new_cmd_map().
1665 */
1666int transport_generic_handle_cdb_map(
1667 struct se_cmd *cmd)
1668{
Andy Grovere3d6f902011-07-19 08:55:10 +00001669 if (!cmd->se_lun) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001670 dump_stack();
Andy Grover6708bb22011-06-08 10:36:43 -07001671 pr_err("cmd->se_lun is NULL\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001672 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001673 }
1674
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001675 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001676 return 0;
1677}
1678EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1679
1680/* transport_generic_handle_data():
1681 *
1682 *
1683 */
1684int transport_generic_handle_data(
1685 struct se_cmd *cmd)
1686{
1687 /*
1688 * For the software fabric case, then we assume the nexus is being
1689 * failed/shutdown when signals are pending from the kthread context
1690 * caller, so we return a failure. For the HW target mode case running
1691 * in interrupt code, the signal_pending() check is skipped.
1692 */
1693 if (!in_interrupt() && signal_pending(current))
Andy Grovere3d6f902011-07-19 08:55:10 +00001694 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001695 /*
1696 * If the received CDB has aleady been ABORTED by the generic
1697 * target engine, we now call transport_check_aborted_status()
1698 * to queue any delated TASK_ABORTED status for the received CDB to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001699 * fabric module as we are expecting no further incoming DATA OUT
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001700 * sequences at this point.
1701 */
1702 if (transport_check_aborted_status(cmd, 1) != 0)
1703 return 0;
1704
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001705 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001706 return 0;
1707}
1708EXPORT_SYMBOL(transport_generic_handle_data);
1709
1710/* transport_generic_handle_tmr():
1711 *
1712 *
1713 */
1714int transport_generic_handle_tmr(
1715 struct se_cmd *cmd)
1716{
Christoph Hellwigf7a5cc02011-10-17 13:56:42 -04001717 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001718 return 0;
1719}
1720EXPORT_SYMBOL(transport_generic_handle_tmr);
1721
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001722/*
1723 * If the task is active, request it to be stopped and sleep until it
1724 * has completed.
1725 */
1726bool target_stop_task(struct se_task *task, unsigned long *flags)
1727{
1728 struct se_cmd *cmd = task->task_se_cmd;
1729 bool was_active = false;
1730
1731 if (task->task_flags & TF_ACTIVE) {
1732 task->task_flags |= TF_REQUEST_STOP;
1733 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1734
1735 pr_debug("Task %p waiting to complete\n", task);
1736 wait_for_completion(&task->task_stop_comp);
1737 pr_debug("Task %p stopped successfully\n", task);
1738
1739 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1740 atomic_dec(&cmd->t_task_cdbs_left);
1741 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1742 was_active = true;
1743 }
1744
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001745 return was_active;
1746}
1747
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001748static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1749{
1750 struct se_task *task, *task_tmp;
1751 unsigned long flags;
1752 int ret = 0;
1753
Andy Grover6708bb22011-06-08 10:36:43 -07001754 pr_debug("ITT[0x%08x] - Stopping tasks\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001755 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001756
1757 /*
1758 * No tasks remain in the execution queue
1759 */
Andy Grovera1d8b492011-05-02 17:12:10 -07001760 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001761 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07001762 &cmd->t_task_list, t_list) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001763 pr_debug("Processing task %p\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001764 /*
1765 * If the struct se_task has not been sent and is not active,
1766 * remove the struct se_task from the execution queue.
1767 */
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04001768 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
Andy Grovera1d8b492011-05-02 17:12:10 -07001769 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001770 flags);
1771 transport_remove_task_from_execute_queue(task,
Christoph Hellwig42bf8292011-10-12 11:07:00 -04001772 cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001773
Christoph Hellwig04629b72011-10-12 11:07:04 -04001774 pr_debug("Task %p removed from execute queue\n", task);
Andy Grovera1d8b492011-05-02 17:12:10 -07001775 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001776 continue;
1777 }
1778
Christoph Hellwigcdbb70b2011-10-17 13:56:46 -04001779 if (!target_stop_task(task, &flags)) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04001780 pr_debug("Task %p - did nothing\n", task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001781 ret++;
1782 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001783 }
Andy Grovera1d8b492011-05-02 17:12:10 -07001784 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001785
1786 return ret;
1787}
1788
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001789/*
1790 * Handle SAM-esque emulation for generic transport request failures.
1791 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001792static void transport_generic_request_failure(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001793{
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001794 int ret = 0;
1795
Andy Grover6708bb22011-06-08 10:36:43 -07001796 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
Andy Grovere3d6f902011-07-19 08:55:10 +00001797 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
Andy Grovera1d8b492011-05-02 17:12:10 -07001798 cmd->t_task_cdb[0]);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001799 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001800 cmd->se_tfo->get_cmd_state(cmd),
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001801 cmd->t_state, cmd->scsi_sense_reason);
Andy Grover6708bb22011-06-08 10:36:43 -07001802 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001803 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
1804 " t_transport_active: %d t_transport_stop: %d"
Andy Grover6708bb22011-06-08 10:36:43 -07001805 " t_transport_sent: %d\n", cmd->t_task_list_num,
Andy Grovera1d8b492011-05-02 17:12:10 -07001806 atomic_read(&cmd->t_task_cdbs_left),
1807 atomic_read(&cmd->t_task_cdbs_sent),
1808 atomic_read(&cmd->t_task_cdbs_ex_left),
1809 atomic_read(&cmd->t_transport_active),
1810 atomic_read(&cmd->t_transport_stop),
1811 atomic_read(&cmd->t_transport_sent));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001812
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001813 /*
1814 * For SAM Task Attribute emulation for failed struct se_cmd
1815 */
1816 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1817 transport_complete_task_attr(cmd);
1818
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001819 switch (cmd->scsi_sense_reason) {
1820 case TCM_NON_EXISTENT_LUN:
1821 case TCM_UNSUPPORTED_SCSI_OPCODE:
1822 case TCM_INVALID_CDB_FIELD:
1823 case TCM_INVALID_PARAMETER_LIST:
1824 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1825 case TCM_UNKNOWN_MODE_PAGE:
1826 case TCM_WRITE_PROTECTED:
1827 case TCM_CHECK_CONDITION_ABORT_CMD:
1828 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1829 case TCM_CHECK_CONDITION_NOT_READY:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001830 break;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001831 case TCM_RESERVATION_CONFLICT:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001832 /*
1833 * No SENSE Data payload for this case, set SCSI Status
1834 * and queue the response to $FABRIC_MOD.
1835 *
1836 * Uses linux/include/scsi/scsi.h SAM status codes defs
1837 */
1838 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1839 /*
1840 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1841 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1842 * CONFLICT STATUS.
1843 *
1844 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1845 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001846 if (cmd->se_sess &&
1847 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1848 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001849 cmd->orig_fe_lun, 0x2C,
1850 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1851
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001852 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07001853 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001854 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001855 goto check_stop;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001856 default:
Andy Grover6708bb22011-06-08 10:36:43 -07001857 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001858 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001859 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1860 break;
1861 }
Nicholas Bellinger16ab8e62011-08-08 19:03:38 -07001862 /*
1863 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1864 * make the call to transport_send_check_condition_and_sense()
1865 * directly. Otherwise expect the fabric to make the call to
1866 * transport_send_check_condition_and_sense() after handling
1867 * possible unsoliticied write data payloads.
1868 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001869 ret = transport_send_check_condition_and_sense(cmd,
1870 cmd->scsi_sense_reason, 0);
1871 if (ret == -EAGAIN || ret == -ENOMEM)
1872 goto queue_full;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001873
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001874check_stop:
1875 transport_lun_remove_cmd(cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07001876 if (!transport_cmd_check_stop_to_fabric(cmd))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001877 ;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07001878 return;
1879
1880queue_full:
Christoph Hellwige057f532011-10-17 13:56:41 -04001881 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1882 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001883}
1884
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001885static inline u32 transport_lba_21(unsigned char *cdb)
1886{
1887 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1888}
1889
1890static inline u32 transport_lba_32(unsigned char *cdb)
1891{
1892 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1893}
1894
1895static inline unsigned long long transport_lba_64(unsigned char *cdb)
1896{
1897 unsigned int __v1, __v2;
1898
1899 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1900 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1901
1902 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1903}
1904
1905/*
1906 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1907 */
1908static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1909{
1910 unsigned int __v1, __v2;
1911
1912 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1913 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1914
1915 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1916}
1917
1918static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
1919{
1920 unsigned long flags;
1921
Andy Grovera1d8b492011-05-02 17:12:10 -07001922 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001923 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
Andy Grovera1d8b492011-05-02 17:12:10 -07001924 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001925}
1926
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001927static inline int transport_tcq_window_closed(struct se_device *dev)
1928{
1929 if (dev->dev_tcq_window_closed++ <
1930 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
1931 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
1932 } else
1933 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
1934
Andy Grovere3d6f902011-07-19 08:55:10 +00001935 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001936 return 0;
1937}
1938
1939/*
1940 * Called from Fabric Module context from transport_execute_tasks()
1941 *
1942 * The return of this function determins if the tasks from struct se_cmd
1943 * get added to the execution queue in transport_execute_tasks(),
1944 * or are added to the delayed or ordered lists here.
1945 */
1946static inline int transport_execute_task_attr(struct se_cmd *cmd)
1947{
Andy Grover5951146d2011-07-19 10:26:37 +00001948 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001949 return 1;
1950 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001951 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001952 * to allow the passed struct se_cmd list of tasks to the front of the list.
1953 */
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001954 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Andy Grover5951146d2011-07-19 10:26:37 +00001955 atomic_inc(&cmd->se_dev->dev_hoq_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001956 smp_mb__after_atomic_inc();
Andy Grover6708bb22011-06-08 10:36:43 -07001957 pr_debug("Added HEAD_OF_QUEUE for CDB:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001958 " 0x%02x, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07001959 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001960 cmd->se_ordered_id);
1961 return 1;
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07001962 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Andy Grover5951146d2011-07-19 10:26:37 +00001963 atomic_inc(&cmd->se_dev->dev_ordered_sync);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001964 smp_mb__after_atomic_inc();
1965
Andy Grover6708bb22011-06-08 10:36:43 -07001966 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001967 " list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07001968 cmd->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001969 cmd->se_ordered_id);
1970 /*
1971 * Add ORDERED command to tail of execution queue if
1972 * no other older commands exist that need to be
1973 * completed first.
1974 */
Andy Grover6708bb22011-06-08 10:36:43 -07001975 if (!atomic_read(&cmd->se_dev->simple_cmds))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001976 return 1;
1977 } else {
1978 /*
1979 * For SIMPLE and UNTAGGED Task Attribute commands
1980 */
Andy Grover5951146d2011-07-19 10:26:37 +00001981 atomic_inc(&cmd->se_dev->simple_cmds);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001982 smp_mb__after_atomic_inc();
1983 }
1984 /*
1985 * Otherwise if one or more outstanding ORDERED task attribute exist,
1986 * add the dormant task(s) built for the passed struct se_cmd to the
1987 * execution queue and become in Active state for this struct se_device.
1988 */
Andy Grover5951146d2011-07-19 10:26:37 +00001989 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001990 /*
1991 * Otherwise, add cmd w/ tasks to delayed cmd queue that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001992 * will be drained upon completion of HEAD_OF_QUEUE task.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001993 */
Andy Grover5951146d2011-07-19 10:26:37 +00001994 spin_lock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001995 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
Andy Grover5951146d2011-07-19 10:26:37 +00001996 list_add_tail(&cmd->se_delayed_node,
1997 &cmd->se_dev->delayed_cmd_list);
1998 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001999
Andy Grover6708bb22011-06-08 10:36:43 -07002000 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002001 " delayed CMD list, se_ordered_id: %u\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07002002 cmd->t_task_cdb[0], cmd->sam_task_attr,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002003 cmd->se_ordered_id);
2004 /*
2005 * Return zero to let transport_execute_tasks() know
2006 * not to add the delayed tasks to the execution list.
2007 */
2008 return 0;
2009 }
2010 /*
2011 * Otherwise, no ORDERED task attributes exist..
2012 */
2013 return 1;
2014}
2015
2016/*
2017 * Called from fabric module context in transport_generic_new_cmd() and
2018 * transport_generic_process_write()
2019 */
2020static int transport_execute_tasks(struct se_cmd *cmd)
2021{
2022 int add_tasks;
2023
Christoph Hellwig58a28012011-11-14 11:36:27 -05002024 if (se_dev_check_online(cmd->se_dev) != 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002025 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2026 transport_generic_request_failure(cmd);
Christoph Hellwigdb1620a2011-05-31 17:06:43 -04002027 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002028 }
Christoph Hellwigdb1620a2011-05-31 17:06:43 -04002029
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002030 /*
2031 * Call transport_cmd_check_stop() to see if a fabric exception
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002032 * has occurred that prevents execution.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002033 */
Andy Grover6708bb22011-06-08 10:36:43 -07002034 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002035 /*
2036 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2037 * attribute for the tasks of the received struct se_cmd CDB
2038 */
2039 add_tasks = transport_execute_task_attr(cmd);
Andy Grovere3d6f902011-07-19 08:55:10 +00002040 if (!add_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002041 goto execute_tasks;
2042 /*
2043 * This calls transport_add_tasks_from_cmd() to handle
2044 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2045 * (if enabled) in __transport_add_task_to_execute_queue() and
2046 * transport_add_task_check_sam_attr().
2047 */
2048 transport_add_tasks_from_cmd(cmd);
2049 }
2050 /*
2051 * Kick the execution queue for the cmd associated struct se_device
2052 * storage object.
2053 */
2054execute_tasks:
Andy Grover5951146d2011-07-19 10:26:37 +00002055 __transport_execute_tasks(cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002056 return 0;
2057}
2058
2059/*
2060 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2061 * from struct se_device->execute_task_list and
2062 *
2063 * Called from transport_processing_thread()
2064 */
2065static int __transport_execute_tasks(struct se_device *dev)
2066{
2067 int error;
2068 struct se_cmd *cmd = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +00002069 struct se_task *task = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002070 unsigned long flags;
2071
2072 /*
2073 * Check if there is enough room in the device and HBA queue to send
Andy Grovera1d8b492011-05-02 17:12:10 -07002074 * struct se_tasks to the selected transport.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002075 */
2076check_depth:
Andy Grovere3d6f902011-07-19 08:55:10 +00002077 if (!atomic_read(&dev->depth_left))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002078 return transport_tcq_window_closed(dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002079
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002080 dev->dev_tcq_window_closed = 0;
2081
Andy Grovere3d6f902011-07-19 08:55:10 +00002082 spin_lock_irq(&dev->execute_task_lock);
2083 if (list_empty(&dev->execute_task_list)) {
2084 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002085 return 0;
2086 }
Andy Grovere3d6f902011-07-19 08:55:10 +00002087 task = list_first_entry(&dev->execute_task_list,
2088 struct se_task, t_execute_list);
Christoph Hellwig04629b72011-10-12 11:07:04 -04002089 __transport_remove_task_from_execute_queue(task, dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002090 spin_unlock_irq(&dev->execute_task_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002091
2092 atomic_dec(&dev->depth_left);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002093
Andy Grovere3d6f902011-07-19 08:55:10 +00002094 cmd = task->task_se_cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002095
Andy Grovera1d8b492011-05-02 17:12:10 -07002096 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig6c76bf92011-10-12 11:07:03 -04002097 task->task_flags |= (TF_ACTIVE | TF_SENT);
Andy Grovera1d8b492011-05-02 17:12:10 -07002098 atomic_inc(&cmd->t_task_cdbs_sent);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002099
Andy Grovera1d8b492011-05-02 17:12:10 -07002100 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2101 cmd->t_task_list_num)
Nicholas Bellinger415a0902011-10-23 18:16:13 -07002102 atomic_set(&cmd->t_transport_sent, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002103
Andy Grovera1d8b492011-05-02 17:12:10 -07002104 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002105
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002106 if (cmd->execute_task)
2107 error = cmd->execute_task(task);
2108 else
2109 error = dev->transport->do_task(task);
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002110 if (error != 0) {
Christoph Hellwigd29a5b62011-11-03 17:50:44 -04002111 spin_lock_irqsave(&cmd->t_state_lock, flags);
2112 task->task_flags &= ~TF_ACTIVE;
2113 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2114 atomic_set(&cmd->t_transport_sent, 0);
2115 transport_stop_tasks_for_cmd(cmd);
2116 atomic_inc(&dev->depth_left);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002117 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002118 }
2119
2120 goto check_depth;
2121
2122 return 0;
2123}
2124
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002125static inline u32 transport_get_sectors_6(
2126 unsigned char *cdb,
2127 struct se_cmd *cmd,
2128 int *ret)
2129{
Andy Grover5951146d2011-07-19 10:26:37 +00002130 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002131
2132 /*
2133 * Assume TYPE_DISK for non struct se_device objects.
2134 * Use 8-bit sector value.
2135 */
2136 if (!dev)
2137 goto type_disk;
2138
2139 /*
2140 * Use 24-bit allocation length for TYPE_TAPE.
2141 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002142 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002143 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2144
2145 /*
2146 * Everything else assume TYPE_DISK Sector CDB location.
2147 * Use 8-bit sector value.
2148 */
2149type_disk:
2150 return (u32)cdb[4];
2151}
2152
2153static inline u32 transport_get_sectors_10(
2154 unsigned char *cdb,
2155 struct se_cmd *cmd,
2156 int *ret)
2157{
Andy Grover5951146d2011-07-19 10:26:37 +00002158 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002159
2160 /*
2161 * Assume TYPE_DISK for non struct se_device objects.
2162 * Use 16-bit sector value.
2163 */
2164 if (!dev)
2165 goto type_disk;
2166
2167 /*
2168 * XXX_10 is not defined in SSC, throw an exception
2169 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002170 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2171 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002172 return 0;
2173 }
2174
2175 /*
2176 * Everything else assume TYPE_DISK Sector CDB location.
2177 * Use 16-bit sector value.
2178 */
2179type_disk:
2180 return (u32)(cdb[7] << 8) + cdb[8];
2181}
2182
2183static inline u32 transport_get_sectors_12(
2184 unsigned char *cdb,
2185 struct se_cmd *cmd,
2186 int *ret)
2187{
Andy Grover5951146d2011-07-19 10:26:37 +00002188 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002189
2190 /*
2191 * Assume TYPE_DISK for non struct se_device objects.
2192 * Use 32-bit sector value.
2193 */
2194 if (!dev)
2195 goto type_disk;
2196
2197 /*
2198 * XXX_12 is not defined in SSC, throw an exception
2199 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002200 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2201 *ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002202 return 0;
2203 }
2204
2205 /*
2206 * Everything else assume TYPE_DISK Sector CDB location.
2207 * Use 32-bit sector value.
2208 */
2209type_disk:
2210 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2211}
2212
2213static inline u32 transport_get_sectors_16(
2214 unsigned char *cdb,
2215 struct se_cmd *cmd,
2216 int *ret)
2217{
Andy Grover5951146d2011-07-19 10:26:37 +00002218 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002219
2220 /*
2221 * Assume TYPE_DISK for non struct se_device objects.
2222 * Use 32-bit sector value.
2223 */
2224 if (!dev)
2225 goto type_disk;
2226
2227 /*
2228 * Use 24-bit allocation length for TYPE_TAPE.
2229 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002230 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002231 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2232
2233type_disk:
2234 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2235 (cdb[12] << 8) + cdb[13];
2236}
2237
2238/*
2239 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2240 */
2241static inline u32 transport_get_sectors_32(
2242 unsigned char *cdb,
2243 struct se_cmd *cmd,
2244 int *ret)
2245{
2246 /*
2247 * Assume TYPE_DISK for non struct se_device objects.
2248 * Use 32-bit sector value.
2249 */
2250 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2251 (cdb[30] << 8) + cdb[31];
2252
2253}
2254
2255static inline u32 transport_get_size(
2256 u32 sectors,
2257 unsigned char *cdb,
2258 struct se_cmd *cmd)
2259{
Andy Grover5951146d2011-07-19 10:26:37 +00002260 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002261
Andy Grovere3d6f902011-07-19 08:55:10 +00002262 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002263 if (cdb[1] & 1) { /* sectors */
Andy Grovere3d6f902011-07-19 08:55:10 +00002264 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002265 } else /* bytes */
2266 return sectors;
2267 }
2268#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002269 pr_debug("Returning block_size: %u, sectors: %u == %u for"
Andy Grovere3d6f902011-07-19 08:55:10 +00002270 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2271 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2272 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002273#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00002274 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002275}
2276
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002277static void transport_xor_callback(struct se_cmd *cmd)
2278{
2279 unsigned char *buf, *addr;
Andy Groverec98f782011-07-20 19:28:46 +00002280 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002281 unsigned int offset;
2282 int i;
Andy Groverec98f782011-07-20 19:28:46 +00002283 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002284 /*
2285 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2286 *
2287 * 1) read the specified logical block(s);
2288 * 2) transfer logical blocks from the data-out buffer;
2289 * 3) XOR the logical blocks transferred from the data-out buffer with
2290 * the logical blocks read, storing the resulting XOR data in a buffer;
2291 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2292 * blocks transferred from the data-out buffer; and
2293 * 5) transfer the resulting XOR data to the data-in buffer.
2294 */
2295 buf = kmalloc(cmd->data_length, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07002296 if (!buf) {
2297 pr_err("Unable to allocate xor_callback buf\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002298 return;
2299 }
2300 /*
Andy Groverec98f782011-07-20 19:28:46 +00002301 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002302 * into the locally allocated *buf
2303 */
Andy Groverec98f782011-07-20 19:28:46 +00002304 sg_copy_to_buffer(cmd->t_data_sg,
2305 cmd->t_data_nents,
2306 buf,
2307 cmd->data_length);
2308
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002309 /*
2310 * Now perform the XOR against the BIDI read memory located at
Andy Grovera1d8b492011-05-02 17:12:10 -07002311 * cmd->t_mem_bidi_list
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002312 */
2313
2314 offset = 0;
Andy Groverec98f782011-07-20 19:28:46 +00002315 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2316 addr = kmap_atomic(sg_page(sg), KM_USER0);
2317 if (!addr)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002318 goto out;
2319
Andy Groverec98f782011-07-20 19:28:46 +00002320 for (i = 0; i < sg->length; i++)
2321 *(addr + sg->offset + i) ^= *(buf + offset + i);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002322
Andy Groverec98f782011-07-20 19:28:46 +00002323 offset += sg->length;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002324 kunmap_atomic(addr, KM_USER0);
2325 }
Andy Groverec98f782011-07-20 19:28:46 +00002326
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002327out:
2328 kfree(buf);
2329}
2330
2331/*
2332 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2333 */
2334static int transport_get_sense_data(struct se_cmd *cmd)
2335{
2336 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002337 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002338 struct se_task *task = NULL, *task_tmp;
2339 unsigned long flags;
2340 u32 offset = 0;
2341
Andy Grovere3d6f902011-07-19 08:55:10 +00002342 WARN_ON(!cmd->se_lun);
2343
Christoph Hellwig42bf8292011-10-12 11:07:00 -04002344 if (!dev)
2345 return 0;
2346
Andy Grovera1d8b492011-05-02 17:12:10 -07002347 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002348 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07002349 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002350 return 0;
2351 }
2352
2353 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07002354 &cmd->t_task_list, t_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002355 if (!task->task_sense)
2356 continue;
2357
Andy Grovere3d6f902011-07-19 08:55:10 +00002358 if (!dev->transport->get_sense_buffer) {
Andy Grover6708bb22011-06-08 10:36:43 -07002359 pr_err("dev->transport->get_sense_buffer"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002360 " is NULL\n");
2361 continue;
2362 }
2363
Andy Grovere3d6f902011-07-19 08:55:10 +00002364 sense_buffer = dev->transport->get_sense_buffer(task);
Andy Grover6708bb22011-06-08 10:36:43 -07002365 if (!sense_buffer) {
Christoph Hellwig04629b72011-10-12 11:07:04 -04002366 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002367 " sense buffer for task with sense\n",
Christoph Hellwig04629b72011-10-12 11:07:04 -04002368 cmd->se_tfo->get_task_tag(cmd), task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002369 continue;
2370 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002371 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002372
Andy Grovere3d6f902011-07-19 08:55:10 +00002373 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002374 TRANSPORT_SENSE_BUFFER);
2375
Andy Grover5951146d2011-07-19 10:26:37 +00002376 memcpy(&buffer[offset], sense_buffer,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002377 TRANSPORT_SENSE_BUFFER);
2378 cmd->scsi_status = task->task_scsi_status;
2379 /* Automatically padded */
2380 cmd->scsi_sense_length =
2381 (TRANSPORT_SENSE_BUFFER + offset);
2382
Andy Grover6708bb22011-06-08 10:36:43 -07002383 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002384 " and sense\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002385 dev->se_hba->hba_id, dev->transport->name,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002386 cmd->scsi_status);
2387 return 0;
2388 }
Andy Grovera1d8b492011-05-02 17:12:10 -07002389 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002390
2391 return -1;
2392}
2393
Andy Groverec98f782011-07-20 19:28:46 +00002394static inline long long transport_dev_end_lba(struct se_device *dev)
2395{
2396 return dev->transport->get_blocks(dev) + 1;
2397}
2398
2399static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2400{
2401 struct se_device *dev = cmd->se_dev;
2402 u32 sectors;
2403
2404 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2405 return 0;
2406
2407 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2408
Andy Grover6708bb22011-06-08 10:36:43 -07002409 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2410 pr_err("LBA: %llu Sectors: %u exceeds"
Andy Groverec98f782011-07-20 19:28:46 +00002411 " transport_dev_end_lba(): %llu\n",
2412 cmd->t_task_lba, sectors,
2413 transport_dev_end_lba(dev));
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002414 return -EINVAL;
Andy Groverec98f782011-07-20 19:28:46 +00002415 }
2416
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002417 return 0;
Andy Groverec98f782011-07-20 19:28:46 +00002418}
2419
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002420static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2421{
2422 /*
2423 * Determine if the received WRITE_SAME is used to for direct
2424 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2425 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2426 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2427 */
2428 int passthrough = (dev->transport->transport_type ==
2429 TRANSPORT_PLUGIN_PHBA_PDEV);
2430
2431 if (!passthrough) {
2432 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2433 pr_err("WRITE_SAME PBDATA and LBDATA"
2434 " bits not supported for Block Discard"
2435 " Emulation\n");
2436 return -ENOSYS;
2437 }
2438 /*
2439 * Currently for the emulated case we only accept
2440 * tpws with the UNMAP=1 bit set.
2441 */
2442 if (!(flags[0] & 0x08)) {
2443 pr_err("WRITE_SAME w/o UNMAP bit not"
2444 " supported for Block Discard Emulation\n");
2445 return -ENOSYS;
2446 }
2447 }
2448
2449 return 0;
2450}
2451
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002452/* transport_generic_cmd_sequencer():
2453 *
2454 * Generic Command Sequencer that should work for most DAS transport
2455 * drivers.
2456 *
2457 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2458 * RX Thread.
2459 *
2460 * FIXME: Need to support other SCSI OPCODES where as well.
2461 */
2462static int transport_generic_cmd_sequencer(
2463 struct se_cmd *cmd,
2464 unsigned char *cdb)
2465{
Andy Grover5951146d2011-07-19 10:26:37 +00002466 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002467 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2468 int ret = 0, sector_ret = 0, passthrough;
2469 u32 sectors = 0, size = 0, pr_reg_type = 0;
2470 u16 service_action;
2471 u8 alua_ascq = 0;
2472 /*
2473 * Check for an existing UNIT ATTENTION condition
2474 */
2475 if (core_scsi3_ua_check(cmd, cdb) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002476 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2477 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
Andy Grover5951146d2011-07-19 10:26:37 +00002478 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002479 }
2480 /*
2481 * Check status of Asymmetric Logical Unit Assignment port
2482 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002483 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002484 if (ret != 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002485 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002486 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002487 * The ALUA additional sense code qualifier (ASCQ) is determined
2488 * by the ALUA primary or secondary access state..
2489 */
2490 if (ret > 0) {
2491#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07002492 pr_debug("[%s]: ALUA TG Port not available,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002493 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002494 cmd->se_tfo->get_fabric_name(), alua_ascq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002495#endif
2496 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2497 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2498 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
Andy Grover5951146d2011-07-19 10:26:37 +00002499 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002500 }
2501 goto out_invalid_cdb_field;
2502 }
2503 /*
2504 * Check status for SPC-3 Persistent Reservations
2505 */
Andy Grovere3d6f902011-07-19 08:55:10 +00002506 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2507 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07002508 cmd, cdb, pr_reg_type) != 0) {
2509 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2510 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2511 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2512 return -EBUSY;
2513 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002514 /*
2515 * This means the CDB is allowed for the SCSI Initiator port
2516 * when said port is *NOT* holding the legacy SPC-2 or
2517 * SPC-3 Persistent Reservation.
2518 */
2519 }
2520
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002521 /*
2522 * If we operate in passthrough mode we skip most CDB emulation and
2523 * instead hand the commands down to the physical SCSI device.
2524 */
2525 passthrough =
2526 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2527
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002528 switch (cdb[0]) {
2529 case READ_6:
2530 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2531 if (sector_ret)
2532 goto out_unsupported_cdb;
2533 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002534 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002535 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2536 break;
2537 case READ_10:
2538 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2539 if (sector_ret)
2540 goto out_unsupported_cdb;
2541 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002542 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002543 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2544 break;
2545 case READ_12:
2546 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2547 if (sector_ret)
2548 goto out_unsupported_cdb;
2549 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002550 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002551 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2552 break;
2553 case READ_16:
2554 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2555 if (sector_ret)
2556 goto out_unsupported_cdb;
2557 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002558 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002559 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2560 break;
2561 case WRITE_6:
2562 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2563 if (sector_ret)
2564 goto out_unsupported_cdb;
2565 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002566 cmd->t_task_lba = transport_lba_21(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002567 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2568 break;
2569 case WRITE_10:
2570 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2571 if (sector_ret)
2572 goto out_unsupported_cdb;
2573 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002574 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002575 if (cdb[1] & 0x8)
2576 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002577 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2578 break;
2579 case WRITE_12:
2580 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2581 if (sector_ret)
2582 goto out_unsupported_cdb;
2583 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002584 cmd->t_task_lba = transport_lba_32(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002585 if (cdb[1] & 0x8)
2586 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002587 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2588 break;
2589 case WRITE_16:
2590 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2591 if (sector_ret)
2592 goto out_unsupported_cdb;
2593 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002594 cmd->t_task_lba = transport_lba_64(cdb);
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002595 if (cdb[1] & 0x8)
2596 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002597 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2598 break;
2599 case XDWRITEREAD_10:
2600 if ((cmd->data_direction != DMA_TO_DEVICE) ||
Christoph Hellwig33c3faf2011-11-14 11:36:30 -05002601 !(cmd->se_cmd_flags & SCF_BIDI))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002602 goto out_invalid_cdb_field;
2603 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2604 if (sector_ret)
2605 goto out_unsupported_cdb;
2606 size = transport_get_size(sectors, cdb, cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07002607 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002608 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002609
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002610 /*
2611 * Do now allow BIDI commands for passthrough mode.
2612 */
2613 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002614 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002615
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002616 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002617 * Setup BIDI XOR callback to be run after I/O completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002618 */
2619 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002620 if (cdb[1] & 0x8)
2621 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002622 break;
2623 case VARIABLE_LENGTH_CMD:
2624 service_action = get_unaligned_be16(&cdb[8]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002625 switch (service_action) {
2626 case XDWRITEREAD_32:
2627 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2628 if (sector_ret)
2629 goto out_unsupported_cdb;
2630 size = transport_get_size(sectors, cdb, cmd);
2631 /*
2632 * Use WRITE_32 and READ_32 opcodes for the emulated
2633 * XDWRITE_READ_32 logic.
2634 */
Andy Grovera1d8b492011-05-02 17:12:10 -07002635 cmd->t_task_lba = transport_lba_64_ext(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002636 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2637
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002638 /*
2639 * Do now allow BIDI commands for passthrough mode.
2640 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002641 if (passthrough)
Christoph Hellwig7c1c6af2011-10-18 06:57:00 -04002642 goto out_unsupported_cdb;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002643
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002644 /*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04002645 * Setup BIDI XOR callback to be run during after I/O
2646 * completion.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002647 */
2648 cmd->transport_complete_callback = &transport_xor_callback;
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -05002649 if (cdb[1] & 0x8)
2650 cmd->se_cmd_flags |= SCF_FUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002651 break;
2652 case WRITE_SAME_32:
2653 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2654 if (sector_ret)
2655 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002656
Andy Grover6708bb22011-06-08 10:36:43 -07002657 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002658 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002659 else {
2660 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2661 " supported\n");
2662 goto out_invalid_cdb_field;
2663 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002664
Andy Grovera1d8b492011-05-02 17:12:10 -07002665 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002666 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2667
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002668 if (target_check_write_same_discard(&cdb[10], dev) < 0)
2669 goto out_invalid_cdb_field;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002670 if (!passthrough)
2671 cmd->execute_task = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002672 break;
2673 default:
Andy Grover6708bb22011-06-08 10:36:43 -07002674 pr_err("VARIABLE_LENGTH_CMD service action"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002675 " 0x%04x not supported\n", service_action);
2676 goto out_unsupported_cdb;
2677 }
2678 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002679 case MAINTENANCE_IN:
Andy Grovere3d6f902011-07-19 08:55:10 +00002680 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002681 /* MAINTENANCE_IN from SCC-2 */
2682 /*
2683 * Check for emulated MI_REPORT_TARGET_PGS.
2684 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002685 if (cdb[1] == MI_REPORT_TARGET_PGS &&
2686 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2687 cmd->execute_task =
2688 target_emulate_report_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002689 }
2690 size = (cdb[6] << 24) | (cdb[7] << 16) |
2691 (cdb[8] << 8) | cdb[9];
2692 } else {
2693 /* GPCMD_SEND_KEY from multi media commands */
2694 size = (cdb[8] << 8) + cdb[9];
2695 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002696 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002697 break;
2698 case MODE_SELECT:
2699 size = cdb[4];
2700 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2701 break;
2702 case MODE_SELECT_10:
2703 size = (cdb[7] << 8) + cdb[8];
2704 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2705 break;
2706 case MODE_SENSE:
2707 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002708 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002709 if (!passthrough)
2710 cmd->execute_task = target_emulate_modesense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002711 break;
2712 case MODE_SENSE_10:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002713 size = (cdb[7] << 8) + cdb[8];
2714 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2715 if (!passthrough)
2716 cmd->execute_task = target_emulate_modesense;
2717 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002718 case GPCMD_READ_BUFFER_CAPACITY:
2719 case GPCMD_SEND_OPC:
2720 case LOG_SELECT:
2721 case LOG_SENSE:
2722 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002723 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002724 break;
2725 case READ_BLOCK_LIMITS:
2726 size = READ_BLOCK_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002727 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002728 break;
2729 case GPCMD_GET_CONFIGURATION:
2730 case GPCMD_READ_FORMAT_CAPACITIES:
2731 case GPCMD_READ_DISC_INFO:
2732 case GPCMD_READ_TRACK_RZONE_INFO:
2733 size = (cdb[7] << 8) + cdb[8];
2734 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2735 break;
2736 case PERSISTENT_RESERVE_IN:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002737 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002738 cmd->execute_task = target_scsi3_emulate_pr_in;
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002739 size = (cdb[7] << 8) + cdb[8];
2740 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2741 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002742 case PERSISTENT_RESERVE_OUT:
Christoph Hellwig617c0e02011-11-03 17:50:41 -04002743 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002744 cmd->execute_task = target_scsi3_emulate_pr_out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002745 size = (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002746 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002747 break;
2748 case GPCMD_MECHANISM_STATUS:
2749 case GPCMD_READ_DVD_STRUCTURE:
2750 size = (cdb[8] << 8) + cdb[9];
2751 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2752 break;
2753 case READ_POSITION:
2754 size = READ_POSITION_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002755 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002756 break;
Nicholas Bellingere434f1f12011-02-21 07:49:26 +00002757 case MAINTENANCE_OUT:
Andy Grovere3d6f902011-07-19 08:55:10 +00002758 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002759 /* MAINTENANCE_OUT from SCC-2
2760 *
2761 * Check for emulated MO_SET_TARGET_PGS.
2762 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002763 if (cdb[1] == MO_SET_TARGET_PGS &&
2764 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2765 cmd->execute_task =
2766 target_emulate_set_target_port_groups;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002767 }
2768
2769 size = (cdb[6] << 24) | (cdb[7] << 16) |
2770 (cdb[8] << 8) | cdb[9];
2771 } else {
2772 /* GPCMD_REPORT_KEY from multi media commands */
2773 size = (cdb[8] << 8) + cdb[9];
2774 }
Andy Grover05d1c7c2011-07-20 19:13:28 +00002775 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002776 break;
2777 case INQUIRY:
2778 size = (cdb[3] << 8) + cdb[4];
2779 /*
2780 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2781 * See spc4r17 section 5.3
2782 */
Andy Grover5951146d2011-07-19 10:26:37 +00002783 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07002784 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002785 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002786 if (!passthrough)
2787 cmd->execute_task = target_emulate_inquiry;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002788 break;
2789 case READ_BUFFER:
2790 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002791 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002792 break;
2793 case READ_CAPACITY:
2794 size = READ_CAP_LEN;
Andy Grover05d1c7c2011-07-20 19:13:28 +00002795 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002796 if (!passthrough)
2797 cmd->execute_task = target_emulate_readcapacity;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002798 break;
2799 case READ_MEDIA_SERIAL_NUMBER:
2800 case SECURITY_PROTOCOL_IN:
2801 case SECURITY_PROTOCOL_OUT:
2802 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002803 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002804 break;
2805 case SERVICE_ACTION_IN:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002806 switch (cmd->t_task_cdb[1] & 0x1f) {
2807 case SAI_READ_CAPACITY_16:
2808 if (!passthrough)
2809 cmd->execute_task =
2810 target_emulate_readcapacity_16;
2811 break;
2812 default:
2813 if (passthrough)
2814 break;
2815
2816 pr_err("Unsupported SA: 0x%02x\n",
2817 cmd->t_task_cdb[1] & 0x1f);
2818 goto out_unsupported_cdb;
2819 }
2820 /*FALLTHROUGH*/
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002821 case ACCESS_CONTROL_IN:
2822 case ACCESS_CONTROL_OUT:
2823 case EXTENDED_COPY:
2824 case READ_ATTRIBUTE:
2825 case RECEIVE_COPY_RESULTS:
2826 case WRITE_ATTRIBUTE:
2827 size = (cdb[10] << 24) | (cdb[11] << 16) |
2828 (cdb[12] << 8) | cdb[13];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002829 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002830 break;
2831 case RECEIVE_DIAGNOSTIC:
2832 case SEND_DIAGNOSTIC:
2833 size = (cdb[3] << 8) | cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002834 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002835 break;
2836/* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2837#if 0
2838 case GPCMD_READ_CD:
2839 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2840 size = (2336 * sectors);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002841 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002842 break;
2843#endif
2844 case READ_TOC:
2845 size = cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002846 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002847 break;
2848 case REQUEST_SENSE:
2849 size = cdb[4];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002850 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002851 if (!passthrough)
2852 cmd->execute_task = target_emulate_request_sense;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002853 break;
2854 case READ_ELEMENT_STATUS:
2855 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002856 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002857 break;
2858 case WRITE_BUFFER:
2859 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
Andy Grover05d1c7c2011-07-20 19:13:28 +00002860 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002861 break;
2862 case RESERVE:
2863 case RESERVE_10:
2864 /*
2865 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2866 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2867 */
2868 if (cdb[0] == RESERVE_10)
2869 size = (cdb[7] << 8) | cdb[8];
2870 else
2871 size = cmd->data_length;
2872
2873 /*
2874 * Setup the legacy emulated handler for SPC-2 and
2875 * >= SPC-3 compatible reservation handling (CRH=1)
2876 * Otherwise, we assume the underlying SCSI logic is
2877 * is running in SPC_PASSTHROUGH, and wants reservations
2878 * emulation disabled.
2879 */
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002880 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2881 cmd->execute_task = target_scsi2_reservation_reserve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002882 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2883 break;
2884 case RELEASE:
2885 case RELEASE_10:
2886 /*
2887 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2888 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2889 */
2890 if (cdb[0] == RELEASE_10)
2891 size = (cdb[7] << 8) | cdb[8];
2892 else
2893 size = cmd->data_length;
2894
Christoph Hellwige76a35d2011-11-03 17:50:42 -04002895 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2896 cmd->execute_task = target_scsi2_reservation_release;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002897 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2898 break;
2899 case SYNCHRONIZE_CACHE:
2900 case 0x91: /* SYNCHRONIZE_CACHE_16: */
2901 /*
2902 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2903 */
2904 if (cdb[0] == SYNCHRONIZE_CACHE) {
2905 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002906 cmd->t_task_lba = transport_lba_32(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002907 } else {
2908 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
Andy Grovera1d8b492011-05-02 17:12:10 -07002909 cmd->t_task_lba = transport_lba_64(cdb);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002910 }
2911 if (sector_ret)
2912 goto out_unsupported_cdb;
2913
2914 size = transport_get_size(sectors, cdb, cmd);
2915 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2916
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002917 if (passthrough)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002918 break;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002919
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002920 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002921 * Check to ensure that LBA + Range does not exceed past end of
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002922 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002923 */
Nicholas Bellinger7abbe7f32011-08-10 18:41:14 -07002924 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
2925 if (transport_cmd_get_valid_sectors(cmd) < 0)
2926 goto out_invalid_cdb_field;
2927 }
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002928 cmd->execute_task = target_emulate_synchronize_cache;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002929 break;
2930 case UNMAP:
2931 size = get_unaligned_be16(&cdb[7]);
Andy Grover05d1c7c2011-07-20 19:13:28 +00002932 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002933 if (!passthrough)
2934 cmd->execute_task = target_emulate_unmap;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002935 break;
2936 case WRITE_SAME_16:
2937 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2938 if (sector_ret)
2939 goto out_unsupported_cdb;
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002940
Andy Grover6708bb22011-06-08 10:36:43 -07002941 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002942 size = transport_get_size(1, cdb, cmd);
Andy Grover6708bb22011-06-08 10:36:43 -07002943 else {
2944 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2945 goto out_invalid_cdb_field;
2946 }
Nicholas Bellingerdd3a5ad2011-05-06 17:55:35 -07002947
Nicholas Bellinger5db07532011-07-27 22:18:52 -07002948 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002949 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002950
2951 if (target_check_write_same_discard(&cdb[1], dev) < 0)
2952 goto out_invalid_cdb_field;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002953 if (!passthrough)
2954 cmd->execute_task = target_emulate_write_same;
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002955 break;
2956 case WRITE_SAME:
2957 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2958 if (sector_ret)
2959 goto out_unsupported_cdb;
2960
2961 if (sectors)
Nicholas Bellinger12850622011-08-08 19:08:23 -07002962 size = transport_get_size(1, cdb, cmd);
Nicholas Bellinger706d5862011-07-28 00:07:03 -07002963 else {
2964 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2965 goto out_invalid_cdb_field;
2966 }
2967
2968 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
2969 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2970 /*
2971 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
2972 * of byte 1 bit 3 UNMAP instead of original reserved field
2973 */
2974 if (target_check_write_same_discard(&cdb[1], dev) < 0)
2975 goto out_invalid_cdb_field;
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002976 if (!passthrough)
2977 cmd->execute_task = target_emulate_write_same;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002978 break;
2979 case ALLOW_MEDIUM_REMOVAL:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002980 case ERASE:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002981 case REZERO_UNIT:
2982 case SEEK_10:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002983 case SPACE:
2984 case START_STOP:
2985 case TEST_UNIT_READY:
2986 case VERIFY:
2987 case WRITE_FILEMARKS:
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04002988 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2989 if (!passthrough)
2990 cmd->execute_task = target_emulate_noop;
2991 break;
2992 case GPCMD_CLOSE_TRACK:
2993 case INITIALIZE_ELEMENT_STATUS:
2994 case GPCMD_LOAD_UNLOAD:
2995 case GPCMD_SET_SPEED:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002996 case MOVE_MEDIUM:
2997 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2998 break;
2999 case REPORT_LUNS:
Christoph Hellwige76a35d2011-11-03 17:50:42 -04003000 cmd->execute_task = target_report_luns;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003001 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3002 /*
3003 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3004 * See spc4r17 section 5.3
3005 */
Andy Grover5951146d2011-07-19 10:26:37 +00003006 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003007 cmd->sam_task_attr = MSG_HEAD_TAG;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003008 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003009 break;
3010 default:
Andy Grover6708bb22011-06-08 10:36:43 -07003011 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003012 " 0x%02x, sending CHECK_CONDITION.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00003013 cmd->se_tfo->get_fabric_name(), cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003014 goto out_unsupported_cdb;
3015 }
3016
3017 if (size != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -07003018 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003019 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
Andy Grovere3d6f902011-07-19 08:55:10 +00003020 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003021 cmd->data_length, size, cdb[0]);
3022
3023 cmd->cmd_spdtl = size;
3024
3025 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grover6708bb22011-06-08 10:36:43 -07003026 pr_err("Rejecting underflow/overflow"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003027 " WRITE data\n");
3028 goto out_invalid_cdb_field;
3029 }
3030 /*
3031 * Reject READ_* or WRITE_* with overflow/underflow for
3032 * type SCF_SCSI_DATA_SG_IO_CDB.
3033 */
Andy Grover6708bb22011-06-08 10:36:43 -07003034 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3035 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003036 " CDB on non 512-byte sector setup subsystem"
Andy Grovere3d6f902011-07-19 08:55:10 +00003037 " plugin: %s\n", dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003038 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3039 goto out_invalid_cdb_field;
3040 }
3041
3042 if (size > cmd->data_length) {
3043 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3044 cmd->residual_count = (size - cmd->data_length);
3045 } else {
3046 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3047 cmd->residual_count = (cmd->data_length - size);
3048 }
3049 cmd->data_length = size;
3050 }
3051
Christoph Hellwig5bda90c2011-11-03 17:50:45 -04003052 /* reject any command that we don't have a handler for */
3053 if (!(passthrough || cmd->execute_task ||
3054 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3055 goto out_unsupported_cdb;
3056
Andy Groverd0229ae2011-07-08 17:04:53 -07003057 /* Let's limit control cdbs to a page, for simplicity's sake. */
3058 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3059 size > PAGE_SIZE)
3060 goto out_invalid_cdb_field;
3061
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003062 transport_set_supported_SAM_opcode(cmd);
3063 return ret;
3064
3065out_unsupported_cdb:
3066 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3067 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover5951146d2011-07-19 10:26:37 +00003068 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003069out_invalid_cdb_field:
3070 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3071 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Andy Grover5951146d2011-07-19 10:26:37 +00003072 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003073}
3074
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003075/*
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003076 * Called from I/O completion to determine which dormant/delayed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003077 * and ordered cmds need to have their tasks added to the execution queue.
3078 */
3079static void transport_complete_task_attr(struct se_cmd *cmd)
3080{
Andy Grover5951146d2011-07-19 10:26:37 +00003081 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003082 struct se_cmd *cmd_p, *cmd_tmp;
3083 int new_active_tasks = 0;
3084
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003085 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003086 atomic_dec(&dev->simple_cmds);
3087 smp_mb__after_atomic_dec();
3088 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003089 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003090 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3091 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003092 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003093 atomic_dec(&dev->dev_hoq_count);
3094 smp_mb__after_atomic_dec();
3095 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003096 pr_debug("Incremented dev_cur_ordered_id: %u for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003097 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3098 cmd->se_ordered_id);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003099 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003100 atomic_dec(&dev->dev_ordered_sync);
3101 smp_mb__after_atomic_dec();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003102
3103 dev->dev_cur_ordered_id++;
Andy Grover6708bb22011-06-08 10:36:43 -07003104 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003105 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3106 }
3107 /*
3108 * Process all commands up to the last received
3109 * ORDERED task attribute which requires another blocking
3110 * boundary
3111 */
3112 spin_lock(&dev->delayed_cmd_lock);
3113 list_for_each_entry_safe(cmd_p, cmd_tmp,
Andy Grover5951146d2011-07-19 10:26:37 +00003114 &dev->delayed_cmd_list, se_delayed_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003115
Andy Grover5951146d2011-07-19 10:26:37 +00003116 list_del(&cmd_p->se_delayed_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003117 spin_unlock(&dev->delayed_cmd_lock);
3118
Andy Grover6708bb22011-06-08 10:36:43 -07003119 pr_debug("Calling add_tasks() for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003120 " cmd_p: 0x%02x Task Attr: 0x%02x"
3121 " Dormant -> Active, se_ordered_id: %u\n",
Andy Grover6708bb22011-06-08 10:36:43 -07003122 cmd_p->t_task_cdb[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003123 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3124
3125 transport_add_tasks_from_cmd(cmd_p);
3126 new_active_tasks++;
3127
3128 spin_lock(&dev->delayed_cmd_lock);
Nicholas Bellingere66ecd52011-05-19 20:19:14 -07003129 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003130 break;
3131 }
3132 spin_unlock(&dev->delayed_cmd_lock);
3133 /*
3134 * If new tasks have become active, wake up the transport thread
3135 * to do the processing of the Active tasks.
3136 */
3137 if (new_active_tasks != 0)
Andy Grovere3d6f902011-07-19 08:55:10 +00003138 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003139}
3140
Christoph Hellwige057f532011-10-17 13:56:41 -04003141static void transport_complete_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003142{
3143 int ret = 0;
3144
Christoph Hellwige057f532011-10-17 13:56:41 -04003145 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3146 transport_complete_task_attr(cmd);
3147
3148 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3149 ret = cmd->se_tfo->queue_status(cmd);
3150 if (ret)
3151 goto out;
3152 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003153
3154 switch (cmd->data_direction) {
3155 case DMA_FROM_DEVICE:
3156 ret = cmd->se_tfo->queue_data_in(cmd);
3157 break;
3158 case DMA_TO_DEVICE:
Andy Groverec98f782011-07-20 19:28:46 +00003159 if (cmd->t_bidi_data_sg) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003160 ret = cmd->se_tfo->queue_data_in(cmd);
3161 if (ret < 0)
Christoph Hellwige057f532011-10-17 13:56:41 -04003162 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003163 }
3164 /* Fall through for DMA_TO_DEVICE */
3165 case DMA_NONE:
3166 ret = cmd->se_tfo->queue_status(cmd);
3167 break;
3168 default:
3169 break;
3170 }
3171
Christoph Hellwige057f532011-10-17 13:56:41 -04003172out:
3173 if (ret < 0) {
3174 transport_handle_queue_full(cmd, cmd->se_dev);
3175 return;
3176 }
3177 transport_lun_remove_cmd(cmd);
3178 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003179}
3180
3181static void transport_handle_queue_full(
3182 struct se_cmd *cmd,
Christoph Hellwige057f532011-10-17 13:56:41 -04003183 struct se_device *dev)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003184{
3185 spin_lock_irq(&dev->qf_cmd_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003186 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3187 atomic_inc(&dev->dev_qf_count);
3188 smp_mb__after_atomic_inc();
3189 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3190
3191 schedule_work(&cmd->se_dev->qf_work_queue);
3192}
3193
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003194static void target_complete_ok_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003195{
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003196 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003197 int reason = 0, ret;
Christoph Hellwig35e0e752011-10-17 13:56:53 -04003198
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003199 /*
3200 * Check if we need to move delayed/dormant tasks from cmds on the
3201 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3202 * Attribute.
3203 */
Andy Grover5951146d2011-07-19 10:26:37 +00003204 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003205 transport_complete_task_attr(cmd);
3206 /*
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003207 * Check to schedule QUEUE_FULL work, or execute an existing
3208 * cmd->transport_qf_callback()
3209 */
3210 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3211 schedule_work(&cmd->se_dev->qf_work_queue);
3212
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003213 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003214 * Check if we need to retrieve a sense buffer from
3215 * the struct se_cmd in question.
3216 */
3217 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3218 if (transport_get_sense_data(cmd) < 0)
3219 reason = TCM_NON_EXISTENT_LUN;
3220
3221 /*
3222 * Only set when an struct se_task->task_scsi_status returned
3223 * a non GOOD status.
3224 */
3225 if (cmd->scsi_status) {
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003226 ret = transport_send_check_condition_and_sense(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003227 cmd, reason, 1);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003228 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003229 goto queue_full;
3230
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003231 transport_lun_remove_cmd(cmd);
3232 transport_cmd_check_stop_to_fabric(cmd);
3233 return;
3234 }
3235 }
3236 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003237 * Check for a callback, used by amongst other things
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003238 * XDWRITE_READ_10 emulation.
3239 */
3240 if (cmd->transport_complete_callback)
3241 cmd->transport_complete_callback(cmd);
3242
3243 switch (cmd->data_direction) {
3244 case DMA_FROM_DEVICE:
3245 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003246 if (cmd->se_lun->lun_sep) {
3247 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003248 cmd->data_length;
3249 }
3250 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003251
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003252 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003253 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003254 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003255 break;
3256 case DMA_TO_DEVICE:
3257 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003258 if (cmd->se_lun->lun_sep) {
3259 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003260 cmd->data_length;
3261 }
3262 spin_unlock(&cmd->se_lun->lun_sep_lock);
3263 /*
3264 * Check if we need to send READ payload for BIDI-COMMAND
3265 */
Andy Groverec98f782011-07-20 19:28:46 +00003266 if (cmd->t_bidi_data_sg) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003267 spin_lock(&cmd->se_lun->lun_sep_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00003268 if (cmd->se_lun->lun_sep) {
3269 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003270 cmd->data_length;
3271 }
3272 spin_unlock(&cmd->se_lun->lun_sep_lock);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003273 ret = cmd->se_tfo->queue_data_in(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003274 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003275 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003276 break;
3277 }
3278 /* Fall through for DMA_TO_DEVICE */
3279 case DMA_NONE:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003280 ret = cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003281 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003282 goto queue_full;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003283 break;
3284 default:
3285 break;
3286 }
3287
3288 transport_lun_remove_cmd(cmd);
3289 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003290 return;
3291
3292queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003293 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003294 " data_direction: %d\n", cmd, cmd->data_direction);
Christoph Hellwige057f532011-10-17 13:56:41 -04003295 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3296 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003297}
3298
3299static void transport_free_dev_tasks(struct se_cmd *cmd)
3300{
3301 struct se_task *task, *task_tmp;
3302 unsigned long flags;
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003303 LIST_HEAD(dispose_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003304
Andy Grovera1d8b492011-05-02 17:12:10 -07003305 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003306 list_for_each_entry_safe(task, task_tmp,
Andy Grovera1d8b492011-05-02 17:12:10 -07003307 &cmd->t_task_list, t_list) {
Christoph Hellwig0c2cfe52011-10-17 13:56:45 -04003308 if (!(task->task_flags & TF_ACTIVE))
3309 list_move_tail(&task->t_list, &dispose_list);
3310 }
3311 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3312
3313 while (!list_empty(&dispose_list)) {
3314 task = list_first_entry(&dispose_list, struct se_task, t_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003315
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003316 if (task->task_sg != cmd->t_data_sg &&
3317 task->task_sg != cmd->t_bidi_data_sg)
3318 kfree(task->task_sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003319
3320 list_del(&task->t_list);
3321
Christoph Hellwig42bf8292011-10-12 11:07:00 -04003322 cmd->se_dev->transport->free_task(task);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003323 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003324}
3325
Andy Grover6708bb22011-06-08 10:36:43 -07003326static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003327{
Andy Groverec98f782011-07-20 19:28:46 +00003328 struct scatterlist *sg;
Andy Groverec98f782011-07-20 19:28:46 +00003329 int count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003330
Andy Grover6708bb22011-06-08 10:36:43 -07003331 for_each_sg(sgl, sg, nents, count)
3332 __free_page(sg_page(sg));
3333
3334 kfree(sgl);
3335}
3336
3337static inline void transport_free_pages(struct se_cmd *cmd)
3338{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003339 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
Andy Grover6708bb22011-06-08 10:36:43 -07003340 return;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003341
Andy Grover6708bb22011-06-08 10:36:43 -07003342 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003343 cmd->t_data_sg = NULL;
3344 cmd->t_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003345
Andy Grover6708bb22011-06-08 10:36:43 -07003346 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
Andy Groverec98f782011-07-20 19:28:46 +00003347 cmd->t_bidi_data_sg = NULL;
3348 cmd->t_bidi_data_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003349}
3350
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003351/**
3352 * transport_put_cmd - release a reference to a command
3353 * @cmd: command to release
3354 *
3355 * This routine releases our reference to the command and frees it if possible.
3356 */
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003357static void transport_put_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003358{
3359 unsigned long flags;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003360 int free_tasks = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003361
Andy Grovera1d8b492011-05-02 17:12:10 -07003362 spin_lock_irqsave(&cmd->t_state_lock, flags);
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003363 if (atomic_read(&cmd->t_fe_count)) {
3364 if (!atomic_dec_and_test(&cmd->t_fe_count))
3365 goto out_busy;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003366 }
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003367
3368 if (atomic_read(&cmd->t_se_count)) {
3369 if (!atomic_dec_and_test(&cmd->t_se_count))
3370 goto out_busy;
3371 }
3372
3373 if (atomic_read(&cmd->transport_dev_active)) {
3374 atomic_set(&cmd->transport_dev_active, 0);
3375 transport_all_task_dev_remove_state(cmd);
3376 free_tasks = 1;
3377 }
Andy Grovera1d8b492011-05-02 17:12:10 -07003378 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003379
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003380 if (free_tasks != 0)
3381 transport_free_dev_tasks(cmd);
Christoph Hellwigd3df7822011-09-13 23:08:32 +02003382
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003383 transport_free_pages(cmd);
Christoph Hellwig31afc392011-09-13 23:08:11 +02003384 transport_release_cmd(cmd);
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003385 return;
Christoph Hellwig4911e3c2011-09-13 23:08:42 +02003386out_busy:
3387 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003388}
3389
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003390/*
Andy Groverec98f782011-07-20 19:28:46 +00003391 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3392 * allocating in the core.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003393 * @cmd: Associated se_cmd descriptor
3394 * @mem: SGL style memory for TCM WRITE / READ
3395 * @sg_mem_num: Number of SGL elements
3396 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3397 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3398 *
3399 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3400 * of parameters.
3401 */
3402int transport_generic_map_mem_to_cmd(
3403 struct se_cmd *cmd,
Andy Grover5951146d2011-07-19 10:26:37 +00003404 struct scatterlist *sgl,
3405 u32 sgl_count,
3406 struct scatterlist *sgl_bidi,
3407 u32 sgl_bidi_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003408{
Andy Grover5951146d2011-07-19 10:26:37 +00003409 if (!sgl || !sgl_count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003410 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003411
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003412 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3413 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
Nicholas Bellingerfef58a62011-11-15 22:13:24 -08003414 /*
3415 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3416 * scatterlists already have been set to follow what the fabric
3417 * passes for the original expected data transfer length.
3418 */
3419 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3420 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3421 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3422 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3423 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3424 return -EINVAL;
3425 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003426
Andy Groverec98f782011-07-20 19:28:46 +00003427 cmd->t_data_sg = sgl;
3428 cmd->t_data_nents = sgl_count;
3429
Andy Grover5951146d2011-07-19 10:26:37 +00003430 if (sgl_bidi && sgl_bidi_count) {
Andy Groverec98f782011-07-20 19:28:46 +00003431 cmd->t_bidi_data_sg = sgl_bidi;
3432 cmd->t_bidi_data_nents = sgl_bidi_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003433 }
3434 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003435 }
3436
3437 return 0;
3438}
3439EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3440
Andy Grover05d1c7c2011-07-20 19:13:28 +00003441void *transport_kmap_first_data_page(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003442{
Andy Groverec98f782011-07-20 19:28:46 +00003443 struct scatterlist *sg = cmd->t_data_sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003444
Andy Groverec98f782011-07-20 19:28:46 +00003445 BUG_ON(!sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +00003446 /*
Andy Groverec98f782011-07-20 19:28:46 +00003447 * We need to take into account a possible offset here for fabrics like
3448 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3449 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
Andy Grover05d1c7c2011-07-20 19:13:28 +00003450 */
Andy Groverec98f782011-07-20 19:28:46 +00003451 return kmap(sg_page(sg)) + sg->offset;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003452}
3453EXPORT_SYMBOL(transport_kmap_first_data_page);
3454
3455void transport_kunmap_first_data_page(struct se_cmd *cmd)
3456{
Andy Groverec98f782011-07-20 19:28:46 +00003457 kunmap(sg_page(cmd->t_data_sg));
Andy Grover05d1c7c2011-07-20 19:13:28 +00003458}
3459EXPORT_SYMBOL(transport_kunmap_first_data_page);
3460
3461static int
3462transport_generic_get_mem(struct se_cmd *cmd)
3463{
Andy Groverec98f782011-07-20 19:28:46 +00003464 u32 length = cmd->data_length;
3465 unsigned int nents;
3466 struct page *page;
3467 int i = 0;
Andy Grover05d1c7c2011-07-20 19:13:28 +00003468
Andy Groverec98f782011-07-20 19:28:46 +00003469 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3470 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3471 if (!cmd->t_data_sg)
Andy Grovere3d6f902011-07-19 08:55:10 +00003472 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003473
Andy Groverec98f782011-07-20 19:28:46 +00003474 cmd->t_data_nents = nents;
3475 sg_init_table(cmd->t_data_sg, nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003476
Andy Groverec98f782011-07-20 19:28:46 +00003477 while (length) {
3478 u32 page_len = min_t(u32, length, PAGE_SIZE);
3479 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3480 if (!page)
3481 goto out;
3482
3483 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3484 length -= page_len;
3485 i++;
3486 }
3487 return 0;
3488
3489out:
3490 while (i >= 0) {
3491 __free_page(sg_page(&cmd->t_data_sg[i]));
3492 i--;
3493 }
3494 kfree(cmd->t_data_sg);
3495 cmd->t_data_sg = NULL;
3496 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003497}
3498
Andy Grovera1d8b492011-05-02 17:12:10 -07003499/* Reduce sectors if they are too long for the device */
3500static inline sector_t transport_limit_task_sectors(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003501 struct se_device *dev,
3502 unsigned long long lba,
Andy Grovera1d8b492011-05-02 17:12:10 -07003503 sector_t sectors)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003504{
Andy Grovera1d8b492011-05-02 17:12:10 -07003505 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003506
Andy Grovera1d8b492011-05-02 17:12:10 -07003507 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3508 if ((lba + sectors) > transport_dev_end_lba(dev))
3509 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003510
Andy Grovera1d8b492011-05-02 17:12:10 -07003511 return sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003512}
3513
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003514
3515/*
3516 * This function can be used by HW target mode drivers to create a linked
3517 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3518 * This is intended to be called during the completion path by TCM Core
3519 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3520 */
3521void transport_do_task_sg_chain(struct se_cmd *cmd)
3522{
Andy Groverec98f782011-07-20 19:28:46 +00003523 struct scatterlist *sg_first = NULL;
3524 struct scatterlist *sg_prev = NULL;
3525 int sg_prev_nents = 0;
3526 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003527 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003528 u32 chained_nents = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003529 int i;
3530
Andy Groverec98f782011-07-20 19:28:46 +00003531 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3532
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003533 /*
3534 * Walk the struct se_task list and setup scatterlist chains
Andy Grovera1d8b492011-05-02 17:12:10 -07003535 * for each contiguously allocated struct se_task->task_sg[].
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003536 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003537 list_for_each_entry(task, &cmd->t_task_list, t_list) {
Andy Groverec98f782011-07-20 19:28:46 +00003538 if (!task->task_sg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003539 continue;
3540
Andy Groverec98f782011-07-20 19:28:46 +00003541 if (!sg_first) {
3542 sg_first = task->task_sg;
Andy Grover6708bb22011-06-08 10:36:43 -07003543 chained_nents = task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003544 } else {
Andy Groverec98f782011-07-20 19:28:46 +00003545 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
Andy Grover6708bb22011-06-08 10:36:43 -07003546 chained_nents += task->task_sg_nents;
Nicholas Bellinger97868c82011-05-19 20:19:09 -07003547 }
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003548 /*
3549 * For the padded tasks, use the extra SGL vector allocated
3550 * in transport_allocate_data_tasks() for the sg_prev_nents
Christoph Hellwig04629b72011-10-12 11:07:04 -04003551 * offset into sg_chain() above.
3552 *
3553 * We do not need the padding for the last task (or a single
3554 * task), but in that case we will never use the sg_prev_nents
3555 * value below which would be incorrect.
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003556 */
Christoph Hellwig04629b72011-10-12 11:07:04 -04003557 sg_prev_nents = (task->task_sg_nents + 1);
Andy Groverec98f782011-07-20 19:28:46 +00003558 sg_prev = task->task_sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003559 }
3560 /*
3561 * Setup the starting pointer and total t_tasks_sg_linked_no including
3562 * padding SGs for linking and to mark the end.
3563 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003564 cmd->t_tasks_sg_chained = sg_first;
Andy Groverec98f782011-07-20 19:28:46 +00003565 cmd->t_tasks_sg_chained_no = chained_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003566
Andy Grover6708bb22011-06-08 10:36:43 -07003567 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
Andy Grovera1d8b492011-05-02 17:12:10 -07003568 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3569 cmd->t_tasks_sg_chained_no);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003570
Andy Grovera1d8b492011-05-02 17:12:10 -07003571 for_each_sg(cmd->t_tasks_sg_chained, sg,
3572 cmd->t_tasks_sg_chained_no, i) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003573
Andy Grover6708bb22011-06-08 10:36:43 -07003574 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
Andy Grover5951146d2011-07-19 10:26:37 +00003575 i, sg, sg_page(sg), sg->length, sg->offset);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003576 if (sg_is_chain(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003577 pr_debug("SG: %p sg_is_chain=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003578 if (sg_is_last(sg))
Andy Grover6708bb22011-06-08 10:36:43 -07003579 pr_debug("SG: %p sg_is_last=1\n", sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003580 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003581}
3582EXPORT_SYMBOL(transport_do_task_sg_chain);
3583
Andy Grovera1d8b492011-05-02 17:12:10 -07003584/*
3585 * Break up cmd into chunks transport can handle
3586 */
Christoph Hellwig38b400672011-10-18 06:57:02 -04003587static int
3588transport_allocate_data_tasks(struct se_cmd *cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003589 enum dma_data_direction data_direction,
Christoph Hellwig38b400672011-10-18 06:57:02 -04003590 struct scatterlist *cmd_sg, unsigned int sgl_nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003591{
Andy Grover5951146d2011-07-19 10:26:37 +00003592 struct se_device *dev = cmd->se_dev;
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003593 int task_count, i;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003594 unsigned long long lba;
3595 sector_t sectors, dev_max_sectors;
3596 u32 sector_size;
3597
3598 if (transport_cmd_get_valid_sectors(cmd) < 0)
3599 return -EINVAL;
3600
3601 dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3602 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003603
Andy Groverec98f782011-07-20 19:28:46 +00003604 WARN_ON(cmd->data_length % sector_size);
Christoph Hellwig38b400672011-10-18 06:57:02 -04003605
3606 lba = cmd->t_task_lba;
Andy Groverec98f782011-07-20 19:28:46 +00003607 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
Nicholas Bellinger277c5f22011-07-26 00:38:40 -07003608 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003609
3610 /*
3611 * If we need just a single task reuse the SG list in the command
3612 * and avoid a lot of work.
3613 */
3614 if (task_count == 1) {
3615 struct se_task *task;
3616 unsigned long flags;
3617
3618 task = transport_generic_get_task(cmd, data_direction);
3619 if (!task)
3620 return -ENOMEM;
3621
3622 task->task_sg = cmd_sg;
3623 task->task_sg_nents = sgl_nents;
3624
3625 task->task_lba = lba;
3626 task->task_sectors = sectors;
3627 task->task_size = task->task_sectors * sector_size;
3628
3629 spin_lock_irqsave(&cmd->t_state_lock, flags);
3630 list_add_tail(&task->t_list, &cmd->t_task_list);
3631 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3632
3633 return task_count;
3634 }
3635
Andy Groverec98f782011-07-20 19:28:46 +00003636 for (i = 0; i < task_count; i++) {
Christoph Hellwig38b400672011-10-18 06:57:02 -04003637 struct se_task *task;
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003638 unsigned int task_size, task_sg_nents_padded;
Christoph Hellwig38b400672011-10-18 06:57:02 -04003639 struct scatterlist *sg;
3640 unsigned long flags;
Andy Groverec98f782011-07-20 19:28:46 +00003641 int count;
Andy Grovera1d8b492011-05-02 17:12:10 -07003642
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003643 task = transport_generic_get_task(cmd, data_direction);
Andy Grovera1d8b492011-05-02 17:12:10 -07003644 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003645 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003646
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003647 task->task_lba = lba;
Andy Groverec98f782011-07-20 19:28:46 +00003648 task->task_sectors = min(sectors, dev_max_sectors);
3649 task->task_size = task->task_sectors * sector_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003650
Nicholas Bellinger525a48a2011-08-13 02:11:38 -07003651 /*
3652 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3653 * in order to calculate the number per task SGL entries
3654 */
3655 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003656 /*
Andy Groverec98f782011-07-20 19:28:46 +00003657 * Check if the fabric module driver is requesting that all
3658 * struct se_task->task_sg[] be chained together.. If so,
3659 * then allocate an extra padding SG entry for linking and
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003660 * marking the end of the chained SGL for every task except
3661 * the last one for (task_count > 1) operation, or skipping
3662 * the extra padding for the (task_count == 1) case.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003663 */
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003664 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3665 task_sg_nents_padded = (task->task_sg_nents + 1);
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003666 } else
3667 task_sg_nents_padded = task->task_sg_nents;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003668
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003669 task->task_sg = kmalloc(sizeof(struct scatterlist) *
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003670 task_sg_nents_padded, GFP_KERNEL);
Andy Groverec98f782011-07-20 19:28:46 +00003671 if (!task->task_sg) {
3672 cmd->se_dev->transport->free_task(task);
3673 return -ENOMEM;
3674 }
3675
Nicholas Bellingerc3c74c72011-08-13 05:30:31 -07003676 sg_init_table(task->task_sg, task_sg_nents_padded);
Andy Groverec98f782011-07-20 19:28:46 +00003677
3678 task_size = task->task_size;
3679
3680 /* Build new sgl, only up to task_size */
Andy Grover6708bb22011-06-08 10:36:43 -07003681 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
Andy Groverec98f782011-07-20 19:28:46 +00003682 if (cmd_sg->length > task_size)
3683 break;
3684
3685 *sg = *cmd_sg;
3686 task_size -= cmd_sg->length;
3687 cmd_sg = sg_next(cmd_sg);
3688 }
3689
3690 lba += task->task_sectors;
3691 sectors -= task->task_sectors;
3692
3693 spin_lock_irqsave(&cmd->t_state_lock, flags);
3694 list_add_tail(&task->t_list, &cmd->t_task_list);
3695 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003696 }
3697
Andy Groverec98f782011-07-20 19:28:46 +00003698 return task_count;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003699}
3700
3701static int
Andy Groverec98f782011-07-20 19:28:46 +00003702transport_allocate_control_task(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003703{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003704 struct se_task *task;
Andy Groverec98f782011-07-20 19:28:46 +00003705 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003706
3707 task = transport_generic_get_task(cmd, cmd->data_direction);
3708 if (!task)
Andy Groverec98f782011-07-20 19:28:46 +00003709 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003710
Christoph Hellwigaf3f00c2011-10-18 06:57:03 -04003711 task->task_sg = cmd->t_data_sg;
Andy Groverec98f782011-07-20 19:28:46 +00003712 task->task_size = cmd->data_length;
Andy Grover6708bb22011-06-08 10:36:43 -07003713 task->task_sg_nents = cmd->t_data_nents;
Andy Groverec98f782011-07-20 19:28:46 +00003714
3715 spin_lock_irqsave(&cmd->t_state_lock, flags);
3716 list_add_tail(&task->t_list, &cmd->t_task_list);
3717 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003718
Andy Grover6708bb22011-06-08 10:36:43 -07003719 /* Success! Return number of tasks allocated */
Christoph Hellwiga3eedc22011-09-25 14:56:43 -04003720 return 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003721}
3722
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003723/*
3724 * Allocate any required ressources to execute the command, and either place
3725 * it on the execution queue if possible. For writes we might not have the
3726 * payload yet, thus notify the fabric via a call to ->write_pending instead.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003727 */
Andy Grovera1d8b492011-05-02 17:12:10 -07003728int transport_generic_new_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003729{
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003730 struct se_device *dev = cmd->se_dev;
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003731 int task_cdbs, task_cdbs_bidi = 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003732 int set_counts = 1;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003733 int ret = 0;
3734
3735 /*
3736 * Determine is the TCM fabric module has already allocated physical
3737 * memory, and is directly calling transport_generic_map_mem_to_cmd()
Andy Groverec98f782011-07-20 19:28:46 +00003738 * beforehand.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003739 */
Andy Groverec98f782011-07-20 19:28:46 +00003740 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3741 cmd->data_length) {
Andy Grover05d1c7c2011-07-20 19:13:28 +00003742 ret = transport_generic_get_mem(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003743 if (ret < 0)
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003744 goto out_fail;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003745 }
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003746
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003747 /*
Christoph Hellwig38b400672011-10-18 06:57:02 -04003748 * For BIDI command set up the read tasks first.
Nicholas Bellinger1d20bb62011-07-21 04:41:48 +00003749 */
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003750 if (cmd->t_bidi_data_sg &&
Christoph Hellwig38b400672011-10-18 06:57:02 -04003751 dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
3752 BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
3753
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003754 task_cdbs_bidi = transport_allocate_data_tasks(cmd,
3755 DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
3756 cmd->t_bidi_data_nents);
3757 if (task_cdbs_bidi <= 0)
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003758 goto out_fail;
3759
3760 atomic_inc(&cmd->t_fe_count);
3761 atomic_inc(&cmd->t_se_count);
3762 set_counts = 0;
3763 }
Christoph Hellwig38b400672011-10-18 06:57:02 -04003764
3765 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3766 task_cdbs = transport_allocate_data_tasks(cmd,
3767 cmd->data_direction, cmd->t_data_sg,
3768 cmd->t_data_nents);
3769 } else {
3770 task_cdbs = transport_allocate_control_task(cmd);
3771 }
3772
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003773 if (task_cdbs <= 0)
3774 goto out_fail;
3775
3776 if (set_counts) {
3777 atomic_inc(&cmd->t_fe_count);
3778 atomic_inc(&cmd->t_se_count);
3779 }
3780
Nicholas Bellinger9ac54982011-10-22 04:06:23 -07003781 cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
3782 atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
3783 atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003784
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003785 /*
Andy Grovera1d8b492011-05-02 17:12:10 -07003786 * For WRITEs, let the fabric know its buffer is ready..
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003787 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3788 * will be added to the struct se_device execution queue after its WRITE
3789 * data has arrived. (ie: It gets handled by the transport processing
3790 * thread a second time)
3791 */
3792 if (cmd->data_direction == DMA_TO_DEVICE) {
3793 transport_add_tasks_to_state_queue(cmd);
3794 return transport_generic_write_pending(cmd);
3795 }
3796 /*
3797 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3798 * to the execution queue.
3799 */
3800 transport_execute_tasks(cmd);
3801 return 0;
Christoph Hellwigda0f7612011-10-18 06:57:01 -04003802
3803out_fail:
3804 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3805 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3806 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003807}
Andy Grovera1d8b492011-05-02 17:12:10 -07003808EXPORT_SYMBOL(transport_generic_new_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003809
3810/* transport_generic_process_write():
3811 *
3812 *
3813 */
3814void transport_generic_process_write(struct se_cmd *cmd)
3815{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003816 transport_execute_tasks(cmd);
3817}
3818EXPORT_SYMBOL(transport_generic_process_write);
3819
Christoph Hellwige057f532011-10-17 13:56:41 -04003820static void transport_write_pending_qf(struct se_cmd *cmd)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003821{
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003822 int ret;
3823
3824 ret = cmd->se_tfo->write_pending(cmd);
3825 if (ret == -EAGAIN || ret == -ENOMEM) {
Christoph Hellwige057f532011-10-17 13:56:41 -04003826 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3827 cmd);
3828 transport_handle_queue_full(cmd, cmd->se_dev);
3829 }
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003830}
3831
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003832static int transport_generic_write_pending(struct se_cmd *cmd)
3833{
3834 unsigned long flags;
3835 int ret;
3836
Andy Grovera1d8b492011-05-02 17:12:10 -07003837 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003838 cmd->t_state = TRANSPORT_WRITE_PENDING;
Andy Grovera1d8b492011-05-02 17:12:10 -07003839 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003840
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003841 /*
3842 * Clear the se_cmd for WRITE_PENDING status in order to set
Andy Grovera1d8b492011-05-02 17:12:10 -07003843 * cmd->t_transport_active=0 so that transport_generic_handle_data
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003844 * can be called from HW target mode interrupt code. This is safe
Andy Grovere3d6f902011-07-19 08:55:10 +00003845 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003846 * because the se_cmd->se_lun pointer is not being cleared.
3847 */
3848 transport_cmd_check_stop(cmd, 1, 0);
3849
3850 /*
3851 * Call the fabric write_pending function here to let the
3852 * frontend know that WRITE buffers are ready.
3853 */
Andy Grovere3d6f902011-07-19 08:55:10 +00003854 ret = cmd->se_tfo->write_pending(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003855 if (ret == -EAGAIN || ret == -ENOMEM)
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003856 goto queue_full;
3857 else if (ret < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003858 return ret;
3859
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07003860 return 1;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003861
3862queue_full:
Andy Grover6708bb22011-06-08 10:36:43 -07003863 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07003864 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
Christoph Hellwige057f532011-10-17 13:56:41 -04003865 transport_handle_queue_full(cmd, cmd->se_dev);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07003866 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003867}
3868
Christoph Hellwig2dbc43d2011-09-13 23:08:19 +02003869/**
3870 * transport_release_cmd - free a command
3871 * @cmd: command to free
3872 *
3873 * This routine unconditionally frees a command, and reference counting
3874 * or list removal must be done in the caller.
3875 */
Christoph Hellwig35462972011-05-31 23:56:57 -04003876void transport_release_cmd(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003877{
Andy Grovere3d6f902011-07-19 08:55:10 +00003878 BUG_ON(!cmd->se_tfo);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003879
Christoph Hellwig2dbc43d2011-09-13 23:08:19 +02003880 if (cmd->se_tmr_req)
3881 core_tmr_release_req(cmd->se_tmr_req);
3882 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3883 kfree(cmd->t_task_cdb);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003884 /*
3885 * Check if target_wait_for_sess_cmds() is expecting to
3886 * release se_cmd directly here..
3887 */
3888 if (cmd->check_release != 0 && cmd->se_tfo->check_release_cmd)
3889 if (cmd->se_tfo->check_release_cmd(cmd) != 0)
3890 return;
3891
Christoph Hellwig35462972011-05-31 23:56:57 -04003892 cmd->se_tfo->release_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003893}
Christoph Hellwig35462972011-05-31 23:56:57 -04003894EXPORT_SYMBOL(transport_release_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003895
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003896void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003897{
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003898 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
3899 if (wait_for_tasks && cmd->se_tmr_req)
3900 transport_wait_for_tasks(cmd);
3901
Christoph Hellwig35462972011-05-31 23:56:57 -04003902 transport_release_cmd(cmd);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07003903 } else {
3904 if (wait_for_tasks)
3905 transport_wait_for_tasks(cmd);
3906
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003907 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
3908
Christoph Hellwig82f1c8a2011-09-13 23:09:01 +02003909 if (cmd->se_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003910 transport_lun_remove_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003911
Nicholas Bellingerf4366772011-05-19 20:19:11 -07003912 transport_free_dev_tasks(cmd);
3913
Nicholas Bellinger39c05f32011-10-08 13:59:52 -07003914 transport_put_cmd(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08003915 }
3916}
3917EXPORT_SYMBOL(transport_generic_free_cmd);
3918
Nicholas Bellingera17f0912011-11-02 21:52:08 -07003919/* target_get_sess_cmd - Add command to active ->sess_cmd_list
3920 * @se_sess: session to reference
3921 * @se_cmd: command descriptor to add
3922 */
3923void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3924{
3925 unsigned long flags;
3926
3927 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3928 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
3929 se_cmd->check_release = 1;
3930 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3931}
3932EXPORT_SYMBOL(target_get_sess_cmd);
3933
3934/* target_put_sess_cmd - Check for active I/O shutdown or list delete
3935 * @se_sess: session to reference
3936 * @se_cmd: command descriptor to drop
3937 */
3938int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3939{
3940 unsigned long flags;
3941
3942 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3943 if (list_empty(&se_cmd->se_cmd_list)) {
3944 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3945 WARN_ON(1);
3946 return 0;
3947 }
3948
3949 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
3950 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3951 complete(&se_cmd->cmd_wait_comp);
3952 return 1;
3953 }
3954 list_del(&se_cmd->se_cmd_list);
3955 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3956
3957 return 0;
3958}
3959EXPORT_SYMBOL(target_put_sess_cmd);
3960
3961/* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
3962 * @se_sess: session to split
3963 */
3964void target_splice_sess_cmd_list(struct se_session *se_sess)
3965{
3966 struct se_cmd *se_cmd;
3967 unsigned long flags;
3968
3969 WARN_ON(!list_empty(&se_sess->sess_wait_list));
3970 INIT_LIST_HEAD(&se_sess->sess_wait_list);
3971
3972 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3973 se_sess->sess_tearing_down = 1;
3974
3975 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
3976
3977 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
3978 se_cmd->cmd_wait_set = 1;
3979
3980 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3981}
3982EXPORT_SYMBOL(target_splice_sess_cmd_list);
3983
3984/* target_wait_for_sess_cmds - Wait for outstanding descriptors
3985 * @se_sess: session to wait for active I/O
3986 * @wait_for_tasks: Make extra transport_wait_for_tasks call
3987 */
3988void target_wait_for_sess_cmds(
3989 struct se_session *se_sess,
3990 int wait_for_tasks)
3991{
3992 struct se_cmd *se_cmd, *tmp_cmd;
3993 bool rc = false;
3994
3995 list_for_each_entry_safe(se_cmd, tmp_cmd,
3996 &se_sess->sess_wait_list, se_cmd_list) {
3997 list_del(&se_cmd->se_cmd_list);
3998
3999 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
4000 " %d\n", se_cmd, se_cmd->t_state,
4001 se_cmd->se_tfo->get_cmd_state(se_cmd));
4002
4003 if (wait_for_tasks) {
4004 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
4005 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4006 se_cmd->se_tfo->get_cmd_state(se_cmd));
4007
4008 rc = transport_wait_for_tasks(se_cmd);
4009
4010 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
4011 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4012 se_cmd->se_tfo->get_cmd_state(se_cmd));
4013 }
4014
4015 if (!rc) {
4016 wait_for_completion(&se_cmd->cmd_wait_comp);
4017 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
4018 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4019 se_cmd->se_tfo->get_cmd_state(se_cmd));
4020 }
4021
4022 se_cmd->se_tfo->release_cmd(se_cmd);
4023 }
4024}
4025EXPORT_SYMBOL(target_wait_for_sess_cmds);
4026
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004027/* transport_lun_wait_for_tasks():
4028 *
4029 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4030 * an struct se_lun to be successfully shutdown.
4031 */
4032static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4033{
4034 unsigned long flags;
4035 int ret;
4036 /*
4037 * If the frontend has already requested this struct se_cmd to
4038 * be stopped, we can safely ignore this struct se_cmd.
4039 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004040 spin_lock_irqsave(&cmd->t_state_lock, flags);
4041 if (atomic_read(&cmd->t_transport_stop)) {
4042 atomic_set(&cmd->transport_lun_stop, 0);
Andy Grover6708bb22011-06-08 10:36:43 -07004043 pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
Andy Grovere3d6f902011-07-19 08:55:10 +00004044 " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004045 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004046 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovere3d6f902011-07-19 08:55:10 +00004047 return -EPERM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004048 }
Andy Grovera1d8b492011-05-02 17:12:10 -07004049 atomic_set(&cmd->transport_lun_fe_stop, 1);
4050 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004051
Andy Grover5951146d2011-07-19 10:26:37 +00004052 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004053
4054 ret = transport_stop_tasks_for_cmd(cmd);
4055
Andy Grover6708bb22011-06-08 10:36:43 -07004056 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4057 " %d\n", cmd, cmd->t_task_list_num, ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004058 if (!ret) {
Andy Grover6708bb22011-06-08 10:36:43 -07004059 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004060 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004061 wait_for_completion(&cmd->transport_lun_stop_comp);
Andy Grover6708bb22011-06-08 10:36:43 -07004062 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004063 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004064 }
Christoph Hellwig3df8d402011-10-17 13:56:43 -04004065 transport_remove_cmd_from_queue(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004066
4067 return 0;
4068}
4069
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004070static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4071{
4072 struct se_cmd *cmd = NULL;
4073 unsigned long lun_flags, cmd_flags;
4074 /*
4075 * Do exception processing and return CHECK_CONDITION status to the
4076 * Initiator Port.
4077 */
4078 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
Andy Grover5951146d2011-07-19 10:26:37 +00004079 while (!list_empty(&lun->lun_cmd_list)) {
4080 cmd = list_first_entry(&lun->lun_cmd_list,
4081 struct se_cmd, se_lun_node);
4082 list_del(&cmd->se_lun_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004083
Andy Grovera1d8b492011-05-02 17:12:10 -07004084 atomic_set(&cmd->transport_lun_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004085 /*
4086 * This will notify iscsi_target_transport.c:
4087 * transport_cmd_check_stop() that a LUN shutdown is in
4088 * progress for the iscsi_cmd_t.
4089 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004090 spin_lock(&cmd->t_state_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07004091 pr_debug("SE_LUN[%d] - Setting cmd->transport"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004092 "_lun_stop for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004093 cmd->se_lun->unpacked_lun,
4094 cmd->se_tfo->get_task_tag(cmd));
Andy Grovera1d8b492011-05-02 17:12:10 -07004095 atomic_set(&cmd->transport_lun_stop, 1);
4096 spin_unlock(&cmd->t_state_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004097
4098 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4099
Andy Grover6708bb22011-06-08 10:36:43 -07004100 if (!cmd->se_lun) {
4101 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004102 cmd->se_tfo->get_task_tag(cmd),
4103 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004104 BUG();
4105 }
4106 /*
4107 * If the Storage engine still owns the iscsi_cmd_t, determine
4108 * and/or stop its context.
4109 */
Andy Grover6708bb22011-06-08 10:36:43 -07004110 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
Andy Grovere3d6f902011-07-19 08:55:10 +00004111 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4112 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004113
Andy Grovere3d6f902011-07-19 08:55:10 +00004114 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004115 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4116 continue;
4117 }
4118
Andy Grover6708bb22011-06-08 10:36:43 -07004119 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004120 "_wait_for_tasks(): SUCCESS\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004121 cmd->se_lun->unpacked_lun,
4122 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004123
Andy Grovera1d8b492011-05-02 17:12:10 -07004124 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
Andy Grover6708bb22011-06-08 10:36:43 -07004125 if (!atomic_read(&cmd->transport_dev_active)) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004126 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004127 goto check_cond;
4128 }
Andy Grovera1d8b492011-05-02 17:12:10 -07004129 atomic_set(&cmd->transport_dev_active, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004130 transport_all_task_dev_remove_state(cmd);
Andy Grovera1d8b492011-05-02 17:12:10 -07004131 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004132
4133 transport_free_dev_tasks(cmd);
4134 /*
4135 * The Storage engine stopped this struct se_cmd before it was
4136 * send to the fabric frontend for delivery back to the
4137 * Initiator Node. Return this SCSI CDB back with an
4138 * CHECK_CONDITION status.
4139 */
4140check_cond:
4141 transport_send_check_condition_and_sense(cmd,
4142 TCM_NON_EXISTENT_LUN, 0);
4143 /*
4144 * If the fabric frontend is waiting for this iscsi_cmd_t to
4145 * be released, notify the waiting thread now that LU has
4146 * finished accessing it.
4147 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004148 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4149 if (atomic_read(&cmd->transport_lun_fe_stop)) {
Andy Grover6708bb22011-06-08 10:36:43 -07004150 pr_debug("SE_LUN[%d] - Detected FE stop for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004151 " struct se_cmd: %p ITT: 0x%08x\n",
4152 lun->unpacked_lun,
Andy Grovere3d6f902011-07-19 08:55:10 +00004153 cmd, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004154
Andy Grovera1d8b492011-05-02 17:12:10 -07004155 spin_unlock_irqrestore(&cmd->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004156 cmd_flags);
4157 transport_cmd_check_stop(cmd, 1, 0);
Andy Grovera1d8b492011-05-02 17:12:10 -07004158 complete(&cmd->transport_lun_fe_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004159 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4160 continue;
4161 }
Andy Grover6708bb22011-06-08 10:36:43 -07004162 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004163 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004164
Andy Grovera1d8b492011-05-02 17:12:10 -07004165 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004166 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4167 }
4168 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4169}
4170
4171static int transport_clear_lun_thread(void *p)
4172{
4173 struct se_lun *lun = (struct se_lun *)p;
4174
4175 __transport_clear_lun_from_sessions(lun);
4176 complete(&lun->lun_shutdown_comp);
4177
4178 return 0;
4179}
4180
4181int transport_clear_lun_from_sessions(struct se_lun *lun)
4182{
4183 struct task_struct *kt;
4184
Andy Grover5951146d2011-07-19 10:26:37 +00004185 kt = kthread_run(transport_clear_lun_thread, lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004186 "tcm_cl_%u", lun->unpacked_lun);
4187 if (IS_ERR(kt)) {
Andy Grover6708bb22011-06-08 10:36:43 -07004188 pr_err("Unable to start clear_lun thread\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00004189 return PTR_ERR(kt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004190 }
4191 wait_for_completion(&lun->lun_shutdown_comp);
4192
4193 return 0;
4194}
4195
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004196/**
4197 * transport_wait_for_tasks - wait for completion to occur
4198 * @cmd: command to wait
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004199 *
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004200 * Called from frontend fabric context to wait for storage engine
4201 * to pause and/or release frontend generated struct se_cmd.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004202 */
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004203bool transport_wait_for_tasks(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004204{
4205 unsigned long flags;
4206
Andy Grovera1d8b492011-05-02 17:12:10 -07004207 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004208 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req)) {
4209 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004210 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004211 }
4212 /*
4213 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4214 * has been set in transport_set_supported_SAM_opcode().
4215 */
4216 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && !cmd->se_tmr_req) {
4217 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004218 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004219 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004220 /*
4221 * If we are already stopped due to an external event (ie: LUN shutdown)
4222 * sleep until the connection can have the passed struct se_cmd back.
Andy Grovera1d8b492011-05-02 17:12:10 -07004223 * The cmd->transport_lun_stopped_sem will be upped by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004224 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4225 * has completed its operation on the struct se_cmd.
4226 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004227 if (atomic_read(&cmd->transport_lun_stop)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004228
Andy Grover6708bb22011-06-08 10:36:43 -07004229 pr_debug("wait_for_tasks: Stopping"
Andy Grovere3d6f902011-07-19 08:55:10 +00004230 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004231 "_stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004232 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004233 /*
4234 * There is a special case for WRITES where a FE exception +
4235 * LUN shutdown means ConfigFS context is still sleeping on
4236 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4237 * We go ahead and up transport_lun_stop_comp just to be sure
4238 * here.
4239 */
Andy Grovera1d8b492011-05-02 17:12:10 -07004240 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4241 complete(&cmd->transport_lun_stop_comp);
4242 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4243 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004244
4245 transport_all_task_dev_remove_state(cmd);
4246 /*
4247 * At this point, the frontend who was the originator of this
4248 * struct se_cmd, now owns the structure and can be released through
4249 * normal means below.
4250 */
Andy Grover6708bb22011-06-08 10:36:43 -07004251 pr_debug("wait_for_tasks: Stopped"
Andy Grovere3d6f902011-07-19 08:55:10 +00004252 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004253 "stop_comp); for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004254 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004255
Andy Grovera1d8b492011-05-02 17:12:10 -07004256 atomic_set(&cmd->transport_lun_stop, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004257 }
Andy Grovera1d8b492011-05-02 17:12:10 -07004258 if (!atomic_read(&cmd->t_transport_active) ||
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004259 atomic_read(&cmd->t_transport_aborted)) {
4260 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004261 return false;
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004262 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004263
Andy Grovera1d8b492011-05-02 17:12:10 -07004264 atomic_set(&cmd->t_transport_stop, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004265
Andy Grover6708bb22011-06-08 10:36:43 -07004266 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004267 " i_state: %d, t_state: %d, t_transport_stop = TRUE\n",
4268 cmd, cmd->se_tfo->get_task_tag(cmd),
4269 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004270
Andy Grovera1d8b492011-05-02 17:12:10 -07004271 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004272
Andy Grover5951146d2011-07-19 10:26:37 +00004273 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004274
Andy Grovera1d8b492011-05-02 17:12:10 -07004275 wait_for_completion(&cmd->t_transport_stop_comp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004276
Andy Grovera1d8b492011-05-02 17:12:10 -07004277 spin_lock_irqsave(&cmd->t_state_lock, flags);
4278 atomic_set(&cmd->t_transport_active, 0);
4279 atomic_set(&cmd->t_transport_stop, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004280
Andy Grover6708bb22011-06-08 10:36:43 -07004281 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
Andy Grovera1d8b492011-05-02 17:12:10 -07004282 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00004283 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004284
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004285 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingera17f0912011-11-02 21:52:08 -07004286
4287 return true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004288}
Nicholas Bellingerd14921d2011-10-09 01:00:58 -07004289EXPORT_SYMBOL(transport_wait_for_tasks);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004290
4291static int transport_get_sense_codes(
4292 struct se_cmd *cmd,
4293 u8 *asc,
4294 u8 *ascq)
4295{
4296 *asc = cmd->scsi_asc;
4297 *ascq = cmd->scsi_ascq;
4298
4299 return 0;
4300}
4301
4302static int transport_set_sense_codes(
4303 struct se_cmd *cmd,
4304 u8 asc,
4305 u8 ascq)
4306{
4307 cmd->scsi_asc = asc;
4308 cmd->scsi_ascq = ascq;
4309
4310 return 0;
4311}
4312
4313int transport_send_check_condition_and_sense(
4314 struct se_cmd *cmd,
4315 u8 reason,
4316 int from_transport)
4317{
4318 unsigned char *buffer = cmd->sense_buffer;
4319 unsigned long flags;
4320 int offset;
4321 u8 asc = 0, ascq = 0;
4322
Andy Grovera1d8b492011-05-02 17:12:10 -07004323 spin_lock_irqsave(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004324 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004325 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004326 return 0;
4327 }
4328 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
Andy Grovera1d8b492011-05-02 17:12:10 -07004329 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004330
4331 if (!reason && from_transport)
4332 goto after_reason;
4333
4334 if (!from_transport)
4335 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4336 /*
4337 * Data Segment and SenseLength of the fabric response PDU.
4338 *
4339 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4340 * from include/scsi/scsi_cmnd.h
4341 */
Andy Grovere3d6f902011-07-19 08:55:10 +00004342 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004343 TRANSPORT_SENSE_BUFFER);
4344 /*
4345 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4346 * SENSE KEY values from include/scsi/scsi.h
4347 */
4348 switch (reason) {
4349 case TCM_NON_EXISTENT_LUN:
Nicholas Bellingereb39d342011-07-26 16:59:00 -07004350 /* CURRENT ERROR */
4351 buffer[offset] = 0x70;
4352 /* ILLEGAL REQUEST */
4353 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4354 /* LOGICAL UNIT NOT SUPPORTED */
4355 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4356 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004357 case TCM_UNSUPPORTED_SCSI_OPCODE:
4358 case TCM_SECTOR_COUNT_TOO_MANY:
4359 /* CURRENT ERROR */
4360 buffer[offset] = 0x70;
4361 /* ILLEGAL REQUEST */
4362 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4363 /* INVALID COMMAND OPERATION CODE */
4364 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4365 break;
4366 case TCM_UNKNOWN_MODE_PAGE:
4367 /* CURRENT ERROR */
4368 buffer[offset] = 0x70;
4369 /* ILLEGAL REQUEST */
4370 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4371 /* INVALID FIELD IN CDB */
4372 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4373 break;
4374 case TCM_CHECK_CONDITION_ABORT_CMD:
4375 /* CURRENT ERROR */
4376 buffer[offset] = 0x70;
4377 /* ABORTED COMMAND */
4378 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4379 /* BUS DEVICE RESET FUNCTION OCCURRED */
4380 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4381 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4382 break;
4383 case TCM_INCORRECT_AMOUNT_OF_DATA:
4384 /* CURRENT ERROR */
4385 buffer[offset] = 0x70;
4386 /* ABORTED COMMAND */
4387 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4388 /* WRITE ERROR */
4389 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4390 /* NOT ENOUGH UNSOLICITED DATA */
4391 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4392 break;
4393 case TCM_INVALID_CDB_FIELD:
4394 /* CURRENT ERROR */
4395 buffer[offset] = 0x70;
4396 /* ABORTED COMMAND */
4397 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4398 /* INVALID FIELD IN CDB */
4399 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4400 break;
4401 case TCM_INVALID_PARAMETER_LIST:
4402 /* CURRENT ERROR */
4403 buffer[offset] = 0x70;
4404 /* ABORTED COMMAND */
4405 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4406 /* INVALID FIELD IN PARAMETER LIST */
4407 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4408 break;
4409 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4410 /* CURRENT ERROR */
4411 buffer[offset] = 0x70;
4412 /* ABORTED COMMAND */
4413 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4414 /* WRITE ERROR */
4415 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4416 /* UNEXPECTED_UNSOLICITED_DATA */
4417 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4418 break;
4419 case TCM_SERVICE_CRC_ERROR:
4420 /* CURRENT ERROR */
4421 buffer[offset] = 0x70;
4422 /* ABORTED COMMAND */
4423 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4424 /* PROTOCOL SERVICE CRC ERROR */
4425 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4426 /* N/A */
4427 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4428 break;
4429 case TCM_SNACK_REJECTED:
4430 /* CURRENT ERROR */
4431 buffer[offset] = 0x70;
4432 /* ABORTED COMMAND */
4433 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4434 /* READ ERROR */
4435 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4436 /* FAILED RETRANSMISSION REQUEST */
4437 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4438 break;
4439 case TCM_WRITE_PROTECTED:
4440 /* CURRENT ERROR */
4441 buffer[offset] = 0x70;
4442 /* DATA PROTECT */
4443 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4444 /* WRITE PROTECTED */
4445 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4446 break;
4447 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4448 /* CURRENT ERROR */
4449 buffer[offset] = 0x70;
4450 /* UNIT ATTENTION */
4451 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4452 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4453 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4454 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4455 break;
4456 case TCM_CHECK_CONDITION_NOT_READY:
4457 /* CURRENT ERROR */
4458 buffer[offset] = 0x70;
4459 /* Not Ready */
4460 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4461 transport_get_sense_codes(cmd, &asc, &ascq);
4462 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4463 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4464 break;
4465 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4466 default:
4467 /* CURRENT ERROR */
4468 buffer[offset] = 0x70;
4469 /* ILLEGAL REQUEST */
4470 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4471 /* LOGICAL UNIT COMMUNICATION FAILURE */
4472 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4473 break;
4474 }
4475 /*
4476 * This code uses linux/include/scsi/scsi.h SAM status codes!
4477 */
4478 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4479 /*
4480 * Automatically padded, this value is encoded in the fabric's
4481 * data_length response PDU containing the SCSI defined sense data.
4482 */
4483 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4484
4485after_reason:
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004486 return cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004487}
4488EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4489
4490int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4491{
4492 int ret = 0;
4493
Andy Grovera1d8b492011-05-02 17:12:10 -07004494 if (atomic_read(&cmd->t_transport_aborted) != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07004495 if (!send_status ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004496 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4497 return 1;
4498#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004499 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004500 " status for CDB: 0x%02x ITT: 0x%08x\n",
Andy Grovera1d8b492011-05-02 17:12:10 -07004501 cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004502 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004503#endif
4504 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
Andy Grovere3d6f902011-07-19 08:55:10 +00004505 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004506 ret = 1;
4507 }
4508 return ret;
4509}
4510EXPORT_SYMBOL(transport_check_aborted_status);
4511
4512void transport_send_task_abort(struct se_cmd *cmd)
4513{
Nicholas Bellingerc252f002011-09-29 14:22:13 -07004514 unsigned long flags;
4515
4516 spin_lock_irqsave(&cmd->t_state_lock, flags);
4517 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4518 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4519 return;
4520 }
4521 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4522
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004523 /*
4524 * If there are still expected incoming fabric WRITEs, we wait
4525 * until until they have completed before sending a TASK_ABORTED
4526 * response. This response with TASK_ABORTED status will be
4527 * queued back to fabric module by transport_check_aborted_status().
4528 */
4529 if (cmd->data_direction == DMA_TO_DEVICE) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004530 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
Andy Grovera1d8b492011-05-02 17:12:10 -07004531 atomic_inc(&cmd->t_transport_aborted);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004532 smp_mb__after_atomic_inc();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004533 }
4534 }
4535 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4536#if 0
Andy Grover6708bb22011-06-08 10:36:43 -07004537 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
Andy Grovera1d8b492011-05-02 17:12:10 -07004538 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
Andy Grovere3d6f902011-07-19 08:55:10 +00004539 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004540#endif
Andy Grovere3d6f902011-07-19 08:55:10 +00004541 cmd->se_tfo->queue_status(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004542}
4543
4544/* transport_generic_do_tmr():
4545 *
4546 *
4547 */
4548int transport_generic_do_tmr(struct se_cmd *cmd)
4549{
Andy Grover5951146d2011-07-19 10:26:37 +00004550 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004551 struct se_tmr_req *tmr = cmd->se_tmr_req;
4552 int ret;
4553
4554 switch (tmr->function) {
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004555 case TMR_ABORT_TASK:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004556 tmr->response = TMR_FUNCTION_REJECTED;
4557 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004558 case TMR_ABORT_TASK_SET:
4559 case TMR_CLEAR_ACA:
4560 case TMR_CLEAR_TASK_SET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004561 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4562 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004563 case TMR_LUN_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004564 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4565 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4566 TMR_FUNCTION_REJECTED;
4567 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004568 case TMR_TARGET_WARM_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004569 tmr->response = TMR_FUNCTION_REJECTED;
4570 break;
Nicholas Bellinger5c6cd612011-03-14 04:06:04 -07004571 case TMR_TARGET_COLD_RESET:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004572 tmr->response = TMR_FUNCTION_REJECTED;
4573 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004574 default:
Andy Grover6708bb22011-06-08 10:36:43 -07004575 pr_err("Uknown TMR function: 0x%02x.\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004576 tmr->function);
4577 tmr->response = TMR_FUNCTION_REJECTED;
4578 break;
4579 }
4580
4581 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
Andy Grovere3d6f902011-07-19 08:55:10 +00004582 cmd->se_tfo->queue_tm_rsp(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004583
Christoph Hellwigb7b8bef2011-10-17 13:56:44 -04004584 transport_cmd_check_stop_to_fabric(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004585 return 0;
4586}
4587
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004588/* transport_processing_thread():
4589 *
4590 *
4591 */
4592static int transport_processing_thread(void *param)
4593{
Andy Grover5951146d2011-07-19 10:26:37 +00004594 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004595 struct se_cmd *cmd;
4596 struct se_device *dev = (struct se_device *) param;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004597
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004598 while (!kthread_should_stop()) {
Andy Grovere3d6f902011-07-19 08:55:10 +00004599 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4600 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004601 kthread_should_stop());
4602 if (ret < 0)
4603 goto out;
4604
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004605get_cmd:
4606 __transport_execute_tasks(dev);
4607
Andy Grover5951146d2011-07-19 10:26:37 +00004608 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4609 if (!cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004610 continue;
4611
Andy Grover5951146d2011-07-19 10:26:37 +00004612 switch (cmd->t_state) {
Christoph Hellwig680b73c2011-09-12 21:51:14 +02004613 case TRANSPORT_NEW_CMD:
4614 BUG();
4615 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004616 case TRANSPORT_NEW_CMD_MAP:
Andy Grover6708bb22011-06-08 10:36:43 -07004617 if (!cmd->se_tfo->new_cmd_map) {
4618 pr_err("cmd->se_tfo->new_cmd_map is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004619 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4620 BUG();
4621 }
Andy Grovere3d6f902011-07-19 08:55:10 +00004622 ret = cmd->se_tfo->new_cmd_map(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004623 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004624 transport_generic_request_failure(cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004625 break;
4626 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004627 ret = transport_generic_new_cmd(cmd);
Nicholas Bellingerf147abb2011-10-25 23:57:41 -07004628 if (ret < 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07004629 transport_generic_request_failure(cmd);
4630 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004631 }
4632 break;
4633 case TRANSPORT_PROCESS_WRITE:
4634 transport_generic_process_write(cmd);
4635 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004636 case TRANSPORT_PROCESS_TMR:
4637 transport_generic_do_tmr(cmd);
4638 break;
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004639 case TRANSPORT_COMPLETE_QF_WP:
Christoph Hellwige057f532011-10-17 13:56:41 -04004640 transport_write_pending_qf(cmd);
4641 break;
4642 case TRANSPORT_COMPLETE_QF_OK:
4643 transport_complete_qf(cmd);
Nicholas Bellinger07bde792011-06-13 14:46:09 -07004644 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004645 default:
Christoph Hellwigf2da9db2011-10-17 13:56:51 -04004646 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4647 "i_state: %d on SE LUN: %u\n",
4648 cmd->t_state,
Andy Grovere3d6f902011-07-19 08:55:10 +00004649 cmd->se_tfo->get_task_tag(cmd),
4650 cmd->se_tfo->get_cmd_state(cmd),
4651 cmd->se_lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004652 BUG();
4653 }
4654
4655 goto get_cmd;
4656 }
4657
4658out:
Nicholas Bellingerce8762f2011-10-09 02:19:01 -07004659 WARN_ON(!list_empty(&dev->state_task_list));
4660 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08004661 dev->process_thread = NULL;
4662 return 0;
4663}