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