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