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