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