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