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> |
Paul Gortmaker | 827509e | 2011-08-30 14:20:44 -0400 | [diff] [blame] | 39 | #include <linux/module.h> |
Roland Dreier | 015487b | 2012-02-13 16:18:17 -0800 | [diff] [blame] | 40 | #include <linux/ratelimit.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 41 | #include <asm/unaligned.h> |
| 42 | #include <net/sock.h> |
| 43 | #include <net/tcp.h> |
| 44 | #include <scsi/scsi.h> |
| 45 | #include <scsi/scsi_cmnd.h> |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 46 | #include <scsi/scsi_tcq.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 47 | |
| 48 | #include <target/target_core_base.h> |
Christoph Hellwig | c4795fb | 2011-11-16 09:46:48 -0500 | [diff] [blame] | 49 | #include <target/target_core_backend.h> |
| 50 | #include <target/target_core_fabric.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 51 | #include <target/target_core_configfs.h> |
| 52 | |
Christoph Hellwig | e26d99a | 2011-11-14 12:30:30 -0500 | [diff] [blame] | 53 | #include "target_core_internal.h" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 54 | #include "target_core_alua.h" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 55 | #include "target_core_pr.h" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 56 | #include "target_core_ua.h" |
| 57 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 58 | static int sub_api_initialized; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 59 | |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 60 | static struct workqueue_struct *target_completion_wq; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 61 | static struct kmem_cache *se_sess_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 62 | struct kmem_cache *se_ua_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 63 | struct kmem_cache *t10_pr_reg_cache; |
| 64 | struct kmem_cache *t10_alua_lu_gp_cache; |
| 65 | struct kmem_cache *t10_alua_lu_gp_mem_cache; |
| 66 | struct kmem_cache *t10_alua_tg_pt_gp_cache; |
| 67 | struct kmem_cache *t10_alua_tg_pt_gp_mem_cache; |
| 68 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 69 | static void transport_complete_task_attr(struct se_cmd *cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 70 | static void transport_handle_queue_full(struct se_cmd *cmd, |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 71 | struct se_device *dev); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 72 | static int transport_generic_get_mem(struct se_cmd *cmd); |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 73 | static int target_get_sess_cmd(struct se_session *, struct se_cmd *, bool); |
Nicholas Bellinger | 39c05f3 | 2011-10-08 13:59:52 -0700 | [diff] [blame] | 74 | static void transport_put_cmd(struct se_cmd *cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 75 | 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] | 76 | static void target_complete_ok_work(struct work_struct *work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 77 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 78 | int init_se_kmem_caches(void) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 79 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 80 | se_sess_cache = kmem_cache_create("se_sess_cache", |
| 81 | sizeof(struct se_session), __alignof__(struct se_session), |
| 82 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 83 | if (!se_sess_cache) { |
| 84 | pr_err("kmem_cache_create() for struct se_session" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 85 | " failed\n"); |
Andy Grover | c8e31f2 | 2012-01-19 13:39:17 -0800 | [diff] [blame] | 86 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 87 | } |
| 88 | se_ua_cache = kmem_cache_create("se_ua_cache", |
| 89 | sizeof(struct se_ua), __alignof__(struct se_ua), |
| 90 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 91 | if (!se_ua_cache) { |
| 92 | pr_err("kmem_cache_create() for struct se_ua failed\n"); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 93 | goto out_free_sess_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 94 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 95 | t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache", |
| 96 | sizeof(struct t10_pr_registration), |
| 97 | __alignof__(struct t10_pr_registration), 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 98 | if (!t10_pr_reg_cache) { |
| 99 | pr_err("kmem_cache_create() for struct t10_pr_registration" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 100 | " failed\n"); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 101 | goto out_free_ua_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 102 | } |
| 103 | t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache", |
| 104 | sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp), |
| 105 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 106 | if (!t10_alua_lu_gp_cache) { |
| 107 | pr_err("kmem_cache_create() for t10_alua_lu_gp_cache" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 108 | " failed\n"); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 109 | goto out_free_pr_reg_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 110 | } |
| 111 | t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache", |
| 112 | sizeof(struct t10_alua_lu_gp_member), |
| 113 | __alignof__(struct t10_alua_lu_gp_member), 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 114 | if (!t10_alua_lu_gp_mem_cache) { |
| 115 | pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 116 | "cache failed\n"); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 117 | goto out_free_lu_gp_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 118 | } |
| 119 | t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache", |
| 120 | sizeof(struct t10_alua_tg_pt_gp), |
| 121 | __alignof__(struct t10_alua_tg_pt_gp), 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 122 | if (!t10_alua_tg_pt_gp_cache) { |
| 123 | pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 124 | "cache failed\n"); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 125 | goto out_free_lu_gp_mem_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 126 | } |
| 127 | t10_alua_tg_pt_gp_mem_cache = kmem_cache_create( |
| 128 | "t10_alua_tg_pt_gp_mem_cache", |
| 129 | sizeof(struct t10_alua_tg_pt_gp_member), |
| 130 | __alignof__(struct t10_alua_tg_pt_gp_member), |
| 131 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 132 | if (!t10_alua_tg_pt_gp_mem_cache) { |
| 133 | pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 134 | "mem_t failed\n"); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 135 | goto out_free_tg_pt_gp_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 138 | target_completion_wq = alloc_workqueue("target_completion", |
| 139 | WQ_MEM_RECLAIM, 0); |
| 140 | if (!target_completion_wq) |
| 141 | goto out_free_tg_pt_gp_mem_cache; |
| 142 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 143 | return 0; |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 144 | |
| 145 | out_free_tg_pt_gp_mem_cache: |
| 146 | kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache); |
| 147 | out_free_tg_pt_gp_cache: |
| 148 | kmem_cache_destroy(t10_alua_tg_pt_gp_cache); |
| 149 | out_free_lu_gp_mem_cache: |
| 150 | kmem_cache_destroy(t10_alua_lu_gp_mem_cache); |
| 151 | out_free_lu_gp_cache: |
| 152 | kmem_cache_destroy(t10_alua_lu_gp_cache); |
| 153 | out_free_pr_reg_cache: |
| 154 | kmem_cache_destroy(t10_pr_reg_cache); |
| 155 | out_free_ua_cache: |
| 156 | kmem_cache_destroy(se_ua_cache); |
| 157 | out_free_sess_cache: |
| 158 | kmem_cache_destroy(se_sess_cache); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 159 | out: |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 160 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 163 | void release_se_kmem_caches(void) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 164 | { |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 165 | destroy_workqueue(target_completion_wq); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 166 | kmem_cache_destroy(se_sess_cache); |
| 167 | kmem_cache_destroy(se_ua_cache); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 168 | kmem_cache_destroy(t10_pr_reg_cache); |
| 169 | kmem_cache_destroy(t10_alua_lu_gp_cache); |
| 170 | kmem_cache_destroy(t10_alua_lu_gp_mem_cache); |
| 171 | kmem_cache_destroy(t10_alua_tg_pt_gp_cache); |
| 172 | kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 175 | /* This code ensures unique mib indexes are handed out. */ |
| 176 | static DEFINE_SPINLOCK(scsi_mib_index_lock); |
| 177 | static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX]; |
Nicholas Bellinger | e89d15e | 2011-02-09 15:35:03 -0800 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Allocate a new row index for the entry type specified |
| 181 | */ |
| 182 | u32 scsi_get_new_index(scsi_index_t type) |
| 183 | { |
| 184 | u32 new_index; |
| 185 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 186 | BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX)); |
Nicholas Bellinger | e89d15e | 2011-02-09 15:35:03 -0800 | [diff] [blame] | 187 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 188 | spin_lock(&scsi_mib_index_lock); |
| 189 | new_index = ++scsi_mib_index[type]; |
| 190 | spin_unlock(&scsi_mib_index_lock); |
Nicholas Bellinger | e89d15e | 2011-02-09 15:35:03 -0800 | [diff] [blame] | 191 | |
| 192 | return new_index; |
| 193 | } |
| 194 | |
Nicholas Bellinger | dbc5623 | 2011-10-22 01:03:54 -0700 | [diff] [blame] | 195 | void transport_subsystem_check_init(void) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 196 | { |
| 197 | int ret; |
| 198 | |
Nicholas Bellinger | dbc5623 | 2011-10-22 01:03:54 -0700 | [diff] [blame] | 199 | if (sub_api_initialized) |
| 200 | return; |
| 201 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 202 | ret = request_module("target_core_iblock"); |
| 203 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 204 | pr_err("Unable to load target_core_iblock\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 205 | |
| 206 | ret = request_module("target_core_file"); |
| 207 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 208 | pr_err("Unable to load target_core_file\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 209 | |
| 210 | ret = request_module("target_core_pscsi"); |
| 211 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 212 | pr_err("Unable to load target_core_pscsi\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 213 | |
| 214 | ret = request_module("target_core_stgt"); |
| 215 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 216 | pr_err("Unable to load target_core_stgt\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 217 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 218 | sub_api_initialized = 1; |
Nicholas Bellinger | dbc5623 | 2011-10-22 01:03:54 -0700 | [diff] [blame] | 219 | return; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | struct se_session *transport_init_session(void) |
| 223 | { |
| 224 | struct se_session *se_sess; |
| 225 | |
| 226 | se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 227 | if (!se_sess) { |
| 228 | pr_err("Unable to allocate struct se_session from" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 229 | " se_sess_cache\n"); |
| 230 | return ERR_PTR(-ENOMEM); |
| 231 | } |
| 232 | INIT_LIST_HEAD(&se_sess->sess_list); |
| 233 | INIT_LIST_HEAD(&se_sess->sess_acl_list); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 234 | INIT_LIST_HEAD(&se_sess->sess_cmd_list); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 235 | spin_lock_init(&se_sess->sess_cmd_lock); |
Nicholas Bellinger | 41ac82b | 2012-02-26 22:22:10 -0800 | [diff] [blame] | 236 | kref_init(&se_sess->sess_kref); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 237 | |
| 238 | return se_sess; |
| 239 | } |
| 240 | EXPORT_SYMBOL(transport_init_session); |
| 241 | |
| 242 | /* |
Nicholas Bellinger | 140854c | 2011-08-31 12:34:39 -0700 | [diff] [blame] | 243 | * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 244 | */ |
| 245 | void __transport_register_session( |
| 246 | struct se_portal_group *se_tpg, |
| 247 | struct se_node_acl *se_nacl, |
| 248 | struct se_session *se_sess, |
| 249 | void *fabric_sess_ptr) |
| 250 | { |
| 251 | unsigned char buf[PR_REG_ISID_LEN]; |
| 252 | |
| 253 | se_sess->se_tpg = se_tpg; |
| 254 | se_sess->fabric_sess_ptr = fabric_sess_ptr; |
| 255 | /* |
| 256 | * Used by struct se_node_acl's under ConfigFS to locate active se_session-t |
| 257 | * |
| 258 | * Only set for struct se_session's that will actually be moving I/O. |
| 259 | * eg: *NOT* discovery sessions. |
| 260 | */ |
| 261 | if (se_nacl) { |
| 262 | /* |
| 263 | * If the fabric module supports an ISID based TransportID, |
| 264 | * save this value in binary from the fabric I_T Nexus now. |
| 265 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 266 | if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 267 | memset(&buf[0], 0, PR_REG_ISID_LEN); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 268 | se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 269 | &buf[0], PR_REG_ISID_LEN); |
| 270 | se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]); |
| 271 | } |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 272 | kref_get(&se_nacl->acl_kref); |
| 273 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 274 | spin_lock_irq(&se_nacl->nacl_sess_lock); |
| 275 | /* |
| 276 | * The se_nacl->nacl_sess pointer will be set to the |
| 277 | * last active I_T Nexus for each struct se_node_acl. |
| 278 | */ |
| 279 | se_nacl->nacl_sess = se_sess; |
| 280 | |
| 281 | list_add_tail(&se_sess->sess_acl_list, |
| 282 | &se_nacl->acl_sess_list); |
| 283 | spin_unlock_irq(&se_nacl->nacl_sess_lock); |
| 284 | } |
| 285 | list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list); |
| 286 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 287 | pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 288 | 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] | 289 | } |
| 290 | EXPORT_SYMBOL(__transport_register_session); |
| 291 | |
| 292 | void transport_register_session( |
| 293 | struct se_portal_group *se_tpg, |
| 294 | struct se_node_acl *se_nacl, |
| 295 | struct se_session *se_sess, |
| 296 | void *fabric_sess_ptr) |
| 297 | { |
Nicholas Bellinger | 140854c | 2011-08-31 12:34:39 -0700 | [diff] [blame] | 298 | unsigned long flags; |
| 299 | |
| 300 | spin_lock_irqsave(&se_tpg->session_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 301 | __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr); |
Nicholas Bellinger | 140854c | 2011-08-31 12:34:39 -0700 | [diff] [blame] | 302 | spin_unlock_irqrestore(&se_tpg->session_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 303 | } |
| 304 | EXPORT_SYMBOL(transport_register_session); |
| 305 | |
Joern Engel | 4149268 | 2012-05-18 13:57:19 -0700 | [diff] [blame] | 306 | void target_release_session(struct kref *kref) |
Nicholas Bellinger | 41ac82b | 2012-02-26 22:22:10 -0800 | [diff] [blame] | 307 | { |
| 308 | struct se_session *se_sess = container_of(kref, |
| 309 | struct se_session, sess_kref); |
| 310 | struct se_portal_group *se_tpg = se_sess->se_tpg; |
| 311 | |
| 312 | se_tpg->se_tpg_tfo->close_session(se_sess); |
| 313 | } |
| 314 | |
| 315 | void target_get_session(struct se_session *se_sess) |
| 316 | { |
| 317 | kref_get(&se_sess->sess_kref); |
| 318 | } |
| 319 | EXPORT_SYMBOL(target_get_session); |
| 320 | |
Jörn Engel | 33933a0 | 2012-05-11 10:35:08 -0400 | [diff] [blame] | 321 | void target_put_session(struct se_session *se_sess) |
Nicholas Bellinger | 41ac82b | 2012-02-26 22:22:10 -0800 | [diff] [blame] | 322 | { |
Joern Engel | 4149268 | 2012-05-18 13:57:19 -0700 | [diff] [blame] | 323 | struct se_portal_group *tpg = se_sess->se_tpg; |
| 324 | |
| 325 | if (tpg->se_tpg_tfo->put_session != NULL) { |
| 326 | tpg->se_tpg_tfo->put_session(se_sess); |
| 327 | return; |
| 328 | } |
Jörn Engel | 33933a0 | 2012-05-11 10:35:08 -0400 | [diff] [blame] | 329 | kref_put(&se_sess->sess_kref, target_release_session); |
Nicholas Bellinger | 41ac82b | 2012-02-26 22:22:10 -0800 | [diff] [blame] | 330 | } |
| 331 | EXPORT_SYMBOL(target_put_session); |
| 332 | |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 333 | static void target_complete_nacl(struct kref *kref) |
| 334 | { |
| 335 | struct se_node_acl *nacl = container_of(kref, |
| 336 | struct se_node_acl, acl_kref); |
| 337 | |
| 338 | complete(&nacl->acl_free_comp); |
| 339 | } |
| 340 | |
| 341 | void target_put_nacl(struct se_node_acl *nacl) |
| 342 | { |
| 343 | kref_put(&nacl->acl_kref, target_complete_nacl); |
| 344 | } |
| 345 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 346 | void transport_deregister_session_configfs(struct se_session *se_sess) |
| 347 | { |
| 348 | struct se_node_acl *se_nacl; |
Roland Dreier | 2338886 | 2011-06-22 01:02:21 -0700 | [diff] [blame] | 349 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 350 | /* |
| 351 | * Used by struct se_node_acl's under ConfigFS to locate active struct se_session |
| 352 | */ |
| 353 | se_nacl = se_sess->se_node_acl; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 354 | if (se_nacl) { |
Roland Dreier | 2338886 | 2011-06-22 01:02:21 -0700 | [diff] [blame] | 355 | spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags); |
Nicholas Bellinger | 337c060 | 2012-03-10 14:36:21 -0800 | [diff] [blame] | 356 | if (se_nacl->acl_stop == 0) |
| 357 | list_del(&se_sess->sess_acl_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 358 | /* |
| 359 | * If the session list is empty, then clear the pointer. |
| 360 | * Otherwise, set the struct se_session pointer from the tail |
| 361 | * element of the per struct se_node_acl active session list. |
| 362 | */ |
| 363 | if (list_empty(&se_nacl->acl_sess_list)) |
| 364 | se_nacl->nacl_sess = NULL; |
| 365 | else { |
| 366 | se_nacl->nacl_sess = container_of( |
| 367 | se_nacl->acl_sess_list.prev, |
| 368 | struct se_session, sess_acl_list); |
| 369 | } |
Roland Dreier | 2338886 | 2011-06-22 01:02:21 -0700 | [diff] [blame] | 370 | spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | EXPORT_SYMBOL(transport_deregister_session_configfs); |
| 374 | |
| 375 | void transport_free_session(struct se_session *se_sess) |
| 376 | { |
| 377 | kmem_cache_free(se_sess_cache, se_sess); |
| 378 | } |
| 379 | EXPORT_SYMBOL(transport_free_session); |
| 380 | |
| 381 | void transport_deregister_session(struct se_session *se_sess) |
| 382 | { |
| 383 | struct se_portal_group *se_tpg = se_sess->se_tpg; |
Nicholas Bellinger | 0146834 | 2012-03-10 14:32:52 -0800 | [diff] [blame] | 384 | struct target_core_fabric_ops *se_tfo; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 385 | struct se_node_acl *se_nacl; |
Roland Dreier | e63a8e1 | 2011-08-12 16:01:02 -0700 | [diff] [blame] | 386 | unsigned long flags; |
Nicholas Bellinger | 0146834 | 2012-03-10 14:32:52 -0800 | [diff] [blame] | 387 | bool comp_nacl = true; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 388 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 389 | if (!se_tpg) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 390 | transport_free_session(se_sess); |
| 391 | return; |
| 392 | } |
Nicholas Bellinger | 0146834 | 2012-03-10 14:32:52 -0800 | [diff] [blame] | 393 | se_tfo = se_tpg->se_tpg_tfo; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 394 | |
Roland Dreier | e63a8e1 | 2011-08-12 16:01:02 -0700 | [diff] [blame] | 395 | spin_lock_irqsave(&se_tpg->session_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 396 | list_del(&se_sess->sess_list); |
| 397 | se_sess->se_tpg = NULL; |
| 398 | se_sess->fabric_sess_ptr = NULL; |
Roland Dreier | e63a8e1 | 2011-08-12 16:01:02 -0700 | [diff] [blame] | 399 | spin_unlock_irqrestore(&se_tpg->session_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 400 | |
| 401 | /* |
| 402 | * Determine if we need to do extra work for this initiator node's |
| 403 | * struct se_node_acl if it had been previously dynamically generated. |
| 404 | */ |
| 405 | se_nacl = se_sess->se_node_acl; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 406 | |
Nicholas Bellinger | 0146834 | 2012-03-10 14:32:52 -0800 | [diff] [blame] | 407 | spin_lock_irqsave(&se_tpg->acl_node_lock, flags); |
| 408 | if (se_nacl && se_nacl->dynamic_node_acl) { |
| 409 | if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) { |
| 410 | list_del(&se_nacl->acl_list); |
| 411 | se_tpg->num_node_acls--; |
| 412 | spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags); |
| 413 | core_tpg_wait_for_nacl_pr_ref(se_nacl); |
| 414 | core_free_device_list_for_node(se_nacl, se_tpg); |
| 415 | se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl); |
| 416 | |
| 417 | comp_nacl = false; |
| 418 | spin_lock_irqsave(&se_tpg->acl_node_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 419 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 420 | } |
Nicholas Bellinger | 0146834 | 2012-03-10 14:32:52 -0800 | [diff] [blame] | 421 | spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 422 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 423 | pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 424 | se_tpg->se_tpg_tfo->get_fabric_name()); |
Nicholas Bellinger | 0146834 | 2012-03-10 14:32:52 -0800 | [diff] [blame] | 425 | /* |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 426 | * If last kref is dropping now for an explict NodeACL, awake sleeping |
| 427 | * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group |
| 428 | * removal context. |
Nicholas Bellinger | 0146834 | 2012-03-10 14:32:52 -0800 | [diff] [blame] | 429 | */ |
| 430 | if (se_nacl && comp_nacl == true) |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 431 | target_put_nacl(se_nacl); |
Nicholas Bellinger | 0146834 | 2012-03-10 14:32:52 -0800 | [diff] [blame] | 432 | |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 433 | transport_free_session(se_sess); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 434 | } |
| 435 | EXPORT_SYMBOL(transport_deregister_session); |
| 436 | |
| 437 | /* |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 438 | * Called with cmd->t_state_lock held. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 439 | */ |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 440 | static void target_remove_from_state_list(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 441 | { |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 442 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 443 | unsigned long flags; |
| 444 | |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 445 | if (!dev) |
| 446 | return; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 447 | |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 448 | if (cmd->transport_state & CMD_T_BUSY) |
| 449 | return; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 450 | |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 451 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
| 452 | if (cmd->state_active) { |
| 453 | list_del(&cmd->state_list); |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 454 | cmd->state_active = false; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 455 | } |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 456 | spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 459 | static int transport_cmd_check_stop(struct se_cmd *cmd, bool remove_from_lists) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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 | */ |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 468 | if (cmd->transport_state & CMD_T_LUN_STOP) { |
| 469 | pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n", |
| 470 | __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 471 | |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 472 | cmd->transport_state &= ~CMD_T_ACTIVE; |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 473 | if (remove_from_lists) |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 474 | target_remove_from_state_list(cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 475 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 476 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 477 | complete(&cmd->transport_lun_stop_comp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 478 | return 1; |
| 479 | } |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 480 | |
| 481 | if (remove_from_lists) { |
| 482 | target_remove_from_state_list(cmd); |
| 483 | |
| 484 | /* |
| 485 | * Clear struct se_cmd->se_lun before the handoff to FE. |
| 486 | */ |
| 487 | cmd->se_lun = NULL; |
| 488 | } |
| 489 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 490 | /* |
| 491 | * Determine if frontend context caller is requesting the stopping of |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 492 | * this command for frontend exceptions. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 493 | */ |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 494 | if (cmd->transport_state & CMD_T_STOP) { |
| 495 | pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n", |
| 496 | __func__, __LINE__, |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 497 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 498 | |
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 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 504 | |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 505 | cmd->transport_state &= ~CMD_T_ACTIVE; |
| 506 | if (remove_from_lists) { |
| 507 | /* |
| 508 | * Some fabric modules like tcm_loop can release |
| 509 | * their internally allocated I/O reference now and |
| 510 | * struct se_cmd now. |
| 511 | * |
| 512 | * Fabric modules are expected to return '1' here if the |
| 513 | * se_cmd being passed is released at this point, |
| 514 | * or zero if not being released. |
| 515 | */ |
| 516 | if (cmd->se_tfo->check_stop_free != NULL) { |
| 517 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 518 | return cmd->se_tfo->check_stop_free(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 519 | } |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 520 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 521 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 522 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd) |
| 527 | { |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 528 | return transport_cmd_check_stop(cmd, true); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static void transport_lun_remove_cmd(struct se_cmd *cmd) |
| 532 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 533 | struct se_lun *lun = cmd->se_lun; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 534 | unsigned long flags; |
| 535 | |
| 536 | if (!lun) |
| 537 | return; |
| 538 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 539 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 540 | if (cmd->transport_state & CMD_T_DEV_ACTIVE) { |
| 541 | cmd->transport_state &= ~CMD_T_DEV_ACTIVE; |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 542 | target_remove_from_state_list(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 543 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 544 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 545 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 546 | spin_lock_irqsave(&lun->lun_cmd_lock, flags); |
Christoph Hellwig | 3d26fea | 2011-12-21 14:14:05 -0500 | [diff] [blame] | 547 | if (!list_empty(&cmd->se_lun_node)) |
| 548 | list_del_init(&cmd->se_lun_node); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 549 | spin_unlock_irqrestore(&lun->lun_cmd_lock, flags); |
| 550 | } |
| 551 | |
| 552 | void transport_cmd_finish_abort(struct se_cmd *cmd, int remove) |
| 553 | { |
Andy Grover | c8e31f2 | 2012-01-19 13:39:17 -0800 | [diff] [blame] | 554 | if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) |
Nicholas Bellinger | 8dc52b5 | 2011-10-09 02:02:51 -0700 | [diff] [blame] | 555 | transport_lun_remove_cmd(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 556 | |
| 557 | if (transport_cmd_check_stop_to_fabric(cmd)) |
| 558 | return; |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 559 | if (remove) |
Christoph Hellwig | e6a2573 | 2011-09-13 23:08:50 +0200 | [diff] [blame] | 560 | transport_put_cmd(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 563 | static void target_complete_failure_work(struct work_struct *work) |
| 564 | { |
| 565 | struct se_cmd *cmd = container_of(work, struct se_cmd, work); |
| 566 | |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 567 | transport_generic_request_failure(cmd); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 568 | } |
| 569 | |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 570 | /* |
| 571 | * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd |
| 572 | */ |
Paolo Bonzini | 27a2709 | 2012-09-05 17:09:14 +0200 | [diff] [blame^] | 573 | static void transport_get_sense_data(struct se_cmd *cmd) |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 574 | { |
| 575 | unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL; |
| 576 | struct se_device *dev = cmd->se_dev; |
| 577 | unsigned long flags; |
| 578 | u32 offset = 0; |
| 579 | |
| 580 | WARN_ON(!cmd->se_lun); |
| 581 | |
| 582 | if (!dev) |
Paolo Bonzini | 27a2709 | 2012-09-05 17:09:14 +0200 | [diff] [blame^] | 583 | return; |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 584 | |
| 585 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 586 | if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) { |
| 587 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Paolo Bonzini | 27a2709 | 2012-09-05 17:09:14 +0200 | [diff] [blame^] | 588 | return; |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | sense_buffer = dev->transport->get_sense_buffer(cmd); |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 592 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 593 | |
| 594 | offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER); |
| 595 | |
| 596 | memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER); |
| 597 | |
| 598 | /* Automatically padded */ |
| 599 | cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset; |
| 600 | |
| 601 | pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n", |
| 602 | dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status); |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 603 | } |
| 604 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 605 | void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 606 | { |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 607 | struct se_device *dev = cmd->se_dev; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 608 | int success = scsi_status == GOOD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 609 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 610 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 611 | cmd->scsi_status = scsi_status; |
| 612 | |
| 613 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 614 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 615 | cmd->transport_state &= ~CMD_T_BUSY; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 616 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 617 | if (dev && dev->transport->transport_complete) { |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 618 | if (dev->transport->transport_complete(cmd, |
| 619 | cmd->t_data_sg) != 0) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 620 | cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 621 | success = 1; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /* |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 626 | * See if we are waiting to complete for an exception condition. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 627 | */ |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 628 | if (cmd->transport_state & CMD_T_REQUEST_STOP) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 629 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 630 | complete(&cmd->task_stop_comp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 631 | return; |
| 632 | } |
Christoph Hellwig | 2235007 | 2011-11-02 05:06:35 -0700 | [diff] [blame] | 633 | |
| 634 | if (!success) |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 635 | cmd->transport_state |= CMD_T_FAILED; |
Christoph Hellwig | 2235007 | 2011-11-02 05:06:35 -0700 | [diff] [blame] | 636 | |
Nicholas Bellinger | 3d28934 | 2012-02-13 02:38:14 -0800 | [diff] [blame] | 637 | /* |
| 638 | * Check for case where an explict ABORT_TASK has been received |
| 639 | * and transport_wait_for_tasks() will be waiting for completion.. |
| 640 | */ |
| 641 | if (cmd->transport_state & CMD_T_ABORTED && |
| 642 | cmd->transport_state & CMD_T_STOP) { |
| 643 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 644 | complete(&cmd->t_transport_stop_comp); |
| 645 | return; |
| 646 | } else if (cmd->transport_state & CMD_T_FAILED) { |
Christoph Hellwig | 41e16e9 | 2011-11-23 06:54:15 -0500 | [diff] [blame] | 647 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 648 | INIT_WORK(&cmd->work, target_complete_failure_work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 649 | } else { |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 650 | INIT_WORK(&cmd->work, target_complete_ok_work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 651 | } |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 652 | |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 653 | cmd->t_state = TRANSPORT_COMPLETE; |
Nicholas Bellinger | 3d28934 | 2012-02-13 02:38:14 -0800 | [diff] [blame] | 654 | cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 655 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 656 | |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 657 | queue_work(target_completion_wq, &cmd->work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 658 | } |
Christoph Hellwig | 6bb35e0 | 2012-04-23 11:35:33 -0400 | [diff] [blame] | 659 | EXPORT_SYMBOL(target_complete_cmd); |
| 660 | |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 661 | static void target_add_to_state_list(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 662 | { |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 663 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 664 | unsigned long flags; |
| 665 | |
Nicholas Bellinger | 4d2300c | 2011-11-30 18:18:33 -0800 | [diff] [blame] | 666 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 667 | if (!cmd->state_active) { |
| 668 | list_add_tail(&cmd->state_list, &dev->state_list); |
| 669 | cmd->state_active = true; |
| 670 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 671 | spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 674 | /* |
Nicholas Bellinger | f147abb | 2011-10-25 23:57:41 -0700 | [diff] [blame] | 675 | * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 676 | */ |
Christoph Hellwig | 7a6f0a1 | 2012-07-08 15:58:47 -0400 | [diff] [blame] | 677 | static void transport_write_pending_qf(struct se_cmd *cmd); |
| 678 | static void transport_complete_qf(struct se_cmd *cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 679 | |
| 680 | static void target_qf_do_work(struct work_struct *work) |
| 681 | { |
| 682 | struct se_device *dev = container_of(work, struct se_device, |
| 683 | qf_work_queue); |
Roland Dreier | bcac364 | 2011-08-27 21:33:16 -0700 | [diff] [blame] | 684 | LIST_HEAD(qf_cmd_list); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 685 | struct se_cmd *cmd, *cmd_tmp; |
| 686 | |
| 687 | spin_lock_irq(&dev->qf_cmd_lock); |
Roland Dreier | bcac364 | 2011-08-27 21:33:16 -0700 | [diff] [blame] | 688 | list_splice_init(&dev->qf_cmd_list, &qf_cmd_list); |
| 689 | spin_unlock_irq(&dev->qf_cmd_lock); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 690 | |
Roland Dreier | bcac364 | 2011-08-27 21:33:16 -0700 | [diff] [blame] | 691 | 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] | 692 | list_del(&cmd->se_qf_node); |
| 693 | atomic_dec(&dev->dev_qf_count); |
| 694 | smp_mb__after_atomic_dec(); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 695 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 696 | pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue" |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 697 | " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd, |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 698 | (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" : |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 699 | (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING" |
| 700 | : "UNKNOWN"); |
Christoph Hellwig | f7a5cc0 | 2011-10-17 13:56:42 -0400 | [diff] [blame] | 701 | |
Christoph Hellwig | 7a6f0a1 | 2012-07-08 15:58:47 -0400 | [diff] [blame] | 702 | if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) |
| 703 | transport_write_pending_qf(cmd); |
| 704 | else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) |
| 705 | transport_complete_qf(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 706 | } |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 709 | unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd) |
| 710 | { |
| 711 | switch (cmd->data_direction) { |
| 712 | case DMA_NONE: |
| 713 | return "NONE"; |
| 714 | case DMA_FROM_DEVICE: |
| 715 | return "READ"; |
| 716 | case DMA_TO_DEVICE: |
| 717 | return "WRITE"; |
| 718 | case DMA_BIDIRECTIONAL: |
| 719 | return "BIDI"; |
| 720 | default: |
| 721 | break; |
| 722 | } |
| 723 | |
| 724 | return "UNKNOWN"; |
| 725 | } |
| 726 | |
| 727 | void transport_dump_dev_state( |
| 728 | struct se_device *dev, |
| 729 | char *b, |
| 730 | int *bl) |
| 731 | { |
| 732 | *bl += sprintf(b + *bl, "Status: "); |
| 733 | switch (dev->dev_status) { |
| 734 | case TRANSPORT_DEVICE_ACTIVATED: |
| 735 | *bl += sprintf(b + *bl, "ACTIVATED"); |
| 736 | break; |
| 737 | case TRANSPORT_DEVICE_DEACTIVATED: |
| 738 | *bl += sprintf(b + *bl, "DEACTIVATED"); |
| 739 | break; |
| 740 | case TRANSPORT_DEVICE_SHUTDOWN: |
| 741 | *bl += sprintf(b + *bl, "SHUTDOWN"); |
| 742 | break; |
| 743 | case TRANSPORT_DEVICE_OFFLINE_ACTIVATED: |
| 744 | case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED: |
| 745 | *bl += sprintf(b + *bl, "OFFLINE"); |
| 746 | break; |
| 747 | default: |
| 748 | *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status); |
| 749 | break; |
| 750 | } |
| 751 | |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 752 | *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth); |
Nicholas Bellinger | 11e764b | 2012-05-09 12:42:09 -0700 | [diff] [blame] | 753 | *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n", |
| 754 | dev->se_sub_dev->se_dev_attrib.block_size, |
| 755 | dev->se_sub_dev->se_dev_attrib.hw_max_sectors); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 756 | *bl += sprintf(b + *bl, " "); |
| 757 | } |
| 758 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 759 | void transport_dump_vpd_proto_id( |
| 760 | struct t10_vpd *vpd, |
| 761 | unsigned char *p_buf, |
| 762 | int p_buf_len) |
| 763 | { |
| 764 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
| 765 | int len; |
| 766 | |
| 767 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 768 | len = sprintf(buf, "T10 VPD Protocol Identifier: "); |
| 769 | |
| 770 | switch (vpd->protocol_identifier) { |
| 771 | case 0x00: |
| 772 | sprintf(buf+len, "Fibre Channel\n"); |
| 773 | break; |
| 774 | case 0x10: |
| 775 | sprintf(buf+len, "Parallel SCSI\n"); |
| 776 | break; |
| 777 | case 0x20: |
| 778 | sprintf(buf+len, "SSA\n"); |
| 779 | break; |
| 780 | case 0x30: |
| 781 | sprintf(buf+len, "IEEE 1394\n"); |
| 782 | break; |
| 783 | case 0x40: |
| 784 | sprintf(buf+len, "SCSI Remote Direct Memory Access" |
| 785 | " Protocol\n"); |
| 786 | break; |
| 787 | case 0x50: |
| 788 | sprintf(buf+len, "Internet SCSI (iSCSI)\n"); |
| 789 | break; |
| 790 | case 0x60: |
| 791 | sprintf(buf+len, "SAS Serial SCSI Protocol\n"); |
| 792 | break; |
| 793 | case 0x70: |
| 794 | sprintf(buf+len, "Automation/Drive Interface Transport" |
| 795 | " Protocol\n"); |
| 796 | break; |
| 797 | case 0x80: |
| 798 | sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n"); |
| 799 | break; |
| 800 | default: |
| 801 | sprintf(buf+len, "Unknown 0x%02x\n", |
| 802 | vpd->protocol_identifier); |
| 803 | break; |
| 804 | } |
| 805 | |
| 806 | if (p_buf) |
| 807 | strncpy(p_buf, buf, p_buf_len); |
| 808 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 809 | pr_debug("%s", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | void |
| 813 | transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83) |
| 814 | { |
| 815 | /* |
| 816 | * Check if the Protocol Identifier Valid (PIV) bit is set.. |
| 817 | * |
| 818 | * from spc3r23.pdf section 7.5.1 |
| 819 | */ |
| 820 | if (page_83[1] & 0x80) { |
| 821 | vpd->protocol_identifier = (page_83[0] & 0xf0); |
| 822 | vpd->protocol_identifier_set = 1; |
| 823 | transport_dump_vpd_proto_id(vpd, NULL, 0); |
| 824 | } |
| 825 | } |
| 826 | EXPORT_SYMBOL(transport_set_vpd_proto_id); |
| 827 | |
| 828 | int transport_dump_vpd_assoc( |
| 829 | struct t10_vpd *vpd, |
| 830 | unsigned char *p_buf, |
| 831 | int p_buf_len) |
| 832 | { |
| 833 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 834 | int ret = 0; |
| 835 | int len; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 836 | |
| 837 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 838 | len = sprintf(buf, "T10 VPD Identifier Association: "); |
| 839 | |
| 840 | switch (vpd->association) { |
| 841 | case 0x00: |
| 842 | sprintf(buf+len, "addressed logical unit\n"); |
| 843 | break; |
| 844 | case 0x10: |
| 845 | sprintf(buf+len, "target port\n"); |
| 846 | break; |
| 847 | case 0x20: |
| 848 | sprintf(buf+len, "SCSI target device\n"); |
| 849 | break; |
| 850 | default: |
| 851 | sprintf(buf+len, "Unknown 0x%02x\n", vpd->association); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 852 | ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 853 | break; |
| 854 | } |
| 855 | |
| 856 | if (p_buf) |
| 857 | strncpy(p_buf, buf, p_buf_len); |
| 858 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 859 | pr_debug("%s", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 860 | |
| 861 | return ret; |
| 862 | } |
| 863 | |
| 864 | int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83) |
| 865 | { |
| 866 | /* |
| 867 | * The VPD identification association.. |
| 868 | * |
| 869 | * from spc3r23.pdf Section 7.6.3.1 Table 297 |
| 870 | */ |
| 871 | vpd->association = (page_83[1] & 0x30); |
| 872 | return transport_dump_vpd_assoc(vpd, NULL, 0); |
| 873 | } |
| 874 | EXPORT_SYMBOL(transport_set_vpd_assoc); |
| 875 | |
| 876 | int transport_dump_vpd_ident_type( |
| 877 | struct t10_vpd *vpd, |
| 878 | unsigned char *p_buf, |
| 879 | int p_buf_len) |
| 880 | { |
| 881 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 882 | int ret = 0; |
| 883 | int len; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 884 | |
| 885 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 886 | len = sprintf(buf, "T10 VPD Identifier Type: "); |
| 887 | |
| 888 | switch (vpd->device_identifier_type) { |
| 889 | case 0x00: |
| 890 | sprintf(buf+len, "Vendor specific\n"); |
| 891 | break; |
| 892 | case 0x01: |
| 893 | sprintf(buf+len, "T10 Vendor ID based\n"); |
| 894 | break; |
| 895 | case 0x02: |
| 896 | sprintf(buf+len, "EUI-64 based\n"); |
| 897 | break; |
| 898 | case 0x03: |
| 899 | sprintf(buf+len, "NAA\n"); |
| 900 | break; |
| 901 | case 0x04: |
| 902 | sprintf(buf+len, "Relative target port identifier\n"); |
| 903 | break; |
| 904 | case 0x08: |
| 905 | sprintf(buf+len, "SCSI name string\n"); |
| 906 | break; |
| 907 | default: |
| 908 | sprintf(buf+len, "Unsupported: 0x%02x\n", |
| 909 | vpd->device_identifier_type); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 910 | ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 911 | break; |
| 912 | } |
| 913 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 914 | if (p_buf) { |
| 915 | if (p_buf_len < strlen(buf)+1) |
| 916 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 917 | strncpy(p_buf, buf, p_buf_len); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 918 | } else { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 919 | pr_debug("%s", buf); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 920 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 921 | |
| 922 | return ret; |
| 923 | } |
| 924 | |
| 925 | int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83) |
| 926 | { |
| 927 | /* |
| 928 | * The VPD identifier type.. |
| 929 | * |
| 930 | * from spc3r23.pdf Section 7.6.3.1 Table 298 |
| 931 | */ |
| 932 | vpd->device_identifier_type = (page_83[1] & 0x0f); |
| 933 | return transport_dump_vpd_ident_type(vpd, NULL, 0); |
| 934 | } |
| 935 | EXPORT_SYMBOL(transport_set_vpd_ident_type); |
| 936 | |
| 937 | int transport_dump_vpd_ident( |
| 938 | struct t10_vpd *vpd, |
| 939 | unsigned char *p_buf, |
| 940 | int p_buf_len) |
| 941 | { |
| 942 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
| 943 | int ret = 0; |
| 944 | |
| 945 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 946 | |
| 947 | switch (vpd->device_identifier_code_set) { |
| 948 | case 0x01: /* Binary */ |
| 949 | sprintf(buf, "T10 VPD Binary Device Identifier: %s\n", |
| 950 | &vpd->device_identifier[0]); |
| 951 | break; |
| 952 | case 0x02: /* ASCII */ |
| 953 | sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n", |
| 954 | &vpd->device_identifier[0]); |
| 955 | break; |
| 956 | case 0x03: /* UTF-8 */ |
| 957 | sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n", |
| 958 | &vpd->device_identifier[0]); |
| 959 | break; |
| 960 | default: |
| 961 | sprintf(buf, "T10 VPD Device Identifier encoding unsupported:" |
| 962 | " 0x%02x", vpd->device_identifier_code_set); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 963 | ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 964 | break; |
| 965 | } |
| 966 | |
| 967 | if (p_buf) |
| 968 | strncpy(p_buf, buf, p_buf_len); |
| 969 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 970 | pr_debug("%s", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 971 | |
| 972 | return ret; |
| 973 | } |
| 974 | |
| 975 | int |
| 976 | transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83) |
| 977 | { |
| 978 | static const char hex_str[] = "0123456789abcdef"; |
| 979 | int j = 0, i = 4; /* offset to start of the identifer */ |
| 980 | |
| 981 | /* |
| 982 | * The VPD Code Set (encoding) |
| 983 | * |
| 984 | * from spc3r23.pdf Section 7.6.3.1 Table 296 |
| 985 | */ |
| 986 | vpd->device_identifier_code_set = (page_83[0] & 0x0f); |
| 987 | switch (vpd->device_identifier_code_set) { |
| 988 | case 0x01: /* Binary */ |
| 989 | vpd->device_identifier[j++] = |
| 990 | hex_str[vpd->device_identifier_type]; |
| 991 | while (i < (4 + page_83[3])) { |
| 992 | vpd->device_identifier[j++] = |
| 993 | hex_str[(page_83[i] & 0xf0) >> 4]; |
| 994 | vpd->device_identifier[j++] = |
| 995 | hex_str[page_83[i] & 0x0f]; |
| 996 | i++; |
| 997 | } |
| 998 | break; |
| 999 | case 0x02: /* ASCII */ |
| 1000 | case 0x03: /* UTF-8 */ |
| 1001 | while (i < (4 + page_83[3])) |
| 1002 | vpd->device_identifier[j++] = page_83[i++]; |
| 1003 | break; |
| 1004 | default: |
| 1005 | break; |
| 1006 | } |
| 1007 | |
| 1008 | return transport_dump_vpd_ident(vpd, NULL, 0); |
| 1009 | } |
| 1010 | EXPORT_SYMBOL(transport_set_vpd_ident); |
| 1011 | |
| 1012 | static void core_setup_task_attr_emulation(struct se_device *dev) |
| 1013 | { |
| 1014 | /* |
| 1015 | * If this device is from Target_Core_Mod/pSCSI, disable the |
| 1016 | * SAM Task Attribute emulation. |
| 1017 | * |
| 1018 | * This is currently not available in upsream Linux/SCSI Target |
| 1019 | * mode code, and is assumed to be disabled while using TCM/pSCSI. |
| 1020 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1021 | if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1022 | dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH; |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1027 | pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1028 | " device\n", dev->transport->name, |
| 1029 | dev->transport->get_device_rev(dev)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | static void scsi_dump_inquiry(struct se_device *dev) |
| 1033 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1034 | struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn; |
Sebastian Andrzej Siewior | e59a41b | 2012-01-10 14:16:57 +0100 | [diff] [blame] | 1035 | char buf[17]; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1036 | int i, device_type; |
| 1037 | /* |
| 1038 | * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer |
| 1039 | */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1040 | for (i = 0; i < 8; i++) |
| 1041 | if (wwn->vendor[i] >= 0x20) |
Sebastian Andrzej Siewior | e59a41b | 2012-01-10 14:16:57 +0100 | [diff] [blame] | 1042 | buf[i] = wwn->vendor[i]; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1043 | else |
Sebastian Andrzej Siewior | e59a41b | 2012-01-10 14:16:57 +0100 | [diff] [blame] | 1044 | buf[i] = ' '; |
| 1045 | buf[i] = '\0'; |
| 1046 | pr_debug(" Vendor: %s\n", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1047 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1048 | for (i = 0; i < 16; i++) |
| 1049 | if (wwn->model[i] >= 0x20) |
Sebastian Andrzej Siewior | e59a41b | 2012-01-10 14:16:57 +0100 | [diff] [blame] | 1050 | buf[i] = wwn->model[i]; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1051 | else |
Sebastian Andrzej Siewior | e59a41b | 2012-01-10 14:16:57 +0100 | [diff] [blame] | 1052 | buf[i] = ' '; |
| 1053 | buf[i] = '\0'; |
| 1054 | pr_debug(" Model: %s\n", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1055 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1056 | for (i = 0; i < 4; i++) |
| 1057 | if (wwn->revision[i] >= 0x20) |
Sebastian Andrzej Siewior | e59a41b | 2012-01-10 14:16:57 +0100 | [diff] [blame] | 1058 | buf[i] = wwn->revision[i]; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1059 | else |
Sebastian Andrzej Siewior | e59a41b | 2012-01-10 14:16:57 +0100 | [diff] [blame] | 1060 | buf[i] = ' '; |
| 1061 | buf[i] = '\0'; |
| 1062 | pr_debug(" Revision: %s\n", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1063 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1064 | device_type = dev->transport->get_device_type(dev); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1065 | pr_debug(" Type: %s ", scsi_device_type(device_type)); |
| 1066 | pr_debug(" ANSI SCSI revision: %02x\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1067 | dev->transport->get_device_rev(dev)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | struct se_device *transport_add_device_to_core_hba( |
| 1071 | struct se_hba *hba, |
| 1072 | struct se_subsystem_api *transport, |
| 1073 | struct se_subsystem_dev *se_dev, |
| 1074 | u32 device_flags, |
| 1075 | void *transport_dev, |
| 1076 | struct se_dev_limits *dev_limits, |
| 1077 | const char *inquiry_prod, |
| 1078 | const char *inquiry_rev) |
| 1079 | { |
Nicholas Bellinger | 12a18bd | 2011-03-14 04:06:06 -0700 | [diff] [blame] | 1080 | int force_pt; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1081 | struct se_device *dev; |
| 1082 | |
| 1083 | dev = kzalloc(sizeof(struct se_device), GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1084 | if (!dev) { |
| 1085 | pr_err("Unable to allocate memory for se_dev_t\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1086 | return NULL; |
| 1087 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1088 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1089 | dev->dev_flags = device_flags; |
| 1090 | dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1091 | dev->dev_ptr = transport_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1092 | dev->se_hba = hba; |
| 1093 | dev->se_sub_dev = se_dev; |
| 1094 | dev->transport = transport; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1095 | INIT_LIST_HEAD(&dev->dev_list); |
| 1096 | INIT_LIST_HEAD(&dev->dev_sep_list); |
| 1097 | INIT_LIST_HEAD(&dev->dev_tmr_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1098 | INIT_LIST_HEAD(&dev->delayed_cmd_list); |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 1099 | INIT_LIST_HEAD(&dev->state_list); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1100 | INIT_LIST_HEAD(&dev->qf_cmd_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1101 | spin_lock_init(&dev->execute_task_lock); |
| 1102 | spin_lock_init(&dev->delayed_cmd_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1103 | spin_lock_init(&dev->dev_reservation_lock); |
| 1104 | spin_lock_init(&dev->dev_status_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1105 | spin_lock_init(&dev->se_port_lock); |
| 1106 | spin_lock_init(&dev->se_tmr_lock); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1107 | spin_lock_init(&dev->qf_cmd_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1108 | atomic_set(&dev->dev_ordered_id, 0); |
| 1109 | |
| 1110 | se_dev_set_default_attribs(dev, dev_limits); |
| 1111 | |
| 1112 | dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX); |
| 1113 | dev->creation_time = get_jiffies_64(); |
| 1114 | spin_lock_init(&dev->stats_lock); |
| 1115 | |
| 1116 | spin_lock(&hba->device_lock); |
| 1117 | list_add_tail(&dev->dev_list, &hba->hba_dev_list); |
| 1118 | hba->dev_count++; |
| 1119 | spin_unlock(&hba->device_lock); |
| 1120 | /* |
| 1121 | * Setup the SAM Task Attribute emulation for struct se_device |
| 1122 | */ |
| 1123 | core_setup_task_attr_emulation(dev); |
| 1124 | /* |
| 1125 | * Force PR and ALUA passthrough emulation with internal object use. |
| 1126 | */ |
| 1127 | force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE); |
| 1128 | /* |
| 1129 | * Setup the Reservations infrastructure for struct se_device |
| 1130 | */ |
| 1131 | core_setup_reservations(dev, force_pt); |
| 1132 | /* |
| 1133 | * Setup the Asymmetric Logical Unit Assignment for struct se_device |
| 1134 | */ |
| 1135 | if (core_setup_alua(dev, force_pt) < 0) |
Dan Carpenter | 2962846 | 2012-07-20 10:07:34 +0300 | [diff] [blame] | 1136 | goto err_dev_list; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1137 | |
| 1138 | /* |
| 1139 | * Startup the struct se_device processing thread |
| 1140 | */ |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 1141 | dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1, |
| 1142 | dev->transport->name); |
| 1143 | if (!dev->tmr_wq) { |
| 1144 | pr_err("Unable to create tmr workqueue for %s\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1145 | dev->transport->name); |
Dan Carpenter | 2962846 | 2012-07-20 10:07:34 +0300 | [diff] [blame] | 1146 | goto err_dev_list; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1147 | } |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1148 | /* |
| 1149 | * Setup work_queue for QUEUE_FULL |
| 1150 | */ |
| 1151 | INIT_WORK(&dev->qf_work_queue, target_qf_do_work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1152 | /* |
| 1153 | * Preload the initial INQUIRY const values if we are doing |
| 1154 | * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI |
| 1155 | * passthrough because this is being provided by the backend LLD. |
| 1156 | * This is required so that transport_get_inquiry() copies these |
| 1157 | * originals once back into DEV_T10_WWN(dev) for the virtual device |
| 1158 | * setup. |
| 1159 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1160 | if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) { |
Roland Dreier | f22c119 | 2011-05-02 22:15:37 -0700 | [diff] [blame] | 1161 | if (!inquiry_prod || !inquiry_rev) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1162 | pr_err("All non TCM/pSCSI plugins require" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1163 | " INQUIRY consts\n"); |
Dan Carpenter | 2962846 | 2012-07-20 10:07:34 +0300 | [diff] [blame] | 1164 | goto err_wq; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1167 | strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8); |
| 1168 | strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16); |
| 1169 | strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1170 | } |
| 1171 | scsi_dump_inquiry(dev); |
| 1172 | |
Nicholas Bellinger | 12a18bd | 2011-03-14 04:06:06 -0700 | [diff] [blame] | 1173 | return dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1174 | |
Dan Carpenter | 2962846 | 2012-07-20 10:07:34 +0300 | [diff] [blame] | 1175 | err_wq: |
| 1176 | destroy_workqueue(dev->tmr_wq); |
| 1177 | err_dev_list: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1178 | spin_lock(&hba->device_lock); |
| 1179 | list_del(&dev->dev_list); |
| 1180 | hba->dev_count--; |
| 1181 | spin_unlock(&hba->device_lock); |
| 1182 | |
| 1183 | se_release_vpd_for_dev(dev); |
| 1184 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1185 | kfree(dev); |
| 1186 | |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | EXPORT_SYMBOL(transport_add_device_to_core_hba); |
| 1190 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1191 | int target_cmd_size_check(struct se_cmd *cmd, unsigned int size) |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1192 | { |
| 1193 | struct se_device *dev = cmd->se_dev; |
| 1194 | |
| 1195 | if (cmd->unknown_data_length) { |
| 1196 | cmd->data_length = size; |
| 1197 | } else if (size != cmd->data_length) { |
| 1198 | pr_warn("TARGET_CORE[%s]: Expected Transfer Length:" |
| 1199 | " %u does not match SCSI CDB Length: %u for SAM Opcode:" |
| 1200 | " 0x%02x\n", cmd->se_tfo->get_fabric_name(), |
| 1201 | cmd->data_length, size, cmd->t_task_cdb[0]); |
| 1202 | |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1203 | if (cmd->data_direction == DMA_TO_DEVICE) { |
| 1204 | pr_err("Rejecting underflow/overflow" |
| 1205 | " WRITE data\n"); |
| 1206 | goto out_invalid_cdb_field; |
| 1207 | } |
| 1208 | /* |
| 1209 | * Reject READ_* or WRITE_* with overflow/underflow for |
| 1210 | * type SCF_SCSI_DATA_CDB. |
| 1211 | */ |
| 1212 | if (dev->se_sub_dev->se_dev_attrib.block_size != 512) { |
| 1213 | pr_err("Failing OVERFLOW/UNDERFLOW for LBA op" |
| 1214 | " CDB on non 512-byte sector setup subsystem" |
| 1215 | " plugin: %s\n", dev->transport->name); |
| 1216 | /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */ |
| 1217 | goto out_invalid_cdb_field; |
| 1218 | } |
Nicholas Bellinger | 4c054ba | 2012-08-16 15:33:10 -0700 | [diff] [blame] | 1219 | /* |
| 1220 | * For the overflow case keep the existing fabric provided |
| 1221 | * ->data_length. Otherwise for the underflow case, reset |
| 1222 | * ->data_length to the smaller SCSI expected data transfer |
| 1223 | * length. |
| 1224 | */ |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1225 | if (size > cmd->data_length) { |
| 1226 | cmd->se_cmd_flags |= SCF_OVERFLOW_BIT; |
| 1227 | cmd->residual_count = (size - cmd->data_length); |
| 1228 | } else { |
| 1229 | cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT; |
| 1230 | cmd->residual_count = (cmd->data_length - size); |
Nicholas Bellinger | 4c054ba | 2012-08-16 15:33:10 -0700 | [diff] [blame] | 1231 | cmd->data_length = size; |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1232 | } |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | return 0; |
| 1236 | |
| 1237 | out_invalid_cdb_field: |
| 1238 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 1239 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
| 1240 | return -EINVAL; |
| 1241 | } |
| 1242 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1243 | /* |
| 1244 | * Used by fabric modules containing a local struct se_cmd within their |
| 1245 | * fabric dependent per I/O descriptor. |
| 1246 | */ |
| 1247 | void transport_init_se_cmd( |
| 1248 | struct se_cmd *cmd, |
| 1249 | struct target_core_fabric_ops *tfo, |
| 1250 | struct se_session *se_sess, |
| 1251 | u32 data_length, |
| 1252 | int data_direction, |
| 1253 | int task_attr, |
| 1254 | unsigned char *sense_buffer) |
| 1255 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1256 | INIT_LIST_HEAD(&cmd->se_lun_node); |
| 1257 | INIT_LIST_HEAD(&cmd->se_delayed_node); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1258 | INIT_LIST_HEAD(&cmd->se_qf_node); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 1259 | INIT_LIST_HEAD(&cmd->se_cmd_list); |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 1260 | INIT_LIST_HEAD(&cmd->state_list); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1261 | init_completion(&cmd->transport_lun_fe_stop_comp); |
| 1262 | init_completion(&cmd->transport_lun_stop_comp); |
| 1263 | init_completion(&cmd->t_transport_stop_comp); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 1264 | init_completion(&cmd->cmd_wait_comp); |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 1265 | init_completion(&cmd->task_stop_comp); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1266 | spin_lock_init(&cmd->t_state_lock); |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 1267 | cmd->transport_state = CMD_T_DEV_ACTIVE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1268 | |
| 1269 | cmd->se_tfo = tfo; |
| 1270 | cmd->se_sess = se_sess; |
| 1271 | cmd->data_length = data_length; |
| 1272 | cmd->data_direction = data_direction; |
| 1273 | cmd->sam_task_attr = task_attr; |
| 1274 | cmd->sense_buffer = sense_buffer; |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 1275 | |
| 1276 | cmd->state_active = false; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1277 | } |
| 1278 | EXPORT_SYMBOL(transport_init_se_cmd); |
| 1279 | |
| 1280 | static int transport_check_alloc_task_attr(struct se_cmd *cmd) |
| 1281 | { |
| 1282 | /* |
| 1283 | * Check if SAM Task Attribute emulation is enabled for this |
| 1284 | * struct se_device storage object |
| 1285 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1286 | if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1287 | return 0; |
| 1288 | |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 1289 | if (cmd->sam_task_attr == MSG_ACA_TAG) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1290 | pr_debug("SAM Task Attribute ACA" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1291 | " emulation is not supported\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1292 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1293 | } |
| 1294 | /* |
| 1295 | * Used to determine when ORDERED commands should go from |
| 1296 | * Dormant to Active status. |
| 1297 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1298 | 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] | 1299 | smp_mb__after_atomic_inc(); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1300 | 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] | 1301 | cmd->se_ordered_id, cmd->sam_task_attr, |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1302 | cmd->se_dev->transport->name); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1303 | return 0; |
| 1304 | } |
| 1305 | |
Andy Grover | a12f41f | 2012-04-03 15:51:20 -0700 | [diff] [blame] | 1306 | /* target_setup_cmd_from_cdb(): |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1307 | * |
| 1308 | * Called from fabric RX Thread. |
| 1309 | */ |
Andy Grover | a12f41f | 2012-04-03 15:51:20 -0700 | [diff] [blame] | 1310 | int target_setup_cmd_from_cdb( |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1311 | struct se_cmd *cmd, |
| 1312 | unsigned char *cdb) |
| 1313 | { |
Christoph Hellwig | cb4f4d3 | 2012-05-20 11:59:10 -0400 | [diff] [blame] | 1314 | struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev; |
| 1315 | u32 pr_reg_type = 0; |
| 1316 | u8 alua_ascq = 0; |
| 1317 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1318 | int ret; |
| 1319 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1320 | /* |
| 1321 | * Ensure that the received CDB is less than the max (252 + 8) bytes |
| 1322 | * for VARIABLE_LENGTH_CMD |
| 1323 | */ |
| 1324 | if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1325 | pr_err("Received SCSI CDB with command_size: %d that" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1326 | " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", |
| 1327 | scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE); |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1328 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 1329 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1330 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1331 | } |
| 1332 | /* |
| 1333 | * If the received CDB is larger than TCM_MAX_COMMAND_SIZE, |
| 1334 | * allocate the additional extended CDB buffer now.. Otherwise |
| 1335 | * setup the pointer from __t_task_cdb to t_task_cdb. |
| 1336 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1337 | if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) { |
| 1338 | cmd->t_task_cdb = kzalloc(scsi_command_size(cdb), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1339 | GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1340 | if (!cmd->t_task_cdb) { |
| 1341 | pr_err("Unable to allocate cmd->t_task_cdb" |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1342 | " %u > sizeof(cmd->__t_task_cdb): %lu ops\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1343 | scsi_command_size(cdb), |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1344 | (unsigned long)sizeof(cmd->__t_task_cdb)); |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1345 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 1346 | cmd->scsi_sense_reason = |
| 1347 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1348 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1349 | } |
| 1350 | } else |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1351 | cmd->t_task_cdb = &cmd->__t_task_cdb[0]; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1352 | /* |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1353 | * Copy the original CDB into cmd-> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1354 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1355 | memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb)); |
Christoph Hellwig | cb4f4d3 | 2012-05-20 11:59:10 -0400 | [diff] [blame] | 1356 | |
| 1357 | /* |
| 1358 | * Check for an existing UNIT ATTENTION condition |
| 1359 | */ |
| 1360 | if (core_scsi3_ua_check(cmd, cdb) < 0) { |
| 1361 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 1362 | cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION; |
| 1363 | return -EINVAL; |
| 1364 | } |
| 1365 | |
| 1366 | ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq); |
| 1367 | if (ret != 0) { |
| 1368 | /* |
| 1369 | * Set SCSI additional sense code (ASC) to 'LUN Not Accessible'; |
| 1370 | * The ALUA additional sense code qualifier (ASCQ) is determined |
| 1371 | * by the ALUA primary or secondary access state.. |
| 1372 | */ |
| 1373 | if (ret > 0) { |
| 1374 | pr_debug("[%s]: ALUA TG Port not available, " |
| 1375 | "SenseKey: NOT_READY, ASC/ASCQ: " |
| 1376 | "0x04/0x%02x\n", |
| 1377 | cmd->se_tfo->get_fabric_name(), alua_ascq); |
| 1378 | |
| 1379 | transport_set_sense_codes(cmd, 0x04, alua_ascq); |
| 1380 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 1381 | cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY; |
| 1382 | return -EINVAL; |
| 1383 | } |
| 1384 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 1385 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
| 1386 | return -EINVAL; |
| 1387 | } |
| 1388 | |
| 1389 | /* |
| 1390 | * Check status for SPC-3 Persistent Reservations |
| 1391 | */ |
| 1392 | if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type)) { |
| 1393 | if (su_dev->t10_pr.pr_ops.t10_seq_non_holder( |
| 1394 | cmd, cdb, pr_reg_type) != 0) { |
| 1395 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 1396 | cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT; |
| 1397 | cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; |
| 1398 | cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT; |
| 1399 | return -EBUSY; |
| 1400 | } |
| 1401 | /* |
| 1402 | * This means the CDB is allowed for the SCSI Initiator port |
| 1403 | * when said port is *NOT* holding the legacy SPC-2 or |
| 1404 | * SPC-3 Persistent Reservation. |
| 1405 | */ |
| 1406 | } |
| 1407 | |
Christoph Hellwig | 1fd032e | 2012-05-20 11:59:15 -0400 | [diff] [blame] | 1408 | ret = cmd->se_dev->transport->parse_cdb(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1409 | if (ret < 0) |
| 1410 | return ret; |
Christoph Hellwig | cb4f4d3 | 2012-05-20 11:59:10 -0400 | [diff] [blame] | 1411 | |
| 1412 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 1413 | cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE; |
| 1414 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 1415 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1416 | /* |
| 1417 | * Check for SAM Task Attribute Emulation |
| 1418 | */ |
| 1419 | if (transport_check_alloc_task_attr(cmd) < 0) { |
| 1420 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 1421 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1422 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1423 | } |
| 1424 | spin_lock(&cmd->se_lun->lun_sep_lock); |
| 1425 | if (cmd->se_lun->lun_sep) |
| 1426 | cmd->se_lun->lun_sep->sep_stats.cmd_pdus++; |
| 1427 | spin_unlock(&cmd->se_lun->lun_sep_lock); |
| 1428 | return 0; |
| 1429 | } |
Andy Grover | a12f41f | 2012-04-03 15:51:20 -0700 | [diff] [blame] | 1430 | EXPORT_SYMBOL(target_setup_cmd_from_cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1431 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1432 | /* |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1433 | * Used by fabric module frontends to queue tasks directly. |
| 1434 | * Many only be used from process context only |
| 1435 | */ |
| 1436 | int transport_handle_cdb_direct( |
| 1437 | struct se_cmd *cmd) |
| 1438 | { |
Nicholas Bellinger | dd8ae59 | 2011-07-30 05:03:58 -0700 | [diff] [blame] | 1439 | int ret; |
| 1440 | |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1441 | if (!cmd->se_lun) { |
| 1442 | dump_stack(); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1443 | pr_err("cmd->se_lun is NULL\n"); |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1444 | return -EINVAL; |
| 1445 | } |
| 1446 | if (in_interrupt()) { |
| 1447 | dump_stack(); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1448 | pr_err("transport_generic_handle_cdb cannot be called" |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1449 | " from interrupt context\n"); |
| 1450 | return -EINVAL; |
| 1451 | } |
Nicholas Bellinger | dd8ae59 | 2011-07-30 05:03:58 -0700 | [diff] [blame] | 1452 | /* |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 1453 | * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that |
| 1454 | * outstanding descriptors are handled correctly during shutdown via |
| 1455 | * transport_wait_for_tasks() |
Nicholas Bellinger | dd8ae59 | 2011-07-30 05:03:58 -0700 | [diff] [blame] | 1456 | * |
| 1457 | * Also, we don't take cmd->t_state_lock here as we only expect |
| 1458 | * this to be called for initial descriptor submission. |
| 1459 | */ |
| 1460 | cmd->t_state = TRANSPORT_NEW_CMD; |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 1461 | cmd->transport_state |= CMD_T_ACTIVE; |
| 1462 | |
Nicholas Bellinger | dd8ae59 | 2011-07-30 05:03:58 -0700 | [diff] [blame] | 1463 | /* |
| 1464 | * transport_generic_new_cmd() is already handling QUEUE_FULL, |
| 1465 | * so follow TRANSPORT_NEW_CMD processing thread context usage |
| 1466 | * and call transport_generic_request_failure() if necessary.. |
| 1467 | */ |
| 1468 | ret = transport_generic_new_cmd(cmd); |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1469 | if (ret < 0) |
| 1470 | transport_generic_request_failure(cmd); |
| 1471 | |
Nicholas Bellinger | dd8ae59 | 2011-07-30 05:03:58 -0700 | [diff] [blame] | 1472 | return 0; |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1473 | } |
| 1474 | EXPORT_SYMBOL(transport_handle_cdb_direct); |
| 1475 | |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1476 | /** |
| 1477 | * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd |
| 1478 | * |
| 1479 | * @se_cmd: command descriptor to submit |
| 1480 | * @se_sess: associated se_sess for endpoint |
| 1481 | * @cdb: pointer to SCSI CDB |
| 1482 | * @sense: pointer to SCSI sense buffer |
| 1483 | * @unpacked_lun: unpacked LUN to reference for struct se_lun |
| 1484 | * @data_length: fabric expected data transfer length |
| 1485 | * @task_addr: SAM task attribute |
| 1486 | * @data_dir: DMA data direction |
| 1487 | * @flags: flags for command submission from target_sc_flags_tables |
| 1488 | * |
Roland Dreier | d6dfc86 | 2012-07-16 11:04:39 -0700 | [diff] [blame] | 1489 | * Returns non zero to signal active I/O shutdown failure. All other |
| 1490 | * setup exceptions will be returned as a SCSI CHECK_CONDITION response, |
| 1491 | * but still return zero here. |
| 1492 | * |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1493 | * This may only be called from process context, and also currently |
| 1494 | * assumes internal allocation of fabric payload buffer by target-core. |
| 1495 | **/ |
Roland Dreier | d6dfc86 | 2012-07-16 11:04:39 -0700 | [diff] [blame] | 1496 | int target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess, |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1497 | unsigned char *cdb, unsigned char *sense, u32 unpacked_lun, |
| 1498 | u32 data_length, int task_attr, int data_dir, int flags) |
| 1499 | { |
| 1500 | struct se_portal_group *se_tpg; |
| 1501 | int rc; |
| 1502 | |
| 1503 | se_tpg = se_sess->se_tpg; |
| 1504 | BUG_ON(!se_tpg); |
| 1505 | BUG_ON(se_cmd->se_tfo || se_cmd->se_sess); |
| 1506 | BUG_ON(in_interrupt()); |
| 1507 | /* |
| 1508 | * Initialize se_cmd for target operation. From this point |
| 1509 | * exceptions are handled by sending exception status via |
| 1510 | * target_core_fabric_ops->queue_status() callback |
| 1511 | */ |
| 1512 | transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, |
| 1513 | data_length, data_dir, task_attr, sense); |
Sebastian Andrzej Siewior | b0d7994 | 2012-01-10 14:16:59 +0100 | [diff] [blame] | 1514 | if (flags & TARGET_SCF_UNKNOWN_SIZE) |
| 1515 | se_cmd->unknown_data_length = 1; |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1516 | /* |
| 1517 | * Obtain struct se_cmd->cmd_kref reference and add new cmd to |
| 1518 | * se_sess->sess_cmd_list. A second kref_get here is necessary |
| 1519 | * for fabrics using TARGET_SCF_ACK_KREF that expect a second |
| 1520 | * kref_put() to happen during fabric packet acknowledgement. |
| 1521 | */ |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 1522 | rc = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF)); |
| 1523 | if (rc) |
Roland Dreier | d6dfc86 | 2012-07-16 11:04:39 -0700 | [diff] [blame] | 1524 | return rc; |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1525 | /* |
| 1526 | * Signal bidirectional data payloads to target-core |
| 1527 | */ |
| 1528 | if (flags & TARGET_SCF_BIDI_OP) |
| 1529 | se_cmd->se_cmd_flags |= SCF_BIDI; |
| 1530 | /* |
| 1531 | * Locate se_lun pointer and attach it to struct se_cmd |
| 1532 | */ |
Nicholas Bellinger | 735703c | 2012-01-20 19:02:56 -0800 | [diff] [blame] | 1533 | if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) { |
| 1534 | transport_send_check_condition_and_sense(se_cmd, |
| 1535 | se_cmd->scsi_sense_reason, 0); |
| 1536 | target_put_sess_cmd(se_sess, se_cmd); |
Roland Dreier | d6dfc86 | 2012-07-16 11:04:39 -0700 | [diff] [blame] | 1537 | return 0; |
Nicholas Bellinger | 735703c | 2012-01-20 19:02:56 -0800 | [diff] [blame] | 1538 | } |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1539 | |
Andy Grover | a12f41f | 2012-04-03 15:51:20 -0700 | [diff] [blame] | 1540 | rc = target_setup_cmd_from_cdb(se_cmd, cdb); |
Nicholas Bellinger | 735703c | 2012-01-20 19:02:56 -0800 | [diff] [blame] | 1541 | if (rc != 0) { |
| 1542 | transport_generic_request_failure(se_cmd); |
Roland Dreier | d6dfc86 | 2012-07-16 11:04:39 -0700 | [diff] [blame] | 1543 | return 0; |
Nicholas Bellinger | 735703c | 2012-01-20 19:02:56 -0800 | [diff] [blame] | 1544 | } |
Andy Grover | 11e319e | 2012-04-03 15:51:28 -0700 | [diff] [blame] | 1545 | |
| 1546 | /* |
| 1547 | * Check if we need to delay processing because of ALUA |
| 1548 | * Active/NonOptimized primary access state.. |
| 1549 | */ |
| 1550 | core_alua_check_nonop_delay(se_cmd); |
| 1551 | |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1552 | transport_handle_cdb_direct(se_cmd); |
Roland Dreier | d6dfc86 | 2012-07-16 11:04:39 -0700 | [diff] [blame] | 1553 | return 0; |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1554 | } |
| 1555 | EXPORT_SYMBOL(target_submit_cmd); |
| 1556 | |
Nicholas Bellinger | 9f0d05c | 2012-02-25 05:02:48 -0800 | [diff] [blame] | 1557 | static void target_complete_tmr_failure(struct work_struct *work) |
| 1558 | { |
| 1559 | struct se_cmd *se_cmd = container_of(work, struct se_cmd, work); |
| 1560 | |
| 1561 | se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST; |
| 1562 | se_cmd->se_tfo->queue_tm_rsp(se_cmd); |
| 1563 | transport_generic_free_cmd(se_cmd, 0); |
| 1564 | } |
| 1565 | |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1566 | /** |
| 1567 | * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd |
| 1568 | * for TMR CDBs |
| 1569 | * |
| 1570 | * @se_cmd: command descriptor to submit |
| 1571 | * @se_sess: associated se_sess for endpoint |
| 1572 | * @sense: pointer to SCSI sense buffer |
| 1573 | * @unpacked_lun: unpacked LUN to reference for struct se_lun |
| 1574 | * @fabric_context: fabric context for TMR req |
| 1575 | * @tm_type: Type of TM request |
Nicholas Bellinger | c0974f8 | 2012-02-25 05:10:04 -0800 | [diff] [blame] | 1576 | * @gfp: gfp type for caller |
| 1577 | * @tag: referenced task tag for TMR_ABORT_TASK |
Nicholas Bellinger | c7042ca | 2012-02-25 01:40:24 -0800 | [diff] [blame] | 1578 | * @flags: submit cmd flags |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1579 | * |
| 1580 | * Callable from all contexts. |
| 1581 | **/ |
| 1582 | |
Nicholas Bellinger | c7042ca | 2012-02-25 01:40:24 -0800 | [diff] [blame] | 1583 | int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess, |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1584 | unsigned char *sense, u32 unpacked_lun, |
Nicholas Bellinger | c0974f8 | 2012-02-25 05:10:04 -0800 | [diff] [blame] | 1585 | void *fabric_tmr_ptr, unsigned char tm_type, |
| 1586 | gfp_t gfp, unsigned int tag, int flags) |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1587 | { |
| 1588 | struct se_portal_group *se_tpg; |
| 1589 | int ret; |
| 1590 | |
| 1591 | se_tpg = se_sess->se_tpg; |
| 1592 | BUG_ON(!se_tpg); |
| 1593 | |
| 1594 | transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, |
| 1595 | 0, DMA_NONE, MSG_SIMPLE_TAG, sense); |
Nicholas Bellinger | c7042ca | 2012-02-25 01:40:24 -0800 | [diff] [blame] | 1596 | /* |
| 1597 | * FIXME: Currently expect caller to handle se_cmd->se_tmr_req |
| 1598 | * allocation failure. |
| 1599 | */ |
Nicholas Bellinger | c0974f8 | 2012-02-25 05:10:04 -0800 | [diff] [blame] | 1600 | ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp); |
Nicholas Bellinger | c7042ca | 2012-02-25 01:40:24 -0800 | [diff] [blame] | 1601 | if (ret < 0) |
| 1602 | return -ENOMEM; |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1603 | |
Nicholas Bellinger | c0974f8 | 2012-02-25 05:10:04 -0800 | [diff] [blame] | 1604 | if (tm_type == TMR_ABORT_TASK) |
| 1605 | se_cmd->se_tmr_req->ref_task_tag = tag; |
| 1606 | |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1607 | /* See target_submit_cmd for commentary */ |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 1608 | ret = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF)); |
| 1609 | if (ret) { |
| 1610 | core_tmr_release_req(se_cmd->se_tmr_req); |
| 1611 | return ret; |
| 1612 | } |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1613 | |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1614 | ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun); |
| 1615 | if (ret) { |
Nicholas Bellinger | 9f0d05c | 2012-02-25 05:02:48 -0800 | [diff] [blame] | 1616 | /* |
| 1617 | * For callback during failure handling, push this work off |
| 1618 | * to process context with TMR_LUN_DOES_NOT_EXIST status. |
| 1619 | */ |
| 1620 | INIT_WORK(&se_cmd->work, target_complete_tmr_failure); |
| 1621 | schedule_work(&se_cmd->work); |
Nicholas Bellinger | c7042ca | 2012-02-25 01:40:24 -0800 | [diff] [blame] | 1622 | return 0; |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1623 | } |
| 1624 | transport_generic_handle_tmr(se_cmd); |
Nicholas Bellinger | c7042ca | 2012-02-25 01:40:24 -0800 | [diff] [blame] | 1625 | return 0; |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1626 | } |
| 1627 | EXPORT_SYMBOL(target_submit_tmr); |
| 1628 | |
Christoph Hellwig | cdbb70b | 2011-10-17 13:56:46 -0400 | [diff] [blame] | 1629 | /* |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 1630 | * If the cmd is active, request it to be stopped and sleep until it |
Christoph Hellwig | cdbb70b | 2011-10-17 13:56:46 -0400 | [diff] [blame] | 1631 | * has completed. |
| 1632 | */ |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 1633 | bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags) |
Christoph Hellwig | cdbb70b | 2011-10-17 13:56:46 -0400 | [diff] [blame] | 1634 | { |
Christoph Hellwig | cdbb70b | 2011-10-17 13:56:46 -0400 | [diff] [blame] | 1635 | bool was_active = false; |
| 1636 | |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 1637 | if (cmd->transport_state & CMD_T_BUSY) { |
| 1638 | cmd->transport_state |= CMD_T_REQUEST_STOP; |
Christoph Hellwig | cdbb70b | 2011-10-17 13:56:46 -0400 | [diff] [blame] | 1639 | spin_unlock_irqrestore(&cmd->t_state_lock, *flags); |
| 1640 | |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 1641 | pr_debug("cmd %p waiting to complete\n", cmd); |
| 1642 | wait_for_completion(&cmd->task_stop_comp); |
| 1643 | pr_debug("cmd %p stopped successfully\n", cmd); |
Christoph Hellwig | cdbb70b | 2011-10-17 13:56:46 -0400 | [diff] [blame] | 1644 | |
| 1645 | spin_lock_irqsave(&cmd->t_state_lock, *flags); |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 1646 | cmd->transport_state &= ~CMD_T_REQUEST_STOP; |
| 1647 | cmd->transport_state &= ~CMD_T_BUSY; |
Christoph Hellwig | cdbb70b | 2011-10-17 13:56:46 -0400 | [diff] [blame] | 1648 | was_active = true; |
| 1649 | } |
| 1650 | |
Christoph Hellwig | cdbb70b | 2011-10-17 13:56:46 -0400 | [diff] [blame] | 1651 | return was_active; |
| 1652 | } |
| 1653 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1654 | /* |
| 1655 | * Handle SAM-esque emulation for generic transport request failures. |
| 1656 | */ |
Nicholas Bellinger | 2fbff12 | 2012-02-10 16:18:11 -0800 | [diff] [blame] | 1657 | void transport_generic_request_failure(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1658 | { |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1659 | int ret = 0; |
| 1660 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1661 | pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1662 | " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd), |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1663 | cmd->t_task_cdb[0]); |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1664 | pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1665 | cmd->se_tfo->get_cmd_state(cmd), |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1666 | cmd->t_state, cmd->scsi_sense_reason); |
Christoph Hellwig | d43d6ae | 2012-04-24 00:25:08 -0400 | [diff] [blame] | 1667 | pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n", |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 1668 | (cmd->transport_state & CMD_T_ACTIVE) != 0, |
| 1669 | (cmd->transport_state & CMD_T_STOP) != 0, |
| 1670 | (cmd->transport_state & CMD_T_SENT) != 0); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1671 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1672 | /* |
| 1673 | * For SAM Task Attribute emulation for failed struct se_cmd |
| 1674 | */ |
| 1675 | if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED) |
| 1676 | transport_complete_task_attr(cmd); |
| 1677 | |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1678 | switch (cmd->scsi_sense_reason) { |
| 1679 | case TCM_NON_EXISTENT_LUN: |
| 1680 | case TCM_UNSUPPORTED_SCSI_OPCODE: |
| 1681 | case TCM_INVALID_CDB_FIELD: |
| 1682 | case TCM_INVALID_PARAMETER_LIST: |
| 1683 | case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE: |
| 1684 | case TCM_UNKNOWN_MODE_PAGE: |
| 1685 | case TCM_WRITE_PROTECTED: |
Roland Dreier | e2397c7 | 2012-07-16 15:34:21 -0700 | [diff] [blame] | 1686 | case TCM_ADDRESS_OUT_OF_RANGE: |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1687 | case TCM_CHECK_CONDITION_ABORT_CMD: |
| 1688 | case TCM_CHECK_CONDITION_UNIT_ATTENTION: |
| 1689 | case TCM_CHECK_CONDITION_NOT_READY: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1690 | break; |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1691 | case TCM_RESERVATION_CONFLICT: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1692 | /* |
| 1693 | * No SENSE Data payload for this case, set SCSI Status |
| 1694 | * and queue the response to $FABRIC_MOD. |
| 1695 | * |
| 1696 | * Uses linux/include/scsi/scsi.h SAM status codes defs |
| 1697 | */ |
| 1698 | cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; |
| 1699 | /* |
| 1700 | * For UA Interlock Code 11b, a RESERVATION CONFLICT will |
| 1701 | * establish a UNIT ATTENTION with PREVIOUS RESERVATION |
| 1702 | * CONFLICT STATUS. |
| 1703 | * |
| 1704 | * See spc4r17, section 7.4.6 Control Mode Page, Table 349 |
| 1705 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1706 | if (cmd->se_sess && |
| 1707 | cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2) |
| 1708 | core_scsi3_ua_allocate(cmd->se_sess->se_node_acl, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1709 | cmd->orig_fe_lun, 0x2C, |
| 1710 | ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS); |
| 1711 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1712 | ret = cmd->se_tfo->queue_status(cmd); |
Nicholas Bellinger | f147abb | 2011-10-25 23:57:41 -0700 | [diff] [blame] | 1713 | if (ret == -EAGAIN || ret == -ENOMEM) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1714 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1715 | goto check_stop; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1716 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1717 | pr_err("Unknown transport error for CDB 0x%02x: %d\n", |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1718 | cmd->t_task_cdb[0], cmd->scsi_sense_reason); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1719 | cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; |
| 1720 | break; |
| 1721 | } |
Christoph Hellwig | f314643 | 2012-07-08 15:58:48 -0400 | [diff] [blame] | 1722 | |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1723 | ret = transport_send_check_condition_and_sense(cmd, |
| 1724 | cmd->scsi_sense_reason, 0); |
| 1725 | if (ret == -EAGAIN || ret == -ENOMEM) |
| 1726 | goto queue_full; |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1727 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1728 | check_stop: |
| 1729 | transport_lun_remove_cmd(cmd); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1730 | if (!transport_cmd_check_stop_to_fabric(cmd)) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1731 | ; |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1732 | return; |
| 1733 | |
| 1734 | queue_full: |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 1735 | cmd->t_state = TRANSPORT_COMPLETE_QF_OK; |
| 1736 | transport_handle_queue_full(cmd, cmd->se_dev); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1737 | } |
Nicholas Bellinger | 2fbff12 | 2012-02-10 16:18:11 -0800 | [diff] [blame] | 1738 | EXPORT_SYMBOL(transport_generic_request_failure); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1739 | |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1740 | static void __target_execute_cmd(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1741 | { |
Christoph Hellwig | 0c2ad7d | 2012-06-17 18:40:52 -0400 | [diff] [blame] | 1742 | int error = 0; |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1743 | |
| 1744 | spin_lock_irq(&cmd->t_state_lock); |
| 1745 | cmd->transport_state |= (CMD_T_BUSY|CMD_T_SENT); |
| 1746 | spin_unlock_irq(&cmd->t_state_lock); |
| 1747 | |
| 1748 | if (cmd->execute_cmd) |
| 1749 | error = cmd->execute_cmd(cmd); |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1750 | |
| 1751 | if (error) { |
| 1752 | spin_lock_irq(&cmd->t_state_lock); |
| 1753 | cmd->transport_state &= ~(CMD_T_BUSY|CMD_T_SENT); |
| 1754 | spin_unlock_irq(&cmd->t_state_lock); |
| 1755 | |
| 1756 | transport_generic_request_failure(cmd); |
| 1757 | } |
| 1758 | } |
| 1759 | |
Christoph Hellwig | 70baf0a | 2012-07-08 15:58:39 -0400 | [diff] [blame] | 1760 | void target_execute_cmd(struct se_cmd *cmd) |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1761 | { |
| 1762 | struct se_device *dev = cmd->se_dev; |
| 1763 | |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 1764 | /* |
Christoph Hellwig | d59a02b | 2012-07-08 15:58:40 -0400 | [diff] [blame] | 1765 | * If the received CDB has aleady been aborted stop processing it here. |
| 1766 | */ |
| 1767 | if (transport_check_aborted_status(cmd, 1)) |
| 1768 | return; |
| 1769 | |
| 1770 | /* |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 1771 | * Determine if IOCTL context caller in requesting the stopping of this |
| 1772 | * command for LUN shutdown purposes. |
| 1773 | */ |
| 1774 | spin_lock_irq(&cmd->t_state_lock); |
| 1775 | if (cmd->transport_state & CMD_T_LUN_STOP) { |
| 1776 | pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n", |
| 1777 | __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd)); |
| 1778 | |
| 1779 | cmd->transport_state &= ~CMD_T_ACTIVE; |
| 1780 | spin_unlock_irq(&cmd->t_state_lock); |
| 1781 | complete(&cmd->transport_lun_stop_comp); |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1782 | return; |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 1783 | } |
| 1784 | /* |
| 1785 | * Determine if frontend context caller is requesting the stopping of |
| 1786 | * this command for frontend exceptions. |
| 1787 | */ |
| 1788 | if (cmd->transport_state & CMD_T_STOP) { |
| 1789 | pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n", |
| 1790 | __func__, __LINE__, |
| 1791 | cmd->se_tfo->get_task_tag(cmd)); |
| 1792 | |
| 1793 | spin_unlock_irq(&cmd->t_state_lock); |
| 1794 | complete(&cmd->t_transport_stop_comp); |
| 1795 | return; |
| 1796 | } |
| 1797 | |
| 1798 | cmd->t_state = TRANSPORT_PROCESSING; |
| 1799 | spin_unlock_irq(&cmd->t_state_lock); |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1800 | |
| 1801 | if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) |
| 1802 | goto execute; |
| 1803 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1804 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1805 | * 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] | 1806 | * to allow the passed struct se_cmd list of tasks to the front of the list. |
| 1807 | */ |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1808 | switch (cmd->sam_task_attr) { |
| 1809 | case MSG_HEAD_TAG: |
| 1810 | pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, " |
| 1811 | "se_ordered_id: %u\n", |
| 1812 | cmd->t_task_cdb[0], cmd->se_ordered_id); |
| 1813 | goto execute; |
| 1814 | case MSG_ORDERED_TAG: |
| 1815 | atomic_inc(&dev->dev_ordered_sync); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1816 | smp_mb__after_atomic_inc(); |
| 1817 | |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1818 | pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, " |
| 1819 | " se_ordered_id: %u\n", |
| 1820 | cmd->t_task_cdb[0], cmd->se_ordered_id); |
| 1821 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1822 | /* |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1823 | * Execute an ORDERED command if no other older commands |
| 1824 | * exist that need to be completed first. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1825 | */ |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1826 | if (!atomic_read(&dev->simple_cmds)) |
| 1827 | goto execute; |
| 1828 | break; |
| 1829 | default: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1830 | /* |
| 1831 | * For SIMPLE and UNTAGGED Task Attribute commands |
| 1832 | */ |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1833 | atomic_inc(&dev->simple_cmds); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1834 | smp_mb__after_atomic_inc(); |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1835 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1836 | } |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1837 | |
| 1838 | if (atomic_read(&dev->dev_ordered_sync) != 0) { |
| 1839 | spin_lock(&dev->delayed_cmd_lock); |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1840 | list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list); |
| 1841 | spin_unlock(&dev->delayed_cmd_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1842 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1843 | pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1844 | " delayed CMD list, se_ordered_id: %u\n", |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1845 | cmd->t_task_cdb[0], cmd->sam_task_attr, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1846 | cmd->se_ordered_id); |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1847 | return; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1848 | } |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1849 | |
| 1850 | execute: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1851 | /* |
| 1852 | * Otherwise, no ORDERED task attributes exist.. |
| 1853 | */ |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1854 | __target_execute_cmd(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1855 | } |
Christoph Hellwig | 70baf0a | 2012-07-08 15:58:39 -0400 | [diff] [blame] | 1856 | EXPORT_SYMBOL(target_execute_cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1857 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1858 | /* |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1859 | * Process all commands up to the last received ORDERED task attribute which |
| 1860 | * requires another blocking boundary |
| 1861 | */ |
| 1862 | static void target_restart_delayed_cmds(struct se_device *dev) |
| 1863 | { |
| 1864 | for (;;) { |
| 1865 | struct se_cmd *cmd; |
| 1866 | |
| 1867 | spin_lock(&dev->delayed_cmd_lock); |
| 1868 | if (list_empty(&dev->delayed_cmd_list)) { |
| 1869 | spin_unlock(&dev->delayed_cmd_lock); |
| 1870 | break; |
| 1871 | } |
| 1872 | |
| 1873 | cmd = list_entry(dev->delayed_cmd_list.next, |
| 1874 | struct se_cmd, se_delayed_node); |
| 1875 | list_del(&cmd->se_delayed_node); |
| 1876 | spin_unlock(&dev->delayed_cmd_lock); |
| 1877 | |
| 1878 | __target_execute_cmd(cmd); |
| 1879 | |
| 1880 | if (cmd->sam_task_attr == MSG_ORDERED_TAG) |
| 1881 | break; |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | /* |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 1886 | * Called from I/O completion to determine which dormant/delayed |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1887 | * and ordered cmds need to have their tasks added to the execution queue. |
| 1888 | */ |
| 1889 | static void transport_complete_task_attr(struct se_cmd *cmd) |
| 1890 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1891 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1892 | |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 1893 | if (cmd->sam_task_attr == MSG_SIMPLE_TAG) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1894 | atomic_dec(&dev->simple_cmds); |
| 1895 | smp_mb__after_atomic_dec(); |
| 1896 | dev->dev_cur_ordered_id++; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1897 | pr_debug("Incremented dev->dev_cur_ordered_id: %u for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1898 | " SIMPLE: %u\n", dev->dev_cur_ordered_id, |
| 1899 | cmd->se_ordered_id); |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 1900 | } else if (cmd->sam_task_attr == MSG_HEAD_TAG) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1901 | dev->dev_cur_ordered_id++; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1902 | pr_debug("Incremented dev_cur_ordered_id: %u for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1903 | " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id, |
| 1904 | cmd->se_ordered_id); |
Nicholas Bellinger | e66ecd5 | 2011-05-19 20:19:14 -0700 | [diff] [blame] | 1905 | } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1906 | atomic_dec(&dev->dev_ordered_sync); |
| 1907 | smp_mb__after_atomic_dec(); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1908 | |
| 1909 | dev->dev_cur_ordered_id++; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1910 | pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1911 | " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id); |
| 1912 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1913 | |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1914 | target_restart_delayed_cmds(dev); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1915 | } |
| 1916 | |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 1917 | static void transport_complete_qf(struct se_cmd *cmd) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1918 | { |
| 1919 | int ret = 0; |
| 1920 | |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 1921 | if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED) |
| 1922 | transport_complete_task_attr(cmd); |
| 1923 | |
| 1924 | if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) { |
| 1925 | ret = cmd->se_tfo->queue_status(cmd); |
| 1926 | if (ret) |
| 1927 | goto out; |
| 1928 | } |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1929 | |
| 1930 | switch (cmd->data_direction) { |
| 1931 | case DMA_FROM_DEVICE: |
| 1932 | ret = cmd->se_tfo->queue_data_in(cmd); |
| 1933 | break; |
| 1934 | case DMA_TO_DEVICE: |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 1935 | if (cmd->t_bidi_data_sg) { |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1936 | ret = cmd->se_tfo->queue_data_in(cmd); |
| 1937 | if (ret < 0) |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 1938 | break; |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1939 | } |
| 1940 | /* Fall through for DMA_TO_DEVICE */ |
| 1941 | case DMA_NONE: |
| 1942 | ret = cmd->se_tfo->queue_status(cmd); |
| 1943 | break; |
| 1944 | default: |
| 1945 | break; |
| 1946 | } |
| 1947 | |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 1948 | out: |
| 1949 | if (ret < 0) { |
| 1950 | transport_handle_queue_full(cmd, cmd->se_dev); |
| 1951 | return; |
| 1952 | } |
| 1953 | transport_lun_remove_cmd(cmd); |
| 1954 | transport_cmd_check_stop_to_fabric(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | static void transport_handle_queue_full( |
| 1958 | struct se_cmd *cmd, |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 1959 | struct se_device *dev) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1960 | { |
| 1961 | spin_lock_irq(&dev->qf_cmd_lock); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1962 | list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list); |
| 1963 | atomic_inc(&dev->dev_qf_count); |
| 1964 | smp_mb__after_atomic_inc(); |
| 1965 | spin_unlock_irq(&cmd->se_dev->qf_cmd_lock); |
| 1966 | |
| 1967 | schedule_work(&cmd->se_dev->qf_work_queue); |
| 1968 | } |
| 1969 | |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 1970 | static void target_complete_ok_work(struct work_struct *work) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1971 | { |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 1972 | struct se_cmd *cmd = container_of(work, struct se_cmd, work); |
Paolo Bonzini | 27a2709 | 2012-09-05 17:09:14 +0200 | [diff] [blame^] | 1973 | int ret; |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 1974 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1975 | /* |
| 1976 | * Check if we need to move delayed/dormant tasks from cmds on the |
| 1977 | * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task |
| 1978 | * Attribute. |
| 1979 | */ |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1980 | if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1981 | transport_complete_task_attr(cmd); |
| 1982 | /* |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1983 | * Check to schedule QUEUE_FULL work, or execute an existing |
| 1984 | * cmd->transport_qf_callback() |
| 1985 | */ |
| 1986 | if (atomic_read(&cmd->se_dev->dev_qf_count) != 0) |
| 1987 | schedule_work(&cmd->se_dev->qf_work_queue); |
| 1988 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1989 | /* |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1990 | * Check if we need to retrieve a sense buffer from |
| 1991 | * the struct se_cmd in question. |
| 1992 | */ |
| 1993 | if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) { |
Paolo Bonzini | 27a2709 | 2012-09-05 17:09:14 +0200 | [diff] [blame^] | 1994 | WARN_ON(!cmd->scsi_status); |
| 1995 | transport_get_sense_data(cmd); |
| 1996 | ret = transport_send_check_condition_and_sense( |
| 1997 | cmd, 0, 1); |
| 1998 | if (ret == -EAGAIN || ret == -ENOMEM) |
| 1999 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2000 | |
Paolo Bonzini | 27a2709 | 2012-09-05 17:09:14 +0200 | [diff] [blame^] | 2001 | transport_lun_remove_cmd(cmd); |
| 2002 | transport_cmd_check_stop_to_fabric(cmd); |
| 2003 | return; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2004 | } |
| 2005 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2006 | * Check for a callback, used by amongst other things |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2007 | * XDWRITE_READ_10 emulation. |
| 2008 | */ |
| 2009 | if (cmd->transport_complete_callback) |
| 2010 | cmd->transport_complete_callback(cmd); |
| 2011 | |
| 2012 | switch (cmd->data_direction) { |
| 2013 | case DMA_FROM_DEVICE: |
| 2014 | spin_lock(&cmd->se_lun->lun_sep_lock); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2015 | if (cmd->se_lun->lun_sep) { |
| 2016 | cmd->se_lun->lun_sep->sep_stats.tx_data_octets += |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2017 | cmd->data_length; |
| 2018 | } |
| 2019 | spin_unlock(&cmd->se_lun->lun_sep_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2020 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2021 | ret = cmd->se_tfo->queue_data_in(cmd); |
Nicholas Bellinger | f147abb | 2011-10-25 23:57:41 -0700 | [diff] [blame] | 2022 | if (ret == -EAGAIN || ret == -ENOMEM) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2023 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2024 | break; |
| 2025 | case DMA_TO_DEVICE: |
| 2026 | spin_lock(&cmd->se_lun->lun_sep_lock); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2027 | if (cmd->se_lun->lun_sep) { |
| 2028 | cmd->se_lun->lun_sep->sep_stats.rx_data_octets += |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2029 | cmd->data_length; |
| 2030 | } |
| 2031 | spin_unlock(&cmd->se_lun->lun_sep_lock); |
| 2032 | /* |
| 2033 | * Check if we need to send READ payload for BIDI-COMMAND |
| 2034 | */ |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2035 | if (cmd->t_bidi_data_sg) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2036 | spin_lock(&cmd->se_lun->lun_sep_lock); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2037 | if (cmd->se_lun->lun_sep) { |
| 2038 | cmd->se_lun->lun_sep->sep_stats.tx_data_octets += |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2039 | cmd->data_length; |
| 2040 | } |
| 2041 | spin_unlock(&cmd->se_lun->lun_sep_lock); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2042 | ret = cmd->se_tfo->queue_data_in(cmd); |
Nicholas Bellinger | f147abb | 2011-10-25 23:57:41 -0700 | [diff] [blame] | 2043 | if (ret == -EAGAIN || ret == -ENOMEM) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2044 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2045 | break; |
| 2046 | } |
| 2047 | /* Fall through for DMA_TO_DEVICE */ |
| 2048 | case DMA_NONE: |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2049 | ret = cmd->se_tfo->queue_status(cmd); |
Nicholas Bellinger | f147abb | 2011-10-25 23:57:41 -0700 | [diff] [blame] | 2050 | if (ret == -EAGAIN || ret == -ENOMEM) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2051 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2052 | break; |
| 2053 | default: |
| 2054 | break; |
| 2055 | } |
| 2056 | |
| 2057 | transport_lun_remove_cmd(cmd); |
| 2058 | transport_cmd_check_stop_to_fabric(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2059 | return; |
| 2060 | |
| 2061 | queue_full: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2062 | pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p," |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2063 | " data_direction: %d\n", cmd, cmd->data_direction); |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 2064 | cmd->t_state = TRANSPORT_COMPLETE_QF_OK; |
| 2065 | transport_handle_queue_full(cmd, cmd->se_dev); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2066 | } |
| 2067 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2068 | static inline void transport_free_sgl(struct scatterlist *sgl, int nents) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2069 | { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2070 | struct scatterlist *sg; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2071 | int count; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2072 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2073 | for_each_sg(sgl, sg, nents, count) |
| 2074 | __free_page(sg_page(sg)); |
| 2075 | |
| 2076 | kfree(sgl); |
| 2077 | } |
| 2078 | |
| 2079 | static inline void transport_free_pages(struct se_cmd *cmd) |
| 2080 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2081 | if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2082 | return; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2083 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2084 | transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents); |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2085 | cmd->t_data_sg = NULL; |
| 2086 | cmd->t_data_nents = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2087 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2088 | 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] | 2089 | cmd->t_bidi_data_sg = NULL; |
| 2090 | cmd->t_bidi_data_nents = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2091 | } |
| 2092 | |
Christoph Hellwig | d3df782 | 2011-09-13 23:08:32 +0200 | [diff] [blame] | 2093 | /** |
Christoph Hellwig | e26d99a | 2011-11-14 12:30:30 -0500 | [diff] [blame] | 2094 | * transport_release_cmd - free a command |
| 2095 | * @cmd: command to free |
| 2096 | * |
| 2097 | * This routine unconditionally frees a command, and reference counting |
| 2098 | * or list removal must be done in the caller. |
| 2099 | */ |
| 2100 | static void transport_release_cmd(struct se_cmd *cmd) |
| 2101 | { |
| 2102 | BUG_ON(!cmd->se_tfo); |
| 2103 | |
Andy Grover | c8e31f2 | 2012-01-19 13:39:17 -0800 | [diff] [blame] | 2104 | if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) |
Christoph Hellwig | e26d99a | 2011-11-14 12:30:30 -0500 | [diff] [blame] | 2105 | core_tmr_release_req(cmd->se_tmr_req); |
| 2106 | if (cmd->t_task_cdb != cmd->__t_task_cdb) |
| 2107 | kfree(cmd->t_task_cdb); |
| 2108 | /* |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2109 | * If this cmd has been setup with target_get_sess_cmd(), drop |
| 2110 | * the kref and call ->release_cmd() in kref callback. |
Christoph Hellwig | e26d99a | 2011-11-14 12:30:30 -0500 | [diff] [blame] | 2111 | */ |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2112 | if (cmd->check_release != 0) { |
| 2113 | target_put_sess_cmd(cmd->se_sess, cmd); |
| 2114 | return; |
| 2115 | } |
Christoph Hellwig | e26d99a | 2011-11-14 12:30:30 -0500 | [diff] [blame] | 2116 | cmd->se_tfo->release_cmd(cmd); |
| 2117 | } |
| 2118 | |
| 2119 | /** |
Christoph Hellwig | d3df782 | 2011-09-13 23:08:32 +0200 | [diff] [blame] | 2120 | * transport_put_cmd - release a reference to a command |
| 2121 | * @cmd: command to release |
| 2122 | * |
| 2123 | * This routine releases our reference to the command and frees it if possible. |
| 2124 | */ |
Nicholas Bellinger | 39c05f3 | 2011-10-08 13:59:52 -0700 | [diff] [blame] | 2125 | static void transport_put_cmd(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2126 | { |
| 2127 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2128 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2129 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Christoph Hellwig | 4911e3c | 2011-09-13 23:08:42 +0200 | [diff] [blame] | 2130 | if (atomic_read(&cmd->t_fe_count)) { |
| 2131 | if (!atomic_dec_and_test(&cmd->t_fe_count)) |
| 2132 | goto out_busy; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2133 | } |
Christoph Hellwig | 4911e3c | 2011-09-13 23:08:42 +0200 | [diff] [blame] | 2134 | |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2135 | if (cmd->transport_state & CMD_T_DEV_ACTIVE) { |
| 2136 | cmd->transport_state &= ~CMD_T_DEV_ACTIVE; |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 2137 | target_remove_from_state_list(cmd); |
Christoph Hellwig | 4911e3c | 2011-09-13 23:08:42 +0200 | [diff] [blame] | 2138 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2139 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2140 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2141 | transport_free_pages(cmd); |
Christoph Hellwig | 31afc39 | 2011-09-13 23:08:11 +0200 | [diff] [blame] | 2142 | transport_release_cmd(cmd); |
Nicholas Bellinger | 39c05f3 | 2011-10-08 13:59:52 -0700 | [diff] [blame] | 2143 | return; |
Christoph Hellwig | 4911e3c | 2011-09-13 23:08:42 +0200 | [diff] [blame] | 2144 | out_busy: |
| 2145 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2146 | } |
| 2147 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2148 | /* |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2149 | * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of |
| 2150 | * allocating in the core. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2151 | * @cmd: Associated se_cmd descriptor |
| 2152 | * @mem: SGL style memory for TCM WRITE / READ |
| 2153 | * @sg_mem_num: Number of SGL elements |
| 2154 | * @mem_bidi_in: SGL style memory for TCM BIDI READ |
| 2155 | * @sg_mem_bidi_num: Number of BIDI READ SGL elements |
| 2156 | * |
| 2157 | * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage |
| 2158 | * of parameters. |
| 2159 | */ |
| 2160 | int transport_generic_map_mem_to_cmd( |
| 2161 | struct se_cmd *cmd, |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2162 | struct scatterlist *sgl, |
| 2163 | u32 sgl_count, |
| 2164 | struct scatterlist *sgl_bidi, |
| 2165 | u32 sgl_bidi_count) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2166 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2167 | if (!sgl || !sgl_count) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2168 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2169 | |
Christoph Hellwig | 64f1db3 | 2012-05-20 11:59:11 -0400 | [diff] [blame] | 2170 | /* |
| 2171 | * Reject SCSI data overflow with map_mem_to_cmd() as incoming |
| 2172 | * scatterlists already have been set to follow what the fabric |
| 2173 | * passes for the original expected data transfer length. |
| 2174 | */ |
| 2175 | if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) { |
| 2176 | pr_warn("Rejecting SCSI DATA overflow for fabric using" |
| 2177 | " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n"); |
| 2178 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 2179 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; |
| 2180 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2181 | } |
| 2182 | |
Christoph Hellwig | 64f1db3 | 2012-05-20 11:59:11 -0400 | [diff] [blame] | 2183 | cmd->t_data_sg = sgl; |
| 2184 | cmd->t_data_nents = sgl_count; |
| 2185 | |
| 2186 | if (sgl_bidi && sgl_bidi_count) { |
| 2187 | cmd->t_bidi_data_sg = sgl_bidi; |
| 2188 | cmd->t_bidi_data_nents = sgl_bidi_count; |
| 2189 | } |
| 2190 | cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2191 | return 0; |
| 2192 | } |
| 2193 | EXPORT_SYMBOL(transport_generic_map_mem_to_cmd); |
| 2194 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2195 | void *transport_kmap_data_sg(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2196 | { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2197 | struct scatterlist *sg = cmd->t_data_sg; |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2198 | struct page **pages; |
| 2199 | int i; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2200 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2201 | BUG_ON(!sg); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2202 | /* |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2203 | * We need to take into account a possible offset here for fabrics like |
| 2204 | * tcm_loop who may be using a contig buffer from the SCSI midlayer for |
| 2205 | * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd() |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2206 | */ |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2207 | if (!cmd->t_data_nents) |
| 2208 | return NULL; |
| 2209 | else if (cmd->t_data_nents == 1) |
| 2210 | return kmap(sg_page(sg)) + sg->offset; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2211 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2212 | /* >1 page. use vmap */ |
| 2213 | pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL); |
| 2214 | if (!pages) |
| 2215 | return NULL; |
| 2216 | |
| 2217 | /* convert sg[] to pages[] */ |
| 2218 | for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) { |
| 2219 | pages[i] = sg_page(sg); |
| 2220 | } |
| 2221 | |
| 2222 | cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL); |
| 2223 | kfree(pages); |
| 2224 | if (!cmd->t_data_vmap) |
| 2225 | return NULL; |
| 2226 | |
| 2227 | return cmd->t_data_vmap + cmd->t_data_sg[0].offset; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2228 | } |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2229 | EXPORT_SYMBOL(transport_kmap_data_sg); |
| 2230 | |
| 2231 | void transport_kunmap_data_sg(struct se_cmd *cmd) |
| 2232 | { |
Andy Grover | a1edf9c | 2012-02-09 12:18:06 -0800 | [diff] [blame] | 2233 | if (!cmd->t_data_nents) { |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2234 | return; |
Andy Grover | a1edf9c | 2012-02-09 12:18:06 -0800 | [diff] [blame] | 2235 | } else if (cmd->t_data_nents == 1) { |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2236 | kunmap(sg_page(cmd->t_data_sg)); |
Andy Grover | a1edf9c | 2012-02-09 12:18:06 -0800 | [diff] [blame] | 2237 | return; |
| 2238 | } |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2239 | |
| 2240 | vunmap(cmd->t_data_vmap); |
| 2241 | cmd->t_data_vmap = NULL; |
| 2242 | } |
| 2243 | EXPORT_SYMBOL(transport_kunmap_data_sg); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2244 | |
| 2245 | static int |
| 2246 | transport_generic_get_mem(struct se_cmd *cmd) |
| 2247 | { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2248 | u32 length = cmd->data_length; |
| 2249 | unsigned int nents; |
| 2250 | struct page *page; |
roland@purestorage.com | 9db9da3 | 2012-01-04 15:59:58 -0800 | [diff] [blame] | 2251 | gfp_t zero_flag; |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2252 | int i = 0; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2253 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2254 | nents = DIV_ROUND_UP(length, PAGE_SIZE); |
| 2255 | cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL); |
| 2256 | if (!cmd->t_data_sg) |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2257 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2258 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2259 | cmd->t_data_nents = nents; |
| 2260 | sg_init_table(cmd->t_data_sg, nents); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2261 | |
Christoph Hellwig | 64f1db3 | 2012-05-20 11:59:11 -0400 | [diff] [blame] | 2262 | zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_CDB ? 0 : __GFP_ZERO; |
roland@purestorage.com | 9db9da3 | 2012-01-04 15:59:58 -0800 | [diff] [blame] | 2263 | |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2264 | while (length) { |
| 2265 | u32 page_len = min_t(u32, length, PAGE_SIZE); |
roland@purestorage.com | 9db9da3 | 2012-01-04 15:59:58 -0800 | [diff] [blame] | 2266 | page = alloc_page(GFP_KERNEL | zero_flag); |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2267 | if (!page) |
| 2268 | goto out; |
| 2269 | |
| 2270 | sg_set_page(&cmd->t_data_sg[i], page, page_len, 0); |
| 2271 | length -= page_len; |
| 2272 | i++; |
| 2273 | } |
| 2274 | return 0; |
| 2275 | |
| 2276 | out: |
Yi Zou | d0e27c8 | 2012-08-14 16:06:43 -0700 | [diff] [blame] | 2277 | while (i > 0) { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2278 | i--; |
Yi Zou | d0e27c8 | 2012-08-14 16:06:43 -0700 | [diff] [blame] | 2279 | __free_page(sg_page(&cmd->t_data_sg[i])); |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2280 | } |
| 2281 | kfree(cmd->t_data_sg); |
| 2282 | cmd->t_data_sg = NULL; |
| 2283 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2284 | } |
| 2285 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2286 | /* |
Andy Grover | b16a35b | 2012-04-03 15:51:21 -0700 | [diff] [blame] | 2287 | * Allocate any required resources to execute the command. For writes we |
| 2288 | * might not have the payload yet, so notify the fabric via a call to |
| 2289 | * ->write_pending instead. Otherwise place it on the execution queue. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2290 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2291 | int transport_generic_new_cmd(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2292 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2293 | int ret = 0; |
| 2294 | |
| 2295 | /* |
| 2296 | * Determine is the TCM fabric module has already allocated physical |
| 2297 | * memory, and is directly calling transport_generic_map_mem_to_cmd() |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2298 | * beforehand. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2299 | */ |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2300 | if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) && |
| 2301 | cmd->data_length) { |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2302 | ret = transport_generic_get_mem(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2303 | if (ret < 0) |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 2304 | goto out_fail; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2305 | } |
Nicholas Bellinger | 74f4cf2 | 2012-08-16 17:06:41 -0700 | [diff] [blame] | 2306 | /* |
| 2307 | * If this command doesn't have any payload and we don't have to call |
| 2308 | * into the fabric for data transfers, go ahead and complete it right |
| 2309 | * away. |
| 2310 | */ |
| 2311 | if (!cmd->data_length) { |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2312 | spin_lock_irq(&cmd->t_state_lock); |
Roland Dreier | 410f670 | 2011-11-22 13:51:32 -0800 | [diff] [blame] | 2313 | cmd->t_state = TRANSPORT_COMPLETE; |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2314 | cmd->transport_state |= CMD_T_ACTIVE; |
| 2315 | spin_unlock_irq(&cmd->t_state_lock); |
Nicholas Bellinger | 91ec1d3 | 2012-01-13 12:01:34 -0800 | [diff] [blame] | 2316 | |
| 2317 | if (cmd->t_task_cdb[0] == REQUEST_SENSE) { |
| 2318 | u8 ua_asc = 0, ua_ascq = 0; |
| 2319 | |
| 2320 | core_scsi3_ua_clear_for_request_sense(cmd, |
| 2321 | &ua_asc, &ua_ascq); |
| 2322 | } |
| 2323 | |
Roland Dreier | 410f670 | 2011-11-22 13:51:32 -0800 | [diff] [blame] | 2324 | INIT_WORK(&cmd->work, target_complete_ok_work); |
| 2325 | queue_work(target_completion_wq, &cmd->work); |
| 2326 | return 0; |
| 2327 | } |
Christoph Hellwig | da0f761 | 2011-10-18 06:57:01 -0400 | [diff] [blame] | 2328 | |
Christoph Hellwig | 4101f0a | 2012-04-24 00:25:03 -0400 | [diff] [blame] | 2329 | atomic_inc(&cmd->t_fe_count); |
Christoph Hellwig | 4101f0a | 2012-04-24 00:25:03 -0400 | [diff] [blame] | 2330 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2331 | /* |
Christoph Hellwig | c3196f0 | 2012-07-08 15:58:41 -0400 | [diff] [blame] | 2332 | * If this command is not a write we can execute it right here, |
| 2333 | * for write buffers we need to notify the fabric driver first |
| 2334 | * and let it call back once the write buffers are ready. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2335 | */ |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 2336 | target_add_to_state_list(cmd); |
Christoph Hellwig | c3196f0 | 2012-07-08 15:58:41 -0400 | [diff] [blame] | 2337 | if (cmd->data_direction != DMA_TO_DEVICE) { |
| 2338 | target_execute_cmd(cmd); |
| 2339 | return 0; |
| 2340 | } |
| 2341 | |
| 2342 | spin_lock_irq(&cmd->t_state_lock); |
| 2343 | cmd->t_state = TRANSPORT_WRITE_PENDING; |
| 2344 | spin_unlock_irq(&cmd->t_state_lock); |
| 2345 | |
| 2346 | transport_cmd_check_stop(cmd, false); |
| 2347 | |
| 2348 | ret = cmd->se_tfo->write_pending(cmd); |
| 2349 | if (ret == -EAGAIN || ret == -ENOMEM) |
| 2350 | goto queue_full; |
| 2351 | |
| 2352 | if (ret < 0) |
| 2353 | return ret; |
| 2354 | return 1; |
Christoph Hellwig | da0f761 | 2011-10-18 06:57:01 -0400 | [diff] [blame] | 2355 | |
| 2356 | out_fail: |
| 2357 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
| 2358 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 2359 | return -EINVAL; |
Christoph Hellwig | c3196f0 | 2012-07-08 15:58:41 -0400 | [diff] [blame] | 2360 | queue_full: |
| 2361 | pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd); |
| 2362 | cmd->t_state = TRANSPORT_COMPLETE_QF_WP; |
| 2363 | transport_handle_queue_full(cmd, cmd->se_dev); |
| 2364 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2365 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2366 | EXPORT_SYMBOL(transport_generic_new_cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2367 | |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 2368 | static void transport_write_pending_qf(struct se_cmd *cmd) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2369 | { |
Nicholas Bellinger | f147abb | 2011-10-25 23:57:41 -0700 | [diff] [blame] | 2370 | int ret; |
| 2371 | |
| 2372 | ret = cmd->se_tfo->write_pending(cmd); |
| 2373 | if (ret == -EAGAIN || ret == -ENOMEM) { |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 2374 | pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", |
| 2375 | cmd); |
| 2376 | transport_handle_queue_full(cmd, cmd->se_dev); |
| 2377 | } |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2378 | } |
| 2379 | |
Nicholas Bellinger | 39c05f3 | 2011-10-08 13:59:52 -0700 | [diff] [blame] | 2380 | 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] | 2381 | { |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2382 | if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) { |
Andy Grover | c8e31f2 | 2012-01-19 13:39:17 -0800 | [diff] [blame] | 2383 | if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2384 | transport_wait_for_tasks(cmd); |
| 2385 | |
Christoph Hellwig | 3546297 | 2011-05-31 23:56:57 -0400 | [diff] [blame] | 2386 | transport_release_cmd(cmd); |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2387 | } else { |
| 2388 | if (wait_for_tasks) |
| 2389 | transport_wait_for_tasks(cmd); |
| 2390 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2391 | core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd); |
| 2392 | |
Christoph Hellwig | 82f1c8a | 2011-09-13 23:09:01 +0200 | [diff] [blame] | 2393 | if (cmd->se_lun) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2394 | transport_lun_remove_cmd(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2395 | |
Nicholas Bellinger | 39c05f3 | 2011-10-08 13:59:52 -0700 | [diff] [blame] | 2396 | transport_put_cmd(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2397 | } |
| 2398 | } |
| 2399 | EXPORT_SYMBOL(transport_generic_free_cmd); |
| 2400 | |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2401 | /* target_get_sess_cmd - Add command to active ->sess_cmd_list |
| 2402 | * @se_sess: session to reference |
| 2403 | * @se_cmd: command descriptor to add |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 2404 | * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd() |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2405 | */ |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 2406 | static int target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd, |
| 2407 | bool ack_kref) |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2408 | { |
| 2409 | unsigned long flags; |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 2410 | int ret = 0; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2411 | |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2412 | kref_init(&se_cmd->cmd_kref); |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 2413 | /* |
| 2414 | * Add a second kref if the fabric caller is expecting to handle |
| 2415 | * fabric acknowledgement that requires two target_put_sess_cmd() |
| 2416 | * invocations before se_cmd descriptor release. |
| 2417 | */ |
Nicholas Bellinger | 8671556 | 2012-02-13 01:07:22 -0800 | [diff] [blame] | 2418 | if (ack_kref == true) { |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 2419 | kref_get(&se_cmd->cmd_kref); |
Nicholas Bellinger | 8671556 | 2012-02-13 01:07:22 -0800 | [diff] [blame] | 2420 | se_cmd->se_cmd_flags |= SCF_ACK_KREF; |
| 2421 | } |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2422 | |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2423 | spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 2424 | if (se_sess->sess_tearing_down) { |
| 2425 | ret = -ESHUTDOWN; |
| 2426 | goto out; |
| 2427 | } |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2428 | list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list); |
| 2429 | se_cmd->check_release = 1; |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 2430 | |
| 2431 | out: |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2432 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 2433 | return ret; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2434 | } |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2435 | |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2436 | static void target_release_cmd_kref(struct kref *kref) |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2437 | { |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2438 | struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref); |
| 2439 | struct se_session *se_sess = se_cmd->se_sess; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2440 | unsigned long flags; |
| 2441 | |
| 2442 | spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); |
| 2443 | if (list_empty(&se_cmd->se_cmd_list)) { |
| 2444 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
Nicholas Bellinger | ffc32d5 | 2012-02-13 02:35:01 -0800 | [diff] [blame] | 2445 | se_cmd->se_tfo->release_cmd(se_cmd); |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2446 | return; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2447 | } |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2448 | if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) { |
| 2449 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
| 2450 | complete(&se_cmd->cmd_wait_comp); |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2451 | return; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2452 | } |
| 2453 | list_del(&se_cmd->se_cmd_list); |
| 2454 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
| 2455 | |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2456 | se_cmd->se_tfo->release_cmd(se_cmd); |
| 2457 | } |
| 2458 | |
| 2459 | /* target_put_sess_cmd - Check for active I/O shutdown via kref_put |
| 2460 | * @se_sess: session to reference |
| 2461 | * @se_cmd: command descriptor to drop |
| 2462 | */ |
| 2463 | int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd) |
| 2464 | { |
| 2465 | return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2466 | } |
| 2467 | EXPORT_SYMBOL(target_put_sess_cmd); |
| 2468 | |
Roland Dreier | 1c7b13f | 2012-07-16 11:04:42 -0700 | [diff] [blame] | 2469 | /* target_sess_cmd_list_set_waiting - Flag all commands in |
| 2470 | * sess_cmd_list to complete cmd_wait_comp. Set |
| 2471 | * sess_tearing_down so no more commands are queued. |
| 2472 | * @se_sess: session to flag |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2473 | */ |
Roland Dreier | 1c7b13f | 2012-07-16 11:04:42 -0700 | [diff] [blame] | 2474 | void target_sess_cmd_list_set_waiting(struct se_session *se_sess) |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2475 | { |
| 2476 | struct se_cmd *se_cmd; |
| 2477 | unsigned long flags; |
| 2478 | |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2479 | spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); |
Roland Dreier | 1c7b13f | 2012-07-16 11:04:42 -0700 | [diff] [blame] | 2480 | |
| 2481 | WARN_ON(se_sess->sess_tearing_down); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2482 | se_sess->sess_tearing_down = 1; |
| 2483 | |
Roland Dreier | 1c7b13f | 2012-07-16 11:04:42 -0700 | [diff] [blame] | 2484 | list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2485 | se_cmd->cmd_wait_set = 1; |
| 2486 | |
| 2487 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
| 2488 | } |
Roland Dreier | 1c7b13f | 2012-07-16 11:04:42 -0700 | [diff] [blame] | 2489 | EXPORT_SYMBOL(target_sess_cmd_list_set_waiting); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2490 | |
| 2491 | /* target_wait_for_sess_cmds - Wait for outstanding descriptors |
| 2492 | * @se_sess: session to wait for active I/O |
| 2493 | * @wait_for_tasks: Make extra transport_wait_for_tasks call |
| 2494 | */ |
| 2495 | void target_wait_for_sess_cmds( |
| 2496 | struct se_session *se_sess, |
| 2497 | int wait_for_tasks) |
| 2498 | { |
| 2499 | struct se_cmd *se_cmd, *tmp_cmd; |
| 2500 | bool rc = false; |
| 2501 | |
| 2502 | list_for_each_entry_safe(se_cmd, tmp_cmd, |
Roland Dreier | 1c7b13f | 2012-07-16 11:04:42 -0700 | [diff] [blame] | 2503 | &se_sess->sess_cmd_list, se_cmd_list) { |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2504 | list_del(&se_cmd->se_cmd_list); |
| 2505 | |
| 2506 | pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:" |
| 2507 | " %d\n", se_cmd, se_cmd->t_state, |
| 2508 | se_cmd->se_tfo->get_cmd_state(se_cmd)); |
| 2509 | |
| 2510 | if (wait_for_tasks) { |
| 2511 | pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d," |
| 2512 | " fabric state: %d\n", se_cmd, se_cmd->t_state, |
| 2513 | se_cmd->se_tfo->get_cmd_state(se_cmd)); |
| 2514 | |
| 2515 | rc = transport_wait_for_tasks(se_cmd); |
| 2516 | |
| 2517 | pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d," |
| 2518 | " fabric state: %d\n", se_cmd, se_cmd->t_state, |
| 2519 | se_cmd->se_tfo->get_cmd_state(se_cmd)); |
| 2520 | } |
| 2521 | |
| 2522 | if (!rc) { |
| 2523 | wait_for_completion(&se_cmd->cmd_wait_comp); |
| 2524 | pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d" |
| 2525 | " fabric state: %d\n", se_cmd, se_cmd->t_state, |
| 2526 | se_cmd->se_tfo->get_cmd_state(se_cmd)); |
| 2527 | } |
| 2528 | |
| 2529 | se_cmd->se_tfo->release_cmd(se_cmd); |
| 2530 | } |
| 2531 | } |
| 2532 | EXPORT_SYMBOL(target_wait_for_sess_cmds); |
| 2533 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2534 | /* transport_lun_wait_for_tasks(): |
| 2535 | * |
| 2536 | * Called from ConfigFS context to stop the passed struct se_cmd to allow |
| 2537 | * an struct se_lun to be successfully shutdown. |
| 2538 | */ |
| 2539 | static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun) |
| 2540 | { |
| 2541 | unsigned long flags; |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 2542 | int ret = 0; |
| 2543 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2544 | /* |
| 2545 | * If the frontend has already requested this struct se_cmd to |
| 2546 | * be stopped, we can safely ignore this struct se_cmd. |
| 2547 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2548 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2549 | if (cmd->transport_state & CMD_T_STOP) { |
| 2550 | cmd->transport_state &= ~CMD_T_LUN_STOP; |
| 2551 | |
| 2552 | pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n", |
| 2553 | cmd->se_tfo->get_task_tag(cmd)); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2554 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 2555 | transport_cmd_check_stop(cmd, false); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2556 | return -EPERM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2557 | } |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2558 | cmd->transport_state |= CMD_T_LUN_FE_STOP; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2559 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2560 | |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 2561 | // XXX: audit task_flags checks. |
| 2562 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 2563 | if ((cmd->transport_state & CMD_T_BUSY) && |
| 2564 | (cmd->transport_state & CMD_T_SENT)) { |
| 2565 | if (!target_stop_cmd(cmd, &flags)) |
| 2566 | ret++; |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 2567 | } |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 2568 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2569 | |
Christoph Hellwig | 785fdf7 | 2012-04-24 00:25:04 -0400 | [diff] [blame] | 2570 | pr_debug("ConfigFS: cmd: %p stop tasks ret:" |
| 2571 | " %d\n", cmd, ret); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2572 | if (!ret) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2573 | pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2574 | cmd->se_tfo->get_task_tag(cmd)); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2575 | wait_for_completion(&cmd->transport_lun_stop_comp); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2576 | pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2577 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2578 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2579 | |
| 2580 | return 0; |
| 2581 | } |
| 2582 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2583 | static void __transport_clear_lun_from_sessions(struct se_lun *lun) |
| 2584 | { |
| 2585 | struct se_cmd *cmd = NULL; |
| 2586 | unsigned long lun_flags, cmd_flags; |
| 2587 | /* |
| 2588 | * Do exception processing and return CHECK_CONDITION status to the |
| 2589 | * Initiator Port. |
| 2590 | */ |
| 2591 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2592 | while (!list_empty(&lun->lun_cmd_list)) { |
| 2593 | cmd = list_first_entry(&lun->lun_cmd_list, |
| 2594 | struct se_cmd, se_lun_node); |
Christoph Hellwig | 3d26fea | 2011-12-21 14:14:05 -0500 | [diff] [blame] | 2595 | list_del_init(&cmd->se_lun_node); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2596 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2597 | spin_lock(&cmd->t_state_lock); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2598 | pr_debug("SE_LUN[%d] - Setting cmd->transport" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2599 | "_lun_stop for ITT: 0x%08x\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2600 | cmd->se_lun->unpacked_lun, |
| 2601 | cmd->se_tfo->get_task_tag(cmd)); |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2602 | cmd->transport_state |= CMD_T_LUN_STOP; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2603 | spin_unlock(&cmd->t_state_lock); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2604 | |
| 2605 | spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags); |
| 2606 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2607 | if (!cmd->se_lun) { |
| 2608 | pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2609 | cmd->se_tfo->get_task_tag(cmd), |
| 2610 | cmd->se_tfo->get_cmd_state(cmd), cmd->t_state); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2611 | BUG(); |
| 2612 | } |
| 2613 | /* |
| 2614 | * If the Storage engine still owns the iscsi_cmd_t, determine |
| 2615 | * and/or stop its context. |
| 2616 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2617 | pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2618 | "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun, |
| 2619 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2620 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2621 | if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2622 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); |
| 2623 | continue; |
| 2624 | } |
| 2625 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2626 | pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2627 | "_wait_for_tasks(): SUCCESS\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2628 | cmd->se_lun->unpacked_lun, |
| 2629 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2630 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2631 | spin_lock_irqsave(&cmd->t_state_lock, cmd_flags); |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2632 | if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2633 | spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2634 | goto check_cond; |
| 2635 | } |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2636 | cmd->transport_state &= ~CMD_T_DEV_ACTIVE; |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 2637 | target_remove_from_state_list(cmd); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2638 | spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2639 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2640 | /* |
| 2641 | * The Storage engine stopped this struct se_cmd before it was |
| 2642 | * send to the fabric frontend for delivery back to the |
| 2643 | * Initiator Node. Return this SCSI CDB back with an |
| 2644 | * CHECK_CONDITION status. |
| 2645 | */ |
| 2646 | check_cond: |
| 2647 | transport_send_check_condition_and_sense(cmd, |
| 2648 | TCM_NON_EXISTENT_LUN, 0); |
| 2649 | /* |
| 2650 | * If the fabric frontend is waiting for this iscsi_cmd_t to |
| 2651 | * be released, notify the waiting thread now that LU has |
| 2652 | * finished accessing it. |
| 2653 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2654 | spin_lock_irqsave(&cmd->t_state_lock, cmd_flags); |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2655 | if (cmd->transport_state & CMD_T_LUN_FE_STOP) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2656 | pr_debug("SE_LUN[%d] - Detected FE stop for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2657 | " struct se_cmd: %p ITT: 0x%08x\n", |
| 2658 | lun->unpacked_lun, |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2659 | cmd, cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2660 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2661 | spin_unlock_irqrestore(&cmd->t_state_lock, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2662 | cmd_flags); |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 2663 | transport_cmd_check_stop(cmd, false); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2664 | complete(&cmd->transport_lun_fe_stop_comp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2665 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); |
| 2666 | continue; |
| 2667 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2668 | pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2669 | lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2670 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2671 | spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2672 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); |
| 2673 | } |
| 2674 | spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags); |
| 2675 | } |
| 2676 | |
| 2677 | static int transport_clear_lun_thread(void *p) |
| 2678 | { |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 2679 | struct se_lun *lun = p; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2680 | |
| 2681 | __transport_clear_lun_from_sessions(lun); |
| 2682 | complete(&lun->lun_shutdown_comp); |
| 2683 | |
| 2684 | return 0; |
| 2685 | } |
| 2686 | |
| 2687 | int transport_clear_lun_from_sessions(struct se_lun *lun) |
| 2688 | { |
| 2689 | struct task_struct *kt; |
| 2690 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2691 | kt = kthread_run(transport_clear_lun_thread, lun, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2692 | "tcm_cl_%u", lun->unpacked_lun); |
| 2693 | if (IS_ERR(kt)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2694 | pr_err("Unable to start clear_lun thread\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2695 | return PTR_ERR(kt); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2696 | } |
| 2697 | wait_for_completion(&lun->lun_shutdown_comp); |
| 2698 | |
| 2699 | return 0; |
| 2700 | } |
| 2701 | |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2702 | /** |
| 2703 | * transport_wait_for_tasks - wait for completion to occur |
| 2704 | * @cmd: command to wait |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2705 | * |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2706 | * Called from frontend fabric context to wait for storage engine |
| 2707 | * to pause and/or release frontend generated struct se_cmd. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2708 | */ |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2709 | bool transport_wait_for_tasks(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2710 | { |
| 2711 | unsigned long flags; |
| 2712 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2713 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Andy Grover | c8e31f2 | 2012-01-19 13:39:17 -0800 | [diff] [blame] | 2714 | if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && |
| 2715 | !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) { |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2716 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2717 | return false; |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2718 | } |
Christoph Hellwig | cb4f4d3 | 2012-05-20 11:59:10 -0400 | [diff] [blame] | 2719 | |
Andy Grover | c8e31f2 | 2012-01-19 13:39:17 -0800 | [diff] [blame] | 2720 | if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && |
| 2721 | !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) { |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2722 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2723 | return false; |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2724 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2725 | /* |
| 2726 | * If we are already stopped due to an external event (ie: LUN shutdown) |
| 2727 | * sleep until the connection can have the passed struct se_cmd back. |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2728 | * The cmd->transport_lun_stopped_sem will be upped by |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2729 | * transport_clear_lun_from_sessions() once the ConfigFS context caller |
| 2730 | * has completed its operation on the struct se_cmd. |
| 2731 | */ |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2732 | if (cmd->transport_state & CMD_T_LUN_STOP) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2733 | pr_debug("wait_for_tasks: Stopping" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2734 | " wait_for_completion(&cmd->t_tasktransport_lun_fe" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2735 | "_stop_comp); for ITT: 0x%08x\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2736 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2737 | /* |
| 2738 | * There is a special case for WRITES where a FE exception + |
| 2739 | * LUN shutdown means ConfigFS context is still sleeping on |
| 2740 | * transport_lun_stop_comp in transport_lun_wait_for_tasks(). |
| 2741 | * We go ahead and up transport_lun_stop_comp just to be sure |
| 2742 | * here. |
| 2743 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2744 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 2745 | complete(&cmd->transport_lun_stop_comp); |
| 2746 | wait_for_completion(&cmd->transport_lun_fe_stop_comp); |
| 2747 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2748 | |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 2749 | target_remove_from_state_list(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2750 | /* |
| 2751 | * At this point, the frontend who was the originator of this |
| 2752 | * struct se_cmd, now owns the structure and can be released through |
| 2753 | * normal means below. |
| 2754 | */ |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2755 | pr_debug("wait_for_tasks: Stopped" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2756 | " wait_for_completion(&cmd->t_tasktransport_lun_fe_" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2757 | "stop_comp); for ITT: 0x%08x\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2758 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2759 | |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2760 | cmd->transport_state &= ~CMD_T_LUN_STOP; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2761 | } |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2762 | |
Nicholas Bellinger | 3d28934 | 2012-02-13 02:38:14 -0800 | [diff] [blame] | 2763 | if (!(cmd->transport_state & CMD_T_ACTIVE)) { |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2764 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2765 | return false; |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2766 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2767 | |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2768 | cmd->transport_state |= CMD_T_STOP; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2769 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2770 | pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x" |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2771 | " i_state: %d, t_state: %d, CMD_T_STOP\n", |
Christoph Hellwig | f2da9db | 2011-10-17 13:56:51 -0400 | [diff] [blame] | 2772 | cmd, cmd->se_tfo->get_task_tag(cmd), |
| 2773 | cmd->se_tfo->get_cmd_state(cmd), cmd->t_state); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2774 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2775 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2776 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2777 | wait_for_completion(&cmd->t_transport_stop_comp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2778 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2779 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 2780 | cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2781 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2782 | pr_debug("wait_for_tasks: Stopped wait_for_compltion(" |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2783 | "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2784 | cmd->se_tfo->get_task_tag(cmd)); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2785 | |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2786 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2787 | |
| 2788 | return true; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2789 | } |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2790 | EXPORT_SYMBOL(transport_wait_for_tasks); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2791 | |
| 2792 | static int transport_get_sense_codes( |
| 2793 | struct se_cmd *cmd, |
| 2794 | u8 *asc, |
| 2795 | u8 *ascq) |
| 2796 | { |
| 2797 | *asc = cmd->scsi_asc; |
| 2798 | *ascq = cmd->scsi_ascq; |
| 2799 | |
| 2800 | return 0; |
| 2801 | } |
| 2802 | |
| 2803 | static int transport_set_sense_codes( |
| 2804 | struct se_cmd *cmd, |
| 2805 | u8 asc, |
| 2806 | u8 ascq) |
| 2807 | { |
| 2808 | cmd->scsi_asc = asc; |
| 2809 | cmd->scsi_ascq = ascq; |
| 2810 | |
| 2811 | return 0; |
| 2812 | } |
| 2813 | |
| 2814 | int transport_send_check_condition_and_sense( |
| 2815 | struct se_cmd *cmd, |
| 2816 | u8 reason, |
| 2817 | int from_transport) |
| 2818 | { |
| 2819 | unsigned char *buffer = cmd->sense_buffer; |
| 2820 | unsigned long flags; |
| 2821 | int offset; |
| 2822 | u8 asc = 0, ascq = 0; |
| 2823 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2824 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2825 | if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2826 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2827 | return 0; |
| 2828 | } |
| 2829 | cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2830 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2831 | |
| 2832 | if (!reason && from_transport) |
| 2833 | goto after_reason; |
| 2834 | |
| 2835 | if (!from_transport) |
| 2836 | cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE; |
| 2837 | /* |
| 2838 | * Data Segment and SenseLength of the fabric response PDU. |
| 2839 | * |
| 2840 | * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE |
| 2841 | * from include/scsi/scsi_cmnd.h |
| 2842 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 2843 | offset = cmd->se_tfo->set_fabric_sense_len(cmd, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2844 | TRANSPORT_SENSE_BUFFER); |
| 2845 | /* |
| 2846 | * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses |
| 2847 | * SENSE KEY values from include/scsi/scsi.h |
| 2848 | */ |
| 2849 | switch (reason) { |
| 2850 | case TCM_NON_EXISTENT_LUN: |
Nicholas Bellinger | eb39d34 | 2011-07-26 16:59:00 -0700 | [diff] [blame] | 2851 | /* CURRENT ERROR */ |
| 2852 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2853 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | eb39d34 | 2011-07-26 16:59:00 -0700 | [diff] [blame] | 2854 | /* ILLEGAL REQUEST */ |
| 2855 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
| 2856 | /* LOGICAL UNIT NOT SUPPORTED */ |
| 2857 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25; |
| 2858 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2859 | case TCM_UNSUPPORTED_SCSI_OPCODE: |
| 2860 | case TCM_SECTOR_COUNT_TOO_MANY: |
| 2861 | /* CURRENT ERROR */ |
| 2862 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2863 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2864 | /* ILLEGAL REQUEST */ |
| 2865 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
| 2866 | /* INVALID COMMAND OPERATION CODE */ |
| 2867 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20; |
| 2868 | break; |
| 2869 | case TCM_UNKNOWN_MODE_PAGE: |
| 2870 | /* CURRENT ERROR */ |
| 2871 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2872 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2873 | /* ILLEGAL REQUEST */ |
| 2874 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
| 2875 | /* INVALID FIELD IN CDB */ |
| 2876 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24; |
| 2877 | break; |
| 2878 | case TCM_CHECK_CONDITION_ABORT_CMD: |
| 2879 | /* CURRENT ERROR */ |
| 2880 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2881 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2882 | /* ABORTED COMMAND */ |
| 2883 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 2884 | /* BUS DEVICE RESET FUNCTION OCCURRED */ |
| 2885 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29; |
| 2886 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03; |
| 2887 | break; |
| 2888 | case TCM_INCORRECT_AMOUNT_OF_DATA: |
| 2889 | /* CURRENT ERROR */ |
| 2890 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2891 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2892 | /* ABORTED COMMAND */ |
| 2893 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 2894 | /* WRITE ERROR */ |
| 2895 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c; |
| 2896 | /* NOT ENOUGH UNSOLICITED DATA */ |
| 2897 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d; |
| 2898 | break; |
| 2899 | case TCM_INVALID_CDB_FIELD: |
| 2900 | /* CURRENT ERROR */ |
| 2901 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2902 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Roland Dreier | 9fbc890 | 2012-01-09 17:54:00 -0800 | [diff] [blame] | 2903 | /* ILLEGAL REQUEST */ |
| 2904 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2905 | /* INVALID FIELD IN CDB */ |
| 2906 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24; |
| 2907 | break; |
| 2908 | case TCM_INVALID_PARAMETER_LIST: |
| 2909 | /* CURRENT ERROR */ |
| 2910 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2911 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Roland Dreier | 9fbc890 | 2012-01-09 17:54:00 -0800 | [diff] [blame] | 2912 | /* ILLEGAL REQUEST */ |
| 2913 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2914 | /* INVALID FIELD IN PARAMETER LIST */ |
| 2915 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26; |
| 2916 | break; |
| 2917 | case TCM_UNEXPECTED_UNSOLICITED_DATA: |
| 2918 | /* CURRENT ERROR */ |
| 2919 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2920 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2921 | /* ABORTED COMMAND */ |
| 2922 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 2923 | /* WRITE ERROR */ |
| 2924 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c; |
| 2925 | /* UNEXPECTED_UNSOLICITED_DATA */ |
| 2926 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c; |
| 2927 | break; |
| 2928 | case TCM_SERVICE_CRC_ERROR: |
| 2929 | /* CURRENT ERROR */ |
| 2930 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2931 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2932 | /* ABORTED COMMAND */ |
| 2933 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 2934 | /* PROTOCOL SERVICE CRC ERROR */ |
| 2935 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47; |
| 2936 | /* N/A */ |
| 2937 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05; |
| 2938 | break; |
| 2939 | case TCM_SNACK_REJECTED: |
| 2940 | /* CURRENT ERROR */ |
| 2941 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2942 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2943 | /* ABORTED COMMAND */ |
| 2944 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
| 2945 | /* READ ERROR */ |
| 2946 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11; |
| 2947 | /* FAILED RETRANSMISSION REQUEST */ |
| 2948 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13; |
| 2949 | break; |
| 2950 | case TCM_WRITE_PROTECTED: |
| 2951 | /* CURRENT ERROR */ |
| 2952 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2953 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2954 | /* DATA PROTECT */ |
| 2955 | buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT; |
| 2956 | /* WRITE PROTECTED */ |
| 2957 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27; |
| 2958 | break; |
Roland Dreier | e2397c7 | 2012-07-16 15:34:21 -0700 | [diff] [blame] | 2959 | case TCM_ADDRESS_OUT_OF_RANGE: |
| 2960 | /* CURRENT ERROR */ |
| 2961 | buffer[offset] = 0x70; |
| 2962 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
| 2963 | /* ILLEGAL REQUEST */ |
| 2964 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
| 2965 | /* LOGICAL BLOCK ADDRESS OUT OF RANGE */ |
| 2966 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x21; |
| 2967 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2968 | case TCM_CHECK_CONDITION_UNIT_ATTENTION: |
| 2969 | /* CURRENT ERROR */ |
| 2970 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2971 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2972 | /* UNIT ATTENTION */ |
| 2973 | buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION; |
| 2974 | core_scsi3_ua_for_check_condition(cmd, &asc, &ascq); |
| 2975 | buffer[offset+SPC_ASC_KEY_OFFSET] = asc; |
| 2976 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq; |
| 2977 | break; |
| 2978 | case TCM_CHECK_CONDITION_NOT_READY: |
| 2979 | /* CURRENT ERROR */ |
| 2980 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2981 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2982 | /* Not Ready */ |
| 2983 | buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY; |
| 2984 | transport_get_sense_codes(cmd, &asc, &ascq); |
| 2985 | buffer[offset+SPC_ASC_KEY_OFFSET] = asc; |
| 2986 | buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq; |
| 2987 | break; |
| 2988 | case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE: |
| 2989 | default: |
| 2990 | /* CURRENT ERROR */ |
| 2991 | buffer[offset] = 0x70; |
Roland Dreier | 895f302 | 2011-12-13 14:55:33 -0800 | [diff] [blame] | 2992 | buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2993 | /* ILLEGAL REQUEST */ |
| 2994 | buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
| 2995 | /* LOGICAL UNIT COMMUNICATION FAILURE */ |
| 2996 | buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80; |
| 2997 | break; |
| 2998 | } |
| 2999 | /* |
| 3000 | * This code uses linux/include/scsi/scsi.h SAM status codes! |
| 3001 | */ |
| 3002 | cmd->scsi_status = SAM_STAT_CHECK_CONDITION; |
| 3003 | /* |
| 3004 | * Automatically padded, this value is encoded in the fabric's |
| 3005 | * data_length response PDU containing the SCSI defined sense data. |
| 3006 | */ |
| 3007 | cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset; |
| 3008 | |
| 3009 | after_reason: |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3010 | return cmd->se_tfo->queue_status(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3011 | } |
| 3012 | EXPORT_SYMBOL(transport_send_check_condition_and_sense); |
| 3013 | |
| 3014 | int transport_check_aborted_status(struct se_cmd *cmd, int send_status) |
| 3015 | { |
| 3016 | int ret = 0; |
| 3017 | |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 3018 | if (cmd->transport_state & CMD_T_ABORTED) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3019 | if (!send_status || |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3020 | (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS)) |
| 3021 | return 1; |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3022 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3023 | pr_debug("Sending delayed SAM_STAT_TASK_ABORTED" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3024 | " status for CDB: 0x%02x ITT: 0x%08x\n", |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3025 | cmd->t_task_cdb[0], |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3026 | cmd->se_tfo->get_task_tag(cmd)); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3027 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3028 | cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3029 | cmd->se_tfo->queue_status(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3030 | ret = 1; |
| 3031 | } |
| 3032 | return ret; |
| 3033 | } |
| 3034 | EXPORT_SYMBOL(transport_check_aborted_status); |
| 3035 | |
| 3036 | void transport_send_task_abort(struct se_cmd *cmd) |
| 3037 | { |
Nicholas Bellinger | c252f00 | 2011-09-29 14:22:13 -0700 | [diff] [blame] | 3038 | unsigned long flags; |
| 3039 | |
| 3040 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 3041 | if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) { |
| 3042 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 3043 | return; |
| 3044 | } |
| 3045 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 3046 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3047 | /* |
| 3048 | * If there are still expected incoming fabric WRITEs, we wait |
| 3049 | * until until they have completed before sending a TASK_ABORTED |
| 3050 | * response. This response with TASK_ABORTED status will be |
| 3051 | * queued back to fabric module by transport_check_aborted_status(). |
| 3052 | */ |
| 3053 | if (cmd->data_direction == DMA_TO_DEVICE) { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3054 | if (cmd->se_tfo->write_pending_status(cmd) != 0) { |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 3055 | cmd->transport_state |= CMD_T_ABORTED; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3056 | smp_mb__after_atomic_inc(); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3057 | } |
| 3058 | } |
| 3059 | cmd->scsi_status = SAM_STAT_TASK_ABORTED; |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3060 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3061 | pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x," |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3062 | " ITT: 0x%08x\n", cmd->t_task_cdb[0], |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3063 | cmd->se_tfo->get_task_tag(cmd)); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3064 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3065 | cmd->se_tfo->queue_status(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3066 | } |
| 3067 | |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 3068 | static void target_tmr_work(struct work_struct *work) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3069 | { |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 3070 | struct se_cmd *cmd = container_of(work, struct se_cmd, work); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3071 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3072 | struct se_tmr_req *tmr = cmd->se_tmr_req; |
| 3073 | int ret; |
| 3074 | |
| 3075 | switch (tmr->function) { |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 3076 | case TMR_ABORT_TASK: |
Nicholas Bellinger | 3d28934 | 2012-02-13 02:38:14 -0800 | [diff] [blame] | 3077 | core_tmr_abort_task(dev, tmr, cmd->se_sess); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3078 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 3079 | case TMR_ABORT_TASK_SET: |
| 3080 | case TMR_CLEAR_ACA: |
| 3081 | case TMR_CLEAR_TASK_SET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3082 | tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED; |
| 3083 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 3084 | case TMR_LUN_RESET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3085 | ret = core_tmr_lun_reset(dev, tmr, NULL, NULL); |
| 3086 | tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE : |
| 3087 | TMR_FUNCTION_REJECTED; |
| 3088 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 3089 | case TMR_TARGET_WARM_RESET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3090 | tmr->response = TMR_FUNCTION_REJECTED; |
| 3091 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 3092 | case TMR_TARGET_COLD_RESET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3093 | tmr->response = TMR_FUNCTION_REJECTED; |
| 3094 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3095 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 3096 | pr_err("Uknown TMR function: 0x%02x.\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3097 | tmr->function); |
| 3098 | tmr->response = TMR_FUNCTION_REJECTED; |
| 3099 | break; |
| 3100 | } |
| 3101 | |
| 3102 | cmd->t_state = TRANSPORT_ISTATE_PROCESSING; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3103 | cmd->se_tfo->queue_tm_rsp(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3104 | |
Christoph Hellwig | b7b8bef | 2011-10-17 13:56:44 -0400 | [diff] [blame] | 3105 | transport_cmd_check_stop_to_fabric(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3106 | } |
| 3107 | |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 3108 | int transport_generic_handle_tmr( |
| 3109 | struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3110 | { |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 3111 | INIT_WORK(&cmd->work, target_tmr_work); |
| 3112 | queue_work(cmd->se_dev->tmr_wq, &cmd->work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3113 | return 0; |
| 3114 | } |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 3115 | EXPORT_SYMBOL(transport_generic_handle_tmr); |