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 | * |
Nicholas Bellinger | 4c76251 | 2013-09-05 15:29:12 -0700 | [diff] [blame] | 6 | * (c) Copyright 2002-2013 Datera, Inc. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 7 | * |
| 8 | * Nicholas A. Bellinger <nab@kernel.org> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 23 | * |
| 24 | ******************************************************************************/ |
| 25 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 26 | #include <linux/net.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/string.h> |
| 29 | #include <linux/timer.h> |
| 30 | #include <linux/slab.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 31 | #include <linux/spinlock.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 32 | #include <linux/kthread.h> |
| 33 | #include <linux/in.h> |
| 34 | #include <linux/cdrom.h> |
Paul Gortmaker | 827509e | 2011-08-30 14:20:44 -0400 | [diff] [blame] | 35 | #include <linux/module.h> |
Roland Dreier | 015487b | 2012-02-13 16:18:17 -0800 | [diff] [blame] | 36 | #include <linux/ratelimit.h> |
David S. Miller | 5538d29 | 2015-05-28 11:35:41 -0700 | [diff] [blame] | 37 | #include <linux/vmalloc.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 38 | #include <asm/unaligned.h> |
| 39 | #include <net/sock.h> |
| 40 | #include <net/tcp.h> |
Bart Van Assche | ba92999 | 2015-05-08 10:11:12 +0200 | [diff] [blame] | 41 | #include <scsi/scsi_proto.h> |
Sagi Grimberg | 9ec1e1c | 2015-07-08 17:58:53 +0300 | [diff] [blame] | 42 | #include <scsi/scsi_common.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 43 | |
| 44 | #include <target/target_core_base.h> |
Christoph Hellwig | c4795fb | 2011-11-16 09:46:48 -0500 | [diff] [blame] | 45 | #include <target/target_core_backend.h> |
| 46 | #include <target/target_core_fabric.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 47 | |
Christoph Hellwig | e26d99a | 2011-11-14 12:30:30 -0500 | [diff] [blame] | 48 | #include "target_core_internal.h" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 49 | #include "target_core_alua.h" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 50 | #include "target_core_pr.h" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 51 | #include "target_core_ua.h" |
| 52 | |
Roland Dreier | e5c0d6a | 2013-06-26 17:36:17 -0700 | [diff] [blame] | 53 | #define CREATE_TRACE_POINTS |
| 54 | #include <trace/events/target.h> |
| 55 | |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 56 | static struct workqueue_struct *target_completion_wq; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 57 | static struct kmem_cache *se_sess_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 58 | struct kmem_cache *se_ua_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 59 | struct kmem_cache *t10_pr_reg_cache; |
| 60 | struct kmem_cache *t10_alua_lu_gp_cache; |
| 61 | struct kmem_cache *t10_alua_lu_gp_mem_cache; |
| 62 | struct kmem_cache *t10_alua_tg_pt_gp_cache; |
Hannes Reinecke | 229d4f1 | 2013-12-17 09:18:50 +0100 | [diff] [blame] | 63 | struct kmem_cache *t10_alua_lba_map_cache; |
| 64 | struct kmem_cache *t10_alua_lba_map_mem_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 65 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 66 | static void transport_complete_task_attr(struct se_cmd *cmd); |
Bart Van Assche | 17e391d | 2018-06-22 14:53:05 -0700 | [diff] [blame] | 67 | static void translate_sense_reason(struct se_cmd *cmd, sense_reason_t reason); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 68 | static void transport_handle_queue_full(struct se_cmd *cmd, |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 69 | struct se_device *dev, int err, bool write_pending); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 70 | static void target_complete_ok_work(struct work_struct *work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 71 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 72 | int init_se_kmem_caches(void) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 73 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 74 | se_sess_cache = kmem_cache_create("se_sess_cache", |
| 75 | sizeof(struct se_session), __alignof__(struct se_session), |
| 76 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 77 | if (!se_sess_cache) { |
| 78 | pr_err("kmem_cache_create() for struct se_session" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 79 | " failed\n"); |
Andy Grover | c8e31f2 | 2012-01-19 13:39:17 -0800 | [diff] [blame] | 80 | goto out; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 81 | } |
| 82 | se_ua_cache = kmem_cache_create("se_ua_cache", |
| 83 | sizeof(struct se_ua), __alignof__(struct se_ua), |
| 84 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 85 | if (!se_ua_cache) { |
| 86 | pr_err("kmem_cache_create() for struct se_ua failed\n"); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 87 | goto out_free_sess_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 88 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 89 | t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache", |
| 90 | sizeof(struct t10_pr_registration), |
| 91 | __alignof__(struct t10_pr_registration), 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 92 | if (!t10_pr_reg_cache) { |
| 93 | pr_err("kmem_cache_create() for struct t10_pr_registration" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 94 | " failed\n"); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 95 | goto out_free_ua_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 96 | } |
| 97 | t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache", |
| 98 | sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp), |
| 99 | 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 100 | if (!t10_alua_lu_gp_cache) { |
| 101 | pr_err("kmem_cache_create() for t10_alua_lu_gp_cache" |
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_pr_reg_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 104 | } |
| 105 | t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache", |
| 106 | sizeof(struct t10_alua_lu_gp_member), |
| 107 | __alignof__(struct t10_alua_lu_gp_member), 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 108 | if (!t10_alua_lu_gp_mem_cache) { |
| 109 | pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 110 | "cache failed\n"); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 111 | goto out_free_lu_gp_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 112 | } |
| 113 | t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache", |
| 114 | sizeof(struct t10_alua_tg_pt_gp), |
| 115 | __alignof__(struct t10_alua_tg_pt_gp), 0, NULL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 116 | if (!t10_alua_tg_pt_gp_cache) { |
| 117 | pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_" |
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_mem_cache; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 120 | } |
Hannes Reinecke | 229d4f1 | 2013-12-17 09:18:50 +0100 | [diff] [blame] | 121 | t10_alua_lba_map_cache = kmem_cache_create( |
| 122 | "t10_alua_lba_map_cache", |
| 123 | sizeof(struct t10_alua_lba_map), |
| 124 | __alignof__(struct t10_alua_lba_map), 0, NULL); |
| 125 | if (!t10_alua_lba_map_cache) { |
| 126 | pr_err("kmem_cache_create() for t10_alua_lba_map_" |
| 127 | "cache failed\n"); |
Christoph Hellwig | adf653f | 2015-05-25 21:33:08 -0700 | [diff] [blame] | 128 | goto out_free_tg_pt_gp_cache; |
Hannes Reinecke | 229d4f1 | 2013-12-17 09:18:50 +0100 | [diff] [blame] | 129 | } |
| 130 | t10_alua_lba_map_mem_cache = kmem_cache_create( |
| 131 | "t10_alua_lba_map_mem_cache", |
| 132 | sizeof(struct t10_alua_lba_map_member), |
| 133 | __alignof__(struct t10_alua_lba_map_member), 0, NULL); |
| 134 | if (!t10_alua_lba_map_mem_cache) { |
| 135 | pr_err("kmem_cache_create() for t10_alua_lba_map_mem_" |
| 136 | "cache failed\n"); |
| 137 | goto out_free_lba_map_cache; |
| 138 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 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) |
Hannes Reinecke | 229d4f1 | 2013-12-17 09:18:50 +0100 | [diff] [blame] | 143 | goto out_free_lba_map_mem_cache; |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 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 | |
Hannes Reinecke | 229d4f1 | 2013-12-17 09:18:50 +0100 | [diff] [blame] | 147 | out_free_lba_map_mem_cache: |
| 148 | kmem_cache_destroy(t10_alua_lba_map_mem_cache); |
| 149 | out_free_lba_map_cache: |
| 150 | kmem_cache_destroy(t10_alua_lba_map_cache); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 151 | out_free_tg_pt_gp_cache: |
| 152 | kmem_cache_destroy(t10_alua_tg_pt_gp_cache); |
| 153 | out_free_lu_gp_mem_cache: |
| 154 | kmem_cache_destroy(t10_alua_lu_gp_mem_cache); |
| 155 | out_free_lu_gp_cache: |
| 156 | kmem_cache_destroy(t10_alua_lu_gp_cache); |
| 157 | out_free_pr_reg_cache: |
| 158 | kmem_cache_destroy(t10_pr_reg_cache); |
| 159 | out_free_ua_cache: |
| 160 | kmem_cache_destroy(se_ua_cache); |
| 161 | out_free_sess_cache: |
| 162 | kmem_cache_destroy(se_sess_cache); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 163 | out: |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 164 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 167 | void release_se_kmem_caches(void) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 168 | { |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 169 | destroy_workqueue(target_completion_wq); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 170 | kmem_cache_destroy(se_sess_cache); |
| 171 | kmem_cache_destroy(se_ua_cache); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 172 | kmem_cache_destroy(t10_pr_reg_cache); |
| 173 | kmem_cache_destroy(t10_alua_lu_gp_cache); |
| 174 | kmem_cache_destroy(t10_alua_lu_gp_mem_cache); |
| 175 | kmem_cache_destroy(t10_alua_tg_pt_gp_cache); |
Hannes Reinecke | 229d4f1 | 2013-12-17 09:18:50 +0100 | [diff] [blame] | 176 | kmem_cache_destroy(t10_alua_lba_map_cache); |
| 177 | kmem_cache_destroy(t10_alua_lba_map_mem_cache); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 180 | /* This code ensures unique mib indexes are handed out. */ |
| 181 | static DEFINE_SPINLOCK(scsi_mib_index_lock); |
| 182 | static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX]; |
Nicholas Bellinger | e89d15e | 2011-02-09 15:35:03 -0800 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * Allocate a new row index for the entry type specified |
| 186 | */ |
| 187 | u32 scsi_get_new_index(scsi_index_t type) |
| 188 | { |
| 189 | u32 new_index; |
| 190 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 191 | BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX)); |
Nicholas Bellinger | e89d15e | 2011-02-09 15:35:03 -0800 | [diff] [blame] | 192 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 193 | spin_lock(&scsi_mib_index_lock); |
| 194 | new_index = ++scsi_mib_index[type]; |
| 195 | spin_unlock(&scsi_mib_index_lock); |
Nicholas Bellinger | e89d15e | 2011-02-09 15:35:03 -0800 | [diff] [blame] | 196 | |
| 197 | return new_index; |
| 198 | } |
| 199 | |
Nicholas Bellinger | dbc5623 | 2011-10-22 01:03:54 -0700 | [diff] [blame] | 200 | void transport_subsystem_check_init(void) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 201 | { |
| 202 | int ret; |
Andy Grover | 283669d | 2012-07-30 15:54:17 -0700 | [diff] [blame] | 203 | static int sub_api_initialized; |
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 | if (sub_api_initialized) |
| 206 | return; |
| 207 | |
Roland Dreier | 6110f37 | 2018-10-20 09:45:16 -0700 | [diff] [blame] | 208 | ret = IS_ENABLED(CONFIG_TCM_IBLOCK) && request_module("target_core_iblock"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 209 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 210 | pr_err("Unable to load target_core_iblock\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 211 | |
Roland Dreier | 6110f37 | 2018-10-20 09:45:16 -0700 | [diff] [blame] | 212 | ret = IS_ENABLED(CONFIG_TCM_FILEIO) && request_module("target_core_file"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 213 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 214 | pr_err("Unable to load target_core_file\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 215 | |
Roland Dreier | 6110f37 | 2018-10-20 09:45:16 -0700 | [diff] [blame] | 216 | ret = IS_ENABLED(CONFIG_TCM_PSCSI) && request_module("target_core_pscsi"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 217 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 218 | pr_err("Unable to load target_core_pscsi\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 219 | |
Roland Dreier | 6110f37 | 2018-10-20 09:45:16 -0700 | [diff] [blame] | 220 | ret = IS_ENABLED(CONFIG_TCM_USER2) && request_module("target_core_user"); |
Andy Grover | 7c9e7a6 | 2014-10-01 16:07:05 -0700 | [diff] [blame] | 221 | if (ret != 0) |
| 222 | pr_err("Unable to load target_core_user\n"); |
| 223 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 224 | sub_api_initialized = 1; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Bart Van Assche | 317f897 | 2018-06-22 14:52:51 -0700 | [diff] [blame] | 227 | /** |
Bart Van Assche | d1bff07 | 2018-06-22 14:52:52 -0700 | [diff] [blame] | 228 | * transport_init_session - initialize a session object |
| 229 | * @se_sess: Session object pointer. |
| 230 | * |
| 231 | * The caller must have zero-initialized @se_sess before calling this function. |
| 232 | */ |
| 233 | void transport_init_session(struct se_session *se_sess) |
| 234 | { |
| 235 | INIT_LIST_HEAD(&se_sess->sess_list); |
| 236 | INIT_LIST_HEAD(&se_sess->sess_acl_list); |
| 237 | INIT_LIST_HEAD(&se_sess->sess_cmd_list); |
Bart Van Assche | d1bff07 | 2018-06-22 14:52:52 -0700 | [diff] [blame] | 238 | spin_lock_init(&se_sess->sess_cmd_lock); |
Bart Van Assche | 00d909a | 2018-06-22 14:52:53 -0700 | [diff] [blame] | 239 | init_waitqueue_head(&se_sess->cmd_list_wq); |
Bart Van Assche | d1bff07 | 2018-06-22 14:52:52 -0700 | [diff] [blame] | 240 | } |
| 241 | EXPORT_SYMBOL(transport_init_session); |
| 242 | |
| 243 | /** |
Bart Van Assche | 317f897 | 2018-06-22 14:52:51 -0700 | [diff] [blame] | 244 | * transport_alloc_session - allocate a session object and initialize it |
| 245 | * @sup_prot_ops: bitmask that defines which T10-PI modes are supported. |
| 246 | */ |
| 247 | struct se_session *transport_alloc_session(enum target_prot_op sup_prot_ops) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 248 | { |
| 249 | struct se_session *se_sess; |
| 250 | |
| 251 | se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 252 | if (!se_sess) { |
| 253 | pr_err("Unable to allocate struct se_session from" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 254 | " se_sess_cache\n"); |
| 255 | return ERR_PTR(-ENOMEM); |
| 256 | } |
Bart Van Assche | d1bff07 | 2018-06-22 14:52:52 -0700 | [diff] [blame] | 257 | transport_init_session(se_sess); |
Nicholas Bellinger | e70beee | 2014-04-02 12:52:38 -0700 | [diff] [blame] | 258 | se_sess->sup_prot_ops = sup_prot_ops; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 259 | |
| 260 | return se_sess; |
| 261 | } |
Bart Van Assche | 317f897 | 2018-06-22 14:52:51 -0700 | [diff] [blame] | 262 | EXPORT_SYMBOL(transport_alloc_session); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 263 | |
Bart Van Assche | 317f897 | 2018-06-22 14:52:51 -0700 | [diff] [blame] | 264 | /** |
| 265 | * transport_alloc_session_tags - allocate target driver private data |
| 266 | * @se_sess: Session pointer. |
| 267 | * @tag_num: Maximum number of in-flight commands between initiator and target. |
| 268 | * @tag_size: Size in bytes of the private data a target driver associates with |
| 269 | * each command. |
| 270 | */ |
Nicholas Bellinger | c0add7f | 2013-06-07 17:38:58 -0700 | [diff] [blame] | 271 | int transport_alloc_session_tags(struct se_session *se_sess, |
| 272 | unsigned int tag_num, unsigned int tag_size) |
| 273 | { |
| 274 | int rc; |
| 275 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 276 | se_sess->sess_cmd_map = kcalloc(tag_size, tag_num, |
Michal Hocko | dcda9b0 | 2017-07-12 14:36:45 -0700 | [diff] [blame] | 277 | GFP_KERNEL | __GFP_NOWARN | __GFP_RETRY_MAYFAIL); |
Nicholas Bellinger | c0add7f | 2013-06-07 17:38:58 -0700 | [diff] [blame] | 278 | if (!se_sess->sess_cmd_map) { |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 279 | se_sess->sess_cmd_map = vzalloc(array_size(tag_size, tag_num)); |
Nicholas Bellinger | 8c7f6e9 | 2013-09-23 11:57:38 -0700 | [diff] [blame] | 280 | if (!se_sess->sess_cmd_map) { |
| 281 | pr_err("Unable to allocate se_sess->sess_cmd_map\n"); |
| 282 | return -ENOMEM; |
| 283 | } |
Nicholas Bellinger | c0add7f | 2013-06-07 17:38:58 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Matthew Wilcox | 10e9cbb | 2018-06-12 12:05:44 -0700 | [diff] [blame] | 286 | rc = sbitmap_queue_init_node(&se_sess->sess_tag_pool, tag_num, -1, |
| 287 | false, GFP_KERNEL, NUMA_NO_NODE); |
Nicholas Bellinger | c0add7f | 2013-06-07 17:38:58 -0700 | [diff] [blame] | 288 | if (rc < 0) { |
| 289 | pr_err("Unable to init se_sess->sess_tag_pool," |
| 290 | " tag_num: %u\n", tag_num); |
Pekka Enberg | de64d3a | 2015-06-30 14:59:24 -0700 | [diff] [blame] | 291 | kvfree(se_sess->sess_cmd_map); |
Nicholas Bellinger | c0add7f | 2013-06-07 17:38:58 -0700 | [diff] [blame] | 292 | se_sess->sess_cmd_map = NULL; |
| 293 | return -ENOMEM; |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | EXPORT_SYMBOL(transport_alloc_session_tags); |
| 299 | |
Bart Van Assche | 317f897 | 2018-06-22 14:52:51 -0700 | [diff] [blame] | 300 | /** |
| 301 | * transport_init_session_tags - allocate a session and target driver private data |
| 302 | * @tag_num: Maximum number of in-flight commands between initiator and target. |
| 303 | * @tag_size: Size in bytes of the private data a target driver associates with |
| 304 | * each command. |
| 305 | * @sup_prot_ops: bitmask that defines which T10-PI modes are supported. |
| 306 | */ |
Mike Christie | 3cd1428 | 2018-08-02 12:12:22 -0500 | [diff] [blame] | 307 | static struct se_session * |
| 308 | transport_init_session_tags(unsigned int tag_num, unsigned int tag_size, |
| 309 | enum target_prot_op sup_prot_ops) |
Nicholas Bellinger | c0add7f | 2013-06-07 17:38:58 -0700 | [diff] [blame] | 310 | { |
| 311 | struct se_session *se_sess; |
| 312 | int rc; |
| 313 | |
Nicholas Bellinger | 7861728 | 2016-01-09 05:29:24 -0800 | [diff] [blame] | 314 | if (tag_num != 0 && !tag_size) { |
| 315 | pr_err("init_session_tags called with percpu-ida tag_num:" |
| 316 | " %u, but zero tag_size\n", tag_num); |
| 317 | return ERR_PTR(-EINVAL); |
| 318 | } |
| 319 | if (!tag_num && tag_size) { |
| 320 | pr_err("init_session_tags called with percpu-ida tag_size:" |
| 321 | " %u, but zero tag_num\n", tag_size); |
| 322 | return ERR_PTR(-EINVAL); |
| 323 | } |
| 324 | |
Bart Van Assche | 317f897 | 2018-06-22 14:52:51 -0700 | [diff] [blame] | 325 | se_sess = transport_alloc_session(sup_prot_ops); |
Nicholas Bellinger | c0add7f | 2013-06-07 17:38:58 -0700 | [diff] [blame] | 326 | if (IS_ERR(se_sess)) |
| 327 | return se_sess; |
| 328 | |
| 329 | rc = transport_alloc_session_tags(se_sess, tag_num, tag_size); |
| 330 | if (rc < 0) { |
| 331 | transport_free_session(se_sess); |
| 332 | return ERR_PTR(-ENOMEM); |
| 333 | } |
| 334 | |
| 335 | return se_sess; |
| 336 | } |
Nicholas Bellinger | c0add7f | 2013-06-07 17:38:58 -0700 | [diff] [blame] | 337 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 338 | /* |
Nicholas Bellinger | 140854c | 2011-08-31 12:34:39 -0700 | [diff] [blame] | 339 | * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 340 | */ |
| 341 | void __transport_register_session( |
| 342 | struct se_portal_group *se_tpg, |
| 343 | struct se_node_acl *se_nacl, |
| 344 | struct se_session *se_sess, |
| 345 | void *fabric_sess_ptr) |
| 346 | { |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 347 | const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 348 | unsigned char buf[PR_REG_ISID_LEN]; |
Mike Christie | 6a64f6e | 2018-08-02 12:12:20 -0500 | [diff] [blame] | 349 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 350 | |
| 351 | se_sess->se_tpg = se_tpg; |
| 352 | se_sess->fabric_sess_ptr = fabric_sess_ptr; |
| 353 | /* |
| 354 | * Used by struct se_node_acl's under ConfigFS to locate active se_session-t |
| 355 | * |
| 356 | * Only set for struct se_session's that will actually be moving I/O. |
| 357 | * eg: *NOT* discovery sessions. |
| 358 | */ |
| 359 | if (se_nacl) { |
| 360 | /* |
Nicholas Bellinger | bffb512 | 2015-04-14 11:52:22 -0700 | [diff] [blame] | 361 | * |
| 362 | * Determine if fabric allows for T10-PI feature bits exposed to |
| 363 | * initiators for device backends with !dev->dev_attrib.pi_prot_type. |
| 364 | * |
| 365 | * If so, then always save prot_type on a per se_node_acl node |
| 366 | * basis and re-instate the previous sess_prot_type to avoid |
| 367 | * disabling PI from below any previously initiator side |
| 368 | * registered LUNs. |
| 369 | */ |
| 370 | if (se_nacl->saved_prot_type) |
| 371 | se_sess->sess_prot_type = se_nacl->saved_prot_type; |
| 372 | else if (tfo->tpg_check_prot_fabric_only) |
| 373 | se_sess->sess_prot_type = se_nacl->saved_prot_type = |
| 374 | tfo->tpg_check_prot_fabric_only(se_tpg); |
| 375 | /* |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 376 | * If the fabric module supports an ISID based TransportID, |
| 377 | * save this value in binary from the fabric I_T Nexus now. |
| 378 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 379 | if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 380 | memset(&buf[0], 0, PR_REG_ISID_LEN); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 381 | se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 382 | &buf[0], PR_REG_ISID_LEN); |
| 383 | se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]); |
| 384 | } |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 385 | |
Mike Christie | 6a64f6e | 2018-08-02 12:12:20 -0500 | [diff] [blame] | 386 | spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 387 | /* |
| 388 | * The se_nacl->nacl_sess pointer will be set to the |
| 389 | * last active I_T Nexus for each struct se_node_acl. |
| 390 | */ |
| 391 | se_nacl->nacl_sess = se_sess; |
| 392 | |
| 393 | list_add_tail(&se_sess->sess_acl_list, |
| 394 | &se_nacl->acl_sess_list); |
Mike Christie | 6a64f6e | 2018-08-02 12:12:20 -0500 | [diff] [blame] | 395 | spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 396 | } |
| 397 | list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list); |
| 398 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 399 | pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 400 | 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] | 401 | } |
| 402 | EXPORT_SYMBOL(__transport_register_session); |
| 403 | |
| 404 | void transport_register_session( |
| 405 | struct se_portal_group *se_tpg, |
| 406 | struct se_node_acl *se_nacl, |
| 407 | struct se_session *se_sess, |
| 408 | void *fabric_sess_ptr) |
| 409 | { |
Nicholas Bellinger | 140854c | 2011-08-31 12:34:39 -0700 | [diff] [blame] | 410 | unsigned long flags; |
| 411 | |
| 412 | spin_lock_irqsave(&se_tpg->session_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 413 | __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr); |
Nicholas Bellinger | 140854c | 2011-08-31 12:34:39 -0700 | [diff] [blame] | 414 | spin_unlock_irqrestore(&se_tpg->session_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 415 | } |
| 416 | EXPORT_SYMBOL(transport_register_session); |
| 417 | |
Nicholas Bellinger | 7861728 | 2016-01-09 05:29:24 -0800 | [diff] [blame] | 418 | struct se_session * |
Mike Christie | fa83428 | 2018-08-02 12:12:23 -0500 | [diff] [blame] | 419 | target_setup_session(struct se_portal_group *tpg, |
Nicholas Bellinger | 7861728 | 2016-01-09 05:29:24 -0800 | [diff] [blame] | 420 | unsigned int tag_num, unsigned int tag_size, |
| 421 | enum target_prot_op prot_op, |
| 422 | const char *initiatorname, void *private, |
| 423 | int (*callback)(struct se_portal_group *, |
| 424 | struct se_session *, void *)) |
| 425 | { |
| 426 | struct se_session *sess; |
| 427 | |
| 428 | /* |
| 429 | * If the fabric driver is using percpu-ida based pre allocation |
| 430 | * of I/O descriptor tags, go ahead and perform that setup now.. |
| 431 | */ |
| 432 | if (tag_num != 0) |
| 433 | sess = transport_init_session_tags(tag_num, tag_size, prot_op); |
| 434 | else |
Bart Van Assche | 317f897 | 2018-06-22 14:52:51 -0700 | [diff] [blame] | 435 | sess = transport_alloc_session(prot_op); |
Nicholas Bellinger | 7861728 | 2016-01-09 05:29:24 -0800 | [diff] [blame] | 436 | |
| 437 | if (IS_ERR(sess)) |
| 438 | return sess; |
| 439 | |
| 440 | sess->se_node_acl = core_tpg_check_initiator_node_acl(tpg, |
| 441 | (unsigned char *)initiatorname); |
| 442 | if (!sess->se_node_acl) { |
| 443 | transport_free_session(sess); |
| 444 | return ERR_PTR(-EACCES); |
| 445 | } |
| 446 | /* |
| 447 | * Go ahead and perform any remaining fabric setup that is |
| 448 | * required before transport_register_session(). |
| 449 | */ |
| 450 | if (callback != NULL) { |
| 451 | int rc = callback(tpg, sess, private); |
| 452 | if (rc) { |
| 453 | transport_free_session(sess); |
| 454 | return ERR_PTR(rc); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | transport_register_session(tpg, sess->se_node_acl, sess, private); |
| 459 | return sess; |
| 460 | } |
Mike Christie | fa83428 | 2018-08-02 12:12:23 -0500 | [diff] [blame] | 461 | EXPORT_SYMBOL(target_setup_session); |
Nicholas Bellinger | 7861728 | 2016-01-09 05:29:24 -0800 | [diff] [blame] | 462 | |
Nicholas Bellinger | f8e471f | 2015-03-06 20:34:32 -0800 | [diff] [blame] | 463 | ssize_t target_show_dynamic_sessions(struct se_portal_group *se_tpg, char *page) |
| 464 | { |
| 465 | struct se_session *se_sess; |
| 466 | ssize_t len = 0; |
| 467 | |
| 468 | spin_lock_bh(&se_tpg->session_lock); |
| 469 | list_for_each_entry(se_sess, &se_tpg->tpg_sess_list, sess_list) { |
| 470 | if (!se_sess->se_node_acl) |
| 471 | continue; |
| 472 | if (!se_sess->se_node_acl->dynamic_node_acl) |
| 473 | continue; |
| 474 | if (strlen(se_sess->se_node_acl->initiatorname) + 1 + len > PAGE_SIZE) |
| 475 | break; |
| 476 | |
| 477 | len += snprintf(page + len, PAGE_SIZE - len, "%s\n", |
| 478 | se_sess->se_node_acl->initiatorname); |
| 479 | len += 1; /* Include NULL terminator */ |
| 480 | } |
| 481 | spin_unlock_bh(&se_tpg->session_lock); |
| 482 | |
| 483 | return len; |
| 484 | } |
| 485 | EXPORT_SYMBOL(target_show_dynamic_sessions); |
| 486 | |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 487 | static void target_complete_nacl(struct kref *kref) |
| 488 | { |
| 489 | struct se_node_acl *nacl = container_of(kref, |
| 490 | struct se_node_acl, acl_kref); |
Nicholas Bellinger | 01d4d67 | 2016-12-07 12:55:54 -0800 | [diff] [blame] | 491 | struct se_portal_group *se_tpg = nacl->se_tpg; |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 492 | |
Nicholas Bellinger | 01d4d67 | 2016-12-07 12:55:54 -0800 | [diff] [blame] | 493 | if (!nacl->dynamic_stop) { |
| 494 | complete(&nacl->acl_free_comp); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | mutex_lock(&se_tpg->acl_node_mutex); |
Nicholas Bellinger | 6f48655f | 2017-08-06 16:10:03 -0700 | [diff] [blame] | 499 | list_del_init(&nacl->acl_list); |
Nicholas Bellinger | 01d4d67 | 2016-12-07 12:55:54 -0800 | [diff] [blame] | 500 | mutex_unlock(&se_tpg->acl_node_mutex); |
| 501 | |
| 502 | core_tpg_wait_for_nacl_pr_ref(nacl); |
| 503 | core_free_device_list_for_node(nacl, se_tpg); |
| 504 | kfree(nacl); |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | void target_put_nacl(struct se_node_acl *nacl) |
| 508 | { |
| 509 | kref_put(&nacl->acl_kref, target_complete_nacl); |
| 510 | } |
Nicholas Bellinger | 21aaa23 | 2016-01-07 22:09:27 -0800 | [diff] [blame] | 511 | EXPORT_SYMBOL(target_put_nacl); |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 512 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 513 | void transport_deregister_session_configfs(struct se_session *se_sess) |
| 514 | { |
| 515 | struct se_node_acl *se_nacl; |
Roland Dreier | 2338886 | 2011-06-22 01:02:21 -0700 | [diff] [blame] | 516 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 517 | /* |
| 518 | * Used by struct se_node_acl's under ConfigFS to locate active struct se_session |
| 519 | */ |
| 520 | se_nacl = se_sess->se_node_acl; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 521 | if (se_nacl) { |
Roland Dreier | 2338886 | 2011-06-22 01:02:21 -0700 | [diff] [blame] | 522 | spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags); |
Christoph Hellwig | fba81f8 | 2016-05-02 15:45:20 +0200 | [diff] [blame] | 523 | if (!list_empty(&se_sess->sess_acl_list)) |
| 524 | list_del_init(&se_sess->sess_acl_list); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 525 | /* |
| 526 | * If the session list is empty, then clear the pointer. |
| 527 | * Otherwise, set the struct se_session pointer from the tail |
| 528 | * element of the per struct se_node_acl active session list. |
| 529 | */ |
| 530 | if (list_empty(&se_nacl->acl_sess_list)) |
| 531 | se_nacl->nacl_sess = NULL; |
| 532 | else { |
| 533 | se_nacl->nacl_sess = container_of( |
| 534 | se_nacl->acl_sess_list.prev, |
| 535 | struct se_session, sess_acl_list); |
| 536 | } |
Roland Dreier | 2338886 | 2011-06-22 01:02:21 -0700 | [diff] [blame] | 537 | spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | EXPORT_SYMBOL(transport_deregister_session_configfs); |
| 541 | |
| 542 | void transport_free_session(struct se_session *se_sess) |
| 543 | { |
Nicholas Bellinger | 21aaa23 | 2016-01-07 22:09:27 -0800 | [diff] [blame] | 544 | struct se_node_acl *se_nacl = se_sess->se_node_acl; |
Nicholas Bellinger | 01d4d67 | 2016-12-07 12:55:54 -0800 | [diff] [blame] | 545 | |
Nicholas Bellinger | 21aaa23 | 2016-01-07 22:09:27 -0800 | [diff] [blame] | 546 | /* |
| 547 | * Drop the se_node_acl->nacl_kref obtained from within |
| 548 | * core_tpg_get_initiator_node_acl(). |
| 549 | */ |
| 550 | if (se_nacl) { |
Nicholas Bellinger | 01d4d67 | 2016-12-07 12:55:54 -0800 | [diff] [blame] | 551 | struct se_portal_group *se_tpg = se_nacl->se_tpg; |
| 552 | const struct target_core_fabric_ops *se_tfo = se_tpg->se_tpg_tfo; |
| 553 | unsigned long flags; |
| 554 | |
Nicholas Bellinger | 21aaa23 | 2016-01-07 22:09:27 -0800 | [diff] [blame] | 555 | se_sess->se_node_acl = NULL; |
Nicholas Bellinger | 01d4d67 | 2016-12-07 12:55:54 -0800 | [diff] [blame] | 556 | |
| 557 | /* |
| 558 | * Also determine if we need to drop the extra ->cmd_kref if |
| 559 | * it had been previously dynamically generated, and |
| 560 | * the endpoint is not caching dynamic ACLs. |
| 561 | */ |
| 562 | mutex_lock(&se_tpg->acl_node_mutex); |
| 563 | if (se_nacl->dynamic_node_acl && |
| 564 | !se_tfo->tpg_check_demo_mode_cache(se_tpg)) { |
| 565 | spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags); |
| 566 | if (list_empty(&se_nacl->acl_sess_list)) |
| 567 | se_nacl->dynamic_stop = true; |
| 568 | spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags); |
| 569 | |
| 570 | if (se_nacl->dynamic_stop) |
Nicholas Bellinger | 6f48655f | 2017-08-06 16:10:03 -0700 | [diff] [blame] | 571 | list_del_init(&se_nacl->acl_list); |
Nicholas Bellinger | 01d4d67 | 2016-12-07 12:55:54 -0800 | [diff] [blame] | 572 | } |
| 573 | mutex_unlock(&se_tpg->acl_node_mutex); |
| 574 | |
| 575 | if (se_nacl->dynamic_stop) |
| 576 | target_put_nacl(se_nacl); |
| 577 | |
Nicholas Bellinger | 21aaa23 | 2016-01-07 22:09:27 -0800 | [diff] [blame] | 578 | target_put_nacl(se_nacl); |
| 579 | } |
Nicholas Bellinger | c0add7f | 2013-06-07 17:38:58 -0700 | [diff] [blame] | 580 | if (se_sess->sess_cmd_map) { |
Matthew Wilcox | 10e9cbb | 2018-06-12 12:05:44 -0700 | [diff] [blame] | 581 | sbitmap_queue_free(&se_sess->sess_tag_pool); |
Pekka Enberg | de64d3a | 2015-06-30 14:59:24 -0700 | [diff] [blame] | 582 | kvfree(se_sess->sess_cmd_map); |
Nicholas Bellinger | c0add7f | 2013-06-07 17:38:58 -0700 | [diff] [blame] | 583 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 584 | kmem_cache_free(se_sess_cache, se_sess); |
| 585 | } |
| 586 | EXPORT_SYMBOL(transport_free_session); |
| 587 | |
| 588 | void transport_deregister_session(struct se_session *se_sess) |
| 589 | { |
| 590 | struct se_portal_group *se_tpg = se_sess->se_tpg; |
Roland Dreier | e63a8e1 | 2011-08-12 16:01:02 -0700 | [diff] [blame] | 591 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 592 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 593 | if (!se_tpg) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 594 | transport_free_session(se_sess); |
| 595 | return; |
| 596 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 597 | |
Roland Dreier | e63a8e1 | 2011-08-12 16:01:02 -0700 | [diff] [blame] | 598 | spin_lock_irqsave(&se_tpg->session_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 599 | list_del(&se_sess->sess_list); |
| 600 | se_sess->se_tpg = NULL; |
| 601 | se_sess->fabric_sess_ptr = NULL; |
Roland Dreier | e63a8e1 | 2011-08-12 16:01:02 -0700 | [diff] [blame] | 602 | spin_unlock_irqrestore(&se_tpg->session_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 603 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 604 | pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n", |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 605 | se_tpg->se_tpg_tfo->get_fabric_name()); |
Nicholas Bellinger | 0146834 | 2012-03-10 14:32:52 -0800 | [diff] [blame] | 606 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 607 | * If last kref is dropping now for an explicit NodeACL, awake sleeping |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 608 | * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group |
Nicholas Bellinger | 21aaa23 | 2016-01-07 22:09:27 -0800 | [diff] [blame] | 609 | * removal context from within transport_free_session() code. |
Nicholas Bellinger | 01d4d67 | 2016-12-07 12:55:54 -0800 | [diff] [blame] | 610 | * |
| 611 | * For dynamic ACL, target_put_nacl() uses target_complete_nacl() |
| 612 | * to release all remaining generate_node_acl=1 created ACL resources. |
Nicholas Bellinger | 0146834 | 2012-03-10 14:32:52 -0800 | [diff] [blame] | 613 | */ |
Nicholas Bellinger | 0146834 | 2012-03-10 14:32:52 -0800 | [diff] [blame] | 614 | |
Nicholas Bellinger | afb999f | 2012-03-08 23:45:02 -0800 | [diff] [blame] | 615 | transport_free_session(se_sess); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 616 | } |
| 617 | EXPORT_SYMBOL(transport_deregister_session); |
| 618 | |
Mike Christie | fb7c70f | 2018-08-02 12:12:24 -0500 | [diff] [blame] | 619 | void target_remove_session(struct se_session *se_sess) |
| 620 | { |
| 621 | transport_deregister_session_configfs(se_sess); |
| 622 | transport_deregister_session(se_sess); |
| 623 | } |
| 624 | EXPORT_SYMBOL(target_remove_session); |
| 625 | |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 626 | static void target_remove_from_state_list(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 627 | { |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 628 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 629 | unsigned long flags; |
| 630 | |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 631 | if (!dev) |
| 632 | return; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 633 | |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 634 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
| 635 | if (cmd->state_active) { |
| 636 | list_del(&cmd->state_list); |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 637 | cmd->state_active = false; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 638 | } |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 639 | spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Bart Van Assche | 953bcf7 | 2018-06-22 14:52:54 -0700 | [diff] [blame] | 642 | /* |
| 643 | * This function is called by the target core after the target core has |
| 644 | * finished processing a SCSI command or SCSI TMF. Both the regular command |
| 645 | * processing code and the code for aborting commands can call this |
| 646 | * function. CMD_T_STOP is set if and only if another thread is waiting |
| 647 | * inside transport_wait_for_tasks() for t_transport_stop_comp. |
| 648 | */ |
Bart Van Assche | b1a2ecd | 2017-02-01 16:58:35 -0800 | [diff] [blame] | 649 | static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 650 | { |
| 651 | unsigned long flags; |
| 652 | |
Bart Van Assche | b1a2ecd | 2017-02-01 16:58:35 -0800 | [diff] [blame] | 653 | target_remove_from_state_list(cmd); |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 654 | |
Bart Van Assche | b1a2ecd | 2017-02-01 16:58:35 -0800 | [diff] [blame] | 655 | /* |
| 656 | * Clear struct se_cmd->se_lun before the handoff to FE. |
| 657 | */ |
| 658 | cmd->se_lun = NULL; |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 659 | |
Nicholas Bellinger | febe562 | 2016-01-11 21:31:09 -0800 | [diff] [blame] | 660 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 661 | /* |
| 662 | * Determine if frontend context caller is requesting the stopping of |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 663 | * this command for frontend exceptions. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 664 | */ |
Christoph Hellwig | 7d680f3b | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 665 | if (cmd->transport_state & CMD_T_STOP) { |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 666 | pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n", |
| 667 | __func__, __LINE__, cmd->tag); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 668 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 669 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 670 | |
Nicholas Bellinger | a95d651 | 2014-06-09 23:36:51 +0000 | [diff] [blame] | 671 | complete_all(&cmd->t_transport_stop_comp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 672 | return 1; |
| 673 | } |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 674 | cmd->transport_state &= ~CMD_T_ACTIVE; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 675 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 676 | |
Bart Van Assche | b1a2ecd | 2017-02-01 16:58:35 -0800 | [diff] [blame] | 677 | /* |
| 678 | * Some fabric modules like tcm_loop can release their internally |
| 679 | * allocated I/O reference and struct se_cmd now. |
| 680 | * |
| 681 | * Fabric modules are expected to return '1' here if the se_cmd being |
| 682 | * passed is released at this point, or zero if not being released. |
| 683 | */ |
Nicholas Bellinger | 9c28ca4 | 2017-03-08 00:09:59 -0800 | [diff] [blame] | 684 | return cmd->se_tfo->check_stop_free(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static void transport_lun_remove_cmd(struct se_cmd *cmd) |
| 688 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 689 | struct se_lun *lun = cmd->se_lun; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 690 | |
Nicholas Bellinger | 5259a06 | 2014-01-28 17:56:30 -0800 | [diff] [blame] | 691 | if (!lun) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 692 | return; |
| 693 | |
Nicholas Bellinger | 5259a06 | 2014-01-28 17:56:30 -0800 | [diff] [blame] | 694 | if (cmpxchg(&cmd->lun_ref_active, true, false)) |
| 695 | percpu_ref_put(&lun->lun_ref); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 696 | } |
| 697 | |
Bart Van Assche | 65422d7 | 2018-06-22 14:52:56 -0700 | [diff] [blame] | 698 | int transport_cmd_finish_abort(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 699 | { |
Bart Van Assche | 65422d7 | 2018-06-22 14:52:56 -0700 | [diff] [blame] | 700 | bool send_tas = cmd->transport_state & CMD_T_TAS; |
Nicholas Bellinger | febe562 | 2016-01-11 21:31:09 -0800 | [diff] [blame] | 701 | bool ack_kref = (cmd->se_cmd_flags & SCF_ACK_KREF); |
Nicholas Bellinger | 73d4e58 | 2017-06-02 20:00:17 -0700 | [diff] [blame] | 702 | int ret = 0; |
Nicholas Bellinger | febe562 | 2016-01-11 21:31:09 -0800 | [diff] [blame] | 703 | |
Bart Van Assche | 65422d7 | 2018-06-22 14:52:56 -0700 | [diff] [blame] | 704 | if (send_tas) |
| 705 | transport_send_task_abort(cmd); |
| 706 | |
Alex Leung | 68259b5 | 2014-03-21 22:20:41 -0700 | [diff] [blame] | 707 | if (cmd->se_cmd_flags & SCF_SE_LUN_CMD) |
| 708 | transport_lun_remove_cmd(cmd); |
Nicholas Bellinger | 131e6ab | 2014-03-22 14:55:56 -0700 | [diff] [blame] | 709 | /* |
| 710 | * Allow the fabric driver to unmap any resources before |
| 711 | * releasing the descriptor via TFO->release_cmd() |
| 712 | */ |
Bart Van Assche | 65422d7 | 2018-06-22 14:52:56 -0700 | [diff] [blame] | 713 | if (!send_tas) |
Nicholas Bellinger | 131e6ab | 2014-03-22 14:55:56 -0700 | [diff] [blame] | 714 | cmd->se_tfo->aborted_task(cmd); |
Alex Leung | 68259b5 | 2014-03-21 22:20:41 -0700 | [diff] [blame] | 715 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 716 | if (transport_cmd_check_stop_to_fabric(cmd)) |
Nicholas Bellinger | 73d4e58 | 2017-06-02 20:00:17 -0700 | [diff] [blame] | 717 | return 1; |
Bart Van Assche | 65422d7 | 2018-06-22 14:52:56 -0700 | [diff] [blame] | 718 | if (!send_tas && ack_kref) |
Bart Van Assche | c017069 | 2017-10-31 11:03:11 -0700 | [diff] [blame] | 719 | ret = target_put_sess_cmd(cmd); |
Nicholas Bellinger | 73d4e58 | 2017-06-02 20:00:17 -0700 | [diff] [blame] | 720 | |
| 721 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 722 | } |
| 723 | |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 724 | static void target_complete_failure_work(struct work_struct *work) |
| 725 | { |
| 726 | struct se_cmd *cmd = container_of(work, struct se_cmd, work); |
| 727 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 728 | transport_generic_request_failure(cmd, |
| 729 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE); |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 730 | } |
| 731 | |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 732 | /* |
Paolo Bonzini | d5829ea | 2012-09-05 17:09:15 +0200 | [diff] [blame] | 733 | * Used when asking transport to copy Sense Data from the underlying |
| 734 | * Linux/SCSI struct scsi_cmnd |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 735 | */ |
Paolo Bonzini | d5829ea | 2012-09-05 17:09:15 +0200 | [diff] [blame] | 736 | static unsigned char *transport_get_sense_buffer(struct se_cmd *cmd) |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 737 | { |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 738 | struct se_device *dev = cmd->se_dev; |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 739 | |
| 740 | WARN_ON(!cmd->se_lun); |
| 741 | |
| 742 | if (!dev) |
Paolo Bonzini | d5829ea | 2012-09-05 17:09:15 +0200 | [diff] [blame] | 743 | return NULL; |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 744 | |
Paolo Bonzini | d5829ea | 2012-09-05 17:09:15 +0200 | [diff] [blame] | 745 | if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) |
| 746 | return NULL; |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 747 | |
Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 748 | cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER; |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 749 | |
Paolo Bonzini | d5829ea | 2012-09-05 17:09:15 +0200 | [diff] [blame] | 750 | pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n", |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 751 | dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status); |
Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 752 | return cmd->sense_buffer; |
Paolo Bonzini | 6138ed2 | 2012-09-05 17:09:13 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Mike Christie | c6d66ab | 2017-05-31 15:52:39 -0500 | [diff] [blame] | 755 | void transport_copy_sense_to_cmd(struct se_cmd *cmd, unsigned char *sense) |
| 756 | { |
| 757 | unsigned char *cmd_sense_buf; |
| 758 | unsigned long flags; |
| 759 | |
| 760 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 761 | cmd_sense_buf = transport_get_sense_buffer(cmd); |
| 762 | if (!cmd_sense_buf) { |
| 763 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 764 | return; |
| 765 | } |
| 766 | |
| 767 | cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE; |
| 768 | memcpy(cmd_sense_buf, sense, cmd->scsi_sense_length); |
| 769 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 770 | } |
| 771 | EXPORT_SYMBOL(transport_copy_sense_to_cmd); |
| 772 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 773 | void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 774 | { |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 775 | struct se_device *dev = cmd->se_dev; |
Mike Christie | 402242c | 2017-05-31 15:52:43 -0500 | [diff] [blame] | 776 | int success; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 777 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 778 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 779 | cmd->scsi_status = scsi_status; |
| 780 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 781 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Mike Christie | 402242c | 2017-05-31 15:52:43 -0500 | [diff] [blame] | 782 | switch (cmd->scsi_status) { |
| 783 | case SAM_STAT_CHECK_CONDITION: |
Paolo Bonzini | d5829ea | 2012-09-05 17:09:15 +0200 | [diff] [blame] | 784 | if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 785 | success = 1; |
Mike Christie | 402242c | 2017-05-31 15:52:43 -0500 | [diff] [blame] | 786 | else |
| 787 | success = 0; |
| 788 | break; |
| 789 | default: |
Mike Christie | 9fe3698 | 2017-05-31 15:52:38 -0500 | [diff] [blame] | 790 | success = 1; |
Mike Christie | 402242c | 2017-05-31 15:52:43 -0500 | [diff] [blame] | 791 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* |
Hannes Reinecke | 125d011 | 2013-11-19 09:07:46 +0100 | [diff] [blame] | 795 | * Check for case where an explicit ABORT_TASK has been received |
Nicholas Bellinger | 3d28934 | 2012-02-13 02:38:14 -0800 | [diff] [blame] | 796 | * and transport_wait_for_tasks() will be waiting for completion.. |
| 797 | */ |
Nicholas Bellinger | febe562 | 2016-01-11 21:31:09 -0800 | [diff] [blame] | 798 | if (cmd->transport_state & CMD_T_ABORTED || |
Nicholas Bellinger | 3d28934 | 2012-02-13 02:38:14 -0800 | [diff] [blame] | 799 | cmd->transport_state & CMD_T_STOP) { |
| 800 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Jiang Yi | 1d6ef27 | 2017-06-25 12:28:50 -0700 | [diff] [blame] | 801 | /* |
| 802 | * If COMPARE_AND_WRITE was stopped by __transport_wait_for_tasks(), |
| 803 | * release se_device->caw_sem obtained by sbc_compare_and_write() |
| 804 | * since target_complete_ok_work() or target_complete_failure_work() |
| 805 | * won't be called to invoke the normal CAW completion callbacks. |
| 806 | */ |
| 807 | if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) { |
| 808 | up(&dev->caw_sem); |
| 809 | } |
Nicholas Bellinger | a95d651 | 2014-06-09 23:36:51 +0000 | [diff] [blame] | 810 | complete_all(&cmd->t_transport_stop_comp); |
Nicholas Bellinger | 3d28934 | 2012-02-13 02:38:14 -0800 | [diff] [blame] | 811 | return; |
Roland Dreier | 3dca147 | 2014-02-03 14:08:26 -0800 | [diff] [blame] | 812 | } else if (!success) { |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 813 | INIT_WORK(&cmd->work, target_complete_failure_work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 814 | } else { |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 815 | INIT_WORK(&cmd->work, target_complete_ok_work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 816 | } |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 817 | |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 818 | cmd->t_state = TRANSPORT_COMPLETE; |
Nicholas Bellinger | 3d28934 | 2012-02-13 02:38:14 -0800 | [diff] [blame] | 819 | cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 820 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 821 | |
Quinn Tran | 9095ada | 2016-02-10 18:59:13 -0500 | [diff] [blame] | 822 | if (cmd->se_cmd_flags & SCF_USE_CPUID) |
Quinn Tran | fb3269b | 2015-12-17 14:57:06 -0500 | [diff] [blame] | 823 | queue_work_on(cmd->cpuid, target_completion_wq, &cmd->work); |
Quinn Tran | 9095ada | 2016-02-10 18:59:13 -0500 | [diff] [blame] | 824 | else |
| 825 | queue_work(target_completion_wq, &cmd->work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 826 | } |
Christoph Hellwig | 6bb35e0 | 2012-04-23 11:35:33 -0400 | [diff] [blame] | 827 | EXPORT_SYMBOL(target_complete_cmd); |
| 828 | |
Roland Dreier | 2426bd4 | 2014-06-10 11:07:47 -0700 | [diff] [blame] | 829 | void target_complete_cmd_with_length(struct se_cmd *cmd, u8 scsi_status, int length) |
| 830 | { |
Lee Duncan | bd81372 | 2018-05-15 18:25:24 -0700 | [diff] [blame] | 831 | if ((scsi_status == SAM_STAT_GOOD || |
| 832 | cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) && |
| 833 | length < cmd->data_length) { |
Roland Dreier | 2426bd4 | 2014-06-10 11:07:47 -0700 | [diff] [blame] | 834 | if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) { |
| 835 | cmd->residual_count += cmd->data_length - length; |
| 836 | } else { |
| 837 | cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT; |
| 838 | cmd->residual_count = cmd->data_length - length; |
| 839 | } |
| 840 | |
| 841 | cmd->data_length = length; |
| 842 | } |
| 843 | |
| 844 | target_complete_cmd(cmd, scsi_status); |
| 845 | } |
| 846 | EXPORT_SYMBOL(target_complete_cmd_with_length); |
| 847 | |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 848 | static void target_add_to_state_list(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 849 | { |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 850 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 851 | unsigned long flags; |
| 852 | |
Nicholas Bellinger | 4d2300c | 2011-11-30 18:18:33 -0800 | [diff] [blame] | 853 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 854 | if (!cmd->state_active) { |
| 855 | list_add_tail(&cmd->state_list, &dev->state_list); |
| 856 | cmd->state_active = true; |
| 857 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 858 | spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 859 | } |
| 860 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 861 | /* |
Nicholas Bellinger | f147abb | 2011-10-25 23:57:41 -0700 | [diff] [blame] | 862 | * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 863 | */ |
Christoph Hellwig | 7a6f0a1 | 2012-07-08 15:58:47 -0400 | [diff] [blame] | 864 | static void transport_write_pending_qf(struct se_cmd *cmd); |
| 865 | static void transport_complete_qf(struct se_cmd *cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 866 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 867 | void target_qf_do_work(struct work_struct *work) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 868 | { |
| 869 | struct se_device *dev = container_of(work, struct se_device, |
| 870 | qf_work_queue); |
Roland Dreier | bcac364 | 2011-08-27 21:33:16 -0700 | [diff] [blame] | 871 | LIST_HEAD(qf_cmd_list); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 872 | struct se_cmd *cmd, *cmd_tmp; |
| 873 | |
| 874 | spin_lock_irq(&dev->qf_cmd_lock); |
Roland Dreier | bcac364 | 2011-08-27 21:33:16 -0700 | [diff] [blame] | 875 | list_splice_init(&dev->qf_cmd_list, &qf_cmd_list); |
| 876 | spin_unlock_irq(&dev->qf_cmd_lock); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 877 | |
Roland Dreier | bcac364 | 2011-08-27 21:33:16 -0700 | [diff] [blame] | 878 | 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] | 879 | list_del(&cmd->se_qf_node); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 880 | atomic_dec_mb(&dev->dev_qf_count); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 881 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 882 | pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue" |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 883 | " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd, |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 884 | (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" : |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 885 | (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING" |
| 886 | : "UNKNOWN"); |
Christoph Hellwig | f7a5cc0 | 2011-10-17 13:56:42 -0400 | [diff] [blame] | 887 | |
Christoph Hellwig | 7a6f0a1 | 2012-07-08 15:58:47 -0400 | [diff] [blame] | 888 | if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) |
| 889 | transport_write_pending_qf(cmd); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 890 | else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK || |
| 891 | cmd->t_state == TRANSPORT_COMPLETE_QF_ERR) |
Christoph Hellwig | 7a6f0a1 | 2012-07-08 15:58:47 -0400 | [diff] [blame] | 892 | transport_complete_qf(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 893 | } |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 896 | unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd) |
| 897 | { |
| 898 | switch (cmd->data_direction) { |
| 899 | case DMA_NONE: |
| 900 | return "NONE"; |
| 901 | case DMA_FROM_DEVICE: |
| 902 | return "READ"; |
| 903 | case DMA_TO_DEVICE: |
| 904 | return "WRITE"; |
| 905 | case DMA_BIDIRECTIONAL: |
| 906 | return "BIDI"; |
| 907 | default: |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | return "UNKNOWN"; |
| 912 | } |
| 913 | |
| 914 | void transport_dump_dev_state( |
| 915 | struct se_device *dev, |
| 916 | char *b, |
| 917 | int *bl) |
| 918 | { |
| 919 | *bl += sprintf(b + *bl, "Status: "); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 920 | if (dev->export_count) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 921 | *bl += sprintf(b + *bl, "ACTIVATED"); |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 922 | else |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 923 | *bl += sprintf(b + *bl, "DEACTIVATED"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 924 | |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 925 | *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth); |
Nicholas Bellinger | 11e764b | 2012-05-09 12:42:09 -0700 | [diff] [blame] | 926 | *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n", |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 927 | dev->dev_attrib.block_size, |
| 928 | dev->dev_attrib.hw_max_sectors); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 929 | *bl += sprintf(b + *bl, " "); |
| 930 | } |
| 931 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 932 | void transport_dump_vpd_proto_id( |
| 933 | struct t10_vpd *vpd, |
| 934 | unsigned char *p_buf, |
| 935 | int p_buf_len) |
| 936 | { |
| 937 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
| 938 | int len; |
| 939 | |
| 940 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 941 | len = sprintf(buf, "T10 VPD Protocol Identifier: "); |
| 942 | |
| 943 | switch (vpd->protocol_identifier) { |
| 944 | case 0x00: |
| 945 | sprintf(buf+len, "Fibre Channel\n"); |
| 946 | break; |
| 947 | case 0x10: |
| 948 | sprintf(buf+len, "Parallel SCSI\n"); |
| 949 | break; |
| 950 | case 0x20: |
| 951 | sprintf(buf+len, "SSA\n"); |
| 952 | break; |
| 953 | case 0x30: |
| 954 | sprintf(buf+len, "IEEE 1394\n"); |
| 955 | break; |
| 956 | case 0x40: |
| 957 | sprintf(buf+len, "SCSI Remote Direct Memory Access" |
| 958 | " Protocol\n"); |
| 959 | break; |
| 960 | case 0x50: |
| 961 | sprintf(buf+len, "Internet SCSI (iSCSI)\n"); |
| 962 | break; |
| 963 | case 0x60: |
| 964 | sprintf(buf+len, "SAS Serial SCSI Protocol\n"); |
| 965 | break; |
| 966 | case 0x70: |
| 967 | sprintf(buf+len, "Automation/Drive Interface Transport" |
| 968 | " Protocol\n"); |
| 969 | break; |
| 970 | case 0x80: |
| 971 | sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n"); |
| 972 | break; |
| 973 | default: |
| 974 | sprintf(buf+len, "Unknown 0x%02x\n", |
| 975 | vpd->protocol_identifier); |
| 976 | break; |
| 977 | } |
| 978 | |
| 979 | if (p_buf) |
| 980 | strncpy(p_buf, buf, p_buf_len); |
| 981 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 982 | pr_debug("%s", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | void |
| 986 | transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83) |
| 987 | { |
| 988 | /* |
| 989 | * Check if the Protocol Identifier Valid (PIV) bit is set.. |
| 990 | * |
| 991 | * from spc3r23.pdf section 7.5.1 |
| 992 | */ |
| 993 | if (page_83[1] & 0x80) { |
| 994 | vpd->protocol_identifier = (page_83[0] & 0xf0); |
| 995 | vpd->protocol_identifier_set = 1; |
| 996 | transport_dump_vpd_proto_id(vpd, NULL, 0); |
| 997 | } |
| 998 | } |
| 999 | EXPORT_SYMBOL(transport_set_vpd_proto_id); |
| 1000 | |
| 1001 | int transport_dump_vpd_assoc( |
| 1002 | struct t10_vpd *vpd, |
| 1003 | unsigned char *p_buf, |
| 1004 | int p_buf_len) |
| 1005 | { |
| 1006 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1007 | int ret = 0; |
| 1008 | int len; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1009 | |
| 1010 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 1011 | len = sprintf(buf, "T10 VPD Identifier Association: "); |
| 1012 | |
| 1013 | switch (vpd->association) { |
| 1014 | case 0x00: |
| 1015 | sprintf(buf+len, "addressed logical unit\n"); |
| 1016 | break; |
| 1017 | case 0x10: |
| 1018 | sprintf(buf+len, "target port\n"); |
| 1019 | break; |
| 1020 | case 0x20: |
| 1021 | sprintf(buf+len, "SCSI target device\n"); |
| 1022 | break; |
| 1023 | default: |
| 1024 | sprintf(buf+len, "Unknown 0x%02x\n", vpd->association); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1025 | ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1026 | break; |
| 1027 | } |
| 1028 | |
| 1029 | if (p_buf) |
| 1030 | strncpy(p_buf, buf, p_buf_len); |
| 1031 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1032 | pr_debug("%s", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1033 | |
| 1034 | return ret; |
| 1035 | } |
| 1036 | |
| 1037 | int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83) |
| 1038 | { |
| 1039 | /* |
| 1040 | * The VPD identification association.. |
| 1041 | * |
| 1042 | * from spc3r23.pdf Section 7.6.3.1 Table 297 |
| 1043 | */ |
| 1044 | vpd->association = (page_83[1] & 0x30); |
| 1045 | return transport_dump_vpd_assoc(vpd, NULL, 0); |
| 1046 | } |
| 1047 | EXPORT_SYMBOL(transport_set_vpd_assoc); |
| 1048 | |
| 1049 | int transport_dump_vpd_ident_type( |
| 1050 | struct t10_vpd *vpd, |
| 1051 | unsigned char *p_buf, |
| 1052 | int p_buf_len) |
| 1053 | { |
| 1054 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1055 | int ret = 0; |
| 1056 | int len; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1057 | |
| 1058 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 1059 | len = sprintf(buf, "T10 VPD Identifier Type: "); |
| 1060 | |
| 1061 | switch (vpd->device_identifier_type) { |
| 1062 | case 0x00: |
| 1063 | sprintf(buf+len, "Vendor specific\n"); |
| 1064 | break; |
| 1065 | case 0x01: |
| 1066 | sprintf(buf+len, "T10 Vendor ID based\n"); |
| 1067 | break; |
| 1068 | case 0x02: |
| 1069 | sprintf(buf+len, "EUI-64 based\n"); |
| 1070 | break; |
| 1071 | case 0x03: |
| 1072 | sprintf(buf+len, "NAA\n"); |
| 1073 | break; |
| 1074 | case 0x04: |
| 1075 | sprintf(buf+len, "Relative target port identifier\n"); |
| 1076 | break; |
| 1077 | case 0x08: |
| 1078 | sprintf(buf+len, "SCSI name string\n"); |
| 1079 | break; |
| 1080 | default: |
| 1081 | sprintf(buf+len, "Unsupported: 0x%02x\n", |
| 1082 | vpd->device_identifier_type); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1083 | ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1084 | break; |
| 1085 | } |
| 1086 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1087 | if (p_buf) { |
| 1088 | if (p_buf_len < strlen(buf)+1) |
| 1089 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1090 | strncpy(p_buf, buf, p_buf_len); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1091 | } else { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1092 | pr_debug("%s", buf); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1093 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1094 | |
| 1095 | return ret; |
| 1096 | } |
| 1097 | |
| 1098 | int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83) |
| 1099 | { |
| 1100 | /* |
| 1101 | * The VPD identifier type.. |
| 1102 | * |
| 1103 | * from spc3r23.pdf Section 7.6.3.1 Table 298 |
| 1104 | */ |
| 1105 | vpd->device_identifier_type = (page_83[1] & 0x0f); |
| 1106 | return transport_dump_vpd_ident_type(vpd, NULL, 0); |
| 1107 | } |
| 1108 | EXPORT_SYMBOL(transport_set_vpd_ident_type); |
| 1109 | |
| 1110 | int transport_dump_vpd_ident( |
| 1111 | struct t10_vpd *vpd, |
| 1112 | unsigned char *p_buf, |
| 1113 | int p_buf_len) |
| 1114 | { |
| 1115 | unsigned char buf[VPD_TMP_BUF_SIZE]; |
| 1116 | int ret = 0; |
| 1117 | |
| 1118 | memset(buf, 0, VPD_TMP_BUF_SIZE); |
| 1119 | |
| 1120 | switch (vpd->device_identifier_code_set) { |
| 1121 | case 0x01: /* Binary */ |
Dan Carpenter | 703d641 | 2013-01-18 16:05:12 +0300 | [diff] [blame] | 1122 | snprintf(buf, sizeof(buf), |
| 1123 | "T10 VPD Binary Device Identifier: %s\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1124 | &vpd->device_identifier[0]); |
| 1125 | break; |
| 1126 | case 0x02: /* ASCII */ |
Dan Carpenter | 703d641 | 2013-01-18 16:05:12 +0300 | [diff] [blame] | 1127 | snprintf(buf, sizeof(buf), |
| 1128 | "T10 VPD ASCII Device Identifier: %s\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1129 | &vpd->device_identifier[0]); |
| 1130 | break; |
| 1131 | case 0x03: /* UTF-8 */ |
Dan Carpenter | 703d641 | 2013-01-18 16:05:12 +0300 | [diff] [blame] | 1132 | snprintf(buf, sizeof(buf), |
| 1133 | "T10 VPD UTF-8 Device Identifier: %s\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1134 | &vpd->device_identifier[0]); |
| 1135 | break; |
| 1136 | default: |
| 1137 | sprintf(buf, "T10 VPD Device Identifier encoding unsupported:" |
| 1138 | " 0x%02x", vpd->device_identifier_code_set); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1139 | ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1140 | break; |
| 1141 | } |
| 1142 | |
| 1143 | if (p_buf) |
| 1144 | strncpy(p_buf, buf, p_buf_len); |
| 1145 | else |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1146 | pr_debug("%s", buf); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1147 | |
| 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | int |
| 1152 | transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83) |
| 1153 | { |
| 1154 | static const char hex_str[] = "0123456789abcdef"; |
Masanari Iida | 35d1efe | 2012-08-16 22:43:13 +0900 | [diff] [blame] | 1155 | int j = 0, i = 4; /* offset to start of the identifier */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1156 | |
| 1157 | /* |
| 1158 | * The VPD Code Set (encoding) |
| 1159 | * |
| 1160 | * from spc3r23.pdf Section 7.6.3.1 Table 296 |
| 1161 | */ |
| 1162 | vpd->device_identifier_code_set = (page_83[0] & 0x0f); |
| 1163 | switch (vpd->device_identifier_code_set) { |
| 1164 | case 0x01: /* Binary */ |
| 1165 | vpd->device_identifier[j++] = |
| 1166 | hex_str[vpd->device_identifier_type]; |
| 1167 | while (i < (4 + page_83[3])) { |
| 1168 | vpd->device_identifier[j++] = |
| 1169 | hex_str[(page_83[i] & 0xf0) >> 4]; |
| 1170 | vpd->device_identifier[j++] = |
| 1171 | hex_str[page_83[i] & 0x0f]; |
| 1172 | i++; |
| 1173 | } |
| 1174 | break; |
| 1175 | case 0x02: /* ASCII */ |
| 1176 | case 0x03: /* UTF-8 */ |
| 1177 | while (i < (4 + page_83[3])) |
| 1178 | vpd->device_identifier[j++] = page_83[i++]; |
| 1179 | break; |
| 1180 | default: |
| 1181 | break; |
| 1182 | } |
| 1183 | |
| 1184 | return transport_dump_vpd_ident(vpd, NULL, 0); |
| 1185 | } |
| 1186 | EXPORT_SYMBOL(transport_set_vpd_ident); |
| 1187 | |
Nicholas Bellinger | 8f9b565 | 2015-07-30 18:28:13 -0700 | [diff] [blame] | 1188 | static sense_reason_t |
| 1189 | target_check_max_data_sg_nents(struct se_cmd *cmd, struct se_device *dev, |
| 1190 | unsigned int size) |
| 1191 | { |
| 1192 | u32 mtl; |
| 1193 | |
| 1194 | if (!cmd->se_tfo->max_data_sg_nents) |
| 1195 | return TCM_NO_SENSE; |
| 1196 | /* |
| 1197 | * Check if fabric enforced maximum SGL entries per I/O descriptor |
| 1198 | * exceeds se_cmd->data_length. If true, set SCF_UNDERFLOW_BIT + |
| 1199 | * residual_count and reduce original cmd->data_length to maximum |
| 1200 | * length based on single PAGE_SIZE entry scatter-lists. |
| 1201 | */ |
| 1202 | mtl = (cmd->se_tfo->max_data_sg_nents * PAGE_SIZE); |
| 1203 | if (cmd->data_length > mtl) { |
| 1204 | /* |
| 1205 | * If an existing CDB overflow is present, calculate new residual |
| 1206 | * based on CDB size minus fabric maximum transfer length. |
| 1207 | * |
| 1208 | * If an existing CDB underflow is present, calculate new residual |
| 1209 | * based on original cmd->data_length minus fabric maximum transfer |
| 1210 | * length. |
| 1211 | * |
| 1212 | * Otherwise, set the underflow residual based on cmd->data_length |
| 1213 | * minus fabric maximum transfer length. |
| 1214 | */ |
| 1215 | if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) { |
| 1216 | cmd->residual_count = (size - mtl); |
| 1217 | } else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) { |
| 1218 | u32 orig_dl = size + cmd->residual_count; |
| 1219 | cmd->residual_count = (orig_dl - mtl); |
| 1220 | } else { |
| 1221 | cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT; |
| 1222 | cmd->residual_count = (cmd->data_length - mtl); |
| 1223 | } |
| 1224 | cmd->data_length = mtl; |
| 1225 | /* |
| 1226 | * Reset sbc_check_prot() calculated protection payload |
| 1227 | * length based upon the new smaller MTL. |
| 1228 | */ |
| 1229 | if (cmd->prot_length) { |
| 1230 | u32 sectors = (mtl / dev->dev_attrib.block_size); |
| 1231 | cmd->prot_length = dev->prot_length * sectors; |
| 1232 | } |
| 1233 | } |
| 1234 | return TCM_NO_SENSE; |
| 1235 | } |
| 1236 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1237 | sense_reason_t |
| 1238 | target_cmd_size_check(struct se_cmd *cmd, unsigned int size) |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1239 | { |
| 1240 | struct se_device *dev = cmd->se_dev; |
| 1241 | |
| 1242 | if (cmd->unknown_data_length) { |
| 1243 | cmd->data_length = size; |
| 1244 | } else if (size != cmd->data_length) { |
Nicholas Bellinger | 4ff83da | 2017-05-11 01:07:24 -0700 | [diff] [blame] | 1245 | pr_warn_ratelimited("TARGET_CORE[%s]: Expected Transfer Length:" |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1246 | " %u does not match SCSI CDB Length: %u for SAM Opcode:" |
| 1247 | " 0x%02x\n", cmd->se_tfo->get_fabric_name(), |
| 1248 | cmd->data_length, size, cmd->t_task_cdb[0]); |
| 1249 | |
Nicholas Bellinger | 4ff83da | 2017-05-11 01:07:24 -0700 | [diff] [blame] | 1250 | if (cmd->data_direction == DMA_TO_DEVICE) { |
| 1251 | if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) { |
| 1252 | pr_err_ratelimited("Rejecting underflow/overflow" |
| 1253 | " for WRITE data CDB\n"); |
| 1254 | return TCM_INVALID_CDB_FIELD; |
| 1255 | } |
| 1256 | /* |
| 1257 | * Some fabric drivers like iscsi-target still expect to |
| 1258 | * always reject overflow writes. Reject this case until |
| 1259 | * full fabric driver level support for overflow writes |
| 1260 | * is introduced tree-wide. |
| 1261 | */ |
| 1262 | if (size > cmd->data_length) { |
| 1263 | pr_err_ratelimited("Rejecting overflow for" |
| 1264 | " WRITE control CDB\n"); |
| 1265 | return TCM_INVALID_CDB_FIELD; |
| 1266 | } |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1267 | } |
| 1268 | /* |
| 1269 | * Reject READ_* or WRITE_* with overflow/underflow for |
| 1270 | * type SCF_SCSI_DATA_CDB. |
| 1271 | */ |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1272 | if (dev->dev_attrib.block_size != 512) { |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1273 | pr_err("Failing OVERFLOW/UNDERFLOW for LBA op" |
| 1274 | " CDB on non 512-byte sector setup subsystem" |
| 1275 | " plugin: %s\n", dev->transport->name); |
| 1276 | /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1277 | return TCM_INVALID_CDB_FIELD; |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1278 | } |
Nicholas Bellinger | 4c054ba | 2012-08-16 15:33:10 -0700 | [diff] [blame] | 1279 | /* |
| 1280 | * For the overflow case keep the existing fabric provided |
| 1281 | * ->data_length. Otherwise for the underflow case, reset |
| 1282 | * ->data_length to the smaller SCSI expected data transfer |
| 1283 | * length. |
| 1284 | */ |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1285 | if (size > cmd->data_length) { |
| 1286 | cmd->se_cmd_flags |= SCF_OVERFLOW_BIT; |
| 1287 | cmd->residual_count = (size - cmd->data_length); |
| 1288 | } else { |
| 1289 | cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT; |
| 1290 | cmd->residual_count = (cmd->data_length - size); |
Nicholas Bellinger | 4c054ba | 2012-08-16 15:33:10 -0700 | [diff] [blame] | 1291 | cmd->data_length = size; |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1292 | } |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1293 | } |
| 1294 | |
Nicholas Bellinger | 8f9b565 | 2015-07-30 18:28:13 -0700 | [diff] [blame] | 1295 | return target_check_max_data_sg_nents(cmd, dev, size); |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1296 | |
Christoph Hellwig | 9b3b804 | 2012-05-20 11:59:12 -0400 | [diff] [blame] | 1297 | } |
| 1298 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1299 | /* |
| 1300 | * Used by fabric modules containing a local struct se_cmd within their |
| 1301 | * fabric dependent per I/O descriptor. |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 1302 | * |
| 1303 | * Preserves the value of @cmd->tag. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1304 | */ |
| 1305 | void transport_init_se_cmd( |
| 1306 | struct se_cmd *cmd, |
Christoph Hellwig | 9ac8928 | 2015-04-08 20:01:35 +0200 | [diff] [blame] | 1307 | const struct target_core_fabric_ops *tfo, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1308 | struct se_session *se_sess, |
| 1309 | u32 data_length, |
| 1310 | int data_direction, |
| 1311 | int task_attr, |
| 1312 | unsigned char *sense_buffer) |
| 1313 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 1314 | INIT_LIST_HEAD(&cmd->se_delayed_node); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1315 | INIT_LIST_HEAD(&cmd->se_qf_node); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 1316 | INIT_LIST_HEAD(&cmd->se_cmd_list); |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 1317 | INIT_LIST_HEAD(&cmd->state_list); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1318 | init_completion(&cmd->t_transport_stop_comp); |
Bart Van Assche | 7b2cc7d | 2018-06-22 14:52:59 -0700 | [diff] [blame] | 1319 | cmd->compl = NULL; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1320 | spin_lock_init(&cmd->t_state_lock); |
Bart Van Assche | f2b72d6a | 2017-05-23 16:48:24 -0700 | [diff] [blame] | 1321 | INIT_WORK(&cmd->work, NULL); |
Mikulas Patocka | 1e1110c | 2014-05-17 06:49:22 -0400 | [diff] [blame] | 1322 | kref_init(&cmd->cmd_kref); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1323 | |
| 1324 | cmd->se_tfo = tfo; |
| 1325 | cmd->se_sess = se_sess; |
| 1326 | cmd->data_length = data_length; |
| 1327 | cmd->data_direction = data_direction; |
| 1328 | cmd->sam_task_attr = task_attr; |
| 1329 | cmd->sense_buffer = sense_buffer; |
Christoph Hellwig | cf572a9 | 2012-04-24 00:25:05 -0400 | [diff] [blame] | 1330 | |
| 1331 | cmd->state_active = false; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1332 | } |
| 1333 | EXPORT_SYMBOL(transport_init_se_cmd); |
| 1334 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1335 | static sense_reason_t |
| 1336 | transport_check_alloc_task_attr(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1337 | { |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1338 | struct se_device *dev = cmd->se_dev; |
| 1339 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1340 | /* |
| 1341 | * Check if SAM Task Attribute emulation is enabled for this |
| 1342 | * struct se_device storage object |
| 1343 | */ |
Andy Grover | a354170 | 2015-05-19 14:44:41 -0700 | [diff] [blame] | 1344 | if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1345 | return 0; |
| 1346 | |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1347 | if (cmd->sam_task_attr == TCM_ACA_TAG) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1348 | pr_debug("SAM Task Attribute ACA" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1349 | " emulation is not supported\n"); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1350 | return TCM_INVALID_CDB_FIELD; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1351 | } |
Roland Dreier | 9c31820 | 2015-07-21 17:45:32 -0700 | [diff] [blame] | 1352 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1353 | return 0; |
| 1354 | } |
| 1355 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1356 | sense_reason_t |
| 1357 | target_setup_cmd_from_cdb(struct se_cmd *cmd, unsigned char *cdb) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1358 | { |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1359 | struct se_device *dev = cmd->se_dev; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1360 | sense_reason_t ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1361 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1362 | /* |
| 1363 | * Ensure that the received CDB is less than the max (252 + 8) bytes |
| 1364 | * for VARIABLE_LENGTH_CMD |
| 1365 | */ |
| 1366 | if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1367 | pr_err("Received SCSI CDB with command_size: %d that" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1368 | " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", |
| 1369 | scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1370 | return TCM_INVALID_CDB_FIELD; |
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)); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1385 | return TCM_OUT_OF_RESOURCES; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1386 | } |
| 1387 | } else |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1388 | cmd->t_task_cdb = &cmd->__t_task_cdb[0]; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1389 | /* |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1390 | * Copy the original CDB into cmd-> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1391 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 1392 | memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb)); |
Christoph Hellwig | cb4f4d3 | 2012-05-20 11:59:10 -0400 | [diff] [blame] | 1393 | |
Roland Dreier | e5c0d6a | 2013-06-26 17:36:17 -0700 | [diff] [blame] | 1394 | trace_target_sequencer_start(cmd); |
| 1395 | |
Christoph Hellwig | 0fd97cc | 2012-10-08 00:03:19 -0400 | [diff] [blame] | 1396 | ret = dev->transport->parse_cdb(cmd); |
Joern Engel | 568e1f6 | 2015-07-22 15:01:36 -0700 | [diff] [blame] | 1397 | if (ret == TCM_UNSUPPORTED_SCSI_OPCODE) |
| 1398 | pr_warn_ratelimited("%s/%s: Unsupported SCSI Opcode 0x%02x, sending CHECK_CONDITION.\n", |
| 1399 | cmd->se_tfo->get_fabric_name(), |
| 1400 | cmd->se_sess->se_node_acl->initiatorname, |
| 1401 | cmd->t_task_cdb[0]); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1402 | if (ret) |
| 1403 | return ret; |
Christoph Hellwig | cb4f4d3 | 2012-05-20 11:59:10 -0400 | [diff] [blame] | 1404 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1405 | ret = transport_check_alloc_task_attr(cmd); |
| 1406 | if (ret) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1407 | return ret; |
Christoph Hellwig | cb4f4d3 | 2012-05-20 11:59:10 -0400 | [diff] [blame] | 1408 | |
Christoph Hellwig | cb4f4d3 | 2012-05-20 11:59:10 -0400 | [diff] [blame] | 1409 | cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE; |
Nicholas Bellinger | 4cc987e | 2015-05-19 00:03:07 -0700 | [diff] [blame] | 1410 | atomic_long_inc(&cmd->se_lun->lun_stats.cmd_pdus); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1411 | return 0; |
| 1412 | } |
Andy Grover | a12f41f | 2012-04-03 15:51:20 -0700 | [diff] [blame] | 1413 | EXPORT_SYMBOL(target_setup_cmd_from_cdb); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1414 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1415 | /* |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1416 | * Used by fabric module frontends to queue tasks directly. |
Bart Van Assche | ac75d8b | 2015-10-22 15:52:01 -0700 | [diff] [blame] | 1417 | * May only be used from process context. |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1418 | */ |
| 1419 | int transport_handle_cdb_direct( |
| 1420 | struct se_cmd *cmd) |
| 1421 | { |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1422 | sense_reason_t ret; |
Nicholas Bellinger | dd8ae59 | 2011-07-30 05:03:58 -0700 | [diff] [blame] | 1423 | |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1424 | if (!cmd->se_lun) { |
| 1425 | dump_stack(); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1426 | pr_err("cmd->se_lun is NULL\n"); |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1427 | return -EINVAL; |
| 1428 | } |
| 1429 | if (in_interrupt()) { |
| 1430 | dump_stack(); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1431 | pr_err("transport_generic_handle_cdb cannot be called" |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1432 | " from interrupt context\n"); |
| 1433 | return -EINVAL; |
| 1434 | } |
Nicholas Bellinger | dd8ae59 | 2011-07-30 05:03:58 -0700 | [diff] [blame] | 1435 | /* |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 1436 | * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that |
| 1437 | * outstanding descriptors are handled correctly during shutdown via |
| 1438 | * transport_wait_for_tasks() |
Nicholas Bellinger | dd8ae59 | 2011-07-30 05:03:58 -0700 | [diff] [blame] | 1439 | * |
| 1440 | * Also, we don't take cmd->t_state_lock here as we only expect |
| 1441 | * this to be called for initial descriptor submission. |
| 1442 | */ |
| 1443 | cmd->t_state = TRANSPORT_NEW_CMD; |
Christoph Hellwig | 7d680f3b | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 1444 | cmd->transport_state |= CMD_T_ACTIVE; |
| 1445 | |
Nicholas Bellinger | dd8ae59 | 2011-07-30 05:03:58 -0700 | [diff] [blame] | 1446 | /* |
| 1447 | * transport_generic_new_cmd() is already handling QUEUE_FULL, |
| 1448 | * so follow TRANSPORT_NEW_CMD processing thread context usage |
| 1449 | * and call transport_generic_request_failure() if necessary.. |
| 1450 | */ |
| 1451 | ret = transport_generic_new_cmd(cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1452 | if (ret) |
| 1453 | transport_generic_request_failure(cmd, ret); |
Nicholas Bellinger | dd8ae59 | 2011-07-30 05:03:58 -0700 | [diff] [blame] | 1454 | return 0; |
Nicholas Bellinger | 695434e1 | 2011-06-03 20:59:19 -0700 | [diff] [blame] | 1455 | } |
| 1456 | EXPORT_SYMBOL(transport_handle_cdb_direct); |
| 1457 | |
Nicholas Bellinger | c5ff8d6 | 2013-08-22 11:58:43 -0700 | [diff] [blame] | 1458 | sense_reason_t |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1459 | transport_generic_map_mem_to_cmd(struct se_cmd *cmd, struct scatterlist *sgl, |
| 1460 | u32 sgl_count, struct scatterlist *sgl_bidi, u32 sgl_bidi_count) |
| 1461 | { |
| 1462 | if (!sgl || !sgl_count) |
| 1463 | return 0; |
| 1464 | |
| 1465 | /* |
| 1466 | * Reject SCSI data overflow with map_mem_to_cmd() as incoming |
| 1467 | * scatterlists already have been set to follow what the fabric |
| 1468 | * passes for the original expected data transfer length. |
| 1469 | */ |
| 1470 | if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) { |
| 1471 | pr_warn("Rejecting SCSI DATA overflow for fabric using" |
| 1472 | " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n"); |
| 1473 | return TCM_INVALID_CDB_FIELD; |
| 1474 | } |
| 1475 | |
| 1476 | cmd->t_data_sg = sgl; |
| 1477 | cmd->t_data_nents = sgl_count; |
Ilias Tsitsimpis | b32bd0a | 2015-04-23 21:30:07 +0300 | [diff] [blame] | 1478 | cmd->t_bidi_data_sg = sgl_bidi; |
| 1479 | cmd->t_bidi_data_nents = sgl_bidi_count; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1480 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1481 | cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC; |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
Randy Dunlap | 9ad97b8 | 2018-04-14 10:51:04 -0700 | [diff] [blame] | 1485 | /** |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1486 | * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized |
| 1487 | * se_cmd + use pre-allocated SGL memory. |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1488 | * |
| 1489 | * @se_cmd: command descriptor to submit |
| 1490 | * @se_sess: associated se_sess for endpoint |
| 1491 | * @cdb: pointer to SCSI CDB |
| 1492 | * @sense: pointer to SCSI sense buffer |
| 1493 | * @unpacked_lun: unpacked LUN to reference for struct se_lun |
| 1494 | * @data_length: fabric expected data transfer length |
Randy Dunlap | 9ad97b8 | 2018-04-14 10:51:04 -0700 | [diff] [blame] | 1495 | * @task_attr: SAM task attribute |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1496 | * @data_dir: DMA data direction |
| 1497 | * @flags: flags for command submission from target_sc_flags_tables |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1498 | * @sgl: struct scatterlist memory for unidirectional mapping |
| 1499 | * @sgl_count: scatterlist count for unidirectional mapping |
| 1500 | * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping |
| 1501 | * @sgl_bidi_count: scatterlist count for bidirectional READ mapping |
Nicholas Bellinger | def2b33 | 2013-12-23 20:38:30 +0000 | [diff] [blame] | 1502 | * @sgl_prot: struct scatterlist memory protection information |
| 1503 | * @sgl_prot_count: scatterlist count for protection information |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1504 | * |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 1505 | * Task tags are supported if the caller has set @se_cmd->tag. |
| 1506 | * |
Roland Dreier | d6dfc86 | 2012-07-16 11:04:39 -0700 | [diff] [blame] | 1507 | * Returns non zero to signal active I/O shutdown failure. All other |
| 1508 | * setup exceptions will be returned as a SCSI CHECK_CONDITION response, |
| 1509 | * but still return zero here. |
| 1510 | * |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1511 | * This may only be called from process context, and also currently |
| 1512 | * assumes internal allocation of fabric payload buffer by target-core. |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1513 | */ |
| 1514 | int target_submit_cmd_map_sgls(struct se_cmd *se_cmd, struct se_session *se_sess, |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 1515 | unsigned char *cdb, unsigned char *sense, u64 unpacked_lun, |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1516 | u32 data_length, int task_attr, int data_dir, int flags, |
| 1517 | struct scatterlist *sgl, u32 sgl_count, |
Nicholas Bellinger | def2b33 | 2013-12-23 20:38:30 +0000 | [diff] [blame] | 1518 | struct scatterlist *sgl_bidi, u32 sgl_bidi_count, |
| 1519 | struct scatterlist *sgl_prot, u32 sgl_prot_count) |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1520 | { |
| 1521 | struct se_portal_group *se_tpg; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1522 | sense_reason_t rc; |
| 1523 | int ret; |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1524 | |
| 1525 | se_tpg = se_sess->se_tpg; |
| 1526 | BUG_ON(!se_tpg); |
| 1527 | BUG_ON(se_cmd->se_tfo || se_cmd->se_sess); |
| 1528 | BUG_ON(in_interrupt()); |
| 1529 | /* |
| 1530 | * Initialize se_cmd for target operation. From this point |
| 1531 | * exceptions are handled by sending exception status via |
| 1532 | * target_core_fabric_ops->queue_status() callback |
| 1533 | */ |
| 1534 | transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, |
| 1535 | data_length, data_dir, task_attr, sense); |
Quinn Tran | 9095ada | 2016-02-10 18:59:13 -0500 | [diff] [blame] | 1536 | |
| 1537 | if (flags & TARGET_SCF_USE_CPUID) |
| 1538 | se_cmd->se_cmd_flags |= SCF_USE_CPUID; |
| 1539 | else |
| 1540 | se_cmd->cpuid = WORK_CPU_UNBOUND; |
| 1541 | |
Sebastian Andrzej Siewior | b0d7994 | 2012-01-10 14:16:59 +0100 | [diff] [blame] | 1542 | if (flags & TARGET_SCF_UNKNOWN_SIZE) |
| 1543 | se_cmd->unknown_data_length = 1; |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1544 | /* |
| 1545 | * Obtain struct se_cmd->cmd_kref reference and add new cmd to |
| 1546 | * se_sess->sess_cmd_list. A second kref_get here is necessary |
| 1547 | * for fabrics using TARGET_SCF_ACK_KREF that expect a second |
| 1548 | * kref_put() to happen during fabric packet acknowledgement. |
| 1549 | */ |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 1550 | ret = target_get_sess_cmd(se_cmd, flags & TARGET_SCF_ACK_KREF); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1551 | if (ret) |
| 1552 | return ret; |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1553 | /* |
| 1554 | * Signal bidirectional data payloads to target-core |
| 1555 | */ |
| 1556 | if (flags & TARGET_SCF_BIDI_OP) |
| 1557 | se_cmd->se_cmd_flags |= SCF_BIDI; |
| 1558 | /* |
| 1559 | * Locate se_lun pointer and attach it to struct se_cmd |
| 1560 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1561 | rc = transport_lookup_cmd_lun(se_cmd, unpacked_lun); |
| 1562 | if (rc) { |
| 1563 | transport_send_check_condition_and_sense(se_cmd, rc, 0); |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 1564 | target_put_sess_cmd(se_cmd); |
Roland Dreier | d6dfc86 | 2012-07-16 11:04:39 -0700 | [diff] [blame] | 1565 | return 0; |
Nicholas Bellinger | 735703c | 2012-01-20 19:02:56 -0800 | [diff] [blame] | 1566 | } |
Sagi Grimberg | b5b8e29 | 2014-02-19 17:50:17 +0200 | [diff] [blame] | 1567 | |
| 1568 | rc = target_setup_cmd_from_cdb(se_cmd, cdb); |
| 1569 | if (rc != 0) { |
| 1570 | transport_generic_request_failure(se_cmd, rc); |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
Nicholas Bellinger | def2b33 | 2013-12-23 20:38:30 +0000 | [diff] [blame] | 1574 | /* |
| 1575 | * Save pointers for SGLs containing protection information, |
| 1576 | * if present. |
| 1577 | */ |
| 1578 | if (sgl_prot_count) { |
| 1579 | se_cmd->t_prot_sg = sgl_prot; |
| 1580 | se_cmd->t_prot_nents = sgl_prot_count; |
Akinobu Mita | 5835812 | 2015-05-01 15:23:49 +0900 | [diff] [blame] | 1581 | se_cmd->se_cmd_flags |= SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC; |
Nicholas Bellinger | def2b33 | 2013-12-23 20:38:30 +0000 | [diff] [blame] | 1582 | } |
Christoph Hellwig | d6e0175 | 2012-05-20 11:59:14 -0400 | [diff] [blame] | 1583 | |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1584 | /* |
| 1585 | * When a non zero sgl_count has been passed perform SGL passthrough |
| 1586 | * mapping for pre-allocated fabric memory instead of having target |
| 1587 | * core perform an internal SGL allocation.. |
| 1588 | */ |
| 1589 | if (sgl_count != 0) { |
| 1590 | BUG_ON(!sgl); |
Andy Grover | 11e319e | 2012-04-03 15:51:28 -0700 | [diff] [blame] | 1591 | |
Nicholas Bellinger | 944981c | 2012-10-02 14:00:33 -0700 | [diff] [blame] | 1592 | /* |
| 1593 | * A work-around for tcm_loop as some userspace code via |
| 1594 | * scsi-generic do not memset their associated read buffers, |
| 1595 | * so go ahead and do that here for type non-data CDBs. Also |
| 1596 | * note that this is currently guaranteed to be a single SGL |
| 1597 | * for this case by target core in target_setup_cmd_from_cdb() |
| 1598 | * -> transport_generic_cmd_sequencer(). |
| 1599 | */ |
| 1600 | if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && |
| 1601 | se_cmd->data_direction == DMA_FROM_DEVICE) { |
| 1602 | unsigned char *buf = NULL; |
| 1603 | |
| 1604 | if (sgl) |
| 1605 | buf = kmap(sg_page(sgl)) + sgl->offset; |
| 1606 | |
| 1607 | if (buf) { |
| 1608 | memset(buf, 0, sgl->length); |
| 1609 | kunmap(sg_page(sgl)); |
| 1610 | } |
| 1611 | } |
| 1612 | |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1613 | rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count, |
| 1614 | sgl_bidi, sgl_bidi_count); |
| 1615 | if (rc != 0) { |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1616 | transport_generic_request_failure(se_cmd, rc); |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1617 | return 0; |
| 1618 | } |
| 1619 | } |
Nicholas Bellinger | def2b33 | 2013-12-23 20:38:30 +0000 | [diff] [blame] | 1620 | |
Andy Grover | 11e319e | 2012-04-03 15:51:28 -0700 | [diff] [blame] | 1621 | /* |
| 1622 | * Check if we need to delay processing because of ALUA |
| 1623 | * Active/NonOptimized primary access state.. |
| 1624 | */ |
| 1625 | core_alua_check_nonop_delay(se_cmd); |
| 1626 | |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1627 | transport_handle_cdb_direct(se_cmd); |
Roland Dreier | d6dfc86 | 2012-07-16 11:04:39 -0700 | [diff] [blame] | 1628 | return 0; |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1629 | } |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1630 | EXPORT_SYMBOL(target_submit_cmd_map_sgls); |
| 1631 | |
Randy Dunlap | 9ad97b8 | 2018-04-14 10:51:04 -0700 | [diff] [blame] | 1632 | /** |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1633 | * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd |
| 1634 | * |
| 1635 | * @se_cmd: command descriptor to submit |
| 1636 | * @se_sess: associated se_sess for endpoint |
| 1637 | * @cdb: pointer to SCSI CDB |
| 1638 | * @sense: pointer to SCSI sense buffer |
| 1639 | * @unpacked_lun: unpacked LUN to reference for struct se_lun |
| 1640 | * @data_length: fabric expected data transfer length |
Randy Dunlap | 9ad97b8 | 2018-04-14 10:51:04 -0700 | [diff] [blame] | 1641 | * @task_attr: SAM task attribute |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1642 | * @data_dir: DMA data direction |
| 1643 | * @flags: flags for command submission from target_sc_flags_tables |
| 1644 | * |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 1645 | * Task tags are supported if the caller has set @se_cmd->tag. |
| 1646 | * |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1647 | * Returns non zero to signal active I/O shutdown failure. All other |
| 1648 | * setup exceptions will be returned as a SCSI CHECK_CONDITION response, |
| 1649 | * but still return zero here. |
| 1650 | * |
| 1651 | * This may only be called from process context, and also currently |
| 1652 | * assumes internal allocation of fabric payload buffer by target-core. |
| 1653 | * |
| 1654 | * It also assumes interal target core SGL memory allocation. |
| 1655 | */ |
| 1656 | int target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess, |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 1657 | unsigned char *cdb, unsigned char *sense, u64 unpacked_lun, |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1658 | u32 data_length, int task_attr, int data_dir, int flags) |
| 1659 | { |
| 1660 | return target_submit_cmd_map_sgls(se_cmd, se_sess, cdb, sense, |
| 1661 | unpacked_lun, data_length, task_attr, data_dir, |
Nicholas Bellinger | def2b33 | 2013-12-23 20:38:30 +0000 | [diff] [blame] | 1662 | flags, NULL, 0, NULL, 0, NULL, 0); |
Nicholas Bellinger | a026757 | 2012-10-01 17:23:22 -0700 | [diff] [blame] | 1663 | } |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 1664 | EXPORT_SYMBOL(target_submit_cmd); |
| 1665 | |
Nicholas Bellinger | 9f0d05c | 2012-02-25 05:02:48 -0800 | [diff] [blame] | 1666 | static void target_complete_tmr_failure(struct work_struct *work) |
| 1667 | { |
| 1668 | struct se_cmd *se_cmd = container_of(work, struct se_cmd, work); |
| 1669 | |
| 1670 | se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST; |
| 1671 | se_cmd->se_tfo->queue_tm_rsp(se_cmd); |
Roland Dreier | 5a3b6fc0 | 2013-01-02 12:47:59 -0800 | [diff] [blame] | 1672 | |
Nicholas Bellinger | eeb64d2 | 2017-06-03 06:41:03 -0700 | [diff] [blame] | 1673 | transport_lun_remove_cmd(se_cmd); |
Roland Dreier | 5a3b6fc0 | 2013-01-02 12:47:59 -0800 | [diff] [blame] | 1674 | transport_cmd_check_stop_to_fabric(se_cmd); |
Nicholas Bellinger | 9f0d05c | 2012-02-25 05:02:48 -0800 | [diff] [blame] | 1675 | } |
| 1676 | |
Nicholas Bellinger | 5465e7d | 2017-06-03 06:55:50 -0700 | [diff] [blame] | 1677 | static bool target_lookup_lun_from_tag(struct se_session *se_sess, u64 tag, |
| 1678 | u64 *unpacked_lun) |
| 1679 | { |
| 1680 | struct se_cmd *se_cmd; |
| 1681 | unsigned long flags; |
| 1682 | bool ret = false; |
| 1683 | |
| 1684 | spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); |
| 1685 | list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) { |
| 1686 | if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) |
| 1687 | continue; |
| 1688 | |
| 1689 | if (se_cmd->tag == tag) { |
| 1690 | *unpacked_lun = se_cmd->orig_fe_lun; |
| 1691 | ret = true; |
| 1692 | break; |
| 1693 | } |
| 1694 | } |
| 1695 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
| 1696 | |
| 1697 | return ret; |
| 1698 | } |
| 1699 | |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1700 | /** |
| 1701 | * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd |
| 1702 | * for TMR CDBs |
| 1703 | * |
| 1704 | * @se_cmd: command descriptor to submit |
| 1705 | * @se_sess: associated se_sess for endpoint |
| 1706 | * @sense: pointer to SCSI sense buffer |
| 1707 | * @unpacked_lun: unpacked LUN to reference for struct se_lun |
Randy Dunlap | 1e74aff | 2018-04-14 10:51:03 -0700 | [diff] [blame] | 1708 | * @fabric_tmr_ptr: fabric context for TMR req |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1709 | * @tm_type: Type of TM request |
Nicholas Bellinger | c0974f8 | 2012-02-25 05:10:04 -0800 | [diff] [blame] | 1710 | * @gfp: gfp type for caller |
| 1711 | * @tag: referenced task tag for TMR_ABORT_TASK |
Nicholas Bellinger | c7042ca | 2012-02-25 01:40:24 -0800 | [diff] [blame] | 1712 | * @flags: submit cmd flags |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1713 | * |
| 1714 | * Callable from all contexts. |
| 1715 | **/ |
| 1716 | |
Nicholas Bellinger | c7042ca | 2012-02-25 01:40:24 -0800 | [diff] [blame] | 1717 | int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess, |
Hannes Reinecke | f2d3068 | 2015-06-10 08:41:22 +0200 | [diff] [blame] | 1718 | unsigned char *sense, u64 unpacked_lun, |
Nicholas Bellinger | c0974f8 | 2012-02-25 05:10:04 -0800 | [diff] [blame] | 1719 | void *fabric_tmr_ptr, unsigned char tm_type, |
Bart Van Assche | 5261d86 | 2016-01-05 14:46:39 +0100 | [diff] [blame] | 1720 | gfp_t gfp, u64 tag, int flags) |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1721 | { |
| 1722 | struct se_portal_group *se_tpg; |
| 1723 | int ret; |
| 1724 | |
| 1725 | se_tpg = se_sess->se_tpg; |
| 1726 | BUG_ON(!se_tpg); |
| 1727 | |
| 1728 | transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1729 | 0, DMA_NONE, TCM_SIMPLE_TAG, sense); |
Nicholas Bellinger | c7042ca | 2012-02-25 01:40:24 -0800 | [diff] [blame] | 1730 | /* |
| 1731 | * FIXME: Currently expect caller to handle se_cmd->se_tmr_req |
| 1732 | * allocation failure. |
| 1733 | */ |
Nicholas Bellinger | c0974f8 | 2012-02-25 05:10:04 -0800 | [diff] [blame] | 1734 | 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] | 1735 | if (ret < 0) |
| 1736 | return -ENOMEM; |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1737 | |
Nicholas Bellinger | c0974f8 | 2012-02-25 05:10:04 -0800 | [diff] [blame] | 1738 | if (tm_type == TMR_ABORT_TASK) |
| 1739 | se_cmd->se_tmr_req->ref_task_tag = tag; |
| 1740 | |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1741 | /* See target_submit_cmd for commentary */ |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 1742 | ret = target_get_sess_cmd(se_cmd, flags & TARGET_SCF_ACK_KREF); |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 1743 | if (ret) { |
| 1744 | core_tmr_release_req(se_cmd->se_tmr_req); |
| 1745 | return ret; |
| 1746 | } |
Nicholas Bellinger | 5465e7d | 2017-06-03 06:55:50 -0700 | [diff] [blame] | 1747 | /* |
| 1748 | * If this is ABORT_TASK with no explicit fabric provided LUN, |
| 1749 | * go ahead and search active session tags for a match to figure |
| 1750 | * out unpacked_lun for the original se_cmd. |
| 1751 | */ |
| 1752 | if (tm_type == TMR_ABORT_TASK && (flags & TARGET_SCF_LOOKUP_LUN_FROM_TAG)) { |
| 1753 | if (!target_lookup_lun_from_tag(se_sess, tag, &unpacked_lun)) |
| 1754 | goto failure; |
| 1755 | } |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1756 | |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1757 | ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun); |
Nicholas Bellinger | 5465e7d | 2017-06-03 06:55:50 -0700 | [diff] [blame] | 1758 | if (ret) |
| 1759 | goto failure; |
| 1760 | |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1761 | transport_generic_handle_tmr(se_cmd); |
Nicholas Bellinger | c7042ca | 2012-02-25 01:40:24 -0800 | [diff] [blame] | 1762 | return 0; |
Nicholas Bellinger | 5465e7d | 2017-06-03 06:55:50 -0700 | [diff] [blame] | 1763 | |
| 1764 | /* |
| 1765 | * For callback during failure handling, push this work off |
| 1766 | * to process context with TMR_LUN_DOES_NOT_EXIST status. |
| 1767 | */ |
| 1768 | failure: |
| 1769 | INIT_WORK(&se_cmd->work, target_complete_tmr_failure); |
| 1770 | schedule_work(&se_cmd->work); |
| 1771 | return 0; |
Andy Grover | ea98d7f | 2012-01-19 13:39:21 -0800 | [diff] [blame] | 1772 | } |
| 1773 | EXPORT_SYMBOL(target_submit_tmr); |
| 1774 | |
Christoph Hellwig | cdbb70b | 2011-10-17 13:56:46 -0400 | [diff] [blame] | 1775 | /* |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1776 | * Handle SAM-esque emulation for generic transport request failures. |
| 1777 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1778 | void transport_generic_request_failure(struct se_cmd *cmd, |
| 1779 | sense_reason_t sense_reason) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1780 | { |
Bart Van Assche | aa73237 | 2018-10-15 08:51:39 -0700 | [diff] [blame] | 1781 | int ret = 0; |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1782 | |
Bart Van Assche | c00e622 | 2017-05-23 16:48:28 -0700 | [diff] [blame] | 1783 | pr_debug("-----[ Storage Engine Exception; sense_reason %d\n", |
| 1784 | sense_reason); |
| 1785 | target_show_cmd("-----[ ", cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1786 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1787 | /* |
| 1788 | * For SAM Task Attribute emulation for failed struct se_cmd |
| 1789 | */ |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1790 | transport_complete_task_attr(cmd); |
Nicholas Bellinger | fd2f928 | 2017-09-29 16:03:24 -0700 | [diff] [blame] | 1791 | |
Bart Van Assche | aa73237 | 2018-10-15 08:51:39 -0700 | [diff] [blame] | 1792 | if (cmd->transport_complete_callback) |
| 1793 | cmd->transport_complete_callback(cmd, false, NULL); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1794 | |
Nicholas Bellinger | fd2f928 | 2017-09-29 16:03:24 -0700 | [diff] [blame] | 1795 | if (transport_check_aborted_status(cmd, 1)) |
| 1796 | return; |
| 1797 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1798 | switch (sense_reason) { |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1799 | case TCM_NON_EXISTENT_LUN: |
| 1800 | case TCM_UNSUPPORTED_SCSI_OPCODE: |
| 1801 | case TCM_INVALID_CDB_FIELD: |
| 1802 | case TCM_INVALID_PARAMETER_LIST: |
Roland Dreier | bb992e7 | 2013-02-08 15:18:39 -0800 | [diff] [blame] | 1803 | case TCM_PARAMETER_LIST_LENGTH_ERROR: |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1804 | case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE: |
| 1805 | case TCM_UNKNOWN_MODE_PAGE: |
| 1806 | case TCM_WRITE_PROTECTED: |
Roland Dreier | e2397c7 | 2012-07-16 15:34:21 -0700 | [diff] [blame] | 1807 | case TCM_ADDRESS_OUT_OF_RANGE: |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1808 | case TCM_CHECK_CONDITION_ABORT_CMD: |
| 1809 | case TCM_CHECK_CONDITION_UNIT_ATTENTION: |
| 1810 | case TCM_CHECK_CONDITION_NOT_READY: |
Nicholas Bellinger | 94387aa | 2014-02-23 14:04:09 +0000 | [diff] [blame] | 1811 | case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED: |
| 1812 | case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED: |
| 1813 | case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED: |
Nicholas Bellinger | 449a137 | 2016-10-08 17:26:44 -0700 | [diff] [blame] | 1814 | case TCM_COPY_TARGET_DEVICE_NOT_REACHABLE: |
David Disseldorp | e864212 | 2016-12-23 11:37:53 +0100 | [diff] [blame] | 1815 | case TCM_TOO_MANY_TARGET_DESCS: |
| 1816 | case TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE: |
| 1817 | case TCM_TOO_MANY_SEGMENT_DESCS: |
| 1818 | case TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1819 | break; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1820 | case TCM_OUT_OF_RESOURCES: |
Mike Christie | a271eac | 2017-10-25 11:47:14 -0500 | [diff] [blame] | 1821 | cmd->scsi_status = SAM_STAT_TASK_SET_FULL; |
| 1822 | goto queue_status; |
Mike Christie | d120c70 | 2017-12-19 04:03:55 -0600 | [diff] [blame] | 1823 | case TCM_LUN_BUSY: |
| 1824 | cmd->scsi_status = SAM_STAT_BUSY; |
| 1825 | goto queue_status; |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1826 | case TCM_RESERVATION_CONFLICT: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1827 | /* |
| 1828 | * No SENSE Data payload for this case, set SCSI Status |
| 1829 | * and queue the response to $FABRIC_MOD. |
| 1830 | * |
| 1831 | * Uses linux/include/scsi/scsi.h SAM status codes defs |
| 1832 | */ |
| 1833 | cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; |
| 1834 | /* |
| 1835 | * For UA Interlock Code 11b, a RESERVATION CONFLICT will |
| 1836 | * establish a UNIT ATTENTION with PREVIOUS RESERVATION |
| 1837 | * CONFLICT STATUS. |
| 1838 | * |
| 1839 | * See spc4r17, section 7.4.6 Control Mode Page, Table 349 |
| 1840 | */ |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 1841 | if (cmd->se_sess && |
Hannes Reinecke | c51c8e7 | 2015-06-11 10:01:26 +0200 | [diff] [blame] | 1842 | cmd->se_dev->dev_attrib.emulate_ua_intlck_ctrl == 2) { |
| 1843 | target_ua_allocate_lun(cmd->se_sess->se_node_acl, |
| 1844 | cmd->orig_fe_lun, 0x2C, |
| 1845 | ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS); |
| 1846 | } |
Mike Christie | a271eac | 2017-10-25 11:47:14 -0500 | [diff] [blame] | 1847 | |
| 1848 | goto queue_status; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1849 | default: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 1850 | pr_err("Unknown transport error for CDB 0x%02x: %d\n", |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1851 | cmd->t_task_cdb[0], sense_reason); |
| 1852 | sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1853 | break; |
| 1854 | } |
Christoph Hellwig | f314643 | 2012-07-08 15:58:48 -0400 | [diff] [blame] | 1855 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1856 | ret = transport_send_check_condition_and_sense(cmd, sense_reason, 0); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 1857 | if (ret) |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 1858 | goto queue_full; |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1859 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1860 | check_stop: |
| 1861 | transport_lun_remove_cmd(cmd); |
Andy Grover | 06b967e | 2015-09-03 16:03:43 -0700 | [diff] [blame] | 1862 | transport_cmd_check_stop_to_fabric(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1863 | return; |
| 1864 | |
Mike Christie | a271eac | 2017-10-25 11:47:14 -0500 | [diff] [blame] | 1865 | queue_status: |
| 1866 | trace_target_cmd_complete(cmd); |
| 1867 | ret = cmd->se_tfo->queue_status(cmd); |
| 1868 | if (!ret) |
| 1869 | goto check_stop; |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 1870 | queue_full: |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 1871 | transport_handle_queue_full(cmd, cmd->se_dev, ret, false); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1872 | } |
Nicholas Bellinger | 2fbff127 | 2012-02-10 16:18:11 -0800 | [diff] [blame] | 1873 | EXPORT_SYMBOL(transport_generic_request_failure); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1874 | |
Nicholas Bellinger | dff0ca9 | 2016-05-17 22:19:10 -0700 | [diff] [blame] | 1875 | void __target_execute_cmd(struct se_cmd *cmd, bool do_checks) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1876 | { |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1877 | sense_reason_t ret; |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1878 | |
Nicholas Bellinger | dff0ca9 | 2016-05-17 22:19:10 -0700 | [diff] [blame] | 1879 | if (!cmd->execute_cmd) { |
| 1880 | ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 1881 | goto err; |
| 1882 | } |
| 1883 | if (do_checks) { |
| 1884 | /* |
| 1885 | * Check for an existing UNIT ATTENTION condition after |
| 1886 | * target_handle_task_attr() has done SAM task attr |
| 1887 | * checking, and possibly have already defered execution |
| 1888 | * out to target_restart_delayed_cmds() context. |
| 1889 | */ |
| 1890 | ret = target_scsi3_ua_check(cmd); |
| 1891 | if (ret) |
| 1892 | goto err; |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1893 | |
Nicholas Bellinger | dff0ca9 | 2016-05-17 22:19:10 -0700 | [diff] [blame] | 1894 | ret = target_alua_state_check(cmd); |
| 1895 | if (ret) |
| 1896 | goto err; |
| 1897 | |
| 1898 | ret = target_check_reservation(cmd); |
| 1899 | if (ret) { |
| 1900 | cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; |
| 1901 | goto err; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1902 | } |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1903 | } |
Nicholas Bellinger | dff0ca9 | 2016-05-17 22:19:10 -0700 | [diff] [blame] | 1904 | |
| 1905 | ret = cmd->execute_cmd(cmd); |
| 1906 | if (!ret) |
| 1907 | return; |
| 1908 | err: |
| 1909 | spin_lock_irq(&cmd->t_state_lock); |
Bart Van Assche | fd5e64d | 2017-01-03 10:44:11 +0100 | [diff] [blame] | 1910 | cmd->transport_state &= ~CMD_T_SENT; |
Nicholas Bellinger | dff0ca9 | 2016-05-17 22:19:10 -0700 | [diff] [blame] | 1911 | spin_unlock_irq(&cmd->t_state_lock); |
| 1912 | |
| 1913 | transport_generic_request_failure(cmd, ret); |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1914 | } |
| 1915 | |
Nicholas Bellinger | aa58b53 | 2015-02-08 12:39:06 -0800 | [diff] [blame] | 1916 | static int target_write_prot_action(struct se_cmd *cmd) |
| 1917 | { |
Nicholas Bellinger | 5132d1e | 2015-02-08 03:06:17 -0800 | [diff] [blame] | 1918 | u32 sectors; |
Nicholas Bellinger | aa58b53 | 2015-02-08 12:39:06 -0800 | [diff] [blame] | 1919 | /* |
| 1920 | * Perform WRITE_INSERT of PI using software emulation when backend |
| 1921 | * device has PI enabled, if the transport has not already generated |
| 1922 | * PI using hardware WRITE_INSERT offload. |
| 1923 | */ |
| 1924 | switch (cmd->prot_op) { |
| 1925 | case TARGET_PROT_DOUT_INSERT: |
| 1926 | if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_INSERT)) |
| 1927 | sbc_dif_generate(cmd); |
| 1928 | break; |
Nicholas Bellinger | 5132d1e | 2015-02-08 03:06:17 -0800 | [diff] [blame] | 1929 | case TARGET_PROT_DOUT_STRIP: |
| 1930 | if (cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_STRIP) |
| 1931 | break; |
| 1932 | |
| 1933 | sectors = cmd->data_length >> ilog2(cmd->se_dev->dev_attrib.block_size); |
Sagi Grimberg | f75b6fa | 2015-04-19 20:27:19 +0300 | [diff] [blame] | 1934 | cmd->pi_err = sbc_dif_verify(cmd, cmd->t_task_lba, |
| 1935 | sectors, 0, cmd->t_prot_sg, 0); |
Nicholas Bellinger | 5132d1e | 2015-02-08 03:06:17 -0800 | [diff] [blame] | 1936 | if (unlikely(cmd->pi_err)) { |
| 1937 | spin_lock_irq(&cmd->t_state_lock); |
Bart Van Assche | fd5e64d | 2017-01-03 10:44:11 +0100 | [diff] [blame] | 1938 | cmd->transport_state &= ~CMD_T_SENT; |
Nicholas Bellinger | 5132d1e | 2015-02-08 03:06:17 -0800 | [diff] [blame] | 1939 | spin_unlock_irq(&cmd->t_state_lock); |
| 1940 | transport_generic_request_failure(cmd, cmd->pi_err); |
| 1941 | return -1; |
| 1942 | } |
| 1943 | break; |
Nicholas Bellinger | aa58b53 | 2015-02-08 12:39:06 -0800 | [diff] [blame] | 1944 | default: |
| 1945 | break; |
| 1946 | } |
| 1947 | |
| 1948 | return 0; |
| 1949 | } |
| 1950 | |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1951 | static bool target_handle_task_attr(struct se_cmd *cmd) |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 1952 | { |
| 1953 | struct se_device *dev = cmd->se_dev; |
| 1954 | |
Andy Grover | a354170 | 2015-05-19 14:44:41 -0700 | [diff] [blame] | 1955 | if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1956 | return false; |
| 1957 | |
Nicholas Bellinger | 410c29d | 2016-06-13 22:58:09 -0700 | [diff] [blame] | 1958 | cmd->se_cmd_flags |= SCF_TASK_ATTR_SET; |
| 1959 | |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1960 | /* |
| 1961 | * Check for the existence of HEAD_OF_QUEUE, and if true return 1 |
| 1962 | * to allow the passed struct se_cmd list of tasks to the front of the list. |
| 1963 | */ |
| 1964 | switch (cmd->sam_task_attr) { |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1965 | case TCM_HEAD_TAG: |
Roland Dreier | 9c31820 | 2015-07-21 17:45:32 -0700 | [diff] [blame] | 1966 | pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x\n", |
| 1967 | cmd->t_task_cdb[0]); |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1968 | return false; |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1969 | case TCM_ORDERED_TAG: |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1970 | atomic_inc_mb(&dev->dev_ordered_sync); |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1971 | |
Roland Dreier | 9c31820 | 2015-07-21 17:45:32 -0700 | [diff] [blame] | 1972 | pr_debug("Added ORDERED for CDB: 0x%02x to ordered list\n", |
| 1973 | cmd->t_task_cdb[0]); |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1974 | |
| 1975 | /* |
| 1976 | * Execute an ORDERED command if no other older commands |
| 1977 | * exist that need to be completed first. |
| 1978 | */ |
| 1979 | if (!atomic_read(&dev->simple_cmds)) |
| 1980 | return false; |
| 1981 | break; |
| 1982 | default: |
| 1983 | /* |
| 1984 | * For SIMPLE and UNTAGGED Task Attribute commands |
| 1985 | */ |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 1986 | atomic_inc_mb(&dev->simple_cmds); |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1987 | break; |
| 1988 | } |
| 1989 | |
| 1990 | if (atomic_read(&dev->dev_ordered_sync) == 0) |
| 1991 | return false; |
| 1992 | |
| 1993 | spin_lock(&dev->delayed_cmd_lock); |
| 1994 | list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list); |
| 1995 | spin_unlock(&dev->delayed_cmd_lock); |
| 1996 | |
Roland Dreier | 9c31820 | 2015-07-21 17:45:32 -0700 | [diff] [blame] | 1997 | pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to delayed CMD listn", |
| 1998 | cmd->t_task_cdb[0], cmd->sam_task_attr); |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 1999 | return true; |
| 2000 | } |
| 2001 | |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 2002 | static int __transport_check_aborted_status(struct se_cmd *, int); |
| 2003 | |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 2004 | void target_execute_cmd(struct se_cmd *cmd) |
| 2005 | { |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 2006 | /* |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 2007 | * Determine if frontend context caller is requesting the stopping of |
| 2008 | * this command for frontend exceptions. |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 2009 | * |
Bart Van Assche | 4240d44 | 2018-10-15 08:51:34 -0700 | [diff] [blame] | 2010 | * If the received CDB has already been aborted stop processing it here. |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 2011 | */ |
Nicholas Bellinger | 4a9a6c8 | 2013-11-06 21:05:19 -0800 | [diff] [blame] | 2012 | spin_lock_irq(&cmd->t_state_lock); |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 2013 | if (__transport_check_aborted_status(cmd, 1)) { |
| 2014 | spin_unlock_irq(&cmd->t_state_lock); |
| 2015 | return; |
| 2016 | } |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 2017 | if (cmd->transport_state & CMD_T_STOP) { |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 2018 | pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n", |
| 2019 | __func__, __LINE__, cmd->tag); |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 2020 | |
| 2021 | spin_unlock_irq(&cmd->t_state_lock); |
Nicholas Bellinger | a95d651 | 2014-06-09 23:36:51 +0000 | [diff] [blame] | 2022 | complete_all(&cmd->t_transport_stop_comp); |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 2023 | return; |
| 2024 | } |
| 2025 | |
| 2026 | cmd->t_state = TRANSPORT_PROCESSING; |
Nicholas Bellinger | 1c21a48 | 2017-10-27 22:19:26 -0800 | [diff] [blame] | 2027 | cmd->transport_state &= ~CMD_T_PRE_EXECUTE; |
Bart Van Assche | fd5e64d | 2017-01-03 10:44:11 +0100 | [diff] [blame] | 2028 | cmd->transport_state |= CMD_T_ACTIVE | CMD_T_SENT; |
Christoph Hellwig | f7113a4 | 2012-07-08 15:58:38 -0400 | [diff] [blame] | 2029 | spin_unlock_irq(&cmd->t_state_lock); |
Nicholas Bellinger | aa58b53 | 2015-02-08 12:39:06 -0800 | [diff] [blame] | 2030 | |
| 2031 | if (target_write_prot_action(cmd)) |
| 2032 | return; |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 2033 | |
Nicholas Bellinger | 1a398b9 | 2013-06-06 01:40:27 -0700 | [diff] [blame] | 2034 | if (target_handle_task_attr(cmd)) { |
| 2035 | spin_lock_irq(&cmd->t_state_lock); |
Bart Van Assche | fd5e64d | 2017-01-03 10:44:11 +0100 | [diff] [blame] | 2036 | cmd->transport_state &= ~CMD_T_SENT; |
Nicholas Bellinger | 1a398b9 | 2013-06-06 01:40:27 -0700 | [diff] [blame] | 2037 | spin_unlock_irq(&cmd->t_state_lock); |
| 2038 | return; |
| 2039 | } |
| 2040 | |
Nicholas Bellinger | dff0ca9 | 2016-05-17 22:19:10 -0700 | [diff] [blame] | 2041 | __target_execute_cmd(cmd, true); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2042 | } |
Christoph Hellwig | 70baf0a | 2012-07-08 15:58:39 -0400 | [diff] [blame] | 2043 | EXPORT_SYMBOL(target_execute_cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2044 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2045 | /* |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 2046 | * Process all commands up to the last received ORDERED task attribute which |
| 2047 | * requires another blocking boundary |
| 2048 | */ |
| 2049 | static void target_restart_delayed_cmds(struct se_device *dev) |
| 2050 | { |
| 2051 | for (;;) { |
| 2052 | struct se_cmd *cmd; |
| 2053 | |
| 2054 | spin_lock(&dev->delayed_cmd_lock); |
| 2055 | if (list_empty(&dev->delayed_cmd_list)) { |
| 2056 | spin_unlock(&dev->delayed_cmd_lock); |
| 2057 | break; |
| 2058 | } |
| 2059 | |
| 2060 | cmd = list_entry(dev->delayed_cmd_list.next, |
| 2061 | struct se_cmd, se_delayed_node); |
| 2062 | list_del(&cmd->se_delayed_node); |
| 2063 | spin_unlock(&dev->delayed_cmd_lock); |
| 2064 | |
Nicholas Bellinger | 1c79df1 | 2017-09-22 16:48:28 -0700 | [diff] [blame] | 2065 | cmd->transport_state |= CMD_T_SENT; |
| 2066 | |
Nicholas Bellinger | dff0ca9 | 2016-05-17 22:19:10 -0700 | [diff] [blame] | 2067 | __target_execute_cmd(cmd, true); |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 2068 | |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 2069 | if (cmd->sam_task_attr == TCM_ORDERED_TAG) |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 2070 | break; |
| 2071 | } |
| 2072 | } |
| 2073 | |
| 2074 | /* |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 2075 | * Called from I/O completion to determine which dormant/delayed |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2076 | * and ordered cmds need to have their tasks added to the execution queue. |
| 2077 | */ |
| 2078 | static void transport_complete_task_attr(struct se_cmd *cmd) |
| 2079 | { |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 2080 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2081 | |
Andy Grover | a354170 | 2015-05-19 14:44:41 -0700 | [diff] [blame] | 2082 | if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 2083 | return; |
| 2084 | |
Nicholas Bellinger | 410c29d | 2016-06-13 22:58:09 -0700 | [diff] [blame] | 2085 | if (!(cmd->se_cmd_flags & SCF_TASK_ATTR_SET)) |
| 2086 | goto restart; |
| 2087 | |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 2088 | if (cmd->sam_task_attr == TCM_SIMPLE_TAG) { |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 2089 | atomic_dec_mb(&dev->simple_cmds); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2090 | dev->dev_cur_ordered_id++; |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 2091 | } else if (cmd->sam_task_attr == TCM_HEAD_TAG) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2092 | dev->dev_cur_ordered_id++; |
Roland Dreier | 9c31820 | 2015-07-21 17:45:32 -0700 | [diff] [blame] | 2093 | pr_debug("Incremented dev_cur_ordered_id: %u for HEAD_OF_QUEUE\n", |
| 2094 | dev->dev_cur_ordered_id); |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 2095 | } else if (cmd->sam_task_attr == TCM_ORDERED_TAG) { |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 2096 | atomic_dec_mb(&dev->dev_ordered_sync); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2097 | |
| 2098 | dev->dev_cur_ordered_id++; |
Roland Dreier | 9c31820 | 2015-07-21 17:45:32 -0700 | [diff] [blame] | 2099 | pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED\n", |
| 2100 | dev->dev_cur_ordered_id); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2101 | } |
Nicholas Bellinger | 1c79df1 | 2017-09-22 16:48:28 -0700 | [diff] [blame] | 2102 | cmd->se_cmd_flags &= ~SCF_TASK_ATTR_SET; |
| 2103 | |
Nicholas Bellinger | 410c29d | 2016-06-13 22:58:09 -0700 | [diff] [blame] | 2104 | restart: |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 2105 | target_restart_delayed_cmds(dev); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2106 | } |
| 2107 | |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 2108 | static void transport_complete_qf(struct se_cmd *cmd) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2109 | { |
| 2110 | int ret = 0; |
| 2111 | |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 2112 | transport_complete_task_attr(cmd); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2113 | /* |
| 2114 | * If a fabric driver ->write_pending() or ->queue_data_in() callback |
| 2115 | * has returned neither -ENOMEM or -EAGAIN, assume it's fatal and |
| 2116 | * the same callbacks should not be retried. Return CHECK_CONDITION |
| 2117 | * if a scsi_status is not already set. |
| 2118 | * |
| 2119 | * If a fabric driver ->queue_status() has returned non zero, always |
| 2120 | * keep retrying no matter what.. |
| 2121 | */ |
| 2122 | if (cmd->t_state == TRANSPORT_COMPLETE_QF_ERR) { |
| 2123 | if (cmd->scsi_status) |
| 2124 | goto queue_status; |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 2125 | |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2126 | translate_sense_reason(cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE); |
| 2127 | goto queue_status; |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 2128 | } |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2129 | |
Lee Duncan | bd81372 | 2018-05-15 18:25:24 -0700 | [diff] [blame] | 2130 | /* |
| 2131 | * Check if we need to send a sense buffer from |
| 2132 | * the struct se_cmd in question. We do NOT want |
| 2133 | * to take this path of the IO has been marked as |
| 2134 | * needing to be treated like a "normal read". This |
| 2135 | * is the case if it's a tape read, and either the |
| 2136 | * FM, EOM, or ILI bits are set, but there is no |
| 2137 | * sense data. |
| 2138 | */ |
| 2139 | if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) && |
| 2140 | cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2141 | goto queue_status; |
| 2142 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2143 | switch (cmd->data_direction) { |
| 2144 | case DMA_FROM_DEVICE: |
Lee Duncan | bd81372 | 2018-05-15 18:25:24 -0700 | [diff] [blame] | 2145 | /* queue status if not treating this as a normal read */ |
| 2146 | if (cmd->scsi_status && |
| 2147 | !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL)) |
Nicholas Bellinger | 4347ab5a | 2016-03-04 22:30:52 -0800 | [diff] [blame] | 2148 | goto queue_status; |
| 2149 | |
Roland Dreier | e5c0d6a | 2013-06-26 17:36:17 -0700 | [diff] [blame] | 2150 | trace_target_cmd_complete(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2151 | ret = cmd->se_tfo->queue_data_in(cmd); |
| 2152 | break; |
| 2153 | case DMA_TO_DEVICE: |
Nicholas Bellinger | 6457740 | 2013-08-21 14:39:19 -0700 | [diff] [blame] | 2154 | if (cmd->se_cmd_flags & SCF_BIDI) { |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2155 | ret = cmd->se_tfo->queue_data_in(cmd); |
Bart Van Assche | 63509c6 | 2015-05-13 09:17:54 +0200 | [diff] [blame] | 2156 | break; |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2157 | } |
Bart Van Assche | d7e595d | 2017-10-31 11:03:10 -0700 | [diff] [blame] | 2158 | /* fall through */ |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2159 | case DMA_NONE: |
Nicholas Bellinger | 4347ab5a | 2016-03-04 22:30:52 -0800 | [diff] [blame] | 2160 | queue_status: |
Roland Dreier | e5c0d6a | 2013-06-26 17:36:17 -0700 | [diff] [blame] | 2161 | trace_target_cmd_complete(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2162 | ret = cmd->se_tfo->queue_status(cmd); |
| 2163 | break; |
| 2164 | default: |
| 2165 | break; |
| 2166 | } |
| 2167 | |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 2168 | if (ret < 0) { |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2169 | transport_handle_queue_full(cmd, cmd->se_dev, ret, false); |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 2170 | return; |
| 2171 | } |
| 2172 | transport_lun_remove_cmd(cmd); |
| 2173 | transport_cmd_check_stop_to_fabric(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2176 | static void transport_handle_queue_full(struct se_cmd *cmd, struct se_device *dev, |
| 2177 | int err, bool write_pending) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2178 | { |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2179 | /* |
| 2180 | * -EAGAIN or -ENOMEM signals retry of ->write_pending() and/or |
| 2181 | * ->queue_data_in() callbacks from new process context. |
| 2182 | * |
| 2183 | * Otherwise for other errors, transport_complete_qf() will send |
| 2184 | * CHECK_CONDITION via ->queue_status() instead of attempting to |
| 2185 | * retry associated fabric driver data-transfer callbacks. |
| 2186 | */ |
| 2187 | if (err == -EAGAIN || err == -ENOMEM) { |
| 2188 | cmd->t_state = (write_pending) ? TRANSPORT_COMPLETE_QF_WP : |
| 2189 | TRANSPORT_COMPLETE_QF_OK; |
| 2190 | } else { |
| 2191 | pr_warn_ratelimited("Got unknown fabric queue status: %d\n", err); |
| 2192 | cmd->t_state = TRANSPORT_COMPLETE_QF_ERR; |
| 2193 | } |
| 2194 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2195 | spin_lock_irq(&dev->qf_cmd_lock); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2196 | list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list); |
Joern Engel | 33940d0 | 2014-09-16 16:23:12 -0400 | [diff] [blame] | 2197 | atomic_inc_mb(&dev->dev_qf_count); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2198 | spin_unlock_irq(&cmd->se_dev->qf_cmd_lock); |
| 2199 | |
| 2200 | schedule_work(&cmd->se_dev->qf_work_queue); |
| 2201 | } |
| 2202 | |
Nicholas Bellinger | fdeab85 | 2015-02-08 02:53:25 -0800 | [diff] [blame] | 2203 | static bool target_read_prot_action(struct se_cmd *cmd) |
Nicholas Bellinger | bc00586 | 2014-04-02 14:55:33 -0700 | [diff] [blame] | 2204 | { |
Nicholas Bellinger | fdeab85 | 2015-02-08 02:53:25 -0800 | [diff] [blame] | 2205 | switch (cmd->prot_op) { |
| 2206 | case TARGET_PROT_DIN_STRIP: |
| 2207 | if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_STRIP)) { |
Sagi Grimberg | f75b6fa | 2015-04-19 20:27:19 +0300 | [diff] [blame] | 2208 | u32 sectors = cmd->data_length >> |
| 2209 | ilog2(cmd->se_dev->dev_attrib.block_size); |
| 2210 | |
| 2211 | cmd->pi_err = sbc_dif_verify(cmd, cmd->t_task_lba, |
| 2212 | sectors, 0, cmd->t_prot_sg, |
| 2213 | 0); |
| 2214 | if (cmd->pi_err) |
Nicholas Bellinger | fdeab85 | 2015-02-08 02:53:25 -0800 | [diff] [blame] | 2215 | return true; |
Nicholas Bellinger | bc00586 | 2014-04-02 14:55:33 -0700 | [diff] [blame] | 2216 | } |
Nicholas Bellinger | fdeab85 | 2015-02-08 02:53:25 -0800 | [diff] [blame] | 2217 | break; |
Nicholas Bellinger | 72c03850 | 2015-02-08 04:02:08 -0800 | [diff] [blame] | 2218 | case TARGET_PROT_DIN_INSERT: |
| 2219 | if (cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_INSERT) |
| 2220 | break; |
| 2221 | |
| 2222 | sbc_dif_generate(cmd); |
| 2223 | break; |
Nicholas Bellinger | fdeab85 | 2015-02-08 02:53:25 -0800 | [diff] [blame] | 2224 | default: |
| 2225 | break; |
Nicholas Bellinger | bc00586 | 2014-04-02 14:55:33 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | return false; |
| 2229 | } |
| 2230 | |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 2231 | static void target_complete_ok_work(struct work_struct *work) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2232 | { |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 2233 | struct se_cmd *cmd = container_of(work, struct se_cmd, work); |
Paolo Bonzini | 27a2709 | 2012-09-05 17:09:14 +0200 | [diff] [blame] | 2234 | int ret; |
Christoph Hellwig | 35e0e75 | 2011-10-17 13:56:53 -0400 | [diff] [blame] | 2235 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2236 | /* |
| 2237 | * Check if we need to move delayed/dormant tasks from cmds on the |
| 2238 | * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task |
| 2239 | * Attribute. |
| 2240 | */ |
Christoph Hellwig | 019c4ca | 2012-10-10 17:37:14 -0400 | [diff] [blame] | 2241 | transport_complete_task_attr(cmd); |
| 2242 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2243 | /* |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2244 | * Check to schedule QUEUE_FULL work, or execute an existing |
| 2245 | * cmd->transport_qf_callback() |
| 2246 | */ |
| 2247 | if (atomic_read(&cmd->se_dev->dev_qf_count) != 0) |
| 2248 | schedule_work(&cmd->se_dev->qf_work_queue); |
| 2249 | |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2250 | /* |
Paolo Bonzini | d5829ea | 2012-09-05 17:09:15 +0200 | [diff] [blame] | 2251 | * Check if we need to send a sense buffer from |
Lee Duncan | bd81372 | 2018-05-15 18:25:24 -0700 | [diff] [blame] | 2252 | * the struct se_cmd in question. We do NOT want |
| 2253 | * to take this path of the IO has been marked as |
| 2254 | * needing to be treated like a "normal read". This |
| 2255 | * is the case if it's a tape read, and either the |
| 2256 | * FM, EOM, or ILI bits are set, but there is no |
| 2257 | * sense data. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2258 | */ |
Lee Duncan | bd81372 | 2018-05-15 18:25:24 -0700 | [diff] [blame] | 2259 | if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) && |
| 2260 | cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) { |
Paolo Bonzini | 27a2709 | 2012-09-05 17:09:14 +0200 | [diff] [blame] | 2261 | WARN_ON(!cmd->scsi_status); |
Paolo Bonzini | 27a2709 | 2012-09-05 17:09:14 +0200 | [diff] [blame] | 2262 | ret = transport_send_check_condition_and_sense( |
| 2263 | cmd, 0, 1); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2264 | if (ret) |
Paolo Bonzini | 27a2709 | 2012-09-05 17:09:14 +0200 | [diff] [blame] | 2265 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2266 | |
Paolo Bonzini | 27a2709 | 2012-09-05 17:09:14 +0200 | [diff] [blame] | 2267 | transport_lun_remove_cmd(cmd); |
| 2268 | transport_cmd_check_stop_to_fabric(cmd); |
| 2269 | return; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2270 | } |
| 2271 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2272 | * Check for a callback, used by amongst other things |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 2273 | * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2274 | */ |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 2275 | if (cmd->transport_complete_callback) { |
| 2276 | sense_reason_t rc; |
Nicholas Bellinger | 057085e | 2015-11-05 23:37:59 -0800 | [diff] [blame] | 2277 | bool caw = (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE); |
| 2278 | bool zero_dl = !(cmd->data_length); |
| 2279 | int post_ret = 0; |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 2280 | |
Nicholas Bellinger | 057085e | 2015-11-05 23:37:59 -0800 | [diff] [blame] | 2281 | rc = cmd->transport_complete_callback(cmd, true, &post_ret); |
| 2282 | if (!rc && !post_ret) { |
| 2283 | if (caw && zero_dl) |
Nicholas Bellinger | c8e6398 | 2015-04-07 21:53:27 +0000 | [diff] [blame] | 2284 | goto queue_rsp; |
| 2285 | |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 2286 | return; |
Nicholas Bellinger | a289008 | 2013-08-21 18:10:04 -0700 | [diff] [blame] | 2287 | } else if (rc) { |
| 2288 | ret = transport_send_check_condition_and_sense(cmd, |
| 2289 | rc, 0); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2290 | if (ret) |
Nicholas Bellinger | a289008 | 2013-08-21 18:10:04 -0700 | [diff] [blame] | 2291 | goto queue_full; |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 2292 | |
Nicholas Bellinger | a289008 | 2013-08-21 18:10:04 -0700 | [diff] [blame] | 2293 | transport_lun_remove_cmd(cmd); |
| 2294 | transport_cmd_check_stop_to_fabric(cmd); |
| 2295 | return; |
| 2296 | } |
Nicholas Bellinger | a6b0133 | 2013-08-19 14:34:17 -0700 | [diff] [blame] | 2297 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2298 | |
Nicholas Bellinger | c8e6398 | 2015-04-07 21:53:27 +0000 | [diff] [blame] | 2299 | queue_rsp: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2300 | switch (cmd->data_direction) { |
| 2301 | case DMA_FROM_DEVICE: |
Lee Duncan | bd81372 | 2018-05-15 18:25:24 -0700 | [diff] [blame] | 2302 | /* |
| 2303 | * if this is a READ-type IO, but SCSI status |
| 2304 | * is set, then skip returning data and just |
| 2305 | * return the status -- unless this IO is marked |
| 2306 | * as needing to be treated as a normal read, |
| 2307 | * in which case we want to go ahead and return |
| 2308 | * the data. This happens, for example, for tape |
| 2309 | * reads with the FM, EOM, or ILI bits set, with |
| 2310 | * no sense data. |
| 2311 | */ |
| 2312 | if (cmd->scsi_status && |
| 2313 | !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL)) |
Nicholas Bellinger | 4347ab5a | 2016-03-04 22:30:52 -0800 | [diff] [blame] | 2314 | goto queue_status; |
| 2315 | |
Nicholas Bellinger | 4cc987e | 2015-05-19 00:03:07 -0700 | [diff] [blame] | 2316 | atomic_long_add(cmd->data_length, |
| 2317 | &cmd->se_lun->lun_stats.tx_data_octets); |
Nicholas Bellinger | bc00586 | 2014-04-02 14:55:33 -0700 | [diff] [blame] | 2318 | /* |
| 2319 | * Perform READ_STRIP of PI using software emulation when |
| 2320 | * backend had PI enabled, if the transport will not be |
| 2321 | * performing hardware READ_STRIP offload. |
| 2322 | */ |
Nicholas Bellinger | fdeab85 | 2015-02-08 02:53:25 -0800 | [diff] [blame] | 2323 | if (target_read_prot_action(cmd)) { |
Nicholas Bellinger | bc00586 | 2014-04-02 14:55:33 -0700 | [diff] [blame] | 2324 | ret = transport_send_check_condition_and_sense(cmd, |
| 2325 | cmd->pi_err, 0); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2326 | if (ret) |
Nicholas Bellinger | bc00586 | 2014-04-02 14:55:33 -0700 | [diff] [blame] | 2327 | goto queue_full; |
| 2328 | |
| 2329 | transport_lun_remove_cmd(cmd); |
| 2330 | transport_cmd_check_stop_to_fabric(cmd); |
| 2331 | return; |
| 2332 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2333 | |
Roland Dreier | e5c0d6a | 2013-06-26 17:36:17 -0700 | [diff] [blame] | 2334 | trace_target_cmd_complete(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2335 | ret = cmd->se_tfo->queue_data_in(cmd); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2336 | if (ret) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2337 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2338 | break; |
| 2339 | case DMA_TO_DEVICE: |
Nicholas Bellinger | 4cc987e | 2015-05-19 00:03:07 -0700 | [diff] [blame] | 2340 | atomic_long_add(cmd->data_length, |
| 2341 | &cmd->se_lun->lun_stats.rx_data_octets); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2342 | /* |
| 2343 | * Check if we need to send READ payload for BIDI-COMMAND |
| 2344 | */ |
Nicholas Bellinger | 6457740 | 2013-08-21 14:39:19 -0700 | [diff] [blame] | 2345 | if (cmd->se_cmd_flags & SCF_BIDI) { |
Nicholas Bellinger | 4cc987e | 2015-05-19 00:03:07 -0700 | [diff] [blame] | 2346 | atomic_long_add(cmd->data_length, |
| 2347 | &cmd->se_lun->lun_stats.tx_data_octets); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2348 | ret = cmd->se_tfo->queue_data_in(cmd); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2349 | if (ret) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2350 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2351 | break; |
| 2352 | } |
Bart Van Assche | d7e595d | 2017-10-31 11:03:10 -0700 | [diff] [blame] | 2353 | /* fall through */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2354 | case DMA_NONE: |
Nicholas Bellinger | 4347ab5a | 2016-03-04 22:30:52 -0800 | [diff] [blame] | 2355 | queue_status: |
Roland Dreier | e5c0d6a | 2013-06-26 17:36:17 -0700 | [diff] [blame] | 2356 | trace_target_cmd_complete(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2357 | ret = cmd->se_tfo->queue_status(cmd); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2358 | if (ret) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2359 | goto queue_full; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2360 | break; |
| 2361 | default: |
| 2362 | break; |
| 2363 | } |
| 2364 | |
| 2365 | transport_lun_remove_cmd(cmd); |
| 2366 | transport_cmd_check_stop_to_fabric(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2367 | return; |
| 2368 | |
| 2369 | queue_full: |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2370 | pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p," |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2371 | " data_direction: %d\n", cmd, cmd->data_direction); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2372 | |
| 2373 | transport_handle_queue_full(cmd, cmd->se_dev, ret, false); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2374 | } |
| 2375 | |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2376 | void target_free_sgl(struct scatterlist *sgl, int nents) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2377 | { |
Bart Van Assche | 8c7a8d1 | 2018-01-19 11:00:54 -0800 | [diff] [blame] | 2378 | sgl_free_n_order(sgl, nents, 0); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2379 | } |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2380 | EXPORT_SYMBOL(target_free_sgl); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2381 | |
Nicholas Bellinger | 47e459e | 2013-08-20 10:45:16 -0700 | [diff] [blame] | 2382 | static inline void transport_reset_sgl_orig(struct se_cmd *cmd) |
| 2383 | { |
| 2384 | /* |
| 2385 | * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE |
| 2386 | * emulation, and free + reset pointers if necessary.. |
| 2387 | */ |
| 2388 | if (!cmd->t_data_sg_orig) |
| 2389 | return; |
| 2390 | |
| 2391 | kfree(cmd->t_data_sg); |
| 2392 | cmd->t_data_sg = cmd->t_data_sg_orig; |
| 2393 | cmd->t_data_sg_orig = NULL; |
| 2394 | cmd->t_data_nents = cmd->t_data_nents_orig; |
| 2395 | cmd->t_data_nents_orig = 0; |
| 2396 | } |
| 2397 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2398 | static inline void transport_free_pages(struct se_cmd *cmd) |
| 2399 | { |
Akinobu Mita | 5835812 | 2015-05-01 15:23:49 +0900 | [diff] [blame] | 2400 | if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) { |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2401 | target_free_sgl(cmd->t_prot_sg, cmd->t_prot_nents); |
Akinobu Mita | 5835812 | 2015-05-01 15:23:49 +0900 | [diff] [blame] | 2402 | cmd->t_prot_sg = NULL; |
| 2403 | cmd->t_prot_nents = 0; |
| 2404 | } |
| 2405 | |
Nicholas Bellinger | 47e459e | 2013-08-20 10:45:16 -0700 | [diff] [blame] | 2406 | if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) { |
Nicholas Bellinger | c8e6398 | 2015-04-07 21:53:27 +0000 | [diff] [blame] | 2407 | /* |
| 2408 | * Release special case READ buffer payload required for |
| 2409 | * SG_TO_MEM_NOALLOC to function with COMPARE_AND_WRITE |
| 2410 | */ |
| 2411 | if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) { |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2412 | target_free_sgl(cmd->t_bidi_data_sg, |
Nicholas Bellinger | c8e6398 | 2015-04-07 21:53:27 +0000 | [diff] [blame] | 2413 | cmd->t_bidi_data_nents); |
| 2414 | cmd->t_bidi_data_sg = NULL; |
| 2415 | cmd->t_bidi_data_nents = 0; |
| 2416 | } |
Nicholas Bellinger | 47e459e | 2013-08-20 10:45:16 -0700 | [diff] [blame] | 2417 | transport_reset_sgl_orig(cmd); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 2418 | return; |
Nicholas Bellinger | 47e459e | 2013-08-20 10:45:16 -0700 | [diff] [blame] | 2419 | } |
| 2420 | transport_reset_sgl_orig(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2421 | |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2422 | target_free_sgl(cmd->t_data_sg, cmd->t_data_nents); |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2423 | cmd->t_data_sg = NULL; |
| 2424 | cmd->t_data_nents = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2425 | |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2426 | target_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents); |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2427 | cmd->t_bidi_data_sg = NULL; |
| 2428 | cmd->t_bidi_data_nents = 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2429 | } |
| 2430 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2431 | void *transport_kmap_data_sg(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2432 | { |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2433 | struct scatterlist *sg = cmd->t_data_sg; |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2434 | struct page **pages; |
| 2435 | int i; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2436 | |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2437 | /* |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2438 | * We need to take into account a possible offset here for fabrics like |
| 2439 | * tcm_loop who may be using a contig buffer from the SCSI midlayer for |
| 2440 | * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd() |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2441 | */ |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2442 | if (!cmd->t_data_nents) |
| 2443 | return NULL; |
Paolo Bonzini | 3717ef0 | 2012-09-07 17:30:35 +0200 | [diff] [blame] | 2444 | |
| 2445 | BUG_ON(!sg); |
| 2446 | if (cmd->t_data_nents == 1) |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2447 | return kmap(sg_page(sg)) + sg->offset; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2448 | |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2449 | /* >1 page. use vmap */ |
Markus Elfring | df6751f | 2017-04-09 21:07:14 +0200 | [diff] [blame] | 2450 | pages = kmalloc_array(cmd->t_data_nents, sizeof(*pages), GFP_KERNEL); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2451 | if (!pages) |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2452 | return NULL; |
| 2453 | |
| 2454 | /* convert sg[] to pages[] */ |
| 2455 | for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) { |
| 2456 | pages[i] = sg_page(sg); |
| 2457 | } |
| 2458 | |
| 2459 | cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL); |
| 2460 | kfree(pages); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2461 | if (!cmd->t_data_vmap) |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2462 | return NULL; |
| 2463 | |
| 2464 | return cmd->t_data_vmap + cmd->t_data_sg[0].offset; |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2465 | } |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2466 | EXPORT_SYMBOL(transport_kmap_data_sg); |
| 2467 | |
| 2468 | void transport_kunmap_data_sg(struct se_cmd *cmd) |
| 2469 | { |
Andy Grover | a1edf9c | 2012-02-09 12:18:06 -0800 | [diff] [blame] | 2470 | if (!cmd->t_data_nents) { |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2471 | return; |
Andy Grover | a1edf9c | 2012-02-09 12:18:06 -0800 | [diff] [blame] | 2472 | } else if (cmd->t_data_nents == 1) { |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2473 | kunmap(sg_page(cmd->t_data_sg)); |
Andy Grover | a1edf9c | 2012-02-09 12:18:06 -0800 | [diff] [blame] | 2474 | return; |
| 2475 | } |
Andy Grover | 4949314 | 2012-01-16 16:57:08 -0800 | [diff] [blame] | 2476 | |
| 2477 | vunmap(cmd->t_data_vmap); |
| 2478 | cmd->t_data_vmap = NULL; |
| 2479 | } |
| 2480 | EXPORT_SYMBOL(transport_kunmap_data_sg); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2481 | |
Nicholas Bellinger | c5ff8d6 | 2013-08-22 11:58:43 -0700 | [diff] [blame] | 2482 | int |
Nicholas Bellinger | 2009399 | 2013-08-25 15:44:03 -0700 | [diff] [blame] | 2483 | target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length, |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2484 | bool zero_page, bool chainable) |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2485 | { |
Bart Van Assche | 14db491 | 2018-01-05 08:26:50 -0800 | [diff] [blame] | 2486 | gfp_t gfp = GFP_KERNEL | (zero_page ? __GFP_ZERO : 0); |
Andy Grover | 05d1c7c | 2011-07-20 19:13:28 +0000 | [diff] [blame] | 2487 | |
Bart Van Assche | 14db491 | 2018-01-05 08:26:50 -0800 | [diff] [blame] | 2488 | *sgl = sgl_alloc_order(length, 0, chainable, gfp, nents); |
| 2489 | return *sgl ? 0 : -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2490 | } |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2491 | EXPORT_SYMBOL(target_alloc_sgl); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2492 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2493 | /* |
Andy Grover | b16a35b | 2012-04-03 15:51:21 -0700 | [diff] [blame] | 2494 | * Allocate any required resources to execute the command. For writes we |
| 2495 | * might not have the payload yet, so notify the fabric via a call to |
| 2496 | * ->write_pending instead. Otherwise place it on the execution queue. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2497 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2498 | sense_reason_t |
| 2499 | transport_generic_new_cmd(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2500 | { |
Bart Van Assche | b1a2ecd | 2017-02-01 16:58:35 -0800 | [diff] [blame] | 2501 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2502 | int ret = 0; |
Nicholas Bellinger | c8e6398 | 2015-04-07 21:53:27 +0000 | [diff] [blame] | 2503 | bool zero_flag = !(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2504 | |
Akinobu Mita | 5835812 | 2015-05-01 15:23:49 +0900 | [diff] [blame] | 2505 | if (cmd->prot_op != TARGET_PROT_NORMAL && |
| 2506 | !(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) { |
| 2507 | ret = target_alloc_sgl(&cmd->t_prot_sg, &cmd->t_prot_nents, |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2508 | cmd->prot_length, true, false); |
Akinobu Mita | 5835812 | 2015-05-01 15:23:49 +0900 | [diff] [blame] | 2509 | if (ret < 0) |
| 2510 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 2511 | } |
| 2512 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2513 | /* |
Bart Van Assche | 4240d44 | 2018-10-15 08:51:34 -0700 | [diff] [blame] | 2514 | * Determine if the TCM fabric module has already allocated physical |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2515 | * memory, and is directly calling transport_generic_map_mem_to_cmd() |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2516 | * beforehand. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2517 | */ |
Andy Grover | ec98f78 | 2011-07-20 19:28:46 +0000 | [diff] [blame] | 2518 | if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) && |
| 2519 | cmd->data_length) { |
Nicholas Bellinger | 2009399 | 2013-08-25 15:44:03 -0700 | [diff] [blame] | 2520 | |
Nicholas Bellinger | 8cefe07 | 2013-08-25 16:10:57 -0700 | [diff] [blame] | 2521 | if ((cmd->se_cmd_flags & SCF_BIDI) || |
| 2522 | (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) { |
| 2523 | u32 bidi_length; |
| 2524 | |
| 2525 | if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) |
| 2526 | bidi_length = cmd->t_task_nolb * |
| 2527 | cmd->se_dev->dev_attrib.block_size; |
| 2528 | else |
| 2529 | bidi_length = cmd->data_length; |
| 2530 | |
| 2531 | ret = target_alloc_sgl(&cmd->t_bidi_data_sg, |
| 2532 | &cmd->t_bidi_data_nents, |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2533 | bidi_length, zero_flag, false); |
Nicholas Bellinger | 8cefe07 | 2013-08-25 16:10:57 -0700 | [diff] [blame] | 2534 | if (ret < 0) |
| 2535 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 2536 | } |
| 2537 | |
Nicholas Bellinger | 2009399 | 2013-08-25 15:44:03 -0700 | [diff] [blame] | 2538 | ret = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents, |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2539 | cmd->data_length, zero_flag, false); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2540 | if (ret < 0) |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2541 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c8e6398 | 2015-04-07 21:53:27 +0000 | [diff] [blame] | 2542 | } else if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) && |
| 2543 | cmd->data_length) { |
| 2544 | /* |
| 2545 | * Special case for COMPARE_AND_WRITE with fabrics |
| 2546 | * using SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC. |
| 2547 | */ |
| 2548 | u32 caw_length = cmd->t_task_nolb * |
| 2549 | cmd->se_dev->dev_attrib.block_size; |
| 2550 | |
| 2551 | ret = target_alloc_sgl(&cmd->t_bidi_data_sg, |
| 2552 | &cmd->t_bidi_data_nents, |
Christoph Hellwig | e64aa65 | 2016-05-03 18:01:10 +0200 | [diff] [blame] | 2553 | caw_length, zero_flag, false); |
Nicholas Bellinger | c8e6398 | 2015-04-07 21:53:27 +0000 | [diff] [blame] | 2554 | if (ret < 0) |
| 2555 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2556 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2557 | /* |
Christoph Hellwig | c3196f0 | 2012-07-08 15:58:41 -0400 | [diff] [blame] | 2558 | * If this command is not a write we can execute it right here, |
| 2559 | * for write buffers we need to notify the fabric driver first |
| 2560 | * and let it call back once the write buffers are ready. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2561 | */ |
Christoph Hellwig | 5f41a31 | 2012-05-20 14:34:44 -0400 | [diff] [blame] | 2562 | target_add_to_state_list(cmd); |
Roland Dreier | 885e7b0 | 2014-10-14 14:16:24 -0700 | [diff] [blame] | 2563 | if (cmd->data_direction != DMA_TO_DEVICE || cmd->data_length == 0) { |
Christoph Hellwig | c3196f0 | 2012-07-08 15:58:41 -0400 | [diff] [blame] | 2564 | target_execute_cmd(cmd); |
| 2565 | return 0; |
| 2566 | } |
Bart Van Assche | b1a2ecd | 2017-02-01 16:58:35 -0800 | [diff] [blame] | 2567 | |
| 2568 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 2569 | cmd->t_state = TRANSPORT_WRITE_PENDING; |
| 2570 | /* |
| 2571 | * Determine if frontend context caller is requesting the stopping of |
| 2572 | * this command for frontend exceptions. |
| 2573 | */ |
| 2574 | if (cmd->transport_state & CMD_T_STOP) { |
| 2575 | pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n", |
| 2576 | __func__, __LINE__, cmd->tag); |
| 2577 | |
| 2578 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 2579 | |
| 2580 | complete_all(&cmd->t_transport_stop_comp); |
Bart Van Assche | a5eb307 | 2017-01-09 10:14:54 -0800 | [diff] [blame] | 2581 | return 0; |
Bart Van Assche | b1a2ecd | 2017-02-01 16:58:35 -0800 | [diff] [blame] | 2582 | } |
| 2583 | cmd->transport_state &= ~CMD_T_ACTIVE; |
| 2584 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Christoph Hellwig | c3196f0 | 2012-07-08 15:58:41 -0400 | [diff] [blame] | 2585 | |
| 2586 | ret = cmd->se_tfo->write_pending(cmd); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2587 | if (ret) |
Christoph Hellwig | c3196f0 | 2012-07-08 15:58:41 -0400 | [diff] [blame] | 2588 | goto queue_full; |
| 2589 | |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2590 | return 0; |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 2591 | |
Christoph Hellwig | c3196f0 | 2012-07-08 15:58:41 -0400 | [diff] [blame] | 2592 | queue_full: |
| 2593 | pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2594 | transport_handle_queue_full(cmd, cmd->se_dev, ret, true); |
Christoph Hellwig | c3196f0 | 2012-07-08 15:58:41 -0400 | [diff] [blame] | 2595 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2596 | } |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 2597 | EXPORT_SYMBOL(transport_generic_new_cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2598 | |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 2599 | static void transport_write_pending_qf(struct se_cmd *cmd) |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2600 | { |
Nicholas Bellinger | 9574a49 | 2017-09-29 16:43:11 -0700 | [diff] [blame] | 2601 | unsigned long flags; |
Nicholas Bellinger | f147abb | 2011-10-25 23:57:41 -0700 | [diff] [blame] | 2602 | int ret; |
Nicholas Bellinger | 9574a49 | 2017-09-29 16:43:11 -0700 | [diff] [blame] | 2603 | bool stop; |
| 2604 | |
| 2605 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 2606 | stop = (cmd->transport_state & (CMD_T_STOP | CMD_T_ABORTED)); |
| 2607 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 2608 | |
| 2609 | if (stop) { |
| 2610 | pr_debug("%s:%d CMD_T_STOP|CMD_T_ABORTED for ITT: 0x%08llx\n", |
| 2611 | __func__, __LINE__, cmd->tag); |
| 2612 | complete_all(&cmd->t_transport_stop_comp); |
| 2613 | return; |
| 2614 | } |
Nicholas Bellinger | f147abb | 2011-10-25 23:57:41 -0700 | [diff] [blame] | 2615 | |
| 2616 | ret = cmd->se_tfo->write_pending(cmd); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2617 | if (ret) { |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 2618 | pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", |
| 2619 | cmd); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 2620 | transport_handle_queue_full(cmd, cmd->se_dev, ret, true); |
Christoph Hellwig | e057f53 | 2011-10-17 13:56:41 -0400 | [diff] [blame] | 2621 | } |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 2622 | } |
| 2623 | |
Nicholas Bellinger | 0f4a943 | 2016-01-19 15:23:02 -0800 | [diff] [blame] | 2624 | static bool |
| 2625 | __transport_wait_for_tasks(struct se_cmd *, bool, bool *, bool *, |
| 2626 | unsigned long *flags); |
| 2627 | |
| 2628 | static void target_wait_free_cmd(struct se_cmd *cmd, bool *aborted, bool *tas) |
| 2629 | { |
| 2630 | unsigned long flags; |
| 2631 | |
| 2632 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 2633 | __transport_wait_for_tasks(cmd, true, aborted, tas, &flags); |
| 2634 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 2635 | } |
| 2636 | |
Bart Van Assche | 953bcf7 | 2018-06-22 14:52:54 -0700 | [diff] [blame] | 2637 | /* |
| 2638 | * This function is called by frontend drivers after processing of a command |
| 2639 | * has finished. |
| 2640 | * |
| 2641 | * The protocol for ensuring that either the regular flow or the TMF |
| 2642 | * code drops one reference is as follows: |
| 2643 | * - Calling .queue_data_in(), .queue_status() or queue_tm_rsp() will cause |
| 2644 | * the frontend driver to drop one reference, synchronously or asynchronously. |
| 2645 | * - During regular command processing the target core sets CMD_T_COMPLETE |
| 2646 | * before invoking one of the .queue_*() functions. |
| 2647 | * - The code that aborts commands skips commands and TMFs for which |
| 2648 | * CMD_T_COMPLETE has been set. |
| 2649 | * - CMD_T_ABORTED is set atomically after the CMD_T_COMPLETE check for |
| 2650 | * commands that will be aborted. |
| 2651 | * - If the CMD_T_ABORTED flag is set but CMD_T_TAS has not been set |
| 2652 | * transport_generic_free_cmd() skips its call to target_put_sess_cmd(). |
| 2653 | * - For aborted commands for which CMD_T_TAS has been set .queue_status() will |
| 2654 | * be called and will drop a reference. |
| 2655 | * - For aborted commands for which CMD_T_TAS has not been set .aborted_task() |
| 2656 | * will be called. transport_cmd_finish_abort() will drop the final reference. |
| 2657 | */ |
Nicholas Bellinger | d5ddad416 | 2013-05-31 00:46:11 -0700 | [diff] [blame] | 2658 | int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2659 | { |
Bart Van Assche | 7b2cc7d | 2018-06-22 14:52:59 -0700 | [diff] [blame] | 2660 | DECLARE_COMPLETION_ONSTACK(compl); |
Nicholas Bellinger | d5ddad416 | 2013-05-31 00:46:11 -0700 | [diff] [blame] | 2661 | int ret = 0; |
Nicholas Bellinger | 0f4a943 | 2016-01-19 15:23:02 -0800 | [diff] [blame] | 2662 | bool aborted = false, tas = false; |
Nicholas Bellinger | d5ddad416 | 2013-05-31 00:46:11 -0700 | [diff] [blame] | 2663 | |
Bart Van Assche | edf46ee | 2018-06-22 14:52:58 -0700 | [diff] [blame] | 2664 | if (wait_for_tasks) |
| 2665 | target_wait_free_cmd(cmd, &aborted, &tas); |
| 2666 | |
| 2667 | if (cmd->se_cmd_flags & SCF_SE_LUN_CMD) { |
Nicholas Bellinger | c130480 | 2013-08-31 15:12:01 -0700 | [diff] [blame] | 2668 | /* |
| 2669 | * Handle WRITE failure case where transport_generic_new_cmd() |
| 2670 | * has already added se_cmd to state_list, but fabric has |
| 2671 | * failed command before I/O submission. |
| 2672 | */ |
Nicholas Bellinger | febe562 | 2016-01-11 21:31:09 -0800 | [diff] [blame] | 2673 | if (cmd->state_active) |
Nicholas Bellinger | c130480 | 2013-08-31 15:12:01 -0700 | [diff] [blame] | 2674 | target_remove_from_state_list(cmd); |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 2675 | |
Christoph Hellwig | 82f1c8a | 2011-09-13 23:09:01 +0200 | [diff] [blame] | 2676 | if (cmd->se_lun) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2677 | transport_lun_remove_cmd(cmd); |
Nicholas Bellinger | 0f4a943 | 2016-01-19 15:23:02 -0800 | [diff] [blame] | 2678 | } |
Bart Van Assche | 7b2cc7d | 2018-06-22 14:52:59 -0700 | [diff] [blame] | 2679 | if (aborted) |
| 2680 | cmd->compl = &compl; |
Bart Van Assche | a8864d8 | 2018-06-22 14:52:57 -0700 | [diff] [blame] | 2681 | if (!aborted || tas) |
| 2682 | ret = target_put_sess_cmd(cmd); |
Nicholas Bellinger | 0f4a943 | 2016-01-19 15:23:02 -0800 | [diff] [blame] | 2683 | if (aborted) { |
| 2684 | pr_debug("Detected CMD_T_ABORTED for ITT: %llu\n", cmd->tag); |
Bart Van Assche | 7b2cc7d | 2018-06-22 14:52:59 -0700 | [diff] [blame] | 2685 | wait_for_completion(&compl); |
Nicholas Bellinger | 0f4a943 | 2016-01-19 15:23:02 -0800 | [diff] [blame] | 2686 | ret = 1; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2687 | } |
Nicholas Bellinger | d5ddad416 | 2013-05-31 00:46:11 -0700 | [diff] [blame] | 2688 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2689 | } |
| 2690 | EXPORT_SYMBOL(transport_generic_free_cmd); |
| 2691 | |
Randy Dunlap | 9ad97b8 | 2018-04-14 10:51:04 -0700 | [diff] [blame] | 2692 | /** |
| 2693 | * target_get_sess_cmd - Add command to active ->sess_cmd_list |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2694 | * @se_cmd: command descriptor to add |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 2695 | * @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] | 2696 | */ |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 2697 | int target_get_sess_cmd(struct se_cmd *se_cmd, bool ack_kref) |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2698 | { |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 2699 | struct se_session *se_sess = se_cmd->se_sess; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2700 | unsigned long flags; |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 2701 | int ret = 0; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2702 | |
Nicholas Bellinger | a636078 | 2011-11-18 20:36:22 -0800 | [diff] [blame] | 2703 | /* |
| 2704 | * Add a second kref if the fabric caller is expecting to handle |
| 2705 | * fabric acknowledgement that requires two target_put_sess_cmd() |
| 2706 | * invocations before se_cmd descriptor release. |
| 2707 | */ |
Nicholas Bellinger | 527268d | 2016-10-04 16:37:05 -0700 | [diff] [blame] | 2708 | if (ack_kref) { |
Hannes Reinecke | 1b4c59b | 2016-08-22 10:54:07 +0200 | [diff] [blame] | 2709 | if (!kref_get_unless_zero(&se_cmd->cmd_kref)) |
| 2710 | return -EINVAL; |
| 2711 | |
Nicholas Bellinger | 527268d | 2016-10-04 16:37:05 -0700 | [diff] [blame] | 2712 | se_cmd->se_cmd_flags |= SCF_ACK_KREF; |
| 2713 | } |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2714 | |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2715 | spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 2716 | if (se_sess->sess_tearing_down) { |
| 2717 | ret = -ESHUTDOWN; |
| 2718 | goto out; |
| 2719 | } |
Nicholas Bellinger | 1c21a48 | 2017-10-27 22:19:26 -0800 | [diff] [blame] | 2720 | se_cmd->transport_state |= CMD_T_PRE_EXECUTE; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2721 | list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list); |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 2722 | out: |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2723 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
Bart Van Assche | 7544e597 | 2015-02-18 15:33:58 +0100 | [diff] [blame] | 2724 | |
| 2725 | if (ret && ack_kref) |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 2726 | target_put_sess_cmd(se_cmd); |
Bart Van Assche | 7544e597 | 2015-02-18 15:33:58 +0100 | [diff] [blame] | 2727 | |
Roland Dreier | bc187ea | 2012-07-16 11:04:40 -0700 | [diff] [blame] | 2728 | return ret; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2729 | } |
Nicholas Bellinger | 20361e6 | 2013-03-21 22:54:28 -0700 | [diff] [blame] | 2730 | EXPORT_SYMBOL(target_get_sess_cmd); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2731 | |
Nicholas Bellinger | febe562 | 2016-01-11 21:31:09 -0800 | [diff] [blame] | 2732 | static void target_free_cmd_mem(struct se_cmd *cmd) |
| 2733 | { |
| 2734 | transport_free_pages(cmd); |
| 2735 | |
| 2736 | if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) |
| 2737 | core_tmr_release_req(cmd->se_tmr_req); |
| 2738 | if (cmd->t_task_cdb != cmd->__t_task_cdb) |
| 2739 | kfree(cmd->t_task_cdb); |
| 2740 | } |
| 2741 | |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2742 | static void target_release_cmd_kref(struct kref *kref) |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2743 | { |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2744 | struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref); |
| 2745 | struct se_session *se_sess = se_cmd->se_sess; |
Bart Van Assche | 7b2cc7d | 2018-06-22 14:52:59 -0700 | [diff] [blame] | 2746 | struct completion *compl = se_cmd->compl; |
Bart Van Assche | 9ff9d15 | 2015-10-22 15:57:04 -0700 | [diff] [blame] | 2747 | unsigned long flags; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2748 | |
Bart Van Assche | 6b6427b | 2017-02-07 12:19:16 -0800 | [diff] [blame] | 2749 | if (se_sess) { |
| 2750 | spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); |
Bart Van Assche | 3eeff19 | 2018-06-22 14:52:50 -0700 | [diff] [blame] | 2751 | list_del_init(&se_cmd->se_cmd_list); |
Nicholas Bellinger | 38fe73c | 2018-10-10 03:23:10 +0000 | [diff] [blame] | 2752 | if (se_sess->sess_tearing_down && list_empty(&se_sess->sess_cmd_list)) |
Bart Van Assche | 00d909a | 2018-06-22 14:52:53 -0700 | [diff] [blame] | 2753 | wake_up(&se_sess->cmd_list_wq); |
Bart Van Assche | 9ff9d15 | 2015-10-22 15:57:04 -0700 | [diff] [blame] | 2754 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2755 | } |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2756 | |
Nicholas Bellinger | febe562 | 2016-01-11 21:31:09 -0800 | [diff] [blame] | 2757 | target_free_cmd_mem(se_cmd); |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2758 | se_cmd->se_tfo->release_cmd(se_cmd); |
Bart Van Assche | 7b2cc7d | 2018-06-22 14:52:59 -0700 | [diff] [blame] | 2759 | if (compl) |
| 2760 | complete(compl); |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2761 | } |
| 2762 | |
Bart Van Assche | 6b6427b | 2017-02-07 12:19:16 -0800 | [diff] [blame] | 2763 | /** |
| 2764 | * target_put_sess_cmd - decrease the command reference count |
| 2765 | * @se_cmd: command to drop a reference from |
| 2766 | * |
| 2767 | * Returns 1 if and only if this target_put_sess_cmd() call caused the |
| 2768 | * refcount to drop to zero. Returns zero otherwise. |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2769 | */ |
Bart Van Assche | afc1660 | 2015-04-27 13:52:36 +0200 | [diff] [blame] | 2770 | int target_put_sess_cmd(struct se_cmd *se_cmd) |
Nicholas Bellinger | 7481deb | 2011-11-12 00:32:17 -0800 | [diff] [blame] | 2771 | { |
Bart Van Assche | 9ff9d15 | 2015-10-22 15:57:04 -0700 | [diff] [blame] | 2772 | return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2773 | } |
| 2774 | EXPORT_SYMBOL(target_put_sess_cmd); |
| 2775 | |
Bart Van Assche | c00e622 | 2017-05-23 16:48:28 -0700 | [diff] [blame] | 2776 | static const char *data_dir_name(enum dma_data_direction d) |
| 2777 | { |
| 2778 | switch (d) { |
| 2779 | case DMA_BIDIRECTIONAL: return "BIDI"; |
| 2780 | case DMA_TO_DEVICE: return "WRITE"; |
| 2781 | case DMA_FROM_DEVICE: return "READ"; |
| 2782 | case DMA_NONE: return "NONE"; |
| 2783 | } |
| 2784 | |
| 2785 | return "(?)"; |
| 2786 | } |
| 2787 | |
| 2788 | static const char *cmd_state_name(enum transport_state_table t) |
| 2789 | { |
| 2790 | switch (t) { |
| 2791 | case TRANSPORT_NO_STATE: return "NO_STATE"; |
| 2792 | case TRANSPORT_NEW_CMD: return "NEW_CMD"; |
| 2793 | case TRANSPORT_WRITE_PENDING: return "WRITE_PENDING"; |
| 2794 | case TRANSPORT_PROCESSING: return "PROCESSING"; |
| 2795 | case TRANSPORT_COMPLETE: return "COMPLETE"; |
| 2796 | case TRANSPORT_ISTATE_PROCESSING: |
| 2797 | return "ISTATE_PROCESSING"; |
| 2798 | case TRANSPORT_COMPLETE_QF_WP: return "COMPLETE_QF_WP"; |
| 2799 | case TRANSPORT_COMPLETE_QF_OK: return "COMPLETE_QF_OK"; |
| 2800 | case TRANSPORT_COMPLETE_QF_ERR: return "COMPLETE_QF_ERR"; |
| 2801 | } |
| 2802 | |
| 2803 | return "(?)"; |
| 2804 | } |
| 2805 | |
| 2806 | static void target_append_str(char **str, const char *txt) |
| 2807 | { |
| 2808 | char *prev = *str; |
| 2809 | |
| 2810 | *str = *str ? kasprintf(GFP_ATOMIC, "%s,%s", *str, txt) : |
| 2811 | kstrdup(txt, GFP_ATOMIC); |
| 2812 | kfree(prev); |
| 2813 | } |
| 2814 | |
| 2815 | /* |
| 2816 | * Convert a transport state bitmask into a string. The caller is |
| 2817 | * responsible for freeing the returned pointer. |
| 2818 | */ |
| 2819 | static char *target_ts_to_str(u32 ts) |
| 2820 | { |
| 2821 | char *str = NULL; |
| 2822 | |
| 2823 | if (ts & CMD_T_ABORTED) |
| 2824 | target_append_str(&str, "aborted"); |
| 2825 | if (ts & CMD_T_ACTIVE) |
| 2826 | target_append_str(&str, "active"); |
| 2827 | if (ts & CMD_T_COMPLETE) |
| 2828 | target_append_str(&str, "complete"); |
| 2829 | if (ts & CMD_T_SENT) |
| 2830 | target_append_str(&str, "sent"); |
| 2831 | if (ts & CMD_T_STOP) |
| 2832 | target_append_str(&str, "stop"); |
| 2833 | if (ts & CMD_T_FABRIC_STOP) |
| 2834 | target_append_str(&str, "fabric_stop"); |
| 2835 | |
| 2836 | return str; |
| 2837 | } |
| 2838 | |
| 2839 | static const char *target_tmf_name(enum tcm_tmreq_table tmf) |
| 2840 | { |
| 2841 | switch (tmf) { |
| 2842 | case TMR_ABORT_TASK: return "ABORT_TASK"; |
| 2843 | case TMR_ABORT_TASK_SET: return "ABORT_TASK_SET"; |
| 2844 | case TMR_CLEAR_ACA: return "CLEAR_ACA"; |
| 2845 | case TMR_CLEAR_TASK_SET: return "CLEAR_TASK_SET"; |
| 2846 | case TMR_LUN_RESET: return "LUN_RESET"; |
| 2847 | case TMR_TARGET_WARM_RESET: return "TARGET_WARM_RESET"; |
| 2848 | case TMR_TARGET_COLD_RESET: return "TARGET_COLD_RESET"; |
| 2849 | case TMR_UNKNOWN: break; |
| 2850 | } |
| 2851 | return "(?)"; |
| 2852 | } |
| 2853 | |
| 2854 | void target_show_cmd(const char *pfx, struct se_cmd *cmd) |
| 2855 | { |
| 2856 | char *ts_str = target_ts_to_str(cmd->transport_state); |
| 2857 | const u8 *cdb = cmd->t_task_cdb; |
| 2858 | struct se_tmr_req *tmf = cmd->se_tmr_req; |
| 2859 | |
| 2860 | if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) { |
| 2861 | pr_debug("%scmd %#02x:%#02x with tag %#llx dir %s i_state %d t_state %s len %d refcnt %d transport_state %s\n", |
| 2862 | pfx, cdb[0], cdb[1], cmd->tag, |
| 2863 | data_dir_name(cmd->data_direction), |
| 2864 | cmd->se_tfo->get_cmd_state(cmd), |
| 2865 | cmd_state_name(cmd->t_state), cmd->data_length, |
| 2866 | kref_read(&cmd->cmd_kref), ts_str); |
| 2867 | } else { |
| 2868 | pr_debug("%stmf %s with tag %#llx ref_task_tag %#llx i_state %d t_state %s refcnt %d transport_state %s\n", |
| 2869 | pfx, target_tmf_name(tmf->function), cmd->tag, |
| 2870 | tmf->ref_task_tag, cmd->se_tfo->get_cmd_state(cmd), |
| 2871 | cmd_state_name(cmd->t_state), |
| 2872 | kref_read(&cmd->cmd_kref), ts_str); |
| 2873 | } |
| 2874 | kfree(ts_str); |
| 2875 | } |
| 2876 | EXPORT_SYMBOL(target_show_cmd); |
| 2877 | |
Randy Dunlap | 9ad97b8 | 2018-04-14 10:51:04 -0700 | [diff] [blame] | 2878 | /** |
Bart Van Assche | 00d909a | 2018-06-22 14:52:53 -0700 | [diff] [blame] | 2879 | * target_sess_cmd_list_set_waiting - Set sess_tearing_down so no new commands are queued. |
Roland Dreier | 1c7b13f | 2012-07-16 11:04:42 -0700 | [diff] [blame] | 2880 | * @se_sess: session to flag |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2881 | */ |
Roland Dreier | 1c7b13f | 2012-07-16 11:04:42 -0700 | [diff] [blame] | 2882 | void target_sess_cmd_list_set_waiting(struct se_session *se_sess) |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2883 | { |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2884 | unsigned long flags; |
| 2885 | |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2886 | spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); |
| 2887 | se_sess->sess_tearing_down = 1; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2888 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
| 2889 | } |
Roland Dreier | 1c7b13f | 2012-07-16 11:04:42 -0700 | [diff] [blame] | 2890 | EXPORT_SYMBOL(target_sess_cmd_list_set_waiting); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2891 | |
Randy Dunlap | 9ad97b8 | 2018-04-14 10:51:04 -0700 | [diff] [blame] | 2892 | /** |
Bart Van Assche | 00d909a | 2018-06-22 14:52:53 -0700 | [diff] [blame] | 2893 | * target_wait_for_sess_cmds - Wait for outstanding commands |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2894 | * @se_sess: session to wait for active I/O |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2895 | */ |
Joern Engel | be646c2d | 2013-05-15 00:44:07 -0700 | [diff] [blame] | 2896 | void target_wait_for_sess_cmds(struct se_session *se_sess) |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2897 | { |
Bart Van Assche | 00d909a | 2018-06-22 14:52:53 -0700 | [diff] [blame] | 2898 | struct se_cmd *cmd; |
| 2899 | int ret; |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2900 | |
Bart Van Assche | 00d909a | 2018-06-22 14:52:53 -0700 | [diff] [blame] | 2901 | WARN_ON_ONCE(!se_sess->sess_tearing_down); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2902 | |
Bart Van Assche | 00d909a | 2018-06-22 14:52:53 -0700 | [diff] [blame] | 2903 | spin_lock_irq(&se_sess->sess_cmd_lock); |
| 2904 | do { |
Nicholas Bellinger | 38fe73c | 2018-10-10 03:23:10 +0000 | [diff] [blame] | 2905 | ret = wait_event_lock_irq_timeout( |
Bart Van Assche | 00d909a | 2018-06-22 14:52:53 -0700 | [diff] [blame] | 2906 | se_sess->cmd_list_wq, |
| 2907 | list_empty(&se_sess->sess_cmd_list), |
| 2908 | se_sess->sess_cmd_lock, 180 * HZ); |
| 2909 | list_for_each_entry(cmd, &se_sess->sess_cmd_list, se_cmd_list) |
| 2910 | target_show_cmd("session shutdown: still waiting for ", |
| 2911 | cmd); |
| 2912 | } while (ret <= 0); |
| 2913 | spin_unlock_irq(&se_sess->sess_cmd_lock); |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 2914 | } |
| 2915 | EXPORT_SYMBOL(target_wait_for_sess_cmds); |
| 2916 | |
Nicholas Bellinger | bd4e2d2 | 2017-02-22 22:06:32 -0800 | [diff] [blame] | 2917 | static void target_lun_confirm(struct percpu_ref *ref) |
| 2918 | { |
| 2919 | struct se_lun *lun = container_of(ref, struct se_lun, lun_ref); |
| 2920 | |
| 2921 | complete(&lun->lun_ref_comp); |
| 2922 | } |
| 2923 | |
Bart Van Assche | b3eeea6 | 2015-05-19 16:16:01 +0200 | [diff] [blame] | 2924 | void transport_clear_lun_ref(struct se_lun *lun) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2925 | { |
Nicholas Bellinger | bd4e2d2 | 2017-02-22 22:06:32 -0800 | [diff] [blame] | 2926 | /* |
| 2927 | * Mark the percpu-ref as DEAD, switch to atomic_t mode, drop |
| 2928 | * the initial reference and schedule confirm kill to be |
| 2929 | * executed after one full RCU grace period has completed. |
| 2930 | */ |
| 2931 | percpu_ref_kill_and_confirm(&lun->lun_ref, target_lun_confirm); |
| 2932 | /* |
| 2933 | * The first completion waits for percpu_ref_switch_to_atomic_rcu() |
| 2934 | * to call target_lun_confirm after lun->lun_ref has been marked |
| 2935 | * as __PERCPU_REF_DEAD on all CPUs, and switches to atomic_t |
| 2936 | * mode so that percpu_ref_tryget_live() lookup of lun->lun_ref |
| 2937 | * fails for all new incoming I/O. |
| 2938 | */ |
Nicholas Bellinger | 5277797 | 2013-11-06 21:03:43 -0800 | [diff] [blame] | 2939 | wait_for_completion(&lun->lun_ref_comp); |
Nicholas Bellinger | bd4e2d2 | 2017-02-22 22:06:32 -0800 | [diff] [blame] | 2940 | /* |
| 2941 | * The second completion waits for percpu_ref_put_many() to |
| 2942 | * invoke ->release() after lun->lun_ref has switched to |
| 2943 | * atomic_t mode, and lun->lun_ref.count has reached zero. |
| 2944 | * |
| 2945 | * At this point all target-core lun->lun_ref references have |
| 2946 | * been dropped via transport_lun_remove_cmd(), and it's safe |
| 2947 | * to proceed with the remaining LUN shutdown. |
| 2948 | */ |
| 2949 | wait_for_completion(&lun->lun_shutdown_comp); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 2950 | } |
| 2951 | |
Nicholas Bellinger | 0f4a943 | 2016-01-19 15:23:02 -0800 | [diff] [blame] | 2952 | static bool |
| 2953 | __transport_wait_for_tasks(struct se_cmd *cmd, bool fabric_stop, |
| 2954 | bool *aborted, bool *tas, unsigned long *flags) |
| 2955 | __releases(&cmd->t_state_lock) |
| 2956 | __acquires(&cmd->t_state_lock) |
| 2957 | { |
| 2958 | |
| 2959 | assert_spin_locked(&cmd->t_state_lock); |
| 2960 | WARN_ON_ONCE(!irqs_disabled()); |
| 2961 | |
| 2962 | if (fabric_stop) |
| 2963 | cmd->transport_state |= CMD_T_FABRIC_STOP; |
| 2964 | |
| 2965 | if (cmd->transport_state & CMD_T_ABORTED) |
| 2966 | *aborted = true; |
| 2967 | |
| 2968 | if (cmd->transport_state & CMD_T_TAS) |
| 2969 | *tas = true; |
| 2970 | |
| 2971 | if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && |
| 2972 | !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) |
| 2973 | return false; |
| 2974 | |
| 2975 | if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && |
| 2976 | !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) |
| 2977 | return false; |
| 2978 | |
| 2979 | if (!(cmd->transport_state & CMD_T_ACTIVE)) |
| 2980 | return false; |
| 2981 | |
| 2982 | if (fabric_stop && *aborted) |
| 2983 | return false; |
| 2984 | |
| 2985 | cmd->transport_state |= CMD_T_STOP; |
| 2986 | |
Bart Van Assche | c00e622 | 2017-05-23 16:48:28 -0700 | [diff] [blame] | 2987 | target_show_cmd("wait_for_tasks: Stopping ", cmd); |
Nicholas Bellinger | 0f4a943 | 2016-01-19 15:23:02 -0800 | [diff] [blame] | 2988 | |
| 2989 | spin_unlock_irqrestore(&cmd->t_state_lock, *flags); |
| 2990 | |
Bart Van Assche | c00e622 | 2017-05-23 16:48:28 -0700 | [diff] [blame] | 2991 | while (!wait_for_completion_timeout(&cmd->t_transport_stop_comp, |
| 2992 | 180 * HZ)) |
| 2993 | target_show_cmd("wait for tasks: ", cmd); |
Nicholas Bellinger | 0f4a943 | 2016-01-19 15:23:02 -0800 | [diff] [blame] | 2994 | |
| 2995 | spin_lock_irqsave(&cmd->t_state_lock, *flags); |
| 2996 | cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP); |
| 2997 | |
| 2998 | pr_debug("wait_for_tasks: Stopped wait_for_completion(&cmd->" |
| 2999 | "t_transport_stop_comp) for ITT: 0x%08llx\n", cmd->tag); |
| 3000 | |
| 3001 | return true; |
| 3002 | } |
| 3003 | |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 3004 | /** |
Bart Van Assche | 2d4760e | 2017-01-10 10:03:28 -0800 | [diff] [blame] | 3005 | * transport_wait_for_tasks - set CMD_T_STOP and wait for t_transport_stop_comp |
| 3006 | * @cmd: command to wait on |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3007 | */ |
Nicholas Bellinger | a17f091 | 2011-11-02 21:52:08 -0700 | [diff] [blame] | 3008 | bool transport_wait_for_tasks(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3009 | { |
| 3010 | unsigned long flags; |
Nicholas Bellinger | 0f4a943 | 2016-01-19 15:23:02 -0800 | [diff] [blame] | 3011 | bool ret, aborted = false, tas = false; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3012 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3013 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | 0f4a943 | 2016-01-19 15:23:02 -0800 | [diff] [blame] | 3014 | ret = __transport_wait_for_tasks(cmd, false, &aborted, &tas, &flags); |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3015 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3016 | |
Nicholas Bellinger | 0f4a943 | 2016-01-19 15:23:02 -0800 | [diff] [blame] | 3017 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3018 | } |
Nicholas Bellinger | d14921d | 2011-10-09 01:00:58 -0700 | [diff] [blame] | 3019 | EXPORT_SYMBOL(transport_wait_for_tasks); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3020 | |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3021 | struct sense_info { |
| 3022 | u8 key; |
| 3023 | u8 asc; |
| 3024 | u8 ascq; |
| 3025 | bool add_sector_info; |
| 3026 | }; |
| 3027 | |
| 3028 | static const struct sense_info sense_info_table[] = { |
| 3029 | [TCM_NO_SENSE] = { |
| 3030 | .key = NOT_READY |
| 3031 | }, |
| 3032 | [TCM_NON_EXISTENT_LUN] = { |
| 3033 | .key = ILLEGAL_REQUEST, |
| 3034 | .asc = 0x25 /* LOGICAL UNIT NOT SUPPORTED */ |
| 3035 | }, |
| 3036 | [TCM_UNSUPPORTED_SCSI_OPCODE] = { |
| 3037 | .key = ILLEGAL_REQUEST, |
| 3038 | .asc = 0x20, /* INVALID COMMAND OPERATION CODE */ |
| 3039 | }, |
| 3040 | [TCM_SECTOR_COUNT_TOO_MANY] = { |
| 3041 | .key = ILLEGAL_REQUEST, |
| 3042 | .asc = 0x20, /* INVALID COMMAND OPERATION CODE */ |
| 3043 | }, |
| 3044 | [TCM_UNKNOWN_MODE_PAGE] = { |
| 3045 | .key = ILLEGAL_REQUEST, |
| 3046 | .asc = 0x24, /* INVALID FIELD IN CDB */ |
| 3047 | }, |
| 3048 | [TCM_CHECK_CONDITION_ABORT_CMD] = { |
| 3049 | .key = ABORTED_COMMAND, |
| 3050 | .asc = 0x29, /* BUS DEVICE RESET FUNCTION OCCURRED */ |
| 3051 | .ascq = 0x03, |
| 3052 | }, |
| 3053 | [TCM_INCORRECT_AMOUNT_OF_DATA] = { |
| 3054 | .key = ABORTED_COMMAND, |
| 3055 | .asc = 0x0c, /* WRITE ERROR */ |
| 3056 | .ascq = 0x0d, /* NOT ENOUGH UNSOLICITED DATA */ |
| 3057 | }, |
| 3058 | [TCM_INVALID_CDB_FIELD] = { |
| 3059 | .key = ILLEGAL_REQUEST, |
| 3060 | .asc = 0x24, /* INVALID FIELD IN CDB */ |
| 3061 | }, |
| 3062 | [TCM_INVALID_PARAMETER_LIST] = { |
| 3063 | .key = ILLEGAL_REQUEST, |
| 3064 | .asc = 0x26, /* INVALID FIELD IN PARAMETER LIST */ |
| 3065 | }, |
David Disseldorp | e864212 | 2016-12-23 11:37:53 +0100 | [diff] [blame] | 3066 | [TCM_TOO_MANY_TARGET_DESCS] = { |
| 3067 | .key = ILLEGAL_REQUEST, |
| 3068 | .asc = 0x26, |
| 3069 | .ascq = 0x06, /* TOO MANY TARGET DESCRIPTORS */ |
| 3070 | }, |
| 3071 | [TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE] = { |
| 3072 | .key = ILLEGAL_REQUEST, |
| 3073 | .asc = 0x26, |
| 3074 | .ascq = 0x07, /* UNSUPPORTED TARGET DESCRIPTOR TYPE CODE */ |
| 3075 | }, |
| 3076 | [TCM_TOO_MANY_SEGMENT_DESCS] = { |
| 3077 | .key = ILLEGAL_REQUEST, |
| 3078 | .asc = 0x26, |
| 3079 | .ascq = 0x08, /* TOO MANY SEGMENT DESCRIPTORS */ |
| 3080 | }, |
| 3081 | [TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE] = { |
| 3082 | .key = ILLEGAL_REQUEST, |
| 3083 | .asc = 0x26, |
| 3084 | .ascq = 0x09, /* UNSUPPORTED SEGMENT DESCRIPTOR TYPE CODE */ |
| 3085 | }, |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3086 | [TCM_PARAMETER_LIST_LENGTH_ERROR] = { |
| 3087 | .key = ILLEGAL_REQUEST, |
| 3088 | .asc = 0x1a, /* PARAMETER LIST LENGTH ERROR */ |
| 3089 | }, |
| 3090 | [TCM_UNEXPECTED_UNSOLICITED_DATA] = { |
| 3091 | .key = ILLEGAL_REQUEST, |
| 3092 | .asc = 0x0c, /* WRITE ERROR */ |
| 3093 | .ascq = 0x0c, /* UNEXPECTED_UNSOLICITED_DATA */ |
| 3094 | }, |
| 3095 | [TCM_SERVICE_CRC_ERROR] = { |
| 3096 | .key = ABORTED_COMMAND, |
| 3097 | .asc = 0x47, /* PROTOCOL SERVICE CRC ERROR */ |
| 3098 | .ascq = 0x05, /* N/A */ |
| 3099 | }, |
| 3100 | [TCM_SNACK_REJECTED] = { |
| 3101 | .key = ABORTED_COMMAND, |
| 3102 | .asc = 0x11, /* READ ERROR */ |
| 3103 | .ascq = 0x13, /* FAILED RETRANSMISSION REQUEST */ |
| 3104 | }, |
| 3105 | [TCM_WRITE_PROTECTED] = { |
| 3106 | .key = DATA_PROTECT, |
| 3107 | .asc = 0x27, /* WRITE PROTECTED */ |
| 3108 | }, |
| 3109 | [TCM_ADDRESS_OUT_OF_RANGE] = { |
| 3110 | .key = ILLEGAL_REQUEST, |
| 3111 | .asc = 0x21, /* LOGICAL BLOCK ADDRESS OUT OF RANGE */ |
| 3112 | }, |
| 3113 | [TCM_CHECK_CONDITION_UNIT_ATTENTION] = { |
| 3114 | .key = UNIT_ATTENTION, |
| 3115 | }, |
| 3116 | [TCM_CHECK_CONDITION_NOT_READY] = { |
| 3117 | .key = NOT_READY, |
| 3118 | }, |
| 3119 | [TCM_MISCOMPARE_VERIFY] = { |
| 3120 | .key = MISCOMPARE, |
| 3121 | .asc = 0x1d, /* MISCOMPARE DURING VERIFY OPERATION */ |
| 3122 | .ascq = 0x00, |
| 3123 | }, |
| 3124 | [TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED] = { |
Sagi Grimberg | 734ca5c | 2015-07-08 17:58:54 +0300 | [diff] [blame] | 3125 | .key = ABORTED_COMMAND, |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3126 | .asc = 0x10, |
| 3127 | .ascq = 0x01, /* LOGICAL BLOCK GUARD CHECK FAILED */ |
| 3128 | .add_sector_info = true, |
| 3129 | }, |
| 3130 | [TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED] = { |
Sagi Grimberg | 734ca5c | 2015-07-08 17:58:54 +0300 | [diff] [blame] | 3131 | .key = ABORTED_COMMAND, |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3132 | .asc = 0x10, |
| 3133 | .ascq = 0x02, /* LOGICAL BLOCK APPLICATION TAG CHECK FAILED */ |
| 3134 | .add_sector_info = true, |
| 3135 | }, |
| 3136 | [TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED] = { |
Sagi Grimberg | 734ca5c | 2015-07-08 17:58:54 +0300 | [diff] [blame] | 3137 | .key = ABORTED_COMMAND, |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3138 | .asc = 0x10, |
| 3139 | .ascq = 0x03, /* LOGICAL BLOCK REFERENCE TAG CHECK FAILED */ |
| 3140 | .add_sector_info = true, |
| 3141 | }, |
Nicholas Bellinger | 449a137 | 2016-10-08 17:26:44 -0700 | [diff] [blame] | 3142 | [TCM_COPY_TARGET_DEVICE_NOT_REACHABLE] = { |
| 3143 | .key = COPY_ABORTED, |
| 3144 | .asc = 0x0d, |
| 3145 | .ascq = 0x02, /* COPY TARGET DEVICE NOT REACHABLE */ |
| 3146 | |
| 3147 | }, |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3148 | [TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE] = { |
| 3149 | /* |
| 3150 | * Returning ILLEGAL REQUEST would cause immediate IO errors on |
| 3151 | * Solaris initiators. Returning NOT READY instead means the |
| 3152 | * operations will be retried a finite number of times and we |
| 3153 | * can survive intermittent errors. |
| 3154 | */ |
| 3155 | .key = NOT_READY, |
| 3156 | .asc = 0x08, /* LOGICAL UNIT COMMUNICATION FAILURE */ |
| 3157 | }, |
tangwenji | 79dd6f2 | 2017-08-21 20:55:41 +0800 | [diff] [blame] | 3158 | [TCM_INSUFFICIENT_REGISTRATION_RESOURCES] = { |
| 3159 | /* |
| 3160 | * From spc4r22 section5.7.7,5.7.8 |
| 3161 | * If a PERSISTENT RESERVE OUT command with a REGISTER service action |
| 3162 | * or a REGISTER AND IGNORE EXISTING KEY service action or |
| 3163 | * REGISTER AND MOVE service actionis attempted, |
| 3164 | * but there are insufficient device server resources to complete the |
| 3165 | * operation, then the command shall be terminated with CHECK CONDITION |
| 3166 | * status, with the sense key set to ILLEGAL REQUEST,and the additonal |
| 3167 | * sense code set to INSUFFICIENT REGISTRATION RESOURCES. |
| 3168 | */ |
| 3169 | .key = ILLEGAL_REQUEST, |
| 3170 | .asc = 0x55, |
| 3171 | .ascq = 0x04, /* INSUFFICIENT REGISTRATION RESOURCES */ |
| 3172 | }, |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3173 | }; |
| 3174 | |
Bart Van Assche | 325c1e8 | 2018-06-22 14:53:06 -0700 | [diff] [blame] | 3175 | /** |
| 3176 | * translate_sense_reason - translate a sense reason into T10 key, asc and ascq |
| 3177 | * @cmd: SCSI command in which the resulting sense buffer or SCSI status will |
| 3178 | * be stored. |
| 3179 | * @reason: LIO sense reason code. If this argument has the value |
| 3180 | * TCM_CHECK_CONDITION_UNIT_ATTENTION, try to dequeue a unit attention. If |
| 3181 | * dequeuing a unit attention fails due to multiple commands being processed |
| 3182 | * concurrently, set the command status to BUSY. |
| 3183 | * |
| 3184 | * Return: 0 upon success or -EINVAL if the sense buffer is too small. |
| 3185 | */ |
Bart Van Assche | 17e391d | 2018-06-22 14:53:05 -0700 | [diff] [blame] | 3186 | static void translate_sense_reason(struct se_cmd *cmd, sense_reason_t reason) |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3187 | { |
| 3188 | const struct sense_info *si; |
| 3189 | u8 *buffer = cmd->sense_buffer; |
| 3190 | int r = (__force int)reason; |
Bart Van Assche | 325c1e8 | 2018-06-22 14:53:06 -0700 | [diff] [blame] | 3191 | u8 key, asc, ascq; |
Sagi Grimberg | 4e4937e | 2015-07-16 10:28:05 +0300 | [diff] [blame] | 3192 | bool desc_format = target_sense_desc_format(cmd->se_dev); |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3193 | |
| 3194 | if (r < ARRAY_SIZE(sense_info_table) && sense_info_table[r].key) |
| 3195 | si = &sense_info_table[r]; |
| 3196 | else |
| 3197 | si = &sense_info_table[(__force int) |
| 3198 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE]; |
| 3199 | |
Bart Van Assche | 325c1e8 | 2018-06-22 14:53:06 -0700 | [diff] [blame] | 3200 | key = si->key; |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3201 | if (reason == TCM_CHECK_CONDITION_UNIT_ATTENTION) { |
Bart Van Assche | 325c1e8 | 2018-06-22 14:53:06 -0700 | [diff] [blame] | 3202 | if (!core_scsi3_ua_for_check_condition(cmd, &key, &asc, |
| 3203 | &ascq)) { |
| 3204 | cmd->scsi_status = SAM_STAT_BUSY; |
| 3205 | return; |
| 3206 | } |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3207 | } else if (si->asc == 0) { |
| 3208 | WARN_ON_ONCE(cmd->scsi_asc == 0); |
| 3209 | asc = cmd->scsi_asc; |
| 3210 | ascq = cmd->scsi_ascq; |
| 3211 | } else { |
| 3212 | asc = si->asc; |
| 3213 | ascq = si->ascq; |
| 3214 | } |
Sagi Grimberg | 9ec1e1c | 2015-07-08 17:58:53 +0300 | [diff] [blame] | 3215 | |
Bart Van Assche | 89a104e | 2018-06-22 14:53:04 -0700 | [diff] [blame] | 3216 | cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE; |
| 3217 | cmd->scsi_status = SAM_STAT_CHECK_CONDITION; |
| 3218 | cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER; |
Bart Van Assche | 325c1e8 | 2018-06-22 14:53:06 -0700 | [diff] [blame] | 3219 | scsi_build_sense_buffer(desc_format, buffer, key, asc, ascq); |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3220 | if (si->add_sector_info) |
Bart Van Assche | 17e391d | 2018-06-22 14:53:05 -0700 | [diff] [blame] | 3221 | WARN_ON_ONCE(scsi_set_sense_information(buffer, |
| 3222 | cmd->scsi_sense_length, |
| 3223 | cmd->bad_sector) < 0); |
Bart Van Assche | ab78fef | 2015-07-08 17:58:51 +0300 | [diff] [blame] | 3224 | } |
| 3225 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 3226 | int |
| 3227 | transport_send_check_condition_and_sense(struct se_cmd *cmd, |
| 3228 | sense_reason_t reason, int from_transport) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3229 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3230 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3231 | |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3232 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3233 | if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3234 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3235 | return 0; |
| 3236 | } |
| 3237 | cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 3238 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3239 | |
Bart Van Assche | 17e391d | 2018-06-22 14:53:05 -0700 | [diff] [blame] | 3240 | if (!from_transport) |
| 3241 | translate_sense_reason(cmd, reason); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3242 | |
Roland Dreier | e5c0d6a | 2013-06-26 17:36:17 -0700 | [diff] [blame] | 3243 | trace_target_cmd_complete(cmd); |
Nicholas Bellinger | 07bde79 | 2011-06-13 14:46:09 -0700 | [diff] [blame] | 3244 | return cmd->se_tfo->queue_status(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3245 | } |
| 3246 | EXPORT_SYMBOL(transport_send_check_condition_and_sense); |
| 3247 | |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 3248 | static int __transport_check_aborted_status(struct se_cmd *cmd, int send_status) |
| 3249 | __releases(&cmd->t_state_lock) |
| 3250 | __acquires(&cmd->t_state_lock) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3251 | { |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 3252 | int ret; |
| 3253 | |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 3254 | assert_spin_locked(&cmd->t_state_lock); |
| 3255 | WARN_ON_ONCE(!irqs_disabled()); |
| 3256 | |
Roland Dreier | c18bc7d | 2012-11-16 08:06:19 -0800 | [diff] [blame] | 3257 | if (!(cmd->transport_state & CMD_T_ABORTED)) |
| 3258 | return 0; |
Alex Leung | 68259b5 | 2014-03-21 22:20:41 -0700 | [diff] [blame] | 3259 | /* |
| 3260 | * If cmd has been aborted but either no status is to be sent or it has |
| 3261 | * already been sent, just return |
| 3262 | */ |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 3263 | if (!send_status || !(cmd->se_cmd_flags & SCF_SEND_DELAYED_TAS)) { |
| 3264 | if (send_status) |
| 3265 | cmd->se_cmd_flags |= SCF_SEND_DELAYED_TAS; |
Roland Dreier | c18bc7d | 2012-11-16 08:06:19 -0800 | [diff] [blame] | 3266 | return 1; |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 3267 | } |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3268 | |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 3269 | pr_debug("Sending delayed SAM_STAT_TASK_ABORTED status for CDB:" |
| 3270 | " 0x%02x ITT: 0x%08llx\n", cmd->t_task_cdb[0], cmd->tag); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3271 | |
Alex Leung | 68259b5 | 2014-03-21 22:20:41 -0700 | [diff] [blame] | 3272 | cmd->se_cmd_flags &= ~SCF_SEND_DELAYED_TAS; |
Nicholas Bellinger | 29f4c09 | 2013-11-13 14:39:14 -0800 | [diff] [blame] | 3273 | cmd->scsi_status = SAM_STAT_TASK_ABORTED; |
Roland Dreier | e5c0d6a | 2013-06-26 17:36:17 -0700 | [diff] [blame] | 3274 | trace_target_cmd_complete(cmd); |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 3275 | |
| 3276 | spin_unlock_irq(&cmd->t_state_lock); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 3277 | ret = cmd->se_tfo->queue_status(cmd); |
| 3278 | if (ret) |
| 3279 | transport_handle_queue_full(cmd, cmd->se_dev, ret, false); |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 3280 | spin_lock_irq(&cmd->t_state_lock); |
Roland Dreier | c18bc7d | 2012-11-16 08:06:19 -0800 | [diff] [blame] | 3281 | |
| 3282 | return 1; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3283 | } |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 3284 | |
| 3285 | int transport_check_aborted_status(struct se_cmd *cmd, int send_status) |
| 3286 | { |
| 3287 | int ret; |
| 3288 | |
| 3289 | spin_lock_irq(&cmd->t_state_lock); |
| 3290 | ret = __transport_check_aborted_status(cmd, send_status); |
| 3291 | spin_unlock_irq(&cmd->t_state_lock); |
| 3292 | |
| 3293 | return ret; |
| 3294 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3295 | EXPORT_SYMBOL(transport_check_aborted_status); |
| 3296 | |
| 3297 | void transport_send_task_abort(struct se_cmd *cmd) |
| 3298 | { |
Nicholas Bellinger | c252f00 | 2011-09-29 14:22:13 -0700 | [diff] [blame] | 3299 | unsigned long flags; |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 3300 | int ret; |
Nicholas Bellinger | c252f00 | 2011-09-29 14:22:13 -0700 | [diff] [blame] | 3301 | |
| 3302 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Alex Leung | 68259b5 | 2014-03-21 22:20:41 -0700 | [diff] [blame] | 3303 | if (cmd->se_cmd_flags & (SCF_SENT_CHECK_CONDITION)) { |
Nicholas Bellinger | c252f00 | 2011-09-29 14:22:13 -0700 | [diff] [blame] | 3304 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 3305 | return; |
| 3306 | } |
| 3307 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 3308 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3309 | /* |
| 3310 | * If there are still expected incoming fabric WRITEs, we wait |
| 3311 | * until until they have completed before sending a TASK_ABORTED |
| 3312 | * response. This response with TASK_ABORTED status will be |
| 3313 | * queued back to fabric module by transport_check_aborted_status(). |
| 3314 | */ |
| 3315 | if (cmd->data_direction == DMA_TO_DEVICE) { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3316 | if (cmd->se_tfo->write_pending_status(cmd) != 0) { |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 3317 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 3318 | if (cmd->se_cmd_flags & SCF_SEND_DELAYED_TAS) { |
| 3319 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 3320 | goto send_abort; |
| 3321 | } |
Alex Leung | 68259b5 | 2014-03-21 22:20:41 -0700 | [diff] [blame] | 3322 | cmd->se_cmd_flags |= SCF_SEND_DELAYED_TAS; |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 3323 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
Nicholas Bellinger | 29f4c09 | 2013-11-13 14:39:14 -0800 | [diff] [blame] | 3324 | return; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3325 | } |
| 3326 | } |
Nicholas Bellinger | 310d3d3 | 2016-02-05 14:51:36 -0800 | [diff] [blame] | 3327 | send_abort: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3328 | cmd->scsi_status = SAM_STAT_TASK_ABORTED; |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3329 | |
Roland Dreier | 72b59d6e | 2013-01-02 12:47:58 -0800 | [diff] [blame] | 3330 | transport_lun_remove_cmd(cmd); |
| 3331 | |
Bart Van Assche | 649ee05 | 2015-04-14 13:26:44 +0200 | [diff] [blame] | 3332 | pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x, ITT: 0x%08llx\n", |
| 3333 | cmd->t_task_cdb[0], cmd->tag); |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3334 | |
Roland Dreier | e5c0d6a | 2013-06-26 17:36:17 -0700 | [diff] [blame] | 3335 | trace_target_cmd_complete(cmd); |
Nicholas Bellinger | fa7e25c | 2016-10-30 17:28:16 -0700 | [diff] [blame] | 3336 | ret = cmd->se_tfo->queue_status(cmd); |
| 3337 | if (ret) |
| 3338 | transport_handle_queue_full(cmd, cmd->se_dev, ret, false); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3339 | } |
| 3340 | |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 3341 | static void target_tmr_work(struct work_struct *work) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3342 | { |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 3343 | struct se_cmd *cmd = container_of(work, struct se_cmd, work); |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 3344 | struct se_device *dev = cmd->se_dev; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3345 | struct se_tmr_req *tmr = cmd->se_tmr_req; |
Nicholas Bellinger | a6d9bb1 | 2016-01-11 21:53:05 -0800 | [diff] [blame] | 3346 | unsigned long flags; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3347 | int ret; |
| 3348 | |
Nicholas Bellinger | a6d9bb1 | 2016-01-11 21:53:05 -0800 | [diff] [blame] | 3349 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 3350 | if (cmd->transport_state & CMD_T_ABORTED) { |
| 3351 | tmr->response = TMR_FUNCTION_REJECTED; |
| 3352 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 3353 | goto check_stop; |
| 3354 | } |
| 3355 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 3356 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3357 | switch (tmr->function) { |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 3358 | case TMR_ABORT_TASK: |
Nicholas Bellinger | 3d28934 | 2012-02-13 02:38:14 -0800 | [diff] [blame] | 3359 | core_tmr_abort_task(dev, tmr, cmd->se_sess); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3360 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 3361 | case TMR_ABORT_TASK_SET: |
| 3362 | case TMR_CLEAR_ACA: |
| 3363 | case TMR_CLEAR_TASK_SET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3364 | tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED; |
| 3365 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 3366 | case TMR_LUN_RESET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3367 | ret = core_tmr_lun_reset(dev, tmr, NULL, NULL); |
| 3368 | tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE : |
| 3369 | TMR_FUNCTION_REJECTED; |
Hannes Reinecke | b5aafb1 | 2015-06-11 10:01:28 +0200 | [diff] [blame] | 3370 | if (tmr->response == TMR_FUNCTION_COMPLETE) { |
| 3371 | target_ua_allocate_lun(cmd->se_sess->se_node_acl, |
| 3372 | cmd->orig_fe_lun, 0x29, |
| 3373 | ASCQ_29H_BUS_DEVICE_RESET_FUNCTION_OCCURRED); |
| 3374 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3375 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 3376 | case TMR_TARGET_WARM_RESET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3377 | tmr->response = TMR_FUNCTION_REJECTED; |
| 3378 | break; |
Nicholas Bellinger | 5c6cd61 | 2011-03-14 04:06:04 -0700 | [diff] [blame] | 3379 | case TMR_TARGET_COLD_RESET: |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3380 | tmr->response = TMR_FUNCTION_REJECTED; |
| 3381 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3382 | default: |
Colin Ian King | c167828 | 2018-05-26 16:02:29 +0100 | [diff] [blame] | 3383 | pr_err("Unknown TMR function: 0x%02x.\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3384 | tmr->function); |
| 3385 | tmr->response = TMR_FUNCTION_REJECTED; |
| 3386 | break; |
| 3387 | } |
| 3388 | |
Nicholas Bellinger | a6d9bb1 | 2016-01-11 21:53:05 -0800 | [diff] [blame] | 3389 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
| 3390 | if (cmd->transport_state & CMD_T_ABORTED) { |
| 3391 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 3392 | goto check_stop; |
| 3393 | } |
Nicholas Bellinger | a6d9bb1 | 2016-01-11 21:53:05 -0800 | [diff] [blame] | 3394 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 3395 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 3396 | cmd->se_tfo->queue_tm_rsp(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3397 | |
Nicholas Bellinger | a6d9bb1 | 2016-01-11 21:53:05 -0800 | [diff] [blame] | 3398 | check_stop: |
Nicholas Bellinger | eeb64d2 | 2017-06-03 06:41:03 -0700 | [diff] [blame] | 3399 | transport_lun_remove_cmd(cmd); |
Christoph Hellwig | b7b8bef | 2011-10-17 13:56:44 -0400 | [diff] [blame] | 3400 | transport_cmd_check_stop_to_fabric(cmd); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3401 | } |
| 3402 | |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 3403 | int transport_generic_handle_tmr( |
| 3404 | struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3405 | { |
Nicholas Bellinger | f15e9cd | 2014-06-09 23:13:20 +0000 | [diff] [blame] | 3406 | unsigned long flags; |
Nicholas Bellinger | c54eeff | 2016-12-06 22:45:46 -0800 | [diff] [blame] | 3407 | bool aborted = false; |
Nicholas Bellinger | f15e9cd | 2014-06-09 23:13:20 +0000 | [diff] [blame] | 3408 | |
| 3409 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
Nicholas Bellinger | c54eeff | 2016-12-06 22:45:46 -0800 | [diff] [blame] | 3410 | if (cmd->transport_state & CMD_T_ABORTED) { |
| 3411 | aborted = true; |
| 3412 | } else { |
| 3413 | cmd->t_state = TRANSPORT_ISTATE_PROCESSING; |
| 3414 | cmd->transport_state |= CMD_T_ACTIVE; |
| 3415 | } |
Nicholas Bellinger | f15e9cd | 2014-06-09 23:13:20 +0000 | [diff] [blame] | 3416 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
| 3417 | |
Nicholas Bellinger | c54eeff | 2016-12-06 22:45:46 -0800 | [diff] [blame] | 3418 | if (aborted) { |
| 3419 | pr_warn_ratelimited("handle_tmr caught CMD_T_ABORTED TMR %d" |
| 3420 | "ref_tag: %llu tag: %llu\n", cmd->se_tmr_req->function, |
| 3421 | cmd->se_tmr_req->ref_task_tag, cmd->tag); |
Nicholas Bellinger | eeb64d2 | 2017-06-03 06:41:03 -0700 | [diff] [blame] | 3422 | transport_lun_remove_cmd(cmd); |
Nicholas Bellinger | c54eeff | 2016-12-06 22:45:46 -0800 | [diff] [blame] | 3423 | transport_cmd_check_stop_to_fabric(cmd); |
| 3424 | return 0; |
| 3425 | } |
| 3426 | |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 3427 | INIT_WORK(&cmd->work, target_tmr_work); |
| 3428 | queue_work(cmd->se_dev->tmr_wq, &cmd->work); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 3429 | return 0; |
| 3430 | } |
Christoph Hellwig | af87729 | 2012-07-08 15:58:49 -0400 | [diff] [blame] | 3431 | EXPORT_SYMBOL(transport_generic_handle_tmr); |
Christoph Hellwig | 814e5b4 | 2015-04-20 15:00:30 +0200 | [diff] [blame] | 3432 | |
| 3433 | bool |
| 3434 | target_check_wce(struct se_device *dev) |
| 3435 | { |
| 3436 | bool wce = false; |
| 3437 | |
| 3438 | if (dev->transport->get_write_cache) |
| 3439 | wce = dev->transport->get_write_cache(dev); |
| 3440 | else if (dev->dev_attrib.emulate_write_cache > 0) |
| 3441 | wce = true; |
| 3442 | |
| 3443 | return wce; |
| 3444 | } |
| 3445 | |
| 3446 | bool |
| 3447 | target_check_fua(struct se_device *dev) |
| 3448 | { |
| 3449 | return target_check_wce(dev) && dev->dev_attrib.emulate_fua_write > 0; |
| 3450 | } |