blob: 5001261f5d69d759dd25161401510551d35c3da7 [file] [log] [blame]
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001/*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2007-2013 Datera, Inc.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00005 *
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 Xu69110e32016-01-24 21:19:52 +080019#include <crypto/hash.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000020#include <linux/string.h>
21#include <linux/kthread.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000022#include <linux/completion.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040023#include <linux/module.h>
David S. Miller5538d292015-05-28 11:35:41 -070024#include <linux/vmalloc.h>
Al Viro40401532012-02-13 03:58:52 +000025#include <linux/idr.h>
Bart Van Assche8dcf07b2016-11-14 15:47:14 -080026#include <linux/delay.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010027#include <linux/sched/signal.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000028#include <asm/unaligned.h>
Bart Van Assche8dcf07b2016-11-14 15:47:14 -080029#include <net/ipv6.h>
Bart Van Asscheba929992015-05-08 10:11:12 +020030#include <scsi/scsi_proto.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000031#include <scsi/iscsi_proto.h>
Andy Groverd28b11692012-04-03 15:51:22 -070032#include <scsi/scsi_tcq.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000033#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050034#include <target/target_core_fabric.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000035
Sagi Grimberg67f091f2015-01-07 14:57:31 +020036#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000037#include "iscsi_target_parameters.h"
38#include "iscsi_target_seq_pdu_list.h"
Nicholas Bellingere48354c2011-07-23 06:43:04 +000039#include "iscsi_target_datain_values.h"
40#include "iscsi_target_erl0.h"
41#include "iscsi_target_erl1.h"
42#include "iscsi_target_erl2.h"
43#include "iscsi_target_login.h"
44#include "iscsi_target_tmr.h"
45#include "iscsi_target_tpg.h"
46#include "iscsi_target_util.h"
47#include "iscsi_target.h"
48#include "iscsi_target_device.h"
Sagi Grimberg67f091f2015-01-07 14:57:31 +020049#include <target/iscsi/iscsi_target_stat.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000050
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080051#include <target/iscsi/iscsi_transport.h>
52
Nicholas Bellingere48354c2011-07-23 06:43:04 +000053static LIST_HEAD(g_tiqn_list);
54static LIST_HEAD(g_np_list);
55static DEFINE_SPINLOCK(tiqn_lock);
Andy Groveree291e62014-01-24 16:18:54 -080056static DEFINE_MUTEX(np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000057
58static struct idr tiqn_idr;
59struct idr sess_idr;
60struct mutex auth_id_lock;
61spinlock_t sess_idr_lock;
62
63struct iscsit_global *iscsit_global;
64
Nicholas Bellingere48354c2011-07-23 06:43:04 +000065struct kmem_cache *lio_qr_cache;
66struct kmem_cache *lio_dr_cache;
67struct kmem_cache *lio_ooo_cache;
68struct kmem_cache *lio_r2t_cache;
69
70static int iscsit_handle_immediate_data(struct iscsi_cmd *,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -070071 struct iscsi_scsi_req *, u32);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000072
73struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
74{
75 struct iscsi_tiqn *tiqn = NULL;
76
77 spin_lock(&tiqn_lock);
78 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
79 if (!strcmp(tiqn->tiqn, buf)) {
80
81 spin_lock(&tiqn->tiqn_state_lock);
82 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
83 tiqn->tiqn_access_count++;
84 spin_unlock(&tiqn->tiqn_state_lock);
85 spin_unlock(&tiqn_lock);
86 return tiqn;
87 }
88 spin_unlock(&tiqn->tiqn_state_lock);
89 }
90 }
91 spin_unlock(&tiqn_lock);
92
93 return NULL;
94}
95
96static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
97{
98 spin_lock(&tiqn->tiqn_state_lock);
99 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
100 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
101 spin_unlock(&tiqn->tiqn_state_lock);
102 return 0;
103 }
104 spin_unlock(&tiqn->tiqn_state_lock);
105
106 return -1;
107}
108
109void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
110{
111 spin_lock(&tiqn->tiqn_state_lock);
112 tiqn->tiqn_access_count--;
113 spin_unlock(&tiqn->tiqn_state_lock);
114}
115
116/*
117 * Note that IQN formatting is expected to be done in userspace, and
118 * no explict IQN format checks are done here.
119 */
120struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
121{
122 struct iscsi_tiqn *tiqn = NULL;
123 int ret;
124
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300125 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000126 pr_err("Target IQN exceeds %d bytes\n",
127 ISCSI_IQN_LEN);
128 return ERR_PTR(-EINVAL);
129 }
130
Markus Elfring3829f382017-04-09 16:00:39 +0200131 tiqn = kzalloc(sizeof(*tiqn), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200132 if (!tiqn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000133 return ERR_PTR(-ENOMEM);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000134
135 sprintf(tiqn->tiqn, "%s", buf);
136 INIT_LIST_HEAD(&tiqn->tiqn_list);
137 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
138 spin_lock_init(&tiqn->tiqn_state_lock);
139 spin_lock_init(&tiqn->tiqn_tpg_lock);
140 spin_lock_init(&tiqn->sess_err_stats.lock);
141 spin_lock_init(&tiqn->login_stats.lock);
142 spin_lock_init(&tiqn->logout_stats.lock);
143
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000144 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
145
Tejun Heoc9365bd2013-02-27 17:04:43 -0800146 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000147 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800148
149 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000150 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800151 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000152 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800153 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000154 kfree(tiqn);
155 return ERR_PTR(ret);
156 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800157 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000158 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800159
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000160 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800161 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000162
163 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
164
165 return tiqn;
166
167}
168
169static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
170{
171 /*
172 * Wait for accesses to said struct iscsi_tiqn to end.
173 */
174 spin_lock(&tiqn->tiqn_state_lock);
175 while (tiqn->tiqn_access_count != 0) {
176 spin_unlock(&tiqn->tiqn_state_lock);
177 msleep(10);
178 spin_lock(&tiqn->tiqn_state_lock);
179 }
180 spin_unlock(&tiqn->tiqn_state_lock);
181}
182
183void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
184{
185 /*
186 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
187 * while holding tiqn->tiqn_state_lock. This means that all subsequent
188 * attempts to access this struct iscsi_tiqn will fail from both transport
189 * fabric and control code paths.
190 */
191 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
192 pr_err("iscsit_set_tiqn_shutdown() failed\n");
193 return;
194 }
195
196 iscsit_wait_for_tiqn(tiqn);
197
198 spin_lock(&tiqn_lock);
199 list_del(&tiqn->tiqn_list);
200 idr_remove(&tiqn_idr, tiqn->tiqn_index);
201 spin_unlock(&tiqn_lock);
202
203 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
204 tiqn->tiqn);
205 kfree(tiqn);
206}
207
208int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
209{
210 int ret;
211 /*
212 * Determine if the network portal is accepting storage traffic.
213 */
214 spin_lock_bh(&np->np_thread_lock);
215 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
216 spin_unlock_bh(&np->np_thread_lock);
217 return -1;
218 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000219 spin_unlock_bh(&np->np_thread_lock);
220 /*
221 * Determine if the portal group is accepting storage traffic.
222 */
223 spin_lock_bh(&tpg->tpg_state_lock);
224 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
225 spin_unlock_bh(&tpg->tpg_state_lock);
226 return -1;
227 }
228 spin_unlock_bh(&tpg->tpg_state_lock);
229
230 /*
231 * Here we serialize access across the TIQN+TPG Tuple.
232 */
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700233 ret = down_interruptible(&tpg->np_login_sem);
Nicholas Bellingeree7619f2015-05-19 15:10:44 -0700234 if (ret != 0)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000235 return -1;
236
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700237 spin_lock_bh(&tpg->tpg_state_lock);
238 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
239 spin_unlock_bh(&tpg->tpg_state_lock);
240 up(&tpg->np_login_sem);
241 return -1;
242 }
243 spin_unlock_bh(&tpg->tpg_state_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000244
245 return 0;
246}
247
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700248void iscsit_login_kref_put(struct kref *kref)
249{
250 struct iscsi_tpg_np *tpg_np = container_of(kref,
251 struct iscsi_tpg_np, tpg_np_kref);
252
253 complete(&tpg_np->tpg_np_comp);
254}
255
256int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
257 struct iscsi_tpg_np *tpg_np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000258{
259 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
260
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700261 up(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000262
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700263 if (tpg_np)
264 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000265
266 if (tiqn)
267 iscsit_put_tiqn_for_login(tiqn);
268
269 return 0;
270}
271
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800272bool iscsit_check_np_match(
Andy Grover13a3cf02015-08-24 10:26:06 -0700273 struct sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800274 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000275 int network_transport)
276{
277 struct sockaddr_in *sock_in, *sock_in_e;
278 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800279 bool ip_match = false;
Andy Grover69d75572015-08-24 10:26:04 -0700280 u16 port, port_e;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000281
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800282 if (sockaddr->ss_family == AF_INET6) {
283 sock_in6 = (struct sockaddr_in6 *)sockaddr;
284 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
285
286 if (!memcmp(&sock_in6->sin6_addr.in6_u,
287 &sock_in6_e->sin6_addr.in6_u,
288 sizeof(struct in6_addr)))
289 ip_match = true;
290
291 port = ntohs(sock_in6->sin6_port);
Andy Grover69d75572015-08-24 10:26:04 -0700292 port_e = ntohs(sock_in6_e->sin6_port);
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800293 } else {
294 sock_in = (struct sockaddr_in *)sockaddr;
295 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
296
297 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
298 ip_match = true;
299
300 port = ntohs(sock_in->sin_port);
Andy Grover69d75572015-08-24 10:26:04 -0700301 port_e = ntohs(sock_in_e->sin_port);
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800302 }
303
Andy Grover69d75572015-08-24 10:26:04 -0700304 if (ip_match && (port_e == port) &&
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800305 (np->np_network_transport == network_transport))
306 return true;
307
308 return false;
309}
310
Andy Groveree291e62014-01-24 16:18:54 -0800311/*
312 * Called with mutex np_lock held
313 */
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800314static struct iscsi_np *iscsit_get_np(
Andy Grover13a3cf02015-08-24 10:26:06 -0700315 struct sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800316 int network_transport)
317{
318 struct iscsi_np *np;
319 bool match;
320
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000321 list_for_each_entry(np, &g_np_list, np_list) {
Andy Groveree291e62014-01-24 16:18:54 -0800322 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000323 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
Andy Groveree291e62014-01-24 16:18:54 -0800324 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000325 continue;
326 }
327
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800328 match = iscsit_check_np_match(sockaddr, np, network_transport);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200329 if (match) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000330 /*
331 * Increment the np_exports reference count now to
332 * prevent iscsit_del_np() below from being called
333 * while iscsi_tpg_add_network_portal() is called.
334 */
335 np->np_exports++;
Andy Groveree291e62014-01-24 16:18:54 -0800336 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000337 return np;
338 }
Andy Groveree291e62014-01-24 16:18:54 -0800339 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000340 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000341
342 return NULL;
343}
344
345struct iscsi_np *iscsit_add_np(
Andy Grover13a3cf02015-08-24 10:26:06 -0700346 struct sockaddr_storage *sockaddr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000347 int network_transport)
348{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000349 struct iscsi_np *np;
350 int ret;
Andy Groveree291e62014-01-24 16:18:54 -0800351
352 mutex_lock(&np_lock);
353
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000354 /*
355 * Locate the existing struct iscsi_np if already active..
356 */
357 np = iscsit_get_np(sockaddr, network_transport);
Andy Groveree291e62014-01-24 16:18:54 -0800358 if (np) {
359 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000360 return np;
Andy Groveree291e62014-01-24 16:18:54 -0800361 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000362
Markus Elfring3829f382017-04-09 16:00:39 +0200363 np = kzalloc(sizeof(*np), GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000364 if (!np) {
Andy Groveree291e62014-01-24 16:18:54 -0800365 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000366 return ERR_PTR(-ENOMEM);
367 }
368
369 np->np_flags |= NPF_IP_NETWORK;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000370 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 Groveree291e62014-01-24 16:18:54 -0800378 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000379 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 Groveree291e62014-01-24 16:18:54 -0800387 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000388 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 Groveree291e62014-01-24 16:18:54 -0800398 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000399
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000400 list_add_tail(&np->np_list, &g_np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800401 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000402
Andy Grover69d75572015-08-24 10:26:04 -0700403 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
404 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000405
406 return np;
407}
408
409int iscsit_reset_np_thread(
410 struct iscsi_np *np,
411 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700412 struct iscsi_portal_group *tpg,
413 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000414{
415 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000416 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;
Nicholas Bellinger978d13d2017-08-04 23:59:31 -0700421 atomic_inc(&np->np_reset_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000422
423 if (np->np_thread) {
424 spin_unlock_bh(&np->np_thread_lock);
425 send_sig(SIGINT, np->np_thread, 1);
426 wait_for_completion(&np->np_restart_comp);
427 spin_lock_bh(&np->np_thread_lock);
428 }
429 spin_unlock_bh(&np->np_thread_lock);
430
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700431 if (tpg_np && shutdown) {
432 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
433
434 wait_for_completion(&tpg_np->tpg_np_comp);
435 }
436
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000437 return 0;
438}
439
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800440static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000441{
Al Virobf6932f2012-07-21 08:55:18 +0100442 if (np->np_socket)
443 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000444}
445
446int iscsit_del_np(struct iscsi_np *np)
447{
448 spin_lock_bh(&np->np_thread_lock);
449 np->np_exports--;
450 if (np->np_exports) {
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700451 np->enabled = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000452 spin_unlock_bh(&np->np_thread_lock);
453 return 0;
454 }
455 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
456 spin_unlock_bh(&np->np_thread_lock);
457
458 if (np->np_thread) {
459 /*
460 * We need to send the signal to wakeup Linux/Net
461 * which may be sleeping in sock_accept()..
462 */
463 send_sig(SIGINT, np->np_thread, 1);
464 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800465 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000466 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800467
468 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000469
Andy Groveree291e62014-01-24 16:18:54 -0800470 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000471 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800472 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000473
Andy Grover69d75572015-08-24 10:26:04 -0700474 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
475 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000476
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800477 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000478 kfree(np);
479 return 0;
480}
481
Varun Prakashe8205cc2016-04-20 00:00:11 +0530482static void iscsit_get_rx_pdu(struct iscsi_conn *);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700483
Varun Prakashd2faaef2016-04-20 00:00:19 +0530484int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700485{
Nicholas Bellingera4467012016-10-30 17:30:08 -0700486 return iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700487}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530488EXPORT_SYMBOL(iscsit_queue_rsp);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700489
Varun Prakashd2faaef2016-04-20 00:00:19 +0530490void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700491{
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700492 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -0700493 if (!list_empty(&cmd->i_conn_node) &&
494 !(cmd->se_cmd.transport_state & CMD_T_FABRIC_STOP))
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700495 list_del_init(&cmd->i_conn_node);
496 spin_unlock_bh(&conn->cmd_lock);
497
Bart Van Assche4412a672017-05-23 16:48:43 -0700498 __iscsit_free_cmd(cmd, true);
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700499}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530500EXPORT_SYMBOL(iscsit_aborted_task);
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700501
Varun Prakash2854bb22016-04-20 00:00:08 +0530502static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
503 u32, u32, u8 *, u8 *);
504static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
505
506static int
507iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
508 const void *data_buf, u32 data_buf_len)
509{
510 struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
511 struct kvec *iov;
512 u32 niov = 0, tx_size = ISCSI_HDR_LEN;
513 int ret;
514
515 iov = &cmd->iov_misc[0];
516 iov[niov].iov_base = cmd->pdu;
517 iov[niov++].iov_len = ISCSI_HDR_LEN;
518
519 if (conn->conn_ops->HeaderDigest) {
520 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
521
522 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
523 ISCSI_HDR_LEN, 0, NULL,
524 (u8 *)header_digest);
525
526 iov[0].iov_len += ISCSI_CRC_LEN;
527 tx_size += ISCSI_CRC_LEN;
528 pr_debug("Attaching CRC32C HeaderDigest"
529 " to opcode 0x%x 0x%08x\n",
530 hdr->opcode, *header_digest);
531 }
532
533 if (data_buf_len) {
534 u32 padding = ((-data_buf_len) & 3);
535
536 iov[niov].iov_base = (void *)data_buf;
537 iov[niov++].iov_len = data_buf_len;
538 tx_size += data_buf_len;
539
540 if (padding != 0) {
541 iov[niov].iov_base = &cmd->pad_bytes;
542 iov[niov++].iov_len = padding;
543 tx_size += padding;
544 pr_debug("Attaching %u additional"
545 " padding bytes.\n", padding);
546 }
547
548 if (conn->conn_ops->DataDigest) {
549 iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
550 data_buf, data_buf_len,
551 padding,
552 (u8 *)&cmd->pad_bytes,
553 (u8 *)&cmd->data_crc);
554
555 iov[niov].iov_base = &cmd->data_crc;
556 iov[niov++].iov_len = ISCSI_CRC_LEN;
557 tx_size += ISCSI_CRC_LEN;
558 pr_debug("Attached DataDigest for %u"
559 " bytes opcode 0x%x, CRC 0x%08x\n",
560 data_buf_len, hdr->opcode, cmd->data_crc);
561 }
562 }
563
564 cmd->iov_misc_count = niov;
565 cmd->tx_size = tx_size;
566
567 ret = iscsit_send_tx_data(cmd, conn, 1);
568 if (ret < 0) {
569 iscsit_tx_thread_wait_for_tcp(conn);
570 return ret;
571 }
572
573 return 0;
574}
575
576static int iscsit_map_iovec(struct iscsi_cmd *, struct kvec *, u32, u32);
577static void iscsit_unmap_iovec(struct iscsi_cmd *);
578static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
579 u32, u32, u32, u8 *);
580static int
581iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
582 const struct iscsi_datain *datain)
583{
584 struct kvec *iov;
585 u32 iov_count = 0, tx_size = 0;
586 int ret, iov_ret;
587
588 iov = &cmd->iov_data[0];
589 iov[iov_count].iov_base = cmd->pdu;
590 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
591 tx_size += ISCSI_HDR_LEN;
592
593 if (conn->conn_ops->HeaderDigest) {
594 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
595
596 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
597 ISCSI_HDR_LEN, 0, NULL,
598 (u8 *)header_digest);
599
600 iov[0].iov_len += ISCSI_CRC_LEN;
601 tx_size += ISCSI_CRC_LEN;
602
603 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
604 *header_digest);
605 }
606
607 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
608 datain->offset, datain->length);
609 if (iov_ret < 0)
610 return -1;
611
612 iov_count += iov_ret;
613 tx_size += datain->length;
614
615 cmd->padding = ((-datain->length) & 3);
616 if (cmd->padding) {
617 iov[iov_count].iov_base = cmd->pad_bytes;
618 iov[iov_count++].iov_len = cmd->padding;
619 tx_size += cmd->padding;
620
621 pr_debug("Attaching %u padding bytes\n", cmd->padding);
622 }
623
624 if (conn->conn_ops->DataDigest) {
625 cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
626 cmd, datain->offset,
627 datain->length,
628 cmd->padding,
629 cmd->pad_bytes);
630
631 iov[iov_count].iov_base = &cmd->data_crc;
632 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
633 tx_size += ISCSI_CRC_LEN;
634
635 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
636 datain->length + cmd->padding, cmd->data_crc);
637 }
638
639 cmd->iov_data_count = iov_count;
640 cmd->tx_size = tx_size;
641
642 ret = iscsit_fe_sendpage_sg(cmd, conn);
643
644 iscsit_unmap_iovec(cmd);
645
646 if (ret < 0) {
647 iscsit_tx_thread_wait_for_tcp(conn);
648 return ret;
649 }
650
651 return 0;
652}
653
654static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
655 struct iscsi_datain_req *dr, const void *buf,
656 u32 buf_len)
657{
658 if (dr)
659 return iscsit_xmit_datain_pdu(conn, cmd, buf);
660 else
661 return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
662}
663
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700664static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
665{
666 return TARGET_PROT_NORMAL;
667}
668
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800669static struct iscsit_transport iscsi_target_transport = {
670 .name = "iSCSI/TCP",
671 .transport_type = ISCSI_TCP,
Nicholas Bellingerbd027d82016-05-14 22:23:34 -0700672 .rdma_shutdown = false,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800673 .owner = NULL,
674 .iscsit_setup_np = iscsit_setup_np,
675 .iscsit_accept_np = iscsit_accept_np,
676 .iscsit_free_np = iscsit_free_np,
677 .iscsit_get_login_rx = iscsit_get_login_rx,
678 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800679 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700680 .iscsit_immediate_queue = iscsit_immediate_queue,
681 .iscsit_response_queue = iscsit_response_queue,
682 .iscsit_queue_data_in = iscsit_queue_rsp,
683 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700684 .iscsit_aborted_task = iscsit_aborted_task,
Varun Prakash2854bb22016-04-20 00:00:08 +0530685 .iscsit_xmit_pdu = iscsit_xmit_pdu,
Varun Prakashe8205cc2016-04-20 00:00:11 +0530686 .iscsit_get_rx_pdu = iscsit_get_rx_pdu,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700687 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800688};
689
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000690static int __init iscsi_target_init_module(void)
691{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800692 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000693
694 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
Markus Elfring3829f382017-04-09 16:00:39 +0200695 iscsit_global = kzalloc(sizeof(*iscsit_global), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200696 if (!iscsit_global)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000697 return -1;
Markus Elfringc46e22f2017-04-09 15:34:50 +0200698
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800699 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000700 mutex_init(&auth_id_lock);
701 spin_lock_init(&sess_idr_lock);
702 idr_init(&tiqn_idr);
703 idr_init(&sess_idr);
704
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200705 ret = target_register_template(&iscsi_ops);
706 if (ret)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000707 goto out;
708
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800709 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
710 iscsit_global->ts_bitmap = vzalloc(size);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200711 if (!iscsit_global->ts_bitmap)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000712 goto configfs_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000713
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000714 lio_qr_cache = kmem_cache_create("lio_qr_cache",
715 sizeof(struct iscsi_queue_req),
716 __alignof__(struct iscsi_queue_req), 0, NULL);
717 if (!lio_qr_cache) {
718 pr_err("nable to kmem_cache_create() for"
719 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800720 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000721 }
722
723 lio_dr_cache = kmem_cache_create("lio_dr_cache",
724 sizeof(struct iscsi_datain_req),
725 __alignof__(struct iscsi_datain_req), 0, NULL);
726 if (!lio_dr_cache) {
727 pr_err("Unable to kmem_cache_create() for"
728 " lio_dr_cache\n");
729 goto qr_out;
730 }
731
732 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
733 sizeof(struct iscsi_ooo_cmdsn),
734 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
735 if (!lio_ooo_cache) {
736 pr_err("Unable to kmem_cache_create() for"
737 " lio_ooo_cache\n");
738 goto dr_out;
739 }
740
741 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
742 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
743 0, NULL);
744 if (!lio_r2t_cache) {
745 pr_err("Unable to kmem_cache_create() for"
746 " lio_r2t_cache\n");
747 goto ooo_out;
748 }
749
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800750 iscsit_register_transport(&iscsi_target_transport);
751
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000752 if (iscsit_load_discovery_tpg() < 0)
753 goto r2t_out;
754
755 return ret;
756r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100757 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000758 kmem_cache_destroy(lio_r2t_cache);
759ooo_out:
760 kmem_cache_destroy(lio_ooo_cache);
761dr_out:
762 kmem_cache_destroy(lio_dr_cache);
763qr_out:
764 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800765bitmap_out:
766 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000767configfs_out:
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200768 /* XXX: this probably wants it to be it's own unwind step.. */
769 if (iscsit_global->discovery_tpg)
770 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
771 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000772out:
773 kfree(iscsit_global);
774 return -ENOMEM;
775}
776
777static void __exit iscsi_target_cleanup_module(void)
778{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000779 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800780 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000781 kmem_cache_destroy(lio_qr_cache);
782 kmem_cache_destroy(lio_dr_cache);
783 kmem_cache_destroy(lio_ooo_cache);
784 kmem_cache_destroy(lio_r2t_cache);
785
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200786 /*
787 * Shutdown discovery sessions and disable discovery TPG
788 */
789 if (iscsit_global->discovery_tpg)
790 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000791
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200792 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000793
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800794 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000795 kfree(iscsit_global);
796}
797
Varun Prakashd2faaef2016-04-20 00:00:19 +0530798int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700799 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000800 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700801 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000802{
803 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000804
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000805 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000806 if (!cmd)
807 return -1;
808
809 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700810 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000811
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100812 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000813 if (!cmd->buf_ptr) {
814 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700815 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000816 return -1;
817 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000818
819 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700820 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000821 spin_unlock_bh(&conn->cmd_lock);
822
823 cmd->i_state = ISTATE_SEND_REJECT;
824 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
825
Nicholas Bellingerba159912013-07-03 03:48:24 -0700826 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000827}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530828EXPORT_SYMBOL(iscsit_add_reject);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000829
Nicholas Bellingerba159912013-07-03 03:48:24 -0700830static int iscsit_add_reject_from_cmd(
831 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000832 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700833 bool add_to_conn,
834 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000835{
836 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000837
838 if (!cmd->conn) {
839 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
840 cmd->init_task_tag);
841 return -1;
842 }
843 conn = cmd->conn;
844
845 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700846 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000847
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100848 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000849 if (!cmd->buf_ptr) {
850 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700851 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000852 return -1;
853 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000854
855 if (add_to_conn) {
856 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700857 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000858 spin_unlock_bh(&conn->cmd_lock);
859 }
860
861 cmd->i_state = ISTATE_SEND_REJECT;
862 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800863 /*
864 * Perform the kref_put now if se_cmd has already been setup by
865 * scsit_setup_scsi_cmd()
866 */
867 if (cmd->se_cmd.se_tfo != NULL) {
868 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +0200869 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800870 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700871 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000872}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700873
874static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
875 unsigned char *buf)
876{
877 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
878}
879
880int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
881{
882 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
883}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530884EXPORT_SYMBOL(iscsit_reject_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000885
886/*
887 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700888 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000889 */
890static int iscsit_map_iovec(
891 struct iscsi_cmd *cmd,
892 struct kvec *iov,
893 u32 data_offset,
894 u32 data_length)
895{
896 u32 i = 0;
897 struct scatterlist *sg;
898 unsigned int page_off;
899
900 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700901 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000902 */
Imran Haider2b165092016-05-08 11:17:54 -0400903 u32 ent = data_offset / PAGE_SIZE;
904
905 if (ent >= cmd->se_cmd.t_data_nents) {
906 pr_err("Initial page entry out-of-bounds\n");
907 return -1;
908 }
909
910 sg = &cmd->se_cmd.t_data_sg[ent];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000911 page_off = (data_offset % PAGE_SIZE);
912
913 cmd->first_data_sg = sg;
914 cmd->first_data_sg_off = page_off;
915
916 while (data_length) {
917 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
918
919 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
920 iov[i].iov_len = cur_len;
921
922 data_length -= cur_len;
923 page_off = 0;
924 sg = sg_next(sg);
925 i++;
926 }
927
928 cmd->kmapped_nents = i;
929
930 return i;
931}
932
933static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
934{
935 u32 i;
936 struct scatterlist *sg;
937
938 sg = cmd->first_data_sg;
939
940 for (i = 0; i < cmd->kmapped_nents; i++)
941 kunmap(sg_page(&sg[i]));
942}
943
944static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
945{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700946 LIST_HEAD(ack_list);
947 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000948
949 conn->exp_statsn = exp_statsn;
950
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800951 if (conn->sess->sess_ops->RDMAExtensions)
952 return;
953
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000954 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700955 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000956 spin_lock(&cmd->istate_lock);
957 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800958 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000959 cmd->i_state = ISTATE_REMOVE;
960 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700961 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000962 continue;
963 }
964 spin_unlock(&cmd->istate_lock);
965 }
966 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700967
968 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800969 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700970 iscsit_free_cmd(cmd, false);
971 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000972}
973
974static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
975{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700976 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000977
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400978 iov_count += ISCSI_IOV_DATA_BUFFER;
Markus Elfringf1725112017-04-09 15:06:00 +0200979 cmd->iov_data = kcalloc(iov_count, sizeof(*cmd->iov_data), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200980 if (!cmd->iov_data)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000981 return -ENOMEM;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000982
983 cmd->orig_iov_data_count = iov_count;
984 return 0;
985}
986
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800987int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
988 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000989{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800990 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000991 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700992 int iscsi_task_attr;
993 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000994
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800995 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000996
997 hdr = (struct iscsi_scsi_req *) buf;
998 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000999
1000 /* FIXME; Add checks for AdditionalHeaderSegment */
1001
1002 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
1003 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1004 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1005 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001006 return iscsit_add_reject_cmd(cmd,
1007 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001008 }
1009
1010 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1011 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1012 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001013 * From RFC-3720 Section 10.3.1:
1014 *
1015 * "Either or both of R and W MAY be 1 when either the
1016 * Expected Data Transfer Length and/or Bidirectional Read
1017 * Expected Data Transfer Length are 0"
1018 *
1019 * For this case, go ahead and clear the unnecssary bits
1020 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001021 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001022 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1023 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001024
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001025 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001026 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001027 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001028 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001029
1030 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1031 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1032 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1033 " MUST be set if Expected Data Transfer Length is not 0."
1034 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001035 return iscsit_add_reject_cmd(cmd,
1036 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001037 }
1038
1039 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1040 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1041 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001042 return iscsit_add_reject_cmd(cmd,
1043 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001044 }
1045
1046 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1047 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1048 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001049 return iscsit_add_reject_cmd(cmd,
1050 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001051 }
1052
1053 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1054 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1055 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001056 return iscsit_add_reject_cmd(cmd,
1057 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001058 }
1059
Nicholas Bellingerba159912013-07-03 03:48:24 -07001060 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001061 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1062 pr_err("Expected Data Transfer Length and Length of"
1063 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1064 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001065 return iscsit_add_reject_cmd(cmd,
1066 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001067 }
1068
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001069 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001070 pr_err("DataSegmentLength: %u is greater than"
1071 " EDTL: %u, protocol error.\n", payload_length,
1072 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001073 return iscsit_add_reject_cmd(cmd,
1074 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001075 }
1076
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001077 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001078 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001079 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1080 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001081 return iscsit_add_reject_cmd(cmd,
1082 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001083 }
1084
1085 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1086 pr_err("DataSegmentLength: %u is greater than"
1087 " FirstBurstLength: %u, protocol error.\n",
1088 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001089 return iscsit_add_reject_cmd(cmd,
1090 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001091 }
1092
1093 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1094 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1095 DMA_NONE;
1096
Andy Groverd28b11692012-04-03 15:51:22 -07001097 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -07001098 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1099 /*
1100 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1101 */
1102 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1103 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001104 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001105 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001106 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001107 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001108 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001109 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001110 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001111 else {
1112 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001113 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
1114 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001115 }
1116
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001117 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
1118 cmd->i_state = ISTATE_NEW_CMD;
1119 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1120 cmd->immediate_data = (payload_length) ? 1 : 0;
1121 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1122 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1123 if (cmd->unsolicited_data)
1124 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1125
1126 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
Alexei Potashnik95473082015-07-21 15:07:56 -07001127 if (hdr->flags & ISCSI_FLAG_CMD_READ)
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02001128 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Alexei Potashnik95473082015-07-21 15:07:56 -07001129 else
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001130 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001131 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1132 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001133 cmd->first_burst_len = payload_length;
1134
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001135 if (!conn->sess->sess_ops->RDMAExtensions &&
1136 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001137 struct iscsi_datain_req *dr;
1138
1139 dr = iscsit_allocate_datain_req();
1140 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001141 return iscsit_add_reject_cmd(cmd,
1142 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001143
1144 iscsit_attach_datain_req(cmd, dr);
1145 }
1146
1147 /*
Andy Grover065ca1e2012-04-03 15:51:23 -07001148 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1149 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001150 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001151 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1152 cmd->data_direction, sam_task_attr,
1153 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -07001154
1155 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1156 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001157 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1158 conn->cid);
1159
Bart Van Asscheafc16602015-04-27 13:52:36 +02001160 target_get_sess_cmd(&cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001161
Christoph Hellwigde103c92012-11-06 12:24:09 -08001162 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1163 scsilun_to_int(&hdr->lun));
1164 if (cmd->sense_reason)
1165 goto attach_cmd;
1166
Bart Van Assche649ee052015-04-14 13:26:44 +02001167 /* only used for printks or comparing with ->ref_task_tag */
1168 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001169 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1170 if (cmd->sense_reason) {
1171 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001172 return iscsit_add_reject_cmd(cmd,
1173 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001174 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001175
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001176 goto attach_cmd;
1177 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001178
Christoph Hellwigde103c92012-11-06 12:24:09 -08001179 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001180 return iscsit_add_reject_cmd(cmd,
1181 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001182 }
1183
1184attach_cmd:
1185 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001186 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001187 spin_unlock_bh(&conn->cmd_lock);
1188 /*
1189 * Check if we need to delay processing because of ALUA
1190 * Active/NonOptimized primary access state..
1191 */
1192 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001193
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001194 return 0;
1195}
1196EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001197
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001198void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1199{
1200 iscsit_set_dataout_sequence_values(cmd);
1201
1202 spin_lock_bh(&cmd->dataout_timeout_lock);
1203 iscsit_start_dataout_timer(cmd, cmd->conn);
1204 spin_unlock_bh(&cmd->dataout_timeout_lock);
1205}
1206EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1207
1208int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1209 struct iscsi_scsi_req *hdr)
1210{
1211 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001212 /*
1213 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1214 * the Immediate Bit is not set, and no Immediate
1215 * Data is attached.
1216 *
1217 * A PDU/CmdSN carrying Immediate Data can only
1218 * be processed after the DataCRC has passed.
1219 * If the DataCRC fails, the CmdSN MUST NOT
1220 * be acknowledged. (See below)
1221 */
1222 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001223 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1224 (unsigned char *)hdr, hdr->cmdsn);
1225 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1226 return -1;
1227 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001228 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001229 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001230 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001231 }
1232
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001233 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001234
1235 /*
1236 * If no Immediate Data is attached, it's OK to return now.
1237 */
1238 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001239 if (!cmd->sense_reason && cmd->unsolicited_data)
1240 iscsit_set_unsoliticed_dataout(cmd);
1241 if (!cmd->sense_reason)
1242 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001243
Bart Van Asscheafc16602015-04-27 13:52:36 +02001244 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001245 return 0;
1246 }
1247
1248 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001249 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1250 * execution. These exceptions are processed in CmdSN order using
1251 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001252 */
Bart Van Assche8fa40112017-05-23 16:48:45 -07001253 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001254 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001255 /*
1256 * Call directly into transport_generic_new_cmd() to perform
1257 * the backend memory allocation.
1258 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001259 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001260 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001261 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001262
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001263 return 0;
1264}
1265EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1266
1267static int
1268iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1269 bool dump_payload)
1270{
1271 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1272 /*
1273 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1274 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001275 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001276 goto after_immediate_data;
Nicholas Bellingerabb85a92017-06-07 20:29:50 -07001277 /*
1278 * Check for underflow case where both EDTL and immediate data payload
1279 * exceeds what is presented by CDB's TRANSFER LENGTH, and what has
1280 * already been set in target_cmd_size_check() as se_cmd->data_length.
1281 *
1282 * For this special case, fail the command and dump the immediate data
1283 * payload.
1284 */
1285 if (cmd->first_burst_len > cmd->se_cmd.data_length) {
1286 cmd->sense_reason = TCM_INVALID_CDB_FIELD;
1287 goto after_immediate_data;
1288 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001289
1290 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1291 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001292after_immediate_data:
1293 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1294 /*
1295 * A PDU/CmdSN carrying Immediate Data passed
1296 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1297 * Immediate Bit is not set.
1298 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001299 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1300 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001301 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001302 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001303
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001304 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001305 int rc;
1306
1307 rc = iscsit_dump_data_payload(cmd->conn,
1308 cmd->first_burst_len, 1);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001309 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001310 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001311 } else if (cmd->unsolicited_data)
1312 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001313
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001314 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1315 /*
1316 * Immediate Data failed DataCRC and ERL>=1,
1317 * silently drop this PDU and let the initiator
1318 * plug the CmdSN gap.
1319 *
1320 * FIXME: Send Unsolicited NOPIN with reserved
1321 * TTT here to help the initiator figure out
1322 * the missing CmdSN, although they should be
1323 * intelligent enough to determine the missing
1324 * CmdSN and issue a retry to plug the sequence.
1325 */
1326 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001327 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001328 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1329 return -1;
1330
1331 return 0;
1332}
1333
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001334static int
1335iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1336 unsigned char *buf)
1337{
1338 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1339 int rc, immed_data;
1340 bool dump_payload = false;
1341
1342 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1343 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001344 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001345 /*
1346 * Allocation iovecs needed for struct socket operations for
1347 * traditional iSCSI block I/O.
1348 */
1349 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001350 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001351 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001352 }
1353 immed_data = cmd->immediate_data;
1354
1355 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1356 if (rc < 0)
1357 return rc;
1358 else if (rc > 0)
1359 dump_payload = true;
1360
1361 if (!immed_data)
1362 return 0;
1363
1364 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1365}
1366
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001367static u32 iscsit_do_crypto_hash_sg(
Herbert Xu69110e32016-01-24 21:19:52 +08001368 struct ahash_request *hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001369 struct iscsi_cmd *cmd,
1370 u32 data_offset,
1371 u32 data_length,
1372 u32 padding,
1373 u8 *pad_bytes)
1374{
1375 u32 data_crc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001376 struct scatterlist *sg;
1377 unsigned int page_off;
1378
Herbert Xu69110e32016-01-24 21:19:52 +08001379 crypto_ahash_init(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001380
1381 sg = cmd->first_data_sg;
1382 page_off = cmd->first_data_sg_off;
1383
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001384 while (data_length) {
Alexei Potashnikaa756792015-07-20 17:12:12 -07001385 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001386
Herbert Xu69110e32016-01-24 21:19:52 +08001387 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1388 crypto_ahash_update(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001389
1390 data_length -= cur_len;
1391 page_off = 0;
Alexei Potashnikaa756792015-07-20 17:12:12 -07001392 /* iscsit_map_iovec has already checked for invalid sg pointers */
1393 sg = sg_next(sg);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001394 }
1395
1396 if (padding) {
1397 struct scatterlist pad_sg;
1398
1399 sg_init_one(&pad_sg, pad_bytes, padding);
Herbert Xu69110e32016-01-24 21:19:52 +08001400 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1401 padding);
1402 crypto_ahash_finup(hash);
1403 } else {
1404 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1405 crypto_ahash_final(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001406 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001407
1408 return data_crc;
1409}
1410
1411static void iscsit_do_crypto_hash_buf(
Herbert Xu69110e32016-01-24 21:19:52 +08001412 struct ahash_request *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001413 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001414 u32 payload_length,
1415 u32 padding,
1416 u8 *pad_bytes,
1417 u8 *data_crc)
1418{
Herbert Xu69110e32016-01-24 21:19:52 +08001419 struct scatterlist sg[2];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001420
Herbert Xu69110e32016-01-24 21:19:52 +08001421 sg_init_table(sg, ARRAY_SIZE(sg));
1422 sg_set_buf(sg, buf, payload_length);
1423 sg_set_buf(sg + 1, pad_bytes, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001424
Herbert Xu69110e32016-01-24 21:19:52 +08001425 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001426
Herbert Xu69110e32016-01-24 21:19:52 +08001427 crypto_ahash_digest(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001428}
1429
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001430int
Varun Prakash9a584bf2017-01-13 20:53:21 +05301431__iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1432 struct iscsi_cmd *cmd, u32 payload_length,
1433 bool *success)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001434{
Varun Prakash9a584bf2017-01-13 20:53:21 +05301435 struct iscsi_data *hdr = buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001436 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001437 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001438
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001439 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001440 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001441
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001442 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1443 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001444 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001445 payload_length, conn->cid);
1446
1447 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1448 pr_err("Command ITT: 0x%08x received DataOUT after"
1449 " last DataOUT received, dumping payload\n",
1450 cmd->init_task_tag);
1451 return iscsit_dump_data_payload(conn, payload_length, 1);
1452 }
1453
1454 if (cmd->data_direction != DMA_TO_DEVICE) {
1455 pr_err("Command ITT: 0x%08x received DataOUT for a"
1456 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001457 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001458 }
1459 se_cmd = &cmd->se_cmd;
1460 iscsit_mod_dataout_timer(cmd);
1461
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001462 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001463 pr_err("DataOut Offset: %u, Length %u greater than"
1464 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001465 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001466 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001467 }
1468
1469 if (cmd->unsolicited_data) {
1470 int dump_unsolicited_data = 0;
1471
1472 if (conn->sess->sess_ops->InitialR2T) {
1473 pr_err("Received unexpected unsolicited data"
1474 " while InitialR2T=Yes, protocol error.\n");
1475 transport_send_check_condition_and_sense(&cmd->se_cmd,
1476 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1477 return -1;
1478 }
1479 /*
1480 * Special case for dealing with Unsolicited DataOUT
1481 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1482 * failures;
1483 */
1484
1485 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001486 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001487 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001488 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001489
1490 if (dump_unsolicited_data) {
1491 /*
1492 * Check if a delayed TASK_ABORTED status needs to
1493 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
Bart Van Assche5a342522015-10-22 15:53:22 -07001494 * received with the unsolicited data out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001495 */
1496 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1497 iscsit_stop_dataout_timer(cmd);
1498
1499 transport_check_aborted_status(se_cmd,
1500 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1501 return iscsit_dump_data_payload(conn, payload_length, 1);
1502 }
1503 } else {
1504 /*
1505 * For the normal solicited data path:
1506 *
1507 * Check for a delayed TASK_ABORTED status and dump any
1508 * incoming data out payload if one exists. Also, when the
1509 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1510 * data out sequence, we decrement outstanding_r2ts. Once
1511 * outstanding_r2ts reaches zero, go ahead and send the delayed
1512 * TASK_ABORTED status.
1513 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001514 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001515 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1516 if (--cmd->outstanding_r2ts < 1) {
1517 iscsit_stop_dataout_timer(cmd);
1518 transport_check_aborted_status(
1519 se_cmd, 1);
1520 }
1521
1522 return iscsit_dump_data_payload(conn, payload_length, 1);
1523 }
1524 }
1525 /*
Bart Van Assche0d5efb82016-12-23 14:37:52 +01001526 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001527 * within-command recovery checks before receiving the payload.
1528 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001529 rc = iscsit_check_pre_dataout(cmd, buf);
1530 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001531 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001532 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001533 return -1;
Varun Prakash9a584bf2017-01-13 20:53:21 +05301534 *success = true;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001535 return 0;
1536}
Varun Prakash9a584bf2017-01-13 20:53:21 +05301537EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
1538
1539int
1540iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1541 struct iscsi_cmd **out_cmd)
1542{
1543 struct iscsi_data *hdr = buf;
1544 struct iscsi_cmd *cmd;
1545 u32 payload_length = ntoh24(hdr->dlength);
1546 int rc;
1547 bool success = false;
1548
1549 if (!payload_length) {
1550 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1551 return 0;
1552 }
1553
1554 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1555 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1556 " MaxXmitDataSegmentLength: %u\n", payload_length,
1557 conn->conn_ops->MaxXmitDataSegmentLength);
1558 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
1559 }
1560
1561 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
1562 if (!cmd)
1563 return 0;
1564
1565 rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
1566
1567 if (success)
1568 *out_cmd = cmd;
1569
1570 return rc;
1571}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001572EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1573
1574static int
1575iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1576 struct iscsi_data *hdr)
1577{
1578 struct kvec *iov;
1579 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1580 u32 payload_length = ntoh24(hdr->dlength);
1581 int iov_ret, data_crc_failed = 0;
1582
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001583 rx_size += payload_length;
1584 iov = &cmd->iov_data[0];
1585
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001586 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1587 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001588 if (iov_ret < 0)
1589 return -1;
1590
1591 iov_count += iov_ret;
1592
1593 padding = ((-payload_length) & 3);
1594 if (padding != 0) {
1595 iov[iov_count].iov_base = cmd->pad_bytes;
1596 iov[iov_count++].iov_len = padding;
1597 rx_size += padding;
1598 pr_debug("Receiving %u padding bytes.\n", padding);
1599 }
1600
1601 if (conn->conn_ops->DataDigest) {
1602 iov[iov_count].iov_base = &checksum;
1603 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1604 rx_size += ISCSI_CRC_LEN;
1605 }
1606
1607 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1608
1609 iscsit_unmap_iovec(cmd);
1610
1611 if (rx_got != rx_size)
1612 return -1;
1613
1614 if (conn->conn_ops->DataDigest) {
1615 u32 data_crc;
1616
Herbert Xu69110e32016-01-24 21:19:52 +08001617 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001618 be32_to_cpu(hdr->offset),
1619 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001620 cmd->pad_bytes);
1621
1622 if (checksum != data_crc) {
1623 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1624 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1625 " does not match computed 0x%08x\n",
1626 hdr->itt, hdr->offset, payload_length,
1627 hdr->datasn, checksum, data_crc);
1628 data_crc_failed = 1;
1629 } else {
1630 pr_debug("Got CRC32C DataDigest 0x%08x for"
1631 " %u bytes of Data Out\n", checksum,
1632 payload_length);
1633 }
1634 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001635
1636 return data_crc_failed;
1637}
1638
1639int
1640iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1641 bool data_crc_failed)
1642{
1643 struct iscsi_conn *conn = cmd->conn;
1644 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001645 /*
1646 * Increment post receive data and CRC values or perform
1647 * within-command recovery.
1648 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001649 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1650 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001651 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001652 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001653 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001654 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1655 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001656 /*
1657 * Handle extra special case for out of order
1658 * Unsolicited Data Out.
1659 */
1660 spin_lock_bh(&cmd->istate_lock);
1661 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1662 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1663 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1664 spin_unlock_bh(&cmd->istate_lock);
1665
1666 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001667 if (ooo_cmdsn)
1668 return 0;
1669 target_execute_cmd(&cmd->se_cmd);
1670 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001671 } else /* DATAOUT_CANNOT_RECOVER */
1672 return -1;
1673
1674 return 0;
1675}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001676EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001677
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001678static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1679{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001680 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001681 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1682 int rc;
1683 bool data_crc_failed = false;
1684
1685 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1686 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001687 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001688 else if (!cmd)
1689 return 0;
1690
1691 rc = iscsit_get_dataout(conn, cmd, hdr);
1692 if (rc < 0)
1693 return rc;
1694 else if (rc > 0)
1695 data_crc_failed = true;
1696
1697 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1698}
1699
Nicholas Bellinger778de362013-06-14 16:07:47 -07001700int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1701 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001702{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001703 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001704
Arshad Hussaina3662602014-03-14 15:28:59 -07001705 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1706 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1707 if (!cmd)
1708 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1709 (unsigned char *)hdr);
1710
1711 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1712 (unsigned char *)hdr);
1713 }
1714
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001715 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001716 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1717 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001718 if (!cmd)
1719 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1720 (unsigned char *)hdr);
1721
Nicholas Bellingerba159912013-07-03 03:48:24 -07001722 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1723 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001724 }
1725
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001726 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001727 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001728 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001729 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001730 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001731 if (!cmd)
1732 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1733 (unsigned char *)hdr);
1734
Nicholas Bellingerba159912013-07-03 03:48:24 -07001735 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1736 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001737 }
1738
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001739 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001740 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001741 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001742 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1743 payload_length);
1744 /*
1745 * This is not a response to a Unsolicited NopIN, which means
1746 * it can either be a NOPOUT ping request (with a valid ITT),
1747 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1748 * Either way, make sure we allocate an struct iscsi_cmd, as both
1749 * can contain ping data.
1750 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001751 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001752 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1753 cmd->i_state = ISTATE_SEND_NOPIN;
1754 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1755 1 : 0);
1756 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1757 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001758 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1759 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001760 cmd->data_direction = DMA_NONE;
1761 }
1762
Nicholas Bellinger778de362013-06-14 16:07:47 -07001763 return 0;
1764}
1765EXPORT_SYMBOL(iscsit_setup_nop_out);
1766
1767int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1768 struct iscsi_nopout *hdr)
1769{
1770 struct iscsi_cmd *cmd_p = NULL;
1771 int cmdsn_ret = 0;
1772 /*
1773 * Initiator is expecting a NopIN ping reply..
1774 */
1775 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001776 if (!cmd)
1777 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1778 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001779
1780 spin_lock_bh(&conn->cmd_lock);
1781 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1782 spin_unlock_bh(&conn->cmd_lock);
1783
1784 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1785
1786 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1787 iscsit_add_cmd_to_response_queue(cmd, conn,
1788 cmd->i_state);
1789 return 0;
1790 }
1791
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001792 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1793 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001794 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1795 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001796 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001797 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001798
1799 return 0;
1800 }
1801 /*
1802 * This was a response to a unsolicited NOPIN ping.
1803 */
1804 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1805 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1806 if (!cmd_p)
1807 return -EINVAL;
1808
1809 iscsit_stop_nopin_response_timer(conn);
1810
1811 cmd_p->i_state = ISTATE_REMOVE;
1812 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1813
1814 iscsit_start_nopin_timer(conn);
1815 return 0;
1816 }
1817 /*
1818 * Otherwise, initiator is not expecting a NOPIN is response.
1819 * Just ignore for now.
1820 */
Varun Prakash1a40f0a2016-09-15 21:20:11 +05301821
1822 if (cmd)
1823 iscsit_free_cmd(cmd, false);
1824
Nicholas Bellinger778de362013-06-14 16:07:47 -07001825 return 0;
1826}
1827EXPORT_SYMBOL(iscsit_process_nop_out);
1828
1829static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1830 unsigned char *buf)
1831{
1832 unsigned char *ping_data = NULL;
1833 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1834 struct kvec *iov = NULL;
1835 u32 payload_length = ntoh24(hdr->dlength);
1836 int ret;
1837
1838 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1839 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001840 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001841 /*
1842 * Handle NOP-OUT payload for traditional iSCSI sockets
1843 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001844 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001845 u32 checksum, data_crc, padding = 0;
1846 int niov = 0, rx_got, rx_size = payload_length;
1847
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001848 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1849 if (!ping_data) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001850 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 Xu69110e32016-01-24 21:19:52 +08001879 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001880 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 Engel8359cf42011-11-24 02:05:51 +01001916 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001917 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 Bellinger778de362013-06-14 16:07:47 -07001924 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001925out:
1926 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001927 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001928
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001929 kfree(ping_data);
1930 return ret;
1931}
1932
Bart Van Asschee381fe92017-01-06 11:32:08 +01001933static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
1934{
1935 switch (iscsi_tmf) {
1936 case ISCSI_TM_FUNC_ABORT_TASK:
1937 return TMR_ABORT_TASK;
1938 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1939 return TMR_ABORT_TASK_SET;
1940 case ISCSI_TM_FUNC_CLEAR_ACA:
1941 return TMR_CLEAR_ACA;
1942 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1943 return TMR_CLEAR_TASK_SET;
1944 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1945 return TMR_LUN_RESET;
1946 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1947 return TMR_TARGET_WARM_RESET;
1948 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1949 return TMR_TARGET_COLD_RESET;
1950 default:
1951 return TMR_UNKNOWN;
1952 }
1953}
1954
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001955int
1956iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1957 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001958{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001959 struct se_tmr_req *se_tmr;
1960 struct iscsi_tmr_req *tmr_req;
1961 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001962 int out_of_order_cmdsn = 0, ret;
1963 bool sess_ref = false;
Bart Van Assche59b69862017-01-05 12:39:57 +01001964 u8 function, tcm_function = TMR_UNKNOWN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001965
1966 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001967 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1968 function = hdr->flags;
1969
1970 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1971 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1972 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1973 hdr->rtt, hdr->refcmdsn, conn->cid);
1974
1975 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1976 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001977 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001978 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001979 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001980 }
1981
1982 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1983 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1984 pr_err("Task Management Request TASK_REASSIGN not"
1985 " issued as immediate command, bad iSCSI Initiator"
1986 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001987 return iscsit_add_reject_cmd(cmd,
1988 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001989 }
1990 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001991 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1992 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001993
Andy Groverd28b11692012-04-03 15:51:22 -07001994 cmd->data_direction = DMA_NONE;
Markus Elfring3829f382017-04-09 16:00:39 +02001995 cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02001996 if (!cmd->tmr_req)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001997 return iscsit_add_reject_cmd(cmd,
1998 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1999 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002000
2001 /*
2002 * TASK_REASSIGN for ERL=2 / connection stays inside of
2003 * LIO-Target $FABRIC_MOD
2004 */
2005 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002006 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Andy Groverd28b11692012-04-03 15:51:22 -07002007 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08002008 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07002009
Bart Van Asscheafc16602015-04-27 13:52:36 +02002010 target_get_sess_cmd(&cmd->se_cmd, true);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002011 sess_ref = true;
Bart Van Asschee381fe92017-01-06 11:32:08 +01002012 tcm_function = iscsit_convert_tmf(function);
2013 if (tcm_function == TMR_UNKNOWN) {
Andy Groverd28b11692012-04-03 15:51:22 -07002014 pr_err("Unknown iSCSI TMR Function:"
2015 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002016 return iscsit_add_reject_cmd(cmd,
2017 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002018 }
Bart Van Assche59b69862017-01-05 12:39:57 +01002019 }
2020 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
2021 GFP_KERNEL);
2022 if (ret < 0)
2023 return iscsit_add_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002024 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002025
Bart Van Assche59b69862017-01-05 12:39:57 +01002026 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002027
2028 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2029 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2030 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2031 cmd->init_task_tag = hdr->itt;
2032 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002033 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2034 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002035 se_tmr = cmd->se_cmd.se_tmr_req;
2036 tmr_req = cmd->tmr_req;
2037 /*
2038 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2039 */
2040 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08002041 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
2042 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002043 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002044 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2045 goto attach;
2046 }
2047 }
2048
2049 switch (function) {
2050 case ISCSI_TM_FUNC_ABORT_TASK:
2051 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002052 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002053 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002054 break;
2055 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2056 case ISCSI_TM_FUNC_CLEAR_ACA:
2057 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2058 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2059 break;
2060 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2061 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002062 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2063 goto attach;
2064 }
2065 break;
2066 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2067 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002068 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2069 goto attach;
2070 }
2071 break;
2072 case ISCSI_TM_FUNC_TASK_REASSIGN:
2073 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2074 /*
2075 * Perform sanity checks on the ExpDataSN only if the
2076 * TASK_REASSIGN was successful.
2077 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002078 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002079 break;
2080
2081 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002082 return iscsit_add_reject_cmd(cmd,
2083 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002084 break;
2085 default:
2086 pr_err("Unknown TMR function: 0x%02x, protocol"
2087 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002088 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2089 goto attach;
2090 }
2091
2092 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2093 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2094 se_tmr->call_transport = 1;
2095attach:
2096 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002097 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002098 spin_unlock_bh(&conn->cmd_lock);
2099
2100 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002101 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002102 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
2103 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002104 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002105 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002106 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002107 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002108 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002109 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002110
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002111 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002112 return 0;
2113 /*
2114 * Found the referenced task, send to transport for processing.
2115 */
2116 if (se_tmr->call_transport)
2117 return transport_generic_handle_tmr(&cmd->se_cmd);
2118
2119 /*
2120 * Could not find the referenced LUN, task, or Task Management
2121 * command not authorized or supported. Change state and
2122 * let the tx_thread send the response.
2123 *
2124 * For connection recovery, this is also the default action for
2125 * TMR TASK_REASSIGN.
2126 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002127 if (sess_ref) {
2128 pr_debug("Handle TMR, using sess_ref=true check\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +02002129 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002130 }
2131
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002132 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2133 return 0;
2134}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002135EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002136
2137/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002138int
2139iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2140 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002141{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002142 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002143
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002144 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002145 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002146 "greater than MaxXmitDataSegmentLength %u.\n",
2147 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002148 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2149 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002150 }
2151
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08002152 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2153 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2154 pr_err("Multi sequence text commands currently not supported\n");
2155 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2156 (unsigned char *)hdr);
2157 }
2158
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002159 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2160 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2161 hdr->exp_statsn, payload_length);
2162
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002163 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2164 cmd->i_state = ISTATE_SEND_TEXTRSP;
2165 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2166 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2167 cmd->targ_xfer_tag = 0xFFFFFFFF;
2168 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2169 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2170 cmd->data_direction = DMA_NONE;
Varun Prakashea8dc5b2017-07-23 20:03:33 +05302171 kfree(cmd->text_in_ptr);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002172 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002173
2174 return 0;
2175}
2176EXPORT_SYMBOL(iscsit_setup_text_cmd);
2177
2178int
2179iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2180 struct iscsi_text *hdr)
2181{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002182 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002183 int cmdsn_ret;
2184
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002185 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002186 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2187 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2188 pr_err("Unable to locate text_in buffer for sendtargets"
2189 " discovery\n");
2190 goto reject;
2191 }
2192 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002193 }
2194 if (strncmp("SendTargets", text_in, 11) != 0) {
2195 pr_err("Received Text Data that is not"
2196 " SendTargets, cannot continue.\n");
2197 goto reject;
2198 }
2199 text_ptr = strchr(text_in, '=');
2200 if (!text_ptr) {
2201 pr_err("No \"=\" separator found in Text Data,"
2202 " cannot continue.\n");
2203 goto reject;
2204 }
2205 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002206 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002207 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2208 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002209 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002210 } else {
2211 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2212 goto reject;
2213 }
2214
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002215 spin_lock_bh(&conn->cmd_lock);
2216 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2217 spin_unlock_bh(&conn->cmd_lock);
2218
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002219empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002220 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2221
2222 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002223 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2224 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002225 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002226 return -1;
2227
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002228 return 0;
2229 }
2230
2231 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002232
2233reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002234 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2235 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002236}
2237EXPORT_SYMBOL(iscsit_process_text_cmd);
2238
2239static int
2240iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2241 unsigned char *buf)
2242{
2243 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2244 char *text_in = NULL;
2245 u32 payload_length = ntoh24(hdr->dlength);
2246 int rx_size, rc;
2247
2248 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2249 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002250 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002251
2252 rx_size = payload_length;
2253 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002254 u32 checksum = 0, data_crc = 0;
2255 u32 padding = 0, pad_bytes = 0;
2256 int niov = 0, rx_got;
2257 struct kvec iov[3];
2258
2259 text_in = kzalloc(payload_length, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02002260 if (!text_in)
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002261 goto reject;
Markus Elfringc46e22f2017-04-09 15:34:50 +02002262
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002263 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002264
2265 memset(iov, 0, 3 * sizeof(struct kvec));
2266 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002267 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002268
2269 padding = ((-payload_length) & 3);
2270 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002271 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002272 iov[niov++].iov_len = padding;
2273 rx_size += padding;
2274 pr_debug("Receiving %u additional bytes"
2275 " for padding.\n", padding);
2276 }
2277 if (conn->conn_ops->DataDigest) {
2278 iov[niov].iov_base = &checksum;
2279 iov[niov++].iov_len = ISCSI_CRC_LEN;
2280 rx_size += ISCSI_CRC_LEN;
2281 }
2282
2283 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002284 if (rx_got != rx_size)
2285 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002286
2287 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08002288 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002289 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002290 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002291 (u8 *)&data_crc);
2292
2293 if (checksum != data_crc) {
2294 pr_err("Text data CRC32C DataDigest"
2295 " 0x%08x does not match computed"
2296 " 0x%08x\n", checksum, data_crc);
2297 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2298 pr_err("Unable to recover from"
2299 " Text Data digest failure while in"
2300 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002301 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002302 } else {
2303 /*
2304 * Silently drop this PDU and let the
2305 * initiator plug the CmdSN gap.
2306 */
2307 pr_debug("Dropping Text"
2308 " Command CmdSN: 0x%08x due to"
2309 " DataCRC error.\n", hdr->cmdsn);
2310 kfree(text_in);
2311 return 0;
2312 }
2313 } else {
2314 pr_debug("Got CRC32C DataDigest"
2315 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002316 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002317 }
2318 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002319 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002320 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002321 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002322 }
2323
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002324 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002325
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002326reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002327 kfree(cmd->text_in_ptr);
2328 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002329 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002330}
2331
2332int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2333{
2334 struct iscsi_conn *conn_p;
2335 struct iscsi_session *sess = conn->sess;
2336
2337 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2338 " for SID: %u.\n", conn->cid, conn->sess->sid);
2339
2340 atomic_set(&sess->session_logout, 1);
2341 atomic_set(&conn->conn_logout_remove, 1);
2342 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2343
2344 iscsit_inc_conn_usage_count(conn);
2345 iscsit_inc_session_usage_count(sess);
2346
2347 spin_lock_bh(&sess->conn_lock);
2348 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2349 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2350 continue;
2351
2352 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2353 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2354 }
2355 spin_unlock_bh(&sess->conn_lock);
2356
2357 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2358
2359 return 0;
2360}
2361
2362int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2363{
2364 struct iscsi_conn *l_conn;
2365 struct iscsi_session *sess = conn->sess;
2366
2367 pr_debug("Received logout request CLOSECONNECTION for CID:"
2368 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2369
2370 /*
2371 * A Logout Request with a CLOSECONNECTION reason code for a CID
2372 * can arrive on a connection with a differing CID.
2373 */
2374 if (conn->cid == cmd->logout_cid) {
2375 spin_lock_bh(&conn->state_lock);
2376 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2377 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2378
2379 atomic_set(&conn->conn_logout_remove, 1);
2380 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2381 iscsit_inc_conn_usage_count(conn);
2382
2383 spin_unlock_bh(&conn->state_lock);
2384 } else {
2385 /*
2386 * Handle all different cid CLOSECONNECTION requests in
2387 * iscsit_logout_post_handler_diffcid() as to give enough
2388 * time for any non immediate command's CmdSN to be
2389 * acknowledged on the connection in question.
2390 *
2391 * Here we simply make sure the CID is still around.
2392 */
2393 l_conn = iscsit_get_conn_from_cid(sess,
2394 cmd->logout_cid);
2395 if (!l_conn) {
2396 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2397 iscsit_add_cmd_to_response_queue(cmd, conn,
2398 cmd->i_state);
2399 return 0;
2400 }
2401
2402 iscsit_dec_conn_usage_count(l_conn);
2403 }
2404
2405 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2406
2407 return 0;
2408}
2409
2410int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2411{
2412 struct iscsi_session *sess = conn->sess;
2413
2414 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2415 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2416
2417 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2418 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2419 " while ERL!=2.\n");
2420 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2421 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2422 return 0;
2423 }
2424
2425 if (conn->cid == cmd->logout_cid) {
2426 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2427 " with CID: %hu on CID: %hu, implementation error.\n",
2428 cmd->logout_cid, conn->cid);
2429 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2430 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2431 return 0;
2432 }
2433
2434 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2435
2436 return 0;
2437}
2438
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002439int
2440iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2441 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002442{
2443 int cmdsn_ret, logout_remove = 0;
2444 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002445 struct iscsi_logout *hdr;
2446 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2447
2448 hdr = (struct iscsi_logout *) buf;
2449 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002450
2451 if (tiqn) {
2452 spin_lock(&tiqn->logout_stats.lock);
2453 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2454 tiqn->logout_stats.normal_logouts++;
2455 else
2456 tiqn->logout_stats.abnormal_logouts++;
2457 spin_unlock(&tiqn->logout_stats.lock);
2458 }
2459
2460 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2461 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2462 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2463 hdr->cid, conn->cid);
2464
2465 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2466 pr_err("Received logout request on connection that"
2467 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002468 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002469 return 0;
2470 }
2471
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002472 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2473 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2474 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2475 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2476 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002477 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2478 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2479 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002480 cmd->logout_reason = reason_code;
2481 cmd->data_direction = DMA_NONE;
2482
2483 /*
2484 * We need to sleep in these cases (by returning 1) until the Logout
2485 * Response gets sent in the tx thread.
2486 */
2487 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2488 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002489 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002490 logout_remove = 1;
2491
2492 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002493 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002494 spin_unlock_bh(&conn->cmd_lock);
2495
2496 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002497 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002498
2499 /*
2500 * Immediate commands are executed, well, immediately.
2501 * Non-Immediate Logout Commands are executed in CmdSN order.
2502 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002503 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002504 int ret = iscsit_execute_cmd(cmd, 0);
2505
2506 if (ret < 0)
2507 return ret;
2508 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002509 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002510 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002511 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002512 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2513 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002514 }
2515
2516 return logout_remove;
2517}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002518EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002519
Varun Prakashd2faaef2016-04-20 00:00:19 +05302520int iscsit_handle_snack(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002521 struct iscsi_conn *conn,
2522 unsigned char *buf)
2523{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002524 struct iscsi_snack *hdr;
2525
2526 hdr = (struct iscsi_snack *) buf;
2527 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002528
2529 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2530 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2531 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2532 hdr->begrun, hdr->runlength, conn->cid);
2533
2534 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2535 pr_err("Initiator sent SNACK request while in"
2536 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002537 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2538 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002539 }
2540 /*
2541 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2542 * call from inside iscsi_send_recovery_datain_or_r2t().
2543 */
2544 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2545 case 0:
2546 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002547 hdr->itt,
2548 be32_to_cpu(hdr->ttt),
2549 be32_to_cpu(hdr->begrun),
2550 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002551 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002552 return iscsit_handle_status_snack(conn, hdr->itt,
2553 be32_to_cpu(hdr->ttt),
2554 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002555 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002556 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2557 be32_to_cpu(hdr->begrun),
2558 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002559 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2560 /* FIXME: Support R-Data SNACK */
2561 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002562 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2563 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002564 default:
2565 pr_err("Unknown SNACK type 0x%02x, protocol"
2566 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002567 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2568 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002569 }
2570
2571 return 0;
2572}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302573EXPORT_SYMBOL(iscsit_handle_snack);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002574
2575static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2576{
2577 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2578 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2579 wait_for_completion_interruptible_timeout(
2580 &conn->rx_half_close_comp,
2581 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2582 }
2583}
2584
2585static int iscsit_handle_immediate_data(
2586 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002587 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002588 u32 length)
2589{
2590 int iov_ret, rx_got = 0, rx_size = 0;
2591 u32 checksum, iov_count = 0, padding = 0;
2592 struct iscsi_conn *conn = cmd->conn;
2593 struct kvec *iov;
2594
2595 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2596 if (iov_ret < 0)
2597 return IMMEDIATE_DATA_CANNOT_RECOVER;
2598
2599 rx_size = length;
2600 iov_count = iov_ret;
2601 iov = &cmd->iov_data[0];
2602
2603 padding = ((-length) & 3);
2604 if (padding != 0) {
2605 iov[iov_count].iov_base = cmd->pad_bytes;
2606 iov[iov_count++].iov_len = padding;
2607 rx_size += padding;
2608 }
2609
2610 if (conn->conn_ops->DataDigest) {
2611 iov[iov_count].iov_base = &checksum;
2612 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2613 rx_size += ISCSI_CRC_LEN;
2614 }
2615
2616 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2617
2618 iscsit_unmap_iovec(cmd);
2619
2620 if (rx_got != rx_size) {
2621 iscsit_rx_thread_wait_for_tcp(conn);
2622 return IMMEDIATE_DATA_CANNOT_RECOVER;
2623 }
2624
2625 if (conn->conn_ops->DataDigest) {
2626 u32 data_crc;
2627
Herbert Xu69110e32016-01-24 21:19:52 +08002628 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002629 cmd->write_data_done, length, padding,
2630 cmd->pad_bytes);
2631
2632 if (checksum != data_crc) {
2633 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2634 " does not match computed 0x%08x\n", checksum,
2635 data_crc);
2636
2637 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2638 pr_err("Unable to recover from"
2639 " Immediate Data digest failure while"
2640 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002641 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002642 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002643 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002644 return IMMEDIATE_DATA_CANNOT_RECOVER;
2645 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002646 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002647 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002648 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002649 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2650 }
2651 } else {
2652 pr_debug("Got CRC32C DataDigest 0x%08x for"
2653 " %u bytes of Immediate Data\n", checksum,
2654 length);
2655 }
2656 }
2657
2658 cmd->write_data_done += length;
2659
Andy Groverebf1d952012-04-03 15:51:24 -07002660 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002661 spin_lock_bh(&cmd->istate_lock);
2662 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2663 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2664 spin_unlock_bh(&cmd->istate_lock);
2665 }
2666
2667 return IMMEDIATE_DATA_NORMAL_OPERATION;
2668}
2669
2670/*
2671 * Called with sess->conn_lock held.
2672 */
2673/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2674 with active network interface */
2675static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2676{
2677 struct iscsi_cmd *cmd;
2678 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002679 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002680
2681 /*
2682 * Only send a Asynchronous Message on connections whos network
2683 * interface is still functional.
2684 */
2685 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2686 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2687 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002688 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002689 break;
2690 }
2691 }
2692
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002693 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002694 return;
2695
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002696 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002697 if (!cmd) {
2698 iscsit_dec_conn_usage_count(conn_p);
2699 return;
2700 }
2701
2702 cmd->logout_cid = conn->cid;
2703 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2704 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2705
2706 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002707 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002708 spin_unlock_bh(&conn_p->cmd_lock);
2709
2710 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2711 iscsit_dec_conn_usage_count(conn_p);
2712}
2713
2714static int iscsit_send_conn_drop_async_message(
2715 struct iscsi_cmd *cmd,
2716 struct iscsi_conn *conn)
2717{
2718 struct iscsi_async *hdr;
2719
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002720 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2721
2722 hdr = (struct iscsi_async *) cmd->pdu;
2723 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2724 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002725 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002726 cmd->targ_xfer_tag = 0xFFFFFFFF;
2727 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2728 cmd->stat_sn = conn->stat_sn++;
2729 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2730 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002731 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002732 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2733 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2734 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2735 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2736
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002737 pr_debug("Sending Connection Dropped Async Message StatSN:"
2738 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2739 cmd->logout_cid, conn->cid);
Varun Prakash2854bb22016-04-20 00:00:08 +05302740
2741 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002742}
2743
Andy Grover6f3c0e62012-04-03 15:51:09 -07002744static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2745{
2746 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2747 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2748 wait_for_completion_interruptible_timeout(
2749 &conn->tx_half_close_comp,
2750 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2751 }
2752}
2753
Varun Prakashd2faaef2016-04-20 00:00:19 +05302754void
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002755iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2756 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2757 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002758{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002759 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2760 hdr->flags = datain->flags;
2761 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2762 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2763 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2764 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2765 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2766 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2767 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2768 }
2769 }
2770 hton24(hdr->dlength, datain->length);
2771 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2772 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2773 (struct scsi_lun *)&hdr->lun);
2774 else
2775 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2776
2777 hdr->itt = cmd->init_task_tag;
2778
2779 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2780 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2781 else
2782 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2783 if (set_statsn)
2784 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2785 else
2786 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2787
2788 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002789 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002790 hdr->datasn = cpu_to_be32(datain->data_sn);
2791 hdr->offset = cpu_to_be32(datain->offset);
2792
2793 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2794 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2795 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2796 ntohl(hdr->offset), datain->length, conn->cid);
2797}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302798EXPORT_SYMBOL(iscsit_build_datain_pdu);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002799
2800static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2801{
2802 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002803 struct iscsi_datain datain;
2804 struct iscsi_datain_req *dr;
Varun Prakash2854bb22016-04-20 00:00:08 +05302805 int eodr = 0, ret;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002806 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002807
2808 memset(&datain, 0, sizeof(struct iscsi_datain));
2809 dr = iscsit_get_datain_values(cmd, &datain);
2810 if (!dr) {
2811 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2812 cmd->init_task_tag);
2813 return -1;
2814 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002815 /*
2816 * Be paranoid and double check the logic for now.
2817 */
Andy Groverebf1d952012-04-03 15:51:24 -07002818 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002819 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2820 " datain.length: %u exceeds cmd->data_length: %u\n",
2821 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002822 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002823 return -1;
2824 }
2825
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002826 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002827 /*
2828 * Special case for successfully execution w/ both DATAIN
2829 * and Sense Data.
2830 */
2831 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2832 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2833 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2834 else {
2835 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2836 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2837 iscsit_increment_maxcmdsn(cmd, conn->sess);
2838 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002839 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002840 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002841 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2842 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002843 }
2844
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002845 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002846
Varun Prakash2854bb22016-04-20 00:00:08 +05302847 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2848 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07002849 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002850
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002851 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002852 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002853 2 : 1;
2854 iscsit_free_datain_req(cmd, dr);
2855 }
2856
Andy Grover6f3c0e62012-04-03 15:51:09 -07002857 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002858}
2859
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002860int
2861iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2862 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002863{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002864 struct iscsi_conn *logout_conn = NULL;
2865 struct iscsi_conn_recovery *cr = NULL;
2866 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002867 /*
2868 * The actual shutting down of Sessions and/or Connections
2869 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2870 * is done in scsi_logout_post_handler().
2871 */
2872 switch (cmd->logout_reason) {
2873 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2874 pr_debug("iSCSI session logout successful, setting"
2875 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2876 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2877 break;
2878 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2879 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2880 break;
2881 /*
2882 * For CLOSECONNECTION logout requests carrying
2883 * a matching logout CID -> local CID, the reference
2884 * for the local CID will have been incremented in
2885 * iscsi_logout_closeconnection().
2886 *
2887 * For CLOSECONNECTION logout requests carrying
2888 * a different CID than the connection it arrived
2889 * on, the connection responding to cmd->logout_cid
2890 * is stopped in iscsit_logout_post_handler_diffcid().
2891 */
2892
2893 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2894 " successful.\n", cmd->logout_cid, conn->cid);
2895 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2896 break;
2897 case ISCSI_LOGOUT_REASON_RECOVERY:
2898 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2899 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2900 break;
2901 /*
2902 * If the connection is still active from our point of view
2903 * force connection recovery to occur.
2904 */
2905 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2906 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002907 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002908 iscsit_connection_reinstatement_rcfr(logout_conn);
2909 iscsit_dec_conn_usage_count(logout_conn);
2910 }
2911
2912 cr = iscsit_get_inactive_connection_recovery_entry(
2913 conn->sess, cmd->logout_cid);
2914 if (!cr) {
2915 pr_err("Unable to locate CID: %hu for"
2916 " REMOVECONNFORRECOVERY Logout Request.\n",
2917 cmd->logout_cid);
2918 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2919 break;
2920 }
2921
2922 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2923
2924 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2925 " for recovery for CID: %hu on CID: %hu successful.\n",
2926 cmd->logout_cid, conn->cid);
2927 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2928 break;
2929 default:
2930 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2931 cmd->logout_reason);
2932 return -1;
2933 }
2934
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002935 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2936 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2937 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002938 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002939 cmd->stat_sn = conn->stat_sn++;
2940 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2941
2942 iscsit_increment_maxcmdsn(cmd, conn->sess);
2943 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002944 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002945
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002946 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2947 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2948 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2949 cmd->logout_cid, conn->cid);
2950
2951 return 0;
2952}
2953EXPORT_SYMBOL(iscsit_build_logout_rsp);
2954
2955static int
2956iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2957{
Varun Prakash2854bb22016-04-20 00:00:08 +05302958 int rc;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002959
2960 rc = iscsit_build_logout_rsp(cmd, conn,
2961 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2962 if (rc < 0)
2963 return rc;
2964
Varun Prakash2854bb22016-04-20 00:00:08 +05302965 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002966}
2967
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002968void
2969iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2970 struct iscsi_nopin *hdr, bool nopout_response)
2971{
2972 hdr->opcode = ISCSI_OP_NOOP_IN;
2973 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2974 hton24(hdr->dlength, cmd->buf_ptr_size);
2975 if (nopout_response)
2976 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2977 hdr->itt = cmd->init_task_tag;
2978 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2979 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2980 conn->stat_sn;
2981 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2982
2983 if (nopout_response)
2984 iscsit_increment_maxcmdsn(cmd, conn->sess);
2985
2986 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002987 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002988
2989 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2990 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
Colin Ian King3fc6a642016-09-02 15:30:34 +01002991 "Solicited" : "Unsolicited", cmd->init_task_tag,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002992 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2993}
2994EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2995
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002996/*
2997 * Unsolicited NOPIN, either requesting a response or not.
2998 */
2999static int iscsit_send_unsolicited_nopin(
3000 struct iscsi_cmd *cmd,
3001 struct iscsi_conn *conn,
3002 int want_response)
3003{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003004 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Varun Prakash2854bb22016-04-20 00:00:08 +05303005 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003006
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003007 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003008
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003009 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3010 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
3011
Varun Prakash2854bb22016-04-20 00:00:08 +05303012 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3013 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003014 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003015
3016 spin_lock_bh(&cmd->istate_lock);
3017 cmd->i_state = want_response ?
3018 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3019 spin_unlock_bh(&cmd->istate_lock);
3020
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003021 return 0;
3022}
3023
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003024static int
3025iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003026{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003027 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003028
3029 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003030
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003031 /*
3032 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3033 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3034 */
Varun Prakash2854bb22016-04-20 00:00:08 +05303035 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003036
Varun Prakash2854bb22016-04-20 00:00:08 +05303037 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3038 cmd->buf_ptr,
3039 cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003040}
3041
Andy Grover6f3c0e62012-04-03 15:51:09 -07003042static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003043 struct iscsi_cmd *cmd,
3044 struct iscsi_conn *conn)
3045{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003046 struct iscsi_r2t *r2t;
3047 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003048 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003049
3050 r2t = iscsit_get_r2t_from_list(cmd);
3051 if (!r2t)
3052 return -1;
3053
3054 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3055 memset(hdr, 0, ISCSI_HDR_LEN);
3056 hdr->opcode = ISCSI_OP_R2T;
3057 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3058 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3059 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003060 hdr->itt = cmd->init_task_tag;
Varun Prakash85672702016-04-20 00:00:13 +05303061 if (conn->conn_transport->iscsit_get_r2t_ttt)
3062 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3063 else
3064 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003065 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3066 hdr->statsn = cpu_to_be32(conn->stat_sn);
3067 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003068 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003069 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3070 hdr->data_offset = cpu_to_be32(r2t->offset);
3071 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3072
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003073 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3074 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3075 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3076 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3077 r2t->offset, r2t->xfer_len, conn->cid);
3078
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003079 spin_lock_bh(&cmd->r2t_lock);
3080 r2t->sent_r2t = 1;
3081 spin_unlock_bh(&cmd->r2t_lock);
3082
Varun Prakash2854bb22016-04-20 00:00:08 +05303083 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003084 if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003085 return ret;
3086 }
3087
3088 spin_lock_bh(&cmd->dataout_timeout_lock);
3089 iscsit_start_dataout_timer(cmd, conn);
3090 spin_unlock_bh(&cmd->dataout_timeout_lock);
3091
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003092 return 0;
3093}
3094
3095/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003096 * @recovery: If called from iscsi_task_reassign_complete_write() for
3097 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003098 */
3099int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003100 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003101 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003102 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003103{
3104 int first_r2t = 1;
3105 u32 offset = 0, xfer_len = 0;
3106
3107 spin_lock_bh(&cmd->r2t_lock);
3108 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3109 spin_unlock_bh(&cmd->r2t_lock);
3110 return 0;
3111 }
3112
Andy Grover8b1e1242012-04-03 15:51:12 -07003113 if (conn->sess->sess_ops->DataSequenceInOrder &&
3114 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003115 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003116
3117 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3118 if (conn->sess->sess_ops->DataSequenceInOrder) {
3119 offset = cmd->r2t_offset;
3120
Andy Grover8b1e1242012-04-03 15:51:12 -07003121 if (first_r2t && recovery) {
3122 int new_data_end = offset +
3123 conn->sess->sess_ops->MaxBurstLength -
3124 cmd->next_burst_len;
3125
Andy Groverebf1d952012-04-03 15:51:24 -07003126 if (new_data_end > cmd->se_cmd.data_length)
3127 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003128 else
3129 xfer_len =
3130 conn->sess->sess_ops->MaxBurstLength -
3131 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003132 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003133 int new_data_end = offset +
3134 conn->sess->sess_ops->MaxBurstLength;
3135
Andy Groverebf1d952012-04-03 15:51:24 -07003136 if (new_data_end > cmd->se_cmd.data_length)
3137 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003138 else
3139 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003140 }
3141 cmd->r2t_offset += xfer_len;
3142
Andy Groverebf1d952012-04-03 15:51:24 -07003143 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003144 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3145 } else {
3146 struct iscsi_seq *seq;
3147
3148 seq = iscsit_get_seq_holder_for_r2t(cmd);
3149 if (!seq) {
3150 spin_unlock_bh(&cmd->r2t_lock);
3151 return -1;
3152 }
3153
3154 offset = seq->offset;
3155 xfer_len = seq->xfer_len;
3156
3157 if (cmd->seq_send_order == cmd->seq_count)
3158 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3159 }
3160 cmd->outstanding_r2ts++;
3161 first_r2t = 0;
3162
3163 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3164 spin_unlock_bh(&cmd->r2t_lock);
3165 return -1;
3166 }
3167
3168 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3169 break;
3170 }
3171 spin_unlock_bh(&cmd->r2t_lock);
3172
3173 return 0;
3174}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303175EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003176
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003177void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3178 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003179{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003180 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003181 cmd->stat_sn = conn->stat_sn++;
3182
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003183 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003184
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003185 memset(hdr, 0, ISCSI_HDR_LEN);
3186 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3187 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3188 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3189 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003190 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003191 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3192 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003193 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003194 }
3195 hdr->response = cmd->iscsi_response;
3196 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003197 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003198 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3199
3200 iscsit_increment_maxcmdsn(cmd, conn->sess);
3201 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003202 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003203
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003204 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3205 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3206 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3207 cmd->se_cmd.scsi_status, conn->cid);
3208}
3209EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3210
3211static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3212{
3213 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003214 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
Varun Prakash2854bb22016-04-20 00:00:08 +05303215 void *data_buf = NULL;
3216 u32 padding = 0, data_buf_len = 0;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003217
3218 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3219
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003220 /*
3221 * Attach SENSE DATA payload to iSCSI Response PDU
3222 */
3223 if (cmd->se_cmd.sense_buffer &&
3224 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3225 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003226 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3227 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3228
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003229 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003230 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Varun Prakash2854bb22016-04-20 00:00:08 +05303231 data_buf = cmd->sense_buffer;
3232 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003233
3234 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003235 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003236 cmd->se_cmd.scsi_sense_length, 0, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003237 pr_debug("Adding %u bytes of padding to"
3238 " SENSE.\n", padding);
3239 }
3240
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003241 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3242 " Response PDU\n",
3243 cmd->se_cmd.scsi_sense_length);
3244 }
3245
Varun Prakash2854bb22016-04-20 00:00:08 +05303246 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3247 data_buf_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003248}
3249
3250static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3251{
3252 switch (se_tmr->response) {
3253 case TMR_FUNCTION_COMPLETE:
3254 return ISCSI_TMF_RSP_COMPLETE;
3255 case TMR_TASK_DOES_NOT_EXIST:
3256 return ISCSI_TMF_RSP_NO_TASK;
3257 case TMR_LUN_DOES_NOT_EXIST:
3258 return ISCSI_TMF_RSP_NO_LUN;
3259 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3260 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003261 case TMR_FUNCTION_REJECTED:
3262 default:
3263 return ISCSI_TMF_RSP_REJECTED;
3264 }
3265}
3266
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003267void
3268iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3269 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003270{
3271 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003272
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003273 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003274 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003275 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003276 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003277 cmd->stat_sn = conn->stat_sn++;
3278 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3279
3280 iscsit_increment_maxcmdsn(cmd, conn->sess);
3281 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003282 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003283
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003284 pr_debug("Built Task Management Response ITT: 0x%08x,"
3285 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3286 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3287}
3288EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3289
3290static int
3291iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3292{
3293 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003294
3295 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3296
Varun Prakash2854bb22016-04-20 00:00:08 +05303297 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003298}
3299
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003300static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3301{
3302 bool ret = false;
3303
3304 if (np->np_sockaddr.ss_family == AF_INET6) {
3305 const struct sockaddr_in6 sin6 = {
3306 .sin6_addr = IN6ADDR_ANY_INIT };
3307 struct sockaddr_in6 *sock_in6 =
3308 (struct sockaddr_in6 *)&np->np_sockaddr;
3309
3310 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3311 sin6.sin6_addr.s6_addr, 16))
3312 ret = true;
3313 } else {
3314 struct sockaddr_in * sock_in =
3315 (struct sockaddr_in *)&np->np_sockaddr;
3316
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003317 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003318 ret = true;
3319 }
3320
3321 return ret;
3322}
3323
Andy Grover8b1e1242012-04-03 15:51:12 -07003324#define SENDTARGETS_BUF_LIMIT 32768U
3325
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003326static int
3327iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003328 enum iscsit_transport_type network_transport,
3329 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003330{
3331 char *payload = NULL;
3332 struct iscsi_conn *conn = cmd->conn;
3333 struct iscsi_portal_group *tpg;
3334 struct iscsi_tiqn *tiqn;
3335 struct iscsi_tpg_np *tpg_np;
3336 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003337 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003338 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003339 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
David Disseldorpa6415cd2015-08-01 00:10:12 -07003340 bool active;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003341
Sagi Grimbergbe7dcfb2015-01-26 12:49:06 +02003342 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003343 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003344
3345 payload = kzalloc(buffer_len, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02003346 if (!payload)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003347 return -ENOMEM;
Markus Elfringc46e22f2017-04-09 15:34:50 +02003348
Nicholas Bellinger66658892013-06-19 22:45:42 -07003349 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003350 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003351 * explicit case..
3352 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003353 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003354 text_ptr = strchr(text_in, '=');
3355 if (!text_ptr) {
3356 pr_err("Unable to locate '=' string in text_in:"
3357 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003358 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003359 return -EINVAL;
3360 }
3361 /*
3362 * Skip over '=' character..
3363 */
3364 text_ptr += 1;
3365 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003366
3367 spin_lock(&tiqn_lock);
3368 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003369 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003370 strcmp(tiqn->tiqn, text_ptr)) {
3371 continue;
3372 }
3373
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003374 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003375
3376 spin_lock(&tiqn->tiqn_tpg_lock);
3377 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3378
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003379 /* If demo_mode_discovery=0 and generate_node_acls=0
3380 * (demo mode dislabed) do not return
3381 * TargetName+TargetAddress unless a NodeACL exists.
3382 */
3383
3384 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3385 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
Nicholas Bellinger21aaa232016-01-07 22:09:27 -08003386 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003387 cmd->conn->sess->sess_ops->InitiatorName))) {
3388 continue;
3389 }
3390
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003391 spin_lock(&tpg->tpg_state_lock);
David Disseldorpa6415cd2015-08-01 00:10:12 -07003392 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003393 spin_unlock(&tpg->tpg_state_lock);
3394
David Disseldorpa6415cd2015-08-01 00:10:12 -07003395 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3396 continue;
3397
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003398 spin_lock(&tpg->tpg_np_lock);
3399 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3400 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003401 struct iscsi_np *np = tpg_np->tpg_np;
3402 bool inaddr_any = iscsit_check_inaddr_any(np);
Andy Grover13a3cf02015-08-24 10:26:06 -07003403 struct sockaddr_storage *sockaddr;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003404
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003405 if (np->np_network_transport != network_transport)
3406 continue;
3407
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003408 if (!target_name_printed) {
3409 len = sprintf(buf, "TargetName=%s",
3410 tiqn->tiqn);
3411 len += 1;
3412
3413 if ((len + payload_len) > buffer_len) {
3414 spin_unlock(&tpg->tpg_np_lock);
3415 spin_unlock(&tiqn->tiqn_tpg_lock);
3416 end_of_buf = 1;
3417 goto eob;
3418 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003419
3420 if (skip_bytes && len <= skip_bytes) {
3421 skip_bytes -= len;
3422 } else {
3423 memcpy(payload + payload_len, buf, len);
3424 payload_len += len;
3425 target_name_printed = 1;
3426 if (len > skip_bytes)
3427 skip_bytes = 0;
3428 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003429 }
3430
Andy Grover69d75572015-08-24 10:26:04 -07003431 if (inaddr_any)
3432 sockaddr = &conn->local_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003433 else
Andy Grover69d75572015-08-24 10:26:04 -07003434 sockaddr = &np->np_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003435
Andy Grover69d75572015-08-24 10:26:04 -07003436 len = sprintf(buf, "TargetAddress="
3437 "%pISpc,%hu",
3438 sockaddr,
3439 tpg->tpgt);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003440 len += 1;
3441
3442 if ((len + payload_len) > buffer_len) {
3443 spin_unlock(&tpg->tpg_np_lock);
3444 spin_unlock(&tiqn->tiqn_tpg_lock);
3445 end_of_buf = 1;
3446 goto eob;
3447 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003448
3449 if (skip_bytes && len <= skip_bytes) {
3450 skip_bytes -= len;
3451 } else {
3452 memcpy(payload + payload_len, buf, len);
3453 payload_len += len;
3454 if (len > skip_bytes)
3455 skip_bytes = 0;
3456 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003457 }
3458 spin_unlock(&tpg->tpg_np_lock);
3459 }
3460 spin_unlock(&tiqn->tiqn_tpg_lock);
3461eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003462 if (end_of_buf) {
3463 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003464 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003465 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003466
Andy Grover8060b8d2015-01-09 15:13:08 -08003467 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003468 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003469 }
3470 spin_unlock(&tiqn_lock);
3471
3472 cmd->buf_ptr = payload;
3473
3474 return payload_len;
3475}
3476
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003477int
3478iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003479 struct iscsi_text_rsp *hdr,
3480 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003481{
3482 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003483 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003484
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003485 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3486 cmd->read_data_done,
3487 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003488 if (text_length < 0)
3489 return text_length;
3490
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003491 if (completed) {
Varun Prakash310d40a2017-07-23 20:03:45 +05303492 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003493 } else {
Varun Prakash310d40a2017-07-23 20:03:45 +05303494 hdr->flags = ISCSI_FLAG_TEXT_CONTINUE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003495 cmd->read_data_done += text_length;
3496 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3497 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3498 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003499 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003500 padding = ((-text_length) & 3);
3501 hton24(hdr->dlength, text_length);
3502 hdr->itt = cmd->init_task_tag;
3503 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3504 cmd->stat_sn = conn->stat_sn++;
3505 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3506
3507 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003508 /*
3509 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3510 * correctly increment MaxCmdSN for each response answering a
3511 * non immediate text request with a valid CmdSN.
3512 */
3513 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003514 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003515 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003516
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003517 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3518 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3519 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3520 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3521 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003522
3523 return text_length + padding;
3524}
3525EXPORT_SYMBOL(iscsit_build_text_rsp);
3526
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003527static int iscsit_send_text_rsp(
3528 struct iscsi_cmd *cmd,
3529 struct iscsi_conn *conn)
3530{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003531 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Varun Prakash2854bb22016-04-20 00:00:08 +05303532 int text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003533
Varun Prakash864e5042016-04-20 00:00:15 +05303534 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3535 conn->conn_transport->transport_type);
Varun Prakash2854bb22016-04-20 00:00:08 +05303536 if (text_length < 0)
3537 return text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003538
Varun Prakash2854bb22016-04-20 00:00:08 +05303539 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3540 cmd->buf_ptr,
3541 text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003542}
3543
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003544void
3545iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3546 struct iscsi_reject *hdr)
3547{
3548 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003549 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003550 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3551 hton24(hdr->dlength, ISCSI_HDR_LEN);
3552 hdr->ffffffff = cpu_to_be32(0xffffffff);
3553 cmd->stat_sn = conn->stat_sn++;
3554 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3555 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003556 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003557
3558}
3559EXPORT_SYMBOL(iscsit_build_reject);
3560
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003561static int iscsit_send_reject(
3562 struct iscsi_cmd *cmd,
3563 struct iscsi_conn *conn)
3564{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003565 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003566
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003567 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003568
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003569 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3570 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3571
Varun Prakash2854bb22016-04-20 00:00:08 +05303572 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3573 cmd->buf_ptr,
3574 ISCSI_HDR_LEN);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003575}
3576
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003577void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3578{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003579 int ord, cpu;
3580 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003581 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3582 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003583 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003584 * Here we use bitmap_id to determine which CPU that this
3585 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003586 * execute upon.
3587 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003588 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003589 for_each_online_cpu(cpu) {
3590 if (ord-- == 0) {
3591 cpumask_set_cpu(cpu, conn->conn_cpumask);
3592 return;
3593 }
3594 }
3595 /*
3596 * This should never be reached..
3597 */
3598 dump_stack();
3599 cpumask_setall(conn->conn_cpumask);
3600}
3601
Varun Prakashd2faaef2016-04-20 00:00:19 +05303602int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003603iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003604{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003605 int ret;
3606
3607 switch (state) {
3608 case ISTATE_SEND_R2T:
3609 ret = iscsit_send_r2t(cmd, conn);
3610 if (ret < 0)
3611 goto err;
3612 break;
3613 case ISTATE_REMOVE:
3614 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003615 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003616 spin_unlock_bh(&conn->cmd_lock);
3617
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003618 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003619 break;
3620 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3621 iscsit_mod_nopin_response_timer(conn);
3622 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3623 if (ret < 0)
3624 goto err;
3625 break;
3626 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3627 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3628 if (ret < 0)
3629 goto err;
3630 break;
3631 default:
3632 pr_err("Unknown Opcode: 0x%02x ITT:"
3633 " 0x%08x, i_state: %d on CID: %hu\n",
3634 cmd->iscsi_opcode, cmd->init_task_tag, state,
3635 conn->cid);
3636 goto err;
3637 }
3638
3639 return 0;
3640
3641err:
3642 return -1;
3643}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303644EXPORT_SYMBOL(iscsit_immediate_queue);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003645
3646static int
3647iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3648{
3649 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003650 struct iscsi_queue_req *qr;
3651 struct iscsi_cmd *cmd;
3652 u8 state;
3653 int ret;
3654
3655 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3656 atomic_set(&conn->check_immediate_queue, 0);
3657 cmd = qr->cmd;
3658 state = qr->state;
3659 kmem_cache_free(lio_qr_cache, qr);
3660
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003661 ret = t->iscsit_immediate_queue(conn, cmd, state);
3662 if (ret < 0)
3663 return ret;
3664 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003665
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003666 return 0;
3667}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003668
Varun Prakashd2faaef2016-04-20 00:00:19 +05303669int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003670iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3671{
3672 int ret;
3673
3674check_rsp_state:
3675 switch (state) {
3676 case ISTATE_SEND_DATAIN:
3677 ret = iscsit_send_datain(cmd, conn);
3678 if (ret < 0)
3679 goto err;
3680 else if (!ret)
3681 /* more drs */
3682 goto check_rsp_state;
3683 else if (ret == 1) {
3684 /* all done */
3685 spin_lock_bh(&cmd->istate_lock);
3686 cmd->i_state = ISTATE_SENT_STATUS;
3687 spin_unlock_bh(&cmd->istate_lock);
3688
3689 if (atomic_read(&conn->check_immediate_queue))
3690 return 1;
3691
3692 return 0;
3693 } else if (ret == 2) {
3694 /* Still must send status,
3695 SCF_TRANSPORT_TASK_SENSE was set */
3696 spin_lock_bh(&cmd->istate_lock);
3697 cmd->i_state = ISTATE_SEND_STATUS;
3698 spin_unlock_bh(&cmd->istate_lock);
3699 state = ISTATE_SEND_STATUS;
3700 goto check_rsp_state;
3701 }
3702
3703 break;
3704 case ISTATE_SEND_STATUS:
3705 case ISTATE_SEND_STATUS_RECOVERY:
3706 ret = iscsit_send_response(cmd, conn);
3707 break;
3708 case ISTATE_SEND_LOGOUTRSP:
3709 ret = iscsit_send_logout(cmd, conn);
3710 break;
3711 case ISTATE_SEND_ASYNCMSG:
3712 ret = iscsit_send_conn_drop_async_message(
3713 cmd, conn);
3714 break;
3715 case ISTATE_SEND_NOPIN:
3716 ret = iscsit_send_nopin(cmd, conn);
3717 break;
3718 case ISTATE_SEND_REJECT:
3719 ret = iscsit_send_reject(cmd, conn);
3720 break;
3721 case ISTATE_SEND_TASKMGTRSP:
3722 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3723 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003724 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003725 ret = iscsit_tmr_post_handler(cmd, conn);
3726 if (ret != 0)
3727 iscsit_fall_back_to_erl0(conn->sess);
3728 break;
3729 case ISTATE_SEND_TEXTRSP:
3730 ret = iscsit_send_text_rsp(cmd, conn);
3731 break;
3732 default:
3733 pr_err("Unknown Opcode: 0x%02x ITT:"
3734 " 0x%08x, i_state: %d on CID: %hu\n",
3735 cmd->iscsi_opcode, cmd->init_task_tag,
3736 state, conn->cid);
3737 goto err;
3738 }
3739 if (ret < 0)
3740 goto err;
3741
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003742 switch (state) {
3743 case ISTATE_SEND_LOGOUTRSP:
3744 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003745 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003746 /* fall through */
3747 case ISTATE_SEND_STATUS:
3748 case ISTATE_SEND_ASYNCMSG:
3749 case ISTATE_SEND_NOPIN:
3750 case ISTATE_SEND_STATUS_RECOVERY:
3751 case ISTATE_SEND_TEXTRSP:
3752 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003753 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003754 spin_lock_bh(&cmd->istate_lock);
3755 cmd->i_state = ISTATE_SENT_STATUS;
3756 spin_unlock_bh(&cmd->istate_lock);
3757 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003758 default:
3759 pr_err("Unknown Opcode: 0x%02x ITT:"
3760 " 0x%08x, i_state: %d on CID: %hu\n",
3761 cmd->iscsi_opcode, cmd->init_task_tag,
3762 cmd->i_state, conn->cid);
3763 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003764 }
3765
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003766 if (atomic_read(&conn->check_immediate_queue))
3767 return 1;
3768
Andy Grover6f3c0e62012-04-03 15:51:09 -07003769 return 0;
3770
3771err:
3772 return -1;
3773}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303774EXPORT_SYMBOL(iscsit_response_queue);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003775
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003776static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003777{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003778 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003779 struct iscsi_queue_req *qr;
3780 struct iscsi_cmd *cmd;
3781 u8 state;
3782 int ret;
3783
3784 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3785 cmd = qr->cmd;
3786 state = qr->state;
3787 kmem_cache_free(lio_qr_cache, qr);
3788
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003789 ret = t->iscsit_response_queue(conn, cmd, state);
3790 if (ret == 1 || ret < 0)
3791 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003792 }
3793
3794 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003795}
3796
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003797int iscsi_target_tx_thread(void *arg)
3798{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003799 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003800 struct iscsi_conn *conn = arg;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003801 bool conn_freed = false;
3802
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003803 /*
3804 * Allow ourselves to be interrupted by SIGINT so that a
3805 * connection recovery / failure event can be triggered externally.
3806 */
3807 allow_signal(SIGINT);
3808
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003809 while (!kthread_should_stop()) {
3810 /*
3811 * Ensure that both TX and RX per connection kthreads
3812 * are scheduled to run on the same CPU.
3813 */
3814 iscsit_thread_check_cpumask(conn, current, 1);
3815
Roland Dreierd5627ac2012-10-31 09:16:46 -07003816 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003817 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003818
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003819 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003820 goto transport_err;
3821
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003822get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003823 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003824 if (ret < 0)
3825 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003826
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003827 ret = iscsit_handle_response_queue(conn);
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003828 if (ret == 1) {
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003829 goto get_immediate;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003830 } else if (ret == -ECONNRESET) {
3831 conn_freed = true;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003832 goto out;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003833 } else if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003834 goto transport_err;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003835 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003836 }
3837
3838transport_err:
Nicholas Bellingere5419862015-07-22 23:14:19 -07003839 /*
3840 * Avoid the normal connection failure code-path if this connection
3841 * is still within LOGIN mode, and iscsi_np process context is
3842 * responsible for cleaning up the early connection failure.
3843 */
3844 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003845 iscsit_take_action_for_connection_exit(conn, &conn_freed);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003846out:
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003847 if (!conn_freed) {
3848 while (!kthread_should_stop()) {
3849 msleep(100);
3850 }
3851 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003852 return 0;
3853}
3854
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003855static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3856{
3857 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3858 struct iscsi_cmd *cmd;
3859 int ret = 0;
3860
3861 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3862 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003863 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003864 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003865 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003866
3867 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3868 break;
3869 case ISCSI_OP_SCSI_DATA_OUT:
3870 ret = iscsit_handle_data_out(conn, buf);
3871 break;
3872 case ISCSI_OP_NOOP_OUT:
3873 cmd = NULL;
3874 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003875 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003876 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003877 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003878 }
3879 ret = iscsit_handle_nop_out(conn, cmd, buf);
3880 break;
3881 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003882 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003883 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003884 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003885
3886 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3887 break;
3888 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003889 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3890 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3891 if (!cmd)
3892 goto reject;
3893 } else {
3894 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3895 if (!cmd)
3896 goto reject;
3897 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003898
3899 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003900 break;
3901 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003902 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003903 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003904 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003905
3906 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3907 if (ret > 0)
3908 wait_for_completion_timeout(&conn->conn_logout_comp,
3909 SECONDS_FOR_LOGOUT_COMP * HZ);
3910 break;
3911 case ISCSI_OP_SNACK:
3912 ret = iscsit_handle_snack(conn, buf);
3913 break;
3914 default:
3915 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3916 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3917 pr_err("Cannot recover from unknown"
3918 " opcode while ERL=0, closing iSCSI connection.\n");
3919 return -1;
3920 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02003921 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3922 " closing iSCSI connection.\n");
3923 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003924 break;
3925 }
3926
3927 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003928reject:
3929 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003930}
3931
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08003932static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3933{
3934 bool ret;
3935
3936 spin_lock_bh(&conn->state_lock);
3937 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3938 spin_unlock_bh(&conn->state_lock);
3939
3940 return ret;
3941}
3942
Varun Prakashe8205cc2016-04-20 00:00:11 +05303943static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003944{
Varun Prakashe8205cc2016-04-20 00:00:11 +05303945 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003946 u8 buffer[ISCSI_HDR_LEN], opcode;
3947 u32 checksum = 0, digest = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003948 struct kvec iov;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003949
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003950 while (!kthread_should_stop()) {
3951 /*
3952 * Ensure that both TX and RX per connection kthreads
3953 * are scheduled to run on the same CPU.
3954 */
3955 iscsit_thread_check_cpumask(conn, current, 0);
3956
3957 memset(buffer, 0, ISCSI_HDR_LEN);
3958 memset(&iov, 0, sizeof(struct kvec));
3959
3960 iov.iov_base = buffer;
3961 iov.iov_len = ISCSI_HDR_LEN;
3962
3963 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3964 if (ret != ISCSI_HDR_LEN) {
3965 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303966 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003967 }
3968
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003969 if (conn->conn_ops->HeaderDigest) {
3970 iov.iov_base = &digest;
3971 iov.iov_len = ISCSI_CRC_LEN;
3972
3973 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3974 if (ret != ISCSI_CRC_LEN) {
3975 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303976 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003977 }
3978
Herbert Xu69110e32016-01-24 21:19:52 +08003979 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003980 buffer, ISCSI_HDR_LEN,
3981 0, NULL, (u8 *)&checksum);
3982
3983 if (digest != checksum) {
3984 pr_err("HeaderDigest CRC32C failed,"
3985 " received 0x%08x, computed 0x%08x\n",
3986 digest, checksum);
3987 /*
3988 * Set the PDU to 0xff so it will intentionally
3989 * hit default in the switch below.
3990 */
3991 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003992 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003993 } else {
3994 pr_debug("Got HeaderDigest CRC32C"
3995 " 0x%08x\n", checksum);
3996 }
3997 }
3998
3999 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
Varun Prakashe8205cc2016-04-20 00:00:11 +05304000 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004001
4002 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4003
4004 if (conn->sess->sess_ops->SessionType &&
4005 ((!(opcode & ISCSI_OP_TEXT)) ||
4006 (!(opcode & ISCSI_OP_LOGOUT)))) {
4007 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4008 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004009 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4010 buffer);
Varun Prakashe8205cc2016-04-20 00:00:11 +05304011 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004012 }
4013
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004014 ret = iscsi_target_rx_opcode(conn, buffer);
4015 if (ret < 0)
Varun Prakashe8205cc2016-04-20 00:00:11 +05304016 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004017 }
Varun Prakashe8205cc2016-04-20 00:00:11 +05304018}
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004019
Varun Prakashe8205cc2016-04-20 00:00:11 +05304020int iscsi_target_rx_thread(void *arg)
4021{
4022 int rc;
4023 struct iscsi_conn *conn = arg;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004024 bool conn_freed = false;
Varun Prakashe8205cc2016-04-20 00:00:11 +05304025
4026 /*
4027 * Allow ourselves to be interrupted by SIGINT so that a
4028 * connection recovery / failure event can be triggered externally.
4029 */
4030 allow_signal(SIGINT);
4031 /*
4032 * Wait for iscsi_post_login_handler() to complete before allowing
4033 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4034 */
4035 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4036 if (rc < 0 || iscsi_target_check_conn_state(conn))
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004037 goto out;
Varun Prakashe8205cc2016-04-20 00:00:11 +05304038
4039 if (!conn->conn_transport->iscsit_get_rx_pdu)
4040 return 0;
4041
4042 conn->conn_transport->iscsit_get_rx_pdu(conn);
4043
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004044 if (!signal_pending(current))
4045 atomic_set(&conn->transport_failed, 1);
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004046 iscsit_take_action_for_connection_exit(conn, &conn_freed);
4047
4048out:
4049 if (!conn_freed) {
4050 while (!kthread_should_stop()) {
4051 msleep(100);
4052 }
4053 }
4054
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004055 return 0;
4056}
4057
4058static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4059{
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004060 LIST_HEAD(tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004061 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4062 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004063 /*
4064 * We expect this function to only ever be called from either RX or TX
4065 * thread context via iscsit_close_connection() once the other context
4066 * has been reset -> returned sleeping pre-handler state.
4067 */
4068 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004069 list_splice_init(&conn->conn_cmd_list, &tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004070
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004071 list_for_each_entry(cmd, &tmp_list, i_conn_node) {
4072 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004073
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004074 if (se_cmd->se_tfo != NULL) {
4075 spin_lock(&se_cmd->t_state_lock);
4076 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4077 spin_unlock(&se_cmd->t_state_lock);
4078 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004079 }
4080 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004081
4082 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4083 list_del_init(&cmd->i_conn_node);
4084
4085 iscsit_increment_maxcmdsn(cmd, sess);
4086 iscsit_free_cmd(cmd, true);
4087
4088 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004089}
4090
4091static void iscsit_stop_timers_for_cmds(
4092 struct iscsi_conn *conn)
4093{
4094 struct iscsi_cmd *cmd;
4095
4096 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004097 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004098 if (cmd->data_direction == DMA_TO_DEVICE)
4099 iscsit_stop_dataout_timer(cmd);
4100 }
4101 spin_unlock_bh(&conn->cmd_lock);
4102}
4103
4104int iscsit_close_connection(
4105 struct iscsi_conn *conn)
4106{
4107 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4108 struct iscsi_session *sess = conn->sess;
4109
4110 pr_debug("Closing iSCSI connection CID %hu on SID:"
4111 " %u\n", conn->cid, sess->sid);
4112 /*
Varun Prakashb4869ee2016-04-20 00:00:18 +05304113 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4114 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4115 * sleeping and the logout response never got sent because the
4116 * connection failed.
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004117 *
4118 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4119 * to signal logout response TX interrupt completion. Go ahead and skip
4120 * this for iser since isert_rx_opcode() does not wait on logout failure,
4121 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004122 */
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07004123 if (!conn->conn_transport->rdma_shutdown)
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004124 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004125
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004126 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4127 if (conn->tx_thread &&
4128 cmpxchg(&conn->tx_thread_active, true, false)) {
4129 send_sig(SIGINT, conn->tx_thread, 1);
4130 kthread_stop(conn->tx_thread);
4131 }
4132 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4133 if (conn->rx_thread &&
4134 cmpxchg(&conn->rx_thread_active, true, false)) {
4135 send_sig(SIGINT, conn->rx_thread, 1);
4136 kthread_stop(conn->rx_thread);
4137 }
4138 }
4139
4140 spin_lock(&iscsit_global->ts_bitmap_lock);
4141 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4142 get_order(1));
4143 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004144
4145 iscsit_stop_timers_for_cmds(conn);
4146 iscsit_stop_nopin_response_timer(conn);
4147 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004148
4149 if (conn->conn_transport->iscsit_wait_conn)
4150 conn->conn_transport->iscsit_wait_conn(conn);
4151
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004152 /*
4153 * During Connection recovery drop unacknowledged out of order
4154 * commands for this connection, and prepare the other commands
Bart Van Assche53c561d2016-12-23 14:40:24 +01004155 * for reallegiance.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004156 *
4157 * During normal operation clear the out of order commands (but
4158 * do not free the struct iscsi_ooo_cmdsn's) and release all
4159 * struct iscsi_cmds.
4160 */
4161 if (atomic_read(&conn->connection_recovery)) {
4162 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
Bart Van Assche53c561d2016-12-23 14:40:24 +01004163 iscsit_prepare_cmds_for_reallegiance(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004164 } else {
4165 iscsit_clear_ooo_cmdsns_for_conn(conn);
4166 iscsit_release_commands_from_conn(conn);
4167 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004168 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004169
4170 /*
4171 * Handle decrementing session or connection usage count if
4172 * a logout response was not able to be sent because the
4173 * connection failed. Fall back to Session Recovery here.
4174 */
4175 if (atomic_read(&conn->conn_logout_remove)) {
4176 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4177 iscsit_dec_conn_usage_count(conn);
4178 iscsit_dec_session_usage_count(sess);
4179 }
4180 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4181 iscsit_dec_conn_usage_count(conn);
4182
4183 atomic_set(&conn->conn_logout_remove, 0);
4184 atomic_set(&sess->session_reinstatement, 0);
4185 atomic_set(&sess->session_fall_back_to_erl0, 1);
4186 }
4187
4188 spin_lock_bh(&sess->conn_lock);
4189 list_del(&conn->conn_list);
4190
4191 /*
4192 * Attempt to let the Initiator know this connection failed by
4193 * sending an Connection Dropped Async Message on another
4194 * active connection.
4195 */
4196 if (atomic_read(&conn->connection_recovery))
4197 iscsit_build_conn_drop_async_message(conn);
4198
4199 spin_unlock_bh(&sess->conn_lock);
4200
4201 /*
4202 * If connection reinstatement is being performed on this connection,
4203 * up the connection reinstatement semaphore that is being blocked on
4204 * in iscsit_cause_connection_reinstatement().
4205 */
4206 spin_lock_bh(&conn->state_lock);
4207 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4208 spin_unlock_bh(&conn->state_lock);
4209 complete(&conn->conn_wait_comp);
4210 wait_for_completion(&conn->conn_post_wait_comp);
4211 spin_lock_bh(&conn->state_lock);
4212 }
4213
4214 /*
4215 * If connection reinstatement is being performed on this connection
4216 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4217 * connection wait rcfr semaphore that is being blocked on
4218 * an iscsit_connection_reinstatement_rcfr().
4219 */
4220 if (atomic_read(&conn->connection_wait_rcfr)) {
4221 spin_unlock_bh(&conn->state_lock);
4222 complete(&conn->conn_wait_rcfr_comp);
4223 wait_for_completion(&conn->conn_post_wait_comp);
4224 spin_lock_bh(&conn->state_lock);
4225 }
4226 atomic_set(&conn->connection_reinstatement, 1);
4227 spin_unlock_bh(&conn->state_lock);
4228
4229 /*
4230 * If any other processes are accessing this connection pointer we
4231 * must wait until they have completed.
4232 */
4233 iscsit_check_conn_usage_count(conn);
4234
Herbert Xu69110e32016-01-24 21:19:52 +08004235 ahash_request_free(conn->conn_tx_hash);
4236 if (conn->conn_rx_hash) {
4237 struct crypto_ahash *tfm;
4238
4239 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4240 ahash_request_free(conn->conn_rx_hash);
4241 crypto_free_ahash(tfm);
4242 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004243
Joern Engelfbecb652014-09-02 17:49:47 -04004244 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004245
4246 kfree(conn->conn_ops);
4247 conn->conn_ops = NULL;
4248
Al Virobf6932f2012-07-21 08:55:18 +01004249 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004250 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004251
4252 if (conn->conn_transport->iscsit_free_conn)
4253 conn->conn_transport->iscsit_free_conn(conn);
4254
4255 iscsit_put_transport(conn->conn_transport);
4256
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004257 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4258 conn->conn_state = TARG_CONN_STATE_FREE;
4259 kfree(conn);
4260
4261 spin_lock_bh(&sess->conn_lock);
4262 atomic_dec(&sess->nconn);
4263 pr_debug("Decremented iSCSI connection count to %hu from node:"
4264 " %s\n", atomic_read(&sess->nconn),
4265 sess->sess_ops->InitiatorName);
4266 /*
4267 * Make sure that if one connection fails in an non ERL=2 iSCSI
4268 * Session that they all fail.
4269 */
4270 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4271 !atomic_read(&sess->session_logout))
4272 atomic_set(&sess->session_fall_back_to_erl0, 1);
4273
4274 /*
4275 * If this was not the last connection in the session, and we are
4276 * performing session reinstatement or falling back to ERL=0, call
4277 * iscsit_stop_session() without sleeping to shutdown the other
4278 * active connections.
4279 */
4280 if (atomic_read(&sess->nconn)) {
4281 if (!atomic_read(&sess->session_reinstatement) &&
4282 !atomic_read(&sess->session_fall_back_to_erl0)) {
4283 spin_unlock_bh(&sess->conn_lock);
4284 return 0;
4285 }
4286 if (!atomic_read(&sess->session_stop_active)) {
4287 atomic_set(&sess->session_stop_active, 1);
4288 spin_unlock_bh(&sess->conn_lock);
4289 iscsit_stop_session(sess, 0, 0);
4290 return 0;
4291 }
4292 spin_unlock_bh(&sess->conn_lock);
4293 return 0;
4294 }
4295
4296 /*
4297 * If this was the last connection in the session and one of the
4298 * following is occurring:
4299 *
4300 * Session Reinstatement is not being performed, and are falling back
4301 * to ERL=0 call iscsit_close_session().
4302 *
4303 * Session Logout was requested. iscsit_close_session() will be called
4304 * elsewhere.
4305 *
4306 * Session Continuation is not being performed, start the Time2Retain
4307 * handler and check if sleep_on_sess_wait_sem is active.
4308 */
4309 if (!atomic_read(&sess->session_reinstatement) &&
4310 atomic_read(&sess->session_fall_back_to_erl0)) {
4311 spin_unlock_bh(&sess->conn_lock);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004312 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004313
4314 return 0;
4315 } else if (atomic_read(&sess->session_logout)) {
4316 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4317 sess->session_state = TARG_SESS_STATE_FREE;
4318 spin_unlock_bh(&sess->conn_lock);
4319
4320 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4321 complete(&sess->session_wait_comp);
4322
4323 return 0;
4324 } else {
4325 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4326 sess->session_state = TARG_SESS_STATE_FAILED;
4327
4328 if (!atomic_read(&sess->session_continuation)) {
4329 spin_unlock_bh(&sess->conn_lock);
4330 iscsit_start_time2retain_handler(sess);
4331 } else
4332 spin_unlock_bh(&sess->conn_lock);
4333
4334 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4335 complete(&sess->session_wait_comp);
4336
4337 return 0;
4338 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004339}
4340
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004341/*
4342 * If the iSCSI Session for the iSCSI Initiator Node exists,
4343 * forcefully shutdown the iSCSI NEXUS.
4344 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004345int iscsit_close_session(struct iscsi_session *sess)
4346{
Andy Grover60bfcf82013-10-09 11:05:58 -07004347 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004348 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4349
4350 if (atomic_read(&sess->nconn)) {
4351 pr_err("%d connection(s) still exist for iSCSI session"
4352 " to %s\n", atomic_read(&sess->nconn),
4353 sess->sess_ops->InitiatorName);
4354 BUG();
4355 }
4356
4357 spin_lock_bh(&se_tpg->session_lock);
4358 atomic_set(&sess->session_logout, 1);
4359 atomic_set(&sess->session_reinstatement, 1);
4360 iscsit_stop_time2retain_timer(sess);
4361 spin_unlock_bh(&se_tpg->session_lock);
4362
4363 /*
4364 * transport_deregister_session_configfs() will clear the
4365 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4366 * can be setting it again with __transport_register_session() in
4367 * iscsi_post_login_handler() again after the iscsit_stop_session()
4368 * completes in iscsi_np context.
4369 */
4370 transport_deregister_session_configfs(sess->se_sess);
4371
4372 /*
4373 * If any other processes are accessing this session pointer we must
4374 * wait until they have completed. If we are in an interrupt (the
4375 * time2retain handler) and contain and active session usage count we
4376 * restart the timer and exit.
4377 */
4378 if (!in_interrupt()) {
4379 if (iscsit_check_session_usage_count(sess) == 1)
4380 iscsit_stop_session(sess, 1, 1);
4381 } else {
4382 if (iscsit_check_session_usage_count(sess) == 2) {
4383 atomic_set(&sess->session_logout, 0);
4384 iscsit_start_time2retain_handler(sess);
4385 return 0;
4386 }
4387 }
4388
4389 transport_deregister_session(sess->se_sess);
4390
4391 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4392 iscsit_free_connection_recovery_entires(sess);
4393
4394 iscsit_free_all_ooo_cmdsns(sess);
4395
4396 spin_lock_bh(&se_tpg->session_lock);
4397 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4398 sess->session_state = TARG_SESS_STATE_FREE;
4399 pr_debug("Released iSCSI session from node: %s\n",
4400 sess->sess_ops->InitiatorName);
4401 tpg->nsessions--;
4402 if (tpg->tpg_tiqn)
4403 tpg->tpg_tiqn->tiqn_nsessions--;
4404
4405 pr_debug("Decremented number of active iSCSI Sessions on"
4406 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4407
4408 spin_lock(&sess_idr_lock);
4409 idr_remove(&sess_idr, sess->session_index);
4410 spin_unlock(&sess_idr_lock);
4411
4412 kfree(sess->sess_ops);
4413 sess->sess_ops = NULL;
4414 spin_unlock_bh(&se_tpg->session_lock);
4415
4416 kfree(sess);
4417 return 0;
4418}
4419
4420static void iscsit_logout_post_handler_closesession(
4421 struct iscsi_conn *conn)
4422{
4423 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004424 int sleep = 1;
4425 /*
4426 * Traditional iscsi/tcp will invoke this logic from TX thread
4427 * context during session logout, so clear tx_thread_active and
4428 * sleep if iscsit_close_connection() has not already occured.
4429 *
4430 * Since iser-target invokes this logic from it's own workqueue,
4431 * always sleep waiting for RX/TX thread shutdown to complete
4432 * within iscsit_close_connection().
4433 */
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004434 if (!conn->conn_transport->rdma_shutdown) {
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004435 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004436 if (!sleep)
4437 return;
4438 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004439
4440 atomic_set(&conn->conn_logout_remove, 0);
4441 complete(&conn->conn_logout_comp);
4442
4443 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004444 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004445 iscsit_dec_session_usage_count(sess);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004446 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004447}
4448
4449static void iscsit_logout_post_handler_samecid(
4450 struct iscsi_conn *conn)
4451{
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004452 int sleep = 1;
4453
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004454 if (!conn->conn_transport->rdma_shutdown) {
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004455 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004456 if (!sleep)
4457 return;
4458 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004459
4460 atomic_set(&conn->conn_logout_remove, 0);
4461 complete(&conn->conn_logout_comp);
4462
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004463 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004464 iscsit_dec_conn_usage_count(conn);
4465}
4466
4467static void iscsit_logout_post_handler_diffcid(
4468 struct iscsi_conn *conn,
4469 u16 cid)
4470{
4471 struct iscsi_conn *l_conn;
4472 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004473 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004474
4475 if (!sess)
4476 return;
4477
4478 spin_lock_bh(&sess->conn_lock);
4479 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4480 if (l_conn->cid == cid) {
4481 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004482 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004483 break;
4484 }
4485 }
4486 spin_unlock_bh(&sess->conn_lock);
4487
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004488 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004489 return;
4490
4491 if (l_conn->sock)
4492 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4493
4494 spin_lock_bh(&l_conn->state_lock);
4495 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4496 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4497 spin_unlock_bh(&l_conn->state_lock);
4498
4499 iscsit_cause_connection_reinstatement(l_conn, 1);
4500 iscsit_dec_conn_usage_count(l_conn);
4501}
4502
4503/*
4504 * Return of 0 causes the TX thread to restart.
4505 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004506int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004507 struct iscsi_cmd *cmd,
4508 struct iscsi_conn *conn)
4509{
4510 int ret = 0;
4511
4512 switch (cmd->logout_reason) {
4513 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4514 switch (cmd->logout_response) {
4515 case ISCSI_LOGOUT_SUCCESS:
4516 case ISCSI_LOGOUT_CLEANUP_FAILED:
4517 default:
4518 iscsit_logout_post_handler_closesession(conn);
4519 break;
4520 }
4521 ret = 0;
4522 break;
4523 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4524 if (conn->cid == cmd->logout_cid) {
4525 switch (cmd->logout_response) {
4526 case ISCSI_LOGOUT_SUCCESS:
4527 case ISCSI_LOGOUT_CLEANUP_FAILED:
4528 default:
4529 iscsit_logout_post_handler_samecid(conn);
4530 break;
4531 }
4532 ret = 0;
4533 } else {
4534 switch (cmd->logout_response) {
4535 case ISCSI_LOGOUT_SUCCESS:
4536 iscsit_logout_post_handler_diffcid(conn,
4537 cmd->logout_cid);
4538 break;
4539 case ISCSI_LOGOUT_CID_NOT_FOUND:
4540 case ISCSI_LOGOUT_CLEANUP_FAILED:
4541 default:
4542 break;
4543 }
4544 ret = 1;
4545 }
4546 break;
4547 case ISCSI_LOGOUT_REASON_RECOVERY:
4548 switch (cmd->logout_response) {
4549 case ISCSI_LOGOUT_SUCCESS:
4550 case ISCSI_LOGOUT_CID_NOT_FOUND:
4551 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4552 case ISCSI_LOGOUT_CLEANUP_FAILED:
4553 default:
4554 break;
4555 }
4556 ret = 1;
4557 break;
4558 default:
4559 break;
4560
4561 }
4562 return ret;
4563}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004564EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004565
4566void iscsit_fail_session(struct iscsi_session *sess)
4567{
4568 struct iscsi_conn *conn;
4569
4570 spin_lock_bh(&sess->conn_lock);
4571 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4572 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4573 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4574 }
4575 spin_unlock_bh(&sess->conn_lock);
4576
4577 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4578 sess->session_state = TARG_SESS_STATE_FAILED;
4579}
4580
4581int iscsit_free_session(struct iscsi_session *sess)
4582{
4583 u16 conn_count = atomic_read(&sess->nconn);
4584 struct iscsi_conn *conn, *conn_tmp = NULL;
4585 int is_last;
4586
4587 spin_lock_bh(&sess->conn_lock);
4588 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4589
4590 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4591 conn_list) {
4592 if (conn_count == 0)
4593 break;
4594
4595 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4596 is_last = 1;
4597 } else {
4598 iscsit_inc_conn_usage_count(conn_tmp);
4599 is_last = 0;
4600 }
4601 iscsit_inc_conn_usage_count(conn);
4602
4603 spin_unlock_bh(&sess->conn_lock);
4604 iscsit_cause_connection_reinstatement(conn, 1);
4605 spin_lock_bh(&sess->conn_lock);
4606
4607 iscsit_dec_conn_usage_count(conn);
4608 if (is_last == 0)
4609 iscsit_dec_conn_usage_count(conn_tmp);
4610
4611 conn_count--;
4612 }
4613
4614 if (atomic_read(&sess->nconn)) {
4615 spin_unlock_bh(&sess->conn_lock);
4616 wait_for_completion(&sess->session_wait_comp);
4617 } else
4618 spin_unlock_bh(&sess->conn_lock);
4619
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004620 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004621 return 0;
4622}
4623
4624void iscsit_stop_session(
4625 struct iscsi_session *sess,
4626 int session_sleep,
4627 int connection_sleep)
4628{
4629 u16 conn_count = atomic_read(&sess->nconn);
4630 struct iscsi_conn *conn, *conn_tmp = NULL;
4631 int is_last;
4632
4633 spin_lock_bh(&sess->conn_lock);
4634 if (session_sleep)
4635 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4636
4637 if (connection_sleep) {
4638 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4639 conn_list) {
4640 if (conn_count == 0)
4641 break;
4642
4643 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4644 is_last = 1;
4645 } else {
4646 iscsit_inc_conn_usage_count(conn_tmp);
4647 is_last = 0;
4648 }
4649 iscsit_inc_conn_usage_count(conn);
4650
4651 spin_unlock_bh(&sess->conn_lock);
4652 iscsit_cause_connection_reinstatement(conn, 1);
4653 spin_lock_bh(&sess->conn_lock);
4654
4655 iscsit_dec_conn_usage_count(conn);
4656 if (is_last == 0)
4657 iscsit_dec_conn_usage_count(conn_tmp);
4658 conn_count--;
4659 }
4660 } else {
4661 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4662 iscsit_cause_connection_reinstatement(conn, 0);
4663 }
4664
4665 if (session_sleep && atomic_read(&sess->nconn)) {
4666 spin_unlock_bh(&sess->conn_lock);
4667 wait_for_completion(&sess->session_wait_comp);
4668 } else
4669 spin_unlock_bh(&sess->conn_lock);
4670}
4671
4672int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4673{
4674 struct iscsi_session *sess;
4675 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4676 struct se_session *se_sess, *se_sess_tmp;
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004677 LIST_HEAD(free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004678 int session_count = 0;
4679
4680 spin_lock_bh(&se_tpg->session_lock);
4681 if (tpg->nsessions && !force) {
4682 spin_unlock_bh(&se_tpg->session_lock);
4683 return -1;
4684 }
4685
4686 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4687 sess_list) {
4688 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4689
4690 spin_lock(&sess->conn_lock);
4691 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4692 atomic_read(&sess->session_logout) ||
4693 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4694 spin_unlock(&sess->conn_lock);
4695 continue;
4696 }
4697 atomic_set(&sess->session_reinstatement, 1);
Nicholas Bellinger197b8062017-04-25 10:55:12 -07004698 atomic_set(&sess->session_fall_back_to_erl0, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004699 spin_unlock(&sess->conn_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004700
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004701 list_move_tail(&se_sess->sess_list, &free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004702 }
4703 spin_unlock_bh(&se_tpg->session_lock);
4704
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004705 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4706 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4707
4708 iscsit_free_session(sess);
4709 session_count++;
4710 }
4711
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004712 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4713 " Group: %hu\n", session_count, tpg->tpgt);
4714 return 0;
4715}
4716
4717MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4718MODULE_VERSION("4.1.x");
4719MODULE_AUTHOR("nab@Linux-iSCSI.org");
4720MODULE_LICENSE("GPL");
4721
4722module_init(iscsi_target_init_module);
4723module_exit(iscsi_target_cleanup_module);