Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * This file contains main functions related to the iSCSI Target Core Driver. |
| 3 | * |
Nicholas Bellinger | 4c76251 | 2013-09-05 15:29:12 -0700 | [diff] [blame] | 4 | * (c) Copyright 2007-2013 Datera, Inc. |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 5 | * |
| 6 | * Author: Nicholas A. Bellinger <nab@linux-iscsi.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | ******************************************************************************/ |
| 18 | |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/kthread.h> |
| 21 | #include <linux/crypto.h> |
| 22 | #include <linux/completion.h> |
Paul Gortmaker | 827509e | 2011-08-30 14:20:44 -0400 | [diff] [blame] | 23 | #include <linux/module.h> |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 24 | #include <linux/idr.h> |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 25 | #include <asm/unaligned.h> |
| 26 | #include <scsi/scsi_device.h> |
| 27 | #include <scsi/iscsi_proto.h> |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 28 | #include <scsi/scsi_tcq.h> |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 29 | #include <target/target_core_base.h> |
Christoph Hellwig | c4795fb | 2011-11-16 09:46:48 -0500 | [diff] [blame] | 30 | #include <target/target_core_fabric.h> |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 31 | #include <target/target_core_configfs.h> |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 32 | |
Sagi Grimberg | 67f091f | 2015-01-07 14:57:31 +0200 | [diff] [blame] | 33 | #include <target/iscsi/iscsi_target_core.h> |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 34 | #include "iscsi_target_parameters.h" |
| 35 | #include "iscsi_target_seq_pdu_list.h" |
| 36 | #include "iscsi_target_tq.h" |
| 37 | #include "iscsi_target_configfs.h" |
| 38 | #include "iscsi_target_datain_values.h" |
| 39 | #include "iscsi_target_erl0.h" |
| 40 | #include "iscsi_target_erl1.h" |
| 41 | #include "iscsi_target_erl2.h" |
| 42 | #include "iscsi_target_login.h" |
| 43 | #include "iscsi_target_tmr.h" |
| 44 | #include "iscsi_target_tpg.h" |
| 45 | #include "iscsi_target_util.h" |
| 46 | #include "iscsi_target.h" |
| 47 | #include "iscsi_target_device.h" |
Sagi Grimberg | 67f091f | 2015-01-07 14:57:31 +0200 | [diff] [blame] | 48 | #include <target/iscsi/iscsi_target_stat.h> |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 49 | |
Nicholas Bellinger | baa4d64 | 2013-03-06 21:54:13 -0800 | [diff] [blame] | 50 | #include <target/iscsi/iscsi_transport.h> |
| 51 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 52 | static LIST_HEAD(g_tiqn_list); |
| 53 | static LIST_HEAD(g_np_list); |
| 54 | static DEFINE_SPINLOCK(tiqn_lock); |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 55 | static DEFINE_MUTEX(np_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 56 | |
| 57 | static struct idr tiqn_idr; |
| 58 | struct idr sess_idr; |
| 59 | struct mutex auth_id_lock; |
| 60 | spinlock_t sess_idr_lock; |
| 61 | |
| 62 | struct iscsit_global *iscsit_global; |
| 63 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 64 | struct kmem_cache *lio_qr_cache; |
| 65 | struct kmem_cache *lio_dr_cache; |
| 66 | struct kmem_cache *lio_ooo_cache; |
| 67 | struct kmem_cache *lio_r2t_cache; |
| 68 | |
| 69 | static int iscsit_handle_immediate_data(struct iscsi_cmd *, |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 70 | struct iscsi_scsi_req *, u32); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 71 | |
| 72 | struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf) |
| 73 | { |
| 74 | struct iscsi_tiqn *tiqn = NULL; |
| 75 | |
| 76 | spin_lock(&tiqn_lock); |
| 77 | list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) { |
| 78 | if (!strcmp(tiqn->tiqn, buf)) { |
| 79 | |
| 80 | spin_lock(&tiqn->tiqn_state_lock); |
| 81 | if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) { |
| 82 | tiqn->tiqn_access_count++; |
| 83 | spin_unlock(&tiqn->tiqn_state_lock); |
| 84 | spin_unlock(&tiqn_lock); |
| 85 | return tiqn; |
| 86 | } |
| 87 | spin_unlock(&tiqn->tiqn_state_lock); |
| 88 | } |
| 89 | } |
| 90 | spin_unlock(&tiqn_lock); |
| 91 | |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn) |
| 96 | { |
| 97 | spin_lock(&tiqn->tiqn_state_lock); |
| 98 | if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) { |
| 99 | tiqn->tiqn_state = TIQN_STATE_SHUTDOWN; |
| 100 | spin_unlock(&tiqn->tiqn_state_lock); |
| 101 | return 0; |
| 102 | } |
| 103 | spin_unlock(&tiqn->tiqn_state_lock); |
| 104 | |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn) |
| 109 | { |
| 110 | spin_lock(&tiqn->tiqn_state_lock); |
| 111 | tiqn->tiqn_access_count--; |
| 112 | spin_unlock(&tiqn->tiqn_state_lock); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Note that IQN formatting is expected to be done in userspace, and |
| 117 | * no explict IQN format checks are done here. |
| 118 | */ |
| 119 | struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf) |
| 120 | { |
| 121 | struct iscsi_tiqn *tiqn = NULL; |
| 122 | int ret; |
| 123 | |
Dan Carpenter | 8f50c7f | 2011-07-27 14:11:43 +0300 | [diff] [blame] | 124 | if (strlen(buf) >= ISCSI_IQN_LEN) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 125 | pr_err("Target IQN exceeds %d bytes\n", |
| 126 | ISCSI_IQN_LEN); |
| 127 | return ERR_PTR(-EINVAL); |
| 128 | } |
| 129 | |
| 130 | tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL); |
| 131 | if (!tiqn) { |
| 132 | pr_err("Unable to allocate struct iscsi_tiqn\n"); |
| 133 | return ERR_PTR(-ENOMEM); |
| 134 | } |
| 135 | |
| 136 | sprintf(tiqn->tiqn, "%s", buf); |
| 137 | INIT_LIST_HEAD(&tiqn->tiqn_list); |
| 138 | INIT_LIST_HEAD(&tiqn->tiqn_tpg_list); |
| 139 | spin_lock_init(&tiqn->tiqn_state_lock); |
| 140 | spin_lock_init(&tiqn->tiqn_tpg_lock); |
| 141 | spin_lock_init(&tiqn->sess_err_stats.lock); |
| 142 | spin_lock_init(&tiqn->login_stats.lock); |
| 143 | spin_lock_init(&tiqn->logout_stats.lock); |
| 144 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 145 | tiqn->tiqn_state = TIQN_STATE_ACTIVE; |
| 146 | |
Tejun Heo | c9365bd | 2013-02-27 17:04:43 -0800 | [diff] [blame] | 147 | idr_preload(GFP_KERNEL); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 148 | spin_lock(&tiqn_lock); |
Tejun Heo | c9365bd | 2013-02-27 17:04:43 -0800 | [diff] [blame] | 149 | |
| 150 | ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 151 | if (ret < 0) { |
Tejun Heo | c9365bd | 2013-02-27 17:04:43 -0800 | [diff] [blame] | 152 | pr_err("idr_alloc() failed for tiqn->tiqn_index\n"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 153 | spin_unlock(&tiqn_lock); |
Tejun Heo | c9365bd | 2013-02-27 17:04:43 -0800 | [diff] [blame] | 154 | idr_preload_end(); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 155 | kfree(tiqn); |
| 156 | return ERR_PTR(ret); |
| 157 | } |
Tejun Heo | c9365bd | 2013-02-27 17:04:43 -0800 | [diff] [blame] | 158 | tiqn->tiqn_index = ret; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 159 | list_add_tail(&tiqn->tiqn_list, &g_tiqn_list); |
Tejun Heo | c9365bd | 2013-02-27 17:04:43 -0800 | [diff] [blame] | 160 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 161 | spin_unlock(&tiqn_lock); |
Tejun Heo | c9365bd | 2013-02-27 17:04:43 -0800 | [diff] [blame] | 162 | idr_preload_end(); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 163 | |
| 164 | pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn); |
| 165 | |
| 166 | return tiqn; |
| 167 | |
| 168 | } |
| 169 | |
| 170 | static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn) |
| 171 | { |
| 172 | /* |
| 173 | * Wait for accesses to said struct iscsi_tiqn to end. |
| 174 | */ |
| 175 | spin_lock(&tiqn->tiqn_state_lock); |
| 176 | while (tiqn->tiqn_access_count != 0) { |
| 177 | spin_unlock(&tiqn->tiqn_state_lock); |
| 178 | msleep(10); |
| 179 | spin_lock(&tiqn->tiqn_state_lock); |
| 180 | } |
| 181 | spin_unlock(&tiqn->tiqn_state_lock); |
| 182 | } |
| 183 | |
| 184 | void iscsit_del_tiqn(struct iscsi_tiqn *tiqn) |
| 185 | { |
| 186 | /* |
| 187 | * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN |
| 188 | * while holding tiqn->tiqn_state_lock. This means that all subsequent |
| 189 | * attempts to access this struct iscsi_tiqn will fail from both transport |
| 190 | * fabric and control code paths. |
| 191 | */ |
| 192 | if (iscsit_set_tiqn_shutdown(tiqn) < 0) { |
| 193 | pr_err("iscsit_set_tiqn_shutdown() failed\n"); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | iscsit_wait_for_tiqn(tiqn); |
| 198 | |
| 199 | spin_lock(&tiqn_lock); |
| 200 | list_del(&tiqn->tiqn_list); |
| 201 | idr_remove(&tiqn_idr, tiqn->tiqn_index); |
| 202 | spin_unlock(&tiqn_lock); |
| 203 | |
| 204 | pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n", |
| 205 | tiqn->tiqn); |
| 206 | kfree(tiqn); |
| 207 | } |
| 208 | |
| 209 | int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg) |
| 210 | { |
| 211 | int ret; |
| 212 | /* |
| 213 | * Determine if the network portal is accepting storage traffic. |
| 214 | */ |
| 215 | spin_lock_bh(&np->np_thread_lock); |
| 216 | if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) { |
| 217 | spin_unlock_bh(&np->np_thread_lock); |
| 218 | return -1; |
| 219 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 220 | spin_unlock_bh(&np->np_thread_lock); |
| 221 | /* |
| 222 | * Determine if the portal group is accepting storage traffic. |
| 223 | */ |
| 224 | spin_lock_bh(&tpg->tpg_state_lock); |
| 225 | if (tpg->tpg_state != TPG_STATE_ACTIVE) { |
| 226 | spin_unlock_bh(&tpg->tpg_state_lock); |
| 227 | return -1; |
| 228 | } |
| 229 | spin_unlock_bh(&tpg->tpg_state_lock); |
| 230 | |
| 231 | /* |
| 232 | * Here we serialize access across the TIQN+TPG Tuple. |
| 233 | */ |
Nicholas Bellinger | a91eb7d | 2013-08-15 12:49:02 -0700 | [diff] [blame] | 234 | ret = down_interruptible(&tpg->np_login_sem); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 235 | if ((ret != 0) || signal_pending(current)) |
| 236 | return -1; |
| 237 | |
Nicholas Bellinger | a91eb7d | 2013-08-15 12:49:02 -0700 | [diff] [blame] | 238 | spin_lock_bh(&tpg->tpg_state_lock); |
| 239 | if (tpg->tpg_state != TPG_STATE_ACTIVE) { |
| 240 | spin_unlock_bh(&tpg->tpg_state_lock); |
| 241 | up(&tpg->np_login_sem); |
| 242 | return -1; |
| 243 | } |
| 244 | spin_unlock_bh(&tpg->tpg_state_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Nicholas Bellinger | a91eb7d | 2013-08-15 12:49:02 -0700 | [diff] [blame] | 249 | void iscsit_login_kref_put(struct kref *kref) |
| 250 | { |
| 251 | struct iscsi_tpg_np *tpg_np = container_of(kref, |
| 252 | struct iscsi_tpg_np, tpg_np_kref); |
| 253 | |
| 254 | complete(&tpg_np->tpg_np_comp); |
| 255 | } |
| 256 | |
| 257 | int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg, |
| 258 | struct iscsi_tpg_np *tpg_np) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 259 | { |
| 260 | struct iscsi_tiqn *tiqn = tpg->tpg_tiqn; |
| 261 | |
Nicholas Bellinger | a91eb7d | 2013-08-15 12:49:02 -0700 | [diff] [blame] | 262 | up(&tpg->np_login_sem); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 263 | |
Nicholas Bellinger | a91eb7d | 2013-08-15 12:49:02 -0700 | [diff] [blame] | 264 | if (tpg_np) |
| 265 | kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 266 | |
| 267 | if (tiqn) |
| 268 | iscsit_put_tiqn_for_login(tiqn); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Nicholas Bellinger | 05b9689 | 2013-02-18 20:59:27 -0800 | [diff] [blame] | 273 | bool iscsit_check_np_match( |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 274 | struct __kernel_sockaddr_storage *sockaddr, |
Nicholas Bellinger | 05b9689 | 2013-02-18 20:59:27 -0800 | [diff] [blame] | 275 | struct iscsi_np *np, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 276 | int network_transport) |
| 277 | { |
| 278 | struct sockaddr_in *sock_in, *sock_in_e; |
| 279 | struct sockaddr_in6 *sock_in6, *sock_in6_e; |
Nicholas Bellinger | 05b9689 | 2013-02-18 20:59:27 -0800 | [diff] [blame] | 280 | bool ip_match = false; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 281 | u16 port; |
| 282 | |
Nicholas Bellinger | 05b9689 | 2013-02-18 20:59:27 -0800 | [diff] [blame] | 283 | if (sockaddr->ss_family == AF_INET6) { |
| 284 | sock_in6 = (struct sockaddr_in6 *)sockaddr; |
| 285 | sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr; |
| 286 | |
| 287 | if (!memcmp(&sock_in6->sin6_addr.in6_u, |
| 288 | &sock_in6_e->sin6_addr.in6_u, |
| 289 | sizeof(struct in6_addr))) |
| 290 | ip_match = true; |
| 291 | |
| 292 | port = ntohs(sock_in6->sin6_port); |
| 293 | } else { |
| 294 | sock_in = (struct sockaddr_in *)sockaddr; |
| 295 | sock_in_e = (struct sockaddr_in *)&np->np_sockaddr; |
| 296 | |
| 297 | if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr) |
| 298 | ip_match = true; |
| 299 | |
| 300 | port = ntohs(sock_in->sin_port); |
| 301 | } |
| 302 | |
Christophe Vu-Brugier | 0bcc297 | 2014-06-06 17:15:16 +0200 | [diff] [blame] | 303 | if (ip_match && (np->np_port == port) && |
Nicholas Bellinger | 05b9689 | 2013-02-18 20:59:27 -0800 | [diff] [blame] | 304 | (np->np_network_transport == network_transport)) |
| 305 | return true; |
| 306 | |
| 307 | return false; |
| 308 | } |
| 309 | |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 310 | /* |
| 311 | * Called with mutex np_lock held |
| 312 | */ |
Nicholas Bellinger | 05b9689 | 2013-02-18 20:59:27 -0800 | [diff] [blame] | 313 | static struct iscsi_np *iscsit_get_np( |
| 314 | struct __kernel_sockaddr_storage *sockaddr, |
| 315 | int network_transport) |
| 316 | { |
| 317 | struct iscsi_np *np; |
| 318 | bool match; |
| 319 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 320 | list_for_each_entry(np, &g_np_list, np_list) { |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 321 | spin_lock_bh(&np->np_thread_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 322 | if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) { |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 323 | spin_unlock_bh(&np->np_thread_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 324 | continue; |
| 325 | } |
| 326 | |
Nicholas Bellinger | 05b9689 | 2013-02-18 20:59:27 -0800 | [diff] [blame] | 327 | match = iscsit_check_np_match(sockaddr, np, network_transport); |
Christophe Vu-Brugier | 0bcc297 | 2014-06-06 17:15:16 +0200 | [diff] [blame] | 328 | if (match) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 329 | /* |
| 330 | * Increment the np_exports reference count now to |
| 331 | * prevent iscsit_del_np() below from being called |
| 332 | * while iscsi_tpg_add_network_portal() is called. |
| 333 | */ |
| 334 | np->np_exports++; |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 335 | spin_unlock_bh(&np->np_thread_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 336 | return np; |
| 337 | } |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 338 | spin_unlock_bh(&np->np_thread_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 339 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 340 | |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | struct iscsi_np *iscsit_add_np( |
| 345 | struct __kernel_sockaddr_storage *sockaddr, |
| 346 | char *ip_str, |
| 347 | int network_transport) |
| 348 | { |
| 349 | struct sockaddr_in *sock_in; |
| 350 | struct sockaddr_in6 *sock_in6; |
| 351 | struct iscsi_np *np; |
| 352 | int ret; |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 353 | |
| 354 | mutex_lock(&np_lock); |
| 355 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 356 | /* |
| 357 | * Locate the existing struct iscsi_np if already active.. |
| 358 | */ |
| 359 | np = iscsit_get_np(sockaddr, network_transport); |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 360 | if (np) { |
| 361 | mutex_unlock(&np_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 362 | return np; |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 363 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 364 | |
| 365 | np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL); |
| 366 | if (!np) { |
| 367 | pr_err("Unable to allocate memory for struct iscsi_np\n"); |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 368 | mutex_unlock(&np_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 369 | return ERR_PTR(-ENOMEM); |
| 370 | } |
| 371 | |
| 372 | np->np_flags |= NPF_IP_NETWORK; |
| 373 | if (sockaddr->ss_family == AF_INET6) { |
| 374 | sock_in6 = (struct sockaddr_in6 *)sockaddr; |
| 375 | snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str); |
| 376 | np->np_port = ntohs(sock_in6->sin6_port); |
| 377 | } else { |
| 378 | sock_in = (struct sockaddr_in *)sockaddr; |
| 379 | sprintf(np->np_ip, "%s", ip_str); |
| 380 | np->np_port = ntohs(sock_in->sin_port); |
| 381 | } |
| 382 | |
| 383 | np->np_network_transport = network_transport; |
| 384 | spin_lock_init(&np->np_thread_lock); |
| 385 | init_completion(&np->np_restart_comp); |
| 386 | INIT_LIST_HEAD(&np->np_list); |
| 387 | |
| 388 | ret = iscsi_target_setup_login_socket(np, sockaddr); |
| 389 | if (ret != 0) { |
| 390 | kfree(np); |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 391 | mutex_unlock(&np_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 392 | return ERR_PTR(ret); |
| 393 | } |
| 394 | |
| 395 | np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np"); |
| 396 | if (IS_ERR(np->np_thread)) { |
| 397 | pr_err("Unable to create kthread: iscsi_np\n"); |
| 398 | ret = PTR_ERR(np->np_thread); |
| 399 | kfree(np); |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 400 | mutex_unlock(&np_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 401 | return ERR_PTR(ret); |
| 402 | } |
| 403 | /* |
| 404 | * Increment the np_exports reference count now to prevent |
| 405 | * iscsit_del_np() below from being run while a new call to |
| 406 | * iscsi_tpg_add_network_portal() for a matching iscsi_np is |
| 407 | * active. We don't need to hold np->np_thread_lock at this |
| 408 | * point because iscsi_np has not been added to g_np_list yet. |
| 409 | */ |
| 410 | np->np_exports = 1; |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 411 | np->np_thread_state = ISCSI_NP_THREAD_ACTIVE; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 412 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 413 | list_add_tail(&np->np_list, &g_np_list); |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 414 | mutex_unlock(&np_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 415 | |
| 416 | pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n", |
Nicholas Bellinger | baa4d64 | 2013-03-06 21:54:13 -0800 | [diff] [blame] | 417 | np->np_ip, np->np_port, np->np_transport->name); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 418 | |
| 419 | return np; |
| 420 | } |
| 421 | |
| 422 | int iscsit_reset_np_thread( |
| 423 | struct iscsi_np *np, |
| 424 | struct iscsi_tpg_np *tpg_np, |
Nicholas Bellinger | a91eb7d | 2013-08-15 12:49:02 -0700 | [diff] [blame] | 425 | struct iscsi_portal_group *tpg, |
| 426 | bool shutdown) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 427 | { |
| 428 | spin_lock_bh(&np->np_thread_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 429 | if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) { |
| 430 | spin_unlock_bh(&np->np_thread_lock); |
| 431 | return 0; |
| 432 | } |
| 433 | np->np_thread_state = ISCSI_NP_THREAD_RESET; |
| 434 | |
| 435 | if (np->np_thread) { |
| 436 | spin_unlock_bh(&np->np_thread_lock); |
| 437 | send_sig(SIGINT, np->np_thread, 1); |
| 438 | wait_for_completion(&np->np_restart_comp); |
| 439 | spin_lock_bh(&np->np_thread_lock); |
| 440 | } |
| 441 | spin_unlock_bh(&np->np_thread_lock); |
| 442 | |
Nicholas Bellinger | a91eb7d | 2013-08-15 12:49:02 -0700 | [diff] [blame] | 443 | if (tpg_np && shutdown) { |
| 444 | kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put); |
| 445 | |
| 446 | wait_for_completion(&tpg_np->tpg_np_comp); |
| 447 | } |
| 448 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
Nicholas Bellinger | baa4d64 | 2013-03-06 21:54:13 -0800 | [diff] [blame] | 452 | static void iscsit_free_np(struct iscsi_np *np) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 453 | { |
Al Viro | bf6932f | 2012-07-21 08:55:18 +0100 | [diff] [blame] | 454 | if (np->np_socket) |
| 455 | sock_release(np->np_socket); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | int iscsit_del_np(struct iscsi_np *np) |
| 459 | { |
| 460 | spin_lock_bh(&np->np_thread_lock); |
| 461 | np->np_exports--; |
| 462 | if (np->np_exports) { |
Nicholas Bellinger | 2363d19 | 2014-06-03 18:27:52 -0700 | [diff] [blame] | 463 | np->enabled = true; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 464 | spin_unlock_bh(&np->np_thread_lock); |
| 465 | return 0; |
| 466 | } |
| 467 | np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN; |
| 468 | spin_unlock_bh(&np->np_thread_lock); |
| 469 | |
| 470 | if (np->np_thread) { |
| 471 | /* |
| 472 | * We need to send the signal to wakeup Linux/Net |
| 473 | * which may be sleeping in sock_accept().. |
| 474 | */ |
| 475 | send_sig(SIGINT, np->np_thread, 1); |
| 476 | kthread_stop(np->np_thread); |
Nicholas Bellinger | db6077f | 2013-12-11 15:45:32 -0800 | [diff] [blame] | 477 | np->np_thread = NULL; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 478 | } |
Nicholas Bellinger | baa4d64 | 2013-03-06 21:54:13 -0800 | [diff] [blame] | 479 | |
| 480 | np->np_transport->iscsit_free_np(np); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 481 | |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 482 | mutex_lock(&np_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 483 | list_del(&np->np_list); |
Andy Grover | ee291e6 | 2014-01-24 16:18:54 -0800 | [diff] [blame] | 484 | mutex_unlock(&np_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 485 | |
| 486 | pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n", |
Nicholas Bellinger | baa4d64 | 2013-03-06 21:54:13 -0800 | [diff] [blame] | 487 | np->np_ip, np->np_port, np->np_transport->name); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 488 | |
Nicholas Bellinger | baa4d64 | 2013-03-06 21:54:13 -0800 | [diff] [blame] | 489 | iscsit_put_transport(np->np_transport); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 490 | kfree(np); |
| 491 | return 0; |
| 492 | } |
| 493 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 494 | static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int); |
| 495 | static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int); |
| 496 | |
| 497 | static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd) |
| 498 | { |
| 499 | iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state); |
| 500 | return 0; |
| 501 | } |
| 502 | |
Nicholas Bellinger | 131e6ab | 2014-03-22 14:55:56 -0700 | [diff] [blame] | 503 | static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd) |
| 504 | { |
| 505 | bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD); |
| 506 | |
| 507 | spin_lock_bh(&conn->cmd_lock); |
| 508 | if (!list_empty(&cmd->i_conn_node)) |
| 509 | list_del_init(&cmd->i_conn_node); |
| 510 | spin_unlock_bh(&conn->cmd_lock); |
| 511 | |
| 512 | __iscsit_free_cmd(cmd, scsi_cmd, true); |
| 513 | } |
| 514 | |
Nicholas Bellinger | e70beee | 2014-04-02 12:52:38 -0700 | [diff] [blame] | 515 | static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn) |
| 516 | { |
| 517 | return TARGET_PROT_NORMAL; |
| 518 | } |
| 519 | |
Nicholas Bellinger | baa4d64 | 2013-03-06 21:54:13 -0800 | [diff] [blame] | 520 | static struct iscsit_transport iscsi_target_transport = { |
| 521 | .name = "iSCSI/TCP", |
| 522 | .transport_type = ISCSI_TCP, |
| 523 | .owner = NULL, |
| 524 | .iscsit_setup_np = iscsit_setup_np, |
| 525 | .iscsit_accept_np = iscsit_accept_np, |
| 526 | .iscsit_free_np = iscsit_free_np, |
| 527 | .iscsit_get_login_rx = iscsit_get_login_rx, |
| 528 | .iscsit_put_login_tx = iscsit_put_login_tx, |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 529 | .iscsit_get_dataout = iscsit_build_r2ts_for_cmd, |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 530 | .iscsit_immediate_queue = iscsit_immediate_queue, |
| 531 | .iscsit_response_queue = iscsit_response_queue, |
| 532 | .iscsit_queue_data_in = iscsit_queue_rsp, |
| 533 | .iscsit_queue_status = iscsit_queue_rsp, |
Nicholas Bellinger | 131e6ab | 2014-03-22 14:55:56 -0700 | [diff] [blame] | 534 | .iscsit_aborted_task = iscsit_aborted_task, |
Nicholas Bellinger | e70beee | 2014-04-02 12:52:38 -0700 | [diff] [blame] | 535 | .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops, |
Nicholas Bellinger | baa4d64 | 2013-03-06 21:54:13 -0800 | [diff] [blame] | 536 | }; |
| 537 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 538 | static int __init iscsi_target_init_module(void) |
| 539 | { |
| 540 | int ret = 0; |
| 541 | |
| 542 | pr_debug("iSCSI-Target "ISCSIT_VERSION"\n"); |
| 543 | |
| 544 | iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL); |
| 545 | if (!iscsit_global) { |
| 546 | pr_err("Unable to allocate memory for iscsit_global\n"); |
| 547 | return -1; |
| 548 | } |
| 549 | mutex_init(&auth_id_lock); |
| 550 | spin_lock_init(&sess_idr_lock); |
| 551 | idr_init(&tiqn_idr); |
| 552 | idr_init(&sess_idr); |
| 553 | |
| 554 | ret = iscsi_target_register_configfs(); |
| 555 | if (ret < 0) |
| 556 | goto out; |
| 557 | |
| 558 | ret = iscsi_thread_set_init(); |
| 559 | if (ret < 0) |
| 560 | goto configfs_out; |
| 561 | |
| 562 | if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) != |
| 563 | TARGET_THREAD_SET_COUNT) { |
| 564 | pr_err("iscsi_allocate_thread_sets() returned" |
| 565 | " unexpected value!\n"); |
| 566 | goto ts_out1; |
| 567 | } |
| 568 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 569 | lio_qr_cache = kmem_cache_create("lio_qr_cache", |
| 570 | sizeof(struct iscsi_queue_req), |
| 571 | __alignof__(struct iscsi_queue_req), 0, NULL); |
| 572 | if (!lio_qr_cache) { |
| 573 | pr_err("nable to kmem_cache_create() for" |
| 574 | " lio_qr_cache\n"); |
Nicholas Bellinger | d703ce2 | 2013-08-17 14:27:56 -0700 | [diff] [blame] | 575 | goto ts_out2; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | lio_dr_cache = kmem_cache_create("lio_dr_cache", |
| 579 | sizeof(struct iscsi_datain_req), |
| 580 | __alignof__(struct iscsi_datain_req), 0, NULL); |
| 581 | if (!lio_dr_cache) { |
| 582 | pr_err("Unable to kmem_cache_create() for" |
| 583 | " lio_dr_cache\n"); |
| 584 | goto qr_out; |
| 585 | } |
| 586 | |
| 587 | lio_ooo_cache = kmem_cache_create("lio_ooo_cache", |
| 588 | sizeof(struct iscsi_ooo_cmdsn), |
| 589 | __alignof__(struct iscsi_ooo_cmdsn), 0, NULL); |
| 590 | if (!lio_ooo_cache) { |
| 591 | pr_err("Unable to kmem_cache_create() for" |
| 592 | " lio_ooo_cache\n"); |
| 593 | goto dr_out; |
| 594 | } |
| 595 | |
| 596 | lio_r2t_cache = kmem_cache_create("lio_r2t_cache", |
| 597 | sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t), |
| 598 | 0, NULL); |
| 599 | if (!lio_r2t_cache) { |
| 600 | pr_err("Unable to kmem_cache_create() for" |
| 601 | " lio_r2t_cache\n"); |
| 602 | goto ooo_out; |
| 603 | } |
| 604 | |
Nicholas Bellinger | baa4d64 | 2013-03-06 21:54:13 -0800 | [diff] [blame] | 605 | iscsit_register_transport(&iscsi_target_transport); |
| 606 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 607 | if (iscsit_load_discovery_tpg() < 0) |
| 608 | goto r2t_out; |
| 609 | |
| 610 | return ret; |
| 611 | r2t_out: |
Lino Sanfilippo | 7f2c53b | 2014-11-30 12:00:11 +0100 | [diff] [blame] | 612 | iscsit_unregister_transport(&iscsi_target_transport); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 613 | kmem_cache_destroy(lio_r2t_cache); |
| 614 | ooo_out: |
| 615 | kmem_cache_destroy(lio_ooo_cache); |
| 616 | dr_out: |
| 617 | kmem_cache_destroy(lio_dr_cache); |
| 618 | qr_out: |
| 619 | kmem_cache_destroy(lio_qr_cache); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 620 | ts_out2: |
| 621 | iscsi_deallocate_thread_sets(); |
| 622 | ts_out1: |
| 623 | iscsi_thread_set_free(); |
| 624 | configfs_out: |
| 625 | iscsi_target_deregister_configfs(); |
| 626 | out: |
| 627 | kfree(iscsit_global); |
| 628 | return -ENOMEM; |
| 629 | } |
| 630 | |
| 631 | static void __exit iscsi_target_cleanup_module(void) |
| 632 | { |
| 633 | iscsi_deallocate_thread_sets(); |
| 634 | iscsi_thread_set_free(); |
| 635 | iscsit_release_discovery_tpg(); |
Nicholas Bellinger | baa4d64 | 2013-03-06 21:54:13 -0800 | [diff] [blame] | 636 | iscsit_unregister_transport(&iscsi_target_transport); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 637 | kmem_cache_destroy(lio_qr_cache); |
| 638 | kmem_cache_destroy(lio_dr_cache); |
| 639 | kmem_cache_destroy(lio_ooo_cache); |
| 640 | kmem_cache_destroy(lio_r2t_cache); |
| 641 | |
| 642 | iscsi_target_deregister_configfs(); |
| 643 | |
| 644 | kfree(iscsit_global); |
| 645 | } |
| 646 | |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 647 | static int iscsit_add_reject( |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 648 | struct iscsi_conn *conn, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 649 | u8 reason, |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 650 | unsigned char *buf) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 651 | { |
| 652 | struct iscsi_cmd *cmd; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 653 | |
Nicholas Bellinger | 676687c | 2014-01-20 03:36:44 +0000 | [diff] [blame] | 654 | cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 655 | if (!cmd) |
| 656 | return -1; |
| 657 | |
| 658 | cmd->iscsi_opcode = ISCSI_OP_REJECT; |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 659 | cmd->reject_reason = reason; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 660 | |
Thomas Meyer | 1c3d579 | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 661 | cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 662 | if (!cmd->buf_ptr) { |
| 663 | pr_err("Unable to allocate memory for cmd->buf_ptr\n"); |
Nicholas Bellinger | aafc9d1 | 2013-05-31 00:49:41 -0700 | [diff] [blame] | 664 | iscsit_free_cmd(cmd, false); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 665 | return -1; |
| 666 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 667 | |
| 668 | spin_lock_bh(&conn->cmd_lock); |
Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 669 | list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 670 | spin_unlock_bh(&conn->cmd_lock); |
| 671 | |
| 672 | cmd->i_state = ISTATE_SEND_REJECT; |
| 673 | iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); |
| 674 | |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 675 | return -1; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 678 | static int iscsit_add_reject_from_cmd( |
| 679 | struct iscsi_cmd *cmd, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 680 | u8 reason, |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 681 | bool add_to_conn, |
| 682 | unsigned char *buf) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 683 | { |
| 684 | struct iscsi_conn *conn; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 685 | |
| 686 | if (!cmd->conn) { |
| 687 | pr_err("cmd->conn is NULL for ITT: 0x%08x\n", |
| 688 | cmd->init_task_tag); |
| 689 | return -1; |
| 690 | } |
| 691 | conn = cmd->conn; |
| 692 | |
| 693 | cmd->iscsi_opcode = ISCSI_OP_REJECT; |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 694 | cmd->reject_reason = reason; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 695 | |
Thomas Meyer | 1c3d579 | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 696 | cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 697 | if (!cmd->buf_ptr) { |
| 698 | pr_err("Unable to allocate memory for cmd->buf_ptr\n"); |
Nicholas Bellinger | aafc9d1 | 2013-05-31 00:49:41 -0700 | [diff] [blame] | 699 | iscsit_free_cmd(cmd, false); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 700 | return -1; |
| 701 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 702 | |
| 703 | if (add_to_conn) { |
| 704 | spin_lock_bh(&conn->cmd_lock); |
Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 705 | list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 706 | spin_unlock_bh(&conn->cmd_lock); |
| 707 | } |
| 708 | |
| 709 | cmd->i_state = ISTATE_SEND_REJECT; |
| 710 | iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 711 | /* |
| 712 | * Perform the kref_put now if se_cmd has already been setup by |
| 713 | * scsit_setup_scsi_cmd() |
| 714 | */ |
| 715 | if (cmd->se_cmd.se_tfo != NULL) { |
| 716 | pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n"); |
| 717 | target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd); |
| 718 | } |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 719 | return -1; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 720 | } |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 721 | |
| 722 | static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason, |
| 723 | unsigned char *buf) |
| 724 | { |
| 725 | return iscsit_add_reject_from_cmd(cmd, reason, true, buf); |
| 726 | } |
| 727 | |
| 728 | int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf) |
| 729 | { |
| 730 | return iscsit_add_reject_from_cmd(cmd, reason, false, buf); |
| 731 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 732 | |
| 733 | /* |
| 734 | * Map some portion of the allocated scatterlist to an iovec, suitable for |
Andy Grover | bfb79ea | 2012-04-03 15:51:29 -0700 | [diff] [blame] | 735 | * kernel sockets to copy data in/out. |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 736 | */ |
| 737 | static int iscsit_map_iovec( |
| 738 | struct iscsi_cmd *cmd, |
| 739 | struct kvec *iov, |
| 740 | u32 data_offset, |
| 741 | u32 data_length) |
| 742 | { |
| 743 | u32 i = 0; |
| 744 | struct scatterlist *sg; |
| 745 | unsigned int page_off; |
| 746 | |
| 747 | /* |
Andy Grover | bfb79ea | 2012-04-03 15:51:29 -0700 | [diff] [blame] | 748 | * We know each entry in t_data_sg contains a page. |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 749 | */ |
Andy Grover | bfb79ea | 2012-04-03 15:51:29 -0700 | [diff] [blame] | 750 | sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE]; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 751 | page_off = (data_offset % PAGE_SIZE); |
| 752 | |
| 753 | cmd->first_data_sg = sg; |
| 754 | cmd->first_data_sg_off = page_off; |
| 755 | |
| 756 | while (data_length) { |
| 757 | u32 cur_len = min_t(u32, data_length, sg->length - page_off); |
| 758 | |
| 759 | iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off; |
| 760 | iov[i].iov_len = cur_len; |
| 761 | |
| 762 | data_length -= cur_len; |
| 763 | page_off = 0; |
| 764 | sg = sg_next(sg); |
| 765 | i++; |
| 766 | } |
| 767 | |
| 768 | cmd->kmapped_nents = i; |
| 769 | |
| 770 | return i; |
| 771 | } |
| 772 | |
| 773 | static void iscsit_unmap_iovec(struct iscsi_cmd *cmd) |
| 774 | { |
| 775 | u32 i; |
| 776 | struct scatterlist *sg; |
| 777 | |
| 778 | sg = cmd->first_data_sg; |
| 779 | |
| 780 | for (i = 0; i < cmd->kmapped_nents; i++) |
| 781 | kunmap(sg_page(&sg[i])); |
| 782 | } |
| 783 | |
| 784 | static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn) |
| 785 | { |
Nicholas Bellinger | f56cbbb | 2013-10-03 13:56:14 -0700 | [diff] [blame] | 786 | LIST_HEAD(ack_list); |
| 787 | struct iscsi_cmd *cmd, *cmd_p; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 788 | |
| 789 | conn->exp_statsn = exp_statsn; |
| 790 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 791 | if (conn->sess->sess_ops->RDMAExtensions) |
| 792 | return; |
| 793 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 794 | spin_lock_bh(&conn->cmd_lock); |
Nicholas Bellinger | f56cbbb | 2013-10-03 13:56:14 -0700 | [diff] [blame] | 795 | list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 796 | spin_lock(&cmd->istate_lock); |
| 797 | if ((cmd->i_state == ISTATE_SENT_STATUS) && |
Steve Hodgson | 64c13330 | 2012-11-05 18:02:41 -0800 | [diff] [blame] | 798 | iscsi_sna_lt(cmd->stat_sn, exp_statsn)) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 799 | cmd->i_state = ISTATE_REMOVE; |
| 800 | spin_unlock(&cmd->istate_lock); |
Nicholas Bellinger | f56cbbb | 2013-10-03 13:56:14 -0700 | [diff] [blame] | 801 | list_move_tail(&cmd->i_conn_node, &ack_list); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 802 | continue; |
| 803 | } |
| 804 | spin_unlock(&cmd->istate_lock); |
| 805 | } |
| 806 | spin_unlock_bh(&conn->cmd_lock); |
Nicholas Bellinger | f56cbbb | 2013-10-03 13:56:14 -0700 | [diff] [blame] | 807 | |
| 808 | list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) { |
Nicholas Bellinger | 5159d76 | 2014-02-03 12:53:51 -0800 | [diff] [blame] | 809 | list_del_init(&cmd->i_conn_node); |
Nicholas Bellinger | f56cbbb | 2013-10-03 13:56:14 -0700 | [diff] [blame] | 810 | iscsit_free_cmd(cmd, false); |
| 811 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd) |
| 815 | { |
Nicholas Bellinger | f80e8ed | 2012-05-20 17:10:29 -0700 | [diff] [blame] | 816 | u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE)); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 817 | |
Christoph Hellwig | c0427f1 | 2011-10-12 11:06:56 -0400 | [diff] [blame] | 818 | iov_count += ISCSI_IOV_DATA_BUFFER; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 819 | |
| 820 | cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL); |
| 821 | if (!cmd->iov_data) { |
| 822 | pr_err("Unable to allocate cmd->iov_data\n"); |
| 823 | return -ENOMEM; |
| 824 | } |
| 825 | |
| 826 | cmd->orig_iov_data_count = iov_count; |
| 827 | return 0; |
| 828 | } |
| 829 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 830 | int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 831 | unsigned char *buf) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 832 | { |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 833 | int data_direction, payload_length; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 834 | struct iscsi_scsi_req *hdr; |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 835 | int iscsi_task_attr; |
| 836 | int sam_task_attr; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 837 | |
Nicholas Bellinger | 04f3b31 | 2013-11-13 18:54:45 -0800 | [diff] [blame] | 838 | atomic_long_inc(&conn->sess->cmd_pdus); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 839 | |
| 840 | hdr = (struct iscsi_scsi_req *) buf; |
| 841 | payload_length = ntoh24(hdr->dlength); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 842 | |
| 843 | /* FIXME; Add checks for AdditionalHeaderSegment */ |
| 844 | |
| 845 | if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) && |
| 846 | !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) { |
| 847 | pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL" |
| 848 | " not set. Bad iSCSI Initiator.\n"); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 849 | return iscsit_add_reject_cmd(cmd, |
| 850 | ISCSI_REASON_BOOKMARK_INVALID, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | if (((hdr->flags & ISCSI_FLAG_CMD_READ) || |
| 854 | (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) { |
| 855 | /* |
Nicholas Bellinger | 4454b66 | 2013-11-25 14:53:57 -0800 | [diff] [blame] | 856 | * From RFC-3720 Section 10.3.1: |
| 857 | * |
| 858 | * "Either or both of R and W MAY be 1 when either the |
| 859 | * Expected Data Transfer Length and/or Bidirectional Read |
| 860 | * Expected Data Transfer Length are 0" |
| 861 | * |
| 862 | * For this case, go ahead and clear the unnecssary bits |
| 863 | * to avoid any confusion with ->data_direction. |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 864 | */ |
Nicholas Bellinger | 4454b66 | 2013-11-25 14:53:57 -0800 | [diff] [blame] | 865 | hdr->flags &= ~ISCSI_FLAG_CMD_READ; |
| 866 | hdr->flags &= ~ISCSI_FLAG_CMD_WRITE; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 867 | |
Nicholas Bellinger | 4454b66 | 2013-11-25 14:53:57 -0800 | [diff] [blame] | 868 | pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE" |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 869 | " set when Expected Data Transfer Length is 0 for" |
Nicholas Bellinger | 4454b66 | 2013-11-25 14:53:57 -0800 | [diff] [blame] | 870 | " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 871 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 872 | |
| 873 | if (!(hdr->flags & ISCSI_FLAG_CMD_READ) && |
| 874 | !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) { |
| 875 | pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE" |
| 876 | " MUST be set if Expected Data Transfer Length is not 0." |
| 877 | " Bad iSCSI Initiator\n"); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 878 | return iscsit_add_reject_cmd(cmd, |
| 879 | ISCSI_REASON_BOOKMARK_INVALID, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | if ((hdr->flags & ISCSI_FLAG_CMD_READ) && |
| 883 | (hdr->flags & ISCSI_FLAG_CMD_WRITE)) { |
| 884 | pr_err("Bidirectional operations not supported!\n"); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 885 | return iscsit_add_reject_cmd(cmd, |
| 886 | ISCSI_REASON_BOOKMARK_INVALID, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | if (hdr->opcode & ISCSI_OP_IMMEDIATE) { |
| 890 | pr_err("Illegally set Immediate Bit in iSCSI Initiator" |
| 891 | " Scsi Command PDU.\n"); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 892 | return iscsit_add_reject_cmd(cmd, |
| 893 | ISCSI_REASON_BOOKMARK_INVALID, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | if (payload_length && !conn->sess->sess_ops->ImmediateData) { |
| 897 | pr_err("ImmediateData=No but DataSegmentLength=%u," |
| 898 | " protocol error.\n", payload_length); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 899 | return iscsit_add_reject_cmd(cmd, |
| 900 | ISCSI_REASON_PROTOCOL_ERROR, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 903 | if ((be32_to_cpu(hdr->data_length) == payload_length) && |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 904 | (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) { |
| 905 | pr_err("Expected Data Transfer Length and Length of" |
| 906 | " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL" |
| 907 | " bit is not set protocol error\n"); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 908 | return iscsit_add_reject_cmd(cmd, |
| 909 | ISCSI_REASON_PROTOCOL_ERROR, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 912 | if (payload_length > be32_to_cpu(hdr->data_length)) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 913 | pr_err("DataSegmentLength: %u is greater than" |
| 914 | " EDTL: %u, protocol error.\n", payload_length, |
| 915 | hdr->data_length); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 916 | return iscsit_add_reject_cmd(cmd, |
| 917 | ISCSI_REASON_PROTOCOL_ERROR, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 920 | if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 921 | pr_err("DataSegmentLength: %u is greater than" |
Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 922 | " MaxXmitDataSegmentLength: %u, protocol error.\n", |
| 923 | payload_length, conn->conn_ops->MaxXmitDataSegmentLength); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 924 | return iscsit_add_reject_cmd(cmd, |
| 925 | ISCSI_REASON_PROTOCOL_ERROR, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | if (payload_length > conn->sess->sess_ops->FirstBurstLength) { |
| 929 | pr_err("DataSegmentLength: %u is greater than" |
| 930 | " FirstBurstLength: %u, protocol error.\n", |
| 931 | payload_length, conn->sess->sess_ops->FirstBurstLength); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 932 | return iscsit_add_reject_cmd(cmd, |
| 933 | ISCSI_REASON_BOOKMARK_INVALID, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE : |
| 937 | (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE : |
| 938 | DMA_NONE; |
| 939 | |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 940 | cmd->data_direction = data_direction; |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 941 | iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK; |
| 942 | /* |
| 943 | * Figure out the SAM Task Attribute for the incoming SCSI CDB |
| 944 | */ |
| 945 | if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) || |
| 946 | (iscsi_task_attr == ISCSI_ATTR_SIMPLE)) |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 947 | sam_task_attr = TCM_SIMPLE_TAG; |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 948 | else if (iscsi_task_attr == ISCSI_ATTR_ORDERED) |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 949 | sam_task_attr = TCM_ORDERED_TAG; |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 950 | else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE) |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 951 | sam_task_attr = TCM_HEAD_TAG; |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 952 | else if (iscsi_task_attr == ISCSI_ATTR_ACA) |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 953 | sam_task_attr = TCM_ACA_TAG; |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 954 | else { |
| 955 | pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using" |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 956 | " TCM_SIMPLE_TAG\n", iscsi_task_attr); |
| 957 | sam_task_attr = TCM_SIMPLE_TAG; |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 960 | cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD; |
| 961 | cmd->i_state = ISTATE_NEW_CMD; |
| 962 | cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); |
| 963 | cmd->immediate_data = (payload_length) ? 1 : 0; |
| 964 | cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) && |
| 965 | (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0); |
| 966 | if (cmd->unsolicited_data) |
| 967 | cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA; |
| 968 | |
| 969 | conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; |
| 970 | if (hdr->flags & ISCSI_FLAG_CMD_READ) { |
Sagi Grimberg | c1e34b6 | 2015-01-26 12:49:05 +0200 | [diff] [blame] | 971 | cmd->targ_xfer_tag = session_get_next_ttt(conn->sess); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 972 | } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE) |
| 973 | cmd->targ_xfer_tag = 0xFFFFFFFF; |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 974 | cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); |
| 975 | cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 976 | cmd->first_burst_len = payload_length; |
| 977 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 978 | if (!conn->sess->sess_ops->RDMAExtensions && |
| 979 | cmd->data_direction == DMA_FROM_DEVICE) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 980 | struct iscsi_datain_req *dr; |
| 981 | |
| 982 | dr = iscsit_allocate_datain_req(); |
| 983 | if (!dr) |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 984 | return iscsit_add_reject_cmd(cmd, |
| 985 | ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 986 | |
| 987 | iscsit_attach_datain_req(cmd, dr); |
| 988 | } |
| 989 | |
| 990 | /* |
Andy Grover | 065ca1e | 2012-04-03 15:51:23 -0700 | [diff] [blame] | 991 | * Initialize struct se_cmd descriptor from target_core_mod infrastructure |
| 992 | */ |
| 993 | transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops, |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 994 | conn->sess->se_sess, be32_to_cpu(hdr->data_length), |
| 995 | cmd->data_direction, sam_task_attr, |
| 996 | cmd->sense_buffer + 2); |
Andy Grover | 065ca1e | 2012-04-03 15:51:23 -0700 | [diff] [blame] | 997 | |
| 998 | pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x," |
| 999 | " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt, |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1000 | hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length, |
| 1001 | conn->cid); |
| 1002 | |
| 1003 | target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true); |
Andy Grover | 065ca1e | 2012-04-03 15:51:23 -0700 | [diff] [blame] | 1004 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1005 | cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd, |
| 1006 | scsilun_to_int(&hdr->lun)); |
| 1007 | if (cmd->sense_reason) |
| 1008 | goto attach_cmd; |
| 1009 | |
| 1010 | cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb); |
| 1011 | if (cmd->sense_reason) { |
| 1012 | if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) { |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1013 | return iscsit_add_reject_cmd(cmd, |
| 1014 | ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1015 | } |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1016 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1017 | goto attach_cmd; |
| 1018 | } |
Andy Grover | a12f41f | 2012-04-03 15:51:20 -0700 | [diff] [blame] | 1019 | |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1020 | if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) { |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1021 | return iscsit_add_reject_cmd(cmd, |
| 1022 | ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | attach_cmd: |
| 1026 | spin_lock_bh(&conn->cmd_lock); |
Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 1027 | list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1028 | spin_unlock_bh(&conn->cmd_lock); |
| 1029 | /* |
| 1030 | * Check if we need to delay processing because of ALUA |
| 1031 | * Active/NonOptimized primary access state.. |
| 1032 | */ |
| 1033 | core_alua_check_nonop_delay(&cmd->se_cmd); |
Andy Grover | bfb79ea | 2012-04-03 15:51:29 -0700 | [diff] [blame] | 1034 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1035 | return 0; |
| 1036 | } |
| 1037 | EXPORT_SYMBOL(iscsit_setup_scsi_cmd); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1038 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1039 | void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd) |
| 1040 | { |
| 1041 | iscsit_set_dataout_sequence_values(cmd); |
| 1042 | |
| 1043 | spin_lock_bh(&cmd->dataout_timeout_lock); |
| 1044 | iscsit_start_dataout_timer(cmd, cmd->conn); |
| 1045 | spin_unlock_bh(&cmd->dataout_timeout_lock); |
| 1046 | } |
| 1047 | EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout); |
| 1048 | |
| 1049 | int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 1050 | struct iscsi_scsi_req *hdr) |
| 1051 | { |
| 1052 | int cmdsn_ret = 0; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1053 | /* |
| 1054 | * Check the CmdSN against ExpCmdSN/MaxCmdSN here if |
| 1055 | * the Immediate Bit is not set, and no Immediate |
| 1056 | * Data is attached. |
| 1057 | * |
| 1058 | * A PDU/CmdSN carrying Immediate Data can only |
| 1059 | * be processed after the DataCRC has passed. |
| 1060 | * If the DataCRC fails, the CmdSN MUST NOT |
| 1061 | * be acknowledged. (See below) |
| 1062 | */ |
| 1063 | if (!cmd->immediate_data) { |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1064 | cmdsn_ret = iscsit_sequence_cmd(conn, cmd, |
| 1065 | (unsigned char *)hdr, hdr->cmdsn); |
| 1066 | if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) |
| 1067 | return -1; |
| 1068 | else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) { |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1069 | target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd); |
Nicholas Bellinger | 7e32da5 | 2011-10-28 13:32:35 -0700 | [diff] [blame] | 1070 | return 0; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1071 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 1074 | iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1075 | |
| 1076 | /* |
| 1077 | * If no Immediate Data is attached, it's OK to return now. |
| 1078 | */ |
| 1079 | if (!cmd->immediate_data) { |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1080 | if (!cmd->sense_reason && cmd->unsolicited_data) |
| 1081 | iscsit_set_unsoliticed_dataout(cmd); |
| 1082 | if (!cmd->sense_reason) |
| 1083 | return 0; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1084 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1085 | target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | /* |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1090 | * Early CHECK_CONDITIONs with ImmediateData never make it to command |
| 1091 | * execution. These exceptions are processed in CmdSN order using |
| 1092 | * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below. |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1093 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1094 | if (cmd->sense_reason) { |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1095 | if (cmd->reject_reason) |
| 1096 | return 0; |
| 1097 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1098 | return 1; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1099 | } |
| 1100 | /* |
| 1101 | * Call directly into transport_generic_new_cmd() to perform |
| 1102 | * the backend memory allocation. |
| 1103 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1104 | cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd); |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1105 | if (cmd->sense_reason) |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1106 | return 1; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1107 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1108 | return 0; |
| 1109 | } |
| 1110 | EXPORT_SYMBOL(iscsit_process_scsi_cmd); |
| 1111 | |
| 1112 | static int |
| 1113 | iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr, |
| 1114 | bool dump_payload) |
| 1115 | { |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1116 | struct iscsi_conn *conn = cmd->conn; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1117 | int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION; |
| 1118 | /* |
| 1119 | * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes. |
| 1120 | */ |
Christophe Vu-Brugier | 0bcc297 | 2014-06-06 17:15:16 +0200 | [diff] [blame] | 1121 | if (dump_payload) |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1122 | goto after_immediate_data; |
| 1123 | |
| 1124 | immed_ret = iscsit_handle_immediate_data(cmd, hdr, |
| 1125 | cmd->first_burst_len); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1126 | after_immediate_data: |
| 1127 | if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) { |
| 1128 | /* |
| 1129 | * A PDU/CmdSN carrying Immediate Data passed |
| 1130 | * DataCRC, check against ExpCmdSN/MaxCmdSN if |
| 1131 | * Immediate Bit is not set. |
| 1132 | */ |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1133 | cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd, |
| 1134 | (unsigned char *)hdr, hdr->cmdsn); |
Nicholas Bellinger | 9d86a2b | 2013-08-22 00:05:45 -0700 | [diff] [blame] | 1135 | if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1136 | return -1; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1137 | |
Nicholas Bellinger | 9d86a2b | 2013-08-22 00:05:45 -0700 | [diff] [blame] | 1138 | if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) { |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1139 | int rc; |
| 1140 | |
| 1141 | rc = iscsit_dump_data_payload(cmd->conn, |
| 1142 | cmd->first_burst_len, 1); |
| 1143 | target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd); |
| 1144 | return rc; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1145 | } else if (cmd->unsolicited_data) |
| 1146 | iscsit_set_unsoliticed_dataout(cmd); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1147 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1148 | } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) { |
| 1149 | /* |
| 1150 | * Immediate Data failed DataCRC and ERL>=1, |
| 1151 | * silently drop this PDU and let the initiator |
| 1152 | * plug the CmdSN gap. |
| 1153 | * |
| 1154 | * FIXME: Send Unsolicited NOPIN with reserved |
| 1155 | * TTT here to help the initiator figure out |
| 1156 | * the missing CmdSN, although they should be |
| 1157 | * intelligent enough to determine the missing |
| 1158 | * CmdSN and issue a retry to plug the sequence. |
| 1159 | */ |
| 1160 | cmd->i_state = ISTATE_REMOVE; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1161 | iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1162 | } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */ |
| 1163 | return -1; |
| 1164 | |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1168 | static int |
| 1169 | iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 1170 | unsigned char *buf) |
| 1171 | { |
| 1172 | struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf; |
| 1173 | int rc, immed_data; |
| 1174 | bool dump_payload = false; |
| 1175 | |
| 1176 | rc = iscsit_setup_scsi_cmd(conn, cmd, buf); |
| 1177 | if (rc < 0) |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1178 | return 0; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1179 | /* |
| 1180 | * Allocation iovecs needed for struct socket operations for |
| 1181 | * traditional iSCSI block I/O. |
| 1182 | */ |
| 1183 | if (iscsit_allocate_iovecs(cmd) < 0) { |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1184 | return iscsit_add_reject_cmd(cmd, |
| 1185 | ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1186 | } |
| 1187 | immed_data = cmd->immediate_data; |
| 1188 | |
| 1189 | rc = iscsit_process_scsi_cmd(conn, cmd, hdr); |
| 1190 | if (rc < 0) |
| 1191 | return rc; |
| 1192 | else if (rc > 0) |
| 1193 | dump_payload = true; |
| 1194 | |
| 1195 | if (!immed_data) |
| 1196 | return 0; |
| 1197 | |
| 1198 | return iscsit_get_immediate_data(cmd, hdr, dump_payload); |
| 1199 | } |
| 1200 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1201 | static u32 iscsit_do_crypto_hash_sg( |
| 1202 | struct hash_desc *hash, |
| 1203 | struct iscsi_cmd *cmd, |
| 1204 | u32 data_offset, |
| 1205 | u32 data_length, |
| 1206 | u32 padding, |
| 1207 | u8 *pad_bytes) |
| 1208 | { |
| 1209 | u32 data_crc; |
| 1210 | u32 i; |
| 1211 | struct scatterlist *sg; |
| 1212 | unsigned int page_off; |
| 1213 | |
| 1214 | crypto_hash_init(hash); |
| 1215 | |
| 1216 | sg = cmd->first_data_sg; |
| 1217 | page_off = cmd->first_data_sg_off; |
| 1218 | |
| 1219 | i = 0; |
| 1220 | while (data_length) { |
| 1221 | u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off)); |
| 1222 | |
| 1223 | crypto_hash_update(hash, &sg[i], cur_len); |
| 1224 | |
| 1225 | data_length -= cur_len; |
| 1226 | page_off = 0; |
| 1227 | i++; |
| 1228 | } |
| 1229 | |
| 1230 | if (padding) { |
| 1231 | struct scatterlist pad_sg; |
| 1232 | |
| 1233 | sg_init_one(&pad_sg, pad_bytes, padding); |
| 1234 | crypto_hash_update(hash, &pad_sg, padding); |
| 1235 | } |
| 1236 | crypto_hash_final(hash, (u8 *) &data_crc); |
| 1237 | |
| 1238 | return data_crc; |
| 1239 | } |
| 1240 | |
| 1241 | static void iscsit_do_crypto_hash_buf( |
| 1242 | struct hash_desc *hash, |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 1243 | const void *buf, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1244 | u32 payload_length, |
| 1245 | u32 padding, |
| 1246 | u8 *pad_bytes, |
| 1247 | u8 *data_crc) |
| 1248 | { |
| 1249 | struct scatterlist sg; |
| 1250 | |
| 1251 | crypto_hash_init(hash); |
| 1252 | |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 1253 | sg_init_one(&sg, buf, payload_length); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1254 | crypto_hash_update(hash, &sg, payload_length); |
| 1255 | |
| 1256 | if (padding) { |
| 1257 | sg_init_one(&sg, pad_bytes, padding); |
| 1258 | crypto_hash_update(hash, &sg, padding); |
| 1259 | } |
| 1260 | crypto_hash_final(hash, data_crc); |
| 1261 | } |
| 1262 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1263 | int |
| 1264 | iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf, |
| 1265 | struct iscsi_cmd **out_cmd) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1266 | { |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1267 | struct iscsi_data *hdr = (struct iscsi_data *)buf; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1268 | struct iscsi_cmd *cmd = NULL; |
| 1269 | struct se_cmd *se_cmd; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1270 | u32 payload_length = ntoh24(hdr->dlength); |
| 1271 | int rc; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (!payload_length) { |
Nicholas Bellinger | dbcbc95 | 2013-11-06 20:55:39 -0800 | [diff] [blame] | 1274 | pr_warn("DataOUT payload is ZERO, ignoring.\n"); |
| 1275 | return 0; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | /* iSCSI write */ |
Nicholas Bellinger | 04f3b31 | 2013-11-13 18:54:45 -0800 | [diff] [blame] | 1279 | atomic_long_add(payload_length, &conn->sess->rx_data_octets); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1280 | |
Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1281 | if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1282 | pr_err("DataSegmentLength: %u is greater than" |
Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1283 | " MaxXmitDataSegmentLength: %u\n", payload_length, |
| 1284 | conn->conn_ops->MaxXmitDataSegmentLength); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1285 | return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, |
| 1286 | buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, |
| 1290 | payload_length); |
| 1291 | if (!cmd) |
| 1292 | return 0; |
| 1293 | |
| 1294 | pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x," |
| 1295 | " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n", |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1296 | hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset), |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1297 | payload_length, conn->cid); |
| 1298 | |
| 1299 | if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) { |
| 1300 | pr_err("Command ITT: 0x%08x received DataOUT after" |
| 1301 | " last DataOUT received, dumping payload\n", |
| 1302 | cmd->init_task_tag); |
| 1303 | return iscsit_dump_data_payload(conn, payload_length, 1); |
| 1304 | } |
| 1305 | |
| 1306 | if (cmd->data_direction != DMA_TO_DEVICE) { |
| 1307 | pr_err("Command ITT: 0x%08x received DataOUT for a" |
| 1308 | " NON-WRITE command.\n", cmd->init_task_tag); |
Nicholas Bellinger | 97c99b47 | 2014-06-20 10:59:57 -0700 | [diff] [blame] | 1309 | return iscsit_dump_data_payload(conn, payload_length, 1); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1310 | } |
| 1311 | se_cmd = &cmd->se_cmd; |
| 1312 | iscsit_mod_dataout_timer(cmd); |
| 1313 | |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 1314 | if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1315 | pr_err("DataOut Offset: %u, Length %u greater than" |
| 1316 | " iSCSI Command EDTL %u, protocol error.\n", |
Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 1317 | hdr->offset, payload_length, cmd->se_cmd.data_length); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1318 | return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | if (cmd->unsolicited_data) { |
| 1322 | int dump_unsolicited_data = 0; |
| 1323 | |
| 1324 | if (conn->sess->sess_ops->InitialR2T) { |
| 1325 | pr_err("Received unexpected unsolicited data" |
| 1326 | " while InitialR2T=Yes, protocol error.\n"); |
| 1327 | transport_send_check_condition_and_sense(&cmd->se_cmd, |
| 1328 | TCM_UNEXPECTED_UNSOLICITED_DATA, 0); |
| 1329 | return -1; |
| 1330 | } |
| 1331 | /* |
| 1332 | * Special case for dealing with Unsolicited DataOUT |
| 1333 | * and Unsupported SAM WRITE Opcodes and SE resource allocation |
| 1334 | * failures; |
| 1335 | */ |
| 1336 | |
| 1337 | /* Something's amiss if we're not in WRITE_PENDING state... */ |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1338 | WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1339 | if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE)) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1340 | dump_unsolicited_data = 1; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (dump_unsolicited_data) { |
| 1343 | /* |
| 1344 | * Check if a delayed TASK_ABORTED status needs to |
| 1345 | * be sent now if the ISCSI_FLAG_CMD_FINAL has been |
| 1346 | * received with the unsolicitied data out. |
| 1347 | */ |
| 1348 | if (hdr->flags & ISCSI_FLAG_CMD_FINAL) |
| 1349 | iscsit_stop_dataout_timer(cmd); |
| 1350 | |
| 1351 | transport_check_aborted_status(se_cmd, |
| 1352 | (hdr->flags & ISCSI_FLAG_CMD_FINAL)); |
| 1353 | return iscsit_dump_data_payload(conn, payload_length, 1); |
| 1354 | } |
| 1355 | } else { |
| 1356 | /* |
| 1357 | * For the normal solicited data path: |
| 1358 | * |
| 1359 | * Check for a delayed TASK_ABORTED status and dump any |
| 1360 | * incoming data out payload if one exists. Also, when the |
| 1361 | * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current |
| 1362 | * data out sequence, we decrement outstanding_r2ts. Once |
| 1363 | * outstanding_r2ts reaches zero, go ahead and send the delayed |
| 1364 | * TASK_ABORTED status. |
| 1365 | */ |
Christoph Hellwig | 7d680f3 | 2011-12-21 14:13:47 -0500 | [diff] [blame] | 1366 | if (se_cmd->transport_state & CMD_T_ABORTED) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1367 | if (hdr->flags & ISCSI_FLAG_CMD_FINAL) |
| 1368 | if (--cmd->outstanding_r2ts < 1) { |
| 1369 | iscsit_stop_dataout_timer(cmd); |
| 1370 | transport_check_aborted_status( |
| 1371 | se_cmd, 1); |
| 1372 | } |
| 1373 | |
| 1374 | return iscsit_dump_data_payload(conn, payload_length, 1); |
| 1375 | } |
| 1376 | } |
| 1377 | /* |
| 1378 | * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and |
| 1379 | * within-command recovery checks before receiving the payload. |
| 1380 | */ |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1381 | rc = iscsit_check_pre_dataout(cmd, buf); |
| 1382 | if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1383 | return 0; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1384 | else if (rc == DATAOUT_CANNOT_RECOVER) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1385 | return -1; |
| 1386 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1387 | *out_cmd = cmd; |
| 1388 | return 0; |
| 1389 | } |
| 1390 | EXPORT_SYMBOL(iscsit_check_dataout_hdr); |
| 1391 | |
| 1392 | static int |
| 1393 | iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 1394 | struct iscsi_data *hdr) |
| 1395 | { |
| 1396 | struct kvec *iov; |
| 1397 | u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0; |
| 1398 | u32 payload_length = ntoh24(hdr->dlength); |
| 1399 | int iov_ret, data_crc_failed = 0; |
| 1400 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1401 | rx_size += payload_length; |
| 1402 | iov = &cmd->iov_data[0]; |
| 1403 | |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 1404 | iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset), |
| 1405 | payload_length); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1406 | if (iov_ret < 0) |
| 1407 | return -1; |
| 1408 | |
| 1409 | iov_count += iov_ret; |
| 1410 | |
| 1411 | padding = ((-payload_length) & 3); |
| 1412 | if (padding != 0) { |
| 1413 | iov[iov_count].iov_base = cmd->pad_bytes; |
| 1414 | iov[iov_count++].iov_len = padding; |
| 1415 | rx_size += padding; |
| 1416 | pr_debug("Receiving %u padding bytes.\n", padding); |
| 1417 | } |
| 1418 | |
| 1419 | if (conn->conn_ops->DataDigest) { |
| 1420 | iov[iov_count].iov_base = &checksum; |
| 1421 | iov[iov_count++].iov_len = ISCSI_CRC_LEN; |
| 1422 | rx_size += ISCSI_CRC_LEN; |
| 1423 | } |
| 1424 | |
| 1425 | rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size); |
| 1426 | |
| 1427 | iscsit_unmap_iovec(cmd); |
| 1428 | |
| 1429 | if (rx_got != rx_size) |
| 1430 | return -1; |
| 1431 | |
| 1432 | if (conn->conn_ops->DataDigest) { |
| 1433 | u32 data_crc; |
| 1434 | |
| 1435 | data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd, |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 1436 | be32_to_cpu(hdr->offset), |
| 1437 | payload_length, padding, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1438 | cmd->pad_bytes); |
| 1439 | |
| 1440 | if (checksum != data_crc) { |
| 1441 | pr_err("ITT: 0x%08x, Offset: %u, Length: %u," |
| 1442 | " DataSN: 0x%08x, CRC32C DataDigest 0x%08x" |
| 1443 | " does not match computed 0x%08x\n", |
| 1444 | hdr->itt, hdr->offset, payload_length, |
| 1445 | hdr->datasn, checksum, data_crc); |
| 1446 | data_crc_failed = 1; |
| 1447 | } else { |
| 1448 | pr_debug("Got CRC32C DataDigest 0x%08x for" |
| 1449 | " %u bytes of Data Out\n", checksum, |
| 1450 | payload_length); |
| 1451 | } |
| 1452 | } |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1453 | |
| 1454 | return data_crc_failed; |
| 1455 | } |
| 1456 | |
| 1457 | int |
| 1458 | iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr, |
| 1459 | bool data_crc_failed) |
| 1460 | { |
| 1461 | struct iscsi_conn *conn = cmd->conn; |
| 1462 | int rc, ooo_cmdsn; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1463 | /* |
| 1464 | * Increment post receive data and CRC values or perform |
| 1465 | * within-command recovery. |
| 1466 | */ |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1467 | rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed); |
| 1468 | if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1469 | return 0; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1470 | else if (rc == DATAOUT_SEND_R2T) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1471 | iscsit_set_dataout_sequence_values(cmd); |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1472 | conn->conn_transport->iscsit_get_dataout(conn, cmd, false); |
| 1473 | } else if (rc == DATAOUT_SEND_TO_TRANSPORT) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1474 | /* |
| 1475 | * Handle extra special case for out of order |
| 1476 | * Unsolicited Data Out. |
| 1477 | */ |
| 1478 | spin_lock_bh(&cmd->istate_lock); |
| 1479 | ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN); |
| 1480 | cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; |
| 1481 | cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; |
| 1482 | spin_unlock_bh(&cmd->istate_lock); |
| 1483 | |
| 1484 | iscsit_stop_dataout_timer(cmd); |
Christoph Hellwig | 67441b6 | 2012-07-08 15:58:42 -0400 | [diff] [blame] | 1485 | if (ooo_cmdsn) |
| 1486 | return 0; |
| 1487 | target_execute_cmd(&cmd->se_cmd); |
| 1488 | return 0; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1489 | } else /* DATAOUT_CANNOT_RECOVER */ |
| 1490 | return -1; |
| 1491 | |
| 1492 | return 0; |
| 1493 | } |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1494 | EXPORT_SYMBOL(iscsit_check_dataout_payload); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1495 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1496 | static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf) |
| 1497 | { |
Nicholas Bellinger | dbcbc95 | 2013-11-06 20:55:39 -0800 | [diff] [blame] | 1498 | struct iscsi_cmd *cmd = NULL; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1499 | struct iscsi_data *hdr = (struct iscsi_data *)buf; |
| 1500 | int rc; |
| 1501 | bool data_crc_failed = false; |
| 1502 | |
| 1503 | rc = iscsit_check_dataout_hdr(conn, buf, &cmd); |
| 1504 | if (rc < 0) |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1505 | return 0; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1506 | else if (!cmd) |
| 1507 | return 0; |
| 1508 | |
| 1509 | rc = iscsit_get_dataout(conn, cmd, hdr); |
| 1510 | if (rc < 0) |
| 1511 | return rc; |
| 1512 | else if (rc > 0) |
| 1513 | data_crc_failed = true; |
| 1514 | |
| 1515 | return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed); |
| 1516 | } |
| 1517 | |
Nicholas Bellinger | 778de36 | 2013-06-14 16:07:47 -0700 | [diff] [blame] | 1518 | int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 1519 | struct iscsi_nopout *hdr) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1520 | { |
Nicholas Bellinger | 778de36 | 2013-06-14 16:07:47 -0700 | [diff] [blame] | 1521 | u32 payload_length = ntoh24(hdr->dlength); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1522 | |
Arshad Hussain | a366260 | 2014-03-14 15:28:59 -0700 | [diff] [blame] | 1523 | if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) { |
| 1524 | pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n"); |
| 1525 | if (!cmd) |
| 1526 | return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, |
| 1527 | (unsigned char *)hdr); |
| 1528 | |
| 1529 | return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, |
| 1530 | (unsigned char *)hdr); |
| 1531 | } |
| 1532 | |
Christoph Hellwig | 66c7db6 | 2012-09-26 08:00:39 -0400 | [diff] [blame] | 1533 | if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1534 | pr_err("NOPOUT ITT is reserved, but Immediate Bit is" |
| 1535 | " not set, protocol error.\n"); |
Nicholas Bellinger | 28aaa95 | 2013-08-23 22:28:56 -0700 | [diff] [blame] | 1536 | if (!cmd) |
| 1537 | return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, |
| 1538 | (unsigned char *)hdr); |
| 1539 | |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1540 | return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, |
| 1541 | (unsigned char *)hdr); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1544 | if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1545 | pr_err("NOPOUT Ping Data DataSegmentLength: %u is" |
Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1546 | " greater than MaxXmitDataSegmentLength: %u, protocol" |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1547 | " error.\n", payload_length, |
Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1548 | conn->conn_ops->MaxXmitDataSegmentLength); |
Nicholas Bellinger | 28aaa95 | 2013-08-23 22:28:56 -0700 | [diff] [blame] | 1549 | if (!cmd) |
| 1550 | return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, |
| 1551 | (unsigned char *)hdr); |
| 1552 | |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1553 | return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, |
| 1554 | (unsigned char *)hdr); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1557 | pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x," |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1558 | " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n", |
Christoph Hellwig | 66c7db6 | 2012-09-26 08:00:39 -0400 | [diff] [blame] | 1559 | hdr->itt == RESERVED_ITT ? "Response" : "Request", |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1560 | hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn, |
| 1561 | payload_length); |
| 1562 | /* |
| 1563 | * This is not a response to a Unsolicited NopIN, which means |
| 1564 | * it can either be a NOPOUT ping request (with a valid ITT), |
| 1565 | * or a NOPOUT not requesting a NOPIN (with a reserved ITT). |
| 1566 | * Either way, make sure we allocate an struct iscsi_cmd, as both |
| 1567 | * can contain ping data. |
| 1568 | */ |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 1569 | if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1570 | cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT; |
| 1571 | cmd->i_state = ISTATE_SEND_NOPIN; |
| 1572 | cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? |
| 1573 | 1 : 0); |
| 1574 | conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; |
| 1575 | cmd->targ_xfer_tag = 0xFFFFFFFF; |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 1576 | cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); |
| 1577 | cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1578 | cmd->data_direction = DMA_NONE; |
| 1579 | } |
| 1580 | |
Nicholas Bellinger | 778de36 | 2013-06-14 16:07:47 -0700 | [diff] [blame] | 1581 | return 0; |
| 1582 | } |
| 1583 | EXPORT_SYMBOL(iscsit_setup_nop_out); |
| 1584 | |
| 1585 | int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 1586 | struct iscsi_nopout *hdr) |
| 1587 | { |
| 1588 | struct iscsi_cmd *cmd_p = NULL; |
| 1589 | int cmdsn_ret = 0; |
| 1590 | /* |
| 1591 | * Initiator is expecting a NopIN ping reply.. |
| 1592 | */ |
| 1593 | if (hdr->itt != RESERVED_ITT) { |
Nicholas Bellinger | 7cbfcc9 | 2014-05-01 13:44:56 -0700 | [diff] [blame] | 1594 | if (!cmd) |
| 1595 | return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, |
| 1596 | (unsigned char *)hdr); |
Nicholas Bellinger | 778de36 | 2013-06-14 16:07:47 -0700 | [diff] [blame] | 1597 | |
| 1598 | spin_lock_bh(&conn->cmd_lock); |
| 1599 | list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); |
| 1600 | spin_unlock_bh(&conn->cmd_lock); |
| 1601 | |
| 1602 | iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); |
| 1603 | |
| 1604 | if (hdr->opcode & ISCSI_OP_IMMEDIATE) { |
| 1605 | iscsit_add_cmd_to_response_queue(cmd, conn, |
| 1606 | cmd->i_state); |
| 1607 | return 0; |
| 1608 | } |
| 1609 | |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1610 | cmdsn_ret = iscsit_sequence_cmd(conn, cmd, |
| 1611 | (unsigned char *)hdr, hdr->cmdsn); |
Nicholas Bellinger | 778de36 | 2013-06-14 16:07:47 -0700 | [diff] [blame] | 1612 | if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) |
| 1613 | return 0; |
Nicholas Bellinger | 778de36 | 2013-06-14 16:07:47 -0700 | [diff] [blame] | 1614 | if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1615 | return -1; |
Nicholas Bellinger | 778de36 | 2013-06-14 16:07:47 -0700 | [diff] [blame] | 1616 | |
| 1617 | return 0; |
| 1618 | } |
| 1619 | /* |
| 1620 | * This was a response to a unsolicited NOPIN ping. |
| 1621 | */ |
| 1622 | if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) { |
| 1623 | cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt)); |
| 1624 | if (!cmd_p) |
| 1625 | return -EINVAL; |
| 1626 | |
| 1627 | iscsit_stop_nopin_response_timer(conn); |
| 1628 | |
| 1629 | cmd_p->i_state = ISTATE_REMOVE; |
| 1630 | iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state); |
| 1631 | |
| 1632 | iscsit_start_nopin_timer(conn); |
| 1633 | return 0; |
| 1634 | } |
| 1635 | /* |
| 1636 | * Otherwise, initiator is not expecting a NOPIN is response. |
| 1637 | * Just ignore for now. |
| 1638 | */ |
| 1639 | return 0; |
| 1640 | } |
| 1641 | EXPORT_SYMBOL(iscsit_process_nop_out); |
| 1642 | |
| 1643 | static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 1644 | unsigned char *buf) |
| 1645 | { |
| 1646 | unsigned char *ping_data = NULL; |
| 1647 | struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf; |
| 1648 | struct kvec *iov = NULL; |
| 1649 | u32 payload_length = ntoh24(hdr->dlength); |
| 1650 | int ret; |
| 1651 | |
| 1652 | ret = iscsit_setup_nop_out(conn, cmd, hdr); |
| 1653 | if (ret < 0) |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1654 | return 0; |
Nicholas Bellinger | 778de36 | 2013-06-14 16:07:47 -0700 | [diff] [blame] | 1655 | /* |
| 1656 | * Handle NOP-OUT payload for traditional iSCSI sockets |
| 1657 | */ |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 1658 | if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) { |
Nicholas Bellinger | 778de36 | 2013-06-14 16:07:47 -0700 | [diff] [blame] | 1659 | u32 checksum, data_crc, padding = 0; |
| 1660 | int niov = 0, rx_got, rx_size = payload_length; |
| 1661 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1662 | ping_data = kzalloc(payload_length + 1, GFP_KERNEL); |
| 1663 | if (!ping_data) { |
| 1664 | pr_err("Unable to allocate memory for" |
| 1665 | " NOPOUT ping data.\n"); |
| 1666 | ret = -1; |
| 1667 | goto out; |
| 1668 | } |
| 1669 | |
| 1670 | iov = &cmd->iov_misc[0]; |
| 1671 | iov[niov].iov_base = ping_data; |
| 1672 | iov[niov++].iov_len = payload_length; |
| 1673 | |
| 1674 | padding = ((-payload_length) & 3); |
| 1675 | if (padding != 0) { |
| 1676 | pr_debug("Receiving %u additional bytes" |
| 1677 | " for padding.\n", padding); |
| 1678 | iov[niov].iov_base = &cmd->pad_bytes; |
| 1679 | iov[niov++].iov_len = padding; |
| 1680 | rx_size += padding; |
| 1681 | } |
| 1682 | if (conn->conn_ops->DataDigest) { |
| 1683 | iov[niov].iov_base = &checksum; |
| 1684 | iov[niov++].iov_len = ISCSI_CRC_LEN; |
| 1685 | rx_size += ISCSI_CRC_LEN; |
| 1686 | } |
| 1687 | |
| 1688 | rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size); |
| 1689 | if (rx_got != rx_size) { |
| 1690 | ret = -1; |
| 1691 | goto out; |
| 1692 | } |
| 1693 | |
| 1694 | if (conn->conn_ops->DataDigest) { |
| 1695 | iscsit_do_crypto_hash_buf(&conn->conn_rx_hash, |
| 1696 | ping_data, payload_length, |
| 1697 | padding, cmd->pad_bytes, |
| 1698 | (u8 *)&data_crc); |
| 1699 | |
| 1700 | if (checksum != data_crc) { |
| 1701 | pr_err("Ping data CRC32C DataDigest" |
| 1702 | " 0x%08x does not match computed 0x%08x\n", |
| 1703 | checksum, data_crc); |
| 1704 | if (!conn->sess->sess_ops->ErrorRecoveryLevel) { |
| 1705 | pr_err("Unable to recover from" |
| 1706 | " NOPOUT Ping DataCRC failure while in" |
| 1707 | " ERL=0.\n"); |
| 1708 | ret = -1; |
| 1709 | goto out; |
| 1710 | } else { |
| 1711 | /* |
| 1712 | * Silently drop this PDU and let the |
| 1713 | * initiator plug the CmdSN gap. |
| 1714 | */ |
| 1715 | pr_debug("Dropping NOPOUT" |
| 1716 | " Command CmdSN: 0x%08x due to" |
| 1717 | " DataCRC error.\n", hdr->cmdsn); |
| 1718 | ret = 0; |
| 1719 | goto out; |
| 1720 | } |
| 1721 | } else { |
| 1722 | pr_debug("Got CRC32C DataDigest" |
| 1723 | " 0x%08x for %u bytes of ping data.\n", |
| 1724 | checksum, payload_length); |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | ping_data[payload_length] = '\0'; |
| 1729 | /* |
| 1730 | * Attach ping data to struct iscsi_cmd->buf_ptr. |
| 1731 | */ |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 1732 | cmd->buf_ptr = ping_data; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1733 | cmd->buf_ptr_size = payload_length; |
| 1734 | |
| 1735 | pr_debug("Got %u bytes of NOPOUT ping" |
| 1736 | " data.\n", payload_length); |
| 1737 | pr_debug("Ping Data: \"%s\"\n", ping_data); |
| 1738 | } |
| 1739 | |
Nicholas Bellinger | 778de36 | 2013-06-14 16:07:47 -0700 | [diff] [blame] | 1740 | return iscsit_process_nop_out(conn, cmd, hdr); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1741 | out: |
| 1742 | if (cmd) |
Nicholas Bellinger | aafc9d1 | 2013-05-31 00:49:41 -0700 | [diff] [blame] | 1743 | iscsit_free_cmd(cmd, false); |
Nicholas Bellinger | 778de36 | 2013-06-14 16:07:47 -0700 | [diff] [blame] | 1744 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1745 | kfree(ping_data); |
| 1746 | return ret; |
| 1747 | } |
| 1748 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1749 | int |
| 1750 | iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 1751 | unsigned char *buf) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1752 | { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1753 | struct se_tmr_req *se_tmr; |
| 1754 | struct iscsi_tmr_req *tmr_req; |
| 1755 | struct iscsi_tm *hdr; |
Nicholas Bellinger | 186a964 | 2013-07-03 03:11:48 -0700 | [diff] [blame] | 1756 | int out_of_order_cmdsn = 0, ret; |
| 1757 | bool sess_ref = false; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1758 | u8 function; |
| 1759 | |
| 1760 | hdr = (struct iscsi_tm *) buf; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1761 | hdr->flags &= ~ISCSI_FLAG_CMD_FINAL; |
| 1762 | function = hdr->flags; |
| 1763 | |
| 1764 | pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:" |
| 1765 | " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:" |
| 1766 | " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function, |
| 1767 | hdr->rtt, hdr->refcmdsn, conn->cid); |
| 1768 | |
| 1769 | if ((function != ISCSI_TM_FUNC_ABORT_TASK) && |
| 1770 | ((function != ISCSI_TM_FUNC_TASK_REASSIGN) && |
Christoph Hellwig | 66c7db6 | 2012-09-26 08:00:39 -0400 | [diff] [blame] | 1771 | hdr->rtt != RESERVED_ITT)) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1772 | pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n"); |
Christoph Hellwig | 66c7db6 | 2012-09-26 08:00:39 -0400 | [diff] [blame] | 1773 | hdr->rtt = RESERVED_ITT; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) && |
| 1777 | !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { |
| 1778 | pr_err("Task Management Request TASK_REASSIGN not" |
| 1779 | " issued as immediate command, bad iSCSI Initiator" |
| 1780 | "implementation\n"); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1781 | return iscsit_add_reject_cmd(cmd, |
| 1782 | ISCSI_REASON_PROTOCOL_ERROR, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1783 | } |
| 1784 | if ((function != ISCSI_TM_FUNC_ABORT_TASK) && |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 1785 | be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG) |
| 1786 | hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1787 | |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 1788 | cmd->data_direction = DMA_NONE; |
| 1789 | |
| 1790 | cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL); |
| 1791 | if (!cmd->tmr_req) { |
| 1792 | pr_err("Unable to allocate memory for" |
| 1793 | " Task Management command!\n"); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1794 | return iscsit_add_reject_cmd(cmd, |
| 1795 | ISCSI_REASON_BOOKMARK_NO_RESOURCES, |
| 1796 | buf); |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | /* |
| 1800 | * TASK_REASSIGN for ERL=2 / connection stays inside of |
| 1801 | * LIO-Target $FABRIC_MOD |
| 1802 | */ |
| 1803 | if (function != ISCSI_TM_FUNC_TASK_REASSIGN) { |
| 1804 | |
| 1805 | u8 tcm_function; |
| 1806 | int ret; |
| 1807 | |
| 1808 | transport_init_se_cmd(&cmd->se_cmd, |
| 1809 | &lio_target_fabric_configfs->tf_ops, |
| 1810 | conn->sess->se_sess, 0, DMA_NONE, |
Christoph Hellwig | 68d81f4 | 2014-11-24 07:07:25 -0800 | [diff] [blame] | 1811 | TCM_SIMPLE_TAG, cmd->sense_buffer + 2); |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 1812 | |
Nicholas Bellinger | 186a964 | 2013-07-03 03:11:48 -0700 | [diff] [blame] | 1813 | target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true); |
| 1814 | sess_ref = true; |
| 1815 | |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 1816 | switch (function) { |
| 1817 | case ISCSI_TM_FUNC_ABORT_TASK: |
| 1818 | tcm_function = TMR_ABORT_TASK; |
| 1819 | break; |
| 1820 | case ISCSI_TM_FUNC_ABORT_TASK_SET: |
| 1821 | tcm_function = TMR_ABORT_TASK_SET; |
| 1822 | break; |
| 1823 | case ISCSI_TM_FUNC_CLEAR_ACA: |
| 1824 | tcm_function = TMR_CLEAR_ACA; |
| 1825 | break; |
| 1826 | case ISCSI_TM_FUNC_CLEAR_TASK_SET: |
| 1827 | tcm_function = TMR_CLEAR_TASK_SET; |
| 1828 | break; |
| 1829 | case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: |
| 1830 | tcm_function = TMR_LUN_RESET; |
| 1831 | break; |
| 1832 | case ISCSI_TM_FUNC_TARGET_WARM_RESET: |
| 1833 | tcm_function = TMR_TARGET_WARM_RESET; |
| 1834 | break; |
| 1835 | case ISCSI_TM_FUNC_TARGET_COLD_RESET: |
| 1836 | tcm_function = TMR_TARGET_COLD_RESET; |
| 1837 | break; |
| 1838 | default: |
| 1839 | pr_err("Unknown iSCSI TMR Function:" |
| 1840 | " 0x%02x\n", function); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1841 | return iscsit_add_reject_cmd(cmd, |
| 1842 | ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, |
| 1846 | tcm_function, GFP_KERNEL); |
| 1847 | if (ret < 0) |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1848 | return iscsit_add_reject_cmd(cmd, |
| 1849 | ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); |
Andy Grover | d28b1169 | 2012-04-03 15:51:22 -0700 | [diff] [blame] | 1850 | |
| 1851 | cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req; |
| 1852 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1853 | |
| 1854 | cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC; |
| 1855 | cmd->i_state = ISTATE_SEND_TASKMGTRSP; |
| 1856 | cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); |
| 1857 | cmd->init_task_tag = hdr->itt; |
| 1858 | cmd->targ_xfer_tag = 0xFFFFFFFF; |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 1859 | cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); |
| 1860 | cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1861 | se_tmr = cmd->se_cmd.se_tmr_req; |
| 1862 | tmr_req = cmd->tmr_req; |
| 1863 | /* |
| 1864 | * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN |
| 1865 | */ |
| 1866 | if (function != ISCSI_TM_FUNC_TASK_REASSIGN) { |
Andy Grover | 4f26998 | 2012-01-19 13:39:14 -0800 | [diff] [blame] | 1867 | ret = transport_lookup_tmr_lun(&cmd->se_cmd, |
| 1868 | scsilun_to_int(&hdr->lun)); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1869 | if (ret < 0) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1870 | se_tmr->response = ISCSI_TMF_RSP_NO_LUN; |
| 1871 | goto attach; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | switch (function) { |
| 1876 | case ISCSI_TM_FUNC_ABORT_TASK: |
| 1877 | se_tmr->response = iscsit_tmr_abort_task(cmd, buf); |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1878 | if (se_tmr->response) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1879 | goto attach; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1880 | break; |
| 1881 | case ISCSI_TM_FUNC_ABORT_TASK_SET: |
| 1882 | case ISCSI_TM_FUNC_CLEAR_ACA: |
| 1883 | case ISCSI_TM_FUNC_CLEAR_TASK_SET: |
| 1884 | case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: |
| 1885 | break; |
| 1886 | case ISCSI_TM_FUNC_TARGET_WARM_RESET: |
| 1887 | if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1888 | se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED; |
| 1889 | goto attach; |
| 1890 | } |
| 1891 | break; |
| 1892 | case ISCSI_TM_FUNC_TARGET_COLD_RESET: |
| 1893 | if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1894 | se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED; |
| 1895 | goto attach; |
| 1896 | } |
| 1897 | break; |
| 1898 | case ISCSI_TM_FUNC_TASK_REASSIGN: |
| 1899 | se_tmr->response = iscsit_tmr_task_reassign(cmd, buf); |
| 1900 | /* |
| 1901 | * Perform sanity checks on the ExpDataSN only if the |
| 1902 | * TASK_REASSIGN was successful. |
| 1903 | */ |
Christoph Hellwig | de103c9 | 2012-11-06 12:24:09 -0800 | [diff] [blame] | 1904 | if (se_tmr->response) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1905 | break; |
| 1906 | |
| 1907 | if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0) |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1908 | return iscsit_add_reject_cmd(cmd, |
| 1909 | ISCSI_REASON_BOOKMARK_INVALID, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1910 | break; |
| 1911 | default: |
| 1912 | pr_err("Unknown TMR function: 0x%02x, protocol" |
| 1913 | " error.\n", function); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1914 | se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED; |
| 1915 | goto attach; |
| 1916 | } |
| 1917 | |
| 1918 | if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) && |
| 1919 | (se_tmr->response == ISCSI_TMF_RSP_COMPLETE)) |
| 1920 | se_tmr->call_transport = 1; |
| 1921 | attach: |
| 1922 | spin_lock_bh(&conn->cmd_lock); |
Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 1923 | list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1924 | spin_unlock_bh(&conn->cmd_lock); |
| 1925 | |
| 1926 | if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) { |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 1927 | int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1928 | if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP) |
| 1929 | out_of_order_cmdsn = 1; |
Nicholas Bellinger | 5a4c866 | 2011-10-28 13:37:19 -0700 | [diff] [blame] | 1930 | else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1931 | return 0; |
Nicholas Bellinger | 5a4c866 | 2011-10-28 13:37:19 -0700 | [diff] [blame] | 1932 | else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1933 | return -1; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1934 | } |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 1935 | iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1936 | |
Nicholas Bellinger | 5a4c866 | 2011-10-28 13:37:19 -0700 | [diff] [blame] | 1937 | if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE)) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1938 | return 0; |
| 1939 | /* |
| 1940 | * Found the referenced task, send to transport for processing. |
| 1941 | */ |
| 1942 | if (se_tmr->call_transport) |
| 1943 | return transport_generic_handle_tmr(&cmd->se_cmd); |
| 1944 | |
| 1945 | /* |
| 1946 | * Could not find the referenced LUN, task, or Task Management |
| 1947 | * command not authorized or supported. Change state and |
| 1948 | * let the tx_thread send the response. |
| 1949 | * |
| 1950 | * For connection recovery, this is also the default action for |
| 1951 | * TMR TASK_REASSIGN. |
| 1952 | */ |
Nicholas Bellinger | 186a964 | 2013-07-03 03:11:48 -0700 | [diff] [blame] | 1953 | if (sess_ref) { |
| 1954 | pr_debug("Handle TMR, using sess_ref=true check\n"); |
| 1955 | target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd); |
| 1956 | } |
| 1957 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1958 | iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); |
| 1959 | return 0; |
| 1960 | } |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 1961 | EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1962 | |
| 1963 | /* #warning FIXME: Support Text Command parameters besides SendTargets */ |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 1964 | int |
| 1965 | iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 1966 | struct iscsi_text *hdr) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1967 | { |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 1968 | u32 payload_length = ntoh24(hdr->dlength); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1969 | |
Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1970 | if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1971 | pr_err("Unable to accept text parameter length: %u" |
Nicholas Bellinger | 21f5aa7 | 2012-09-29 21:51:26 -0700 | [diff] [blame] | 1972 | "greater than MaxXmitDataSegmentLength %u.\n", |
| 1973 | payload_length, conn->conn_ops->MaxXmitDataSegmentLength); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 1974 | return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, |
| 1975 | (unsigned char *)hdr); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Nicholas Bellinger | 122f8af | 2013-11-13 14:33:24 -0800 | [diff] [blame] | 1978 | if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) || |
| 1979 | (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) { |
| 1980 | pr_err("Multi sequence text commands currently not supported\n"); |
| 1981 | return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED, |
| 1982 | (unsigned char *)hdr); |
| 1983 | } |
| 1984 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1985 | pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x," |
| 1986 | " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn, |
| 1987 | hdr->exp_statsn, payload_length); |
| 1988 | |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 1989 | cmd->iscsi_opcode = ISCSI_OP_TEXT; |
| 1990 | cmd->i_state = ISTATE_SEND_TEXTRSP; |
| 1991 | cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); |
| 1992 | conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; |
| 1993 | cmd->targ_xfer_tag = 0xFFFFFFFF; |
| 1994 | cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); |
| 1995 | cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); |
| 1996 | cmd->data_direction = DMA_NONE; |
| 1997 | |
| 1998 | return 0; |
| 1999 | } |
| 2000 | EXPORT_SYMBOL(iscsit_setup_text_cmd); |
| 2001 | |
| 2002 | int |
| 2003 | iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 2004 | struct iscsi_text *hdr) |
| 2005 | { |
Nicholas Bellinger | 9864ca9 | 2013-06-19 22:43:11 -0700 | [diff] [blame] | 2006 | unsigned char *text_in = cmd->text_in_ptr, *text_ptr; |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2007 | int cmdsn_ret; |
| 2008 | |
Nicholas Bellinger | 9864ca9 | 2013-06-19 22:43:11 -0700 | [diff] [blame] | 2009 | if (!text_in) { |
| 2010 | pr_err("Unable to locate text_in buffer for sendtargets" |
| 2011 | " discovery\n"); |
| 2012 | goto reject; |
| 2013 | } |
| 2014 | if (strncmp("SendTargets", text_in, 11) != 0) { |
| 2015 | pr_err("Received Text Data that is not" |
| 2016 | " SendTargets, cannot continue.\n"); |
| 2017 | goto reject; |
| 2018 | } |
| 2019 | text_ptr = strchr(text_in, '='); |
| 2020 | if (!text_ptr) { |
| 2021 | pr_err("No \"=\" separator found in Text Data," |
| 2022 | " cannot continue.\n"); |
| 2023 | goto reject; |
| 2024 | } |
| 2025 | if (!strncmp("=All", text_ptr, 4)) { |
| 2026 | cmd->cmd_flags |= IFC_SENDTARGETS_ALL; |
Nicholas Bellinger | 6665889 | 2013-06-19 22:45:42 -0700 | [diff] [blame] | 2027 | } else if (!strncmp("=iqn.", text_ptr, 5) || |
| 2028 | !strncmp("=eui.", text_ptr, 5)) { |
| 2029 | cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE; |
Nicholas Bellinger | 9864ca9 | 2013-06-19 22:43:11 -0700 | [diff] [blame] | 2030 | } else { |
| 2031 | pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr); |
| 2032 | goto reject; |
| 2033 | } |
| 2034 | |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2035 | spin_lock_bh(&conn->cmd_lock); |
| 2036 | list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); |
| 2037 | spin_unlock_bh(&conn->cmd_lock); |
| 2038 | |
| 2039 | iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); |
| 2040 | |
| 2041 | if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) { |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 2042 | cmdsn_ret = iscsit_sequence_cmd(conn, cmd, |
| 2043 | (unsigned char *)hdr, hdr->cmdsn); |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2044 | if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2045 | return -1; |
| 2046 | |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2047 | return 0; |
| 2048 | } |
| 2049 | |
| 2050 | return iscsit_execute_cmd(cmd, 0); |
Nicholas Bellinger | 9864ca9 | 2013-06-19 22:43:11 -0700 | [diff] [blame] | 2051 | |
| 2052 | reject: |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2053 | return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, |
| 2054 | (unsigned char *)hdr); |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2055 | } |
| 2056 | EXPORT_SYMBOL(iscsit_process_text_cmd); |
| 2057 | |
| 2058 | static int |
| 2059 | iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 2060 | unsigned char *buf) |
| 2061 | { |
| 2062 | struct iscsi_text *hdr = (struct iscsi_text *)buf; |
| 2063 | char *text_in = NULL; |
| 2064 | u32 payload_length = ntoh24(hdr->dlength); |
| 2065 | int rx_size, rc; |
| 2066 | |
| 2067 | rc = iscsit_setup_text_cmd(conn, cmd, hdr); |
| 2068 | if (rc < 0) |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 2069 | return 0; |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2070 | |
| 2071 | rx_size = payload_length; |
| 2072 | if (payload_length) { |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2073 | u32 checksum = 0, data_crc = 0; |
| 2074 | u32 padding = 0, pad_bytes = 0; |
| 2075 | int niov = 0, rx_got; |
| 2076 | struct kvec iov[3]; |
| 2077 | |
| 2078 | text_in = kzalloc(payload_length, GFP_KERNEL); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2079 | if (!text_in) { |
| 2080 | pr_err("Unable to allocate memory for" |
| 2081 | " incoming text parameters\n"); |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2082 | goto reject; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2083 | } |
Nicholas Bellinger | 9864ca9 | 2013-06-19 22:43:11 -0700 | [diff] [blame] | 2084 | cmd->text_in_ptr = text_in; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2085 | |
| 2086 | memset(iov, 0, 3 * sizeof(struct kvec)); |
| 2087 | iov[niov].iov_base = text_in; |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2088 | iov[niov++].iov_len = payload_length; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2089 | |
| 2090 | padding = ((-payload_length) & 3); |
| 2091 | if (padding != 0) { |
Nicholas Bellinger | 76f1928 | 2011-07-27 12:16:22 -0700 | [diff] [blame] | 2092 | iov[niov].iov_base = &pad_bytes; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2093 | iov[niov++].iov_len = padding; |
| 2094 | rx_size += padding; |
| 2095 | pr_debug("Receiving %u additional bytes" |
| 2096 | " for padding.\n", padding); |
| 2097 | } |
| 2098 | if (conn->conn_ops->DataDigest) { |
| 2099 | iov[niov].iov_base = &checksum; |
| 2100 | iov[niov++].iov_len = ISCSI_CRC_LEN; |
| 2101 | rx_size += ISCSI_CRC_LEN; |
| 2102 | } |
| 2103 | |
| 2104 | rx_got = rx_data(conn, &iov[0], niov, rx_size); |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2105 | if (rx_got != rx_size) |
| 2106 | goto reject; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2107 | |
| 2108 | if (conn->conn_ops->DataDigest) { |
| 2109 | iscsit_do_crypto_hash_buf(&conn->conn_rx_hash, |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2110 | text_in, payload_length, |
Nicholas Bellinger | 76f1928 | 2011-07-27 12:16:22 -0700 | [diff] [blame] | 2111 | padding, (u8 *)&pad_bytes, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2112 | (u8 *)&data_crc); |
| 2113 | |
| 2114 | if (checksum != data_crc) { |
| 2115 | pr_err("Text data CRC32C DataDigest" |
| 2116 | " 0x%08x does not match computed" |
| 2117 | " 0x%08x\n", checksum, data_crc); |
| 2118 | if (!conn->sess->sess_ops->ErrorRecoveryLevel) { |
| 2119 | pr_err("Unable to recover from" |
| 2120 | " Text Data digest failure while in" |
| 2121 | " ERL=0.\n"); |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2122 | goto reject; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2123 | } else { |
| 2124 | /* |
| 2125 | * Silently drop this PDU and let the |
| 2126 | * initiator plug the CmdSN gap. |
| 2127 | */ |
| 2128 | pr_debug("Dropping Text" |
| 2129 | " Command CmdSN: 0x%08x due to" |
| 2130 | " DataCRC error.\n", hdr->cmdsn); |
| 2131 | kfree(text_in); |
| 2132 | return 0; |
| 2133 | } |
| 2134 | } else { |
| 2135 | pr_debug("Got CRC32C DataDigest" |
| 2136 | " 0x%08x for %u bytes of text data.\n", |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2137 | checksum, payload_length); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2138 | } |
| 2139 | } |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2140 | text_in[payload_length - 1] = '\0'; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2141 | pr_debug("Successfully read %d bytes of text" |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2142 | " data.\n", payload_length); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2145 | return iscsit_process_text_cmd(conn, cmd, hdr); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2146 | |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2147 | reject: |
Nicholas Bellinger | 9864ca9 | 2013-06-19 22:43:11 -0700 | [diff] [blame] | 2148 | kfree(cmd->text_in_ptr); |
| 2149 | cmd->text_in_ptr = NULL; |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2150 | return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2151 | } |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 2152 | EXPORT_SYMBOL(iscsit_handle_text_cmd); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2153 | |
| 2154 | int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn) |
| 2155 | { |
| 2156 | struct iscsi_conn *conn_p; |
| 2157 | struct iscsi_session *sess = conn->sess; |
| 2158 | |
| 2159 | pr_debug("Received logout request CLOSESESSION on CID: %hu" |
| 2160 | " for SID: %u.\n", conn->cid, conn->sess->sid); |
| 2161 | |
| 2162 | atomic_set(&sess->session_logout, 1); |
| 2163 | atomic_set(&conn->conn_logout_remove, 1); |
| 2164 | conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION; |
| 2165 | |
| 2166 | iscsit_inc_conn_usage_count(conn); |
| 2167 | iscsit_inc_session_usage_count(sess); |
| 2168 | |
| 2169 | spin_lock_bh(&sess->conn_lock); |
| 2170 | list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) { |
| 2171 | if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN) |
| 2172 | continue; |
| 2173 | |
| 2174 | pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); |
| 2175 | conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT; |
| 2176 | } |
| 2177 | spin_unlock_bh(&sess->conn_lock); |
| 2178 | |
| 2179 | iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); |
| 2180 | |
| 2181 | return 0; |
| 2182 | } |
| 2183 | |
| 2184 | int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn) |
| 2185 | { |
| 2186 | struct iscsi_conn *l_conn; |
| 2187 | struct iscsi_session *sess = conn->sess; |
| 2188 | |
| 2189 | pr_debug("Received logout request CLOSECONNECTION for CID:" |
| 2190 | " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid); |
| 2191 | |
| 2192 | /* |
| 2193 | * A Logout Request with a CLOSECONNECTION reason code for a CID |
| 2194 | * can arrive on a connection with a differing CID. |
| 2195 | */ |
| 2196 | if (conn->cid == cmd->logout_cid) { |
| 2197 | spin_lock_bh(&conn->state_lock); |
| 2198 | pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); |
| 2199 | conn->conn_state = TARG_CONN_STATE_IN_LOGOUT; |
| 2200 | |
| 2201 | atomic_set(&conn->conn_logout_remove, 1); |
| 2202 | conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION; |
| 2203 | iscsit_inc_conn_usage_count(conn); |
| 2204 | |
| 2205 | spin_unlock_bh(&conn->state_lock); |
| 2206 | } else { |
| 2207 | /* |
| 2208 | * Handle all different cid CLOSECONNECTION requests in |
| 2209 | * iscsit_logout_post_handler_diffcid() as to give enough |
| 2210 | * time for any non immediate command's CmdSN to be |
| 2211 | * acknowledged on the connection in question. |
| 2212 | * |
| 2213 | * Here we simply make sure the CID is still around. |
| 2214 | */ |
| 2215 | l_conn = iscsit_get_conn_from_cid(sess, |
| 2216 | cmd->logout_cid); |
| 2217 | if (!l_conn) { |
| 2218 | cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND; |
| 2219 | iscsit_add_cmd_to_response_queue(cmd, conn, |
| 2220 | cmd->i_state); |
| 2221 | return 0; |
| 2222 | } |
| 2223 | |
| 2224 | iscsit_dec_conn_usage_count(l_conn); |
| 2225 | } |
| 2226 | |
| 2227 | iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); |
| 2228 | |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
| 2232 | int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn) |
| 2233 | { |
| 2234 | struct iscsi_session *sess = conn->sess; |
| 2235 | |
| 2236 | pr_debug("Received explicit REMOVECONNFORRECOVERY logout for" |
| 2237 | " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid); |
| 2238 | |
| 2239 | if (sess->sess_ops->ErrorRecoveryLevel != 2) { |
| 2240 | pr_err("Received Logout Request REMOVECONNFORRECOVERY" |
| 2241 | " while ERL!=2.\n"); |
| 2242 | cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED; |
| 2243 | iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); |
| 2244 | return 0; |
| 2245 | } |
| 2246 | |
| 2247 | if (conn->cid == cmd->logout_cid) { |
| 2248 | pr_err("Received Logout Request REMOVECONNFORRECOVERY" |
| 2249 | " with CID: %hu on CID: %hu, implementation error.\n", |
| 2250 | cmd->logout_cid, conn->cid); |
| 2251 | cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED; |
| 2252 | iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); |
| 2253 | return 0; |
| 2254 | } |
| 2255 | |
| 2256 | iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state); |
| 2257 | |
| 2258 | return 0; |
| 2259 | } |
| 2260 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 2261 | int |
| 2262 | iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd, |
| 2263 | unsigned char *buf) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2264 | { |
| 2265 | int cmdsn_ret, logout_remove = 0; |
| 2266 | u8 reason_code = 0; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2267 | struct iscsi_logout *hdr; |
| 2268 | struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn); |
| 2269 | |
| 2270 | hdr = (struct iscsi_logout *) buf; |
| 2271 | reason_code = (hdr->flags & 0x7f); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2272 | |
| 2273 | if (tiqn) { |
| 2274 | spin_lock(&tiqn->logout_stats.lock); |
| 2275 | if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) |
| 2276 | tiqn->logout_stats.normal_logouts++; |
| 2277 | else |
| 2278 | tiqn->logout_stats.abnormal_logouts++; |
| 2279 | spin_unlock(&tiqn->logout_stats.lock); |
| 2280 | } |
| 2281 | |
| 2282 | pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x" |
| 2283 | " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n", |
| 2284 | hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code, |
| 2285 | hdr->cid, conn->cid); |
| 2286 | |
| 2287 | if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) { |
| 2288 | pr_err("Received logout request on connection that" |
| 2289 | " is not in logged in state, ignoring request.\n"); |
Nicholas Bellinger | aafc9d1 | 2013-05-31 00:49:41 -0700 | [diff] [blame] | 2290 | iscsit_free_cmd(cmd, false); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2291 | return 0; |
| 2292 | } |
| 2293 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2294 | cmd->iscsi_opcode = ISCSI_OP_LOGOUT; |
| 2295 | cmd->i_state = ISTATE_SEND_LOGOUTRSP; |
| 2296 | cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0); |
| 2297 | conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt; |
| 2298 | cmd->targ_xfer_tag = 0xFFFFFFFF; |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 2299 | cmd->cmd_sn = be32_to_cpu(hdr->cmdsn); |
| 2300 | cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn); |
| 2301 | cmd->logout_cid = be16_to_cpu(hdr->cid); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2302 | cmd->logout_reason = reason_code; |
| 2303 | cmd->data_direction = DMA_NONE; |
| 2304 | |
| 2305 | /* |
| 2306 | * We need to sleep in these cases (by returning 1) until the Logout |
| 2307 | * Response gets sent in the tx thread. |
| 2308 | */ |
| 2309 | if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) || |
| 2310 | ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) && |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 2311 | be16_to_cpu(hdr->cid) == conn->cid)) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2312 | logout_remove = 1; |
| 2313 | |
| 2314 | spin_lock_bh(&conn->cmd_lock); |
Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 2315 | list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2316 | spin_unlock_bh(&conn->cmd_lock); |
| 2317 | |
| 2318 | if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY) |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 2319 | iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn)); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2320 | |
| 2321 | /* |
| 2322 | * Immediate commands are executed, well, immediately. |
| 2323 | * Non-Immediate Logout Commands are executed in CmdSN order. |
| 2324 | */ |
Andy Grover | c6037cc | 2012-04-03 15:51:02 -0700 | [diff] [blame] | 2325 | if (cmd->immediate_cmd) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2326 | int ret = iscsit_execute_cmd(cmd, 0); |
| 2327 | |
| 2328 | if (ret < 0) |
| 2329 | return ret; |
| 2330 | } else { |
Nicholas Bellinger | 561bf15 | 2013-07-03 03:58:58 -0700 | [diff] [blame] | 2331 | cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2332 | if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2333 | logout_remove = 0; |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2334 | else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) |
| 2335 | return -1; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | return logout_remove; |
| 2339 | } |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 2340 | EXPORT_SYMBOL(iscsit_handle_logout_cmd); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2341 | |
| 2342 | static int iscsit_handle_snack( |
| 2343 | struct iscsi_conn *conn, |
| 2344 | unsigned char *buf) |
| 2345 | { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2346 | struct iscsi_snack *hdr; |
| 2347 | |
| 2348 | hdr = (struct iscsi_snack *) buf; |
| 2349 | hdr->flags &= ~ISCSI_FLAG_CMD_FINAL; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2350 | |
| 2351 | pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:" |
| 2352 | " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x," |
| 2353 | " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags, |
| 2354 | hdr->begrun, hdr->runlength, conn->cid); |
| 2355 | |
| 2356 | if (!conn->sess->sess_ops->ErrorRecoveryLevel) { |
| 2357 | pr_err("Initiator sent SNACK request while in" |
| 2358 | " ErrorRecoveryLevel=0.\n"); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2359 | return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, |
| 2360 | buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2361 | } |
| 2362 | /* |
| 2363 | * SNACK_DATA and SNACK_R2T are both 0, so check which function to |
| 2364 | * call from inside iscsi_send_recovery_datain_or_r2t(). |
| 2365 | */ |
| 2366 | switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) { |
| 2367 | case 0: |
| 2368 | return iscsit_handle_recovery_datain_or_r2t(conn, buf, |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 2369 | hdr->itt, |
| 2370 | be32_to_cpu(hdr->ttt), |
| 2371 | be32_to_cpu(hdr->begrun), |
| 2372 | be32_to_cpu(hdr->runlength)); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2373 | case ISCSI_FLAG_SNACK_TYPE_STATUS: |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 2374 | return iscsit_handle_status_snack(conn, hdr->itt, |
| 2375 | be32_to_cpu(hdr->ttt), |
| 2376 | be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength)); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2377 | case ISCSI_FLAG_SNACK_TYPE_DATA_ACK: |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 2378 | return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt), |
| 2379 | be32_to_cpu(hdr->begrun), |
| 2380 | be32_to_cpu(hdr->runlength)); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2381 | case ISCSI_FLAG_SNACK_TYPE_RDATA: |
| 2382 | /* FIXME: Support R-Data SNACK */ |
| 2383 | pr_err("R-Data SNACK Not Supported.\n"); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2384 | return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, |
| 2385 | buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2386 | default: |
| 2387 | pr_err("Unknown SNACK type 0x%02x, protocol" |
| 2388 | " error.\n", hdr->flags & 0x0f); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2389 | return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, |
| 2390 | buf); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | return 0; |
| 2394 | } |
| 2395 | |
| 2396 | static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn) |
| 2397 | { |
| 2398 | if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) || |
| 2399 | (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) { |
| 2400 | wait_for_completion_interruptible_timeout( |
| 2401 | &conn->rx_half_close_comp, |
| 2402 | ISCSI_RX_THREAD_TCP_TIMEOUT * HZ); |
| 2403 | } |
| 2404 | } |
| 2405 | |
| 2406 | static int iscsit_handle_immediate_data( |
| 2407 | struct iscsi_cmd *cmd, |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 2408 | struct iscsi_scsi_req *hdr, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2409 | u32 length) |
| 2410 | { |
| 2411 | int iov_ret, rx_got = 0, rx_size = 0; |
| 2412 | u32 checksum, iov_count = 0, padding = 0; |
| 2413 | struct iscsi_conn *conn = cmd->conn; |
| 2414 | struct kvec *iov; |
| 2415 | |
| 2416 | iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length); |
| 2417 | if (iov_ret < 0) |
| 2418 | return IMMEDIATE_DATA_CANNOT_RECOVER; |
| 2419 | |
| 2420 | rx_size = length; |
| 2421 | iov_count = iov_ret; |
| 2422 | iov = &cmd->iov_data[0]; |
| 2423 | |
| 2424 | padding = ((-length) & 3); |
| 2425 | if (padding != 0) { |
| 2426 | iov[iov_count].iov_base = cmd->pad_bytes; |
| 2427 | iov[iov_count++].iov_len = padding; |
| 2428 | rx_size += padding; |
| 2429 | } |
| 2430 | |
| 2431 | if (conn->conn_ops->DataDigest) { |
| 2432 | iov[iov_count].iov_base = &checksum; |
| 2433 | iov[iov_count++].iov_len = ISCSI_CRC_LEN; |
| 2434 | rx_size += ISCSI_CRC_LEN; |
| 2435 | } |
| 2436 | |
| 2437 | rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size); |
| 2438 | |
| 2439 | iscsit_unmap_iovec(cmd); |
| 2440 | |
| 2441 | if (rx_got != rx_size) { |
| 2442 | iscsit_rx_thread_wait_for_tcp(conn); |
| 2443 | return IMMEDIATE_DATA_CANNOT_RECOVER; |
| 2444 | } |
| 2445 | |
| 2446 | if (conn->conn_ops->DataDigest) { |
| 2447 | u32 data_crc; |
| 2448 | |
| 2449 | data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd, |
| 2450 | cmd->write_data_done, length, padding, |
| 2451 | cmd->pad_bytes); |
| 2452 | |
| 2453 | if (checksum != data_crc) { |
| 2454 | pr_err("ImmediateData CRC32C DataDigest 0x%08x" |
| 2455 | " does not match computed 0x%08x\n", checksum, |
| 2456 | data_crc); |
| 2457 | |
| 2458 | if (!conn->sess->sess_ops->ErrorRecoveryLevel) { |
| 2459 | pr_err("Unable to recover from" |
| 2460 | " Immediate Data digest failure while" |
| 2461 | " in ERL=0.\n"); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2462 | iscsit_reject_cmd(cmd, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2463 | ISCSI_REASON_DATA_DIGEST_ERROR, |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2464 | (unsigned char *)hdr); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2465 | return IMMEDIATE_DATA_CANNOT_RECOVER; |
| 2466 | } else { |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2467 | iscsit_reject_cmd(cmd, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2468 | ISCSI_REASON_DATA_DIGEST_ERROR, |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 2469 | (unsigned char *)hdr); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2470 | return IMMEDIATE_DATA_ERL1_CRC_FAILURE; |
| 2471 | } |
| 2472 | } else { |
| 2473 | pr_debug("Got CRC32C DataDigest 0x%08x for" |
| 2474 | " %u bytes of Immediate Data\n", checksum, |
| 2475 | length); |
| 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | cmd->write_data_done += length; |
| 2480 | |
Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 2481 | if (cmd->write_data_done == cmd->se_cmd.data_length) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2482 | spin_lock_bh(&cmd->istate_lock); |
| 2483 | cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT; |
| 2484 | cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT; |
| 2485 | spin_unlock_bh(&cmd->istate_lock); |
| 2486 | } |
| 2487 | |
| 2488 | return IMMEDIATE_DATA_NORMAL_OPERATION; |
| 2489 | } |
| 2490 | |
| 2491 | /* |
| 2492 | * Called with sess->conn_lock held. |
| 2493 | */ |
| 2494 | /* #warning iscsi_build_conn_drop_async_message() only sends out on connections |
| 2495 | with active network interface */ |
| 2496 | static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn) |
| 2497 | { |
| 2498 | struct iscsi_cmd *cmd; |
| 2499 | struct iscsi_conn *conn_p; |
Nicholas Bellinger | d444edc | 2014-02-19 23:32:14 +0000 | [diff] [blame] | 2500 | bool found = false; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2501 | |
| 2502 | /* |
| 2503 | * Only send a Asynchronous Message on connections whos network |
| 2504 | * interface is still functional. |
| 2505 | */ |
| 2506 | list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) { |
| 2507 | if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) { |
| 2508 | iscsit_inc_conn_usage_count(conn_p); |
Nicholas Bellinger | d444edc | 2014-02-19 23:32:14 +0000 | [diff] [blame] | 2509 | found = true; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2510 | break; |
| 2511 | } |
| 2512 | } |
| 2513 | |
Nicholas Bellinger | d444edc | 2014-02-19 23:32:14 +0000 | [diff] [blame] | 2514 | if (!found) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2515 | return; |
| 2516 | |
Nicholas Bellinger | 676687c | 2014-01-20 03:36:44 +0000 | [diff] [blame] | 2517 | cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2518 | if (!cmd) { |
| 2519 | iscsit_dec_conn_usage_count(conn_p); |
| 2520 | return; |
| 2521 | } |
| 2522 | |
| 2523 | cmd->logout_cid = conn->cid; |
| 2524 | cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT; |
| 2525 | cmd->i_state = ISTATE_SEND_ASYNCMSG; |
| 2526 | |
| 2527 | spin_lock_bh(&conn_p->cmd_lock); |
Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 2528 | list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2529 | spin_unlock_bh(&conn_p->cmd_lock); |
| 2530 | |
| 2531 | iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state); |
| 2532 | iscsit_dec_conn_usage_count(conn_p); |
| 2533 | } |
| 2534 | |
| 2535 | static int iscsit_send_conn_drop_async_message( |
| 2536 | struct iscsi_cmd *cmd, |
| 2537 | struct iscsi_conn *conn) |
| 2538 | { |
| 2539 | struct iscsi_async *hdr; |
| 2540 | |
| 2541 | cmd->tx_size = ISCSI_HDR_LEN; |
| 2542 | cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT; |
| 2543 | |
| 2544 | hdr = (struct iscsi_async *) cmd->pdu; |
| 2545 | hdr->opcode = ISCSI_OP_ASYNC_EVENT; |
| 2546 | hdr->flags = ISCSI_FLAG_CMD_FINAL; |
Christoph Hellwig | 66c7db6 | 2012-09-26 08:00:39 -0400 | [diff] [blame] | 2547 | cmd->init_task_tag = RESERVED_ITT; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2548 | cmd->targ_xfer_tag = 0xFFFFFFFF; |
| 2549 | put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]); |
| 2550 | cmd->stat_sn = conn->stat_sn++; |
| 2551 | hdr->statsn = cpu_to_be32(cmd->stat_sn); |
| 2552 | hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); |
| 2553 | hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); |
| 2554 | hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION; |
| 2555 | hdr->param1 = cpu_to_be16(cmd->logout_cid); |
| 2556 | hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait); |
| 2557 | hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain); |
| 2558 | |
| 2559 | if (conn->conn_ops->HeaderDigest) { |
| 2560 | u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; |
| 2561 | |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 2562 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, |
| 2563 | ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2564 | |
| 2565 | cmd->tx_size += ISCSI_CRC_LEN; |
| 2566 | pr_debug("Attaching CRC32C HeaderDigest to" |
| 2567 | " Async Message 0x%08x\n", *header_digest); |
| 2568 | } |
| 2569 | |
| 2570 | cmd->iov_misc[0].iov_base = cmd->pdu; |
| 2571 | cmd->iov_misc[0].iov_len = cmd->tx_size; |
| 2572 | cmd->iov_misc_count = 1; |
| 2573 | |
| 2574 | pr_debug("Sending Connection Dropped Async Message StatSN:" |
| 2575 | " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn, |
| 2576 | cmd->logout_cid, conn->cid); |
| 2577 | return 0; |
| 2578 | } |
| 2579 | |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2580 | static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn) |
| 2581 | { |
| 2582 | if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) || |
| 2583 | (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) { |
| 2584 | wait_for_completion_interruptible_timeout( |
| 2585 | &conn->tx_half_close_comp, |
| 2586 | ISCSI_TX_THREAD_TCP_TIMEOUT * HZ); |
| 2587 | } |
| 2588 | } |
| 2589 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2590 | static void |
| 2591 | iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn, |
| 2592 | struct iscsi_datain *datain, struct iscsi_data_rsp *hdr, |
| 2593 | bool set_statsn) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2594 | { |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2595 | hdr->opcode = ISCSI_OP_SCSI_DATA_IN; |
| 2596 | hdr->flags = datain->flags; |
| 2597 | if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { |
| 2598 | if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) { |
| 2599 | hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW; |
| 2600 | hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); |
| 2601 | } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) { |
| 2602 | hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW; |
| 2603 | hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); |
| 2604 | } |
| 2605 | } |
| 2606 | hton24(hdr->dlength, datain->length); |
| 2607 | if (hdr->flags & ISCSI_FLAG_DATA_ACK) |
| 2608 | int_to_scsilun(cmd->se_cmd.orig_fe_lun, |
| 2609 | (struct scsi_lun *)&hdr->lun); |
| 2610 | else |
| 2611 | put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun); |
| 2612 | |
| 2613 | hdr->itt = cmd->init_task_tag; |
| 2614 | |
| 2615 | if (hdr->flags & ISCSI_FLAG_DATA_ACK) |
| 2616 | hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag); |
| 2617 | else |
| 2618 | hdr->ttt = cpu_to_be32(0xFFFFFFFF); |
| 2619 | if (set_statsn) |
| 2620 | hdr->statsn = cpu_to_be32(cmd->stat_sn); |
| 2621 | else |
| 2622 | hdr->statsn = cpu_to_be32(0xFFFFFFFF); |
| 2623 | |
| 2624 | hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); |
| 2625 | hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); |
| 2626 | hdr->datasn = cpu_to_be32(datain->data_sn); |
| 2627 | hdr->offset = cpu_to_be32(datain->offset); |
| 2628 | |
| 2629 | pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x," |
| 2630 | " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n", |
| 2631 | cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn), |
| 2632 | ntohl(hdr->offset), datain->length, conn->cid); |
| 2633 | } |
| 2634 | |
| 2635 | static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn) |
| 2636 | { |
| 2637 | struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0]; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2638 | struct iscsi_datain datain; |
| 2639 | struct iscsi_datain_req *dr; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2640 | struct kvec *iov; |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2641 | u32 iov_count = 0, tx_size = 0; |
| 2642 | int eodr = 0, ret, iov_ret; |
| 2643 | bool set_statsn = false; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2644 | |
| 2645 | memset(&datain, 0, sizeof(struct iscsi_datain)); |
| 2646 | dr = iscsit_get_datain_values(cmd, &datain); |
| 2647 | if (!dr) { |
| 2648 | pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n", |
| 2649 | cmd->init_task_tag); |
| 2650 | return -1; |
| 2651 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2652 | /* |
| 2653 | * Be paranoid and double check the logic for now. |
| 2654 | */ |
Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 2655 | if ((datain.offset + datain.length) > cmd->se_cmd.data_length) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2656 | pr_err("Command ITT: 0x%08x, datain.offset: %u and" |
| 2657 | " datain.length: %u exceeds cmd->data_length: %u\n", |
| 2658 | cmd->init_task_tag, datain.offset, datain.length, |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2659 | cmd->se_cmd.data_length); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2660 | return -1; |
| 2661 | } |
| 2662 | |
Nicholas Bellinger | 04f3b31 | 2013-11-13 18:54:45 -0800 | [diff] [blame] | 2663 | atomic_long_add(datain.length, &conn->sess->tx_data_octets); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2664 | /* |
| 2665 | * Special case for successfully execution w/ both DATAIN |
| 2666 | * and Sense Data. |
| 2667 | */ |
| 2668 | if ((datain.flags & ISCSI_FLAG_DATA_STATUS) && |
| 2669 | (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)) |
| 2670 | datain.flags &= ~ISCSI_FLAG_DATA_STATUS; |
| 2671 | else { |
| 2672 | if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) || |
| 2673 | (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) { |
| 2674 | iscsit_increment_maxcmdsn(cmd, conn->sess); |
| 2675 | cmd->stat_sn = conn->stat_sn++; |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2676 | set_statsn = true; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2677 | } else if (dr->dr_complete == |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2678 | DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY) |
| 2679 | set_statsn = true; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2682 | iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2683 | |
| 2684 | iov = &cmd->iov_data[0]; |
| 2685 | iov[iov_count].iov_base = cmd->pdu; |
| 2686 | iov[iov_count++].iov_len = ISCSI_HDR_LEN; |
| 2687 | tx_size += ISCSI_HDR_LEN; |
| 2688 | |
| 2689 | if (conn->conn_ops->HeaderDigest) { |
| 2690 | u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; |
| 2691 | |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 2692 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu, |
| 2693 | ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2694 | |
| 2695 | iov[0].iov_len += ISCSI_CRC_LEN; |
| 2696 | tx_size += ISCSI_CRC_LEN; |
| 2697 | |
| 2698 | pr_debug("Attaching CRC32 HeaderDigest" |
| 2699 | " for DataIN PDU 0x%08x\n", *header_digest); |
| 2700 | } |
| 2701 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2702 | iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], |
| 2703 | datain.offset, datain.length); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2704 | if (iov_ret < 0) |
| 2705 | return -1; |
| 2706 | |
| 2707 | iov_count += iov_ret; |
| 2708 | tx_size += datain.length; |
| 2709 | |
| 2710 | cmd->padding = ((-datain.length) & 3); |
| 2711 | if (cmd->padding) { |
| 2712 | iov[iov_count].iov_base = cmd->pad_bytes; |
| 2713 | iov[iov_count++].iov_len = cmd->padding; |
| 2714 | tx_size += cmd->padding; |
| 2715 | |
| 2716 | pr_debug("Attaching %u padding bytes\n", |
| 2717 | cmd->padding); |
| 2718 | } |
| 2719 | if (conn->conn_ops->DataDigest) { |
| 2720 | cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd, |
| 2721 | datain.offset, datain.length, cmd->padding, cmd->pad_bytes); |
| 2722 | |
| 2723 | iov[iov_count].iov_base = &cmd->data_crc; |
| 2724 | iov[iov_count++].iov_len = ISCSI_CRC_LEN; |
| 2725 | tx_size += ISCSI_CRC_LEN; |
| 2726 | |
| 2727 | pr_debug("Attached CRC32C DataDigest %d bytes, crc" |
| 2728 | " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc); |
| 2729 | } |
| 2730 | |
| 2731 | cmd->iov_data_count = iov_count; |
| 2732 | cmd->tx_size = tx_size; |
| 2733 | |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2734 | /* sendpage is preferred but can't insert markers */ |
| 2735 | if (!conn->conn_ops->IFMarker) |
| 2736 | ret = iscsit_fe_sendpage_sg(cmd, conn); |
| 2737 | else |
| 2738 | ret = iscsit_send_tx_data(cmd, conn, 0); |
| 2739 | |
| 2740 | iscsit_unmap_iovec(cmd); |
| 2741 | |
| 2742 | if (ret < 0) { |
| 2743 | iscsit_tx_thread_wait_for_tcp(conn); |
| 2744 | return ret; |
| 2745 | } |
| 2746 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2747 | if (dr->dr_complete) { |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2748 | eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ? |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2749 | 2 : 1; |
| 2750 | iscsit_free_datain_req(cmd, dr); |
| 2751 | } |
| 2752 | |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2753 | return eodr; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2756 | int |
| 2757 | iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, |
| 2758 | struct iscsi_logout_rsp *hdr) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2759 | { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2760 | struct iscsi_conn *logout_conn = NULL; |
| 2761 | struct iscsi_conn_recovery *cr = NULL; |
| 2762 | struct iscsi_session *sess = conn->sess; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2763 | /* |
| 2764 | * The actual shutting down of Sessions and/or Connections |
| 2765 | * for CLOSESESSION and CLOSECONNECTION Logout Requests |
| 2766 | * is done in scsi_logout_post_handler(). |
| 2767 | */ |
| 2768 | switch (cmd->logout_reason) { |
| 2769 | case ISCSI_LOGOUT_REASON_CLOSE_SESSION: |
| 2770 | pr_debug("iSCSI session logout successful, setting" |
| 2771 | " logout response to ISCSI_LOGOUT_SUCCESS.\n"); |
| 2772 | cmd->logout_response = ISCSI_LOGOUT_SUCCESS; |
| 2773 | break; |
| 2774 | case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION: |
| 2775 | if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND) |
| 2776 | break; |
| 2777 | /* |
| 2778 | * For CLOSECONNECTION logout requests carrying |
| 2779 | * a matching logout CID -> local CID, the reference |
| 2780 | * for the local CID will have been incremented in |
| 2781 | * iscsi_logout_closeconnection(). |
| 2782 | * |
| 2783 | * For CLOSECONNECTION logout requests carrying |
| 2784 | * a different CID than the connection it arrived |
| 2785 | * on, the connection responding to cmd->logout_cid |
| 2786 | * is stopped in iscsit_logout_post_handler_diffcid(). |
| 2787 | */ |
| 2788 | |
| 2789 | pr_debug("iSCSI CID: %hu logout on CID: %hu" |
| 2790 | " successful.\n", cmd->logout_cid, conn->cid); |
| 2791 | cmd->logout_response = ISCSI_LOGOUT_SUCCESS; |
| 2792 | break; |
| 2793 | case ISCSI_LOGOUT_REASON_RECOVERY: |
| 2794 | if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) || |
| 2795 | (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED)) |
| 2796 | break; |
| 2797 | /* |
| 2798 | * If the connection is still active from our point of view |
| 2799 | * force connection recovery to occur. |
| 2800 | */ |
| 2801 | logout_conn = iscsit_get_conn_from_cid_rcfr(sess, |
| 2802 | cmd->logout_cid); |
Andy Grover | ee1b1b9 | 2012-07-12 17:34:54 -0700 | [diff] [blame] | 2803 | if (logout_conn) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2804 | iscsit_connection_reinstatement_rcfr(logout_conn); |
| 2805 | iscsit_dec_conn_usage_count(logout_conn); |
| 2806 | } |
| 2807 | |
| 2808 | cr = iscsit_get_inactive_connection_recovery_entry( |
| 2809 | conn->sess, cmd->logout_cid); |
| 2810 | if (!cr) { |
| 2811 | pr_err("Unable to locate CID: %hu for" |
| 2812 | " REMOVECONNFORRECOVERY Logout Request.\n", |
| 2813 | cmd->logout_cid); |
| 2814 | cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND; |
| 2815 | break; |
| 2816 | } |
| 2817 | |
| 2818 | iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn); |
| 2819 | |
| 2820 | pr_debug("iSCSI REMOVECONNFORRECOVERY logout" |
| 2821 | " for recovery for CID: %hu on CID: %hu successful.\n", |
| 2822 | cmd->logout_cid, conn->cid); |
| 2823 | cmd->logout_response = ISCSI_LOGOUT_SUCCESS; |
| 2824 | break; |
| 2825 | default: |
| 2826 | pr_err("Unknown cmd->logout_reason: 0x%02x\n", |
| 2827 | cmd->logout_reason); |
| 2828 | return -1; |
| 2829 | } |
| 2830 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2831 | hdr->opcode = ISCSI_OP_LOGOUT_RSP; |
| 2832 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
| 2833 | hdr->response = cmd->logout_response; |
Christoph Hellwig | 66c7db6 | 2012-09-26 08:00:39 -0400 | [diff] [blame] | 2834 | hdr->itt = cmd->init_task_tag; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2835 | cmd->stat_sn = conn->stat_sn++; |
| 2836 | hdr->statsn = cpu_to_be32(cmd->stat_sn); |
| 2837 | |
| 2838 | iscsit_increment_maxcmdsn(cmd, conn->sess); |
| 2839 | hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); |
| 2840 | hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); |
| 2841 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2842 | pr_debug("Built Logout Response ITT: 0x%08x StatSN:" |
| 2843 | " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n", |
| 2844 | cmd->init_task_tag, cmd->stat_sn, hdr->response, |
| 2845 | cmd->logout_cid, conn->cid); |
| 2846 | |
| 2847 | return 0; |
| 2848 | } |
| 2849 | EXPORT_SYMBOL(iscsit_build_logout_rsp); |
| 2850 | |
| 2851 | static int |
| 2852 | iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn) |
| 2853 | { |
| 2854 | struct kvec *iov; |
| 2855 | int niov = 0, tx_size, rc; |
| 2856 | |
| 2857 | rc = iscsit_build_logout_rsp(cmd, conn, |
| 2858 | (struct iscsi_logout_rsp *)&cmd->pdu[0]); |
| 2859 | if (rc < 0) |
| 2860 | return rc; |
| 2861 | |
| 2862 | tx_size = ISCSI_HDR_LEN; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2863 | iov = &cmd->iov_misc[0]; |
| 2864 | iov[niov].iov_base = cmd->pdu; |
| 2865 | iov[niov++].iov_len = ISCSI_HDR_LEN; |
| 2866 | |
| 2867 | if (conn->conn_ops->HeaderDigest) { |
| 2868 | u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; |
| 2869 | |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 2870 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0], |
| 2871 | ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2872 | |
| 2873 | iov[0].iov_len += ISCSI_CRC_LEN; |
| 2874 | tx_size += ISCSI_CRC_LEN; |
| 2875 | pr_debug("Attaching CRC32C HeaderDigest to" |
| 2876 | " Logout Response 0x%08x\n", *header_digest); |
| 2877 | } |
| 2878 | cmd->iov_misc_count = niov; |
| 2879 | cmd->tx_size = tx_size; |
| 2880 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2881 | return 0; |
| 2882 | } |
| 2883 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2884 | void |
| 2885 | iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, |
| 2886 | struct iscsi_nopin *hdr, bool nopout_response) |
| 2887 | { |
| 2888 | hdr->opcode = ISCSI_OP_NOOP_IN; |
| 2889 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
| 2890 | hton24(hdr->dlength, cmd->buf_ptr_size); |
| 2891 | if (nopout_response) |
| 2892 | put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun); |
| 2893 | hdr->itt = cmd->init_task_tag; |
| 2894 | hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag); |
| 2895 | cmd->stat_sn = (nopout_response) ? conn->stat_sn++ : |
| 2896 | conn->stat_sn; |
| 2897 | hdr->statsn = cpu_to_be32(cmd->stat_sn); |
| 2898 | |
| 2899 | if (nopout_response) |
| 2900 | iscsit_increment_maxcmdsn(cmd, conn->sess); |
| 2901 | |
| 2902 | hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); |
| 2903 | hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); |
| 2904 | |
| 2905 | pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x," |
| 2906 | " StatSN: 0x%08x, Length %u\n", (nopout_response) ? |
| 2907 | "Solicitied" : "Unsolicitied", cmd->init_task_tag, |
| 2908 | cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size); |
| 2909 | } |
| 2910 | EXPORT_SYMBOL(iscsit_build_nopin_rsp); |
| 2911 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2912 | /* |
| 2913 | * Unsolicited NOPIN, either requesting a response or not. |
| 2914 | */ |
| 2915 | static int iscsit_send_unsolicited_nopin( |
| 2916 | struct iscsi_cmd *cmd, |
| 2917 | struct iscsi_conn *conn, |
| 2918 | int want_response) |
| 2919 | { |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2920 | struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0]; |
| 2921 | int tx_size = ISCSI_HDR_LEN, ret; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2922 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2923 | iscsit_build_nopin_rsp(cmd, conn, hdr, false); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2924 | |
| 2925 | if (conn->conn_ops->HeaderDigest) { |
| 2926 | u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; |
| 2927 | |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 2928 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, |
| 2929 | ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2930 | |
| 2931 | tx_size += ISCSI_CRC_LEN; |
| 2932 | pr_debug("Attaching CRC32C HeaderDigest to" |
| 2933 | " NopIN 0x%08x\n", *header_digest); |
| 2934 | } |
| 2935 | |
| 2936 | cmd->iov_misc[0].iov_base = cmd->pdu; |
| 2937 | cmd->iov_misc[0].iov_len = tx_size; |
| 2938 | cmd->iov_misc_count = 1; |
| 2939 | cmd->tx_size = tx_size; |
| 2940 | |
| 2941 | pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:" |
| 2942 | " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid); |
| 2943 | |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 2944 | ret = iscsit_send_tx_data(cmd, conn, 1); |
| 2945 | if (ret < 0) { |
| 2946 | iscsit_tx_thread_wait_for_tcp(conn); |
| 2947 | return ret; |
| 2948 | } |
| 2949 | |
| 2950 | spin_lock_bh(&cmd->istate_lock); |
| 2951 | cmd->i_state = want_response ? |
| 2952 | ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS; |
| 2953 | spin_unlock_bh(&cmd->istate_lock); |
| 2954 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2955 | return 0; |
| 2956 | } |
| 2957 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2958 | static int |
| 2959 | iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2960 | { |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2961 | struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0]; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2962 | struct kvec *iov; |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 2963 | u32 padding = 0; |
| 2964 | int niov = 0, tx_size; |
| 2965 | |
| 2966 | iscsit_build_nopin_rsp(cmd, conn, hdr, true); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2967 | |
| 2968 | tx_size = ISCSI_HDR_LEN; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2969 | iov = &cmd->iov_misc[0]; |
| 2970 | iov[niov].iov_base = cmd->pdu; |
| 2971 | iov[niov++].iov_len = ISCSI_HDR_LEN; |
| 2972 | |
| 2973 | if (conn->conn_ops->HeaderDigest) { |
| 2974 | u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; |
| 2975 | |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 2976 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, |
| 2977 | ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 2978 | |
| 2979 | iov[0].iov_len += ISCSI_CRC_LEN; |
| 2980 | tx_size += ISCSI_CRC_LEN; |
| 2981 | pr_debug("Attaching CRC32C HeaderDigest" |
| 2982 | " to NopIn 0x%08x\n", *header_digest); |
| 2983 | } |
| 2984 | |
| 2985 | /* |
| 2986 | * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr. |
| 2987 | * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size. |
| 2988 | */ |
| 2989 | if (cmd->buf_ptr_size) { |
| 2990 | iov[niov].iov_base = cmd->buf_ptr; |
| 2991 | iov[niov++].iov_len = cmd->buf_ptr_size; |
| 2992 | tx_size += cmd->buf_ptr_size; |
| 2993 | |
| 2994 | pr_debug("Echoing back %u bytes of ping" |
| 2995 | " data.\n", cmd->buf_ptr_size); |
| 2996 | |
| 2997 | padding = ((-cmd->buf_ptr_size) & 3); |
| 2998 | if (padding != 0) { |
| 2999 | iov[niov].iov_base = &cmd->pad_bytes; |
| 3000 | iov[niov++].iov_len = padding; |
| 3001 | tx_size += padding; |
| 3002 | pr_debug("Attaching %u additional" |
| 3003 | " padding bytes.\n", padding); |
| 3004 | } |
| 3005 | if (conn->conn_ops->DataDigest) { |
| 3006 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, |
| 3007 | cmd->buf_ptr, cmd->buf_ptr_size, |
| 3008 | padding, (u8 *)&cmd->pad_bytes, |
| 3009 | (u8 *)&cmd->data_crc); |
| 3010 | |
| 3011 | iov[niov].iov_base = &cmd->data_crc; |
| 3012 | iov[niov++].iov_len = ISCSI_CRC_LEN; |
| 3013 | tx_size += ISCSI_CRC_LEN; |
| 3014 | pr_debug("Attached DataDigest for %u" |
| 3015 | " bytes of ping data, CRC 0x%08x\n", |
| 3016 | cmd->buf_ptr_size, cmd->data_crc); |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | cmd->iov_misc_count = niov; |
| 3021 | cmd->tx_size = tx_size; |
| 3022 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3023 | return 0; |
| 3024 | } |
| 3025 | |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3026 | static int iscsit_send_r2t( |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3027 | struct iscsi_cmd *cmd, |
| 3028 | struct iscsi_conn *conn) |
| 3029 | { |
| 3030 | int tx_size = 0; |
| 3031 | struct iscsi_r2t *r2t; |
| 3032 | struct iscsi_r2t_rsp *hdr; |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3033 | int ret; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3034 | |
| 3035 | r2t = iscsit_get_r2t_from_list(cmd); |
| 3036 | if (!r2t) |
| 3037 | return -1; |
| 3038 | |
| 3039 | hdr = (struct iscsi_r2t_rsp *) cmd->pdu; |
| 3040 | memset(hdr, 0, ISCSI_HDR_LEN); |
| 3041 | hdr->opcode = ISCSI_OP_R2T; |
| 3042 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
| 3043 | int_to_scsilun(cmd->se_cmd.orig_fe_lun, |
| 3044 | (struct scsi_lun *)&hdr->lun); |
Christoph Hellwig | 66c7db6 | 2012-09-26 08:00:39 -0400 | [diff] [blame] | 3045 | hdr->itt = cmd->init_task_tag; |
Sagi Grimberg | c1e34b6 | 2015-01-26 12:49:05 +0200 | [diff] [blame] | 3046 | r2t->targ_xfer_tag = session_get_next_ttt(conn->sess); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3047 | hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag); |
| 3048 | hdr->statsn = cpu_to_be32(conn->stat_sn); |
| 3049 | hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); |
| 3050 | hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); |
| 3051 | hdr->r2tsn = cpu_to_be32(r2t->r2t_sn); |
| 3052 | hdr->data_offset = cpu_to_be32(r2t->offset); |
| 3053 | hdr->data_length = cpu_to_be32(r2t->xfer_len); |
| 3054 | |
| 3055 | cmd->iov_misc[0].iov_base = cmd->pdu; |
| 3056 | cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN; |
| 3057 | tx_size += ISCSI_HDR_LEN; |
| 3058 | |
| 3059 | if (conn->conn_ops->HeaderDigest) { |
| 3060 | u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; |
| 3061 | |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 3062 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, |
| 3063 | ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3064 | |
| 3065 | cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN; |
| 3066 | tx_size += ISCSI_CRC_LEN; |
| 3067 | pr_debug("Attaching CRC32 HeaderDigest for R2T" |
| 3068 | " PDU 0x%08x\n", *header_digest); |
| 3069 | } |
| 3070 | |
| 3071 | pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:" |
| 3072 | " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n", |
| 3073 | (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag, |
| 3074 | r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn, |
| 3075 | r2t->offset, r2t->xfer_len, conn->cid); |
| 3076 | |
| 3077 | cmd->iov_misc_count = 1; |
| 3078 | cmd->tx_size = tx_size; |
| 3079 | |
| 3080 | spin_lock_bh(&cmd->r2t_lock); |
| 3081 | r2t->sent_r2t = 1; |
| 3082 | spin_unlock_bh(&cmd->r2t_lock); |
| 3083 | |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3084 | ret = iscsit_send_tx_data(cmd, conn, 1); |
| 3085 | if (ret < 0) { |
| 3086 | iscsit_tx_thread_wait_for_tcp(conn); |
| 3087 | return ret; |
| 3088 | } |
| 3089 | |
| 3090 | spin_lock_bh(&cmd->dataout_timeout_lock); |
| 3091 | iscsit_start_dataout_timer(cmd, conn); |
| 3092 | spin_unlock_bh(&cmd->dataout_timeout_lock); |
| 3093 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3094 | return 0; |
| 3095 | } |
| 3096 | |
| 3097 | /* |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3098 | * @recovery: If called from iscsi_task_reassign_complete_write() for |
| 3099 | * connection recovery. |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3100 | */ |
| 3101 | int iscsit_build_r2ts_for_cmd( |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3102 | struct iscsi_conn *conn, |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 3103 | struct iscsi_cmd *cmd, |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3104 | bool recovery) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3105 | { |
| 3106 | int first_r2t = 1; |
| 3107 | u32 offset = 0, xfer_len = 0; |
| 3108 | |
| 3109 | spin_lock_bh(&cmd->r2t_lock); |
| 3110 | if (cmd->cmd_flags & ICF_SENT_LAST_R2T) { |
| 3111 | spin_unlock_bh(&cmd->r2t_lock); |
| 3112 | return 0; |
| 3113 | } |
| 3114 | |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3115 | if (conn->sess->sess_ops->DataSequenceInOrder && |
| 3116 | !recovery) |
Andy Grover | c6037cc | 2012-04-03 15:51:02 -0700 | [diff] [blame] | 3117 | cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3118 | |
| 3119 | while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) { |
| 3120 | if (conn->sess->sess_ops->DataSequenceInOrder) { |
| 3121 | offset = cmd->r2t_offset; |
| 3122 | |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3123 | if (first_r2t && recovery) { |
| 3124 | int new_data_end = offset + |
| 3125 | conn->sess->sess_ops->MaxBurstLength - |
| 3126 | cmd->next_burst_len; |
| 3127 | |
Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 3128 | if (new_data_end > cmd->se_cmd.data_length) |
| 3129 | xfer_len = cmd->se_cmd.data_length - offset; |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3130 | else |
| 3131 | xfer_len = |
| 3132 | conn->sess->sess_ops->MaxBurstLength - |
| 3133 | cmd->next_burst_len; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3134 | } else { |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3135 | int new_data_end = offset + |
| 3136 | conn->sess->sess_ops->MaxBurstLength; |
| 3137 | |
Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 3138 | if (new_data_end > cmd->se_cmd.data_length) |
| 3139 | xfer_len = cmd->se_cmd.data_length - offset; |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3140 | else |
| 3141 | xfer_len = conn->sess->sess_ops->MaxBurstLength; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3142 | } |
| 3143 | cmd->r2t_offset += xfer_len; |
| 3144 | |
Andy Grover | ebf1d95 | 2012-04-03 15:51:24 -0700 | [diff] [blame] | 3145 | if (cmd->r2t_offset == cmd->se_cmd.data_length) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3146 | cmd->cmd_flags |= ICF_SENT_LAST_R2T; |
| 3147 | } else { |
| 3148 | struct iscsi_seq *seq; |
| 3149 | |
| 3150 | seq = iscsit_get_seq_holder_for_r2t(cmd); |
| 3151 | if (!seq) { |
| 3152 | spin_unlock_bh(&cmd->r2t_lock); |
| 3153 | return -1; |
| 3154 | } |
| 3155 | |
| 3156 | offset = seq->offset; |
| 3157 | xfer_len = seq->xfer_len; |
| 3158 | |
| 3159 | if (cmd->seq_send_order == cmd->seq_count) |
| 3160 | cmd->cmd_flags |= ICF_SENT_LAST_R2T; |
| 3161 | } |
| 3162 | cmd->outstanding_r2ts++; |
| 3163 | first_r2t = 0; |
| 3164 | |
| 3165 | if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) { |
| 3166 | spin_unlock_bh(&cmd->r2t_lock); |
| 3167 | return -1; |
| 3168 | } |
| 3169 | |
| 3170 | if (cmd->cmd_flags & ICF_SENT_LAST_R2T) |
| 3171 | break; |
| 3172 | } |
| 3173 | spin_unlock_bh(&cmd->r2t_lock); |
| 3174 | |
| 3175 | return 0; |
| 3176 | } |
| 3177 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3178 | void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn, |
| 3179 | bool inc_stat_sn, struct iscsi_scsi_rsp *hdr) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3180 | { |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3181 | if (inc_stat_sn) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3182 | cmd->stat_sn = conn->stat_sn++; |
| 3183 | |
Nicholas Bellinger | 04f3b31 | 2013-11-13 18:54:45 -0800 | [diff] [blame] | 3184 | atomic_long_inc(&conn->sess->rsp_pdus); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3185 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3186 | memset(hdr, 0, ISCSI_HDR_LEN); |
| 3187 | hdr->opcode = ISCSI_OP_SCSI_CMD_RSP; |
| 3188 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
| 3189 | if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) { |
| 3190 | hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW; |
Nicholas Bellinger | 7e46cf0 | 2011-11-15 23:59:00 -0800 | [diff] [blame] | 3191 | hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3192 | } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) { |
| 3193 | hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW; |
Nicholas Bellinger | 7e46cf0 | 2011-11-15 23:59:00 -0800 | [diff] [blame] | 3194 | hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3195 | } |
| 3196 | hdr->response = cmd->iscsi_response; |
| 3197 | hdr->cmd_status = cmd->se_cmd.scsi_status; |
Christoph Hellwig | 66c7db6 | 2012-09-26 08:00:39 -0400 | [diff] [blame] | 3198 | hdr->itt = cmd->init_task_tag; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3199 | hdr->statsn = cpu_to_be32(cmd->stat_sn); |
| 3200 | |
| 3201 | iscsit_increment_maxcmdsn(cmd, conn->sess); |
| 3202 | hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); |
| 3203 | hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); |
| 3204 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3205 | pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x," |
| 3206 | " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n", |
| 3207 | cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status, |
| 3208 | cmd->se_cmd.scsi_status, conn->cid); |
| 3209 | } |
| 3210 | EXPORT_SYMBOL(iscsit_build_rsp_pdu); |
| 3211 | |
| 3212 | static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn) |
| 3213 | { |
| 3214 | struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0]; |
| 3215 | struct kvec *iov; |
| 3216 | u32 padding = 0, tx_size = 0; |
| 3217 | int iov_count = 0; |
| 3218 | bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS); |
| 3219 | |
| 3220 | iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr); |
| 3221 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3222 | iov = &cmd->iov_misc[0]; |
| 3223 | iov[iov_count].iov_base = cmd->pdu; |
| 3224 | iov[iov_count++].iov_len = ISCSI_HDR_LEN; |
| 3225 | tx_size += ISCSI_HDR_LEN; |
| 3226 | |
| 3227 | /* |
| 3228 | * Attach SENSE DATA payload to iSCSI Response PDU |
| 3229 | */ |
| 3230 | if (cmd->se_cmd.sense_buffer && |
| 3231 | ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) || |
| 3232 | (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) { |
Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 3233 | put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer); |
| 3234 | cmd->se_cmd.scsi_sense_length += sizeof (__be16); |
| 3235 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3236 | padding = -(cmd->se_cmd.scsi_sense_length) & 3; |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 3237 | hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length); |
Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 3238 | iov[iov_count].iov_base = cmd->sense_buffer; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3239 | iov[iov_count++].iov_len = |
| 3240 | (cmd->se_cmd.scsi_sense_length + padding); |
| 3241 | tx_size += cmd->se_cmd.scsi_sense_length; |
| 3242 | |
| 3243 | if (padding) { |
Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 3244 | memset(cmd->sense_buffer + |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3245 | cmd->se_cmd.scsi_sense_length, 0, padding); |
| 3246 | tx_size += padding; |
| 3247 | pr_debug("Adding %u bytes of padding to" |
| 3248 | " SENSE.\n", padding); |
| 3249 | } |
| 3250 | |
| 3251 | if (conn->conn_ops->DataDigest) { |
| 3252 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, |
Roland Dreier | 9c58b7d | 2012-08-15 14:35:25 -0700 | [diff] [blame] | 3253 | cmd->sense_buffer, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3254 | (cmd->se_cmd.scsi_sense_length + padding), |
| 3255 | 0, NULL, (u8 *)&cmd->data_crc); |
| 3256 | |
| 3257 | iov[iov_count].iov_base = &cmd->data_crc; |
| 3258 | iov[iov_count++].iov_len = ISCSI_CRC_LEN; |
| 3259 | tx_size += ISCSI_CRC_LEN; |
| 3260 | |
| 3261 | pr_debug("Attaching CRC32 DataDigest for" |
| 3262 | " SENSE, %u bytes CRC 0x%08x\n", |
| 3263 | (cmd->se_cmd.scsi_sense_length + padding), |
| 3264 | cmd->data_crc); |
| 3265 | } |
| 3266 | |
| 3267 | pr_debug("Attaching SENSE DATA: %u bytes to iSCSI" |
| 3268 | " Response PDU\n", |
| 3269 | cmd->se_cmd.scsi_sense_length); |
| 3270 | } |
| 3271 | |
| 3272 | if (conn->conn_ops->HeaderDigest) { |
| 3273 | u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; |
| 3274 | |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 3275 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu, |
| 3276 | ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3277 | |
| 3278 | iov[0].iov_len += ISCSI_CRC_LEN; |
| 3279 | tx_size += ISCSI_CRC_LEN; |
| 3280 | pr_debug("Attaching CRC32 HeaderDigest for Response" |
| 3281 | " PDU 0x%08x\n", *header_digest); |
| 3282 | } |
| 3283 | |
| 3284 | cmd->iov_misc_count = iov_count; |
| 3285 | cmd->tx_size = tx_size; |
| 3286 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3287 | return 0; |
| 3288 | } |
| 3289 | |
| 3290 | static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr) |
| 3291 | { |
| 3292 | switch (se_tmr->response) { |
| 3293 | case TMR_FUNCTION_COMPLETE: |
| 3294 | return ISCSI_TMF_RSP_COMPLETE; |
| 3295 | case TMR_TASK_DOES_NOT_EXIST: |
| 3296 | return ISCSI_TMF_RSP_NO_TASK; |
| 3297 | case TMR_LUN_DOES_NOT_EXIST: |
| 3298 | return ISCSI_TMF_RSP_NO_LUN; |
| 3299 | case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED: |
| 3300 | return ISCSI_TMF_RSP_NOT_SUPPORTED; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3301 | case TMR_FUNCTION_REJECTED: |
| 3302 | default: |
| 3303 | return ISCSI_TMF_RSP_REJECTED; |
| 3304 | } |
| 3305 | } |
| 3306 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3307 | void |
| 3308 | iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, |
| 3309 | struct iscsi_tm_rsp *hdr) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3310 | { |
| 3311 | struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3312 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3313 | hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP; |
Nicholas Bellinger | 7ae0b10 | 2011-11-27 22:25:14 -0800 | [diff] [blame] | 3314 | hdr->flags = ISCSI_FLAG_CMD_FINAL; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3315 | hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr); |
Christoph Hellwig | 66c7db6 | 2012-09-26 08:00:39 -0400 | [diff] [blame] | 3316 | hdr->itt = cmd->init_task_tag; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3317 | cmd->stat_sn = conn->stat_sn++; |
| 3318 | hdr->statsn = cpu_to_be32(cmd->stat_sn); |
| 3319 | |
| 3320 | iscsit_increment_maxcmdsn(cmd, conn->sess); |
| 3321 | hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); |
| 3322 | hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); |
| 3323 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3324 | pr_debug("Built Task Management Response ITT: 0x%08x," |
| 3325 | " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n", |
| 3326 | cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid); |
| 3327 | } |
| 3328 | EXPORT_SYMBOL(iscsit_build_task_mgt_rsp); |
| 3329 | |
| 3330 | static int |
| 3331 | iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn) |
| 3332 | { |
| 3333 | struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0]; |
| 3334 | u32 tx_size = 0; |
| 3335 | |
| 3336 | iscsit_build_task_mgt_rsp(cmd, conn, hdr); |
| 3337 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3338 | cmd->iov_misc[0].iov_base = cmd->pdu; |
| 3339 | cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN; |
| 3340 | tx_size += ISCSI_HDR_LEN; |
| 3341 | |
| 3342 | if (conn->conn_ops->HeaderDigest) { |
| 3343 | u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; |
| 3344 | |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 3345 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, |
| 3346 | ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3347 | |
| 3348 | cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN; |
| 3349 | tx_size += ISCSI_CRC_LEN; |
| 3350 | pr_debug("Attaching CRC32 HeaderDigest for Task" |
| 3351 | " Mgmt Response PDU 0x%08x\n", *header_digest); |
| 3352 | } |
| 3353 | |
| 3354 | cmd->iov_misc_count = 1; |
| 3355 | cmd->tx_size = tx_size; |
| 3356 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3357 | return 0; |
| 3358 | } |
| 3359 | |
Nicholas Bellinger | 2f9bc89 | 2012-01-16 23:33:48 -0800 | [diff] [blame] | 3360 | static bool iscsit_check_inaddr_any(struct iscsi_np *np) |
| 3361 | { |
| 3362 | bool ret = false; |
| 3363 | |
| 3364 | if (np->np_sockaddr.ss_family == AF_INET6) { |
| 3365 | const struct sockaddr_in6 sin6 = { |
| 3366 | .sin6_addr = IN6ADDR_ANY_INIT }; |
| 3367 | struct sockaddr_in6 *sock_in6 = |
| 3368 | (struct sockaddr_in6 *)&np->np_sockaddr; |
| 3369 | |
| 3370 | if (!memcmp(sock_in6->sin6_addr.s6_addr, |
| 3371 | sin6.sin6_addr.s6_addr, 16)) |
| 3372 | ret = true; |
| 3373 | } else { |
| 3374 | struct sockaddr_in * sock_in = |
| 3375 | (struct sockaddr_in *)&np->np_sockaddr; |
| 3376 | |
Christoph Hellwig | cea0b4c | 2012-09-26 08:00:38 -0400 | [diff] [blame] | 3377 | if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY)) |
Nicholas Bellinger | 2f9bc89 | 2012-01-16 23:33:48 -0800 | [diff] [blame] | 3378 | ret = true; |
| 3379 | } |
| 3380 | |
| 3381 | return ret; |
| 3382 | } |
| 3383 | |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3384 | #define SENDTARGETS_BUF_LIMIT 32768U |
| 3385 | |
Sagi Grimberg | 22c7aaa | 2014-06-10 18:27:59 +0300 | [diff] [blame] | 3386 | static int |
| 3387 | iscsit_build_sendtargets_response(struct iscsi_cmd *cmd, |
| 3388 | enum iscsit_transport_type network_transport) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3389 | { |
| 3390 | char *payload = NULL; |
| 3391 | struct iscsi_conn *conn = cmd->conn; |
| 3392 | struct iscsi_portal_group *tpg; |
| 3393 | struct iscsi_tiqn *tiqn; |
| 3394 | struct iscsi_tpg_np *tpg_np; |
| 3395 | int buffer_len, end_of_buf = 0, len = 0, payload_len = 0; |
Thomas Glanzmann | 2dd1d53 | 2013-10-07 23:13:46 +0200 | [diff] [blame] | 3396 | int target_name_printed; |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3397 | unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */ |
Nicholas Bellinger | 6665889 | 2013-06-19 22:45:42 -0700 | [diff] [blame] | 3398 | unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3399 | |
Sagi Grimberg | be7dcfb | 2015-01-26 12:49:06 +0200 | [diff] [blame^] | 3400 | buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength, |
Andy Grover | 8b1e124 | 2012-04-03 15:51:12 -0700 | [diff] [blame] | 3401 | SENDTARGETS_BUF_LIMIT); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3402 | |
| 3403 | payload = kzalloc(buffer_len, GFP_KERNEL); |
| 3404 | if (!payload) { |
| 3405 | pr_err("Unable to allocate memory for sendtargets" |
| 3406 | " response.\n"); |
| 3407 | return -ENOMEM; |
| 3408 | } |
Nicholas Bellinger | 6665889 | 2013-06-19 22:45:42 -0700 | [diff] [blame] | 3409 | /* |
| 3410 | * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE |
| 3411 | * explicit case.. |
| 3412 | */ |
| 3413 | if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) { |
| 3414 | text_ptr = strchr(text_in, '='); |
| 3415 | if (!text_ptr) { |
| 3416 | pr_err("Unable to locate '=' string in text_in:" |
| 3417 | " %s\n", text_in); |
Dan Carpenter | 4f45d32 | 2013-06-24 18:46:57 +0300 | [diff] [blame] | 3418 | kfree(payload); |
Nicholas Bellinger | 6665889 | 2013-06-19 22:45:42 -0700 | [diff] [blame] | 3419 | return -EINVAL; |
| 3420 | } |
| 3421 | /* |
| 3422 | * Skip over '=' character.. |
| 3423 | */ |
| 3424 | text_ptr += 1; |
| 3425 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3426 | |
| 3427 | spin_lock(&tiqn_lock); |
| 3428 | list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) { |
Nicholas Bellinger | 6665889 | 2013-06-19 22:45:42 -0700 | [diff] [blame] | 3429 | if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) && |
| 3430 | strcmp(tiqn->tiqn, text_ptr)) { |
| 3431 | continue; |
| 3432 | } |
| 3433 | |
Thomas Glanzmann | 2dd1d53 | 2013-10-07 23:13:46 +0200 | [diff] [blame] | 3434 | target_name_printed = 0; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3435 | |
| 3436 | spin_lock(&tiqn->tiqn_tpg_lock); |
| 3437 | list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) { |
| 3438 | |
Thomas Glanzmann | 2dd1d53 | 2013-10-07 23:13:46 +0200 | [diff] [blame] | 3439 | /* If demo_mode_discovery=0 and generate_node_acls=0 |
| 3440 | * (demo mode dislabed) do not return |
| 3441 | * TargetName+TargetAddress unless a NodeACL exists. |
| 3442 | */ |
| 3443 | |
| 3444 | if ((tpg->tpg_attrib.generate_node_acls == 0) && |
| 3445 | (tpg->tpg_attrib.demo_mode_discovery == 0) && |
| 3446 | (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg, |
| 3447 | cmd->conn->sess->sess_ops->InitiatorName))) { |
| 3448 | continue; |
| 3449 | } |
| 3450 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3451 | spin_lock(&tpg->tpg_state_lock); |
| 3452 | if ((tpg->tpg_state == TPG_STATE_FREE) || |
| 3453 | (tpg->tpg_state == TPG_STATE_INACTIVE)) { |
| 3454 | spin_unlock(&tpg->tpg_state_lock); |
| 3455 | continue; |
| 3456 | } |
| 3457 | spin_unlock(&tpg->tpg_state_lock); |
| 3458 | |
| 3459 | spin_lock(&tpg->tpg_np_lock); |
| 3460 | list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, |
| 3461 | tpg_np_list) { |
Nicholas Bellinger | 2f9bc89 | 2012-01-16 23:33:48 -0800 | [diff] [blame] | 3462 | struct iscsi_np *np = tpg_np->tpg_np; |
| 3463 | bool inaddr_any = iscsit_check_inaddr_any(np); |
| 3464 | |
Sagi Grimberg | 22c7aaa | 2014-06-10 18:27:59 +0300 | [diff] [blame] | 3465 | if (np->np_network_transport != network_transport) |
| 3466 | continue; |
| 3467 | |
Thomas Glanzmann | 2dd1d53 | 2013-10-07 23:13:46 +0200 | [diff] [blame] | 3468 | if (!target_name_printed) { |
| 3469 | len = sprintf(buf, "TargetName=%s", |
| 3470 | tiqn->tiqn); |
| 3471 | len += 1; |
| 3472 | |
| 3473 | if ((len + payload_len) > buffer_len) { |
| 3474 | spin_unlock(&tpg->tpg_np_lock); |
| 3475 | spin_unlock(&tiqn->tiqn_tpg_lock); |
| 3476 | end_of_buf = 1; |
| 3477 | goto eob; |
| 3478 | } |
| 3479 | memcpy(payload + payload_len, buf, len); |
| 3480 | payload_len += len; |
| 3481 | target_name_printed = 1; |
| 3482 | } |
| 3483 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3484 | len = sprintf(buf, "TargetAddress=" |
Chris Leech | dfecf61 | 2013-08-12 11:26:28 -0700 | [diff] [blame] | 3485 | "%s:%hu,%hu", |
Christophe Vu-Brugier | 0bcc297 | 2014-06-06 17:15:16 +0200 | [diff] [blame] | 3486 | inaddr_any ? conn->local_ip : np->np_ip, |
Steven Allen | f2774f4 | 2014-10-15 10:59:21 -0700 | [diff] [blame] | 3487 | np->np_port, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3488 | tpg->tpgt); |
| 3489 | len += 1; |
| 3490 | |
| 3491 | if ((len + payload_len) > buffer_len) { |
| 3492 | spin_unlock(&tpg->tpg_np_lock); |
| 3493 | spin_unlock(&tiqn->tiqn_tpg_lock); |
| 3494 | end_of_buf = 1; |
| 3495 | goto eob; |
| 3496 | } |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 3497 | memcpy(payload + payload_len, buf, len); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3498 | payload_len += len; |
| 3499 | } |
| 3500 | spin_unlock(&tpg->tpg_np_lock); |
| 3501 | } |
| 3502 | spin_unlock(&tiqn->tiqn_tpg_lock); |
| 3503 | eob: |
| 3504 | if (end_of_buf) |
| 3505 | break; |
Nicholas Bellinger | 6665889 | 2013-06-19 22:45:42 -0700 | [diff] [blame] | 3506 | |
| 3507 | if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) |
| 3508 | break; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3509 | } |
| 3510 | spin_unlock(&tiqn_lock); |
| 3511 | |
| 3512 | cmd->buf_ptr = payload; |
| 3513 | |
| 3514 | return payload_len; |
| 3515 | } |
| 3516 | |
Nicholas Bellinger | 889c8a6 | 2013-06-14 18:49:55 -0700 | [diff] [blame] | 3517 | int |
| 3518 | iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn, |
Sagi Grimberg | 22c7aaa | 2014-06-10 18:27:59 +0300 | [diff] [blame] | 3519 | struct iscsi_text_rsp *hdr, |
| 3520 | enum iscsit_transport_type network_transport) |
Nicholas Bellinger | 889c8a6 | 2013-06-14 18:49:55 -0700 | [diff] [blame] | 3521 | { |
| 3522 | int text_length, padding; |
| 3523 | |
Sagi Grimberg | 22c7aaa | 2014-06-10 18:27:59 +0300 | [diff] [blame] | 3524 | text_length = iscsit_build_sendtargets_response(cmd, network_transport); |
Nicholas Bellinger | 889c8a6 | 2013-06-14 18:49:55 -0700 | [diff] [blame] | 3525 | if (text_length < 0) |
| 3526 | return text_length; |
| 3527 | |
| 3528 | hdr->opcode = ISCSI_OP_TEXT_RSP; |
| 3529 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
| 3530 | padding = ((-text_length) & 3); |
| 3531 | hton24(hdr->dlength, text_length); |
| 3532 | hdr->itt = cmd->init_task_tag; |
| 3533 | hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag); |
| 3534 | cmd->stat_sn = conn->stat_sn++; |
| 3535 | hdr->statsn = cpu_to_be32(cmd->stat_sn); |
| 3536 | |
| 3537 | iscsit_increment_maxcmdsn(cmd, conn->sess); |
| 3538 | hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); |
| 3539 | hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); |
| 3540 | |
| 3541 | pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x," |
| 3542 | " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn, |
| 3543 | text_length, conn->cid); |
| 3544 | |
| 3545 | return text_length + padding; |
| 3546 | } |
| 3547 | EXPORT_SYMBOL(iscsit_build_text_rsp); |
| 3548 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3549 | /* |
| 3550 | * FIXME: Add support for F_BIT and C_BIT when the length is longer than |
| 3551 | * MaxRecvDataSegmentLength. |
| 3552 | */ |
| 3553 | static int iscsit_send_text_rsp( |
| 3554 | struct iscsi_cmd *cmd, |
| 3555 | struct iscsi_conn *conn) |
| 3556 | { |
Nicholas Bellinger | 889c8a6 | 2013-06-14 18:49:55 -0700 | [diff] [blame] | 3557 | struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3558 | struct kvec *iov; |
Nicholas Bellinger | 889c8a6 | 2013-06-14 18:49:55 -0700 | [diff] [blame] | 3559 | u32 tx_size = 0; |
| 3560 | int text_length, iov_count = 0, rc; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3561 | |
Sagi Grimberg | 22c7aaa | 2014-06-10 18:27:59 +0300 | [diff] [blame] | 3562 | rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP); |
Nicholas Bellinger | 889c8a6 | 2013-06-14 18:49:55 -0700 | [diff] [blame] | 3563 | if (rc < 0) |
| 3564 | return rc; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3565 | |
Nicholas Bellinger | 889c8a6 | 2013-06-14 18:49:55 -0700 | [diff] [blame] | 3566 | text_length = rc; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3567 | iov = &cmd->iov_misc[0]; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3568 | iov[iov_count].iov_base = cmd->pdu; |
| 3569 | iov[iov_count++].iov_len = ISCSI_HDR_LEN; |
| 3570 | iov[iov_count].iov_base = cmd->buf_ptr; |
Nicholas Bellinger | 889c8a6 | 2013-06-14 18:49:55 -0700 | [diff] [blame] | 3571 | iov[iov_count++].iov_len = text_length; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3572 | |
Nicholas Bellinger | 889c8a6 | 2013-06-14 18:49:55 -0700 | [diff] [blame] | 3573 | tx_size += (ISCSI_HDR_LEN + text_length); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3574 | |
| 3575 | if (conn->conn_ops->HeaderDigest) { |
| 3576 | u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; |
| 3577 | |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 3578 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, |
| 3579 | ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3580 | |
| 3581 | iov[0].iov_len += ISCSI_CRC_LEN; |
| 3582 | tx_size += ISCSI_CRC_LEN; |
| 3583 | pr_debug("Attaching CRC32 HeaderDigest for" |
| 3584 | " Text Response PDU 0x%08x\n", *header_digest); |
| 3585 | } |
| 3586 | |
| 3587 | if (conn->conn_ops->DataDigest) { |
| 3588 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, |
Nicholas Bellinger | 889c8a6 | 2013-06-14 18:49:55 -0700 | [diff] [blame] | 3589 | cmd->buf_ptr, text_length, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3590 | 0, NULL, (u8 *)&cmd->data_crc); |
| 3591 | |
| 3592 | iov[iov_count].iov_base = &cmd->data_crc; |
| 3593 | iov[iov_count++].iov_len = ISCSI_CRC_LEN; |
| 3594 | tx_size += ISCSI_CRC_LEN; |
| 3595 | |
| 3596 | pr_debug("Attaching DataDigest for %u bytes of text" |
Nicholas Bellinger | 889c8a6 | 2013-06-14 18:49:55 -0700 | [diff] [blame] | 3597 | " data, CRC 0x%08x\n", text_length, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3598 | cmd->data_crc); |
| 3599 | } |
| 3600 | |
| 3601 | cmd->iov_misc_count = iov_count; |
| 3602 | cmd->tx_size = tx_size; |
| 3603 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3604 | return 0; |
| 3605 | } |
| 3606 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3607 | void |
| 3608 | iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn, |
| 3609 | struct iscsi_reject *hdr) |
| 3610 | { |
| 3611 | hdr->opcode = ISCSI_OP_REJECT; |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 3612 | hdr->reason = cmd->reject_reason; |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3613 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
| 3614 | hton24(hdr->dlength, ISCSI_HDR_LEN); |
| 3615 | hdr->ffffffff = cpu_to_be32(0xffffffff); |
| 3616 | cmd->stat_sn = conn->stat_sn++; |
| 3617 | hdr->statsn = cpu_to_be32(cmd->stat_sn); |
| 3618 | hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn); |
| 3619 | hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn); |
| 3620 | |
| 3621 | } |
| 3622 | EXPORT_SYMBOL(iscsit_build_reject); |
| 3623 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3624 | static int iscsit_send_reject( |
| 3625 | struct iscsi_cmd *cmd, |
| 3626 | struct iscsi_conn *conn) |
| 3627 | { |
Nicholas Bellinger | bfbdb31 | 2013-05-03 16:46:41 -0700 | [diff] [blame] | 3628 | struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0]; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3629 | struct kvec *iov; |
Nicholas Bellinger | bfbdb31 | 2013-05-03 16:46:41 -0700 | [diff] [blame] | 3630 | u32 iov_count = 0, tx_size; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3631 | |
Nicholas Bellinger | bfbdb31 | 2013-05-03 16:46:41 -0700 | [diff] [blame] | 3632 | iscsit_build_reject(cmd, conn, hdr); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3633 | |
| 3634 | iov = &cmd->iov_misc[0]; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3635 | iov[iov_count].iov_base = cmd->pdu; |
| 3636 | iov[iov_count++].iov_len = ISCSI_HDR_LEN; |
| 3637 | iov[iov_count].iov_base = cmd->buf_ptr; |
| 3638 | iov[iov_count++].iov_len = ISCSI_HDR_LEN; |
| 3639 | |
| 3640 | tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN); |
| 3641 | |
| 3642 | if (conn->conn_ops->HeaderDigest) { |
| 3643 | u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN]; |
| 3644 | |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 3645 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr, |
| 3646 | ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3647 | |
| 3648 | iov[0].iov_len += ISCSI_CRC_LEN; |
| 3649 | tx_size += ISCSI_CRC_LEN; |
| 3650 | pr_debug("Attaching CRC32 HeaderDigest for" |
| 3651 | " REJECT PDU 0x%08x\n", *header_digest); |
| 3652 | } |
| 3653 | |
| 3654 | if (conn->conn_ops->DataDigest) { |
Geert Uytterhoeven | 80690fd | 2013-05-03 23:15:57 +0200 | [diff] [blame] | 3655 | iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr, |
| 3656 | ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3657 | |
| 3658 | iov[iov_count].iov_base = &cmd->data_crc; |
| 3659 | iov[iov_count++].iov_len = ISCSI_CRC_LEN; |
| 3660 | tx_size += ISCSI_CRC_LEN; |
| 3661 | pr_debug("Attaching CRC32 DataDigest for REJECT" |
| 3662 | " PDU 0x%08x\n", cmd->data_crc); |
| 3663 | } |
| 3664 | |
| 3665 | cmd->iov_misc_count = iov_count; |
| 3666 | cmd->tx_size = tx_size; |
| 3667 | |
| 3668 | pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x," |
| 3669 | " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid); |
| 3670 | |
| 3671 | return 0; |
| 3672 | } |
| 3673 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3674 | void iscsit_thread_get_cpumask(struct iscsi_conn *conn) |
| 3675 | { |
| 3676 | struct iscsi_thread_set *ts = conn->thread_set; |
| 3677 | int ord, cpu; |
| 3678 | /* |
| 3679 | * thread_id is assigned from iscsit_global->ts_bitmap from |
| 3680 | * within iscsi_thread_set.c:iscsi_allocate_thread_sets() |
| 3681 | * |
| 3682 | * Here we use thread_id to determine which CPU that this |
| 3683 | * iSCSI connection's iscsi_thread_set will be scheduled to |
| 3684 | * execute upon. |
| 3685 | */ |
| 3686 | ord = ts->thread_id % cpumask_weight(cpu_online_mask); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3687 | for_each_online_cpu(cpu) { |
| 3688 | if (ord-- == 0) { |
| 3689 | cpumask_set_cpu(cpu, conn->conn_cpumask); |
| 3690 | return; |
| 3691 | } |
| 3692 | } |
| 3693 | /* |
| 3694 | * This should never be reached.. |
| 3695 | */ |
| 3696 | dump_stack(); |
| 3697 | cpumask_setall(conn->conn_cpumask); |
| 3698 | } |
| 3699 | |
| 3700 | static inline void iscsit_thread_check_cpumask( |
| 3701 | struct iscsi_conn *conn, |
| 3702 | struct task_struct *p, |
| 3703 | int mode) |
| 3704 | { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3705 | /* |
| 3706 | * mode == 1 signals iscsi_target_tx_thread() usage. |
| 3707 | * mode == 0 signals iscsi_target_rx_thread() usage. |
| 3708 | */ |
| 3709 | if (mode == 1) { |
| 3710 | if (!conn->conn_tx_reset_cpumask) |
| 3711 | return; |
| 3712 | conn->conn_tx_reset_cpumask = 0; |
| 3713 | } else { |
| 3714 | if (!conn->conn_rx_reset_cpumask) |
| 3715 | return; |
| 3716 | conn->conn_rx_reset_cpumask = 0; |
| 3717 | } |
| 3718 | /* |
| 3719 | * Update the CPU mask for this single kthread so that |
| 3720 | * both TX and RX kthreads are scheduled to run on the |
| 3721 | * same CPU. |
| 3722 | */ |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3723 | set_cpus_allowed_ptr(p, conn->conn_cpumask); |
| 3724 | } |
| 3725 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3726 | static int |
| 3727 | iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state) |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3728 | { |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3729 | int ret; |
| 3730 | |
| 3731 | switch (state) { |
| 3732 | case ISTATE_SEND_R2T: |
| 3733 | ret = iscsit_send_r2t(cmd, conn); |
| 3734 | if (ret < 0) |
| 3735 | goto err; |
| 3736 | break; |
| 3737 | case ISTATE_REMOVE: |
| 3738 | spin_lock_bh(&conn->cmd_lock); |
Nicholas Bellinger | 5159d76 | 2014-02-03 12:53:51 -0800 | [diff] [blame] | 3739 | list_del_init(&cmd->i_conn_node); |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3740 | spin_unlock_bh(&conn->cmd_lock); |
| 3741 | |
Nicholas Bellinger | aafc9d1 | 2013-05-31 00:49:41 -0700 | [diff] [blame] | 3742 | iscsit_free_cmd(cmd, false); |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3743 | break; |
| 3744 | case ISTATE_SEND_NOPIN_WANT_RESPONSE: |
| 3745 | iscsit_mod_nopin_response_timer(conn); |
| 3746 | ret = iscsit_send_unsolicited_nopin(cmd, conn, 1); |
| 3747 | if (ret < 0) |
| 3748 | goto err; |
| 3749 | break; |
| 3750 | case ISTATE_SEND_NOPIN_NO_RESPONSE: |
| 3751 | ret = iscsit_send_unsolicited_nopin(cmd, conn, 0); |
| 3752 | if (ret < 0) |
| 3753 | goto err; |
| 3754 | break; |
| 3755 | default: |
| 3756 | pr_err("Unknown Opcode: 0x%02x ITT:" |
| 3757 | " 0x%08x, i_state: %d on CID: %hu\n", |
| 3758 | cmd->iscsi_opcode, cmd->init_task_tag, state, |
| 3759 | conn->cid); |
| 3760 | goto err; |
| 3761 | } |
| 3762 | |
| 3763 | return 0; |
| 3764 | |
| 3765 | err: |
| 3766 | return -1; |
| 3767 | } |
| 3768 | |
| 3769 | static int |
| 3770 | iscsit_handle_immediate_queue(struct iscsi_conn *conn) |
| 3771 | { |
| 3772 | struct iscsit_transport *t = conn->conn_transport; |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3773 | struct iscsi_queue_req *qr; |
| 3774 | struct iscsi_cmd *cmd; |
| 3775 | u8 state; |
| 3776 | int ret; |
| 3777 | |
| 3778 | while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) { |
| 3779 | atomic_set(&conn->check_immediate_queue, 0); |
| 3780 | cmd = qr->cmd; |
| 3781 | state = qr->state; |
| 3782 | kmem_cache_free(lio_qr_cache, qr); |
| 3783 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3784 | ret = t->iscsit_immediate_queue(conn, cmd, state); |
| 3785 | if (ret < 0) |
| 3786 | return ret; |
| 3787 | } |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3788 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3789 | return 0; |
| 3790 | } |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3791 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3792 | static int |
| 3793 | iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state) |
| 3794 | { |
| 3795 | int ret; |
| 3796 | |
| 3797 | check_rsp_state: |
| 3798 | switch (state) { |
| 3799 | case ISTATE_SEND_DATAIN: |
| 3800 | ret = iscsit_send_datain(cmd, conn); |
| 3801 | if (ret < 0) |
| 3802 | goto err; |
| 3803 | else if (!ret) |
| 3804 | /* more drs */ |
| 3805 | goto check_rsp_state; |
| 3806 | else if (ret == 1) { |
| 3807 | /* all done */ |
| 3808 | spin_lock_bh(&cmd->istate_lock); |
| 3809 | cmd->i_state = ISTATE_SENT_STATUS; |
| 3810 | spin_unlock_bh(&cmd->istate_lock); |
| 3811 | |
| 3812 | if (atomic_read(&conn->check_immediate_queue)) |
| 3813 | return 1; |
| 3814 | |
| 3815 | return 0; |
| 3816 | } else if (ret == 2) { |
| 3817 | /* Still must send status, |
| 3818 | SCF_TRANSPORT_TASK_SENSE was set */ |
| 3819 | spin_lock_bh(&cmd->istate_lock); |
| 3820 | cmd->i_state = ISTATE_SEND_STATUS; |
| 3821 | spin_unlock_bh(&cmd->istate_lock); |
| 3822 | state = ISTATE_SEND_STATUS; |
| 3823 | goto check_rsp_state; |
| 3824 | } |
| 3825 | |
| 3826 | break; |
| 3827 | case ISTATE_SEND_STATUS: |
| 3828 | case ISTATE_SEND_STATUS_RECOVERY: |
| 3829 | ret = iscsit_send_response(cmd, conn); |
| 3830 | break; |
| 3831 | case ISTATE_SEND_LOGOUTRSP: |
| 3832 | ret = iscsit_send_logout(cmd, conn); |
| 3833 | break; |
| 3834 | case ISTATE_SEND_ASYNCMSG: |
| 3835 | ret = iscsit_send_conn_drop_async_message( |
| 3836 | cmd, conn); |
| 3837 | break; |
| 3838 | case ISTATE_SEND_NOPIN: |
| 3839 | ret = iscsit_send_nopin(cmd, conn); |
| 3840 | break; |
| 3841 | case ISTATE_SEND_REJECT: |
| 3842 | ret = iscsit_send_reject(cmd, conn); |
| 3843 | break; |
| 3844 | case ISTATE_SEND_TASKMGTRSP: |
| 3845 | ret = iscsit_send_task_mgt_rsp(cmd, conn); |
| 3846 | if (ret != 0) |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3847 | break; |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3848 | ret = iscsit_tmr_post_handler(cmd, conn); |
| 3849 | if (ret != 0) |
| 3850 | iscsit_fall_back_to_erl0(conn->sess); |
| 3851 | break; |
| 3852 | case ISTATE_SEND_TEXTRSP: |
| 3853 | ret = iscsit_send_text_rsp(cmd, conn); |
| 3854 | break; |
| 3855 | default: |
| 3856 | pr_err("Unknown Opcode: 0x%02x ITT:" |
| 3857 | " 0x%08x, i_state: %d on CID: %hu\n", |
| 3858 | cmd->iscsi_opcode, cmd->init_task_tag, |
| 3859 | state, conn->cid); |
| 3860 | goto err; |
| 3861 | } |
| 3862 | if (ret < 0) |
| 3863 | goto err; |
| 3864 | |
| 3865 | if (iscsit_send_tx_data(cmd, conn, 1) < 0) { |
| 3866 | iscsit_tx_thread_wait_for_tcp(conn); |
| 3867 | iscsit_unmap_iovec(cmd); |
| 3868 | goto err; |
| 3869 | } |
| 3870 | iscsit_unmap_iovec(cmd); |
| 3871 | |
| 3872 | switch (state) { |
| 3873 | case ISTATE_SEND_LOGOUTRSP: |
| 3874 | if (!iscsit_logout_post_handler(cmd, conn)) |
| 3875 | goto restart; |
| 3876 | /* fall through */ |
| 3877 | case ISTATE_SEND_STATUS: |
| 3878 | case ISTATE_SEND_ASYNCMSG: |
| 3879 | case ISTATE_SEND_NOPIN: |
| 3880 | case ISTATE_SEND_STATUS_RECOVERY: |
| 3881 | case ISTATE_SEND_TEXTRSP: |
| 3882 | case ISTATE_SEND_TASKMGTRSP: |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 3883 | case ISTATE_SEND_REJECT: |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3884 | spin_lock_bh(&cmd->istate_lock); |
| 3885 | cmd->i_state = ISTATE_SENT_STATUS; |
| 3886 | spin_unlock_bh(&cmd->istate_lock); |
| 3887 | break; |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3888 | default: |
| 3889 | pr_err("Unknown Opcode: 0x%02x ITT:" |
| 3890 | " 0x%08x, i_state: %d on CID: %hu\n", |
| 3891 | cmd->iscsi_opcode, cmd->init_task_tag, |
| 3892 | cmd->i_state, conn->cid); |
| 3893 | goto err; |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3894 | } |
| 3895 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3896 | if (atomic_read(&conn->check_immediate_queue)) |
| 3897 | return 1; |
| 3898 | |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3899 | return 0; |
| 3900 | |
| 3901 | err: |
| 3902 | return -1; |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3903 | restart: |
| 3904 | return -EAGAIN; |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3905 | } |
| 3906 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3907 | static int iscsit_handle_response_queue(struct iscsi_conn *conn) |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3908 | { |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3909 | struct iscsit_transport *t = conn->conn_transport; |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3910 | struct iscsi_queue_req *qr; |
| 3911 | struct iscsi_cmd *cmd; |
| 3912 | u8 state; |
| 3913 | int ret; |
| 3914 | |
| 3915 | while ((qr = iscsit_get_cmd_from_response_queue(conn))) { |
| 3916 | cmd = qr->cmd; |
| 3917 | state = qr->state; |
| 3918 | kmem_cache_free(lio_qr_cache, qr); |
| 3919 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3920 | ret = t->iscsit_response_queue(conn, cmd, state); |
| 3921 | if (ret == 1 || ret < 0) |
| 3922 | return ret; |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3923 | } |
| 3924 | |
| 3925 | return 0; |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3926 | } |
| 3927 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3928 | int iscsi_target_tx_thread(void *arg) |
| 3929 | { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3930 | int ret = 0; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3931 | struct iscsi_conn *conn; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 3932 | struct iscsi_thread_set *ts = arg; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3933 | /* |
| 3934 | * Allow ourselves to be interrupted by SIGINT so that a |
| 3935 | * connection recovery / failure event can be triggered externally. |
| 3936 | */ |
| 3937 | allow_signal(SIGINT); |
| 3938 | |
| 3939 | restart: |
| 3940 | conn = iscsi_tx_thread_pre_handler(ts); |
| 3941 | if (!conn) |
| 3942 | goto out; |
| 3943 | |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3944 | ret = 0; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3945 | |
| 3946 | while (!kthread_should_stop()) { |
| 3947 | /* |
| 3948 | * Ensure that both TX and RX per connection kthreads |
| 3949 | * are scheduled to run on the same CPU. |
| 3950 | */ |
| 3951 | iscsit_thread_check_cpumask(conn, current, 1); |
| 3952 | |
Roland Dreier | d5627ac | 2012-10-31 09:16:46 -0700 | [diff] [blame] | 3953 | wait_event_interruptible(conn->queues_wq, |
| 3954 | !iscsit_conn_all_queues_empty(conn) || |
| 3955 | ts->status == ISCSI_THREAD_SET_RESET); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3956 | |
| 3957 | if ((ts->status == ISCSI_THREAD_SET_RESET) || |
| 3958 | signal_pending(current)) |
| 3959 | goto transport_err; |
| 3960 | |
Nicholas Bellinger | fd3a902 | 2013-02-27 17:53:52 -0800 | [diff] [blame] | 3961 | get_immediate: |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3962 | ret = iscsit_handle_immediate_queue(conn); |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3963 | if (ret < 0) |
| 3964 | goto transport_err; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3965 | |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 3966 | ret = iscsit_handle_response_queue(conn); |
Nicholas Bellinger | fd3a902 | 2013-02-27 17:53:52 -0800 | [diff] [blame] | 3967 | if (ret == 1) |
| 3968 | goto get_immediate; |
| 3969 | else if (ret == -EAGAIN) |
Andy Grover | 6f3c0e6 | 2012-04-03 15:51:09 -0700 | [diff] [blame] | 3970 | goto restart; |
| 3971 | else if (ret < 0) |
| 3972 | goto transport_err; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
| 3975 | transport_err: |
| 3976 | iscsit_take_action_for_connection_exit(conn); |
| 3977 | goto restart; |
| 3978 | out: |
| 3979 | return 0; |
| 3980 | } |
| 3981 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 3982 | static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf) |
| 3983 | { |
| 3984 | struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf; |
| 3985 | struct iscsi_cmd *cmd; |
| 3986 | int ret = 0; |
| 3987 | |
| 3988 | switch (hdr->opcode & ISCSI_OPCODE_MASK) { |
| 3989 | case ISCSI_OP_SCSI_CMD: |
Nicholas Bellinger | 676687c | 2014-01-20 03:36:44 +0000 | [diff] [blame] | 3990 | cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 3991 | if (!cmd) |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 3992 | goto reject; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 3993 | |
| 3994 | ret = iscsit_handle_scsi_cmd(conn, cmd, buf); |
| 3995 | break; |
| 3996 | case ISCSI_OP_SCSI_DATA_OUT: |
| 3997 | ret = iscsit_handle_data_out(conn, buf); |
| 3998 | break; |
| 3999 | case ISCSI_OP_NOOP_OUT: |
| 4000 | cmd = NULL; |
| 4001 | if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) { |
Nicholas Bellinger | 676687c | 2014-01-20 03:36:44 +0000 | [diff] [blame] | 4002 | cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 4003 | if (!cmd) |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 4004 | goto reject; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 4005 | } |
| 4006 | ret = iscsit_handle_nop_out(conn, cmd, buf); |
| 4007 | break; |
| 4008 | case ISCSI_OP_SCSI_TMFUNC: |
Nicholas Bellinger | 676687c | 2014-01-20 03:36:44 +0000 | [diff] [blame] | 4009 | cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 4010 | if (!cmd) |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 4011 | goto reject; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 4012 | |
| 4013 | ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf); |
| 4014 | break; |
| 4015 | case ISCSI_OP_TEXT: |
Nicholas Bellinger | 676687c | 2014-01-20 03:36:44 +0000 | [diff] [blame] | 4016 | cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 4017 | if (!cmd) |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 4018 | goto reject; |
Nicholas Bellinger | 64534aa | 2013-06-14 16:46:16 -0700 | [diff] [blame] | 4019 | |
| 4020 | ret = iscsit_handle_text_cmd(conn, cmd, buf); |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 4021 | break; |
| 4022 | case ISCSI_OP_LOGOUT: |
Nicholas Bellinger | 676687c | 2014-01-20 03:36:44 +0000 | [diff] [blame] | 4023 | cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE); |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 4024 | if (!cmd) |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 4025 | goto reject; |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 4026 | |
| 4027 | ret = iscsit_handle_logout_cmd(conn, cmd, buf); |
| 4028 | if (ret > 0) |
| 4029 | wait_for_completion_timeout(&conn->conn_logout_comp, |
| 4030 | SECONDS_FOR_LOGOUT_COMP * HZ); |
| 4031 | break; |
| 4032 | case ISCSI_OP_SNACK: |
| 4033 | ret = iscsit_handle_snack(conn, buf); |
| 4034 | break; |
| 4035 | default: |
| 4036 | pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode); |
| 4037 | if (!conn->sess->sess_ops->ErrorRecoveryLevel) { |
| 4038 | pr_err("Cannot recover from unknown" |
| 4039 | " opcode while ERL=0, closing iSCSI connection.\n"); |
| 4040 | return -1; |
| 4041 | } |
| 4042 | if (!conn->conn_ops->OFMarker) { |
| 4043 | pr_err("Unable to recover from unknown" |
| 4044 | " opcode while OFMarker=No, closing iSCSI" |
| 4045 | " connection.\n"); |
| 4046 | return -1; |
| 4047 | } |
| 4048 | if (iscsit_recover_from_unknown_opcode(conn) < 0) { |
| 4049 | pr_err("Unable to recover from unknown" |
| 4050 | " opcode, closing iSCSI connection.\n"); |
| 4051 | return -1; |
| 4052 | } |
| 4053 | break; |
| 4054 | } |
| 4055 | |
| 4056 | return ret; |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 4057 | reject: |
| 4058 | return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf); |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 4059 | } |
| 4060 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4061 | int iscsi_target_rx_thread(void *arg) |
| 4062 | { |
| 4063 | int ret; |
| 4064 | u8 buffer[ISCSI_HDR_LEN], opcode; |
| 4065 | u32 checksum = 0, digest = 0; |
| 4066 | struct iscsi_conn *conn = NULL; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 4067 | struct iscsi_thread_set *ts = arg; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4068 | struct kvec iov; |
| 4069 | /* |
| 4070 | * Allow ourselves to be interrupted by SIGINT so that a |
| 4071 | * connection recovery / failure event can be triggered externally. |
| 4072 | */ |
| 4073 | allow_signal(SIGINT); |
| 4074 | |
| 4075 | restart: |
| 4076 | conn = iscsi_rx_thread_pre_handler(ts); |
| 4077 | if (!conn) |
| 4078 | goto out; |
| 4079 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 4080 | if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) { |
| 4081 | struct completion comp; |
| 4082 | int rc; |
| 4083 | |
| 4084 | init_completion(&comp); |
| 4085 | rc = wait_for_completion_interruptible(&comp); |
| 4086 | if (rc < 0) |
| 4087 | goto transport_err; |
| 4088 | |
| 4089 | goto out; |
| 4090 | } |
| 4091 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4092 | while (!kthread_should_stop()) { |
| 4093 | /* |
| 4094 | * Ensure that both TX and RX per connection kthreads |
| 4095 | * are scheduled to run on the same CPU. |
| 4096 | */ |
| 4097 | iscsit_thread_check_cpumask(conn, current, 0); |
| 4098 | |
| 4099 | memset(buffer, 0, ISCSI_HDR_LEN); |
| 4100 | memset(&iov, 0, sizeof(struct kvec)); |
| 4101 | |
| 4102 | iov.iov_base = buffer; |
| 4103 | iov.iov_len = ISCSI_HDR_LEN; |
| 4104 | |
| 4105 | ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN); |
| 4106 | if (ret != ISCSI_HDR_LEN) { |
| 4107 | iscsit_rx_thread_wait_for_tcp(conn); |
| 4108 | goto transport_err; |
| 4109 | } |
| 4110 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4111 | if (conn->conn_ops->HeaderDigest) { |
| 4112 | iov.iov_base = &digest; |
| 4113 | iov.iov_len = ISCSI_CRC_LEN; |
| 4114 | |
| 4115 | ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN); |
| 4116 | if (ret != ISCSI_CRC_LEN) { |
| 4117 | iscsit_rx_thread_wait_for_tcp(conn); |
| 4118 | goto transport_err; |
| 4119 | } |
| 4120 | |
| 4121 | iscsit_do_crypto_hash_buf(&conn->conn_rx_hash, |
| 4122 | buffer, ISCSI_HDR_LEN, |
| 4123 | 0, NULL, (u8 *)&checksum); |
| 4124 | |
| 4125 | if (digest != checksum) { |
| 4126 | pr_err("HeaderDigest CRC32C failed," |
| 4127 | " received 0x%08x, computed 0x%08x\n", |
| 4128 | digest, checksum); |
| 4129 | /* |
| 4130 | * Set the PDU to 0xff so it will intentionally |
| 4131 | * hit default in the switch below. |
| 4132 | */ |
| 4133 | memset(buffer, 0xff, ISCSI_HDR_LEN); |
Nicholas Bellinger | 04f3b31 | 2013-11-13 18:54:45 -0800 | [diff] [blame] | 4134 | atomic_long_inc(&conn->sess->conn_digest_errors); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4135 | } else { |
| 4136 | pr_debug("Got HeaderDigest CRC32C" |
| 4137 | " 0x%08x\n", checksum); |
| 4138 | } |
| 4139 | } |
| 4140 | |
| 4141 | if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) |
| 4142 | goto transport_err; |
| 4143 | |
| 4144 | opcode = buffer[0] & ISCSI_OPCODE_MASK; |
| 4145 | |
| 4146 | if (conn->sess->sess_ops->SessionType && |
| 4147 | ((!(opcode & ISCSI_OP_TEXT)) || |
| 4148 | (!(opcode & ISCSI_OP_LOGOUT)))) { |
| 4149 | pr_err("Received illegal iSCSI Opcode: 0x%02x" |
| 4150 | " while in Discovery Session, rejecting.\n", opcode); |
Nicholas Bellinger | ba15991 | 2013-07-03 03:48:24 -0700 | [diff] [blame] | 4151 | iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, |
| 4152 | buffer); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4153 | goto transport_err; |
| 4154 | } |
| 4155 | |
Nicholas Bellinger | 3e1c81a | 2013-03-06 22:18:24 -0800 | [diff] [blame] | 4156 | ret = iscsi_target_rx_opcode(conn, buffer); |
| 4157 | if (ret < 0) |
| 4158 | goto transport_err; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | transport_err: |
| 4162 | if (!signal_pending(current)) |
| 4163 | atomic_set(&conn->transport_failed, 1); |
| 4164 | iscsit_take_action_for_connection_exit(conn); |
| 4165 | goto restart; |
| 4166 | out: |
| 4167 | return 0; |
| 4168 | } |
| 4169 | |
| 4170 | static void iscsit_release_commands_from_conn(struct iscsi_conn *conn) |
| 4171 | { |
| 4172 | struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL; |
| 4173 | struct iscsi_session *sess = conn->sess; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4174 | /* |
| 4175 | * We expect this function to only ever be called from either RX or TX |
| 4176 | * thread context via iscsit_close_connection() once the other context |
| 4177 | * has been reset -> returned sleeping pre-handler state. |
| 4178 | */ |
| 4179 | spin_lock_bh(&conn->cmd_lock); |
Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 4180 | list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4181 | |
Nicholas Bellinger | 5159d76 | 2014-02-03 12:53:51 -0800 | [diff] [blame] | 4182 | list_del_init(&cmd->i_conn_node); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4183 | spin_unlock_bh(&conn->cmd_lock); |
| 4184 | |
| 4185 | iscsit_increment_maxcmdsn(cmd, sess); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4186 | |
Nicholas Bellinger | aafc9d1 | 2013-05-31 00:49:41 -0700 | [diff] [blame] | 4187 | iscsit_free_cmd(cmd, true); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4188 | |
| 4189 | spin_lock_bh(&conn->cmd_lock); |
| 4190 | } |
| 4191 | spin_unlock_bh(&conn->cmd_lock); |
| 4192 | } |
| 4193 | |
| 4194 | static void iscsit_stop_timers_for_cmds( |
| 4195 | struct iscsi_conn *conn) |
| 4196 | { |
| 4197 | struct iscsi_cmd *cmd; |
| 4198 | |
| 4199 | spin_lock_bh(&conn->cmd_lock); |
Andy Grover | 2fbb471 | 2012-04-03 15:51:01 -0700 | [diff] [blame] | 4200 | list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4201 | if (cmd->data_direction == DMA_TO_DEVICE) |
| 4202 | iscsit_stop_dataout_timer(cmd); |
| 4203 | } |
| 4204 | spin_unlock_bh(&conn->cmd_lock); |
| 4205 | } |
| 4206 | |
| 4207 | int iscsit_close_connection( |
| 4208 | struct iscsi_conn *conn) |
| 4209 | { |
| 4210 | int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT); |
| 4211 | struct iscsi_session *sess = conn->sess; |
| 4212 | |
| 4213 | pr_debug("Closing iSCSI connection CID %hu on SID:" |
| 4214 | " %u\n", conn->cid, sess->sid); |
| 4215 | /* |
| 4216 | * Always up conn_logout_comp just in case the RX Thread is sleeping |
| 4217 | * and the logout response never got sent because the connection |
| 4218 | * failed. |
| 4219 | */ |
| 4220 | complete(&conn->conn_logout_comp); |
| 4221 | |
| 4222 | iscsi_release_thread_set(conn); |
| 4223 | |
| 4224 | iscsit_stop_timers_for_cmds(conn); |
| 4225 | iscsit_stop_nopin_response_timer(conn); |
| 4226 | iscsit_stop_nopin_timer(conn); |
Nicholas Bellinger | defd884 | 2014-02-03 12:54:39 -0800 | [diff] [blame] | 4227 | |
| 4228 | if (conn->conn_transport->iscsit_wait_conn) |
| 4229 | conn->conn_transport->iscsit_wait_conn(conn); |
| 4230 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4231 | /* |
| 4232 | * During Connection recovery drop unacknowledged out of order |
| 4233 | * commands for this connection, and prepare the other commands |
| 4234 | * for realligence. |
| 4235 | * |
| 4236 | * During normal operation clear the out of order commands (but |
| 4237 | * do not free the struct iscsi_ooo_cmdsn's) and release all |
| 4238 | * struct iscsi_cmds. |
| 4239 | */ |
| 4240 | if (atomic_read(&conn->connection_recovery)) { |
| 4241 | iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn); |
| 4242 | iscsit_prepare_cmds_for_realligance(conn); |
| 4243 | } else { |
| 4244 | iscsit_clear_ooo_cmdsns_for_conn(conn); |
| 4245 | iscsit_release_commands_from_conn(conn); |
| 4246 | } |
Nicholas Bellinger | bbc0504 | 2014-06-10 04:03:54 +0000 | [diff] [blame] | 4247 | iscsit_free_queue_reqs_for_conn(conn); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4248 | |
| 4249 | /* |
| 4250 | * Handle decrementing session or connection usage count if |
| 4251 | * a logout response was not able to be sent because the |
| 4252 | * connection failed. Fall back to Session Recovery here. |
| 4253 | */ |
| 4254 | if (atomic_read(&conn->conn_logout_remove)) { |
| 4255 | if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) { |
| 4256 | iscsit_dec_conn_usage_count(conn); |
| 4257 | iscsit_dec_session_usage_count(sess); |
| 4258 | } |
| 4259 | if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) |
| 4260 | iscsit_dec_conn_usage_count(conn); |
| 4261 | |
| 4262 | atomic_set(&conn->conn_logout_remove, 0); |
| 4263 | atomic_set(&sess->session_reinstatement, 0); |
| 4264 | atomic_set(&sess->session_fall_back_to_erl0, 1); |
| 4265 | } |
| 4266 | |
| 4267 | spin_lock_bh(&sess->conn_lock); |
| 4268 | list_del(&conn->conn_list); |
| 4269 | |
| 4270 | /* |
| 4271 | * Attempt to let the Initiator know this connection failed by |
| 4272 | * sending an Connection Dropped Async Message on another |
| 4273 | * active connection. |
| 4274 | */ |
| 4275 | if (atomic_read(&conn->connection_recovery)) |
| 4276 | iscsit_build_conn_drop_async_message(conn); |
| 4277 | |
| 4278 | spin_unlock_bh(&sess->conn_lock); |
| 4279 | |
| 4280 | /* |
| 4281 | * If connection reinstatement is being performed on this connection, |
| 4282 | * up the connection reinstatement semaphore that is being blocked on |
| 4283 | * in iscsit_cause_connection_reinstatement(). |
| 4284 | */ |
| 4285 | spin_lock_bh(&conn->state_lock); |
| 4286 | if (atomic_read(&conn->sleep_on_conn_wait_comp)) { |
| 4287 | spin_unlock_bh(&conn->state_lock); |
| 4288 | complete(&conn->conn_wait_comp); |
| 4289 | wait_for_completion(&conn->conn_post_wait_comp); |
| 4290 | spin_lock_bh(&conn->state_lock); |
| 4291 | } |
| 4292 | |
| 4293 | /* |
| 4294 | * If connection reinstatement is being performed on this connection |
| 4295 | * by receiving a REMOVECONNFORRECOVERY logout request, up the |
| 4296 | * connection wait rcfr semaphore that is being blocked on |
| 4297 | * an iscsit_connection_reinstatement_rcfr(). |
| 4298 | */ |
| 4299 | if (atomic_read(&conn->connection_wait_rcfr)) { |
| 4300 | spin_unlock_bh(&conn->state_lock); |
| 4301 | complete(&conn->conn_wait_rcfr_comp); |
| 4302 | wait_for_completion(&conn->conn_post_wait_comp); |
| 4303 | spin_lock_bh(&conn->state_lock); |
| 4304 | } |
| 4305 | atomic_set(&conn->connection_reinstatement, 1); |
| 4306 | spin_unlock_bh(&conn->state_lock); |
| 4307 | |
| 4308 | /* |
| 4309 | * If any other processes are accessing this connection pointer we |
| 4310 | * must wait until they have completed. |
| 4311 | */ |
| 4312 | iscsit_check_conn_usage_count(conn); |
| 4313 | |
| 4314 | if (conn->conn_rx_hash.tfm) |
| 4315 | crypto_free_hash(conn->conn_rx_hash.tfm); |
| 4316 | if (conn->conn_tx_hash.tfm) |
| 4317 | crypto_free_hash(conn->conn_tx_hash.tfm); |
| 4318 | |
Joern Engel | fbecb65 | 2014-09-02 17:49:47 -0400 | [diff] [blame] | 4319 | free_cpumask_var(conn->conn_cpumask); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4320 | |
| 4321 | kfree(conn->conn_ops); |
| 4322 | conn->conn_ops = NULL; |
| 4323 | |
Al Viro | bf6932f | 2012-07-21 08:55:18 +0100 | [diff] [blame] | 4324 | if (conn->sock) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4325 | sock_release(conn->sock); |
Nicholas Bellinger | baa4d64 | 2013-03-06 21:54:13 -0800 | [diff] [blame] | 4326 | |
| 4327 | if (conn->conn_transport->iscsit_free_conn) |
| 4328 | conn->conn_transport->iscsit_free_conn(conn); |
| 4329 | |
| 4330 | iscsit_put_transport(conn->conn_transport); |
| 4331 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4332 | conn->thread_set = NULL; |
| 4333 | |
| 4334 | pr_debug("Moving to TARG_CONN_STATE_FREE.\n"); |
| 4335 | conn->conn_state = TARG_CONN_STATE_FREE; |
| 4336 | kfree(conn); |
| 4337 | |
| 4338 | spin_lock_bh(&sess->conn_lock); |
| 4339 | atomic_dec(&sess->nconn); |
| 4340 | pr_debug("Decremented iSCSI connection count to %hu from node:" |
| 4341 | " %s\n", atomic_read(&sess->nconn), |
| 4342 | sess->sess_ops->InitiatorName); |
| 4343 | /* |
| 4344 | * Make sure that if one connection fails in an non ERL=2 iSCSI |
| 4345 | * Session that they all fail. |
| 4346 | */ |
| 4347 | if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout && |
| 4348 | !atomic_read(&sess->session_logout)) |
| 4349 | atomic_set(&sess->session_fall_back_to_erl0, 1); |
| 4350 | |
| 4351 | /* |
| 4352 | * If this was not the last connection in the session, and we are |
| 4353 | * performing session reinstatement or falling back to ERL=0, call |
| 4354 | * iscsit_stop_session() without sleeping to shutdown the other |
| 4355 | * active connections. |
| 4356 | */ |
| 4357 | if (atomic_read(&sess->nconn)) { |
| 4358 | if (!atomic_read(&sess->session_reinstatement) && |
| 4359 | !atomic_read(&sess->session_fall_back_to_erl0)) { |
| 4360 | spin_unlock_bh(&sess->conn_lock); |
| 4361 | return 0; |
| 4362 | } |
| 4363 | if (!atomic_read(&sess->session_stop_active)) { |
| 4364 | atomic_set(&sess->session_stop_active, 1); |
| 4365 | spin_unlock_bh(&sess->conn_lock); |
| 4366 | iscsit_stop_session(sess, 0, 0); |
| 4367 | return 0; |
| 4368 | } |
| 4369 | spin_unlock_bh(&sess->conn_lock); |
| 4370 | return 0; |
| 4371 | } |
| 4372 | |
| 4373 | /* |
| 4374 | * If this was the last connection in the session and one of the |
| 4375 | * following is occurring: |
| 4376 | * |
| 4377 | * Session Reinstatement is not being performed, and are falling back |
| 4378 | * to ERL=0 call iscsit_close_session(). |
| 4379 | * |
| 4380 | * Session Logout was requested. iscsit_close_session() will be called |
| 4381 | * elsewhere. |
| 4382 | * |
| 4383 | * Session Continuation is not being performed, start the Time2Retain |
| 4384 | * handler and check if sleep_on_sess_wait_sem is active. |
| 4385 | */ |
| 4386 | if (!atomic_read(&sess->session_reinstatement) && |
| 4387 | atomic_read(&sess->session_fall_back_to_erl0)) { |
| 4388 | spin_unlock_bh(&sess->conn_lock); |
Nicholas Bellinger | 99367f0 | 2012-02-27 01:43:32 -0800 | [diff] [blame] | 4389 | target_put_session(sess->se_sess); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4390 | |
| 4391 | return 0; |
| 4392 | } else if (atomic_read(&sess->session_logout)) { |
| 4393 | pr_debug("Moving to TARG_SESS_STATE_FREE.\n"); |
| 4394 | sess->session_state = TARG_SESS_STATE_FREE; |
| 4395 | spin_unlock_bh(&sess->conn_lock); |
| 4396 | |
| 4397 | if (atomic_read(&sess->sleep_on_sess_wait_comp)) |
| 4398 | complete(&sess->session_wait_comp); |
| 4399 | |
| 4400 | return 0; |
| 4401 | } else { |
| 4402 | pr_debug("Moving to TARG_SESS_STATE_FAILED.\n"); |
| 4403 | sess->session_state = TARG_SESS_STATE_FAILED; |
| 4404 | |
| 4405 | if (!atomic_read(&sess->session_continuation)) { |
| 4406 | spin_unlock_bh(&sess->conn_lock); |
| 4407 | iscsit_start_time2retain_handler(sess); |
| 4408 | } else |
| 4409 | spin_unlock_bh(&sess->conn_lock); |
| 4410 | |
| 4411 | if (atomic_read(&sess->sleep_on_sess_wait_comp)) |
| 4412 | complete(&sess->session_wait_comp); |
| 4413 | |
| 4414 | return 0; |
| 4415 | } |
| 4416 | spin_unlock_bh(&sess->conn_lock); |
| 4417 | |
| 4418 | return 0; |
| 4419 | } |
| 4420 | |
| 4421 | int iscsit_close_session(struct iscsi_session *sess) |
| 4422 | { |
Andy Grover | 60bfcf8 | 2013-10-09 11:05:58 -0700 | [diff] [blame] | 4423 | struct iscsi_portal_group *tpg = sess->tpg; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4424 | struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; |
| 4425 | |
| 4426 | if (atomic_read(&sess->nconn)) { |
| 4427 | pr_err("%d connection(s) still exist for iSCSI session" |
| 4428 | " to %s\n", atomic_read(&sess->nconn), |
| 4429 | sess->sess_ops->InitiatorName); |
| 4430 | BUG(); |
| 4431 | } |
| 4432 | |
| 4433 | spin_lock_bh(&se_tpg->session_lock); |
| 4434 | atomic_set(&sess->session_logout, 1); |
| 4435 | atomic_set(&sess->session_reinstatement, 1); |
| 4436 | iscsit_stop_time2retain_timer(sess); |
| 4437 | spin_unlock_bh(&se_tpg->session_lock); |
| 4438 | |
| 4439 | /* |
| 4440 | * transport_deregister_session_configfs() will clear the |
| 4441 | * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context |
| 4442 | * can be setting it again with __transport_register_session() in |
| 4443 | * iscsi_post_login_handler() again after the iscsit_stop_session() |
| 4444 | * completes in iscsi_np context. |
| 4445 | */ |
| 4446 | transport_deregister_session_configfs(sess->se_sess); |
| 4447 | |
| 4448 | /* |
| 4449 | * If any other processes are accessing this session pointer we must |
| 4450 | * wait until they have completed. If we are in an interrupt (the |
| 4451 | * time2retain handler) and contain and active session usage count we |
| 4452 | * restart the timer and exit. |
| 4453 | */ |
| 4454 | if (!in_interrupt()) { |
| 4455 | if (iscsit_check_session_usage_count(sess) == 1) |
| 4456 | iscsit_stop_session(sess, 1, 1); |
| 4457 | } else { |
| 4458 | if (iscsit_check_session_usage_count(sess) == 2) { |
| 4459 | atomic_set(&sess->session_logout, 0); |
| 4460 | iscsit_start_time2retain_handler(sess); |
| 4461 | return 0; |
| 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | transport_deregister_session(sess->se_sess); |
| 4466 | |
| 4467 | if (sess->sess_ops->ErrorRecoveryLevel == 2) |
| 4468 | iscsit_free_connection_recovery_entires(sess); |
| 4469 | |
| 4470 | iscsit_free_all_ooo_cmdsns(sess); |
| 4471 | |
| 4472 | spin_lock_bh(&se_tpg->session_lock); |
| 4473 | pr_debug("Moving to TARG_SESS_STATE_FREE.\n"); |
| 4474 | sess->session_state = TARG_SESS_STATE_FREE; |
| 4475 | pr_debug("Released iSCSI session from node: %s\n", |
| 4476 | sess->sess_ops->InitiatorName); |
| 4477 | tpg->nsessions--; |
| 4478 | if (tpg->tpg_tiqn) |
| 4479 | tpg->tpg_tiqn->tiqn_nsessions--; |
| 4480 | |
| 4481 | pr_debug("Decremented number of active iSCSI Sessions on" |
| 4482 | " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions); |
| 4483 | |
| 4484 | spin_lock(&sess_idr_lock); |
| 4485 | idr_remove(&sess_idr, sess->session_index); |
| 4486 | spin_unlock(&sess_idr_lock); |
| 4487 | |
| 4488 | kfree(sess->sess_ops); |
| 4489 | sess->sess_ops = NULL; |
| 4490 | spin_unlock_bh(&se_tpg->session_lock); |
| 4491 | |
| 4492 | kfree(sess); |
| 4493 | return 0; |
| 4494 | } |
| 4495 | |
| 4496 | static void iscsit_logout_post_handler_closesession( |
| 4497 | struct iscsi_conn *conn) |
| 4498 | { |
| 4499 | struct iscsi_session *sess = conn->sess; |
| 4500 | |
| 4501 | iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD); |
| 4502 | iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD); |
| 4503 | |
| 4504 | atomic_set(&conn->conn_logout_remove, 0); |
| 4505 | complete(&conn->conn_logout_comp); |
| 4506 | |
| 4507 | iscsit_dec_conn_usage_count(conn); |
| 4508 | iscsit_stop_session(sess, 1, 1); |
| 4509 | iscsit_dec_session_usage_count(sess); |
Nicholas Bellinger | 99367f0 | 2012-02-27 01:43:32 -0800 | [diff] [blame] | 4510 | target_put_session(sess->se_sess); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | static void iscsit_logout_post_handler_samecid( |
| 4514 | struct iscsi_conn *conn) |
| 4515 | { |
| 4516 | iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD); |
| 4517 | iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD); |
| 4518 | |
| 4519 | atomic_set(&conn->conn_logout_remove, 0); |
| 4520 | complete(&conn->conn_logout_comp); |
| 4521 | |
| 4522 | iscsit_cause_connection_reinstatement(conn, 1); |
| 4523 | iscsit_dec_conn_usage_count(conn); |
| 4524 | } |
| 4525 | |
| 4526 | static void iscsit_logout_post_handler_diffcid( |
| 4527 | struct iscsi_conn *conn, |
| 4528 | u16 cid) |
| 4529 | { |
| 4530 | struct iscsi_conn *l_conn; |
| 4531 | struct iscsi_session *sess = conn->sess; |
Nicholas Bellinger | b53b0d99 | 2014-09-17 11:45:17 -0700 | [diff] [blame] | 4532 | bool conn_found = false; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4533 | |
| 4534 | if (!sess) |
| 4535 | return; |
| 4536 | |
| 4537 | spin_lock_bh(&sess->conn_lock); |
| 4538 | list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) { |
| 4539 | if (l_conn->cid == cid) { |
| 4540 | iscsit_inc_conn_usage_count(l_conn); |
Nicholas Bellinger | b53b0d99 | 2014-09-17 11:45:17 -0700 | [diff] [blame] | 4541 | conn_found = true; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4542 | break; |
| 4543 | } |
| 4544 | } |
| 4545 | spin_unlock_bh(&sess->conn_lock); |
| 4546 | |
Nicholas Bellinger | b53b0d99 | 2014-09-17 11:45:17 -0700 | [diff] [blame] | 4547 | if (!conn_found) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4548 | return; |
| 4549 | |
| 4550 | if (l_conn->sock) |
| 4551 | l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN); |
| 4552 | |
| 4553 | spin_lock_bh(&l_conn->state_lock); |
| 4554 | pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n"); |
| 4555 | l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT; |
| 4556 | spin_unlock_bh(&l_conn->state_lock); |
| 4557 | |
| 4558 | iscsit_cause_connection_reinstatement(l_conn, 1); |
| 4559 | iscsit_dec_conn_usage_count(l_conn); |
| 4560 | } |
| 4561 | |
| 4562 | /* |
| 4563 | * Return of 0 causes the TX thread to restart. |
| 4564 | */ |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 4565 | int iscsit_logout_post_handler( |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4566 | struct iscsi_cmd *cmd, |
| 4567 | struct iscsi_conn *conn) |
| 4568 | { |
| 4569 | int ret = 0; |
| 4570 | |
| 4571 | switch (cmd->logout_reason) { |
| 4572 | case ISCSI_LOGOUT_REASON_CLOSE_SESSION: |
| 4573 | switch (cmd->logout_response) { |
| 4574 | case ISCSI_LOGOUT_SUCCESS: |
| 4575 | case ISCSI_LOGOUT_CLEANUP_FAILED: |
| 4576 | default: |
| 4577 | iscsit_logout_post_handler_closesession(conn); |
| 4578 | break; |
| 4579 | } |
| 4580 | ret = 0; |
| 4581 | break; |
| 4582 | case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION: |
| 4583 | if (conn->cid == cmd->logout_cid) { |
| 4584 | switch (cmd->logout_response) { |
| 4585 | case ISCSI_LOGOUT_SUCCESS: |
| 4586 | case ISCSI_LOGOUT_CLEANUP_FAILED: |
| 4587 | default: |
| 4588 | iscsit_logout_post_handler_samecid(conn); |
| 4589 | break; |
| 4590 | } |
| 4591 | ret = 0; |
| 4592 | } else { |
| 4593 | switch (cmd->logout_response) { |
| 4594 | case ISCSI_LOGOUT_SUCCESS: |
| 4595 | iscsit_logout_post_handler_diffcid(conn, |
| 4596 | cmd->logout_cid); |
| 4597 | break; |
| 4598 | case ISCSI_LOGOUT_CID_NOT_FOUND: |
| 4599 | case ISCSI_LOGOUT_CLEANUP_FAILED: |
| 4600 | default: |
| 4601 | break; |
| 4602 | } |
| 4603 | ret = 1; |
| 4604 | } |
| 4605 | break; |
| 4606 | case ISCSI_LOGOUT_REASON_RECOVERY: |
| 4607 | switch (cmd->logout_response) { |
| 4608 | case ISCSI_LOGOUT_SUCCESS: |
| 4609 | case ISCSI_LOGOUT_CID_NOT_FOUND: |
| 4610 | case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED: |
| 4611 | case ISCSI_LOGOUT_CLEANUP_FAILED: |
| 4612 | default: |
| 4613 | break; |
| 4614 | } |
| 4615 | ret = 1; |
| 4616 | break; |
| 4617 | default: |
| 4618 | break; |
| 4619 | |
| 4620 | } |
| 4621 | return ret; |
| 4622 | } |
Nicholas Bellinger | 2ec5a8c | 2013-03-20 15:29:15 -0700 | [diff] [blame] | 4623 | EXPORT_SYMBOL(iscsit_logout_post_handler); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4624 | |
| 4625 | void iscsit_fail_session(struct iscsi_session *sess) |
| 4626 | { |
| 4627 | struct iscsi_conn *conn; |
| 4628 | |
| 4629 | spin_lock_bh(&sess->conn_lock); |
| 4630 | list_for_each_entry(conn, &sess->sess_conn_list, conn_list) { |
| 4631 | pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n"); |
| 4632 | conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT; |
| 4633 | } |
| 4634 | spin_unlock_bh(&sess->conn_lock); |
| 4635 | |
| 4636 | pr_debug("Moving to TARG_SESS_STATE_FAILED.\n"); |
| 4637 | sess->session_state = TARG_SESS_STATE_FAILED; |
| 4638 | } |
| 4639 | |
| 4640 | int iscsit_free_session(struct iscsi_session *sess) |
| 4641 | { |
| 4642 | u16 conn_count = atomic_read(&sess->nconn); |
| 4643 | struct iscsi_conn *conn, *conn_tmp = NULL; |
| 4644 | int is_last; |
| 4645 | |
| 4646 | spin_lock_bh(&sess->conn_lock); |
| 4647 | atomic_set(&sess->sleep_on_sess_wait_comp, 1); |
| 4648 | |
| 4649 | list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list, |
| 4650 | conn_list) { |
| 4651 | if (conn_count == 0) |
| 4652 | break; |
| 4653 | |
| 4654 | if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) { |
| 4655 | is_last = 1; |
| 4656 | } else { |
| 4657 | iscsit_inc_conn_usage_count(conn_tmp); |
| 4658 | is_last = 0; |
| 4659 | } |
| 4660 | iscsit_inc_conn_usage_count(conn); |
| 4661 | |
| 4662 | spin_unlock_bh(&sess->conn_lock); |
| 4663 | iscsit_cause_connection_reinstatement(conn, 1); |
| 4664 | spin_lock_bh(&sess->conn_lock); |
| 4665 | |
| 4666 | iscsit_dec_conn_usage_count(conn); |
| 4667 | if (is_last == 0) |
| 4668 | iscsit_dec_conn_usage_count(conn_tmp); |
| 4669 | |
| 4670 | conn_count--; |
| 4671 | } |
| 4672 | |
| 4673 | if (atomic_read(&sess->nconn)) { |
| 4674 | spin_unlock_bh(&sess->conn_lock); |
| 4675 | wait_for_completion(&sess->session_wait_comp); |
| 4676 | } else |
| 4677 | spin_unlock_bh(&sess->conn_lock); |
| 4678 | |
Nicholas Bellinger | 99367f0 | 2012-02-27 01:43:32 -0800 | [diff] [blame] | 4679 | target_put_session(sess->se_sess); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 4680 | return 0; |
| 4681 | } |
| 4682 | |
| 4683 | void iscsit_stop_session( |
| 4684 | struct iscsi_session *sess, |
| 4685 | int session_sleep, |
| 4686 | int connection_sleep) |
| 4687 | { |
| 4688 | u16 conn_count = atomic_read(&sess->nconn); |
| 4689 | struct iscsi_conn *conn, *conn_tmp = NULL; |
| 4690 | int is_last; |
| 4691 | |
| 4692 | spin_lock_bh(&sess->conn_lock); |
| 4693 | if (session_sleep) |
| 4694 | atomic_set(&sess->sleep_on_sess_wait_comp, 1); |
| 4695 | |
| 4696 | if (connection_sleep) { |
| 4697 | list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list, |
| 4698 | conn_list) { |
| 4699 | if (conn_count == 0) |
| 4700 | break; |
| 4701 | |
| 4702 | if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) { |
| 4703 | is_last = 1; |
| 4704 | } else { |
| 4705 | iscsit_inc_conn_usage_count(conn_tmp); |
| 4706 | is_last = 0; |
| 4707 | } |
| 4708 | iscsit_inc_conn_usage_count(conn); |
| 4709 | |
| 4710 | spin_unlock_bh(&sess->conn_lock); |
| 4711 | iscsit_cause_connection_reinstatement(conn, 1); |
| 4712 | spin_lock_bh(&sess->conn_lock); |
| 4713 | |
| 4714 | iscsit_dec_conn_usage_count(conn); |
| 4715 | if (is_last == 0) |
| 4716 | iscsit_dec_conn_usage_count(conn_tmp); |
| 4717 | conn_count--; |
| 4718 | } |
| 4719 | } else { |
| 4720 | list_for_each_entry(conn, &sess->sess_conn_list, conn_list) |
| 4721 | iscsit_cause_connection_reinstatement(conn, 0); |
| 4722 | } |
| 4723 | |
| 4724 | if (session_sleep && atomic_read(&sess->nconn)) { |
| 4725 | spin_unlock_bh(&sess->conn_lock); |
| 4726 | wait_for_completion(&sess->session_wait_comp); |
| 4727 | } else |
| 4728 | spin_unlock_bh(&sess->conn_lock); |
| 4729 | } |
| 4730 | |
| 4731 | int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force) |
| 4732 | { |
| 4733 | struct iscsi_session *sess; |
| 4734 | struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; |
| 4735 | struct se_session *se_sess, *se_sess_tmp; |
| 4736 | int session_count = 0; |
| 4737 | |
| 4738 | spin_lock_bh(&se_tpg->session_lock); |
| 4739 | if (tpg->nsessions && !force) { |
| 4740 | spin_unlock_bh(&se_tpg->session_lock); |
| 4741 | return -1; |
| 4742 | } |
| 4743 | |
| 4744 | list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list, |
| 4745 | sess_list) { |
| 4746 | sess = (struct iscsi_session *)se_sess->fabric_sess_ptr; |
| 4747 | |
| 4748 | spin_lock(&sess->conn_lock); |
| 4749 | if (atomic_read(&sess->session_fall_back_to_erl0) || |
| 4750 | atomic_read(&sess->session_logout) || |
| 4751 | (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) { |
| 4752 | spin_unlock(&sess->conn_lock); |
| 4753 | continue; |
| 4754 | } |
| 4755 | atomic_set(&sess->session_reinstatement, 1); |
| 4756 | spin_unlock(&sess->conn_lock); |
| 4757 | spin_unlock_bh(&se_tpg->session_lock); |
| 4758 | |
| 4759 | iscsit_free_session(sess); |
| 4760 | spin_lock_bh(&se_tpg->session_lock); |
| 4761 | |
| 4762 | session_count++; |
| 4763 | } |
| 4764 | spin_unlock_bh(&se_tpg->session_lock); |
| 4765 | |
| 4766 | pr_debug("Released %d iSCSI Session(s) from Target Portal" |
| 4767 | " Group: %hu\n", session_count, tpg->tpgt); |
| 4768 | return 0; |
| 4769 | } |
| 4770 | |
| 4771 | MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure"); |
| 4772 | MODULE_VERSION("4.1.x"); |
| 4773 | MODULE_AUTHOR("nab@Linux-iSCSI.org"); |
| 4774 | MODULE_LICENSE("GPL"); |
| 4775 | |
| 4776 | module_init(iscsi_target_init_module); |
| 4777 | module_exit(iscsi_target_cleanup_module); |