Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 | |
| 29 | #include <linux/version.h> |
| 30 | #include <linux/net.h> |
| 31 | #include <linux/delay.h> |
| 32 | #include <linux/string.h> |
| 33 | #include <linux/timer.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/blkdev.h> |
| 36 | #include <linux/spinlock.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 37 | #include <linux/kthread.h> |
| 38 | #include <linux/in.h> |
| 39 | #include <linux/cdrom.h> |
| 40 | #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 Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 45 | #include <scsi/scsi_tcq.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 46 | |
| 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" |
| 56 | #include "target_core_hba.h" |
| 57 | #include "target_core_pr.h" |
| 58 | #include "target_core_scdb.h" |
| 59 | #include "target_core_ua.h" |
| 60 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 61 | static int sub_api_initialized; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 62 | |
| 63 | static struct kmem_cache *se_cmd_cache; |
| 64 | static struct kmem_cache *se_sess_cache; |
| 65 | struct kmem_cache *se_tmr_req_cache; |
| 66 | struct kmem_cache *se_ua_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 67 | struct kmem_cache *t10_pr_reg_cache; |
| 68 | struct kmem_cache *t10_alua_lu_gp_cache; |
| 69 | struct kmem_cache *t10_alua_lu_gp_mem_cache; |
| 70 | struct kmem_cache *t10_alua_tg_pt_gp_cache; |
| 71 | struct kmem_cache *t10_alua_tg_pt_gp_mem_cache; |
| 72 | |
| 73 | /* Used for transport_dev_get_map_*() */ |
| 74 | typedef int (*map_func_t)(struct se_task *, u32); |
| 75 | |
| 76 | static int transport_generic_write_pending(struct se_cmd *); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 77 | static int transport_processing_thread(void *param); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 78 | static int __transport_execute_tasks(struct se_device *dev); |
| 79 | static void transport_complete_task_attr(struct se_cmd *cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 80 | static int transport_complete_qf(struct se_cmd *cmd); |
| 81 | static void transport_handle_queue_full(struct se_cmd *cmd, |
| 82 | struct se_device *dev, int (*qf_callback)(struct se_cmd *)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 83 | static void transport_direct_request_timeout(struct se_cmd *cmd); |
| 84 | static void transport_free_dev_tasks(struct se_cmd *cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 85 | static u32 transport_allocate_tasks(struct se_cmd *cmd, |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 86 | unsigned long long starting_lba, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 87 | enum dma_data_direction data_direction, |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 88 | struct scatterlist *sgl, unsigned int nents); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 89 | static int transport_generic_get_mem(struct se_cmd *cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 90 | static int transport_generic_remove(struct se_cmd *cmd, |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 91 | int session_reinstatement); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 92 | static void transport_release_fe_cmd(struct se_cmd *cmd); |
| 93 | static void transport_remove_cmd_from_queue(struct se_cmd *cmd, |
| 94 | struct se_queue_obj *qobj); |
| 95 | static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq); |
| 96 | static void transport_stop_all_task_timers(struct se_cmd *cmd); |
| 97 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 98 | int init_se_kmem_caches(void) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 99 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 100 | se_cmd_cache = kmem_cache_create("se_cmd_cache", |
| 101 | sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 102 | if (!se_cmd_cache) { |
| 103 | pr_err("kmem_cache_create for struct se_cmd failed\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 104 | goto out; |
| 105 | } |
| 106 | se_tmr_req_cache = kmem_cache_create("se_tmr_cache", |
| 107 | sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req), |
| 108 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 109 | if (!se_tmr_req_cache) { |
| 110 | pr_err("kmem_cache_create() for struct se_tmr_req" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 111 | " failed\n"); |
| 112 | goto out; |
| 113 | } |
| 114 | se_sess_cache = kmem_cache_create("se_sess_cache", |
| 115 | sizeof(struct se_session), __alignof__(struct se_session), |
| 116 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 117 | if (!se_sess_cache) { |
| 118 | pr_err("kmem_cache_create() for struct se_session" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 119 | " failed\n"); |
| 120 | goto out; |
| 121 | } |
| 122 | se_ua_cache = kmem_cache_create("se_ua_cache", |
| 123 | sizeof(struct se_ua), __alignof__(struct se_ua), |
| 124 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 125 | if (!se_ua_cache) { |
| 126 | pr_err("kmem_cache_create() for struct se_ua failed\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 127 | goto out; |
| 128 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 129 | t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache", |
| 130 | sizeof(struct t10_pr_registration), |
| 131 | __alignof__(struct t10_pr_registration), 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 132 | if (!t10_pr_reg_cache) { |
| 133 | pr_err("kmem_cache_create() for struct t10_pr_registration" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 134 | " failed\n"); |
| 135 | goto out; |
| 136 | } |
| 137 | t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache", |
| 138 | sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp), |
| 139 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 140 | if (!t10_alua_lu_gp_cache) { |
| 141 | pr_err("kmem_cache_create() for t10_alua_lu_gp_cache" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 142 | " failed\n"); |
| 143 | goto out; |
| 144 | } |
| 145 | t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache", |
| 146 | sizeof(struct t10_alua_lu_gp_member), |
| 147 | __alignof__(struct t10_alua_lu_gp_member), 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 148 | if (!t10_alua_lu_gp_mem_cache) { |
| 149 | pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 150 | "cache failed\n"); |
| 151 | goto out; |
| 152 | } |
| 153 | t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache", |
| 154 | sizeof(struct t10_alua_tg_pt_gp), |
| 155 | __alignof__(struct t10_alua_tg_pt_gp), 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 156 | if (!t10_alua_tg_pt_gp_cache) { |
| 157 | pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 158 | "cache failed\n"); |
| 159 | goto out; |
| 160 | } |
| 161 | t10_alua_tg_pt_gp_mem_cache = kmem_cache_create( |
| 162 | "t10_alua_tg_pt_gp_mem_cache", |
| 163 | sizeof(struct t10_alua_tg_pt_gp_member), |
| 164 | __alignof__(struct t10_alua_tg_pt_gp_member), |
| 165 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 166 | if (!t10_alua_tg_pt_gp_mem_cache) { |
| 167 | pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 168 | "mem_t failed\n"); |
| 169 | goto out; |
| 170 | } |
| 171 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 172 | return 0; |
| 173 | out: |
| 174 | if (se_cmd_cache) |
| 175 | kmem_cache_destroy(se_cmd_cache); |
| 176 | if (se_tmr_req_cache) |
| 177 | kmem_cache_destroy(se_tmr_req_cache); |
| 178 | if (se_sess_cache) |
| 179 | kmem_cache_destroy(se_sess_cache); |
| 180 | if (se_ua_cache) |
| 181 | kmem_cache_destroy(se_ua_cache); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 182 | if (t10_pr_reg_cache) |
| 183 | kmem_cache_destroy(t10_pr_reg_cache); |
| 184 | if (t10_alua_lu_gp_cache) |
| 185 | kmem_cache_destroy(t10_alua_lu_gp_cache); |
| 186 | if (t10_alua_lu_gp_mem_cache) |
| 187 | kmem_cache_destroy(t10_alua_lu_gp_mem_cache); |
| 188 | if (t10_alua_tg_pt_gp_cache) |
| 189 | kmem_cache_destroy(t10_alua_tg_pt_gp_cache); |
| 190 | if (t10_alua_tg_pt_gp_mem_cache) |
| 191 | kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 192 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 195 | void release_se_kmem_caches(void) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 196 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 197 | kmem_cache_destroy(se_cmd_cache); |
| 198 | kmem_cache_destroy(se_tmr_req_cache); |
| 199 | kmem_cache_destroy(se_sess_cache); |
| 200 | kmem_cache_destroy(se_ua_cache); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 201 | kmem_cache_destroy(t10_pr_reg_cache); |
| 202 | kmem_cache_destroy(t10_alua_lu_gp_cache); |
| 203 | kmem_cache_destroy(t10_alua_lu_gp_mem_cache); |
| 204 | kmem_cache_destroy(t10_alua_tg_pt_gp_cache); |
| 205 | kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 208 | /* This code ensures unique mib indexes are handed out. */ |
| 209 | static DEFINE_SPINLOCK(scsi_mib_index_lock); |
| 210 | static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX]; |
Nicholas Bellinger | e89d15e | 2011-02-09 15:35:03 -0800 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * Allocate a new row index for the entry type specified |
| 214 | */ |
| 215 | u32 scsi_get_new_index(scsi_index_t type) |
| 216 | { |
| 217 | u32 new_index; |
| 218 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 219 | BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX)); |
Nicholas Bellinger | e89d15e | 2011-02-09 15:35:03 -0800 | [diff] [blame] | 220 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 221 | spin_lock(&scsi_mib_index_lock); |
| 222 | new_index = ++scsi_mib_index[type]; |
| 223 | spin_unlock(&scsi_mib_index_lock); |
Nicholas Bellinger | e89d15e | 2011-02-09 15:35:03 -0800 | [diff] [blame] | 224 | |
| 225 | return new_index; |
| 226 | } |
| 227 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 228 | void transport_init_queue_obj(struct se_queue_obj *qobj) |
| 229 | { |
| 230 | atomic_set(&qobj->queue_cnt, 0); |
| 231 | INIT_LIST_HEAD(&qobj->qobj_list); |
| 232 | init_waitqueue_head(&qobj->thread_wq); |
| 233 | spin_lock_init(&qobj->cmd_queue_lock); |
| 234 | } |
| 235 | EXPORT_SYMBOL(transport_init_queue_obj); |
| 236 | |
| 237 | static int transport_subsystem_reqmods(void) |
| 238 | { |
| 239 | int ret; |
| 240 | |
| 241 | ret = request_module("target_core_iblock"); |
| 242 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 243 | pr_err("Unable to load target_core_iblock\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 244 | |
| 245 | ret = request_module("target_core_file"); |
| 246 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 247 | pr_err("Unable to load target_core_file\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 248 | |
| 249 | ret = request_module("target_core_pscsi"); |
| 250 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 251 | pr_err("Unable to load target_core_pscsi\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 252 | |
| 253 | ret = request_module("target_core_stgt"); |
| 254 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 255 | pr_err("Unable to load target_core_stgt\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | int transport_subsystem_check_init(void) |
| 261 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 262 | int ret; |
| 263 | |
| 264 | if (sub_api_initialized) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 265 | return 0; |
| 266 | /* |
| 267 | * Request the loading of known TCM subsystem plugins.. |
| 268 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 269 | ret = transport_subsystem_reqmods(); |
| 270 | if (ret < 0) |
| 271 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 272 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 273 | sub_api_initialized = 1; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | struct se_session *transport_init_session(void) |
| 278 | { |
| 279 | struct se_session *se_sess; |
| 280 | |
| 281 | se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 282 | if (!se_sess) { |
| 283 | pr_err("Unable to allocate struct se_session from" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 284 | " se_sess_cache\n"); |
| 285 | return ERR_PTR(-ENOMEM); |
| 286 | } |
| 287 | INIT_LIST_HEAD(&se_sess->sess_list); |
| 288 | INIT_LIST_HEAD(&se_sess->sess_acl_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 289 | |
| 290 | return se_sess; |
| 291 | } |
| 292 | EXPORT_SYMBOL(transport_init_session); |
| 293 | |
| 294 | /* |
| 295 | * Called with spin_lock_bh(&struct se_portal_group->session_lock called. |
| 296 | */ |
| 297 | void __transport_register_session( |
| 298 | struct se_portal_group *se_tpg, |
| 299 | struct se_node_acl *se_nacl, |
| 300 | struct se_session *se_sess, |
| 301 | void *fabric_sess_ptr) |
| 302 | { |
| 303 | unsigned char buf[PR_REG_ISID_LEN]; |
| 304 | |
| 305 | se_sess->se_tpg = se_tpg; |
| 306 | se_sess->fabric_sess_ptr = fabric_sess_ptr; |
| 307 | /* |
| 308 | * Used by struct se_node_acl's under ConfigFS to locate active se_session-t |
| 309 | * |
| 310 | * Only set for struct se_session's that will actually be moving I/O. |
| 311 | * eg: *NOT* discovery sessions. |
| 312 | */ |
| 313 | if (se_nacl) { |
| 314 | /* |
| 315 | * If the fabric module supports an ISID based TransportID, |
| 316 | * save this value in binary from the fabric I_T Nexus now. |
| 317 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 318 | if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 319 | memset(&buf[0], 0, PR_REG_ISID_LEN); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 320 | se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 321 | &buf[0], PR_REG_ISID_LEN); |
| 322 | se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]); |
| 323 | } |
| 324 | spin_lock_irq(&se_nacl->nacl_sess_lock); |
| 325 | /* |
| 326 | * The se_nacl->nacl_sess pointer will be set to the |
| 327 | * last active I_T Nexus for each struct se_node_acl. |
| 328 | */ |
| 329 | se_nacl->nacl_sess = se_sess; |
| 330 | |
| 331 | list_add_tail(&se_sess->sess_acl_list, |
| 332 | &se_nacl->acl_sess_list); |
| 333 | spin_unlock_irq(&se_nacl->nacl_sess_lock); |
| 334 | } |
| 335 | list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list); |
| 336 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 337 | pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 338 | se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 339 | } |
| 340 | EXPORT_SYMBOL(__transport_register_session); |
| 341 | |
| 342 | void transport_register_session( |
| 343 | struct se_portal_group *se_tpg, |
| 344 | struct se_node_acl *se_nacl, |
| 345 | struct se_session *se_sess, |
| 346 | void *fabric_sess_ptr) |
| 347 | { |
| 348 | spin_lock_bh(&se_tpg->session_lock); |
| 349 | __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr); |
| 350 | spin_unlock_bh(&se_tpg->session_lock); |
| 351 | } |
| 352 | EXPORT_SYMBOL(transport_register_session); |
| 353 | |
| 354 | void transport_deregister_session_configfs(struct se_session *se_sess) |
| 355 | { |
| 356 | struct se_node_acl *se_nacl; |
Roland Dreier | 2338886 | 2011-06-22 01:02:21 -0700 | [diff] [blame] | 357 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 358 | /* |
| 359 | * Used by struct se_node_acl's under ConfigFS to locate active struct se_session |
| 360 | */ |
| 361 | se_nacl = se_sess->se_node_acl; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 362 | if (se_nacl) { |
Roland Dreier | 2338886 | 2011-06-22 01:02:21 -0700 | [diff] [blame] | 363 | spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 364 | list_del(&se_sess->sess_acl_list); |
| 365 | /* |
| 366 | * If the session list is empty, then clear the pointer. |
| 367 | * Otherwise, set the struct se_session pointer from the tail |
| 368 | * element of the per struct se_node_acl active session list. |
| 369 | */ |
| 370 | if (list_empty(&se_nacl->acl_sess_list)) |
| 371 | se_nacl->nacl_sess = NULL; |
| 372 | else { |
| 373 | se_nacl->nacl_sess = container_of( |
| 374 | se_nacl->acl_sess_list.prev, |
| 375 | struct se_session, sess_acl_list); |
| 376 | } |
Roland Dreier | 2338886 | 2011-06-22 01:02:21 -0700 | [diff] [blame] | 377 | spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | EXPORT_SYMBOL(transport_deregister_session_configfs); |
| 381 | |
| 382 | void transport_free_session(struct se_session *se_sess) |
| 383 | { |
| 384 | kmem_cache_free(se_sess_cache, se_sess); |
| 385 | } |
| 386 | EXPORT_SYMBOL(transport_free_session); |
| 387 | |
| 388 | void transport_deregister_session(struct se_session *se_sess) |
| 389 | { |
| 390 | struct se_portal_group *se_tpg = se_sess->se_tpg; |
| 391 | struct se_node_acl *se_nacl; |
| 392 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 393 | if (!se_tpg) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 394 | transport_free_session(se_sess); |
| 395 | return; |
| 396 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 397 | |
| 398 | spin_lock_bh(&se_tpg->session_lock); |
| 399 | list_del(&se_sess->sess_list); |
| 400 | se_sess->se_tpg = NULL; |
| 401 | se_sess->fabric_sess_ptr = NULL; |
| 402 | spin_unlock_bh(&se_tpg->session_lock); |
| 403 | |
| 404 | /* |
| 405 | * Determine if we need to do extra work for this initiator node's |
| 406 | * struct se_node_acl if it had been previously dynamically generated. |
| 407 | */ |
| 408 | se_nacl = se_sess->se_node_acl; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 409 | if (se_nacl) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 410 | spin_lock_bh(&se_tpg->acl_node_lock); |
| 411 | if (se_nacl->dynamic_node_acl) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 412 | if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache( |
| 413 | se_tpg)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 414 | list_del(&se_nacl->acl_list); |
| 415 | se_tpg->num_node_acls--; |
| 416 | spin_unlock_bh(&se_tpg->acl_node_lock); |
| 417 | |
| 418 | core_tpg_wait_for_nacl_pr_ref(se_nacl); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 419 | core_free_device_list_for_node(se_nacl, se_tpg); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 420 | se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 421 | se_nacl); |
| 422 | spin_lock_bh(&se_tpg->acl_node_lock); |
| 423 | } |
| 424 | } |
| 425 | spin_unlock_bh(&se_tpg->acl_node_lock); |
| 426 | } |
| 427 | |
| 428 | transport_free_session(se_sess); |
| 429 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 430 | pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 431 | se_tpg->se_tpg_tfo->get_fabric_name()); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 432 | } |
| 433 | EXPORT_SYMBOL(transport_deregister_session); |
| 434 | |
| 435 | /* |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 436 | * Called with cmd->t_state_lock held. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 437 | */ |
| 438 | static void transport_all_task_dev_remove_state(struct se_cmd *cmd) |
| 439 | { |
| 440 | struct se_device *dev; |
| 441 | struct se_task *task; |
| 442 | unsigned long flags; |
| 443 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 444 | list_for_each_entry(task, &cmd->t_task_list, t_list) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 445 | dev = task->se_dev; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 446 | if (!dev) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 447 | continue; |
| 448 | |
| 449 | if (atomic_read(&task->task_active)) |
| 450 | continue; |
| 451 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 452 | if (!atomic_read(&task->task_state_active)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 453 | continue; |
| 454 | |
| 455 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
| 456 | list_del(&task->t_state_list); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 457 | pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n", |
| 458 | cmd->se_tfo->get_task_tag(cmd), dev, task); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 459 | spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
| 460 | |
| 461 | atomic_set(&task->task_state_active, 0); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 462 | atomic_dec(&cmd->t_task_cdbs_ex_left); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
| 466 | /* transport_cmd_check_stop(): |
| 467 | * |
| 468 | * 'transport_off = 1' determines if t_transport_active should be cleared. |
| 469 | * 'transport_off = 2' determines if task_dev_state should be removed. |
| 470 | * |
| 471 | * A non-zero u8 t_state sets cmd->t_state. |
| 472 | * Returns 1 when command is stopped, else 0. |
| 473 | */ |
| 474 | static int transport_cmd_check_stop( |
| 475 | struct se_cmd *cmd, |
| 476 | int transport_off, |
| 477 | u8 t_state) |
| 478 | { |
| 479 | unsigned long flags; |
| 480 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 481 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 482 | /* |
| 483 | * Determine if IOCTL context caller in requesting the stopping of this |
| 484 | * command for LUN shutdown purposes. |
| 485 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 486 | if (atomic_read(&cmd->transport_lun_stop)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 487 | pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 488 | " == TRUE for ITT: 0x%08x\n", __func__, __LINE__, |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 489 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 490 | |
| 491 | cmd->deferred_t_state = cmd->t_state; |
| 492 | cmd->t_state = TRANSPORT_DEFERRED_CMD; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 493 | atomic_set(&cmd->t_transport_active, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 494 | if (transport_off == 2) |
| 495 | transport_all_task_dev_remove_state(cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 496 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 497 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 498 | complete(&cmd->transport_lun_stop_comp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 499 | return 1; |
| 500 | } |
| 501 | /* |
| 502 | * Determine if frontend context caller is requesting the stopping of |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 503 | * this command for frontend exceptions. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 504 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 505 | if (atomic_read(&cmd->t_transport_stop)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 506 | pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) ==" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 507 | " TRUE for ITT: 0x%08x\n", __func__, __LINE__, |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 508 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 509 | |
| 510 | cmd->deferred_t_state = cmd->t_state; |
| 511 | cmd->t_state = TRANSPORT_DEFERRED_CMD; |
| 512 | if (transport_off == 2) |
| 513 | transport_all_task_dev_remove_state(cmd); |
| 514 | |
| 515 | /* |
| 516 | * Clear struct se_cmd->se_lun before the transport_off == 2 handoff |
| 517 | * to FE. |
| 518 | */ |
| 519 | if (transport_off == 2) |
| 520 | cmd->se_lun = NULL; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 521 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 522 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 523 | complete(&cmd->t_transport_stop_comp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 524 | return 1; |
| 525 | } |
| 526 | if (transport_off) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 527 | atomic_set(&cmd->t_transport_active, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 528 | if (transport_off == 2) { |
| 529 | transport_all_task_dev_remove_state(cmd); |
| 530 | /* |
| 531 | * Clear struct se_cmd->se_lun before the transport_off == 2 |
| 532 | * handoff to fabric module. |
| 533 | */ |
| 534 | cmd->se_lun = NULL; |
| 535 | /* |
| 536 | * Some fabric modules like tcm_loop can release |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 537 | * their internally allocated I/O reference now and |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 538 | * struct se_cmd now. |
| 539 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 540 | if (cmd->se_tfo->check_stop_free != NULL) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 541 | spin_unlock_irqrestore( |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 542 | &cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 543 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 544 | cmd->se_tfo->check_stop_free(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 545 | return 1; |
| 546 | } |
| 547 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 548 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 549 | |
| 550 | return 0; |
| 551 | } else if (t_state) |
| 552 | cmd->t_state = t_state; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 553 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd) |
| 559 | { |
| 560 | return transport_cmd_check_stop(cmd, 2, 0); |
| 561 | } |
| 562 | |
| 563 | static void transport_lun_remove_cmd(struct se_cmd *cmd) |
| 564 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 565 | struct se_lun *lun = cmd->se_lun; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 566 | unsigned long flags; |
| 567 | |
| 568 | if (!lun) |
| 569 | return; |
| 570 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 571 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 572 | if (!atomic_read(&cmd->transport_dev_active)) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 573 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 574 | goto check_lun; |
| 575 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 576 | atomic_set(&cmd->transport_dev_active, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 577 | transport_all_task_dev_remove_state(cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 578 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 579 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 580 | |
| 581 | check_lun: |
| 582 | spin_lock_irqsave(&lun->lun_cmd_lock, flags); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 583 | if (atomic_read(&cmd->transport_lun_active)) { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 584 | list_del(&cmd->se_lun_node); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 585 | atomic_set(&cmd->transport_lun_active, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 586 | #if 0 |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 587 | pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 588 | cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 589 | #endif |
| 590 | } |
| 591 | spin_unlock_irqrestore(&lun->lun_cmd_lock, flags); |
| 592 | } |
| 593 | |
| 594 | void transport_cmd_finish_abort(struct se_cmd *cmd, int remove) |
| 595 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 596 | transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 597 | transport_lun_remove_cmd(cmd); |
| 598 | |
| 599 | if (transport_cmd_check_stop_to_fabric(cmd)) |
| 600 | return; |
| 601 | if (remove) |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 602 | transport_generic_remove(cmd, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | void transport_cmd_finish_abort_tmr(struct se_cmd *cmd) |
| 606 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 607 | transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 608 | |
| 609 | if (transport_cmd_check_stop_to_fabric(cmd)) |
| 610 | return; |
| 611 | |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 612 | transport_generic_remove(cmd, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 615 | static void transport_add_cmd_to_queue( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 616 | struct se_cmd *cmd, |
| 617 | int t_state) |
| 618 | { |
| 619 | struct se_device *dev = cmd->se_dev; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 620 | struct se_queue_obj *qobj = &dev->dev_queue_obj; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 621 | unsigned long flags; |
| 622 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 623 | INIT_LIST_HEAD(&cmd->se_queue_node); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 624 | |
| 625 | if (t_state) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 626 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 627 | cmd->t_state = t_state; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 628 | atomic_set(&cmd->t_transport_active, 1); |
| 629 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | spin_lock_irqsave(&qobj->cmd_queue_lock, flags); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 633 | if (cmd->se_cmd_flags & SCF_EMULATE_QUEUE_FULL) { |
| 634 | cmd->se_cmd_flags &= ~SCF_EMULATE_QUEUE_FULL; |
| 635 | list_add(&cmd->se_queue_node, &qobj->qobj_list); |
| 636 | } else |
| 637 | list_add_tail(&cmd->se_queue_node, &qobj->qobj_list); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 638 | atomic_inc(&cmd->t_transport_queue_active); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 639 | spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags); |
| 640 | |
| 641 | atomic_inc(&qobj->queue_cnt); |
| 642 | wake_up_interruptible(&qobj->thread_wq); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 645 | static struct se_cmd * |
| 646 | transport_get_cmd_from_queue(struct se_queue_obj *qobj) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 647 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 648 | struct se_cmd *cmd; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 649 | unsigned long flags; |
| 650 | |
| 651 | spin_lock_irqsave(&qobj->cmd_queue_lock, flags); |
| 652 | if (list_empty(&qobj->qobj_list)) { |
| 653 | spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags); |
| 654 | return NULL; |
| 655 | } |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 656 | cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 657 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 658 | atomic_dec(&cmd->t_transport_queue_active); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 659 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 660 | list_del(&cmd->se_queue_node); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 661 | atomic_dec(&qobj->queue_cnt); |
| 662 | spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags); |
| 663 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 664 | return cmd; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static void transport_remove_cmd_from_queue(struct se_cmd *cmd, |
| 668 | struct se_queue_obj *qobj) |
| 669 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 670 | struct se_cmd *t; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 671 | unsigned long flags; |
| 672 | |
| 673 | spin_lock_irqsave(&qobj->cmd_queue_lock, flags); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 674 | if (!atomic_read(&cmd->t_transport_queue_active)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 675 | spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags); |
| 676 | return; |
| 677 | } |
| 678 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 679 | list_for_each_entry(t, &qobj->qobj_list, se_queue_node) |
| 680 | if (t == cmd) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 681 | atomic_dec(&cmd->t_transport_queue_active); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 682 | atomic_dec(&qobj->queue_cnt); |
| 683 | list_del(&cmd->se_queue_node); |
| 684 | break; |
| 685 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 686 | spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags); |
| 687 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 688 | if (atomic_read(&cmd->t_transport_queue_active)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 689 | pr_err("ITT: 0x%08x t_transport_queue_active: %d\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 690 | cmd->se_tfo->get_task_tag(cmd), |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 691 | atomic_read(&cmd->t_transport_queue_active)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * Completion function used by TCM subsystem plugins (such as FILEIO) |
| 697 | * for queueing up response from struct se_subsystem_api->do_task() |
| 698 | */ |
| 699 | void transport_complete_sync_cache(struct se_cmd *cmd, int good) |
| 700 | { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 701 | struct se_task *task = list_entry(cmd->t_task_list.next, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 702 | struct se_task, t_list); |
| 703 | |
| 704 | if (good) { |
| 705 | cmd->scsi_status = SAM_STAT_GOOD; |
| 706 | task->task_scsi_status = GOOD; |
| 707 | } else { |
| 708 | task->task_scsi_status = SAM_STAT_CHECK_CONDITION; |
| 709 | task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 710 | task->task_se_cmd->transport_error_status = |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 711 | PYX_TRANSPORT_ILLEGAL_REQUEST; |
| 712 | } |
| 713 | |
| 714 | transport_complete_task(task, good); |
| 715 | } |
| 716 | EXPORT_SYMBOL(transport_complete_sync_cache); |
| 717 | |
| 718 | /* transport_complete_task(): |
| 719 | * |
| 720 | * Called from interrupt and non interrupt context depending |
| 721 | * on the transport plugin. |
| 722 | */ |
| 723 | void transport_complete_task(struct se_task *task, int success) |
| 724 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 725 | struct se_cmd *cmd = task->task_se_cmd; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 726 | struct se_device *dev = task->se_dev; |
| 727 | int t_state; |
| 728 | unsigned long flags; |
| 729 | #if 0 |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 730 | pr_debug("task: %p CDB: 0x%02x obj_ptr: %p\n", task, |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 731 | cmd->t_task_cdb[0], dev); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 732 | #endif |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 733 | if (dev) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 734 | atomic_inc(&dev->depth_left); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 735 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 736 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 737 | atomic_set(&task->task_active, 0); |
| 738 | |
| 739 | /* |
| 740 | * See if any sense data exists, if so set the TASK_SENSE flag. |
| 741 | * Also check for any other post completion work that needs to be |
| 742 | * done by the plugins. |
| 743 | */ |
| 744 | if (dev && dev->transport->transport_complete) { |
| 745 | if (dev->transport->transport_complete(task) != 0) { |
| 746 | cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE; |
| 747 | task->task_sense = 1; |
| 748 | success = 1; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * See if we are waiting for outstanding struct se_task |
| 754 | * to complete for an exception condition |
| 755 | */ |
| 756 | if (atomic_read(&task->task_stop)) { |
| 757 | /* |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 758 | * Decrement cmd->t_se_count if this task had |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 759 | * previously thrown its timeout exception handler. |
| 760 | */ |
| 761 | if (atomic_read(&task->task_timeout)) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 762 | atomic_dec(&cmd->t_se_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 763 | atomic_set(&task->task_timeout, 0); |
| 764 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 765 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 766 | |
| 767 | complete(&task->task_stop_comp); |
| 768 | return; |
| 769 | } |
| 770 | /* |
| 771 | * If the task's timeout handler has fired, use the t_task_cdbs_timeout |
| 772 | * left counter to determine when the struct se_cmd is ready to be queued to |
| 773 | * the processing thread. |
| 774 | */ |
| 775 | if (atomic_read(&task->task_timeout)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 776 | if (!atomic_dec_and_test( |
| 777 | &cmd->t_task_cdbs_timeout_left)) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 778 | spin_unlock_irqrestore(&cmd->t_state_lock, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 779 | flags); |
| 780 | return; |
| 781 | } |
| 782 | t_state = TRANSPORT_COMPLETE_TIMEOUT; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 783 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 784 | |
| 785 | transport_add_cmd_to_queue(cmd, t_state); |
| 786 | return; |
| 787 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 788 | atomic_dec(&cmd->t_task_cdbs_timeout_left); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 789 | |
| 790 | /* |
| 791 | * Decrement the outstanding t_task_cdbs_left count. The last |
| 792 | * struct se_task from struct se_cmd will complete itself into the |
| 793 | * device queue depending upon int success. |
| 794 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 795 | if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 796 | if (!success) |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 797 | cmd->t_tasks_failed = 1; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 798 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 799 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 800 | return; |
| 801 | } |
| 802 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 803 | if (!success || cmd->t_tasks_failed) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 804 | t_state = TRANSPORT_COMPLETE_FAILURE; |
| 805 | if (!task->task_error_status) { |
| 806 | task->task_error_status = |
| 807 | PYX_TRANSPORT_UNKNOWN_SAM_OPCODE; |
| 808 | cmd->transport_error_status = |
| 809 | PYX_TRANSPORT_UNKNOWN_SAM_OPCODE; |
| 810 | } |
| 811 | } else { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 812 | atomic_set(&cmd->t_transport_complete, 1); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 813 | t_state = TRANSPORT_COMPLETE_OK; |
| 814 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 815 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 816 | |
| 817 | transport_add_cmd_to_queue(cmd, t_state); |
| 818 | } |
| 819 | EXPORT_SYMBOL(transport_complete_task); |
| 820 | |
| 821 | /* |
| 822 | * Called by transport_add_tasks_from_cmd() once a struct se_cmd's |
| 823 | * struct se_task list are ready to be added to the active execution list |
| 824 | * struct se_device |
| 825 | |
| 826 | * Called with se_dev_t->execute_task_lock called. |
| 827 | */ |
| 828 | static inline int transport_add_task_check_sam_attr( |
| 829 | struct se_task *task, |
| 830 | struct se_task *task_prev, |
| 831 | struct se_device *dev) |
| 832 | { |
| 833 | /* |
| 834 | * No SAM Task attribute emulation enabled, add to tail of |
| 835 | * execution queue |
| 836 | */ |
| 837 | if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) { |
| 838 | list_add_tail(&task->t_execute_list, &dev->execute_task_list); |
| 839 | return 0; |
| 840 | } |
| 841 | /* |
| 842 | * HEAD_OF_QUEUE attribute for received CDB, which means |
| 843 | * the first task that is associated with a struct se_cmd goes to |
| 844 | * head of the struct se_device->execute_task_list, and task_prev |
| 845 | * after that for each subsequent task |
| 846 | */ |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 847 | if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 848 | list_add(&task->t_execute_list, |
| 849 | (task_prev != NULL) ? |
| 850 | &task_prev->t_execute_list : |
| 851 | &dev->execute_task_list); |
| 852 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 853 | pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 854 | " in execution queue\n", |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 855 | task->task_se_cmd->t_task_cdb[0]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 856 | return 1; |
| 857 | } |
| 858 | /* |
| 859 | * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been |
| 860 | * transitioned from Dermant -> Active state, and are added to the end |
| 861 | * of the struct se_device->execute_task_list |
| 862 | */ |
| 863 | list_add_tail(&task->t_execute_list, &dev->execute_task_list); |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | /* __transport_add_task_to_execute_queue(): |
| 868 | * |
| 869 | * Called with se_dev_t->execute_task_lock called. |
| 870 | */ |
| 871 | static void __transport_add_task_to_execute_queue( |
| 872 | struct se_task *task, |
| 873 | struct se_task *task_prev, |
| 874 | struct se_device *dev) |
| 875 | { |
| 876 | int head_of_queue; |
| 877 | |
| 878 | head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev); |
| 879 | atomic_inc(&dev->execute_tasks); |
| 880 | |
| 881 | if (atomic_read(&task->task_state_active)) |
| 882 | return; |
| 883 | /* |
| 884 | * Determine if this task needs to go to HEAD_OF_QUEUE for the |
| 885 | * state list as well. Running with SAM Task Attribute emulation |
| 886 | * will always return head_of_queue == 0 here |
| 887 | */ |
| 888 | if (head_of_queue) |
| 889 | list_add(&task->t_state_list, (task_prev) ? |
| 890 | &task_prev->t_state_list : |
| 891 | &dev->state_task_list); |
| 892 | else |
| 893 | list_add_tail(&task->t_state_list, &dev->state_task_list); |
| 894 | |
| 895 | atomic_set(&task->task_state_active, 1); |
| 896 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 897 | pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 898 | task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 899 | task, dev); |
| 900 | } |
| 901 | |
| 902 | static void transport_add_tasks_to_state_queue(struct se_cmd *cmd) |
| 903 | { |
| 904 | struct se_device *dev; |
| 905 | struct se_task *task; |
| 906 | unsigned long flags; |
| 907 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 908 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 909 | list_for_each_entry(task, &cmd->t_task_list, t_list) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 910 | dev = task->se_dev; |
| 911 | |
| 912 | if (atomic_read(&task->task_state_active)) |
| 913 | continue; |
| 914 | |
| 915 | spin_lock(&dev->execute_task_lock); |
| 916 | list_add_tail(&task->t_state_list, &dev->state_task_list); |
| 917 | atomic_set(&task->task_state_active, 1); |
| 918 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 919 | pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n", |
| 920 | task->task_se_cmd->se_tfo->get_task_tag( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 921 | task->task_se_cmd), task, dev); |
| 922 | |
| 923 | spin_unlock(&dev->execute_task_lock); |
| 924 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 925 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | static void transport_add_tasks_from_cmd(struct se_cmd *cmd) |
| 929 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 930 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 931 | struct se_task *task, *task_prev = NULL; |
| 932 | unsigned long flags; |
| 933 | |
| 934 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 935 | list_for_each_entry(task, &cmd->t_task_list, t_list) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 936 | if (atomic_read(&task->task_execute_queue)) |
| 937 | continue; |
| 938 | /* |
| 939 | * __transport_add_task_to_execute_queue() handles the |
| 940 | * SAM Task Attribute emulation if enabled |
| 941 | */ |
| 942 | __transport_add_task_to_execute_queue(task, task_prev, dev); |
| 943 | atomic_set(&task->task_execute_queue, 1); |
| 944 | task_prev = task; |
| 945 | } |
| 946 | spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /* transport_remove_task_from_execute_queue(): |
| 950 | * |
| 951 | * |
| 952 | */ |
Nicholas Bellinger | 52208ae | 2011-02-24 16:58:20 -0800 | [diff] [blame] | 953 | void transport_remove_task_from_execute_queue( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 954 | struct se_task *task, |
| 955 | struct se_device *dev) |
| 956 | { |
| 957 | unsigned long flags; |
| 958 | |
Nicholas Bellinger | af57c3a | 2011-05-19 20:19:12 -0700 | [diff] [blame] | 959 | if (atomic_read(&task->task_execute_queue) == 0) { |
| 960 | dump_stack(); |
| 961 | return; |
| 962 | } |
| 963 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 964 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
| 965 | list_del(&task->t_execute_list); |
Nicholas Bellinger | af57c3a | 2011-05-19 20:19:12 -0700 | [diff] [blame] | 966 | atomic_set(&task->task_execute_queue, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 967 | atomic_dec(&dev->execute_tasks); |
| 968 | spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
| 969 | } |
| 970 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 971 | /* |
| 972 | * Handle QUEUE_FULL / -EAGAIN status |
| 973 | */ |
| 974 | |
| 975 | static void target_qf_do_work(struct work_struct *work) |
| 976 | { |
| 977 | struct se_device *dev = container_of(work, struct se_device, |
| 978 | qf_work_queue); |
| 979 | struct se_cmd *cmd, *cmd_tmp; |
| 980 | |
| 981 | spin_lock_irq(&dev->qf_cmd_lock); |
| 982 | list_for_each_entry_safe(cmd, cmd_tmp, &dev->qf_cmd_list, se_qf_node) { |
| 983 | |
| 984 | list_del(&cmd->se_qf_node); |
| 985 | atomic_dec(&dev->dev_qf_count); |
| 986 | smp_mb__after_atomic_dec(); |
| 987 | spin_unlock_irq(&dev->qf_cmd_lock); |
| 988 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 989 | pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue" |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 990 | " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd, |
| 991 | (cmd->t_state == TRANSPORT_COMPLETE_OK) ? "COMPLETE_OK" : |
| 992 | (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING" |
| 993 | : "UNKNOWN"); |
| 994 | /* |
| 995 | * The SCF_EMULATE_QUEUE_FULL flag will be cleared once se_cmd |
| 996 | * has been added to head of queue |
| 997 | */ |
| 998 | transport_add_cmd_to_queue(cmd, cmd->t_state); |
| 999 | |
| 1000 | spin_lock_irq(&dev->qf_cmd_lock); |
| 1001 | } |
| 1002 | spin_unlock_irq(&dev->qf_cmd_lock); |
| 1003 | } |
| 1004 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1005 | unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd) |
| 1006 | { |
| 1007 | switch (cmd->data_direction) { |
| 1008 | case DMA_NONE: |
| 1009 | return "NONE"; |
| 1010 | case DMA_FROM_DEVICE: |
| 1011 | return "READ"; |
| 1012 | case DMA_TO_DEVICE: |
| 1013 | return "WRITE"; |
| 1014 | case DMA_BIDIRECTIONAL: |
| 1015 | return "BIDI"; |
| 1016 | default: |
| 1017 | break; |
| 1018 | } |
| 1019 | |
| 1020 | return "UNKNOWN"; |
| 1021 | } |
| 1022 | |
| 1023 | void transport_dump_dev_state( |
| 1024 | struct se_device *dev, |
| 1025 | char *b, |
| 1026 | int *bl) |
| 1027 | { |
| 1028 | *bl += sprintf(b + *bl, "Status: "); |
| 1029 | switch (dev->dev_status) { |
| 1030 | case TRANSPORT_DEVICE_ACTIVATED: |
| 1031 | *bl += sprintf(b + *bl, "ACTIVATED"); |
| 1032 | break; |
| 1033 | case TRANSPORT_DEVICE_DEACTIVATED: |
| 1034 | *bl += sprintf(b + *bl, "DEACTIVATED"); |
| 1035 | break; |
| 1036 | case TRANSPORT_DEVICE_SHUTDOWN: |
| 1037 | *bl += sprintf(b + *bl, "SHUTDOWN"); |
| 1038 | break; |
| 1039 | case TRANSPORT_DEVICE_OFFLINE_ACTIVATED: |
| 1040 | case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED: |
| 1041 | *bl += sprintf(b + *bl, "OFFLINE"); |
| 1042 | break; |
| 1043 | default: |
| 1044 | *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status); |
| 1045 | break; |
| 1046 | } |
| 1047 | |
| 1048 | *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d", |
| 1049 | atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left), |
| 1050 | dev->queue_depth); |
| 1051 | *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1052 | dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1053 | *bl += sprintf(b + *bl, " "); |
| 1054 | } |
| 1055 | |
| 1056 | /* transport_release_all_cmds(): |
| 1057 | * |
| 1058 | * |
| 1059 | */ |
| 1060 | static void transport_release_all_cmds(struct se_device *dev) |
| 1061 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1062 | struct se_cmd *cmd, *tcmd; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1063 | int bug_out = 0, t_state; |
| 1064 | unsigned long flags; |
| 1065 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1066 | spin_lock_irqsave(&dev->dev_queue_obj.cmd_queue_lock, flags); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1067 | list_for_each_entry_safe(cmd, tcmd, &dev->dev_queue_obj.qobj_list, |
| 1068 | se_queue_node) { |
| 1069 | t_state = cmd->t_state; |
| 1070 | list_del(&cmd->se_queue_node); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1071 | spin_unlock_irqrestore(&dev->dev_queue_obj.cmd_queue_lock, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1072 | flags); |
| 1073 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1074 | pr_err("Releasing ITT: 0x%08x, i_state: %u," |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1075 | " t_state: %u directly\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1076 | cmd->se_tfo->get_task_tag(cmd), |
| 1077 | cmd->se_tfo->get_cmd_state(cmd), t_state); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1078 | |
| 1079 | transport_release_fe_cmd(cmd); |
| 1080 | bug_out = 1; |
| 1081 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1082 | spin_lock_irqsave(&dev->dev_queue_obj.cmd_queue_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1083 | } |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1084 | spin_unlock_irqrestore(&dev->dev_queue_obj.cmd_queue_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1085 | #if 0 |
| 1086 | if (bug_out) |
| 1087 | BUG(); |
| 1088 | #endif |
| 1089 | } |
| 1090 | |
| 1091 | void transport_dump_vpd_proto_id( |
| 1092 | struct t10_vpd *vpd, |
| 1093 | unsigned char *p_buf, |
| 1094 | int p_buf_len) |
| 1095 | { |
| 1096 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
| 1097 | int len; |
| 1098 | |
| 1099 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 1100 | len = sprintf(buf, "T10 VPD Protocol Identifier: "); |
| 1101 | |
| 1102 | switch (vpd->protocol_identifier) { |
| 1103 | case 0x00: |
| 1104 | sprintf(buf+len, "Fibre Channel\n"); |
| 1105 | break; |
| 1106 | case 0x10: |
| 1107 | sprintf(buf+len, "Parallel SCSI\n"); |
| 1108 | break; |
| 1109 | case 0x20: |
| 1110 | sprintf(buf+len, "SSA\n"); |
| 1111 | break; |
| 1112 | case 0x30: |
| 1113 | sprintf(buf+len, "IEEE 1394\n"); |
| 1114 | break; |
| 1115 | case 0x40: |
| 1116 | sprintf(buf+len, "SCSI Remote Direct Memory Access" |
| 1117 | " Protocol\n"); |
| 1118 | break; |
| 1119 | case 0x50: |
| 1120 | sprintf(buf+len, "Internet SCSI (iSCSI)\n"); |
| 1121 | break; |
| 1122 | case 0x60: |
| 1123 | sprintf(buf+len, "SAS Serial SCSI Protocol\n"); |
| 1124 | break; |
| 1125 | case 0x70: |
| 1126 | sprintf(buf+len, "Automation/Drive Interface Transport" |
| 1127 | " Protocol\n"); |
| 1128 | break; |
| 1129 | case 0x80: |
| 1130 | sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n"); |
| 1131 | break; |
| 1132 | default: |
| 1133 | sprintf(buf+len, "Unknown 0x%02x\n", |
| 1134 | vpd->protocol_identifier); |
| 1135 | break; |
| 1136 | } |
| 1137 | |
| 1138 | if (p_buf) |
| 1139 | strncpy(p_buf, buf, p_buf_len); |
| 1140 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1141 | pr_debug("%s", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | void |
| 1145 | transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83) |
| 1146 | { |
| 1147 | /* |
| 1148 | * Check if the Protocol Identifier Valid (PIV) bit is set.. |
| 1149 | * |
| 1150 | * from spc3r23.pdf section 7.5.1 |
| 1151 | */ |
| 1152 | if (page_83[1] & 0x80) { |
| 1153 | vpd->protocol_identifier = (page_83[0] & 0xf0); |
| 1154 | vpd->protocol_identifier_set = 1; |
| 1155 | transport_dump_vpd_proto_id(vpd, NULL, 0); |
| 1156 | } |
| 1157 | } |
| 1158 | EXPORT_SYMBOL(transport_set_vpd_proto_id); |
| 1159 | |
| 1160 | int transport_dump_vpd_assoc( |
| 1161 | struct t10_vpd *vpd, |
| 1162 | unsigned char *p_buf, |
| 1163 | int p_buf_len) |
| 1164 | { |
| 1165 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1166 | int ret = 0; |
| 1167 | int len; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1168 | |
| 1169 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 1170 | len = sprintf(buf, "T10 VPD Identifier Association: "); |
| 1171 | |
| 1172 | switch (vpd->association) { |
| 1173 | case 0x00: |
| 1174 | sprintf(buf+len, "addressed logical unit\n"); |
| 1175 | break; |
| 1176 | case 0x10: |
| 1177 | sprintf(buf+len, "target port\n"); |
| 1178 | break; |
| 1179 | case 0x20: |
| 1180 | sprintf(buf+len, "SCSI target device\n"); |
| 1181 | break; |
| 1182 | default: |
| 1183 | sprintf(buf+len, "Unknown 0x%02x\n", vpd->association); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1184 | ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1185 | break; |
| 1186 | } |
| 1187 | |
| 1188 | if (p_buf) |
| 1189 | strncpy(p_buf, buf, p_buf_len); |
| 1190 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1191 | pr_debug("%s", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1192 | |
| 1193 | return ret; |
| 1194 | } |
| 1195 | |
| 1196 | int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83) |
| 1197 | { |
| 1198 | /* |
| 1199 | * The VPD identification association.. |
| 1200 | * |
| 1201 | * from spc3r23.pdf Section 7.6.3.1 Table 297 |
| 1202 | */ |
| 1203 | vpd->association = (page_83[1] & 0x30); |
| 1204 | return transport_dump_vpd_assoc(vpd, NULL, 0); |
| 1205 | } |
| 1206 | EXPORT_SYMBOL(transport_set_vpd_assoc); |
| 1207 | |
| 1208 | int transport_dump_vpd_ident_type( |
| 1209 | struct t10_vpd *vpd, |
| 1210 | unsigned char *p_buf, |
| 1211 | int p_buf_len) |
| 1212 | { |
| 1213 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1214 | int ret = 0; |
| 1215 | int len; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1216 | |
| 1217 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 1218 | len = sprintf(buf, "T10 VPD Identifier Type: "); |
| 1219 | |
| 1220 | switch (vpd->device_identifier_type) { |
| 1221 | case 0x00: |
| 1222 | sprintf(buf+len, "Vendor specific\n"); |
| 1223 | break; |
| 1224 | case 0x01: |
| 1225 | sprintf(buf+len, "T10 Vendor ID based\n"); |
| 1226 | break; |
| 1227 | case 0x02: |
| 1228 | sprintf(buf+len, "EUI-64 based\n"); |
| 1229 | break; |
| 1230 | case 0x03: |
| 1231 | sprintf(buf+len, "NAA\n"); |
| 1232 | break; |
| 1233 | case 0x04: |
| 1234 | sprintf(buf+len, "Relative target port identifier\n"); |
| 1235 | break; |
| 1236 | case 0x08: |
| 1237 | sprintf(buf+len, "SCSI name string\n"); |
| 1238 | break; |
| 1239 | default: |
| 1240 | sprintf(buf+len, "Unsupported: 0x%02x\n", |
| 1241 | vpd->device_identifier_type); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1242 | ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1243 | break; |
| 1244 | } |
| 1245 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1246 | if (p_buf) { |
| 1247 | if (p_buf_len < strlen(buf)+1) |
| 1248 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1249 | strncpy(p_buf, buf, p_buf_len); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1250 | } else { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1251 | pr_debug("%s", buf); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1252 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1253 | |
| 1254 | return ret; |
| 1255 | } |
| 1256 | |
| 1257 | int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83) |
| 1258 | { |
| 1259 | /* |
| 1260 | * The VPD identifier type.. |
| 1261 | * |
| 1262 | * from spc3r23.pdf Section 7.6.3.1 Table 298 |
| 1263 | */ |
| 1264 | vpd->device_identifier_type = (page_83[1] & 0x0f); |
| 1265 | return transport_dump_vpd_ident_type(vpd, NULL, 0); |
| 1266 | } |
| 1267 | EXPORT_SYMBOL(transport_set_vpd_ident_type); |
| 1268 | |
| 1269 | int transport_dump_vpd_ident( |
| 1270 | struct t10_vpd *vpd, |
| 1271 | unsigned char *p_buf, |
| 1272 | int p_buf_len) |
| 1273 | { |
| 1274 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
| 1275 | int ret = 0; |
| 1276 | |
| 1277 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 1278 | |
| 1279 | switch (vpd->device_identifier_code_set) { |
| 1280 | case 0x01: /* Binary */ |
| 1281 | sprintf(buf, "T10 VPD Binary Device Identifier: %s\n", |
| 1282 | &vpd->device_identifier[0]); |
| 1283 | break; |
| 1284 | case 0x02: /* ASCII */ |
| 1285 | sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n", |
| 1286 | &vpd->device_identifier[0]); |
| 1287 | break; |
| 1288 | case 0x03: /* UTF-8 */ |
| 1289 | sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n", |
| 1290 | &vpd->device_identifier[0]); |
| 1291 | break; |
| 1292 | default: |
| 1293 | sprintf(buf, "T10 VPD Device Identifier encoding unsupported:" |
| 1294 | " 0x%02x", vpd->device_identifier_code_set); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1295 | ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1296 | break; |
| 1297 | } |
| 1298 | |
| 1299 | if (p_buf) |
| 1300 | strncpy(p_buf, buf, p_buf_len); |
| 1301 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1302 | pr_debug("%s", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1303 | |
| 1304 | return ret; |
| 1305 | } |
| 1306 | |
| 1307 | int |
| 1308 | transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83) |
| 1309 | { |
| 1310 | static const char hex_str[] = "0123456789abcdef"; |
| 1311 | int j = 0, i = 4; /* offset to start of the identifer */ |
| 1312 | |
| 1313 | /* |
| 1314 | * The VPD Code Set (encoding) |
| 1315 | * |
| 1316 | * from spc3r23.pdf Section 7.6.3.1 Table 296 |
| 1317 | */ |
| 1318 | vpd->device_identifier_code_set = (page_83[0] & 0x0f); |
| 1319 | switch (vpd->device_identifier_code_set) { |
| 1320 | case 0x01: /* Binary */ |
| 1321 | vpd->device_identifier[j++] = |
| 1322 | hex_str[vpd->device_identifier_type]; |
| 1323 | while (i < (4 + page_83[3])) { |
| 1324 | vpd->device_identifier[j++] = |
| 1325 | hex_str[(page_83[i] & 0xf0) >> 4]; |
| 1326 | vpd->device_identifier[j++] = |
| 1327 | hex_str[page_83[i] & 0x0f]; |
| 1328 | i++; |
| 1329 | } |
| 1330 | break; |
| 1331 | case 0x02: /* ASCII */ |
| 1332 | case 0x03: /* UTF-8 */ |
| 1333 | while (i < (4 + page_83[3])) |
| 1334 | vpd->device_identifier[j++] = page_83[i++]; |
| 1335 | break; |
| 1336 | default: |
| 1337 | break; |
| 1338 | } |
| 1339 | |
| 1340 | return transport_dump_vpd_ident(vpd, NULL, 0); |
| 1341 | } |
| 1342 | EXPORT_SYMBOL(transport_set_vpd_ident); |
| 1343 | |
| 1344 | static void core_setup_task_attr_emulation(struct se_device *dev) |
| 1345 | { |
| 1346 | /* |
| 1347 | * If this device is from Target_Core_Mod/pSCSI, disable the |
| 1348 | * SAM Task Attribute emulation. |
| 1349 | * |
| 1350 | * This is currently not available in upsream Linux/SCSI Target |
| 1351 | * mode code, and is assumed to be disabled while using TCM/pSCSI. |
| 1352 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1353 | if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1354 | dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH; |
| 1355 | return; |
| 1356 | } |
| 1357 | |
| 1358 | dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1359 | pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1360 | " device\n", dev->transport->name, |
| 1361 | dev->transport->get_device_rev(dev)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | static void scsi_dump_inquiry(struct se_device *dev) |
| 1365 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1366 | struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1367 | int i, device_type; |
| 1368 | /* |
| 1369 | * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer |
| 1370 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1371 | pr_debug(" Vendor: "); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1372 | for (i = 0; i < 8; i++) |
| 1373 | if (wwn->vendor[i] >= 0x20) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1374 | pr_debug("%c", wwn->vendor[i]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1375 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1376 | pr_debug(" "); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1377 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1378 | pr_debug(" Model: "); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1379 | for (i = 0; i < 16; i++) |
| 1380 | if (wwn->model[i] >= 0x20) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1381 | pr_debug("%c", wwn->model[i]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1382 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1383 | pr_debug(" "); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1384 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1385 | pr_debug(" Revision: "); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1386 | for (i = 0; i < 4; i++) |
| 1387 | if (wwn->revision[i] >= 0x20) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1388 | pr_debug("%c", wwn->revision[i]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1389 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1390 | pr_debug(" "); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1391 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1392 | pr_debug("\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1393 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1394 | device_type = dev->transport->get_device_type(dev); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1395 | pr_debug(" Type: %s ", scsi_device_type(device_type)); |
| 1396 | pr_debug(" ANSI SCSI revision: %02x\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1397 | dev->transport->get_device_rev(dev)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | struct se_device *transport_add_device_to_core_hba( |
| 1401 | struct se_hba *hba, |
| 1402 | struct se_subsystem_api *transport, |
| 1403 | struct se_subsystem_dev *se_dev, |
| 1404 | u32 device_flags, |
| 1405 | void *transport_dev, |
| 1406 | struct se_dev_limits *dev_limits, |
| 1407 | const char *inquiry_prod, |
| 1408 | const char *inquiry_rev) |
| 1409 | { |
Nicholas Bellinger | 12a18bd | 2011-03-14 04:06:06 -0700 | [diff] [blame] | 1410 | int force_pt; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1411 | struct se_device *dev; |
| 1412 | |
| 1413 | dev = kzalloc(sizeof(struct se_device), GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1414 | if (!dev) { |
| 1415 | pr_err("Unable to allocate memory for se_dev_t\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1416 | return NULL; |
| 1417 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1418 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1419 | transport_init_queue_obj(&dev->dev_queue_obj); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1420 | dev->dev_flags = device_flags; |
| 1421 | dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1422 | dev->dev_ptr = transport_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1423 | dev->se_hba = hba; |
| 1424 | dev->se_sub_dev = se_dev; |
| 1425 | dev->transport = transport; |
| 1426 | atomic_set(&dev->active_cmds, 0); |
| 1427 | INIT_LIST_HEAD(&dev->dev_list); |
| 1428 | INIT_LIST_HEAD(&dev->dev_sep_list); |
| 1429 | INIT_LIST_HEAD(&dev->dev_tmr_list); |
| 1430 | INIT_LIST_HEAD(&dev->execute_task_list); |
| 1431 | INIT_LIST_HEAD(&dev->delayed_cmd_list); |
| 1432 | INIT_LIST_HEAD(&dev->ordered_cmd_list); |
| 1433 | INIT_LIST_HEAD(&dev->state_task_list); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1434 | INIT_LIST_HEAD(&dev->qf_cmd_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1435 | spin_lock_init(&dev->execute_task_lock); |
| 1436 | spin_lock_init(&dev->delayed_cmd_lock); |
| 1437 | spin_lock_init(&dev->ordered_cmd_lock); |
| 1438 | spin_lock_init(&dev->state_task_lock); |
| 1439 | spin_lock_init(&dev->dev_alua_lock); |
| 1440 | spin_lock_init(&dev->dev_reservation_lock); |
| 1441 | spin_lock_init(&dev->dev_status_lock); |
| 1442 | spin_lock_init(&dev->dev_status_thr_lock); |
| 1443 | spin_lock_init(&dev->se_port_lock); |
| 1444 | spin_lock_init(&dev->se_tmr_lock); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1445 | spin_lock_init(&dev->qf_cmd_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1446 | |
| 1447 | dev->queue_depth = dev_limits->queue_depth; |
| 1448 | atomic_set(&dev->depth_left, dev->queue_depth); |
| 1449 | atomic_set(&dev->dev_ordered_id, 0); |
| 1450 | |
| 1451 | se_dev_set_default_attribs(dev, dev_limits); |
| 1452 | |
| 1453 | dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX); |
| 1454 | dev->creation_time = get_jiffies_64(); |
| 1455 | spin_lock_init(&dev->stats_lock); |
| 1456 | |
| 1457 | spin_lock(&hba->device_lock); |
| 1458 | list_add_tail(&dev->dev_list, &hba->hba_dev_list); |
| 1459 | hba->dev_count++; |
| 1460 | spin_unlock(&hba->device_lock); |
| 1461 | /* |
| 1462 | * Setup the SAM Task Attribute emulation for struct se_device |
| 1463 | */ |
| 1464 | core_setup_task_attr_emulation(dev); |
| 1465 | /* |
| 1466 | * Force PR and ALUA passthrough emulation with internal object use. |
| 1467 | */ |
| 1468 | force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE); |
| 1469 | /* |
| 1470 | * Setup the Reservations infrastructure for struct se_device |
| 1471 | */ |
| 1472 | core_setup_reservations(dev, force_pt); |
| 1473 | /* |
| 1474 | * Setup the Asymmetric Logical Unit Assignment for struct se_device |
| 1475 | */ |
| 1476 | if (core_setup_alua(dev, force_pt) < 0) |
| 1477 | goto out; |
| 1478 | |
| 1479 | /* |
| 1480 | * Startup the struct se_device processing thread |
| 1481 | */ |
| 1482 | dev->process_thread = kthread_run(transport_processing_thread, dev, |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1483 | "LIO_%s", dev->transport->name); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1484 | if (IS_ERR(dev->process_thread)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1485 | pr_err("Unable to create kthread: LIO_%s\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1486 | dev->transport->name); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1487 | goto out; |
| 1488 | } |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1489 | /* |
| 1490 | * Setup work_queue for QUEUE_FULL |
| 1491 | */ |
| 1492 | INIT_WORK(&dev->qf_work_queue, target_qf_do_work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1493 | /* |
| 1494 | * Preload the initial INQUIRY const values if we are doing |
| 1495 | * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI |
| 1496 | * passthrough because this is being provided by the backend LLD. |
| 1497 | * This is required so that transport_get_inquiry() copies these |
| 1498 | * originals once back into DEV_T10_WWN(dev) for the virtual device |
| 1499 | * setup. |
| 1500 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1501 | if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) { |
Roland Dreier | f22c119 | 2011-05-02 22:15:37 -0700 | [diff] [blame] | 1502 | if (!inquiry_prod || !inquiry_rev) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1503 | pr_err("All non TCM/pSCSI plugins require" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1504 | " INQUIRY consts\n"); |
| 1505 | goto out; |
| 1506 | } |
| 1507 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1508 | strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8); |
| 1509 | strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16); |
| 1510 | strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1511 | } |
| 1512 | scsi_dump_inquiry(dev); |
| 1513 | |
Nicholas Bellinger | 12a18bd | 2011-03-14 04:06:06 -0700 | [diff] [blame] | 1514 | return dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1515 | out: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1516 | kthread_stop(dev->process_thread); |
| 1517 | |
| 1518 | spin_lock(&hba->device_lock); |
| 1519 | list_del(&dev->dev_list); |
| 1520 | hba->dev_count--; |
| 1521 | spin_unlock(&hba->device_lock); |
| 1522 | |
| 1523 | se_release_vpd_for_dev(dev); |
| 1524 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1525 | kfree(dev); |
| 1526 | |
| 1527 | return NULL; |
| 1528 | } |
| 1529 | EXPORT_SYMBOL(transport_add_device_to_core_hba); |
| 1530 | |
| 1531 | /* transport_generic_prepare_cdb(): |
| 1532 | * |
| 1533 | * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will |
| 1534 | * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2. |
| 1535 | * The point of this is since we are mapping iSCSI LUNs to |
| 1536 | * SCSI Target IDs having a non-zero LUN in the CDB will throw the |
| 1537 | * devices and HBAs for a loop. |
| 1538 | */ |
| 1539 | static inline void transport_generic_prepare_cdb( |
| 1540 | unsigned char *cdb) |
| 1541 | { |
| 1542 | switch (cdb[0]) { |
| 1543 | case READ_10: /* SBC - RDProtect */ |
| 1544 | case READ_12: /* SBC - RDProtect */ |
| 1545 | case READ_16: /* SBC - RDProtect */ |
| 1546 | case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */ |
| 1547 | case VERIFY: /* SBC - VRProtect */ |
| 1548 | case VERIFY_16: /* SBC - VRProtect */ |
| 1549 | case WRITE_VERIFY: /* SBC - VRProtect */ |
| 1550 | case WRITE_VERIFY_12: /* SBC - VRProtect */ |
| 1551 | break; |
| 1552 | default: |
| 1553 | cdb[1] &= 0x1f; /* clear logical unit number */ |
| 1554 | break; |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | static struct se_task * |
| 1559 | transport_generic_get_task(struct se_cmd *cmd, |
| 1560 | enum dma_data_direction data_direction) |
| 1561 | { |
| 1562 | struct se_task *task; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1563 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1564 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1565 | task = dev->transport->alloc_task(cmd->t_task_cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1566 | if (!task) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1567 | pr_err("Unable to allocate struct se_task\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1568 | return NULL; |
| 1569 | } |
| 1570 | |
| 1571 | INIT_LIST_HEAD(&task->t_list); |
| 1572 | INIT_LIST_HEAD(&task->t_execute_list); |
| 1573 | INIT_LIST_HEAD(&task->t_state_list); |
| 1574 | init_completion(&task->task_stop_comp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1575 | task->task_se_cmd = cmd; |
| 1576 | task->se_dev = dev; |
| 1577 | task->task_data_direction = data_direction; |
| 1578 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1579 | return task; |
| 1580 | } |
| 1581 | |
| 1582 | static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *); |
| 1583 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1584 | /* |
| 1585 | * Used by fabric modules containing a local struct se_cmd within their |
| 1586 | * fabric dependent per I/O descriptor. |
| 1587 | */ |
| 1588 | void transport_init_se_cmd( |
| 1589 | struct se_cmd *cmd, |
| 1590 | struct target_core_fabric_ops *tfo, |
| 1591 | struct se_session *se_sess, |
| 1592 | u32 data_length, |
| 1593 | int data_direction, |
| 1594 | int task_attr, |
| 1595 | unsigned char *sense_buffer) |
| 1596 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1597 | INIT_LIST_HEAD(&cmd->se_lun_node); |
| 1598 | INIT_LIST_HEAD(&cmd->se_delayed_node); |
| 1599 | INIT_LIST_HEAD(&cmd->se_ordered_node); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1600 | INIT_LIST_HEAD(&cmd->se_qf_node); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1601 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1602 | INIT_LIST_HEAD(&cmd->t_task_list); |
| 1603 | init_completion(&cmd->transport_lun_fe_stop_comp); |
| 1604 | init_completion(&cmd->transport_lun_stop_comp); |
| 1605 | init_completion(&cmd->t_transport_stop_comp); |
| 1606 | spin_lock_init(&cmd->t_state_lock); |
| 1607 | atomic_set(&cmd->transport_dev_active, 1); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1608 | |
| 1609 | cmd->se_tfo = tfo; |
| 1610 | cmd->se_sess = se_sess; |
| 1611 | cmd->data_length = data_length; |
| 1612 | cmd->data_direction = data_direction; |
| 1613 | cmd->sam_task_attr = task_attr; |
| 1614 | cmd->sense_buffer = sense_buffer; |
| 1615 | } |
| 1616 | EXPORT_SYMBOL(transport_init_se_cmd); |
| 1617 | |
| 1618 | static int transport_check_alloc_task_attr(struct se_cmd *cmd) |
| 1619 | { |
| 1620 | /* |
| 1621 | * Check if SAM Task Attribute emulation is enabled for this |
| 1622 | * struct se_device storage object |
| 1623 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1624 | if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1625 | return 0; |
| 1626 | |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 1627 | if (cmd->sam_task_attr == MSG_ACA_TAG) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1628 | pr_debug("SAM Task Attribute ACA" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1629 | " emulation is not supported\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1630 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1631 | } |
| 1632 | /* |
| 1633 | * Used to determine when ORDERED commands should go from |
| 1634 | * Dormant to Active status. |
| 1635 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1636 | cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1637 | smp_mb__after_atomic_inc(); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1638 | pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1639 | cmd->se_ordered_id, cmd->sam_task_attr, |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1640 | cmd->se_dev->transport->name); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1641 | return 0; |
| 1642 | } |
| 1643 | |
| 1644 | void transport_free_se_cmd( |
| 1645 | struct se_cmd *se_cmd) |
| 1646 | { |
| 1647 | if (se_cmd->se_tmr_req) |
| 1648 | core_tmr_release_req(se_cmd->se_tmr_req); |
| 1649 | /* |
| 1650 | * Check and free any extended CDB buffer that was allocated |
| 1651 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1652 | if (se_cmd->t_task_cdb != se_cmd->__t_task_cdb) |
| 1653 | kfree(se_cmd->t_task_cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1654 | } |
| 1655 | EXPORT_SYMBOL(transport_free_se_cmd); |
| 1656 | |
| 1657 | static void transport_generic_wait_for_tasks(struct se_cmd *, int, int); |
| 1658 | |
| 1659 | /* transport_generic_allocate_tasks(): |
| 1660 | * |
| 1661 | * Called from fabric RX Thread. |
| 1662 | */ |
| 1663 | int transport_generic_allocate_tasks( |
| 1664 | struct se_cmd *cmd, |
| 1665 | unsigned char *cdb) |
| 1666 | { |
| 1667 | int ret; |
| 1668 | |
| 1669 | transport_generic_prepare_cdb(cdb); |
| 1670 | |
| 1671 | /* |
| 1672 | * This is needed for early exceptions. |
| 1673 | */ |
| 1674 | cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks; |
| 1675 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1676 | /* |
| 1677 | * Ensure that the received CDB is less than the max (252 + 8) bytes |
| 1678 | * for VARIABLE_LENGTH_CMD |
| 1679 | */ |
| 1680 | if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1681 | pr_err("Received SCSI CDB with command_size: %d that" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1682 | " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", |
| 1683 | scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1684 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1685 | } |
| 1686 | /* |
| 1687 | * If the received CDB is larger than TCM_MAX_COMMAND_SIZE, |
| 1688 | * allocate the additional extended CDB buffer now.. Otherwise |
| 1689 | * setup the pointer from __t_task_cdb to t_task_cdb. |
| 1690 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1691 | if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) { |
| 1692 | cmd->t_task_cdb = kzalloc(scsi_command_size(cdb), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1693 | GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1694 | if (!cmd->t_task_cdb) { |
| 1695 | pr_err("Unable to allocate cmd->t_task_cdb" |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1696 | " %u > sizeof(cmd->__t_task_cdb): %lu ops\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1697 | scsi_command_size(cdb), |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1698 | (unsigned long)sizeof(cmd->__t_task_cdb)); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1699 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1700 | } |
| 1701 | } else |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1702 | cmd->t_task_cdb = &cmd->__t_task_cdb[0]; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1703 | /* |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1704 | * Copy the original CDB into cmd-> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1705 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1706 | memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1707 | /* |
| 1708 | * Setup the received CDB based on SCSI defined opcodes and |
| 1709 | * perform unit attention, persistent reservations and ALUA |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1710 | * checks for virtual device backends. The cmd->t_task_cdb |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1711 | * pointer is expected to be setup before we reach this point. |
| 1712 | */ |
| 1713 | ret = transport_generic_cmd_sequencer(cmd, cdb); |
| 1714 | if (ret < 0) |
| 1715 | return ret; |
| 1716 | /* |
| 1717 | * Check for SAM Task Attribute Emulation |
| 1718 | */ |
| 1719 | if (transport_check_alloc_task_attr(cmd) < 0) { |
| 1720 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 1721 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1722 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1723 | } |
| 1724 | spin_lock(&cmd->se_lun->lun_sep_lock); |
| 1725 | if (cmd->se_lun->lun_sep) |
| 1726 | cmd->se_lun->lun_sep->sep_stats.cmd_pdus++; |
| 1727 | spin_unlock(&cmd->se_lun->lun_sep_lock); |
| 1728 | return 0; |
| 1729 | } |
| 1730 | EXPORT_SYMBOL(transport_generic_allocate_tasks); |
| 1731 | |
| 1732 | /* |
| 1733 | * Used by fabric module frontends not defining a TFO->new_cmd_map() |
| 1734 | * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD statis |
| 1735 | */ |
| 1736 | int transport_generic_handle_cdb( |
| 1737 | struct se_cmd *cmd) |
| 1738 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1739 | if (!cmd->se_lun) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1740 | dump_stack(); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1741 | pr_err("cmd->se_lun is NULL\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1742 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1743 | } |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1744 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1745 | transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD); |
| 1746 | return 0; |
| 1747 | } |
| 1748 | EXPORT_SYMBOL(transport_generic_handle_cdb); |
| 1749 | |
| 1750 | /* |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1751 | * Used by fabric module frontends to queue tasks directly. |
| 1752 | * Many only be used from process context only |
| 1753 | */ |
| 1754 | int transport_handle_cdb_direct( |
| 1755 | struct se_cmd *cmd) |
| 1756 | { |
| 1757 | if (!cmd->se_lun) { |
| 1758 | dump_stack(); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1759 | pr_err("cmd->se_lun is NULL\n"); |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1760 | return -EINVAL; |
| 1761 | } |
| 1762 | if (in_interrupt()) { |
| 1763 | dump_stack(); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1764 | pr_err("transport_generic_handle_cdb cannot be called" |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1765 | " from interrupt context\n"); |
| 1766 | return -EINVAL; |
| 1767 | } |
| 1768 | |
| 1769 | return transport_generic_new_cmd(cmd); |
| 1770 | } |
| 1771 | EXPORT_SYMBOL(transport_handle_cdb_direct); |
| 1772 | |
| 1773 | /* |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1774 | * Used by fabric module frontends defining a TFO->new_cmd_map() caller |
| 1775 | * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to |
| 1776 | * complete setup in TCM process context w/ TFO->new_cmd_map(). |
| 1777 | */ |
| 1778 | int transport_generic_handle_cdb_map( |
| 1779 | struct se_cmd *cmd) |
| 1780 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1781 | if (!cmd->se_lun) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1782 | dump_stack(); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1783 | pr_err("cmd->se_lun is NULL\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1784 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP); |
| 1788 | return 0; |
| 1789 | } |
| 1790 | EXPORT_SYMBOL(transport_generic_handle_cdb_map); |
| 1791 | |
| 1792 | /* transport_generic_handle_data(): |
| 1793 | * |
| 1794 | * |
| 1795 | */ |
| 1796 | int transport_generic_handle_data( |
| 1797 | struct se_cmd *cmd) |
| 1798 | { |
| 1799 | /* |
| 1800 | * For the software fabric case, then we assume the nexus is being |
| 1801 | * failed/shutdown when signals are pending from the kthread context |
| 1802 | * caller, so we return a failure. For the HW target mode case running |
| 1803 | * in interrupt code, the signal_pending() check is skipped. |
| 1804 | */ |
| 1805 | if (!in_interrupt() && signal_pending(current)) |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1806 | return -EPERM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1807 | /* |
| 1808 | * If the received CDB has aleady been ABORTED by the generic |
| 1809 | * target engine, we now call transport_check_aborted_status() |
| 1810 | * to queue any delated TASK_ABORTED status for the received CDB to the |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1811 | * fabric module as we are expecting no further incoming DATA OUT |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1812 | * sequences at this point. |
| 1813 | */ |
| 1814 | if (transport_check_aborted_status(cmd, 1) != 0) |
| 1815 | return 0; |
| 1816 | |
| 1817 | transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE); |
| 1818 | return 0; |
| 1819 | } |
| 1820 | EXPORT_SYMBOL(transport_generic_handle_data); |
| 1821 | |
| 1822 | /* transport_generic_handle_tmr(): |
| 1823 | * |
| 1824 | * |
| 1825 | */ |
| 1826 | int transport_generic_handle_tmr( |
| 1827 | struct se_cmd *cmd) |
| 1828 | { |
| 1829 | /* |
| 1830 | * This is needed for early exceptions. |
| 1831 | */ |
| 1832 | cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1833 | |
| 1834 | transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR); |
| 1835 | return 0; |
| 1836 | } |
| 1837 | EXPORT_SYMBOL(transport_generic_handle_tmr); |
| 1838 | |
Nicholas Bellinger | f436677 | 2011-05-19 20:19:11 -0700 | [diff] [blame] | 1839 | void transport_generic_free_cmd_intr( |
| 1840 | struct se_cmd *cmd) |
| 1841 | { |
| 1842 | transport_add_cmd_to_queue(cmd, TRANSPORT_FREE_CMD_INTR); |
| 1843 | } |
| 1844 | EXPORT_SYMBOL(transport_generic_free_cmd_intr); |
| 1845 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1846 | static int transport_stop_tasks_for_cmd(struct se_cmd *cmd) |
| 1847 | { |
| 1848 | struct se_task *task, *task_tmp; |
| 1849 | unsigned long flags; |
| 1850 | int ret = 0; |
| 1851 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1852 | pr_debug("ITT[0x%08x] - Stopping tasks\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1853 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1854 | |
| 1855 | /* |
| 1856 | * No tasks remain in the execution queue |
| 1857 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1858 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1859 | list_for_each_entry_safe(task, task_tmp, |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1860 | &cmd->t_task_list, t_list) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1861 | pr_debug("task_no[%d] - Processing task %p\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1862 | task->task_no, task); |
| 1863 | /* |
| 1864 | * If the struct se_task has not been sent and is not active, |
| 1865 | * remove the struct se_task from the execution queue. |
| 1866 | */ |
| 1867 | if (!atomic_read(&task->task_sent) && |
| 1868 | !atomic_read(&task->task_active)) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1869 | spin_unlock_irqrestore(&cmd->t_state_lock, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1870 | flags); |
| 1871 | transport_remove_task_from_execute_queue(task, |
| 1872 | task->se_dev); |
| 1873 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1874 | pr_debug("task_no[%d] - Removed from execute queue\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1875 | task->task_no); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1876 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1877 | continue; |
| 1878 | } |
| 1879 | |
| 1880 | /* |
| 1881 | * If the struct se_task is active, sleep until it is returned |
| 1882 | * from the plugin. |
| 1883 | */ |
| 1884 | if (atomic_read(&task->task_active)) { |
| 1885 | atomic_set(&task->task_stop, 1); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1886 | spin_unlock_irqrestore(&cmd->t_state_lock, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1887 | flags); |
| 1888 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1889 | pr_debug("task_no[%d] - Waiting to complete\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1890 | task->task_no); |
| 1891 | wait_for_completion(&task->task_stop_comp); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1892 | pr_debug("task_no[%d] - Stopped successfully\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1893 | task->task_no); |
| 1894 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1895 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 1896 | atomic_dec(&cmd->t_task_cdbs_left); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1897 | |
| 1898 | atomic_set(&task->task_active, 0); |
| 1899 | atomic_set(&task->task_stop, 0); |
| 1900 | } else { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1901 | pr_debug("task_no[%d] - Did nothing\n", task->task_no); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1902 | ret++; |
| 1903 | } |
| 1904 | |
| 1905 | __transport_stop_task_timer(task, &flags); |
| 1906 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1907 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1908 | |
| 1909 | return ret; |
| 1910 | } |
| 1911 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1912 | /* |
| 1913 | * Handle SAM-esque emulation for generic transport request failures. |
| 1914 | */ |
| 1915 | static void transport_generic_request_failure( |
| 1916 | struct se_cmd *cmd, |
| 1917 | struct se_device *dev, |
| 1918 | int complete, |
| 1919 | int sc) |
| 1920 | { |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1921 | int ret = 0; |
| 1922 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1923 | pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1924 | " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd), |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1925 | cmd->t_task_cdb[0]); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1926 | pr_debug("-----[ i_state: %d t_state/def_t_state:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1927 | " %d/%d transport_error_status: %d\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1928 | cmd->se_tfo->get_cmd_state(cmd), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1929 | cmd->t_state, cmd->deferred_t_state, |
| 1930 | cmd->transport_error_status); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1931 | pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1932 | " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --" |
| 1933 | " t_transport_active: %d t_transport_stop: %d" |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 1934 | " t_transport_sent: %d\n", cmd->t_task_list_num, |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1935 | atomic_read(&cmd->t_task_cdbs_left), |
| 1936 | atomic_read(&cmd->t_task_cdbs_sent), |
| 1937 | atomic_read(&cmd->t_task_cdbs_ex_left), |
| 1938 | atomic_read(&cmd->t_transport_active), |
| 1939 | atomic_read(&cmd->t_transport_stop), |
| 1940 | atomic_read(&cmd->t_transport_sent)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1941 | |
| 1942 | transport_stop_all_task_timers(cmd); |
| 1943 | |
| 1944 | if (dev) |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1945 | atomic_inc(&dev->depth_left); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1946 | /* |
| 1947 | * For SAM Task Attribute emulation for failed struct se_cmd |
| 1948 | */ |
| 1949 | if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED) |
| 1950 | transport_complete_task_attr(cmd); |
| 1951 | |
| 1952 | if (complete) { |
| 1953 | transport_direct_request_timeout(cmd); |
| 1954 | cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE; |
| 1955 | } |
| 1956 | |
| 1957 | switch (cmd->transport_error_status) { |
| 1958 | case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE: |
| 1959 | cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; |
| 1960 | break; |
| 1961 | case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS: |
| 1962 | cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY; |
| 1963 | break; |
| 1964 | case PYX_TRANSPORT_INVALID_CDB_FIELD: |
| 1965 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
| 1966 | break; |
| 1967 | case PYX_TRANSPORT_INVALID_PARAMETER_LIST: |
| 1968 | cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST; |
| 1969 | break; |
| 1970 | case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES: |
| 1971 | if (!sc) |
| 1972 | transport_new_cmd_failure(cmd); |
| 1973 | /* |
| 1974 | * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES, |
| 1975 | * we force this session to fall back to session |
| 1976 | * recovery. |
| 1977 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1978 | cmd->se_tfo->fall_back_to_erl0(cmd->se_sess); |
| 1979 | cmd->se_tfo->stop_session(cmd->se_sess, 0, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1980 | |
| 1981 | goto check_stop; |
| 1982 | case PYX_TRANSPORT_LU_COMM_FAILURE: |
| 1983 | case PYX_TRANSPORT_ILLEGAL_REQUEST: |
| 1984 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 1985 | break; |
| 1986 | case PYX_TRANSPORT_UNKNOWN_MODE_PAGE: |
| 1987 | cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE; |
| 1988 | break; |
| 1989 | case PYX_TRANSPORT_WRITE_PROTECTED: |
| 1990 | cmd->scsi_sense_reason = TCM_WRITE_PROTECTED; |
| 1991 | break; |
| 1992 | case PYX_TRANSPORT_RESERVATION_CONFLICT: |
| 1993 | /* |
| 1994 | * No SENSE Data payload for this case, set SCSI Status |
| 1995 | * and queue the response to $FABRIC_MOD. |
| 1996 | * |
| 1997 | * Uses linux/include/scsi/scsi.h SAM status codes defs |
| 1998 | */ |
| 1999 | cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; |
| 2000 | /* |
| 2001 | * For UA Interlock Code 11b, a RESERVATION CONFLICT will |
| 2002 | * establish a UNIT ATTENTION with PREVIOUS RESERVATION |
| 2003 | * CONFLICT STATUS. |
| 2004 | * |
| 2005 | * See spc4r17, section 7.4.6 Control Mode Page, Table 349 |
| 2006 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2007 | if (cmd->se_sess && |
| 2008 | cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2) |
| 2009 | core_scsi3_ua_allocate(cmd->se_sess->se_node_acl, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2010 | cmd->orig_fe_lun, 0x2C, |
| 2011 | ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS); |
| 2012 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2013 | ret = cmd->se_tfo->queue_status(cmd); |
| 2014 | if (ret == -EAGAIN) |
| 2015 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2016 | goto check_stop; |
| 2017 | case PYX_TRANSPORT_USE_SENSE_REASON: |
| 2018 | /* |
| 2019 | * struct se_cmd->scsi_sense_reason already set |
| 2020 | */ |
| 2021 | break; |
| 2022 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2023 | pr_err("Unknown transport error for CDB 0x%02x: %d\n", |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2024 | cmd->t_task_cdb[0], |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2025 | cmd->transport_error_status); |
| 2026 | cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; |
| 2027 | break; |
| 2028 | } |
| 2029 | |
| 2030 | if (!sc) |
| 2031 | transport_new_cmd_failure(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2032 | else { |
| 2033 | ret = transport_send_check_condition_and_sense(cmd, |
| 2034 | cmd->scsi_sense_reason, 0); |
| 2035 | if (ret == -EAGAIN) |
| 2036 | goto queue_full; |
| 2037 | } |
| 2038 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2039 | check_stop: |
| 2040 | transport_lun_remove_cmd(cmd); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2041 | if (!transport_cmd_check_stop_to_fabric(cmd)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2042 | ; |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2043 | return; |
| 2044 | |
| 2045 | queue_full: |
| 2046 | cmd->t_state = TRANSPORT_COMPLETE_OK; |
| 2047 | transport_handle_queue_full(cmd, cmd->se_dev, transport_complete_qf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | static void transport_direct_request_timeout(struct se_cmd *cmd) |
| 2051 | { |
| 2052 | unsigned long flags; |
| 2053 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2054 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2055 | if (!atomic_read(&cmd->t_transport_timeout)) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2056 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2057 | return; |
| 2058 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2059 | if (atomic_read(&cmd->t_task_cdbs_timeout_left)) { |
| 2060 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2061 | return; |
| 2062 | } |
| 2063 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2064 | atomic_sub(atomic_read(&cmd->t_transport_timeout), |
| 2065 | &cmd->t_se_count); |
| 2066 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | static void transport_generic_request_timeout(struct se_cmd *cmd) |
| 2070 | { |
| 2071 | unsigned long flags; |
| 2072 | |
| 2073 | /* |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2074 | * Reset cmd->t_se_count to allow transport_generic_remove() |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2075 | * to allow last call to free memory resources. |
| 2076 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2077 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 2078 | if (atomic_read(&cmd->t_transport_timeout) > 1) { |
| 2079 | int tmp = (atomic_read(&cmd->t_transport_timeout) - 1); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2080 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2081 | atomic_sub(tmp, &cmd->t_se_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2082 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2083 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2084 | |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 2085 | transport_generic_remove(cmd, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2086 | } |
| 2087 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2088 | static inline u32 transport_lba_21(unsigned char *cdb) |
| 2089 | { |
| 2090 | return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3]; |
| 2091 | } |
| 2092 | |
| 2093 | static inline u32 transport_lba_32(unsigned char *cdb) |
| 2094 | { |
| 2095 | return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; |
| 2096 | } |
| 2097 | |
| 2098 | static inline unsigned long long transport_lba_64(unsigned char *cdb) |
| 2099 | { |
| 2100 | unsigned int __v1, __v2; |
| 2101 | |
| 2102 | __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5]; |
| 2103 | __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; |
| 2104 | |
| 2105 | return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; |
| 2106 | } |
| 2107 | |
| 2108 | /* |
| 2109 | * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs |
| 2110 | */ |
| 2111 | static inline unsigned long long transport_lba_64_ext(unsigned char *cdb) |
| 2112 | { |
| 2113 | unsigned int __v1, __v2; |
| 2114 | |
| 2115 | __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15]; |
| 2116 | __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19]; |
| 2117 | |
| 2118 | return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32; |
| 2119 | } |
| 2120 | |
| 2121 | static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd) |
| 2122 | { |
| 2123 | unsigned long flags; |
| 2124 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2125 | spin_lock_irqsave(&se_cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2126 | se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2127 | spin_unlock_irqrestore(&se_cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | /* |
| 2131 | * Called from interrupt context. |
| 2132 | */ |
| 2133 | static void transport_task_timeout_handler(unsigned long data) |
| 2134 | { |
| 2135 | struct se_task *task = (struct se_task *)data; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2136 | struct se_cmd *cmd = task->task_se_cmd; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2137 | unsigned long flags; |
| 2138 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2139 | pr_debug("transport task timeout fired! task: %p cmd: %p\n", task, cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2140 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2141 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2142 | if (task->task_flags & TF_STOP) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2143 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2144 | return; |
| 2145 | } |
| 2146 | task->task_flags &= ~TF_RUNNING; |
| 2147 | |
| 2148 | /* |
| 2149 | * Determine if transport_complete_task() has already been called. |
| 2150 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2151 | if (!atomic_read(&task->task_active)) { |
| 2152 | pr_debug("transport task: %p cmd: %p timeout task_active" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2153 | " == 0\n", task, cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2154 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2155 | return; |
| 2156 | } |
| 2157 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2158 | atomic_inc(&cmd->t_se_count); |
| 2159 | atomic_inc(&cmd->t_transport_timeout); |
| 2160 | cmd->t_tasks_failed = 1; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2161 | |
| 2162 | atomic_set(&task->task_timeout, 1); |
| 2163 | task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT; |
| 2164 | task->task_scsi_status = 1; |
| 2165 | |
| 2166 | if (atomic_read(&task->task_stop)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2167 | pr_debug("transport task: %p cmd: %p timeout task_stop" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2168 | " == 1\n", task, cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2169 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2170 | complete(&task->task_stop_comp); |
| 2171 | return; |
| 2172 | } |
| 2173 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2174 | if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) { |
| 2175 | pr_debug("transport task: %p cmd: %p timeout non zero" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2176 | " t_task_cdbs_left\n", task, cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2177 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2178 | return; |
| 2179 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2180 | pr_debug("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2181 | task, cmd); |
| 2182 | |
| 2183 | cmd->t_state = TRANSPORT_COMPLETE_FAILURE; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2184 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2185 | |
| 2186 | transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE); |
| 2187 | } |
| 2188 | |
| 2189 | /* |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2190 | * Called with cmd->t_state_lock held. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2191 | */ |
| 2192 | static void transport_start_task_timer(struct se_task *task) |
| 2193 | { |
| 2194 | struct se_device *dev = task->se_dev; |
| 2195 | int timeout; |
| 2196 | |
| 2197 | if (task->task_flags & TF_RUNNING) |
| 2198 | return; |
| 2199 | /* |
| 2200 | * If the task_timeout is disabled, exit now. |
| 2201 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2202 | timeout = dev->se_sub_dev->se_dev_attrib.task_timeout; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2203 | if (!timeout) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2204 | return; |
| 2205 | |
| 2206 | init_timer(&task->task_timer); |
| 2207 | task->task_timer.expires = (get_jiffies_64() + timeout * HZ); |
| 2208 | task->task_timer.data = (unsigned long) task; |
| 2209 | task->task_timer.function = transport_task_timeout_handler; |
| 2210 | |
| 2211 | task->task_flags |= TF_RUNNING; |
| 2212 | add_timer(&task->task_timer); |
| 2213 | #if 0 |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2214 | pr_debug("Starting task timer for cmd: %p task: %p seconds:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2215 | " %d\n", task->task_se_cmd, task, timeout); |
| 2216 | #endif |
| 2217 | } |
| 2218 | |
| 2219 | /* |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2220 | * Called with spin_lock_irq(&cmd->t_state_lock) held. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2221 | */ |
| 2222 | void __transport_stop_task_timer(struct se_task *task, unsigned long *flags) |
| 2223 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2224 | struct se_cmd *cmd = task->task_se_cmd; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2225 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2226 | if (!task->task_flags & TF_RUNNING) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2227 | return; |
| 2228 | |
| 2229 | task->task_flags |= TF_STOP; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2230 | spin_unlock_irqrestore(&cmd->t_state_lock, *flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2231 | |
| 2232 | del_timer_sync(&task->task_timer); |
| 2233 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2234 | spin_lock_irqsave(&cmd->t_state_lock, *flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2235 | task->task_flags &= ~TF_RUNNING; |
| 2236 | task->task_flags &= ~TF_STOP; |
| 2237 | } |
| 2238 | |
| 2239 | static void transport_stop_all_task_timers(struct se_cmd *cmd) |
| 2240 | { |
| 2241 | struct se_task *task = NULL, *task_tmp; |
| 2242 | unsigned long flags; |
| 2243 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2244 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2245 | list_for_each_entry_safe(task, task_tmp, |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2246 | &cmd->t_task_list, t_list) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2247 | __transport_stop_task_timer(task, &flags); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2248 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | static inline int transport_tcq_window_closed(struct se_device *dev) |
| 2252 | { |
| 2253 | if (dev->dev_tcq_window_closed++ < |
| 2254 | PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) { |
| 2255 | msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT); |
| 2256 | } else |
| 2257 | msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG); |
| 2258 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2259 | wake_up_interruptible(&dev->dev_queue_obj.thread_wq); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2260 | return 0; |
| 2261 | } |
| 2262 | |
| 2263 | /* |
| 2264 | * Called from Fabric Module context from transport_execute_tasks() |
| 2265 | * |
| 2266 | * The return of this function determins if the tasks from struct se_cmd |
| 2267 | * get added to the execution queue in transport_execute_tasks(), |
| 2268 | * or are added to the delayed or ordered lists here. |
| 2269 | */ |
| 2270 | static inline int transport_execute_task_attr(struct se_cmd *cmd) |
| 2271 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2272 | if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2273 | return 1; |
| 2274 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2275 | * Check for the existence of HEAD_OF_QUEUE, and if true return 1 |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2276 | * to allow the passed struct se_cmd list of tasks to the front of the list. |
| 2277 | */ |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 2278 | if (cmd->sam_task_attr == MSG_HEAD_TAG) { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2279 | atomic_inc(&cmd->se_dev->dev_hoq_count); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2280 | smp_mb__after_atomic_inc(); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2281 | pr_debug("Added HEAD_OF_QUEUE for CDB:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2282 | " 0x%02x, se_ordered_id: %u\n", |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2283 | cmd->t_task_cdb[0], |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2284 | cmd->se_ordered_id); |
| 2285 | return 1; |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 2286 | } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2287 | spin_lock(&cmd->se_dev->ordered_cmd_lock); |
| 2288 | list_add_tail(&cmd->se_ordered_node, |
| 2289 | &cmd->se_dev->ordered_cmd_list); |
| 2290 | spin_unlock(&cmd->se_dev->ordered_cmd_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2291 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2292 | atomic_inc(&cmd->se_dev->dev_ordered_sync); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2293 | smp_mb__after_atomic_inc(); |
| 2294 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2295 | pr_debug("Added ORDERED for CDB: 0x%02x to ordered" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2296 | " list, se_ordered_id: %u\n", |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2297 | cmd->t_task_cdb[0], |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2298 | cmd->se_ordered_id); |
| 2299 | /* |
| 2300 | * Add ORDERED command to tail of execution queue if |
| 2301 | * no other older commands exist that need to be |
| 2302 | * completed first. |
| 2303 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2304 | if (!atomic_read(&cmd->se_dev->simple_cmds)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2305 | return 1; |
| 2306 | } else { |
| 2307 | /* |
| 2308 | * For SIMPLE and UNTAGGED Task Attribute commands |
| 2309 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2310 | atomic_inc(&cmd->se_dev->simple_cmds); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2311 | smp_mb__after_atomic_inc(); |
| 2312 | } |
| 2313 | /* |
| 2314 | * Otherwise if one or more outstanding ORDERED task attribute exist, |
| 2315 | * add the dormant task(s) built for the passed struct se_cmd to the |
| 2316 | * execution queue and become in Active state for this struct se_device. |
| 2317 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2318 | if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2319 | /* |
| 2320 | * Otherwise, add cmd w/ tasks to delayed cmd queue that |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2321 | * will be drained upon completion of HEAD_OF_QUEUE task. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2322 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2323 | spin_lock(&cmd->se_dev->delayed_cmd_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2324 | cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2325 | list_add_tail(&cmd->se_delayed_node, |
| 2326 | &cmd->se_dev->delayed_cmd_list); |
| 2327 | spin_unlock(&cmd->se_dev->delayed_cmd_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2328 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2329 | pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2330 | " delayed CMD list, se_ordered_id: %u\n", |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2331 | cmd->t_task_cdb[0], cmd->sam_task_attr, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2332 | cmd->se_ordered_id); |
| 2333 | /* |
| 2334 | * Return zero to let transport_execute_tasks() know |
| 2335 | * not to add the delayed tasks to the execution list. |
| 2336 | */ |
| 2337 | return 0; |
| 2338 | } |
| 2339 | /* |
| 2340 | * Otherwise, no ORDERED task attributes exist.. |
| 2341 | */ |
| 2342 | return 1; |
| 2343 | } |
| 2344 | |
| 2345 | /* |
| 2346 | * Called from fabric module context in transport_generic_new_cmd() and |
| 2347 | * transport_generic_process_write() |
| 2348 | */ |
| 2349 | static int transport_execute_tasks(struct se_cmd *cmd) |
| 2350 | { |
| 2351 | int add_tasks; |
| 2352 | |
Christoph Hellwig | db1620a | 2011-05-31 17:06:43 -0400 | [diff] [blame] | 2353 | if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) { |
| 2354 | cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE; |
| 2355 | transport_generic_request_failure(cmd, NULL, 0, 1); |
| 2356 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2357 | } |
Christoph Hellwig | db1620a | 2011-05-31 17:06:43 -0400 | [diff] [blame] | 2358 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2359 | /* |
| 2360 | * Call transport_cmd_check_stop() to see if a fabric exception |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2361 | * has occurred that prevents execution. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2362 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2363 | if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2364 | /* |
| 2365 | * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE |
| 2366 | * attribute for the tasks of the received struct se_cmd CDB |
| 2367 | */ |
| 2368 | add_tasks = transport_execute_task_attr(cmd); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2369 | if (!add_tasks) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2370 | goto execute_tasks; |
| 2371 | /* |
| 2372 | * This calls transport_add_tasks_from_cmd() to handle |
| 2373 | * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation |
| 2374 | * (if enabled) in __transport_add_task_to_execute_queue() and |
| 2375 | * transport_add_task_check_sam_attr(). |
| 2376 | */ |
| 2377 | transport_add_tasks_from_cmd(cmd); |
| 2378 | } |
| 2379 | /* |
| 2380 | * Kick the execution queue for the cmd associated struct se_device |
| 2381 | * storage object. |
| 2382 | */ |
| 2383 | execute_tasks: |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2384 | __transport_execute_tasks(cmd->se_dev); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2385 | return 0; |
| 2386 | } |
| 2387 | |
| 2388 | /* |
| 2389 | * Called to check struct se_device tcq depth window, and once open pull struct se_task |
| 2390 | * from struct se_device->execute_task_list and |
| 2391 | * |
| 2392 | * Called from transport_processing_thread() |
| 2393 | */ |
| 2394 | static int __transport_execute_tasks(struct se_device *dev) |
| 2395 | { |
| 2396 | int error; |
| 2397 | struct se_cmd *cmd = NULL; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2398 | struct se_task *task = NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2399 | unsigned long flags; |
| 2400 | |
| 2401 | /* |
| 2402 | * Check if there is enough room in the device and HBA queue to send |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2403 | * struct se_tasks to the selected transport. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2404 | */ |
| 2405 | check_depth: |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2406 | if (!atomic_read(&dev->depth_left)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2407 | return transport_tcq_window_closed(dev); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2408 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2409 | dev->dev_tcq_window_closed = 0; |
| 2410 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2411 | spin_lock_irq(&dev->execute_task_lock); |
| 2412 | if (list_empty(&dev->execute_task_list)) { |
| 2413 | spin_unlock_irq(&dev->execute_task_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2414 | return 0; |
| 2415 | } |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2416 | task = list_first_entry(&dev->execute_task_list, |
| 2417 | struct se_task, t_execute_list); |
| 2418 | list_del(&task->t_execute_list); |
| 2419 | atomic_set(&task->task_execute_queue, 0); |
| 2420 | atomic_dec(&dev->execute_tasks); |
| 2421 | spin_unlock_irq(&dev->execute_task_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2422 | |
| 2423 | atomic_dec(&dev->depth_left); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2424 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2425 | cmd = task->task_se_cmd; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2426 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2427 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2428 | atomic_set(&task->task_active, 1); |
| 2429 | atomic_set(&task->task_sent, 1); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2430 | atomic_inc(&cmd->t_task_cdbs_sent); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2431 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2432 | if (atomic_read(&cmd->t_task_cdbs_sent) == |
| 2433 | cmd->t_task_list_num) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2434 | atomic_set(&cmd->transport_sent, 1); |
| 2435 | |
| 2436 | transport_start_task_timer(task); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2437 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2438 | /* |
| 2439 | * The struct se_cmd->transport_emulate_cdb() function pointer is used |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2440 | * to grab REPORT_LUNS and other CDBs we want to handle before they hit the |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2441 | * struct se_subsystem_api->do_task() caller below. |
| 2442 | */ |
| 2443 | if (cmd->transport_emulate_cdb) { |
| 2444 | error = cmd->transport_emulate_cdb(cmd); |
| 2445 | if (error != 0) { |
| 2446 | cmd->transport_error_status = error; |
| 2447 | atomic_set(&task->task_active, 0); |
| 2448 | atomic_set(&cmd->transport_sent, 0); |
| 2449 | transport_stop_tasks_for_cmd(cmd); |
| 2450 | transport_generic_request_failure(cmd, dev, 0, 1); |
| 2451 | goto check_depth; |
| 2452 | } |
| 2453 | /* |
| 2454 | * Handle the successful completion for transport_emulate_cdb() |
| 2455 | * for synchronous operation, following SCF_EMULATE_CDB_ASYNC |
| 2456 | * Otherwise the caller is expected to complete the task with |
| 2457 | * proper status. |
| 2458 | */ |
| 2459 | if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) { |
| 2460 | cmd->scsi_status = SAM_STAT_GOOD; |
| 2461 | task->task_scsi_status = GOOD; |
| 2462 | transport_complete_task(task, 1); |
| 2463 | } |
| 2464 | } else { |
| 2465 | /* |
| 2466 | * Currently for all virtual TCM plugins including IBLOCK, FILEIO and |
| 2467 | * RAMDISK we use the internal transport_emulate_control_cdb() logic |
| 2468 | * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK |
| 2469 | * LUN emulation code. |
| 2470 | * |
| 2471 | * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we |
| 2472 | * call ->do_task() directly and let the underlying TCM subsystem plugin |
| 2473 | * code handle the CDB emulation. |
| 2474 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2475 | if ((dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) && |
| 2476 | (!(task->task_se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB))) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2477 | error = transport_emulate_control_cdb(task); |
| 2478 | else |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2479 | error = dev->transport->do_task(task); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2480 | |
| 2481 | if (error != 0) { |
| 2482 | cmd->transport_error_status = error; |
| 2483 | atomic_set(&task->task_active, 0); |
| 2484 | atomic_set(&cmd->transport_sent, 0); |
| 2485 | transport_stop_tasks_for_cmd(cmd); |
| 2486 | transport_generic_request_failure(cmd, dev, 0, 1); |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | goto check_depth; |
| 2491 | |
| 2492 | return 0; |
| 2493 | } |
| 2494 | |
| 2495 | void transport_new_cmd_failure(struct se_cmd *se_cmd) |
| 2496 | { |
| 2497 | unsigned long flags; |
| 2498 | /* |
| 2499 | * Any unsolicited data will get dumped for failed command inside of |
| 2500 | * the fabric plugin |
| 2501 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2502 | spin_lock_irqsave(&se_cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2503 | se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED; |
| 2504 | se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2505 | spin_unlock_irqrestore(&se_cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | static void transport_nop_wait_for_tasks(struct se_cmd *, int, int); |
| 2509 | |
| 2510 | static inline u32 transport_get_sectors_6( |
| 2511 | unsigned char *cdb, |
| 2512 | struct se_cmd *cmd, |
| 2513 | int *ret) |
| 2514 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2515 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2516 | |
| 2517 | /* |
| 2518 | * Assume TYPE_DISK for non struct se_device objects. |
| 2519 | * Use 8-bit sector value. |
| 2520 | */ |
| 2521 | if (!dev) |
| 2522 | goto type_disk; |
| 2523 | |
| 2524 | /* |
| 2525 | * Use 24-bit allocation length for TYPE_TAPE. |
| 2526 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2527 | if (dev->transport->get_device_type(dev) == TYPE_TAPE) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2528 | return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4]; |
| 2529 | |
| 2530 | /* |
| 2531 | * Everything else assume TYPE_DISK Sector CDB location. |
| 2532 | * Use 8-bit sector value. |
| 2533 | */ |
| 2534 | type_disk: |
| 2535 | return (u32)cdb[4]; |
| 2536 | } |
| 2537 | |
| 2538 | static inline u32 transport_get_sectors_10( |
| 2539 | unsigned char *cdb, |
| 2540 | struct se_cmd *cmd, |
| 2541 | int *ret) |
| 2542 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2543 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2544 | |
| 2545 | /* |
| 2546 | * Assume TYPE_DISK for non struct se_device objects. |
| 2547 | * Use 16-bit sector value. |
| 2548 | */ |
| 2549 | if (!dev) |
| 2550 | goto type_disk; |
| 2551 | |
| 2552 | /* |
| 2553 | * XXX_10 is not defined in SSC, throw an exception |
| 2554 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2555 | if (dev->transport->get_device_type(dev) == TYPE_TAPE) { |
| 2556 | *ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2557 | return 0; |
| 2558 | } |
| 2559 | |
| 2560 | /* |
| 2561 | * Everything else assume TYPE_DISK Sector CDB location. |
| 2562 | * Use 16-bit sector value. |
| 2563 | */ |
| 2564 | type_disk: |
| 2565 | return (u32)(cdb[7] << 8) + cdb[8]; |
| 2566 | } |
| 2567 | |
| 2568 | static inline u32 transport_get_sectors_12( |
| 2569 | unsigned char *cdb, |
| 2570 | struct se_cmd *cmd, |
| 2571 | int *ret) |
| 2572 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2573 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2574 | |
| 2575 | /* |
| 2576 | * Assume TYPE_DISK for non struct se_device objects. |
| 2577 | * Use 32-bit sector value. |
| 2578 | */ |
| 2579 | if (!dev) |
| 2580 | goto type_disk; |
| 2581 | |
| 2582 | /* |
| 2583 | * XXX_12 is not defined in SSC, throw an exception |
| 2584 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2585 | if (dev->transport->get_device_type(dev) == TYPE_TAPE) { |
| 2586 | *ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2587 | return 0; |
| 2588 | } |
| 2589 | |
| 2590 | /* |
| 2591 | * Everything else assume TYPE_DISK Sector CDB location. |
| 2592 | * Use 32-bit sector value. |
| 2593 | */ |
| 2594 | type_disk: |
| 2595 | return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9]; |
| 2596 | } |
| 2597 | |
| 2598 | static inline u32 transport_get_sectors_16( |
| 2599 | unsigned char *cdb, |
| 2600 | struct se_cmd *cmd, |
| 2601 | int *ret) |
| 2602 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2603 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2604 | |
| 2605 | /* |
| 2606 | * Assume TYPE_DISK for non struct se_device objects. |
| 2607 | * Use 32-bit sector value. |
| 2608 | */ |
| 2609 | if (!dev) |
| 2610 | goto type_disk; |
| 2611 | |
| 2612 | /* |
| 2613 | * Use 24-bit allocation length for TYPE_TAPE. |
| 2614 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2615 | if (dev->transport->get_device_type(dev) == TYPE_TAPE) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2616 | return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14]; |
| 2617 | |
| 2618 | type_disk: |
| 2619 | return (u32)(cdb[10] << 24) + (cdb[11] << 16) + |
| 2620 | (cdb[12] << 8) + cdb[13]; |
| 2621 | } |
| 2622 | |
| 2623 | /* |
| 2624 | * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants |
| 2625 | */ |
| 2626 | static inline u32 transport_get_sectors_32( |
| 2627 | unsigned char *cdb, |
| 2628 | struct se_cmd *cmd, |
| 2629 | int *ret) |
| 2630 | { |
| 2631 | /* |
| 2632 | * Assume TYPE_DISK for non struct se_device objects. |
| 2633 | * Use 32-bit sector value. |
| 2634 | */ |
| 2635 | return (u32)(cdb[28] << 24) + (cdb[29] << 16) + |
| 2636 | (cdb[30] << 8) + cdb[31]; |
| 2637 | |
| 2638 | } |
| 2639 | |
| 2640 | static inline u32 transport_get_size( |
| 2641 | u32 sectors, |
| 2642 | unsigned char *cdb, |
| 2643 | struct se_cmd *cmd) |
| 2644 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2645 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2646 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2647 | if (dev->transport->get_device_type(dev) == TYPE_TAPE) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2648 | if (cdb[1] & 1) { /* sectors */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2649 | return dev->se_sub_dev->se_dev_attrib.block_size * sectors; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2650 | } else /* bytes */ |
| 2651 | return sectors; |
| 2652 | } |
| 2653 | #if 0 |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2654 | pr_debug("Returning block_size: %u, sectors: %u == %u for" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2655 | " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors, |
| 2656 | dev->se_sub_dev->se_dev_attrib.block_size * sectors, |
| 2657 | dev->transport->name); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2658 | #endif |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2659 | return dev->se_sub_dev->se_dev_attrib.block_size * sectors; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | unsigned char transport_asciihex_to_binaryhex(unsigned char val[2]) |
| 2663 | { |
| 2664 | unsigned char result = 0; |
| 2665 | /* |
| 2666 | * MSB |
| 2667 | */ |
| 2668 | if ((val[0] >= 'a') && (val[0] <= 'f')) |
| 2669 | result = ((val[0] - 'a' + 10) & 0xf) << 4; |
| 2670 | else |
| 2671 | if ((val[0] >= 'A') && (val[0] <= 'F')) |
| 2672 | result = ((val[0] - 'A' + 10) & 0xf) << 4; |
| 2673 | else /* digit */ |
| 2674 | result = ((val[0] - '0') & 0xf) << 4; |
| 2675 | /* |
| 2676 | * LSB |
| 2677 | */ |
| 2678 | if ((val[1] >= 'a') && (val[1] <= 'f')) |
| 2679 | result |= ((val[1] - 'a' + 10) & 0xf); |
| 2680 | else |
| 2681 | if ((val[1] >= 'A') && (val[1] <= 'F')) |
| 2682 | result |= ((val[1] - 'A' + 10) & 0xf); |
| 2683 | else /* digit */ |
| 2684 | result |= ((val[1] - '0') & 0xf); |
| 2685 | |
| 2686 | return result; |
| 2687 | } |
| 2688 | EXPORT_SYMBOL(transport_asciihex_to_binaryhex); |
| 2689 | |
| 2690 | static void transport_xor_callback(struct se_cmd *cmd) |
| 2691 | { |
| 2692 | unsigned char *buf, *addr; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2693 | struct scatterlist *sg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2694 | unsigned int offset; |
| 2695 | int i; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2696 | int count; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2697 | /* |
| 2698 | * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command |
| 2699 | * |
| 2700 | * 1) read the specified logical block(s); |
| 2701 | * 2) transfer logical blocks from the data-out buffer; |
| 2702 | * 3) XOR the logical blocks transferred from the data-out buffer with |
| 2703 | * the logical blocks read, storing the resulting XOR data in a buffer; |
| 2704 | * 4) if the DISABLE WRITE bit is set to zero, then write the logical |
| 2705 | * blocks transferred from the data-out buffer; and |
| 2706 | * 5) transfer the resulting XOR data to the data-in buffer. |
| 2707 | */ |
| 2708 | buf = kmalloc(cmd->data_length, GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2709 | if (!buf) { |
| 2710 | pr_err("Unable to allocate xor_callback buf\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2711 | return; |
| 2712 | } |
| 2713 | /* |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2714 | * Copy the scatterlist WRITE buffer located at cmd->t_data_sg |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2715 | * into the locally allocated *buf |
| 2716 | */ |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2717 | sg_copy_to_buffer(cmd->t_data_sg, |
| 2718 | cmd->t_data_nents, |
| 2719 | buf, |
| 2720 | cmd->data_length); |
| 2721 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2722 | /* |
| 2723 | * Now perform the XOR against the BIDI read memory located at |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2724 | * cmd->t_mem_bidi_list |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2725 | */ |
| 2726 | |
| 2727 | offset = 0; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2728 | for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) { |
| 2729 | addr = kmap_atomic(sg_page(sg), KM_USER0); |
| 2730 | if (!addr) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2731 | goto out; |
| 2732 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2733 | for (i = 0; i < sg->length; i++) |
| 2734 | *(addr + sg->offset + i) ^= *(buf + offset + i); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2735 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2736 | offset += sg->length; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2737 | kunmap_atomic(addr, KM_USER0); |
| 2738 | } |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2739 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2740 | out: |
| 2741 | kfree(buf); |
| 2742 | } |
| 2743 | |
| 2744 | /* |
| 2745 | * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd |
| 2746 | */ |
| 2747 | static int transport_get_sense_data(struct se_cmd *cmd) |
| 2748 | { |
| 2749 | unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL; |
| 2750 | struct se_device *dev; |
| 2751 | struct se_task *task = NULL, *task_tmp; |
| 2752 | unsigned long flags; |
| 2753 | u32 offset = 0; |
| 2754 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2755 | WARN_ON(!cmd->se_lun); |
| 2756 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2757 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2758 | if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2759 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2760 | return 0; |
| 2761 | } |
| 2762 | |
| 2763 | list_for_each_entry_safe(task, task_tmp, |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2764 | &cmd->t_task_list, t_list) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2765 | |
| 2766 | if (!task->task_sense) |
| 2767 | continue; |
| 2768 | |
| 2769 | dev = task->se_dev; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2770 | if (!dev) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2771 | continue; |
| 2772 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2773 | if (!dev->transport->get_sense_buffer) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2774 | pr_err("dev->transport->get_sense_buffer" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2775 | " is NULL\n"); |
| 2776 | continue; |
| 2777 | } |
| 2778 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2779 | sense_buffer = dev->transport->get_sense_buffer(task); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2780 | if (!sense_buffer) { |
| 2781 | pr_err("ITT[0x%08x]_TASK[%d]: Unable to locate" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2782 | " sense buffer for task with sense\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2783 | cmd->se_tfo->get_task_tag(cmd), task->task_no); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2784 | continue; |
| 2785 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2786 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2787 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2788 | offset = cmd->se_tfo->set_fabric_sense_len(cmd, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2789 | TRANSPORT_SENSE_BUFFER); |
| 2790 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2791 | memcpy(&buffer[offset], sense_buffer, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2792 | TRANSPORT_SENSE_BUFFER); |
| 2793 | cmd->scsi_status = task->task_scsi_status; |
| 2794 | /* Automatically padded */ |
| 2795 | cmd->scsi_sense_length = |
| 2796 | (TRANSPORT_SENSE_BUFFER + offset); |
| 2797 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2798 | pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2799 | " and sense\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2800 | dev->se_hba->hba_id, dev->transport->name, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2801 | cmd->scsi_status); |
| 2802 | return 0; |
| 2803 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2804 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2805 | |
| 2806 | return -1; |
| 2807 | } |
| 2808 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2809 | static int |
| 2810 | transport_handle_reservation_conflict(struct se_cmd *cmd) |
| 2811 | { |
| 2812 | cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks; |
| 2813 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 2814 | cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT; |
| 2815 | cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; |
| 2816 | /* |
| 2817 | * For UA Interlock Code 11b, a RESERVATION CONFLICT will |
| 2818 | * establish a UNIT ATTENTION with PREVIOUS RESERVATION |
| 2819 | * CONFLICT STATUS. |
| 2820 | * |
| 2821 | * See spc4r17, section 7.4.6 Control Mode Page, Table 349 |
| 2822 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2823 | if (cmd->se_sess && |
| 2824 | cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2) |
| 2825 | core_scsi3_ua_allocate(cmd->se_sess->se_node_acl, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2826 | cmd->orig_fe_lun, 0x2C, |
| 2827 | ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2828 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2829 | } |
| 2830 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2831 | static inline long long transport_dev_end_lba(struct se_device *dev) |
| 2832 | { |
| 2833 | return dev->transport->get_blocks(dev) + 1; |
| 2834 | } |
| 2835 | |
| 2836 | static int transport_cmd_get_valid_sectors(struct se_cmd *cmd) |
| 2837 | { |
| 2838 | struct se_device *dev = cmd->se_dev; |
| 2839 | u32 sectors; |
| 2840 | |
| 2841 | if (dev->transport->get_device_type(dev) != TYPE_DISK) |
| 2842 | return 0; |
| 2843 | |
| 2844 | sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size); |
| 2845 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2846 | if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) { |
| 2847 | pr_err("LBA: %llu Sectors: %u exceeds" |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2848 | " transport_dev_end_lba(): %llu\n", |
| 2849 | cmd->t_task_lba, sectors, |
| 2850 | transport_dev_end_lba(dev)); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2851 | pr_err(" We should return CHECK_CONDITION" |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2852 | " but we don't yet\n"); |
| 2853 | return 0; |
| 2854 | } |
| 2855 | |
| 2856 | return sectors; |
| 2857 | } |
| 2858 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2859 | /* transport_generic_cmd_sequencer(): |
| 2860 | * |
| 2861 | * Generic Command Sequencer that should work for most DAS transport |
| 2862 | * drivers. |
| 2863 | * |
| 2864 | * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD |
| 2865 | * RX Thread. |
| 2866 | * |
| 2867 | * FIXME: Need to support other SCSI OPCODES where as well. |
| 2868 | */ |
| 2869 | static int transport_generic_cmd_sequencer( |
| 2870 | struct se_cmd *cmd, |
| 2871 | unsigned char *cdb) |
| 2872 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2873 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2874 | struct se_subsystem_dev *su_dev = dev->se_sub_dev; |
| 2875 | int ret = 0, sector_ret = 0, passthrough; |
| 2876 | u32 sectors = 0, size = 0, pr_reg_type = 0; |
| 2877 | u16 service_action; |
| 2878 | u8 alua_ascq = 0; |
| 2879 | /* |
| 2880 | * Check for an existing UNIT ATTENTION condition |
| 2881 | */ |
| 2882 | if (core_scsi3_ua_check(cmd, cdb) < 0) { |
| 2883 | cmd->transport_wait_for_tasks = |
| 2884 | &transport_nop_wait_for_tasks; |
| 2885 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 2886 | cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2887 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2888 | } |
| 2889 | /* |
| 2890 | * Check status of Asymmetric Logical Unit Assignment port |
| 2891 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2892 | ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2893 | if (ret != 0) { |
| 2894 | cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks; |
| 2895 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2896 | * Set SCSI additional sense code (ASC) to 'LUN Not Accessible'; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2897 | * The ALUA additional sense code qualifier (ASCQ) is determined |
| 2898 | * by the ALUA primary or secondary access state.. |
| 2899 | */ |
| 2900 | if (ret > 0) { |
| 2901 | #if 0 |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 2902 | pr_debug("[%s]: ALUA TG Port not available," |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2903 | " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2904 | cmd->se_tfo->get_fabric_name(), alua_ascq); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2905 | #endif |
| 2906 | transport_set_sense_codes(cmd, 0x04, alua_ascq); |
| 2907 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 2908 | cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2909 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2910 | } |
| 2911 | goto out_invalid_cdb_field; |
| 2912 | } |
| 2913 | /* |
| 2914 | * Check status for SPC-3 Persistent Reservations |
| 2915 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2916 | if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) { |
| 2917 | if (su_dev->t10_pr.pr_ops.t10_seq_non_holder( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2918 | cmd, cdb, pr_reg_type) != 0) |
| 2919 | return transport_handle_reservation_conflict(cmd); |
| 2920 | /* |
| 2921 | * This means the CDB is allowed for the SCSI Initiator port |
| 2922 | * when said port is *NOT* holding the legacy SPC-2 or |
| 2923 | * SPC-3 Persistent Reservation. |
| 2924 | */ |
| 2925 | } |
| 2926 | |
| 2927 | switch (cdb[0]) { |
| 2928 | case READ_6: |
| 2929 | sectors = transport_get_sectors_6(cdb, cmd, §or_ret); |
| 2930 | if (sector_ret) |
| 2931 | goto out_unsupported_cdb; |
| 2932 | size = transport_get_size(sectors, cdb, cmd); |
| 2933 | cmd->transport_split_cdb = &split_cdb_XX_6; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2934 | cmd->t_task_lba = transport_lba_21(cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2935 | cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB; |
| 2936 | break; |
| 2937 | case READ_10: |
| 2938 | sectors = transport_get_sectors_10(cdb, cmd, §or_ret); |
| 2939 | if (sector_ret) |
| 2940 | goto out_unsupported_cdb; |
| 2941 | size = transport_get_size(sectors, cdb, cmd); |
| 2942 | cmd->transport_split_cdb = &split_cdb_XX_10; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2943 | cmd->t_task_lba = transport_lba_32(cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2944 | cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB; |
| 2945 | break; |
| 2946 | case READ_12: |
| 2947 | sectors = transport_get_sectors_12(cdb, cmd, §or_ret); |
| 2948 | if (sector_ret) |
| 2949 | goto out_unsupported_cdb; |
| 2950 | size = transport_get_size(sectors, cdb, cmd); |
| 2951 | cmd->transport_split_cdb = &split_cdb_XX_12; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2952 | cmd->t_task_lba = transport_lba_32(cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2953 | cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB; |
| 2954 | break; |
| 2955 | case READ_16: |
| 2956 | sectors = transport_get_sectors_16(cdb, cmd, §or_ret); |
| 2957 | if (sector_ret) |
| 2958 | goto out_unsupported_cdb; |
| 2959 | size = transport_get_size(sectors, cdb, cmd); |
| 2960 | cmd->transport_split_cdb = &split_cdb_XX_16; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2961 | cmd->t_task_lba = transport_lba_64(cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2962 | cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB; |
| 2963 | break; |
| 2964 | case WRITE_6: |
| 2965 | sectors = transport_get_sectors_6(cdb, cmd, §or_ret); |
| 2966 | if (sector_ret) |
| 2967 | goto out_unsupported_cdb; |
| 2968 | size = transport_get_size(sectors, cdb, cmd); |
| 2969 | cmd->transport_split_cdb = &split_cdb_XX_6; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2970 | cmd->t_task_lba = transport_lba_21(cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2971 | cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB; |
| 2972 | break; |
| 2973 | case WRITE_10: |
| 2974 | sectors = transport_get_sectors_10(cdb, cmd, §or_ret); |
| 2975 | if (sector_ret) |
| 2976 | goto out_unsupported_cdb; |
| 2977 | size = transport_get_size(sectors, cdb, cmd); |
| 2978 | cmd->transport_split_cdb = &split_cdb_XX_10; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2979 | cmd->t_task_lba = transport_lba_32(cdb); |
| 2980 | cmd->t_tasks_fua = (cdb[1] & 0x8); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2981 | cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB; |
| 2982 | break; |
| 2983 | case WRITE_12: |
| 2984 | sectors = transport_get_sectors_12(cdb, cmd, §or_ret); |
| 2985 | if (sector_ret) |
| 2986 | goto out_unsupported_cdb; |
| 2987 | size = transport_get_size(sectors, cdb, cmd); |
| 2988 | cmd->transport_split_cdb = &split_cdb_XX_12; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2989 | cmd->t_task_lba = transport_lba_32(cdb); |
| 2990 | cmd->t_tasks_fua = (cdb[1] & 0x8); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2991 | cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB; |
| 2992 | break; |
| 2993 | case WRITE_16: |
| 2994 | sectors = transport_get_sectors_16(cdb, cmd, §or_ret); |
| 2995 | if (sector_ret) |
| 2996 | goto out_unsupported_cdb; |
| 2997 | size = transport_get_size(sectors, cdb, cmd); |
| 2998 | cmd->transport_split_cdb = &split_cdb_XX_16; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2999 | cmd->t_task_lba = transport_lba_64(cdb); |
| 3000 | cmd->t_tasks_fua = (cdb[1] & 0x8); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3001 | cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB; |
| 3002 | break; |
| 3003 | case XDWRITEREAD_10: |
| 3004 | if ((cmd->data_direction != DMA_TO_DEVICE) || |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3005 | !(cmd->t_tasks_bidi)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3006 | goto out_invalid_cdb_field; |
| 3007 | sectors = transport_get_sectors_10(cdb, cmd, §or_ret); |
| 3008 | if (sector_ret) |
| 3009 | goto out_unsupported_cdb; |
| 3010 | size = transport_get_size(sectors, cdb, cmd); |
| 3011 | cmd->transport_split_cdb = &split_cdb_XX_10; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3012 | cmd->t_task_lba = transport_lba_32(cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3013 | cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3014 | passthrough = (dev->transport->transport_type == |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3015 | TRANSPORT_PLUGIN_PHBA_PDEV); |
| 3016 | /* |
| 3017 | * Skip the remaining assignments for TCM/PSCSI passthrough |
| 3018 | */ |
| 3019 | if (passthrough) |
| 3020 | break; |
| 3021 | /* |
| 3022 | * Setup BIDI XOR callback to be run during transport_generic_complete_ok() |
| 3023 | */ |
| 3024 | cmd->transport_complete_callback = &transport_xor_callback; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3025 | cmd->t_tasks_fua = (cdb[1] & 0x8); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3026 | break; |
| 3027 | case VARIABLE_LENGTH_CMD: |
| 3028 | service_action = get_unaligned_be16(&cdb[8]); |
| 3029 | /* |
| 3030 | * Determine if this is TCM/PSCSI device and we should disable |
| 3031 | * internal emulation for this CDB. |
| 3032 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3033 | passthrough = (dev->transport->transport_type == |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3034 | TRANSPORT_PLUGIN_PHBA_PDEV); |
| 3035 | |
| 3036 | switch (service_action) { |
| 3037 | case XDWRITEREAD_32: |
| 3038 | sectors = transport_get_sectors_32(cdb, cmd, §or_ret); |
| 3039 | if (sector_ret) |
| 3040 | goto out_unsupported_cdb; |
| 3041 | size = transport_get_size(sectors, cdb, cmd); |
| 3042 | /* |
| 3043 | * Use WRITE_32 and READ_32 opcodes for the emulated |
| 3044 | * XDWRITE_READ_32 logic. |
| 3045 | */ |
| 3046 | cmd->transport_split_cdb = &split_cdb_XX_32; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3047 | cmd->t_task_lba = transport_lba_64_ext(cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3048 | cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB; |
| 3049 | |
| 3050 | /* |
| 3051 | * Skip the remaining assignments for TCM/PSCSI passthrough |
| 3052 | */ |
| 3053 | if (passthrough) |
| 3054 | break; |
| 3055 | |
| 3056 | /* |
| 3057 | * Setup BIDI XOR callback to be run during |
| 3058 | * transport_generic_complete_ok() |
| 3059 | */ |
| 3060 | cmd->transport_complete_callback = &transport_xor_callback; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3061 | cmd->t_tasks_fua = (cdb[10] & 0x8); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3062 | break; |
| 3063 | case WRITE_SAME_32: |
| 3064 | sectors = transport_get_sectors_32(cdb, cmd, §or_ret); |
| 3065 | if (sector_ret) |
| 3066 | goto out_unsupported_cdb; |
Nicholas Bellinger | dd3a5ad | 2011-05-06 17:55:35 -0700 | [diff] [blame] | 3067 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3068 | if (sectors) |
Nicholas Bellinger | dd3a5ad | 2011-05-06 17:55:35 -0700 | [diff] [blame] | 3069 | size = transport_get_size(sectors, cdb, cmd); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3070 | else { |
| 3071 | pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not" |
| 3072 | " supported\n"); |
| 3073 | goto out_invalid_cdb_field; |
| 3074 | } |
Nicholas Bellinger | dd3a5ad | 2011-05-06 17:55:35 -0700 | [diff] [blame] | 3075 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3076 | cmd->t_task_lba = get_unaligned_be64(&cdb[12]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3077 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
| 3078 | |
| 3079 | /* |
| 3080 | * Skip the remaining assignments for TCM/PSCSI passthrough |
| 3081 | */ |
| 3082 | if (passthrough) |
| 3083 | break; |
| 3084 | |
| 3085 | if ((cdb[10] & 0x04) || (cdb[10] & 0x02)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3086 | pr_err("WRITE_SAME PBDATA and LBDATA" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3087 | " bits not supported for Block Discard" |
| 3088 | " Emulation\n"); |
| 3089 | goto out_invalid_cdb_field; |
| 3090 | } |
| 3091 | /* |
| 3092 | * Currently for the emulated case we only accept |
| 3093 | * tpws with the UNMAP=1 bit set. |
| 3094 | */ |
| 3095 | if (!(cdb[10] & 0x08)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3096 | pr_err("WRITE_SAME w/o UNMAP bit not" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3097 | " supported for Block Discard Emulation\n"); |
| 3098 | goto out_invalid_cdb_field; |
| 3099 | } |
| 3100 | break; |
| 3101 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3102 | pr_err("VARIABLE_LENGTH_CMD service action" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3103 | " 0x%04x not supported\n", service_action); |
| 3104 | goto out_unsupported_cdb; |
| 3105 | } |
| 3106 | break; |
Nicholas Bellinger | e434f1f1 | 2011-02-21 07:49:26 +0000 | [diff] [blame] | 3107 | case MAINTENANCE_IN: |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3108 | if (dev->transport->get_device_type(dev) != TYPE_ROM) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3109 | /* MAINTENANCE_IN from SCC-2 */ |
| 3110 | /* |
| 3111 | * Check for emulated MI_REPORT_TARGET_PGS. |
| 3112 | */ |
| 3113 | if (cdb[1] == MI_REPORT_TARGET_PGS) { |
| 3114 | cmd->transport_emulate_cdb = |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3115 | (su_dev->t10_alua.alua_type == |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3116 | SPC3_ALUA_EMULATED) ? |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3117 | core_emulate_report_target_port_groups : |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3118 | NULL; |
| 3119 | } |
| 3120 | size = (cdb[6] << 24) | (cdb[7] << 16) | |
| 3121 | (cdb[8] << 8) | cdb[9]; |
| 3122 | } else { |
| 3123 | /* GPCMD_SEND_KEY from multi media commands */ |
| 3124 | size = (cdb[8] << 8) + cdb[9]; |
| 3125 | } |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3126 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3127 | break; |
| 3128 | case MODE_SELECT: |
| 3129 | size = cdb[4]; |
| 3130 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
| 3131 | break; |
| 3132 | case MODE_SELECT_10: |
| 3133 | size = (cdb[7] << 8) + cdb[8]; |
| 3134 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
| 3135 | break; |
| 3136 | case MODE_SENSE: |
| 3137 | size = cdb[4]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3138 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3139 | break; |
| 3140 | case MODE_SENSE_10: |
| 3141 | case GPCMD_READ_BUFFER_CAPACITY: |
| 3142 | case GPCMD_SEND_OPC: |
| 3143 | case LOG_SELECT: |
| 3144 | case LOG_SENSE: |
| 3145 | size = (cdb[7] << 8) + cdb[8]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3146 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3147 | break; |
| 3148 | case READ_BLOCK_LIMITS: |
| 3149 | size = READ_BLOCK_LEN; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3150 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3151 | break; |
| 3152 | case GPCMD_GET_CONFIGURATION: |
| 3153 | case GPCMD_READ_FORMAT_CAPACITIES: |
| 3154 | case GPCMD_READ_DISC_INFO: |
| 3155 | case GPCMD_READ_TRACK_RZONE_INFO: |
| 3156 | size = (cdb[7] << 8) + cdb[8]; |
| 3157 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
| 3158 | break; |
| 3159 | case PERSISTENT_RESERVE_IN: |
| 3160 | case PERSISTENT_RESERVE_OUT: |
| 3161 | cmd->transport_emulate_cdb = |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3162 | (su_dev->t10_pr.res_type == |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3163 | SPC3_PERSISTENT_RESERVATIONS) ? |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3164 | core_scsi3_emulate_pr : NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3165 | size = (cdb[7] << 8) + cdb[8]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3166 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3167 | break; |
| 3168 | case GPCMD_MECHANISM_STATUS: |
| 3169 | case GPCMD_READ_DVD_STRUCTURE: |
| 3170 | size = (cdb[8] << 8) + cdb[9]; |
| 3171 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
| 3172 | break; |
| 3173 | case READ_POSITION: |
| 3174 | size = READ_POSITION_LEN; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3175 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3176 | break; |
Nicholas Bellinger | e434f1f1 | 2011-02-21 07:49:26 +0000 | [diff] [blame] | 3177 | case MAINTENANCE_OUT: |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3178 | if (dev->transport->get_device_type(dev) != TYPE_ROM) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3179 | /* MAINTENANCE_OUT from SCC-2 |
| 3180 | * |
| 3181 | * Check for emulated MO_SET_TARGET_PGS. |
| 3182 | */ |
| 3183 | if (cdb[1] == MO_SET_TARGET_PGS) { |
| 3184 | cmd->transport_emulate_cdb = |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3185 | (su_dev->t10_alua.alua_type == |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3186 | SPC3_ALUA_EMULATED) ? |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3187 | core_emulate_set_target_port_groups : |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3188 | NULL; |
| 3189 | } |
| 3190 | |
| 3191 | size = (cdb[6] << 24) | (cdb[7] << 16) | |
| 3192 | (cdb[8] << 8) | cdb[9]; |
| 3193 | } else { |
| 3194 | /* GPCMD_REPORT_KEY from multi media commands */ |
| 3195 | size = (cdb[8] << 8) + cdb[9]; |
| 3196 | } |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3197 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3198 | break; |
| 3199 | case INQUIRY: |
| 3200 | size = (cdb[3] << 8) + cdb[4]; |
| 3201 | /* |
| 3202 | * Do implict HEAD_OF_QUEUE processing for INQUIRY. |
| 3203 | * See spc4r17 section 5.3 |
| 3204 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3205 | if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED) |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 3206 | cmd->sam_task_attr = MSG_HEAD_TAG; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3207 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3208 | break; |
| 3209 | case READ_BUFFER: |
| 3210 | size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3211 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3212 | break; |
| 3213 | case READ_CAPACITY: |
| 3214 | size = READ_CAP_LEN; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3215 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3216 | break; |
| 3217 | case READ_MEDIA_SERIAL_NUMBER: |
| 3218 | case SECURITY_PROTOCOL_IN: |
| 3219 | case SECURITY_PROTOCOL_OUT: |
| 3220 | size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3221 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3222 | break; |
| 3223 | case SERVICE_ACTION_IN: |
| 3224 | case ACCESS_CONTROL_IN: |
| 3225 | case ACCESS_CONTROL_OUT: |
| 3226 | case EXTENDED_COPY: |
| 3227 | case READ_ATTRIBUTE: |
| 3228 | case RECEIVE_COPY_RESULTS: |
| 3229 | case WRITE_ATTRIBUTE: |
| 3230 | size = (cdb[10] << 24) | (cdb[11] << 16) | |
| 3231 | (cdb[12] << 8) | cdb[13]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3232 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3233 | break; |
| 3234 | case RECEIVE_DIAGNOSTIC: |
| 3235 | case SEND_DIAGNOSTIC: |
| 3236 | size = (cdb[3] << 8) | cdb[4]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3237 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3238 | break; |
| 3239 | /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */ |
| 3240 | #if 0 |
| 3241 | case GPCMD_READ_CD: |
| 3242 | sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8]; |
| 3243 | size = (2336 * sectors); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3244 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3245 | break; |
| 3246 | #endif |
| 3247 | case READ_TOC: |
| 3248 | size = cdb[8]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3249 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3250 | break; |
| 3251 | case REQUEST_SENSE: |
| 3252 | size = cdb[4]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3253 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3254 | break; |
| 3255 | case READ_ELEMENT_STATUS: |
| 3256 | size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3257 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3258 | break; |
| 3259 | case WRITE_BUFFER: |
| 3260 | size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8]; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3261 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3262 | break; |
| 3263 | case RESERVE: |
| 3264 | case RESERVE_10: |
| 3265 | /* |
| 3266 | * The SPC-2 RESERVE does not contain a size in the SCSI CDB. |
| 3267 | * Assume the passthrough or $FABRIC_MOD will tell us about it. |
| 3268 | */ |
| 3269 | if (cdb[0] == RESERVE_10) |
| 3270 | size = (cdb[7] << 8) | cdb[8]; |
| 3271 | else |
| 3272 | size = cmd->data_length; |
| 3273 | |
| 3274 | /* |
| 3275 | * Setup the legacy emulated handler for SPC-2 and |
| 3276 | * >= SPC-3 compatible reservation handling (CRH=1) |
| 3277 | * Otherwise, we assume the underlying SCSI logic is |
| 3278 | * is running in SPC_PASSTHROUGH, and wants reservations |
| 3279 | * emulation disabled. |
| 3280 | */ |
| 3281 | cmd->transport_emulate_cdb = |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3282 | (su_dev->t10_pr.res_type != |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3283 | SPC_PASSTHROUGH) ? |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3284 | core_scsi2_emulate_crh : NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3285 | cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB; |
| 3286 | break; |
| 3287 | case RELEASE: |
| 3288 | case RELEASE_10: |
| 3289 | /* |
| 3290 | * The SPC-2 RELEASE does not contain a size in the SCSI CDB. |
| 3291 | * Assume the passthrough or $FABRIC_MOD will tell us about it. |
| 3292 | */ |
| 3293 | if (cdb[0] == RELEASE_10) |
| 3294 | size = (cdb[7] << 8) | cdb[8]; |
| 3295 | else |
| 3296 | size = cmd->data_length; |
| 3297 | |
| 3298 | cmd->transport_emulate_cdb = |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3299 | (su_dev->t10_pr.res_type != |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3300 | SPC_PASSTHROUGH) ? |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3301 | core_scsi2_emulate_crh : NULL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3302 | cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB; |
| 3303 | break; |
| 3304 | case SYNCHRONIZE_CACHE: |
| 3305 | case 0x91: /* SYNCHRONIZE_CACHE_16: */ |
| 3306 | /* |
| 3307 | * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE |
| 3308 | */ |
| 3309 | if (cdb[0] == SYNCHRONIZE_CACHE) { |
| 3310 | sectors = transport_get_sectors_10(cdb, cmd, §or_ret); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3311 | cmd->t_task_lba = transport_lba_32(cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3312 | } else { |
| 3313 | sectors = transport_get_sectors_16(cdb, cmd, §or_ret); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3314 | cmd->t_task_lba = transport_lba_64(cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3315 | } |
| 3316 | if (sector_ret) |
| 3317 | goto out_unsupported_cdb; |
| 3318 | |
| 3319 | size = transport_get_size(sectors, cdb, cmd); |
| 3320 | cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB; |
| 3321 | |
| 3322 | /* |
| 3323 | * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb() |
| 3324 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3325 | if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3326 | break; |
| 3327 | /* |
| 3328 | * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation |
| 3329 | * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks() |
| 3330 | */ |
| 3331 | cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC; |
| 3332 | /* |
| 3333 | * Check to ensure that LBA + Range does not exceed past end of |
| 3334 | * device. |
| 3335 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3336 | if (!transport_cmd_get_valid_sectors(cmd)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3337 | goto out_invalid_cdb_field; |
| 3338 | break; |
| 3339 | case UNMAP: |
| 3340 | size = get_unaligned_be16(&cdb[7]); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3341 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3342 | break; |
| 3343 | case WRITE_SAME_16: |
| 3344 | sectors = transport_get_sectors_16(cdb, cmd, §or_ret); |
| 3345 | if (sector_ret) |
| 3346 | goto out_unsupported_cdb; |
Nicholas Bellinger | dd3a5ad | 2011-05-06 17:55:35 -0700 | [diff] [blame] | 3347 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3348 | if (sectors) |
Nicholas Bellinger | dd3a5ad | 2011-05-06 17:55:35 -0700 | [diff] [blame] | 3349 | size = transport_get_size(sectors, cdb, cmd); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3350 | else { |
| 3351 | pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n"); |
| 3352 | goto out_invalid_cdb_field; |
| 3353 | } |
Nicholas Bellinger | dd3a5ad | 2011-05-06 17:55:35 -0700 | [diff] [blame] | 3354 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3355 | cmd->t_task_lba = get_unaligned_be16(&cdb[2]); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3356 | passthrough = (dev->transport->transport_type == |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3357 | TRANSPORT_PLUGIN_PHBA_PDEV); |
| 3358 | /* |
| 3359 | * Determine if the received WRITE_SAME_16 is used to for direct |
| 3360 | * passthrough into Linux/SCSI with struct request via TCM/pSCSI |
| 3361 | * or we are signaling the use of internal WRITE_SAME + UNMAP=1 |
| 3362 | * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and |
| 3363 | * TCM/FILEIO subsystem plugin backstores. |
| 3364 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3365 | if (!passthrough) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3366 | if ((cdb[1] & 0x04) || (cdb[1] & 0x02)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3367 | pr_err("WRITE_SAME PBDATA and LBDATA" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3368 | " bits not supported for Block Discard" |
| 3369 | " Emulation\n"); |
| 3370 | goto out_invalid_cdb_field; |
| 3371 | } |
| 3372 | /* |
| 3373 | * Currently for the emulated case we only accept |
| 3374 | * tpws with the UNMAP=1 bit set. |
| 3375 | */ |
| 3376 | if (!(cdb[1] & 0x08)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3377 | pr_err("WRITE_SAME w/o UNMAP bit not " |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3378 | " supported for Block Discard Emulation\n"); |
| 3379 | goto out_invalid_cdb_field; |
| 3380 | } |
| 3381 | } |
| 3382 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
| 3383 | break; |
| 3384 | case ALLOW_MEDIUM_REMOVAL: |
| 3385 | case GPCMD_CLOSE_TRACK: |
| 3386 | case ERASE: |
| 3387 | case INITIALIZE_ELEMENT_STATUS: |
| 3388 | case GPCMD_LOAD_UNLOAD: |
| 3389 | case REZERO_UNIT: |
| 3390 | case SEEK_10: |
| 3391 | case GPCMD_SET_SPEED: |
| 3392 | case SPACE: |
| 3393 | case START_STOP: |
| 3394 | case TEST_UNIT_READY: |
| 3395 | case VERIFY: |
| 3396 | case WRITE_FILEMARKS: |
| 3397 | case MOVE_MEDIUM: |
| 3398 | cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB; |
| 3399 | break; |
| 3400 | case REPORT_LUNS: |
| 3401 | cmd->transport_emulate_cdb = |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3402 | transport_core_report_lun_response; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3403 | size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9]; |
| 3404 | /* |
| 3405 | * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS |
| 3406 | * See spc4r17 section 5.3 |
| 3407 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3408 | if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED) |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 3409 | cmd->sam_task_attr = MSG_HEAD_TAG; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3410 | cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3411 | break; |
| 3412 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3413 | pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3414 | " 0x%02x, sending CHECK_CONDITION.\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3415 | cmd->se_tfo->get_fabric_name(), cdb[0]); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3416 | cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks; |
| 3417 | goto out_unsupported_cdb; |
| 3418 | } |
| 3419 | |
| 3420 | if (size != cmd->data_length) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3421 | pr_warn("TARGET_CORE[%s]: Expected Transfer Length:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3422 | " %u does not match SCSI CDB Length: %u for SAM Opcode:" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3423 | " 0x%02x\n", cmd->se_tfo->get_fabric_name(), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3424 | cmd->data_length, size, cdb[0]); |
| 3425 | |
| 3426 | cmd->cmd_spdtl = size; |
| 3427 | |
| 3428 | if (cmd->data_direction == DMA_TO_DEVICE) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3429 | pr_err("Rejecting underflow/overflow" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3430 | " WRITE data\n"); |
| 3431 | goto out_invalid_cdb_field; |
| 3432 | } |
| 3433 | /* |
| 3434 | * Reject READ_* or WRITE_* with overflow/underflow for |
| 3435 | * type SCF_SCSI_DATA_SG_IO_CDB. |
| 3436 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3437 | if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) { |
| 3438 | pr_err("Failing OVERFLOW/UNDERFLOW for LBA op" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3439 | " CDB on non 512-byte sector setup subsystem" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3440 | " plugin: %s\n", dev->transport->name); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3441 | /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */ |
| 3442 | goto out_invalid_cdb_field; |
| 3443 | } |
| 3444 | |
| 3445 | if (size > cmd->data_length) { |
| 3446 | cmd->se_cmd_flags |= SCF_OVERFLOW_BIT; |
| 3447 | cmd->residual_count = (size - cmd->data_length); |
| 3448 | } else { |
| 3449 | cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT; |
| 3450 | cmd->residual_count = (cmd->data_length - size); |
| 3451 | } |
| 3452 | cmd->data_length = size; |
| 3453 | } |
| 3454 | |
Andy Grover | d0229ae | 2011-07-08 17:04:53 -0700 | [diff] [blame] | 3455 | /* Let's limit control cdbs to a page, for simplicity's sake. */ |
| 3456 | if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) && |
| 3457 | size > PAGE_SIZE) |
| 3458 | goto out_invalid_cdb_field; |
| 3459 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3460 | transport_set_supported_SAM_opcode(cmd); |
| 3461 | return ret; |
| 3462 | |
| 3463 | out_unsupported_cdb: |
| 3464 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 3465 | cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3466 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3467 | out_invalid_cdb_field: |
| 3468 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 3469 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3470 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3471 | } |
| 3472 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3473 | /* |
| 3474 | * Called from transport_generic_complete_ok() and |
| 3475 | * transport_generic_request_failure() to determine which dormant/delayed |
| 3476 | * and ordered cmds need to have their tasks added to the execution queue. |
| 3477 | */ |
| 3478 | static void transport_complete_task_attr(struct se_cmd *cmd) |
| 3479 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3480 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3481 | struct se_cmd *cmd_p, *cmd_tmp; |
| 3482 | int new_active_tasks = 0; |
| 3483 | |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 3484 | if (cmd->sam_task_attr == MSG_SIMPLE_TAG) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3485 | atomic_dec(&dev->simple_cmds); |
| 3486 | smp_mb__after_atomic_dec(); |
| 3487 | dev->dev_cur_ordered_id++; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3488 | pr_debug("Incremented dev->dev_cur_ordered_id: %u for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3489 | " SIMPLE: %u\n", dev->dev_cur_ordered_id, |
| 3490 | cmd->se_ordered_id); |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 3491 | } else if (cmd->sam_task_attr == MSG_HEAD_TAG) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3492 | atomic_dec(&dev->dev_hoq_count); |
| 3493 | smp_mb__after_atomic_dec(); |
| 3494 | dev->dev_cur_ordered_id++; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3495 | pr_debug("Incremented dev_cur_ordered_id: %u for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3496 | " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id, |
| 3497 | cmd->se_ordered_id); |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 3498 | } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3499 | spin_lock(&dev->ordered_cmd_lock); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3500 | list_del(&cmd->se_ordered_node); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3501 | atomic_dec(&dev->dev_ordered_sync); |
| 3502 | smp_mb__after_atomic_dec(); |
| 3503 | spin_unlock(&dev->ordered_cmd_lock); |
| 3504 | |
| 3505 | dev->dev_cur_ordered_id++; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3506 | pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3507 | " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id); |
| 3508 | } |
| 3509 | /* |
| 3510 | * Process all commands up to the last received |
| 3511 | * ORDERED task attribute which requires another blocking |
| 3512 | * boundary |
| 3513 | */ |
| 3514 | spin_lock(&dev->delayed_cmd_lock); |
| 3515 | list_for_each_entry_safe(cmd_p, cmd_tmp, |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3516 | &dev->delayed_cmd_list, se_delayed_node) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3517 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3518 | list_del(&cmd_p->se_delayed_node); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3519 | spin_unlock(&dev->delayed_cmd_lock); |
| 3520 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3521 | pr_debug("Calling add_tasks() for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3522 | " cmd_p: 0x%02x Task Attr: 0x%02x" |
| 3523 | " Dormant -> Active, se_ordered_id: %u\n", |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3524 | cmd_p->t_task_cdb[0], |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3525 | cmd_p->sam_task_attr, cmd_p->se_ordered_id); |
| 3526 | |
| 3527 | transport_add_tasks_from_cmd(cmd_p); |
| 3528 | new_active_tasks++; |
| 3529 | |
| 3530 | spin_lock(&dev->delayed_cmd_lock); |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 3531 | if (cmd_p->sam_task_attr == MSG_ORDERED_TAG) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3532 | break; |
| 3533 | } |
| 3534 | spin_unlock(&dev->delayed_cmd_lock); |
| 3535 | /* |
| 3536 | * If new tasks have become active, wake up the transport thread |
| 3537 | * to do the processing of the Active tasks. |
| 3538 | */ |
| 3539 | if (new_active_tasks != 0) |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3540 | wake_up_interruptible(&dev->dev_queue_obj.thread_wq); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3541 | } |
| 3542 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3543 | static int transport_complete_qf(struct se_cmd *cmd) |
| 3544 | { |
| 3545 | int ret = 0; |
| 3546 | |
| 3547 | if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) |
| 3548 | return cmd->se_tfo->queue_status(cmd); |
| 3549 | |
| 3550 | switch (cmd->data_direction) { |
| 3551 | case DMA_FROM_DEVICE: |
| 3552 | ret = cmd->se_tfo->queue_data_in(cmd); |
| 3553 | break; |
| 3554 | case DMA_TO_DEVICE: |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3555 | if (cmd->t_bidi_data_sg) { |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3556 | ret = cmd->se_tfo->queue_data_in(cmd); |
| 3557 | if (ret < 0) |
| 3558 | return ret; |
| 3559 | } |
| 3560 | /* Fall through for DMA_TO_DEVICE */ |
| 3561 | case DMA_NONE: |
| 3562 | ret = cmd->se_tfo->queue_status(cmd); |
| 3563 | break; |
| 3564 | default: |
| 3565 | break; |
| 3566 | } |
| 3567 | |
| 3568 | return ret; |
| 3569 | } |
| 3570 | |
| 3571 | static void transport_handle_queue_full( |
| 3572 | struct se_cmd *cmd, |
| 3573 | struct se_device *dev, |
| 3574 | int (*qf_callback)(struct se_cmd *)) |
| 3575 | { |
| 3576 | spin_lock_irq(&dev->qf_cmd_lock); |
| 3577 | cmd->se_cmd_flags |= SCF_EMULATE_QUEUE_FULL; |
| 3578 | cmd->transport_qf_callback = qf_callback; |
| 3579 | list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list); |
| 3580 | atomic_inc(&dev->dev_qf_count); |
| 3581 | smp_mb__after_atomic_inc(); |
| 3582 | spin_unlock_irq(&cmd->se_dev->qf_cmd_lock); |
| 3583 | |
| 3584 | schedule_work(&cmd->se_dev->qf_work_queue); |
| 3585 | } |
| 3586 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3587 | static void transport_generic_complete_ok(struct se_cmd *cmd) |
| 3588 | { |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3589 | int reason = 0, ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3590 | /* |
| 3591 | * Check if we need to move delayed/dormant tasks from cmds on the |
| 3592 | * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task |
| 3593 | * Attribute. |
| 3594 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3595 | if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3596 | transport_complete_task_attr(cmd); |
| 3597 | /* |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3598 | * Check to schedule QUEUE_FULL work, or execute an existing |
| 3599 | * cmd->transport_qf_callback() |
| 3600 | */ |
| 3601 | if (atomic_read(&cmd->se_dev->dev_qf_count) != 0) |
| 3602 | schedule_work(&cmd->se_dev->qf_work_queue); |
| 3603 | |
| 3604 | if (cmd->transport_qf_callback) { |
| 3605 | ret = cmd->transport_qf_callback(cmd); |
| 3606 | if (ret < 0) |
| 3607 | goto queue_full; |
| 3608 | |
| 3609 | cmd->transport_qf_callback = NULL; |
| 3610 | goto done; |
| 3611 | } |
| 3612 | /* |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3613 | * Check if we need to retrieve a sense buffer from |
| 3614 | * the struct se_cmd in question. |
| 3615 | */ |
| 3616 | if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) { |
| 3617 | if (transport_get_sense_data(cmd) < 0) |
| 3618 | reason = TCM_NON_EXISTENT_LUN; |
| 3619 | |
| 3620 | /* |
| 3621 | * Only set when an struct se_task->task_scsi_status returned |
| 3622 | * a non GOOD status. |
| 3623 | */ |
| 3624 | if (cmd->scsi_status) { |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3625 | ret = transport_send_check_condition_and_sense( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3626 | cmd, reason, 1); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3627 | if (ret == -EAGAIN) |
| 3628 | goto queue_full; |
| 3629 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3630 | transport_lun_remove_cmd(cmd); |
| 3631 | transport_cmd_check_stop_to_fabric(cmd); |
| 3632 | return; |
| 3633 | } |
| 3634 | } |
| 3635 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3636 | * Check for a callback, used by amongst other things |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3637 | * XDWRITE_READ_10 emulation. |
| 3638 | */ |
| 3639 | if (cmd->transport_complete_callback) |
| 3640 | cmd->transport_complete_callback(cmd); |
| 3641 | |
| 3642 | switch (cmd->data_direction) { |
| 3643 | case DMA_FROM_DEVICE: |
| 3644 | spin_lock(&cmd->se_lun->lun_sep_lock); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3645 | if (cmd->se_lun->lun_sep) { |
| 3646 | cmd->se_lun->lun_sep->sep_stats.tx_data_octets += |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3647 | cmd->data_length; |
| 3648 | } |
| 3649 | spin_unlock(&cmd->se_lun->lun_sep_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3650 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3651 | ret = cmd->se_tfo->queue_data_in(cmd); |
| 3652 | if (ret == -EAGAIN) |
| 3653 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3654 | break; |
| 3655 | case DMA_TO_DEVICE: |
| 3656 | spin_lock(&cmd->se_lun->lun_sep_lock); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3657 | if (cmd->se_lun->lun_sep) { |
| 3658 | cmd->se_lun->lun_sep->sep_stats.rx_data_octets += |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3659 | cmd->data_length; |
| 3660 | } |
| 3661 | spin_unlock(&cmd->se_lun->lun_sep_lock); |
| 3662 | /* |
| 3663 | * Check if we need to send READ payload for BIDI-COMMAND |
| 3664 | */ |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3665 | if (cmd->t_bidi_data_sg) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3666 | spin_lock(&cmd->se_lun->lun_sep_lock); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3667 | if (cmd->se_lun->lun_sep) { |
| 3668 | cmd->se_lun->lun_sep->sep_stats.tx_data_octets += |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3669 | cmd->data_length; |
| 3670 | } |
| 3671 | spin_unlock(&cmd->se_lun->lun_sep_lock); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3672 | ret = cmd->se_tfo->queue_data_in(cmd); |
| 3673 | if (ret == -EAGAIN) |
| 3674 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3675 | break; |
| 3676 | } |
| 3677 | /* Fall through for DMA_TO_DEVICE */ |
| 3678 | case DMA_NONE: |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3679 | ret = cmd->se_tfo->queue_status(cmd); |
| 3680 | if (ret == -EAGAIN) |
| 3681 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3682 | break; |
| 3683 | default: |
| 3684 | break; |
| 3685 | } |
| 3686 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3687 | done: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3688 | transport_lun_remove_cmd(cmd); |
| 3689 | transport_cmd_check_stop_to_fabric(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3690 | return; |
| 3691 | |
| 3692 | queue_full: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3693 | pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p," |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3694 | " data_direction: %d\n", cmd, cmd->data_direction); |
| 3695 | transport_handle_queue_full(cmd, cmd->se_dev, transport_complete_qf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | static void transport_free_dev_tasks(struct se_cmd *cmd) |
| 3699 | { |
| 3700 | struct se_task *task, *task_tmp; |
| 3701 | unsigned long flags; |
| 3702 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3703 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3704 | list_for_each_entry_safe(task, task_tmp, |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3705 | &cmd->t_task_list, t_list) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3706 | if (atomic_read(&task->task_active)) |
| 3707 | continue; |
| 3708 | |
| 3709 | kfree(task->task_sg_bidi); |
| 3710 | kfree(task->task_sg); |
| 3711 | |
| 3712 | list_del(&task->t_list); |
| 3713 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3714 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3715 | if (task->se_dev) |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3716 | task->se_dev->transport->free_task(task); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3717 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3718 | pr_err("task[%u] - task->se_dev is NULL\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3719 | task->task_no); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3720 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3721 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3722 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3723 | } |
| 3724 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3725 | static inline void transport_free_sgl(struct scatterlist *sgl, int nents) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3726 | { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3727 | struct scatterlist *sg; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3728 | int count; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3729 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3730 | for_each_sg(sgl, sg, nents, count) |
| 3731 | __free_page(sg_page(sg)); |
| 3732 | |
| 3733 | kfree(sgl); |
| 3734 | } |
| 3735 | |
| 3736 | static inline void transport_free_pages(struct se_cmd *cmd) |
| 3737 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3738 | if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3739 | return; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3740 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3741 | transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents); |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3742 | cmd->t_data_sg = NULL; |
| 3743 | cmd->t_data_nents = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3744 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3745 | transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents); |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3746 | cmd->t_bidi_data_sg = NULL; |
| 3747 | cmd->t_bidi_data_nents = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | static inline void transport_release_tasks(struct se_cmd *cmd) |
| 3751 | { |
| 3752 | transport_free_dev_tasks(cmd); |
| 3753 | } |
| 3754 | |
| 3755 | static inline int transport_dec_and_check(struct se_cmd *cmd) |
| 3756 | { |
| 3757 | unsigned long flags; |
| 3758 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3759 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 3760 | if (atomic_read(&cmd->t_fe_count)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3761 | if (!atomic_dec_and_test(&cmd->t_fe_count)) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3762 | spin_unlock_irqrestore(&cmd->t_state_lock, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3763 | flags); |
| 3764 | return 1; |
| 3765 | } |
| 3766 | } |
| 3767 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3768 | if (atomic_read(&cmd->t_se_count)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3769 | if (!atomic_dec_and_test(&cmd->t_se_count)) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3770 | spin_unlock_irqrestore(&cmd->t_state_lock, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3771 | flags); |
| 3772 | return 1; |
| 3773 | } |
| 3774 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3775 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3776 | |
| 3777 | return 0; |
| 3778 | } |
| 3779 | |
| 3780 | static void transport_release_fe_cmd(struct se_cmd *cmd) |
| 3781 | { |
| 3782 | unsigned long flags; |
| 3783 | |
| 3784 | if (transport_dec_and_check(cmd)) |
| 3785 | return; |
| 3786 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3787 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3788 | if (!atomic_read(&cmd->transport_dev_active)) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3789 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3790 | goto free_pages; |
| 3791 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3792 | atomic_set(&cmd->transport_dev_active, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3793 | transport_all_task_dev_remove_state(cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3794 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3795 | |
| 3796 | transport_release_tasks(cmd); |
| 3797 | free_pages: |
| 3798 | transport_free_pages(cmd); |
| 3799 | transport_free_se_cmd(cmd); |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 3800 | cmd->se_tfo->release_cmd(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3801 | } |
| 3802 | |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 3803 | static int |
| 3804 | transport_generic_remove(struct se_cmd *cmd, int session_reinstatement) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3805 | { |
| 3806 | unsigned long flags; |
| 3807 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3808 | if (transport_dec_and_check(cmd)) { |
| 3809 | if (session_reinstatement) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3810 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3811 | transport_all_task_dev_remove_state(cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3812 | spin_unlock_irqrestore(&cmd->t_state_lock, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3813 | flags); |
| 3814 | } |
| 3815 | return 1; |
| 3816 | } |
| 3817 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3818 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3819 | if (!atomic_read(&cmd->transport_dev_active)) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3820 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3821 | goto free_pages; |
| 3822 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3823 | atomic_set(&cmd->transport_dev_active, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3824 | transport_all_task_dev_remove_state(cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3825 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3826 | |
| 3827 | transport_release_tasks(cmd); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3828 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3829 | free_pages: |
| 3830 | transport_free_pages(cmd); |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 3831 | transport_release_cmd(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3832 | return 0; |
| 3833 | } |
| 3834 | |
| 3835 | /* |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3836 | * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of |
| 3837 | * allocating in the core. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3838 | * @cmd: Associated se_cmd descriptor |
| 3839 | * @mem: SGL style memory for TCM WRITE / READ |
| 3840 | * @sg_mem_num: Number of SGL elements |
| 3841 | * @mem_bidi_in: SGL style memory for TCM BIDI READ |
| 3842 | * @sg_mem_bidi_num: Number of BIDI READ SGL elements |
| 3843 | * |
| 3844 | * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage |
| 3845 | * of parameters. |
| 3846 | */ |
| 3847 | int transport_generic_map_mem_to_cmd( |
| 3848 | struct se_cmd *cmd, |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3849 | struct scatterlist *sgl, |
| 3850 | u32 sgl_count, |
| 3851 | struct scatterlist *sgl_bidi, |
| 3852 | u32 sgl_bidi_count) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3853 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3854 | if (!sgl || !sgl_count) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3855 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3856 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3857 | if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) || |
| 3858 | (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3859 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3860 | cmd->t_data_sg = sgl; |
| 3861 | cmd->t_data_nents = sgl_count; |
| 3862 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3863 | if (sgl_bidi && sgl_bidi_count) { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3864 | cmd->t_bidi_data_sg = sgl_bidi; |
| 3865 | cmd->t_bidi_data_nents = sgl_bidi_count; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3866 | } |
| 3867 | cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | return 0; |
| 3871 | } |
| 3872 | EXPORT_SYMBOL(transport_generic_map_mem_to_cmd); |
| 3873 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3874 | static int transport_new_cmd_obj(struct se_cmd *cmd) |
| 3875 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3876 | struct se_device *dev = cmd->se_dev; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3877 | u32 task_cdbs; |
| 3878 | u32 rc; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3879 | int set_counts = 1; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3880 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3881 | /* |
| 3882 | * Setup any BIDI READ tasks and memory from |
| 3883 | * cmd->t_mem_bidi_list so the READ struct se_tasks |
| 3884 | * are queued first for the non pSCSI passthrough case. |
| 3885 | */ |
| 3886 | if (cmd->t_bidi_data_sg && |
| 3887 | (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) { |
| 3888 | rc = transport_allocate_tasks(cmd, |
| 3889 | cmd->t_task_lba, |
| 3890 | DMA_FROM_DEVICE, |
| 3891 | cmd->t_bidi_data_sg, |
| 3892 | cmd->t_bidi_data_nents); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3893 | if (rc <= 0) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3894 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 3895 | cmd->scsi_sense_reason = |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3896 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3897 | return PYX_TRANSPORT_LU_COMM_FAILURE; |
| 3898 | } |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3899 | atomic_inc(&cmd->t_fe_count); |
| 3900 | atomic_inc(&cmd->t_se_count); |
| 3901 | set_counts = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3902 | } |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3903 | /* |
| 3904 | * Setup the tasks and memory from cmd->t_mem_list |
| 3905 | * Note for BIDI transfers this will contain the WRITE payload |
| 3906 | */ |
| 3907 | task_cdbs = transport_allocate_tasks(cmd, |
| 3908 | cmd->t_task_lba, |
| 3909 | cmd->data_direction, |
| 3910 | cmd->t_data_sg, |
| 3911 | cmd->t_data_nents); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 3912 | if (task_cdbs <= 0) { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3913 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 3914 | cmd->scsi_sense_reason = |
| 3915 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 3916 | return PYX_TRANSPORT_LU_COMM_FAILURE; |
| 3917 | } |
| 3918 | |
| 3919 | if (set_counts) { |
| 3920 | atomic_inc(&cmd->t_fe_count); |
| 3921 | atomic_inc(&cmd->t_se_count); |
| 3922 | } |
| 3923 | |
| 3924 | cmd->t_task_list_num = task_cdbs; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3925 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3926 | atomic_set(&cmd->t_task_cdbs_left, task_cdbs); |
| 3927 | atomic_set(&cmd->t_task_cdbs_ex_left, task_cdbs); |
| 3928 | atomic_set(&cmd->t_task_cdbs_timeout_left, task_cdbs); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3929 | return 0; |
| 3930 | } |
| 3931 | |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3932 | void *transport_kmap_first_data_page(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3933 | { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3934 | struct scatterlist *sg = cmd->t_data_sg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3935 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3936 | BUG_ON(!sg); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3937 | /* |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3938 | * We need to take into account a possible offset here for fabrics like |
| 3939 | * tcm_loop who may be using a contig buffer from the SCSI midlayer for |
| 3940 | * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd() |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3941 | */ |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3942 | return kmap(sg_page(sg)) + sg->offset; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3943 | } |
| 3944 | EXPORT_SYMBOL(transport_kmap_first_data_page); |
| 3945 | |
| 3946 | void transport_kunmap_first_data_page(struct se_cmd *cmd) |
| 3947 | { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3948 | kunmap(sg_page(cmd->t_data_sg)); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3949 | } |
| 3950 | EXPORT_SYMBOL(transport_kunmap_first_data_page); |
| 3951 | |
| 3952 | static int |
| 3953 | transport_generic_get_mem(struct se_cmd *cmd) |
| 3954 | { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3955 | u32 length = cmd->data_length; |
| 3956 | unsigned int nents; |
| 3957 | struct page *page; |
| 3958 | int i = 0; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 3959 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3960 | nents = DIV_ROUND_UP(length, PAGE_SIZE); |
| 3961 | cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL); |
| 3962 | if (!cmd->t_data_sg) |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3963 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3964 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3965 | cmd->t_data_nents = nents; |
| 3966 | sg_init_table(cmd->t_data_sg, nents); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3967 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 3968 | while (length) { |
| 3969 | u32 page_len = min_t(u32, length, PAGE_SIZE); |
| 3970 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 3971 | if (!page) |
| 3972 | goto out; |
| 3973 | |
| 3974 | sg_set_page(&cmd->t_data_sg[i], page, page_len, 0); |
| 3975 | length -= page_len; |
| 3976 | i++; |
| 3977 | } |
| 3978 | return 0; |
| 3979 | |
| 3980 | out: |
| 3981 | while (i >= 0) { |
| 3982 | __free_page(sg_page(&cmd->t_data_sg[i])); |
| 3983 | i--; |
| 3984 | } |
| 3985 | kfree(cmd->t_data_sg); |
| 3986 | cmd->t_data_sg = NULL; |
| 3987 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3988 | } |
| 3989 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3990 | /* Reduce sectors if they are too long for the device */ |
| 3991 | static inline sector_t transport_limit_task_sectors( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3992 | struct se_device *dev, |
| 3993 | unsigned long long lba, |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3994 | sector_t sectors) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3995 | { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3996 | sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3997 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3998 | if (dev->transport->get_device_type(dev) == TYPE_DISK) |
| 3999 | if ((lba + sectors) > transport_dev_end_lba(dev)) |
| 4000 | sectors = ((transport_dev_end_lba(dev) - lba) + 1); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4001 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4002 | return sectors; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4003 | } |
| 4004 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4005 | |
| 4006 | /* |
| 4007 | * This function can be used by HW target mode drivers to create a linked |
| 4008 | * scatterlist from all contiguously allocated struct se_task->task_sg[]. |
| 4009 | * This is intended to be called during the completion path by TCM Core |
| 4010 | * when struct target_core_fabric_ops->check_task_sg_chaining is enabled. |
| 4011 | */ |
| 4012 | void transport_do_task_sg_chain(struct se_cmd *cmd) |
| 4013 | { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4014 | struct scatterlist *sg_first = NULL; |
| 4015 | struct scatterlist *sg_prev = NULL; |
| 4016 | int sg_prev_nents = 0; |
| 4017 | struct scatterlist *sg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4018 | struct se_task *task; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4019 | u32 chained_nents = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4020 | int i; |
| 4021 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4022 | BUG_ON(!cmd->se_tfo->task_sg_chaining); |
| 4023 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4024 | /* |
| 4025 | * Walk the struct se_task list and setup scatterlist chains |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4026 | * for each contiguously allocated struct se_task->task_sg[]. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4027 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4028 | list_for_each_entry(task, &cmd->t_task_list, t_list) { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4029 | if (!task->task_sg) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4030 | continue; |
| 4031 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4032 | BUG_ON(!task->task_padded_sg); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4033 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4034 | if (!sg_first) { |
| 4035 | sg_first = task->task_sg; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4036 | chained_nents = task->task_sg_nents; |
Nicholas Bellinger | 97868c8 | 2011-05-19 20:19:09 -0700 | [diff] [blame] | 4037 | } else { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4038 | sg_chain(sg_prev, sg_prev_nents, task->task_sg); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4039 | chained_nents += task->task_sg_nents; |
Nicholas Bellinger | 97868c8 | 2011-05-19 20:19:09 -0700 | [diff] [blame] | 4040 | } |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4041 | |
| 4042 | sg_prev = task->task_sg; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4043 | sg_prev_nents = task->task_sg_nents; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4044 | } |
| 4045 | /* |
| 4046 | * Setup the starting pointer and total t_tasks_sg_linked_no including |
| 4047 | * padding SGs for linking and to mark the end. |
| 4048 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4049 | cmd->t_tasks_sg_chained = sg_first; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4050 | cmd->t_tasks_sg_chained_no = chained_nents; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4051 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4052 | pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and" |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4053 | " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained, |
| 4054 | cmd->t_tasks_sg_chained_no); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4055 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4056 | for_each_sg(cmd->t_tasks_sg_chained, sg, |
| 4057 | cmd->t_tasks_sg_chained_no, i) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4058 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4059 | pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n", |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 4060 | i, sg, sg_page(sg), sg->length, sg->offset); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4061 | if (sg_is_chain(sg)) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4062 | pr_debug("SG: %p sg_is_chain=1\n", sg); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4063 | if (sg_is_last(sg)) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4064 | pr_debug("SG: %p sg_is_last=1\n", sg); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4065 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4066 | } |
| 4067 | EXPORT_SYMBOL(transport_do_task_sg_chain); |
| 4068 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4069 | /* |
| 4070 | * Break up cmd into chunks transport can handle |
| 4071 | */ |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4072 | static int transport_allocate_data_tasks( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4073 | struct se_cmd *cmd, |
| 4074 | unsigned long long lba, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4075 | enum dma_data_direction data_direction, |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4076 | struct scatterlist *sgl, |
| 4077 | unsigned int sgl_nents) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4078 | { |
| 4079 | unsigned char *cdb = NULL; |
| 4080 | struct se_task *task; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 4081 | struct se_device *dev = cmd->se_dev; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4082 | unsigned long flags; |
| 4083 | sector_t sectors; |
| 4084 | int task_count; |
| 4085 | int i; |
| 4086 | sector_t dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors; |
| 4087 | u32 sector_size = dev->se_sub_dev->se_dev_attrib.block_size; |
| 4088 | struct scatterlist *sg; |
| 4089 | struct scatterlist *cmd_sg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4090 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4091 | WARN_ON(cmd->data_length % sector_size); |
| 4092 | sectors = DIV_ROUND_UP(cmd->data_length, sector_size); |
| 4093 | task_count = DIV_ROUND_UP(sectors, dev_max_sectors); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4094 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4095 | cmd_sg = sgl; |
| 4096 | for (i = 0; i < task_count; i++) { |
| 4097 | unsigned int task_size; |
| 4098 | int count; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4099 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4100 | task = transport_generic_get_task(cmd, data_direction); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4101 | if (!task) |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4102 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4103 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4104 | task->task_lba = lba; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4105 | task->task_sectors = min(sectors, dev_max_sectors); |
| 4106 | task->task_size = task->task_sectors * sector_size; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4107 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4108 | cdb = dev->transport->get_cdb(task); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4109 | BUG_ON(!cdb); |
| 4110 | |
| 4111 | memcpy(cdb, cmd->t_task_cdb, |
| 4112 | scsi_command_size(cmd->t_task_cdb)); |
| 4113 | |
| 4114 | /* Update new cdb with updated lba/sectors */ |
Andy Grover | 3a86720 | 2011-06-28 10:31:18 -0700 | [diff] [blame] | 4115 | cmd->transport_split_cdb(task->task_lba, task->task_sectors, cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4116 | |
| 4117 | /* |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4118 | * Check if the fabric module driver is requesting that all |
| 4119 | * struct se_task->task_sg[] be chained together.. If so, |
| 4120 | * then allocate an extra padding SG entry for linking and |
| 4121 | * marking the end of the chained SGL. |
| 4122 | * Possibly over-allocate task sgl size by using cmd sgl size. |
| 4123 | * It's so much easier and only a waste when task_count > 1. |
| 4124 | * That is extremely rare. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4125 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4126 | task->task_sg_nents = sgl_nents; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4127 | if (cmd->se_tfo->task_sg_chaining) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4128 | task->task_sg_nents++; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4129 | task->task_padded_sg = 1; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4130 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4131 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4132 | task->task_sg = kmalloc(sizeof(struct scatterlist) * \ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4133 | task->task_sg_nents, GFP_KERNEL); |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4134 | if (!task->task_sg) { |
| 4135 | cmd->se_dev->transport->free_task(task); |
| 4136 | return -ENOMEM; |
| 4137 | } |
| 4138 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4139 | sg_init_table(task->task_sg, task->task_sg_nents); |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4140 | |
| 4141 | task_size = task->task_size; |
| 4142 | |
| 4143 | /* Build new sgl, only up to task_size */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4144 | for_each_sg(task->task_sg, sg, task->task_sg_nents, count) { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4145 | if (cmd_sg->length > task_size) |
| 4146 | break; |
| 4147 | |
| 4148 | *sg = *cmd_sg; |
| 4149 | task_size -= cmd_sg->length; |
| 4150 | cmd_sg = sg_next(cmd_sg); |
| 4151 | } |
| 4152 | |
| 4153 | lba += task->task_sectors; |
| 4154 | sectors -= task->task_sectors; |
| 4155 | |
| 4156 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 4157 | list_add_tail(&task->t_list, &cmd->t_task_list); |
| 4158 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4159 | } |
| 4160 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4161 | return task_count; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | static int |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4165 | transport_allocate_control_task(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4166 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 4167 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4168 | unsigned char *cdb; |
| 4169 | struct se_task *task; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4170 | unsigned long flags; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4171 | int ret = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4172 | |
| 4173 | task = transport_generic_get_task(cmd, cmd->data_direction); |
| 4174 | if (!task) |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4175 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4176 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4177 | cdb = dev->transport->get_cdb(task); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4178 | BUG_ON(!cdb); |
| 4179 | memcpy(cdb, cmd->t_task_cdb, |
| 4180 | scsi_command_size(cmd->t_task_cdb)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4181 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4182 | task->task_sg = kmalloc(sizeof(struct scatterlist) * cmd->t_data_nents, |
| 4183 | GFP_KERNEL); |
| 4184 | if (!task->task_sg) { |
| 4185 | cmd->se_dev->transport->free_task(task); |
| 4186 | return -ENOMEM; |
| 4187 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4188 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4189 | memcpy(task->task_sg, cmd->t_data_sg, |
| 4190 | sizeof(struct scatterlist) * cmd->t_data_nents); |
| 4191 | task->task_size = cmd->data_length; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4192 | task->task_sg_nents = cmd->t_data_nents; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4193 | |
| 4194 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 4195 | list_add_tail(&task->t_list, &cmd->t_task_list); |
| 4196 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4197 | |
| 4198 | if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4199 | if (dev->transport->map_task_SG) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4200 | ret = dev->transport->map_task_SG(task); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4201 | } else if (cmd->se_cmd_flags & SCF_SCSI_NON_DATA_CDB) { |
| 4202 | if (dev->transport->cdb_none) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4203 | ret = dev->transport->cdb_none(task); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4204 | } else { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4205 | pr_err("target: Unknown control cmd type!\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4206 | BUG(); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4207 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4208 | |
| 4209 | /* Success! Return number of tasks allocated */ |
| 4210 | if (ret == 0) |
| 4211 | return 1; |
| 4212 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4213 | } |
| 4214 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4215 | static u32 transport_allocate_tasks( |
| 4216 | struct se_cmd *cmd, |
| 4217 | unsigned long long lba, |
| 4218 | enum dma_data_direction data_direction, |
| 4219 | struct scatterlist *sgl, |
| 4220 | unsigned int sgl_nents) |
| 4221 | { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4222 | if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4223 | return transport_allocate_data_tasks(cmd, lba, data_direction, |
| 4224 | sgl, sgl_nents); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4225 | else |
| 4226 | return transport_allocate_control_task(cmd); |
| 4227 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4231 | /* transport_generic_new_cmd(): Called from transport_processing_thread() |
| 4232 | * |
| 4233 | * Allocate storage transport resources from a set of values predefined |
| 4234 | * by transport_generic_cmd_sequencer() from the iSCSI Target RX process. |
| 4235 | * Any non zero return here is treated as an "out of resource' op here. |
| 4236 | */ |
| 4237 | /* |
| 4238 | * Generate struct se_task(s) and/or their payloads for this CDB. |
| 4239 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4240 | int transport_generic_new_cmd(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4241 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4242 | struct se_task *task; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 4243 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4244 | int ret = 0; |
| 4245 | |
| 4246 | /* |
| 4247 | * Determine is the TCM fabric module has already allocated physical |
| 4248 | * memory, and is directly calling transport_generic_map_mem_to_cmd() |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4249 | * beforehand. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4250 | */ |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4251 | if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) && |
| 4252 | cmd->data_length) { |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 4253 | ret = transport_generic_get_mem(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4254 | if (ret < 0) |
| 4255 | return ret; |
| 4256 | } |
| 4257 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4258 | ret = transport_new_cmd_obj(cmd); |
| 4259 | if (ret < 0) |
| 4260 | return ret; |
| 4261 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4262 | list_for_each_entry(task, &cmd->t_task_list, t_list) { |
| 4263 | if (atomic_read(&task->task_sent)) |
| 4264 | continue; |
| 4265 | if (!dev->transport->map_task_SG) |
| 4266 | continue; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4267 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 4268 | ret = dev->transport->map_task_SG(task); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4269 | if (ret < 0) |
| 4270 | return ret; |
| 4271 | } |
| 4272 | |
| 4273 | /* |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4274 | * For WRITEs, let the fabric know its buffer is ready.. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4275 | * This WRITE struct se_cmd (and all of its associated struct se_task's) |
| 4276 | * will be added to the struct se_device execution queue after its WRITE |
| 4277 | * data has arrived. (ie: It gets handled by the transport processing |
| 4278 | * thread a second time) |
| 4279 | */ |
| 4280 | if (cmd->data_direction == DMA_TO_DEVICE) { |
| 4281 | transport_add_tasks_to_state_queue(cmd); |
| 4282 | return transport_generic_write_pending(cmd); |
| 4283 | } |
| 4284 | /* |
| 4285 | * Everything else but a WRITE, add the struct se_cmd's struct se_task's |
| 4286 | * to the execution queue. |
| 4287 | */ |
| 4288 | transport_execute_tasks(cmd); |
| 4289 | return 0; |
| 4290 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4291 | EXPORT_SYMBOL(transport_generic_new_cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4292 | |
| 4293 | /* transport_generic_process_write(): |
| 4294 | * |
| 4295 | * |
| 4296 | */ |
| 4297 | void transport_generic_process_write(struct se_cmd *cmd) |
| 4298 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4299 | transport_execute_tasks(cmd); |
| 4300 | } |
| 4301 | EXPORT_SYMBOL(transport_generic_process_write); |
| 4302 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 4303 | static int transport_write_pending_qf(struct se_cmd *cmd) |
| 4304 | { |
| 4305 | return cmd->se_tfo->write_pending(cmd); |
| 4306 | } |
| 4307 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4308 | /* transport_generic_write_pending(): |
| 4309 | * |
| 4310 | * |
| 4311 | */ |
| 4312 | static int transport_generic_write_pending(struct se_cmd *cmd) |
| 4313 | { |
| 4314 | unsigned long flags; |
| 4315 | int ret; |
| 4316 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4317 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4318 | cmd->t_state = TRANSPORT_WRITE_PENDING; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4319 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 4320 | |
| 4321 | if (cmd->transport_qf_callback) { |
| 4322 | ret = cmd->transport_qf_callback(cmd); |
| 4323 | if (ret == -EAGAIN) |
| 4324 | goto queue_full; |
| 4325 | else if (ret < 0) |
| 4326 | return ret; |
| 4327 | |
| 4328 | cmd->transport_qf_callback = NULL; |
| 4329 | return 0; |
| 4330 | } |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 4331 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4332 | /* |
| 4333 | * Clear the se_cmd for WRITE_PENDING status in order to set |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4334 | * cmd->t_transport_active=0 so that transport_generic_handle_data |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4335 | * can be called from HW target mode interrupt code. This is safe |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4336 | * to be called with transport_off=1 before the cmd->se_tfo->write_pending |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4337 | * because the se_cmd->se_lun pointer is not being cleared. |
| 4338 | */ |
| 4339 | transport_cmd_check_stop(cmd, 1, 0); |
| 4340 | |
| 4341 | /* |
| 4342 | * Call the fabric write_pending function here to let the |
| 4343 | * frontend know that WRITE buffers are ready. |
| 4344 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4345 | ret = cmd->se_tfo->write_pending(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 4346 | if (ret == -EAGAIN) |
| 4347 | goto queue_full; |
| 4348 | else if (ret < 0) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4349 | return ret; |
| 4350 | |
| 4351 | return PYX_TRANSPORT_WRITE_PENDING; |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 4352 | |
| 4353 | queue_full: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4354 | pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 4355 | cmd->t_state = TRANSPORT_COMPLETE_QF_WP; |
| 4356 | transport_handle_queue_full(cmd, cmd->se_dev, |
| 4357 | transport_write_pending_qf); |
| 4358 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4359 | } |
| 4360 | |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 4361 | void transport_release_cmd(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4362 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4363 | BUG_ON(!cmd->se_tfo); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4364 | |
| 4365 | transport_free_se_cmd(cmd); |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 4366 | cmd->se_tfo->release_cmd(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4367 | } |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 4368 | EXPORT_SYMBOL(transport_release_cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4369 | |
| 4370 | /* transport_generic_free_cmd(): |
| 4371 | * |
| 4372 | * Called from processing frontend to release storage engine resources |
| 4373 | */ |
| 4374 | void transport_generic_free_cmd( |
| 4375 | struct se_cmd *cmd, |
| 4376 | int wait_for_tasks, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4377 | int session_reinstatement) |
| 4378 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 4379 | if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 4380 | transport_release_cmd(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4381 | else { |
| 4382 | core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd); |
| 4383 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4384 | if (cmd->se_lun) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4385 | #if 0 |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4386 | pr_debug("cmd: %p ITT: 0x%08x contains" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4387 | " cmd->se_lun\n", cmd, |
| 4388 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4389 | #endif |
| 4390 | transport_lun_remove_cmd(cmd); |
| 4391 | } |
| 4392 | |
| 4393 | if (wait_for_tasks && cmd->transport_wait_for_tasks) |
| 4394 | cmd->transport_wait_for_tasks(cmd, 0, 0); |
| 4395 | |
Nicholas Bellinger | f436677 | 2011-05-19 20:19:11 -0700 | [diff] [blame] | 4396 | transport_free_dev_tasks(cmd); |
| 4397 | |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 4398 | transport_generic_remove(cmd, session_reinstatement); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4399 | } |
| 4400 | } |
| 4401 | EXPORT_SYMBOL(transport_generic_free_cmd); |
| 4402 | |
| 4403 | static void transport_nop_wait_for_tasks( |
| 4404 | struct se_cmd *cmd, |
| 4405 | int remove_cmd, |
| 4406 | int session_reinstatement) |
| 4407 | { |
| 4408 | return; |
| 4409 | } |
| 4410 | |
| 4411 | /* transport_lun_wait_for_tasks(): |
| 4412 | * |
| 4413 | * Called from ConfigFS context to stop the passed struct se_cmd to allow |
| 4414 | * an struct se_lun to be successfully shutdown. |
| 4415 | */ |
| 4416 | static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun) |
| 4417 | { |
| 4418 | unsigned long flags; |
| 4419 | int ret; |
| 4420 | /* |
| 4421 | * If the frontend has already requested this struct se_cmd to |
| 4422 | * be stopped, we can safely ignore this struct se_cmd. |
| 4423 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4424 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 4425 | if (atomic_read(&cmd->t_transport_stop)) { |
| 4426 | atomic_set(&cmd->transport_lun_stop, 0); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4427 | pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop ==" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4428 | " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd)); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4429 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4430 | transport_cmd_check_stop(cmd, 1, 0); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4431 | return -EPERM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4432 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4433 | atomic_set(&cmd->transport_lun_fe_stop, 1); |
| 4434 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4435 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 4436 | wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4437 | |
| 4438 | ret = transport_stop_tasks_for_cmd(cmd); |
| 4439 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4440 | pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:" |
| 4441 | " %d\n", cmd, cmd->t_task_list_num, ret); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4442 | if (!ret) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4443 | pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4444 | cmd->se_tfo->get_task_tag(cmd)); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4445 | wait_for_completion(&cmd->transport_lun_stop_comp); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4446 | pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4447 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4448 | } |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 4449 | transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4450 | |
| 4451 | return 0; |
| 4452 | } |
| 4453 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4454 | static void __transport_clear_lun_from_sessions(struct se_lun *lun) |
| 4455 | { |
| 4456 | struct se_cmd *cmd = NULL; |
| 4457 | unsigned long lun_flags, cmd_flags; |
| 4458 | /* |
| 4459 | * Do exception processing and return CHECK_CONDITION status to the |
| 4460 | * Initiator Port. |
| 4461 | */ |
| 4462 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 4463 | while (!list_empty(&lun->lun_cmd_list)) { |
| 4464 | cmd = list_first_entry(&lun->lun_cmd_list, |
| 4465 | struct se_cmd, se_lun_node); |
| 4466 | list_del(&cmd->se_lun_node); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4467 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4468 | atomic_set(&cmd->transport_lun_active, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4469 | /* |
| 4470 | * This will notify iscsi_target_transport.c: |
| 4471 | * transport_cmd_check_stop() that a LUN shutdown is in |
| 4472 | * progress for the iscsi_cmd_t. |
| 4473 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4474 | spin_lock(&cmd->t_state_lock); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4475 | pr_debug("SE_LUN[%d] - Setting cmd->transport" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4476 | "_lun_stop for ITT: 0x%08x\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4477 | cmd->se_lun->unpacked_lun, |
| 4478 | cmd->se_tfo->get_task_tag(cmd)); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4479 | atomic_set(&cmd->transport_lun_stop, 1); |
| 4480 | spin_unlock(&cmd->t_state_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4481 | |
| 4482 | spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags); |
| 4483 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4484 | if (!cmd->se_lun) { |
| 4485 | pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4486 | cmd->se_tfo->get_task_tag(cmd), |
| 4487 | cmd->se_tfo->get_cmd_state(cmd), cmd->t_state); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4488 | BUG(); |
| 4489 | } |
| 4490 | /* |
| 4491 | * If the Storage engine still owns the iscsi_cmd_t, determine |
| 4492 | * and/or stop its context. |
| 4493 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4494 | pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4495 | "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun, |
| 4496 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4497 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4498 | if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4499 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); |
| 4500 | continue; |
| 4501 | } |
| 4502 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4503 | pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4504 | "_wait_for_tasks(): SUCCESS\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4505 | cmd->se_lun->unpacked_lun, |
| 4506 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4507 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4508 | spin_lock_irqsave(&cmd->t_state_lock, cmd_flags); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4509 | if (!atomic_read(&cmd->transport_dev_active)) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4510 | spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4511 | goto check_cond; |
| 4512 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4513 | atomic_set(&cmd->transport_dev_active, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4514 | transport_all_task_dev_remove_state(cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4515 | spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4516 | |
| 4517 | transport_free_dev_tasks(cmd); |
| 4518 | /* |
| 4519 | * The Storage engine stopped this struct se_cmd before it was |
| 4520 | * send to the fabric frontend for delivery back to the |
| 4521 | * Initiator Node. Return this SCSI CDB back with an |
| 4522 | * CHECK_CONDITION status. |
| 4523 | */ |
| 4524 | check_cond: |
| 4525 | transport_send_check_condition_and_sense(cmd, |
| 4526 | TCM_NON_EXISTENT_LUN, 0); |
| 4527 | /* |
| 4528 | * If the fabric frontend is waiting for this iscsi_cmd_t to |
| 4529 | * be released, notify the waiting thread now that LU has |
| 4530 | * finished accessing it. |
| 4531 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4532 | spin_lock_irqsave(&cmd->t_state_lock, cmd_flags); |
| 4533 | if (atomic_read(&cmd->transport_lun_fe_stop)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4534 | pr_debug("SE_LUN[%d] - Detected FE stop for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4535 | " struct se_cmd: %p ITT: 0x%08x\n", |
| 4536 | lun->unpacked_lun, |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4537 | cmd, cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4538 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4539 | spin_unlock_irqrestore(&cmd->t_state_lock, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4540 | cmd_flags); |
| 4541 | transport_cmd_check_stop(cmd, 1, 0); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4542 | complete(&cmd->transport_lun_fe_stop_comp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4543 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); |
| 4544 | continue; |
| 4545 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4546 | pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4547 | lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4548 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4549 | spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4550 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); |
| 4551 | } |
| 4552 | spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags); |
| 4553 | } |
| 4554 | |
| 4555 | static int transport_clear_lun_thread(void *p) |
| 4556 | { |
| 4557 | struct se_lun *lun = (struct se_lun *)p; |
| 4558 | |
| 4559 | __transport_clear_lun_from_sessions(lun); |
| 4560 | complete(&lun->lun_shutdown_comp); |
| 4561 | |
| 4562 | return 0; |
| 4563 | } |
| 4564 | |
| 4565 | int transport_clear_lun_from_sessions(struct se_lun *lun) |
| 4566 | { |
| 4567 | struct task_struct *kt; |
| 4568 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 4569 | kt = kthread_run(transport_clear_lun_thread, lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4570 | "tcm_cl_%u", lun->unpacked_lun); |
| 4571 | if (IS_ERR(kt)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4572 | pr_err("Unable to start clear_lun thread\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4573 | return PTR_ERR(kt); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4574 | } |
| 4575 | wait_for_completion(&lun->lun_shutdown_comp); |
| 4576 | |
| 4577 | return 0; |
| 4578 | } |
| 4579 | |
| 4580 | /* transport_generic_wait_for_tasks(): |
| 4581 | * |
| 4582 | * Called from frontend or passthrough context to wait for storage engine |
| 4583 | * to pause and/or release frontend generated struct se_cmd. |
| 4584 | */ |
| 4585 | static void transport_generic_wait_for_tasks( |
| 4586 | struct se_cmd *cmd, |
| 4587 | int remove_cmd, |
| 4588 | int session_reinstatement) |
| 4589 | { |
| 4590 | unsigned long flags; |
| 4591 | |
| 4592 | if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req)) |
| 4593 | return; |
| 4594 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4595 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4596 | /* |
| 4597 | * If we are already stopped due to an external event (ie: LUN shutdown) |
| 4598 | * sleep until the connection can have the passed struct se_cmd back. |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4599 | * The cmd->transport_lun_stopped_sem will be upped by |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4600 | * transport_clear_lun_from_sessions() once the ConfigFS context caller |
| 4601 | * has completed its operation on the struct se_cmd. |
| 4602 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4603 | if (atomic_read(&cmd->transport_lun_stop)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4604 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4605 | pr_debug("wait_for_tasks: Stopping" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4606 | " wait_for_completion(&cmd->t_tasktransport_lun_fe" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4607 | "_stop_comp); for ITT: 0x%08x\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4608 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4609 | /* |
| 4610 | * There is a special case for WRITES where a FE exception + |
| 4611 | * LUN shutdown means ConfigFS context is still sleeping on |
| 4612 | * transport_lun_stop_comp in transport_lun_wait_for_tasks(). |
| 4613 | * We go ahead and up transport_lun_stop_comp just to be sure |
| 4614 | * here. |
| 4615 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4616 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 4617 | complete(&cmd->transport_lun_stop_comp); |
| 4618 | wait_for_completion(&cmd->transport_lun_fe_stop_comp); |
| 4619 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4620 | |
| 4621 | transport_all_task_dev_remove_state(cmd); |
| 4622 | /* |
| 4623 | * At this point, the frontend who was the originator of this |
| 4624 | * struct se_cmd, now owns the structure and can be released through |
| 4625 | * normal means below. |
| 4626 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4627 | pr_debug("wait_for_tasks: Stopped" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4628 | " wait_for_completion(&cmd->t_tasktransport_lun_fe_" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4629 | "stop_comp); for ITT: 0x%08x\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4630 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4631 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4632 | atomic_set(&cmd->transport_lun_stop, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4633 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4634 | if (!atomic_read(&cmd->t_transport_active) || |
| 4635 | atomic_read(&cmd->t_transport_aborted)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4636 | goto remove; |
| 4637 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4638 | atomic_set(&cmd->t_transport_stop, 1); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4639 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4640 | pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4641 | " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4642 | " = TRUE\n", cmd, cmd->se_tfo->get_task_tag(cmd), |
| 4643 | cmd->se_tfo->get_cmd_state(cmd), cmd->t_state, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4644 | cmd->deferred_t_state); |
| 4645 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4646 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4647 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 4648 | wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4649 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4650 | wait_for_completion(&cmd->t_transport_stop_comp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4651 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4652 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 4653 | atomic_set(&cmd->t_transport_active, 0); |
| 4654 | atomic_set(&cmd->t_transport_stop, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4655 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4656 | pr_debug("wait_for_tasks: Stopped wait_for_compltion(" |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4657 | "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4658 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4659 | remove: |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4660 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4661 | if (!remove_cmd) |
| 4662 | return; |
| 4663 | |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 4664 | transport_generic_free_cmd(cmd, 0, session_reinstatement); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4665 | } |
| 4666 | |
| 4667 | static int transport_get_sense_codes( |
| 4668 | struct se_cmd *cmd, |
| 4669 | u8 *asc, |
| 4670 | u8 *ascq) |
| 4671 | { |
| 4672 | *asc = cmd->scsi_asc; |
| 4673 | *ascq = cmd->scsi_ascq; |
| 4674 | |
| 4675 | return 0; |
| 4676 | } |
| 4677 | |
| 4678 | static int transport_set_sense_codes( |
| 4679 | struct se_cmd *cmd, |
| 4680 | u8 asc, |
| 4681 | u8 ascq) |
| 4682 | { |
| 4683 | cmd->scsi_asc = asc; |
| 4684 | cmd->scsi_ascq = ascq; |
| 4685 | |
| 4686 | return 0; |
| 4687 | } |
| 4688 | |
| 4689 | int transport_send_check_condition_and_sense( |
| 4690 | struct se_cmd *cmd, |
| 4691 | u8 reason, |
| 4692 | int from_transport) |
| 4693 | { |
| 4694 | unsigned char *buffer = cmd->sense_buffer; |
| 4695 | unsigned long flags; |
| 4696 | int offset; |
| 4697 | u8 asc = 0, ascq = 0; |
| 4698 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4699 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4700 | if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4701 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4702 | return 0; |
| 4703 | } |
| 4704 | cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4705 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4706 | |
| 4707 | if (!reason && from_transport) |
| 4708 | goto after_reason; |
| 4709 | |
| 4710 | if (!from_transport) |
| 4711 | cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE; |
| 4712 | /* |
| 4713 | * Data Segment and SenseLength of the fabric response PDU. |
| 4714 | * |
| 4715 | * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE |
| 4716 | * from include/scsi/scsi_cmnd.h |
| 4717 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4718 | offset = cmd->se_tfo->set_fabric_sense_len(cmd, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4719 | TRANSPORT_SENSE_BUFFER); |
| 4720 | /* |
| 4721 | * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses |
| 4722 | * SENSE KEY values from include/scsi/scsi.h |
| 4723 | */ |
| 4724 | switch (reason) { |
| 4725 | case TCM_NON_EXISTENT_LUN: |
| 4726 | case TCM_UNSUPPORTED_SCSI_OPCODE: |
| 4727 | case TCM_SECTOR_COUNT_TOO_MANY: |
| 4728 | /* CURRENT ERROR */ |
| 4729 | buffer[offset] = 0x70; |
| 4730 | /* ILLEGAL REQUEST */ |
| 4731 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
| 4732 | /* INVALID COMMAND OPERATION CODE */ |
| 4733 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20; |
| 4734 | break; |
| 4735 | case TCM_UNKNOWN_MODE_PAGE: |
| 4736 | /* CURRENT ERROR */ |
| 4737 | buffer[offset] = 0x70; |
| 4738 | /* ILLEGAL REQUEST */ |
| 4739 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
| 4740 | /* INVALID FIELD IN CDB */ |
| 4741 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24; |
| 4742 | break; |
| 4743 | case TCM_CHECK_CONDITION_ABORT_CMD: |
| 4744 | /* CURRENT ERROR */ |
| 4745 | buffer[offset] = 0x70; |
| 4746 | /* ABORTED COMMAND */ |
| 4747 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 4748 | /* BUS DEVICE RESET FUNCTION OCCURRED */ |
| 4749 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29; |
| 4750 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03; |
| 4751 | break; |
| 4752 | case TCM_INCORRECT_AMOUNT_OF_DATA: |
| 4753 | /* CURRENT ERROR */ |
| 4754 | buffer[offset] = 0x70; |
| 4755 | /* ABORTED COMMAND */ |
| 4756 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 4757 | /* WRITE ERROR */ |
| 4758 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c; |
| 4759 | /* NOT ENOUGH UNSOLICITED DATA */ |
| 4760 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d; |
| 4761 | break; |
| 4762 | case TCM_INVALID_CDB_FIELD: |
| 4763 | /* CURRENT ERROR */ |
| 4764 | buffer[offset] = 0x70; |
| 4765 | /* ABORTED COMMAND */ |
| 4766 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 4767 | /* INVALID FIELD IN CDB */ |
| 4768 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24; |
| 4769 | break; |
| 4770 | case TCM_INVALID_PARAMETER_LIST: |
| 4771 | /* CURRENT ERROR */ |
| 4772 | buffer[offset] = 0x70; |
| 4773 | /* ABORTED COMMAND */ |
| 4774 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 4775 | /* INVALID FIELD IN PARAMETER LIST */ |
| 4776 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26; |
| 4777 | break; |
| 4778 | case TCM_UNEXPECTED_UNSOLICITED_DATA: |
| 4779 | /* CURRENT ERROR */ |
| 4780 | buffer[offset] = 0x70; |
| 4781 | /* ABORTED COMMAND */ |
| 4782 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 4783 | /* WRITE ERROR */ |
| 4784 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c; |
| 4785 | /* UNEXPECTED_UNSOLICITED_DATA */ |
| 4786 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c; |
| 4787 | break; |
| 4788 | case TCM_SERVICE_CRC_ERROR: |
| 4789 | /* CURRENT ERROR */ |
| 4790 | buffer[offset] = 0x70; |
| 4791 | /* ABORTED COMMAND */ |
| 4792 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 4793 | /* PROTOCOL SERVICE CRC ERROR */ |
| 4794 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47; |
| 4795 | /* N/A */ |
| 4796 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05; |
| 4797 | break; |
| 4798 | case TCM_SNACK_REJECTED: |
| 4799 | /* CURRENT ERROR */ |
| 4800 | buffer[offset] = 0x70; |
| 4801 | /* ABORTED COMMAND */ |
| 4802 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 4803 | /* READ ERROR */ |
| 4804 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11; |
| 4805 | /* FAILED RETRANSMISSION REQUEST */ |
| 4806 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13; |
| 4807 | break; |
| 4808 | case TCM_WRITE_PROTECTED: |
| 4809 | /* CURRENT ERROR */ |
| 4810 | buffer[offset] = 0x70; |
| 4811 | /* DATA PROTECT */ |
| 4812 | buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT; |
| 4813 | /* WRITE PROTECTED */ |
| 4814 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27; |
| 4815 | break; |
| 4816 | case TCM_CHECK_CONDITION_UNIT_ATTENTION: |
| 4817 | /* CURRENT ERROR */ |
| 4818 | buffer[offset] = 0x70; |
| 4819 | /* UNIT ATTENTION */ |
| 4820 | buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION; |
| 4821 | core_scsi3_ua_for_check_condition(cmd, &asc, &ascq); |
| 4822 | buffer[offset+SPC_ASC_KEY_OFFSET] = asc; |
| 4823 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq; |
| 4824 | break; |
| 4825 | case TCM_CHECK_CONDITION_NOT_READY: |
| 4826 | /* CURRENT ERROR */ |
| 4827 | buffer[offset] = 0x70; |
| 4828 | /* Not Ready */ |
| 4829 | buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY; |
| 4830 | transport_get_sense_codes(cmd, &asc, &ascq); |
| 4831 | buffer[offset+SPC_ASC_KEY_OFFSET] = asc; |
| 4832 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq; |
| 4833 | break; |
| 4834 | case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE: |
| 4835 | default: |
| 4836 | /* CURRENT ERROR */ |
| 4837 | buffer[offset] = 0x70; |
| 4838 | /* ILLEGAL REQUEST */ |
| 4839 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
| 4840 | /* LOGICAL UNIT COMMUNICATION FAILURE */ |
| 4841 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80; |
| 4842 | break; |
| 4843 | } |
| 4844 | /* |
| 4845 | * This code uses linux/include/scsi/scsi.h SAM status codes! |
| 4846 | */ |
| 4847 | cmd->scsi_status = SAM_STAT_CHECK_CONDITION; |
| 4848 | /* |
| 4849 | * Automatically padded, this value is encoded in the fabric's |
| 4850 | * data_length response PDU containing the SCSI defined sense data. |
| 4851 | */ |
| 4852 | cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset; |
| 4853 | |
| 4854 | after_reason: |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 4855 | return cmd->se_tfo->queue_status(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4856 | } |
| 4857 | EXPORT_SYMBOL(transport_send_check_condition_and_sense); |
| 4858 | |
| 4859 | int transport_check_aborted_status(struct se_cmd *cmd, int send_status) |
| 4860 | { |
| 4861 | int ret = 0; |
| 4862 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4863 | if (atomic_read(&cmd->t_transport_aborted) != 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4864 | if (!send_status || |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4865 | (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS)) |
| 4866 | return 1; |
| 4867 | #if 0 |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4868 | pr_debug("Sending delayed SAM_STAT_TASK_ABORTED" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4869 | " status for CDB: 0x%02x ITT: 0x%08x\n", |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4870 | cmd->t_task_cdb[0], |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4871 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4872 | #endif |
| 4873 | cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4874 | cmd->se_tfo->queue_status(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4875 | ret = 1; |
| 4876 | } |
| 4877 | return ret; |
| 4878 | } |
| 4879 | EXPORT_SYMBOL(transport_check_aborted_status); |
| 4880 | |
| 4881 | void transport_send_task_abort(struct se_cmd *cmd) |
| 4882 | { |
| 4883 | /* |
| 4884 | * If there are still expected incoming fabric WRITEs, we wait |
| 4885 | * until until they have completed before sending a TASK_ABORTED |
| 4886 | * response. This response with TASK_ABORTED status will be |
| 4887 | * queued back to fabric module by transport_check_aborted_status(). |
| 4888 | */ |
| 4889 | if (cmd->data_direction == DMA_TO_DEVICE) { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4890 | if (cmd->se_tfo->write_pending_status(cmd) != 0) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4891 | atomic_inc(&cmd->t_transport_aborted); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4892 | smp_mb__after_atomic_inc(); |
| 4893 | cmd->scsi_status = SAM_STAT_TASK_ABORTED; |
| 4894 | transport_new_cmd_failure(cmd); |
| 4895 | return; |
| 4896 | } |
| 4897 | } |
| 4898 | cmd->scsi_status = SAM_STAT_TASK_ABORTED; |
| 4899 | #if 0 |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4900 | pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x," |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4901 | " ITT: 0x%08x\n", cmd->t_task_cdb[0], |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4902 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4903 | #endif |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4904 | cmd->se_tfo->queue_status(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | /* transport_generic_do_tmr(): |
| 4908 | * |
| 4909 | * |
| 4910 | */ |
| 4911 | int transport_generic_do_tmr(struct se_cmd *cmd) |
| 4912 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 4913 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4914 | struct se_tmr_req *tmr = cmd->se_tmr_req; |
| 4915 | int ret; |
| 4916 | |
| 4917 | switch (tmr->function) { |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 4918 | case TMR_ABORT_TASK: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4919 | tmr->response = TMR_FUNCTION_REJECTED; |
| 4920 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 4921 | case TMR_ABORT_TASK_SET: |
| 4922 | case TMR_CLEAR_ACA: |
| 4923 | case TMR_CLEAR_TASK_SET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4924 | tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED; |
| 4925 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 4926 | case TMR_LUN_RESET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4927 | ret = core_tmr_lun_reset(dev, tmr, NULL, NULL); |
| 4928 | tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE : |
| 4929 | TMR_FUNCTION_REJECTED; |
| 4930 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 4931 | case TMR_TARGET_WARM_RESET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4932 | tmr->response = TMR_FUNCTION_REJECTED; |
| 4933 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 4934 | case TMR_TARGET_COLD_RESET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4935 | tmr->response = TMR_FUNCTION_REJECTED; |
| 4936 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4937 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4938 | pr_err("Uknown TMR function: 0x%02x.\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4939 | tmr->function); |
| 4940 | tmr->response = TMR_FUNCTION_REJECTED; |
| 4941 | break; |
| 4942 | } |
| 4943 | |
| 4944 | cmd->t_state = TRANSPORT_ISTATE_PROCESSING; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4945 | cmd->se_tfo->queue_tm_rsp(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4946 | |
| 4947 | transport_cmd_check_stop(cmd, 2, 0); |
| 4948 | return 0; |
| 4949 | } |
| 4950 | |
| 4951 | /* |
| 4952 | * Called with spin_lock_irq(&dev->execute_task_lock); held |
| 4953 | * |
| 4954 | */ |
| 4955 | static struct se_task * |
| 4956 | transport_get_task_from_state_list(struct se_device *dev) |
| 4957 | { |
| 4958 | struct se_task *task; |
| 4959 | |
| 4960 | if (list_empty(&dev->state_task_list)) |
| 4961 | return NULL; |
| 4962 | |
| 4963 | list_for_each_entry(task, &dev->state_task_list, t_state_list) |
| 4964 | break; |
| 4965 | |
| 4966 | list_del(&task->t_state_list); |
| 4967 | atomic_set(&task->task_state_active, 0); |
| 4968 | |
| 4969 | return task; |
| 4970 | } |
| 4971 | |
| 4972 | static void transport_processing_shutdown(struct se_device *dev) |
| 4973 | { |
| 4974 | struct se_cmd *cmd; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4975 | struct se_task *task; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4976 | unsigned long flags; |
| 4977 | /* |
| 4978 | * Empty the struct se_device's struct se_task state list. |
| 4979 | */ |
| 4980 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
| 4981 | while ((task = transport_get_task_from_state_list(dev))) { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4982 | if (!task->task_se_cmd) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4983 | pr_err("task->task_se_cmd is NULL!\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4984 | continue; |
| 4985 | } |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 4986 | cmd = task->task_se_cmd; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4987 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4988 | spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
| 4989 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4990 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4991 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4992 | pr_debug("PT: cmd: %p task: %p ITT: 0x%08x," |
| 4993 | " i_state: %d, t_state/def_t_state:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4994 | " %d/%d cdb: 0x%02x\n", cmd, task, |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4995 | cmd->se_tfo->get_task_tag(cmd), |
| 4996 | cmd->se_tfo->get_cmd_state(cmd), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 4997 | cmd->t_state, cmd->deferred_t_state, |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 4998 | cmd->t_task_cdb[0]); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 4999 | pr_debug("PT: ITT[0x%08x] - t_tasks: %d t_task_cdbs_left:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5000 | " %d t_task_cdbs_sent: %d -- t_transport_active: %d" |
| 5001 | " t_transport_stop: %d t_transport_sent: %d\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 5002 | cmd->se_tfo->get_task_tag(cmd), |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 5003 | cmd->t_task_list_num, |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5004 | atomic_read(&cmd->t_task_cdbs_left), |
| 5005 | atomic_read(&cmd->t_task_cdbs_sent), |
| 5006 | atomic_read(&cmd->t_transport_active), |
| 5007 | atomic_read(&cmd->t_transport_stop), |
| 5008 | atomic_read(&cmd->t_transport_sent)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5009 | |
| 5010 | if (atomic_read(&task->task_active)) { |
| 5011 | atomic_set(&task->task_stop, 1); |
| 5012 | spin_unlock_irqrestore( |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5013 | &cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5014 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 5015 | pr_debug("Waiting for task: %p to shutdown for dev:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5016 | " %p\n", task, dev); |
| 5017 | wait_for_completion(&task->task_stop_comp); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 5018 | pr_debug("Completed task: %p shutdown for dev: %p\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5019 | task, dev); |
| 5020 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5021 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 5022 | atomic_dec(&cmd->t_task_cdbs_left); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5023 | |
| 5024 | atomic_set(&task->task_active, 0); |
| 5025 | atomic_set(&task->task_stop, 0); |
Nicholas Bellinger | 52208ae | 2011-02-24 16:58:20 -0800 | [diff] [blame] | 5026 | } else { |
| 5027 | if (atomic_read(&task->task_execute_queue) != 0) |
| 5028 | transport_remove_task_from_execute_queue(task, dev); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5029 | } |
| 5030 | __transport_stop_task_timer(task, &flags); |
| 5031 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 5032 | if (!atomic_dec_and_test(&cmd->t_task_cdbs_ex_left)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5033 | spin_unlock_irqrestore( |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5034 | &cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5035 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 5036 | pr_debug("Skipping task: %p, dev: %p for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5037 | " t_task_cdbs_ex_left: %d\n", task, dev, |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5038 | atomic_read(&cmd->t_task_cdbs_ex_left)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5039 | |
| 5040 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
| 5041 | continue; |
| 5042 | } |
| 5043 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5044 | if (atomic_read(&cmd->t_transport_active)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 5045 | pr_debug("got t_transport_active = 1 for task: %p, dev:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5046 | " %p\n", task, dev); |
| 5047 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5048 | if (atomic_read(&cmd->t_fe_count)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5049 | spin_unlock_irqrestore( |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5050 | &cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5051 | transport_send_check_condition_and_sense( |
| 5052 | cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, |
| 5053 | 0); |
| 5054 | transport_remove_cmd_from_queue(cmd, |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 5055 | &cmd->se_dev->dev_queue_obj); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5056 | |
| 5057 | transport_lun_remove_cmd(cmd); |
| 5058 | transport_cmd_check_stop(cmd, 1, 0); |
| 5059 | } else { |
| 5060 | spin_unlock_irqrestore( |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5061 | &cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5062 | |
| 5063 | transport_remove_cmd_from_queue(cmd, |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 5064 | &cmd->se_dev->dev_queue_obj); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5065 | |
| 5066 | transport_lun_remove_cmd(cmd); |
| 5067 | |
| 5068 | if (transport_cmd_check_stop(cmd, 1, 0)) |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 5069 | transport_generic_remove(cmd, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
| 5073 | continue; |
| 5074 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 5075 | pr_debug("Got t_transport_active = 0 for task: %p, dev: %p\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5076 | task, dev); |
| 5077 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5078 | if (atomic_read(&cmd->t_fe_count)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5079 | spin_unlock_irqrestore( |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5080 | &cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5081 | transport_send_check_condition_and_sense(cmd, |
| 5082 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0); |
| 5083 | transport_remove_cmd_from_queue(cmd, |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 5084 | &cmd->se_dev->dev_queue_obj); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5085 | |
| 5086 | transport_lun_remove_cmd(cmd); |
| 5087 | transport_cmd_check_stop(cmd, 1, 0); |
| 5088 | } else { |
| 5089 | spin_unlock_irqrestore( |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5090 | &cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5091 | |
| 5092 | transport_remove_cmd_from_queue(cmd, |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 5093 | &cmd->se_dev->dev_queue_obj); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5094 | transport_lun_remove_cmd(cmd); |
| 5095 | |
| 5096 | if (transport_cmd_check_stop(cmd, 1, 0)) |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 5097 | transport_generic_remove(cmd, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5098 | } |
| 5099 | |
| 5100 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
| 5101 | } |
| 5102 | spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
| 5103 | /* |
| 5104 | * Empty the struct se_device's struct se_cmd list. |
| 5105 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 5106 | while ((cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj))) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5107 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 5108 | pr_debug("From Device Queue: cmd: %p t_state: %d\n", |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 5109 | cmd, cmd->t_state); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5110 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 5111 | if (atomic_read(&cmd->t_fe_count)) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5112 | transport_send_check_condition_and_sense(cmd, |
| 5113 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0); |
| 5114 | |
| 5115 | transport_lun_remove_cmd(cmd); |
| 5116 | transport_cmd_check_stop(cmd, 1, 0); |
| 5117 | } else { |
| 5118 | transport_lun_remove_cmd(cmd); |
| 5119 | if (transport_cmd_check_stop(cmd, 1, 0)) |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 5120 | transport_generic_remove(cmd, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5121 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5122 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | /* transport_processing_thread(): |
| 5126 | * |
| 5127 | * |
| 5128 | */ |
| 5129 | static int transport_processing_thread(void *param) |
| 5130 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 5131 | int ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5132 | struct se_cmd *cmd; |
| 5133 | struct se_device *dev = (struct se_device *) param; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5134 | |
| 5135 | set_user_nice(current, -20); |
| 5136 | |
| 5137 | while (!kthread_should_stop()) { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 5138 | ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq, |
| 5139 | atomic_read(&dev->dev_queue_obj.queue_cnt) || |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5140 | kthread_should_stop()); |
| 5141 | if (ret < 0) |
| 5142 | goto out; |
| 5143 | |
| 5144 | spin_lock_irq(&dev->dev_status_lock); |
| 5145 | if (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) { |
| 5146 | spin_unlock_irq(&dev->dev_status_lock); |
| 5147 | transport_processing_shutdown(dev); |
| 5148 | continue; |
| 5149 | } |
| 5150 | spin_unlock_irq(&dev->dev_status_lock); |
| 5151 | |
| 5152 | get_cmd: |
| 5153 | __transport_execute_tasks(dev); |
| 5154 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 5155 | cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj); |
| 5156 | if (!cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5157 | continue; |
| 5158 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 5159 | switch (cmd->t_state) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5160 | case TRANSPORT_NEW_CMD_MAP: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 5161 | if (!cmd->se_tfo->new_cmd_map) { |
| 5162 | pr_err("cmd->se_tfo->new_cmd_map is" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5163 | " NULL for TRANSPORT_NEW_CMD_MAP\n"); |
| 5164 | BUG(); |
| 5165 | } |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 5166 | ret = cmd->se_tfo->new_cmd_map(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5167 | if (ret < 0) { |
| 5168 | cmd->transport_error_status = ret; |
| 5169 | transport_generic_request_failure(cmd, NULL, |
| 5170 | 0, (cmd->data_direction != |
| 5171 | DMA_TO_DEVICE)); |
| 5172 | break; |
| 5173 | } |
| 5174 | /* Fall through */ |
| 5175 | case TRANSPORT_NEW_CMD: |
| 5176 | ret = transport_generic_new_cmd(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 5177 | if (ret == -EAGAIN) |
| 5178 | break; |
| 5179 | else if (ret < 0) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5180 | cmd->transport_error_status = ret; |
| 5181 | transport_generic_request_failure(cmd, NULL, |
| 5182 | 0, (cmd->data_direction != |
| 5183 | DMA_TO_DEVICE)); |
| 5184 | } |
| 5185 | break; |
| 5186 | case TRANSPORT_PROCESS_WRITE: |
| 5187 | transport_generic_process_write(cmd); |
| 5188 | break; |
| 5189 | case TRANSPORT_COMPLETE_OK: |
| 5190 | transport_stop_all_task_timers(cmd); |
| 5191 | transport_generic_complete_ok(cmd); |
| 5192 | break; |
| 5193 | case TRANSPORT_REMOVE: |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 5194 | transport_generic_remove(cmd, 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5195 | break; |
Nicholas Bellinger | f436677 | 2011-05-19 20:19:11 -0700 | [diff] [blame] | 5196 | case TRANSPORT_FREE_CMD_INTR: |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 5197 | transport_generic_free_cmd(cmd, 0, 0); |
Nicholas Bellinger | f436677 | 2011-05-19 20:19:11 -0700 | [diff] [blame] | 5198 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5199 | case TRANSPORT_PROCESS_TMR: |
| 5200 | transport_generic_do_tmr(cmd); |
| 5201 | break; |
| 5202 | case TRANSPORT_COMPLETE_FAILURE: |
| 5203 | transport_generic_request_failure(cmd, NULL, 1, 1); |
| 5204 | break; |
| 5205 | case TRANSPORT_COMPLETE_TIMEOUT: |
| 5206 | transport_stop_all_task_timers(cmd); |
| 5207 | transport_generic_request_timeout(cmd); |
| 5208 | break; |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 5209 | case TRANSPORT_COMPLETE_QF_WP: |
| 5210 | transport_generic_write_pending(cmd); |
| 5211 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5212 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame^] | 5213 | pr_err("Unknown t_state: %d deferred_t_state:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5214 | " %d for ITT: 0x%08x i_state: %d on SE LUN:" |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 5215 | " %u\n", cmd->t_state, cmd->deferred_t_state, |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 5216 | cmd->se_tfo->get_task_tag(cmd), |
| 5217 | cmd->se_tfo->get_cmd_state(cmd), |
| 5218 | cmd->se_lun->unpacked_lun); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 5219 | BUG(); |
| 5220 | } |
| 5221 | |
| 5222 | goto get_cmd; |
| 5223 | } |
| 5224 | |
| 5225 | out: |
| 5226 | transport_release_all_cmds(dev); |
| 5227 | dev->process_thread = NULL; |
| 5228 | return 0; |
| 5229 | } |