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