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