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