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