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