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