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