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