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