blob: 12803de99400b148043879b552acbdaf58763f5a [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;
421
422 if (np->np_thread) {
423 spin_unlock_bh(&np->np_thread_lock);
424 send_sig(SIGINT, np->np_thread, 1);
425 wait_for_completion(&np->np_restart_comp);
426 spin_lock_bh(&np->np_thread_lock);
427 }
428 spin_unlock_bh(&np->np_thread_lock);
429
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700430 if (tpg_np && shutdown) {
431 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
432
433 wait_for_completion(&tpg_np->tpg_np_comp);
434 }
435
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000436 return 0;
437}
438
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800439static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000440{
Al Virobf6932f2012-07-21 08:55:18 +0100441 if (np->np_socket)
442 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000443}
444
445int iscsit_del_np(struct iscsi_np *np)
446{
447 spin_lock_bh(&np->np_thread_lock);
448 np->np_exports--;
449 if (np->np_exports) {
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700450 np->enabled = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000451 spin_unlock_bh(&np->np_thread_lock);
452 return 0;
453 }
454 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
455 spin_unlock_bh(&np->np_thread_lock);
456
457 if (np->np_thread) {
458 /*
459 * We need to send the signal to wakeup Linux/Net
460 * which may be sleeping in sock_accept()..
461 */
462 send_sig(SIGINT, np->np_thread, 1);
463 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800464 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000465 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800466
467 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000468
Andy Groveree291e62014-01-24 16:18:54 -0800469 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000470 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800471 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000472
Andy Grover69d75572015-08-24 10:26:04 -0700473 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
474 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000475
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800476 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000477 kfree(np);
478 return 0;
479}
480
Varun Prakashe8205cc2016-04-20 00:00:11 +0530481static void iscsit_get_rx_pdu(struct iscsi_conn *);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700482
Varun Prakashd2faaef2016-04-20 00:00:19 +0530483int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700484{
Nicholas Bellingera4467012016-10-30 17:30:08 -0700485 return iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700486}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530487EXPORT_SYMBOL(iscsit_queue_rsp);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700488
Varun Prakashd2faaef2016-04-20 00:00:19 +0530489void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700490{
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700491 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -0700492 if (!list_empty(&cmd->i_conn_node) &&
493 !(cmd->se_cmd.transport_state & CMD_T_FABRIC_STOP))
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700494 list_del_init(&cmd->i_conn_node);
495 spin_unlock_bh(&conn->cmd_lock);
496
Bart Van Assche4412a672017-05-23 16:48:43 -0700497 __iscsit_free_cmd(cmd, true);
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700498}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530499EXPORT_SYMBOL(iscsit_aborted_task);
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700500
Varun Prakash2854bb22016-04-20 00:00:08 +0530501static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
502 u32, u32, u8 *, u8 *);
503static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
504
505static int
506iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
507 const void *data_buf, u32 data_buf_len)
508{
509 struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
510 struct kvec *iov;
511 u32 niov = 0, tx_size = ISCSI_HDR_LEN;
512 int ret;
513
514 iov = &cmd->iov_misc[0];
515 iov[niov].iov_base = cmd->pdu;
516 iov[niov++].iov_len = ISCSI_HDR_LEN;
517
518 if (conn->conn_ops->HeaderDigest) {
519 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
520
521 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
522 ISCSI_HDR_LEN, 0, NULL,
523 (u8 *)header_digest);
524
525 iov[0].iov_len += ISCSI_CRC_LEN;
526 tx_size += ISCSI_CRC_LEN;
527 pr_debug("Attaching CRC32C HeaderDigest"
528 " to opcode 0x%x 0x%08x\n",
529 hdr->opcode, *header_digest);
530 }
531
532 if (data_buf_len) {
533 u32 padding = ((-data_buf_len) & 3);
534
535 iov[niov].iov_base = (void *)data_buf;
536 iov[niov++].iov_len = data_buf_len;
537 tx_size += data_buf_len;
538
539 if (padding != 0) {
540 iov[niov].iov_base = &cmd->pad_bytes;
541 iov[niov++].iov_len = padding;
542 tx_size += padding;
543 pr_debug("Attaching %u additional"
544 " padding bytes.\n", padding);
545 }
546
547 if (conn->conn_ops->DataDigest) {
548 iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
549 data_buf, data_buf_len,
550 padding,
551 (u8 *)&cmd->pad_bytes,
552 (u8 *)&cmd->data_crc);
553
554 iov[niov].iov_base = &cmd->data_crc;
555 iov[niov++].iov_len = ISCSI_CRC_LEN;
556 tx_size += ISCSI_CRC_LEN;
557 pr_debug("Attached DataDigest for %u"
558 " bytes opcode 0x%x, CRC 0x%08x\n",
559 data_buf_len, hdr->opcode, cmd->data_crc);
560 }
561 }
562
563 cmd->iov_misc_count = niov;
564 cmd->tx_size = tx_size;
565
566 ret = iscsit_send_tx_data(cmd, conn, 1);
567 if (ret < 0) {
568 iscsit_tx_thread_wait_for_tcp(conn);
569 return ret;
570 }
571
572 return 0;
573}
574
575static int iscsit_map_iovec(struct iscsi_cmd *, struct kvec *, u32, u32);
576static void iscsit_unmap_iovec(struct iscsi_cmd *);
577static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
578 u32, u32, u32, u8 *);
579static int
580iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
581 const struct iscsi_datain *datain)
582{
583 struct kvec *iov;
584 u32 iov_count = 0, tx_size = 0;
585 int ret, iov_ret;
586
587 iov = &cmd->iov_data[0];
588 iov[iov_count].iov_base = cmd->pdu;
589 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
590 tx_size += ISCSI_HDR_LEN;
591
592 if (conn->conn_ops->HeaderDigest) {
593 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
594
595 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
596 ISCSI_HDR_LEN, 0, NULL,
597 (u8 *)header_digest);
598
599 iov[0].iov_len += ISCSI_CRC_LEN;
600 tx_size += ISCSI_CRC_LEN;
601
602 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
603 *header_digest);
604 }
605
606 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
607 datain->offset, datain->length);
608 if (iov_ret < 0)
609 return -1;
610
611 iov_count += iov_ret;
612 tx_size += datain->length;
613
614 cmd->padding = ((-datain->length) & 3);
615 if (cmd->padding) {
616 iov[iov_count].iov_base = cmd->pad_bytes;
617 iov[iov_count++].iov_len = cmd->padding;
618 tx_size += cmd->padding;
619
620 pr_debug("Attaching %u padding bytes\n", cmd->padding);
621 }
622
623 if (conn->conn_ops->DataDigest) {
624 cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
625 cmd, datain->offset,
626 datain->length,
627 cmd->padding,
628 cmd->pad_bytes);
629
630 iov[iov_count].iov_base = &cmd->data_crc;
631 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
632 tx_size += ISCSI_CRC_LEN;
633
634 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
635 datain->length + cmd->padding, cmd->data_crc);
636 }
637
638 cmd->iov_data_count = iov_count;
639 cmd->tx_size = tx_size;
640
641 ret = iscsit_fe_sendpage_sg(cmd, conn);
642
643 iscsit_unmap_iovec(cmd);
644
645 if (ret < 0) {
646 iscsit_tx_thread_wait_for_tcp(conn);
647 return ret;
648 }
649
650 return 0;
651}
652
653static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
654 struct iscsi_datain_req *dr, const void *buf,
655 u32 buf_len)
656{
657 if (dr)
658 return iscsit_xmit_datain_pdu(conn, cmd, buf);
659 else
660 return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
661}
662
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700663static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
664{
665 return TARGET_PROT_NORMAL;
666}
667
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800668static struct iscsit_transport iscsi_target_transport = {
669 .name = "iSCSI/TCP",
670 .transport_type = ISCSI_TCP,
Nicholas Bellingerbd027d82016-05-14 22:23:34 -0700671 .rdma_shutdown = false,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800672 .owner = NULL,
673 .iscsit_setup_np = iscsit_setup_np,
674 .iscsit_accept_np = iscsit_accept_np,
675 .iscsit_free_np = iscsit_free_np,
676 .iscsit_get_login_rx = iscsit_get_login_rx,
677 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800678 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700679 .iscsit_immediate_queue = iscsit_immediate_queue,
680 .iscsit_response_queue = iscsit_response_queue,
681 .iscsit_queue_data_in = iscsit_queue_rsp,
682 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700683 .iscsit_aborted_task = iscsit_aborted_task,
Varun Prakash2854bb22016-04-20 00:00:08 +0530684 .iscsit_xmit_pdu = iscsit_xmit_pdu,
Varun Prakashe8205cc2016-04-20 00:00:11 +0530685 .iscsit_get_rx_pdu = iscsit_get_rx_pdu,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700686 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800687};
688
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000689static int __init iscsi_target_init_module(void)
690{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800691 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000692
693 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
Markus Elfring3829f382017-04-09 16:00:39 +0200694 iscsit_global = kzalloc(sizeof(*iscsit_global), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200695 if (!iscsit_global)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000696 return -1;
Markus Elfringc46e22f2017-04-09 15:34:50 +0200697
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800698 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000699 mutex_init(&auth_id_lock);
700 spin_lock_init(&sess_idr_lock);
701 idr_init(&tiqn_idr);
702 idr_init(&sess_idr);
703
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200704 ret = target_register_template(&iscsi_ops);
705 if (ret)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000706 goto out;
707
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800708 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
709 iscsit_global->ts_bitmap = vzalloc(size);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200710 if (!iscsit_global->ts_bitmap)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000711 goto configfs_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000712
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000713 lio_qr_cache = kmem_cache_create("lio_qr_cache",
714 sizeof(struct iscsi_queue_req),
715 __alignof__(struct iscsi_queue_req), 0, NULL);
716 if (!lio_qr_cache) {
717 pr_err("nable to kmem_cache_create() for"
718 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800719 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000720 }
721
722 lio_dr_cache = kmem_cache_create("lio_dr_cache",
723 sizeof(struct iscsi_datain_req),
724 __alignof__(struct iscsi_datain_req), 0, NULL);
725 if (!lio_dr_cache) {
726 pr_err("Unable to kmem_cache_create() for"
727 " lio_dr_cache\n");
728 goto qr_out;
729 }
730
731 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
732 sizeof(struct iscsi_ooo_cmdsn),
733 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
734 if (!lio_ooo_cache) {
735 pr_err("Unable to kmem_cache_create() for"
736 " lio_ooo_cache\n");
737 goto dr_out;
738 }
739
740 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
741 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
742 0, NULL);
743 if (!lio_r2t_cache) {
744 pr_err("Unable to kmem_cache_create() for"
745 " lio_r2t_cache\n");
746 goto ooo_out;
747 }
748
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800749 iscsit_register_transport(&iscsi_target_transport);
750
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000751 if (iscsit_load_discovery_tpg() < 0)
752 goto r2t_out;
753
754 return ret;
755r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100756 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000757 kmem_cache_destroy(lio_r2t_cache);
758ooo_out:
759 kmem_cache_destroy(lio_ooo_cache);
760dr_out:
761 kmem_cache_destroy(lio_dr_cache);
762qr_out:
763 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800764bitmap_out:
765 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000766configfs_out:
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200767 /* XXX: this probably wants it to be it's own unwind step.. */
768 if (iscsit_global->discovery_tpg)
769 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
770 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000771out:
772 kfree(iscsit_global);
773 return -ENOMEM;
774}
775
776static void __exit iscsi_target_cleanup_module(void)
777{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000778 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800779 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000780 kmem_cache_destroy(lio_qr_cache);
781 kmem_cache_destroy(lio_dr_cache);
782 kmem_cache_destroy(lio_ooo_cache);
783 kmem_cache_destroy(lio_r2t_cache);
784
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200785 /*
786 * Shutdown discovery sessions and disable discovery TPG
787 */
788 if (iscsit_global->discovery_tpg)
789 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000790
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200791 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000792
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800793 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000794 kfree(iscsit_global);
795}
796
Varun Prakashd2faaef2016-04-20 00:00:19 +0530797int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700798 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000799 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700800 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000801{
802 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000803
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000804 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000805 if (!cmd)
806 return -1;
807
808 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700809 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000810
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100811 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000812 if (!cmd->buf_ptr) {
813 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700814 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000815 return -1;
816 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000817
818 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700819 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000820 spin_unlock_bh(&conn->cmd_lock);
821
822 cmd->i_state = ISTATE_SEND_REJECT;
823 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
824
Nicholas Bellingerba159912013-07-03 03:48:24 -0700825 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000826}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530827EXPORT_SYMBOL(iscsit_add_reject);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000828
Nicholas Bellingerba159912013-07-03 03:48:24 -0700829static int iscsit_add_reject_from_cmd(
830 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000831 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700832 bool add_to_conn,
833 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000834{
835 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000836
837 if (!cmd->conn) {
838 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
839 cmd->init_task_tag);
840 return -1;
841 }
842 conn = cmd->conn;
843
844 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700845 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000846
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100847 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000848 if (!cmd->buf_ptr) {
849 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700850 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000851 return -1;
852 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000853
854 if (add_to_conn) {
855 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700856 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000857 spin_unlock_bh(&conn->cmd_lock);
858 }
859
860 cmd->i_state = ISTATE_SEND_REJECT;
861 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800862 /*
863 * Perform the kref_put now if se_cmd has already been setup by
864 * scsit_setup_scsi_cmd()
865 */
866 if (cmd->se_cmd.se_tfo != NULL) {
867 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +0200868 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800869 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700870 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000871}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700872
873static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
874 unsigned char *buf)
875{
876 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
877}
878
879int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
880{
881 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
882}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530883EXPORT_SYMBOL(iscsit_reject_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000884
885/*
886 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700887 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000888 */
889static int iscsit_map_iovec(
890 struct iscsi_cmd *cmd,
891 struct kvec *iov,
892 u32 data_offset,
893 u32 data_length)
894{
895 u32 i = 0;
896 struct scatterlist *sg;
897 unsigned int page_off;
898
899 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700900 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000901 */
Imran Haider2b165092016-05-08 11:17:54 -0400902 u32 ent = data_offset / PAGE_SIZE;
903
904 if (ent >= cmd->se_cmd.t_data_nents) {
905 pr_err("Initial page entry out-of-bounds\n");
906 return -1;
907 }
908
909 sg = &cmd->se_cmd.t_data_sg[ent];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000910 page_off = (data_offset % PAGE_SIZE);
911
912 cmd->first_data_sg = sg;
913 cmd->first_data_sg_off = page_off;
914
915 while (data_length) {
916 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
917
918 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
919 iov[i].iov_len = cur_len;
920
921 data_length -= cur_len;
922 page_off = 0;
923 sg = sg_next(sg);
924 i++;
925 }
926
927 cmd->kmapped_nents = i;
928
929 return i;
930}
931
932static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
933{
934 u32 i;
935 struct scatterlist *sg;
936
937 sg = cmd->first_data_sg;
938
939 for (i = 0; i < cmd->kmapped_nents; i++)
940 kunmap(sg_page(&sg[i]));
941}
942
943static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
944{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700945 LIST_HEAD(ack_list);
946 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000947
948 conn->exp_statsn = exp_statsn;
949
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800950 if (conn->sess->sess_ops->RDMAExtensions)
951 return;
952
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000953 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700954 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000955 spin_lock(&cmd->istate_lock);
956 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800957 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000958 cmd->i_state = ISTATE_REMOVE;
959 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700960 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000961 continue;
962 }
963 spin_unlock(&cmd->istate_lock);
964 }
965 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700966
967 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800968 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700969 iscsit_free_cmd(cmd, false);
970 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000971}
972
973static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
974{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700975 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000976
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400977 iov_count += ISCSI_IOV_DATA_BUFFER;
Markus Elfringf1725112017-04-09 15:06:00 +0200978 cmd->iov_data = kcalloc(iov_count, sizeof(*cmd->iov_data), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200979 if (!cmd->iov_data)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000980 return -ENOMEM;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000981
982 cmd->orig_iov_data_count = iov_count;
983 return 0;
984}
985
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800986int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
987 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000988{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800989 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000990 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700991 int iscsi_task_attr;
992 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000993
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800994 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000995
996 hdr = (struct iscsi_scsi_req *) buf;
997 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000998
999 /* FIXME; Add checks for AdditionalHeaderSegment */
1000
1001 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
1002 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1003 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1004 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001005 return iscsit_add_reject_cmd(cmd,
1006 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001007 }
1008
1009 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1010 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1011 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001012 * From RFC-3720 Section 10.3.1:
1013 *
1014 * "Either or both of R and W MAY be 1 when either the
1015 * Expected Data Transfer Length and/or Bidirectional Read
1016 * Expected Data Transfer Length are 0"
1017 *
1018 * For this case, go ahead and clear the unnecssary bits
1019 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001020 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001021 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1022 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001023
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001024 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001025 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001026 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001027 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001028
1029 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1030 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1031 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1032 " MUST be set if Expected Data Transfer Length is not 0."
1033 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001034 return iscsit_add_reject_cmd(cmd,
1035 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001036 }
1037
1038 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1039 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1040 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001041 return iscsit_add_reject_cmd(cmd,
1042 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001043 }
1044
1045 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1046 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1047 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001048 return iscsit_add_reject_cmd(cmd,
1049 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001050 }
1051
1052 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1053 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1054 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001055 return iscsit_add_reject_cmd(cmd,
1056 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001057 }
1058
Nicholas Bellingerba159912013-07-03 03:48:24 -07001059 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001060 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1061 pr_err("Expected Data Transfer Length and Length of"
1062 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1063 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001064 return iscsit_add_reject_cmd(cmd,
1065 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001066 }
1067
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001068 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001069 pr_err("DataSegmentLength: %u is greater than"
1070 " EDTL: %u, protocol error.\n", payload_length,
1071 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001072 return iscsit_add_reject_cmd(cmd,
1073 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001074 }
1075
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001076 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001077 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001078 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1079 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001080 return iscsit_add_reject_cmd(cmd,
1081 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001082 }
1083
1084 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1085 pr_err("DataSegmentLength: %u is greater than"
1086 " FirstBurstLength: %u, protocol error.\n",
1087 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001088 return iscsit_add_reject_cmd(cmd,
1089 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001090 }
1091
1092 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1093 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1094 DMA_NONE;
1095
Andy Groverd28b11692012-04-03 15:51:22 -07001096 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -07001097 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1098 /*
1099 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1100 */
1101 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1102 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001103 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001104 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001105 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001106 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001107 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001108 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001109 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001110 else {
1111 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001112 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
1113 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001114 }
1115
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001116 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
1117 cmd->i_state = ISTATE_NEW_CMD;
1118 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1119 cmd->immediate_data = (payload_length) ? 1 : 0;
1120 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1121 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1122 if (cmd->unsolicited_data)
1123 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1124
1125 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
Alexei Potashnik95473082015-07-21 15:07:56 -07001126 if (hdr->flags & ISCSI_FLAG_CMD_READ)
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02001127 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Alexei Potashnik95473082015-07-21 15:07:56 -07001128 else
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001129 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001130 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1131 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001132 cmd->first_burst_len = payload_length;
1133
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001134 if (!conn->sess->sess_ops->RDMAExtensions &&
1135 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001136 struct iscsi_datain_req *dr;
1137
1138 dr = iscsit_allocate_datain_req();
1139 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001140 return iscsit_add_reject_cmd(cmd,
1141 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001142
1143 iscsit_attach_datain_req(cmd, dr);
1144 }
1145
1146 /*
Andy Grover065ca1e2012-04-03 15:51:23 -07001147 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1148 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001149 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001150 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1151 cmd->data_direction, sam_task_attr,
1152 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -07001153
1154 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1155 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001156 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1157 conn->cid);
1158
Bart Van Asscheafc16602015-04-27 13:52:36 +02001159 target_get_sess_cmd(&cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001160
Christoph Hellwigde103c92012-11-06 12:24:09 -08001161 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1162 scsilun_to_int(&hdr->lun));
1163 if (cmd->sense_reason)
1164 goto attach_cmd;
1165
Bart Van Assche649ee052015-04-14 13:26:44 +02001166 /* only used for printks or comparing with ->ref_task_tag */
1167 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001168 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1169 if (cmd->sense_reason) {
1170 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001171 return iscsit_add_reject_cmd(cmd,
1172 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001173 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001174
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001175 goto attach_cmd;
1176 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001177
Christoph Hellwigde103c92012-11-06 12:24:09 -08001178 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001179 return iscsit_add_reject_cmd(cmd,
1180 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001181 }
1182
1183attach_cmd:
1184 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001185 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001186 spin_unlock_bh(&conn->cmd_lock);
1187 /*
1188 * Check if we need to delay processing because of ALUA
1189 * Active/NonOptimized primary access state..
1190 */
1191 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001192
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001193 return 0;
1194}
1195EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001196
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001197void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1198{
1199 iscsit_set_dataout_sequence_values(cmd);
1200
1201 spin_lock_bh(&cmd->dataout_timeout_lock);
1202 iscsit_start_dataout_timer(cmd, cmd->conn);
1203 spin_unlock_bh(&cmd->dataout_timeout_lock);
1204}
1205EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1206
1207int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1208 struct iscsi_scsi_req *hdr)
1209{
1210 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001211 /*
1212 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1213 * the Immediate Bit is not set, and no Immediate
1214 * Data is attached.
1215 *
1216 * A PDU/CmdSN carrying Immediate Data can only
1217 * be processed after the DataCRC has passed.
1218 * If the DataCRC fails, the CmdSN MUST NOT
1219 * be acknowledged. (See below)
1220 */
1221 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001222 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1223 (unsigned char *)hdr, hdr->cmdsn);
1224 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1225 return -1;
1226 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001227 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001228 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001229 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001230 }
1231
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001232 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001233
1234 /*
1235 * If no Immediate Data is attached, it's OK to return now.
1236 */
1237 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001238 if (!cmd->sense_reason && cmd->unsolicited_data)
1239 iscsit_set_unsoliticed_dataout(cmd);
1240 if (!cmd->sense_reason)
1241 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001242
Bart Van Asscheafc16602015-04-27 13:52:36 +02001243 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001244 return 0;
1245 }
1246
1247 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001248 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1249 * execution. These exceptions are processed in CmdSN order using
1250 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001251 */
Bart Van Assche8fa40112017-05-23 16:48:45 -07001252 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001253 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001254 /*
1255 * Call directly into transport_generic_new_cmd() to perform
1256 * the backend memory allocation.
1257 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001258 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001259 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001260 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001261
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001262 return 0;
1263}
1264EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1265
1266static int
1267iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1268 bool dump_payload)
1269{
1270 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1271 /*
1272 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1273 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001274 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001275 goto after_immediate_data;
Nicholas Bellingerabb85a92017-06-07 20:29:50 -07001276 /*
1277 * Check for underflow case where both EDTL and immediate data payload
1278 * exceeds what is presented by CDB's TRANSFER LENGTH, and what has
1279 * already been set in target_cmd_size_check() as se_cmd->data_length.
1280 *
1281 * For this special case, fail the command and dump the immediate data
1282 * payload.
1283 */
1284 if (cmd->first_burst_len > cmd->se_cmd.data_length) {
1285 cmd->sense_reason = TCM_INVALID_CDB_FIELD;
1286 goto after_immediate_data;
1287 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001288
1289 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1290 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001291after_immediate_data:
1292 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1293 /*
1294 * A PDU/CmdSN carrying Immediate Data passed
1295 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1296 * Immediate Bit is not set.
1297 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001298 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1299 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001300 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001301 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001302
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001303 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001304 int rc;
1305
1306 rc = iscsit_dump_data_payload(cmd->conn,
1307 cmd->first_burst_len, 1);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001308 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001309 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001310 } else if (cmd->unsolicited_data)
1311 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001312
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001313 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1314 /*
1315 * Immediate Data failed DataCRC and ERL>=1,
1316 * silently drop this PDU and let the initiator
1317 * plug the CmdSN gap.
1318 *
1319 * FIXME: Send Unsolicited NOPIN with reserved
1320 * TTT here to help the initiator figure out
1321 * the missing CmdSN, although they should be
1322 * intelligent enough to determine the missing
1323 * CmdSN and issue a retry to plug the sequence.
1324 */
1325 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001326 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001327 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1328 return -1;
1329
1330 return 0;
1331}
1332
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001333static int
1334iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1335 unsigned char *buf)
1336{
1337 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1338 int rc, immed_data;
1339 bool dump_payload = false;
1340
1341 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1342 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001343 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001344 /*
1345 * Allocation iovecs needed for struct socket operations for
1346 * traditional iSCSI block I/O.
1347 */
1348 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001349 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001350 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001351 }
1352 immed_data = cmd->immediate_data;
1353
1354 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1355 if (rc < 0)
1356 return rc;
1357 else if (rc > 0)
1358 dump_payload = true;
1359
1360 if (!immed_data)
1361 return 0;
1362
1363 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1364}
1365
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001366static u32 iscsit_do_crypto_hash_sg(
Herbert Xu69110e32016-01-24 21:19:52 +08001367 struct ahash_request *hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001368 struct iscsi_cmd *cmd,
1369 u32 data_offset,
1370 u32 data_length,
1371 u32 padding,
1372 u8 *pad_bytes)
1373{
1374 u32 data_crc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001375 struct scatterlist *sg;
1376 unsigned int page_off;
1377
Herbert Xu69110e32016-01-24 21:19:52 +08001378 crypto_ahash_init(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001379
1380 sg = cmd->first_data_sg;
1381 page_off = cmd->first_data_sg_off;
1382
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001383 while (data_length) {
Alexei Potashnikaa756792015-07-20 17:12:12 -07001384 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001385
Herbert Xu69110e32016-01-24 21:19:52 +08001386 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1387 crypto_ahash_update(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001388
1389 data_length -= cur_len;
1390 page_off = 0;
Alexei Potashnikaa756792015-07-20 17:12:12 -07001391 /* iscsit_map_iovec has already checked for invalid sg pointers */
1392 sg = sg_next(sg);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001393 }
1394
1395 if (padding) {
1396 struct scatterlist pad_sg;
1397
1398 sg_init_one(&pad_sg, pad_bytes, padding);
Herbert Xu69110e32016-01-24 21:19:52 +08001399 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1400 padding);
1401 crypto_ahash_finup(hash);
1402 } else {
1403 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1404 crypto_ahash_final(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001405 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001406
1407 return data_crc;
1408}
1409
1410static void iscsit_do_crypto_hash_buf(
Herbert Xu69110e32016-01-24 21:19:52 +08001411 struct ahash_request *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001412 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001413 u32 payload_length,
1414 u32 padding,
1415 u8 *pad_bytes,
1416 u8 *data_crc)
1417{
Herbert Xu69110e32016-01-24 21:19:52 +08001418 struct scatterlist sg[2];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001419
Herbert Xu69110e32016-01-24 21:19:52 +08001420 sg_init_table(sg, ARRAY_SIZE(sg));
1421 sg_set_buf(sg, buf, payload_length);
1422 sg_set_buf(sg + 1, pad_bytes, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001423
Herbert Xu69110e32016-01-24 21:19:52 +08001424 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001425
Herbert Xu69110e32016-01-24 21:19:52 +08001426 crypto_ahash_digest(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001427}
1428
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001429int
Varun Prakash9a584bf2017-01-13 20:53:21 +05301430__iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1431 struct iscsi_cmd *cmd, u32 payload_length,
1432 bool *success)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001433{
Varun Prakash9a584bf2017-01-13 20:53:21 +05301434 struct iscsi_data *hdr = buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001435 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001436 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001437
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001438 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001439 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001440
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001441 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1442 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001443 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001444 payload_length, conn->cid);
1445
1446 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1447 pr_err("Command ITT: 0x%08x received DataOUT after"
1448 " last DataOUT received, dumping payload\n",
1449 cmd->init_task_tag);
1450 return iscsit_dump_data_payload(conn, payload_length, 1);
1451 }
1452
1453 if (cmd->data_direction != DMA_TO_DEVICE) {
1454 pr_err("Command ITT: 0x%08x received DataOUT for a"
1455 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001456 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001457 }
1458 se_cmd = &cmd->se_cmd;
1459 iscsit_mod_dataout_timer(cmd);
1460
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001461 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001462 pr_err("DataOut Offset: %u, Length %u greater than"
1463 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001464 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001465 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001466 }
1467
1468 if (cmd->unsolicited_data) {
1469 int dump_unsolicited_data = 0;
1470
1471 if (conn->sess->sess_ops->InitialR2T) {
1472 pr_err("Received unexpected unsolicited data"
1473 " while InitialR2T=Yes, protocol error.\n");
1474 transport_send_check_condition_and_sense(&cmd->se_cmd,
1475 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1476 return -1;
1477 }
1478 /*
1479 * Special case for dealing with Unsolicited DataOUT
1480 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1481 * failures;
1482 */
1483
1484 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001485 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001486 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001487 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001488
1489 if (dump_unsolicited_data) {
1490 /*
1491 * Check if a delayed TASK_ABORTED status needs to
1492 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
Bart Van Assche5a342522015-10-22 15:53:22 -07001493 * received with the unsolicited data out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001494 */
1495 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1496 iscsit_stop_dataout_timer(cmd);
1497
1498 transport_check_aborted_status(se_cmd,
1499 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1500 return iscsit_dump_data_payload(conn, payload_length, 1);
1501 }
1502 } else {
1503 /*
1504 * For the normal solicited data path:
1505 *
1506 * Check for a delayed TASK_ABORTED status and dump any
1507 * incoming data out payload if one exists. Also, when the
1508 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1509 * data out sequence, we decrement outstanding_r2ts. Once
1510 * outstanding_r2ts reaches zero, go ahead and send the delayed
1511 * TASK_ABORTED status.
1512 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001513 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001514 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1515 if (--cmd->outstanding_r2ts < 1) {
1516 iscsit_stop_dataout_timer(cmd);
1517 transport_check_aborted_status(
1518 se_cmd, 1);
1519 }
1520
1521 return iscsit_dump_data_payload(conn, payload_length, 1);
1522 }
1523 }
1524 /*
Bart Van Assche0d5efb82016-12-23 14:37:52 +01001525 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001526 * within-command recovery checks before receiving the payload.
1527 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001528 rc = iscsit_check_pre_dataout(cmd, buf);
1529 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001530 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001531 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001532 return -1;
Varun Prakash9a584bf2017-01-13 20:53:21 +05301533 *success = true;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001534 return 0;
1535}
Varun Prakash9a584bf2017-01-13 20:53:21 +05301536EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
1537
1538int
1539iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1540 struct iscsi_cmd **out_cmd)
1541{
1542 struct iscsi_data *hdr = buf;
1543 struct iscsi_cmd *cmd;
1544 u32 payload_length = ntoh24(hdr->dlength);
1545 int rc;
1546 bool success = false;
1547
1548 if (!payload_length) {
1549 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1550 return 0;
1551 }
1552
1553 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1554 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1555 " MaxXmitDataSegmentLength: %u\n", payload_length,
1556 conn->conn_ops->MaxXmitDataSegmentLength);
1557 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
1558 }
1559
1560 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
1561 if (!cmd)
1562 return 0;
1563
1564 rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
1565
1566 if (success)
1567 *out_cmd = cmd;
1568
1569 return rc;
1570}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001571EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1572
1573static int
1574iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1575 struct iscsi_data *hdr)
1576{
1577 struct kvec *iov;
1578 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1579 u32 payload_length = ntoh24(hdr->dlength);
1580 int iov_ret, data_crc_failed = 0;
1581
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001582 rx_size += payload_length;
1583 iov = &cmd->iov_data[0];
1584
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001585 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1586 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001587 if (iov_ret < 0)
1588 return -1;
1589
1590 iov_count += iov_ret;
1591
1592 padding = ((-payload_length) & 3);
1593 if (padding != 0) {
1594 iov[iov_count].iov_base = cmd->pad_bytes;
1595 iov[iov_count++].iov_len = padding;
1596 rx_size += padding;
1597 pr_debug("Receiving %u padding bytes.\n", padding);
1598 }
1599
1600 if (conn->conn_ops->DataDigest) {
1601 iov[iov_count].iov_base = &checksum;
1602 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1603 rx_size += ISCSI_CRC_LEN;
1604 }
1605
1606 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1607
1608 iscsit_unmap_iovec(cmd);
1609
1610 if (rx_got != rx_size)
1611 return -1;
1612
1613 if (conn->conn_ops->DataDigest) {
1614 u32 data_crc;
1615
Herbert Xu69110e32016-01-24 21:19:52 +08001616 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001617 be32_to_cpu(hdr->offset),
1618 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001619 cmd->pad_bytes);
1620
1621 if (checksum != data_crc) {
1622 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1623 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1624 " does not match computed 0x%08x\n",
1625 hdr->itt, hdr->offset, payload_length,
1626 hdr->datasn, checksum, data_crc);
1627 data_crc_failed = 1;
1628 } else {
1629 pr_debug("Got CRC32C DataDigest 0x%08x for"
1630 " %u bytes of Data Out\n", checksum,
1631 payload_length);
1632 }
1633 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001634
1635 return data_crc_failed;
1636}
1637
1638int
1639iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1640 bool data_crc_failed)
1641{
1642 struct iscsi_conn *conn = cmd->conn;
1643 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001644 /*
1645 * Increment post receive data and CRC values or perform
1646 * within-command recovery.
1647 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001648 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1649 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001650 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001651 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001652 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001653 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1654 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001655 /*
1656 * Handle extra special case for out of order
1657 * Unsolicited Data Out.
1658 */
1659 spin_lock_bh(&cmd->istate_lock);
1660 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1661 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1662 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1663 spin_unlock_bh(&cmd->istate_lock);
1664
1665 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001666 if (ooo_cmdsn)
1667 return 0;
1668 target_execute_cmd(&cmd->se_cmd);
1669 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001670 } else /* DATAOUT_CANNOT_RECOVER */
1671 return -1;
1672
1673 return 0;
1674}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001675EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001676
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001677static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1678{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001679 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001680 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1681 int rc;
1682 bool data_crc_failed = false;
1683
1684 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1685 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001686 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001687 else if (!cmd)
1688 return 0;
1689
1690 rc = iscsit_get_dataout(conn, cmd, hdr);
1691 if (rc < 0)
1692 return rc;
1693 else if (rc > 0)
1694 data_crc_failed = true;
1695
1696 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1697}
1698
Nicholas Bellinger778de362013-06-14 16:07:47 -07001699int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1700 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001701{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001702 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001703
Arshad Hussaina3662602014-03-14 15:28:59 -07001704 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1705 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1706 if (!cmd)
1707 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1708 (unsigned char *)hdr);
1709
1710 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1711 (unsigned char *)hdr);
1712 }
1713
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001714 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001715 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1716 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001717 if (!cmd)
1718 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1719 (unsigned char *)hdr);
1720
Nicholas Bellingerba159912013-07-03 03:48:24 -07001721 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1722 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001723 }
1724
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001725 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001726 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001727 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001728 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001729 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001730 if (!cmd)
1731 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1732 (unsigned char *)hdr);
1733
Nicholas Bellingerba159912013-07-03 03:48:24 -07001734 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1735 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001736 }
1737
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001738 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001739 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001740 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001741 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1742 payload_length);
1743 /*
1744 * This is not a response to a Unsolicited NopIN, which means
1745 * it can either be a NOPOUT ping request (with a valid ITT),
1746 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1747 * Either way, make sure we allocate an struct iscsi_cmd, as both
1748 * can contain ping data.
1749 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001750 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001751 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1752 cmd->i_state = ISTATE_SEND_NOPIN;
1753 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1754 1 : 0);
1755 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1756 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001757 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1758 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001759 cmd->data_direction = DMA_NONE;
1760 }
1761
Nicholas Bellinger778de362013-06-14 16:07:47 -07001762 return 0;
1763}
1764EXPORT_SYMBOL(iscsit_setup_nop_out);
1765
1766int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1767 struct iscsi_nopout *hdr)
1768{
1769 struct iscsi_cmd *cmd_p = NULL;
1770 int cmdsn_ret = 0;
1771 /*
1772 * Initiator is expecting a NopIN ping reply..
1773 */
1774 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001775 if (!cmd)
1776 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1777 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001778
1779 spin_lock_bh(&conn->cmd_lock);
1780 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1781 spin_unlock_bh(&conn->cmd_lock);
1782
1783 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1784
1785 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1786 iscsit_add_cmd_to_response_queue(cmd, conn,
1787 cmd->i_state);
1788 return 0;
1789 }
1790
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001791 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1792 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001793 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1794 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001795 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001796 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001797
1798 return 0;
1799 }
1800 /*
1801 * This was a response to a unsolicited NOPIN ping.
1802 */
1803 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1804 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1805 if (!cmd_p)
1806 return -EINVAL;
1807
1808 iscsit_stop_nopin_response_timer(conn);
1809
1810 cmd_p->i_state = ISTATE_REMOVE;
1811 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1812
1813 iscsit_start_nopin_timer(conn);
1814 return 0;
1815 }
1816 /*
1817 * Otherwise, initiator is not expecting a NOPIN is response.
1818 * Just ignore for now.
1819 */
Varun Prakash1a40f0a2016-09-15 21:20:11 +05301820
1821 if (cmd)
1822 iscsit_free_cmd(cmd, false);
1823
Nicholas Bellinger778de362013-06-14 16:07:47 -07001824 return 0;
1825}
1826EXPORT_SYMBOL(iscsit_process_nop_out);
1827
1828static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1829 unsigned char *buf)
1830{
1831 unsigned char *ping_data = NULL;
1832 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1833 struct kvec *iov = NULL;
1834 u32 payload_length = ntoh24(hdr->dlength);
1835 int ret;
1836
1837 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1838 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001839 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001840 /*
1841 * Handle NOP-OUT payload for traditional iSCSI sockets
1842 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001843 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001844 u32 checksum, data_crc, padding = 0;
1845 int niov = 0, rx_got, rx_size = payload_length;
1846
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001847 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1848 if (!ping_data) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001849 ret = -1;
1850 goto out;
1851 }
1852
1853 iov = &cmd->iov_misc[0];
1854 iov[niov].iov_base = ping_data;
1855 iov[niov++].iov_len = payload_length;
1856
1857 padding = ((-payload_length) & 3);
1858 if (padding != 0) {
1859 pr_debug("Receiving %u additional bytes"
1860 " for padding.\n", padding);
1861 iov[niov].iov_base = &cmd->pad_bytes;
1862 iov[niov++].iov_len = padding;
1863 rx_size += padding;
1864 }
1865 if (conn->conn_ops->DataDigest) {
1866 iov[niov].iov_base = &checksum;
1867 iov[niov++].iov_len = ISCSI_CRC_LEN;
1868 rx_size += ISCSI_CRC_LEN;
1869 }
1870
1871 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1872 if (rx_got != rx_size) {
1873 ret = -1;
1874 goto out;
1875 }
1876
1877 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08001878 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001879 ping_data, payload_length,
1880 padding, cmd->pad_bytes,
1881 (u8 *)&data_crc);
1882
1883 if (checksum != data_crc) {
1884 pr_err("Ping data CRC32C DataDigest"
1885 " 0x%08x does not match computed 0x%08x\n",
1886 checksum, data_crc);
1887 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1888 pr_err("Unable to recover from"
1889 " NOPOUT Ping DataCRC failure while in"
1890 " ERL=0.\n");
1891 ret = -1;
1892 goto out;
1893 } else {
1894 /*
1895 * Silently drop this PDU and let the
1896 * initiator plug the CmdSN gap.
1897 */
1898 pr_debug("Dropping NOPOUT"
1899 " Command CmdSN: 0x%08x due to"
1900 " DataCRC error.\n", hdr->cmdsn);
1901 ret = 0;
1902 goto out;
1903 }
1904 } else {
1905 pr_debug("Got CRC32C DataDigest"
1906 " 0x%08x for %u bytes of ping data.\n",
1907 checksum, payload_length);
1908 }
1909 }
1910
1911 ping_data[payload_length] = '\0';
1912 /*
1913 * Attach ping data to struct iscsi_cmd->buf_ptr.
1914 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001915 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001916 cmd->buf_ptr_size = payload_length;
1917
1918 pr_debug("Got %u bytes of NOPOUT ping"
1919 " data.\n", payload_length);
1920 pr_debug("Ping Data: \"%s\"\n", ping_data);
1921 }
1922
Nicholas Bellinger778de362013-06-14 16:07:47 -07001923 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001924out:
1925 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001926 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001927
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001928 kfree(ping_data);
1929 return ret;
1930}
1931
Bart Van Asschee381fe92017-01-06 11:32:08 +01001932static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
1933{
1934 switch (iscsi_tmf) {
1935 case ISCSI_TM_FUNC_ABORT_TASK:
1936 return TMR_ABORT_TASK;
1937 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1938 return TMR_ABORT_TASK_SET;
1939 case ISCSI_TM_FUNC_CLEAR_ACA:
1940 return TMR_CLEAR_ACA;
1941 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1942 return TMR_CLEAR_TASK_SET;
1943 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1944 return TMR_LUN_RESET;
1945 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1946 return TMR_TARGET_WARM_RESET;
1947 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1948 return TMR_TARGET_COLD_RESET;
1949 default:
1950 return TMR_UNKNOWN;
1951 }
1952}
1953
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001954int
1955iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1956 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001957{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001958 struct se_tmr_req *se_tmr;
1959 struct iscsi_tmr_req *tmr_req;
1960 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001961 int out_of_order_cmdsn = 0, ret;
1962 bool sess_ref = false;
Bart Van Assche59b69862017-01-05 12:39:57 +01001963 u8 function, tcm_function = TMR_UNKNOWN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001964
1965 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001966 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1967 function = hdr->flags;
1968
1969 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1970 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1971 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1972 hdr->rtt, hdr->refcmdsn, conn->cid);
1973
1974 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1975 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001976 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001977 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001978 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001979 }
1980
1981 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1982 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1983 pr_err("Task Management Request TASK_REASSIGN not"
1984 " issued as immediate command, bad iSCSI Initiator"
1985 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001986 return iscsit_add_reject_cmd(cmd,
1987 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001988 }
1989 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001990 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1991 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001992
Andy Groverd28b11692012-04-03 15:51:22 -07001993 cmd->data_direction = DMA_NONE;
Markus Elfring3829f382017-04-09 16:00:39 +02001994 cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02001995 if (!cmd->tmr_req)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001996 return iscsit_add_reject_cmd(cmd,
1997 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1998 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001999
2000 /*
2001 * TASK_REASSIGN for ERL=2 / connection stays inside of
2002 * LIO-Target $FABRIC_MOD
2003 */
2004 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002005 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Andy Groverd28b11692012-04-03 15:51:22 -07002006 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08002007 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07002008
Bart Van Asscheafc16602015-04-27 13:52:36 +02002009 target_get_sess_cmd(&cmd->se_cmd, true);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002010 sess_ref = true;
Bart Van Asschee381fe92017-01-06 11:32:08 +01002011 tcm_function = iscsit_convert_tmf(function);
2012 if (tcm_function == TMR_UNKNOWN) {
Andy Groverd28b11692012-04-03 15:51:22 -07002013 pr_err("Unknown iSCSI TMR Function:"
2014 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002015 return iscsit_add_reject_cmd(cmd,
2016 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002017 }
Bart Van Assche59b69862017-01-05 12:39:57 +01002018 }
2019 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
2020 GFP_KERNEL);
2021 if (ret < 0)
2022 return iscsit_add_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002023 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002024
Bart Van Assche59b69862017-01-05 12:39:57 +01002025 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002026
2027 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2028 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2029 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2030 cmd->init_task_tag = hdr->itt;
2031 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002032 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2033 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002034 se_tmr = cmd->se_cmd.se_tmr_req;
2035 tmr_req = cmd->tmr_req;
2036 /*
2037 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2038 */
2039 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08002040 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
2041 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002042 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002043 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2044 goto attach;
2045 }
2046 }
2047
2048 switch (function) {
2049 case ISCSI_TM_FUNC_ABORT_TASK:
2050 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002051 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002052 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002053 break;
2054 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2055 case ISCSI_TM_FUNC_CLEAR_ACA:
2056 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2057 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2058 break;
2059 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2060 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002061 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2062 goto attach;
2063 }
2064 break;
2065 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2066 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002067 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2068 goto attach;
2069 }
2070 break;
2071 case ISCSI_TM_FUNC_TASK_REASSIGN:
2072 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2073 /*
2074 * Perform sanity checks on the ExpDataSN only if the
2075 * TASK_REASSIGN was successful.
2076 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002077 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002078 break;
2079
2080 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002081 return iscsit_add_reject_cmd(cmd,
2082 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002083 break;
2084 default:
2085 pr_err("Unknown TMR function: 0x%02x, protocol"
2086 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002087 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2088 goto attach;
2089 }
2090
2091 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2092 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2093 se_tmr->call_transport = 1;
2094attach:
2095 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002096 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002097 spin_unlock_bh(&conn->cmd_lock);
2098
2099 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002100 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002101 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
2102 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002103 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002104 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002105 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002106 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002107 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002108 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002109
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002110 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002111 return 0;
2112 /*
2113 * Found the referenced task, send to transport for processing.
2114 */
2115 if (se_tmr->call_transport)
2116 return transport_generic_handle_tmr(&cmd->se_cmd);
2117
2118 /*
2119 * Could not find the referenced LUN, task, or Task Management
2120 * command not authorized or supported. Change state and
2121 * let the tx_thread send the response.
2122 *
2123 * For connection recovery, this is also the default action for
2124 * TMR TASK_REASSIGN.
2125 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002126 if (sess_ref) {
2127 pr_debug("Handle TMR, using sess_ref=true check\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +02002128 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002129 }
2130
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002131 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2132 return 0;
2133}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002134EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002135
2136/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002137int
2138iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2139 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002140{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002141 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002142
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002143 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002144 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002145 "greater than MaxXmitDataSegmentLength %u.\n",
2146 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002147 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2148 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002149 }
2150
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08002151 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2152 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2153 pr_err("Multi sequence text commands currently not supported\n");
2154 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2155 (unsigned char *)hdr);
2156 }
2157
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002158 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2159 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2160 hdr->exp_statsn, payload_length);
2161
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002162 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2163 cmd->i_state = ISTATE_SEND_TEXTRSP;
2164 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2165 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2166 cmd->targ_xfer_tag = 0xFFFFFFFF;
2167 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2168 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2169 cmd->data_direction = DMA_NONE;
Varun Prakashea8dc5b2017-07-23 20:03:33 +05302170 kfree(cmd->text_in_ptr);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002171 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002172
2173 return 0;
2174}
2175EXPORT_SYMBOL(iscsit_setup_text_cmd);
2176
2177int
2178iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2179 struct iscsi_text *hdr)
2180{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002181 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002182 int cmdsn_ret;
2183
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002184 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002185 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2186 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2187 pr_err("Unable to locate text_in buffer for sendtargets"
2188 " discovery\n");
2189 goto reject;
2190 }
2191 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002192 }
2193 if (strncmp("SendTargets", text_in, 11) != 0) {
2194 pr_err("Received Text Data that is not"
2195 " SendTargets, cannot continue.\n");
2196 goto reject;
2197 }
2198 text_ptr = strchr(text_in, '=');
2199 if (!text_ptr) {
2200 pr_err("No \"=\" separator found in Text Data,"
2201 " cannot continue.\n");
2202 goto reject;
2203 }
2204 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002205 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002206 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2207 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002208 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002209 } else {
2210 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2211 goto reject;
2212 }
2213
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002214 spin_lock_bh(&conn->cmd_lock);
2215 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2216 spin_unlock_bh(&conn->cmd_lock);
2217
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002218empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002219 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2220
2221 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002222 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2223 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002224 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002225 return -1;
2226
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002227 return 0;
2228 }
2229
2230 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002231
2232reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002233 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2234 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002235}
2236EXPORT_SYMBOL(iscsit_process_text_cmd);
2237
2238static int
2239iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2240 unsigned char *buf)
2241{
2242 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2243 char *text_in = NULL;
2244 u32 payload_length = ntoh24(hdr->dlength);
2245 int rx_size, rc;
2246
2247 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2248 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002249 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002250
2251 rx_size = payload_length;
2252 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002253 u32 checksum = 0, data_crc = 0;
2254 u32 padding = 0, pad_bytes = 0;
2255 int niov = 0, rx_got;
2256 struct kvec iov[3];
2257
2258 text_in = kzalloc(payload_length, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02002259 if (!text_in)
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002260 goto reject;
Markus Elfringc46e22f2017-04-09 15:34:50 +02002261
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002262 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002263
2264 memset(iov, 0, 3 * sizeof(struct kvec));
2265 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002266 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002267
2268 padding = ((-payload_length) & 3);
2269 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002270 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002271 iov[niov++].iov_len = padding;
2272 rx_size += padding;
2273 pr_debug("Receiving %u additional bytes"
2274 " for padding.\n", padding);
2275 }
2276 if (conn->conn_ops->DataDigest) {
2277 iov[niov].iov_base = &checksum;
2278 iov[niov++].iov_len = ISCSI_CRC_LEN;
2279 rx_size += ISCSI_CRC_LEN;
2280 }
2281
2282 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002283 if (rx_got != rx_size)
2284 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002285
2286 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08002287 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002288 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002289 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002290 (u8 *)&data_crc);
2291
2292 if (checksum != data_crc) {
2293 pr_err("Text data CRC32C DataDigest"
2294 " 0x%08x does not match computed"
2295 " 0x%08x\n", checksum, data_crc);
2296 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2297 pr_err("Unable to recover from"
2298 " Text Data digest failure while in"
2299 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002300 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002301 } else {
2302 /*
2303 * Silently drop this PDU and let the
2304 * initiator plug the CmdSN gap.
2305 */
2306 pr_debug("Dropping Text"
2307 " Command CmdSN: 0x%08x due to"
2308 " DataCRC error.\n", hdr->cmdsn);
2309 kfree(text_in);
2310 return 0;
2311 }
2312 } else {
2313 pr_debug("Got CRC32C DataDigest"
2314 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002315 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002316 }
2317 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002318 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002319 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002320 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002321 }
2322
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002323 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002324
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002325reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002326 kfree(cmd->text_in_ptr);
2327 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002328 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002329}
2330
2331int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2332{
2333 struct iscsi_conn *conn_p;
2334 struct iscsi_session *sess = conn->sess;
2335
2336 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2337 " for SID: %u.\n", conn->cid, conn->sess->sid);
2338
2339 atomic_set(&sess->session_logout, 1);
2340 atomic_set(&conn->conn_logout_remove, 1);
2341 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2342
2343 iscsit_inc_conn_usage_count(conn);
2344 iscsit_inc_session_usage_count(sess);
2345
2346 spin_lock_bh(&sess->conn_lock);
2347 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2348 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2349 continue;
2350
2351 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2352 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2353 }
2354 spin_unlock_bh(&sess->conn_lock);
2355
2356 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2357
2358 return 0;
2359}
2360
2361int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2362{
2363 struct iscsi_conn *l_conn;
2364 struct iscsi_session *sess = conn->sess;
2365
2366 pr_debug("Received logout request CLOSECONNECTION for CID:"
2367 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2368
2369 /*
2370 * A Logout Request with a CLOSECONNECTION reason code for a CID
2371 * can arrive on a connection with a differing CID.
2372 */
2373 if (conn->cid == cmd->logout_cid) {
2374 spin_lock_bh(&conn->state_lock);
2375 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2376 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2377
2378 atomic_set(&conn->conn_logout_remove, 1);
2379 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2380 iscsit_inc_conn_usage_count(conn);
2381
2382 spin_unlock_bh(&conn->state_lock);
2383 } else {
2384 /*
2385 * Handle all different cid CLOSECONNECTION requests in
2386 * iscsit_logout_post_handler_diffcid() as to give enough
2387 * time for any non immediate command's CmdSN to be
2388 * acknowledged on the connection in question.
2389 *
2390 * Here we simply make sure the CID is still around.
2391 */
2392 l_conn = iscsit_get_conn_from_cid(sess,
2393 cmd->logout_cid);
2394 if (!l_conn) {
2395 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2396 iscsit_add_cmd_to_response_queue(cmd, conn,
2397 cmd->i_state);
2398 return 0;
2399 }
2400
2401 iscsit_dec_conn_usage_count(l_conn);
2402 }
2403
2404 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2405
2406 return 0;
2407}
2408
2409int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2410{
2411 struct iscsi_session *sess = conn->sess;
2412
2413 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2414 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2415
2416 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2417 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2418 " while ERL!=2.\n");
2419 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2420 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2421 return 0;
2422 }
2423
2424 if (conn->cid == cmd->logout_cid) {
2425 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2426 " with CID: %hu on CID: %hu, implementation error.\n",
2427 cmd->logout_cid, conn->cid);
2428 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2429 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2430 return 0;
2431 }
2432
2433 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2434
2435 return 0;
2436}
2437
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002438int
2439iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2440 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002441{
2442 int cmdsn_ret, logout_remove = 0;
2443 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002444 struct iscsi_logout *hdr;
2445 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2446
2447 hdr = (struct iscsi_logout *) buf;
2448 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002449
2450 if (tiqn) {
2451 spin_lock(&tiqn->logout_stats.lock);
2452 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2453 tiqn->logout_stats.normal_logouts++;
2454 else
2455 tiqn->logout_stats.abnormal_logouts++;
2456 spin_unlock(&tiqn->logout_stats.lock);
2457 }
2458
2459 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2460 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2461 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2462 hdr->cid, conn->cid);
2463
2464 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2465 pr_err("Received logout request on connection that"
2466 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002467 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002468 return 0;
2469 }
2470
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002471 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2472 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2473 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2474 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2475 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002476 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2477 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2478 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002479 cmd->logout_reason = reason_code;
2480 cmd->data_direction = DMA_NONE;
2481
2482 /*
2483 * We need to sleep in these cases (by returning 1) until the Logout
2484 * Response gets sent in the tx thread.
2485 */
2486 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2487 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002488 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002489 logout_remove = 1;
2490
2491 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002492 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002493 spin_unlock_bh(&conn->cmd_lock);
2494
2495 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002496 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002497
2498 /*
2499 * Immediate commands are executed, well, immediately.
2500 * Non-Immediate Logout Commands are executed in CmdSN order.
2501 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002502 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002503 int ret = iscsit_execute_cmd(cmd, 0);
2504
2505 if (ret < 0)
2506 return ret;
2507 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002508 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002509 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002510 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002511 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2512 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002513 }
2514
2515 return logout_remove;
2516}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002517EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002518
Varun Prakashd2faaef2016-04-20 00:00:19 +05302519int iscsit_handle_snack(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002520 struct iscsi_conn *conn,
2521 unsigned char *buf)
2522{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002523 struct iscsi_snack *hdr;
2524
2525 hdr = (struct iscsi_snack *) buf;
2526 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002527
2528 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2529 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2530 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2531 hdr->begrun, hdr->runlength, conn->cid);
2532
2533 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2534 pr_err("Initiator sent SNACK request while in"
2535 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002536 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2537 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002538 }
2539 /*
2540 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2541 * call from inside iscsi_send_recovery_datain_or_r2t().
2542 */
2543 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2544 case 0:
2545 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002546 hdr->itt,
2547 be32_to_cpu(hdr->ttt),
2548 be32_to_cpu(hdr->begrun),
2549 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002550 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002551 return iscsit_handle_status_snack(conn, hdr->itt,
2552 be32_to_cpu(hdr->ttt),
2553 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002554 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002555 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2556 be32_to_cpu(hdr->begrun),
2557 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002558 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2559 /* FIXME: Support R-Data SNACK */
2560 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002561 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2562 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002563 default:
2564 pr_err("Unknown SNACK type 0x%02x, protocol"
2565 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002566 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2567 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002568 }
2569
2570 return 0;
2571}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302572EXPORT_SYMBOL(iscsit_handle_snack);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002573
2574static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2575{
2576 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2577 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2578 wait_for_completion_interruptible_timeout(
2579 &conn->rx_half_close_comp,
2580 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2581 }
2582}
2583
2584static int iscsit_handle_immediate_data(
2585 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002586 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002587 u32 length)
2588{
2589 int iov_ret, rx_got = 0, rx_size = 0;
2590 u32 checksum, iov_count = 0, padding = 0;
2591 struct iscsi_conn *conn = cmd->conn;
2592 struct kvec *iov;
2593
2594 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2595 if (iov_ret < 0)
2596 return IMMEDIATE_DATA_CANNOT_RECOVER;
2597
2598 rx_size = length;
2599 iov_count = iov_ret;
2600 iov = &cmd->iov_data[0];
2601
2602 padding = ((-length) & 3);
2603 if (padding != 0) {
2604 iov[iov_count].iov_base = cmd->pad_bytes;
2605 iov[iov_count++].iov_len = padding;
2606 rx_size += padding;
2607 }
2608
2609 if (conn->conn_ops->DataDigest) {
2610 iov[iov_count].iov_base = &checksum;
2611 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2612 rx_size += ISCSI_CRC_LEN;
2613 }
2614
2615 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2616
2617 iscsit_unmap_iovec(cmd);
2618
2619 if (rx_got != rx_size) {
2620 iscsit_rx_thread_wait_for_tcp(conn);
2621 return IMMEDIATE_DATA_CANNOT_RECOVER;
2622 }
2623
2624 if (conn->conn_ops->DataDigest) {
2625 u32 data_crc;
2626
Herbert Xu69110e32016-01-24 21:19:52 +08002627 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002628 cmd->write_data_done, length, padding,
2629 cmd->pad_bytes);
2630
2631 if (checksum != data_crc) {
2632 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2633 " does not match computed 0x%08x\n", checksum,
2634 data_crc);
2635
2636 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2637 pr_err("Unable to recover from"
2638 " Immediate Data digest failure while"
2639 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002640 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002641 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002642 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002643 return IMMEDIATE_DATA_CANNOT_RECOVER;
2644 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002645 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002646 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002647 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002648 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2649 }
2650 } else {
2651 pr_debug("Got CRC32C DataDigest 0x%08x for"
2652 " %u bytes of Immediate Data\n", checksum,
2653 length);
2654 }
2655 }
2656
2657 cmd->write_data_done += length;
2658
Andy Groverebf1d952012-04-03 15:51:24 -07002659 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002660 spin_lock_bh(&cmd->istate_lock);
2661 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2662 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2663 spin_unlock_bh(&cmd->istate_lock);
2664 }
2665
2666 return IMMEDIATE_DATA_NORMAL_OPERATION;
2667}
2668
2669/*
2670 * Called with sess->conn_lock held.
2671 */
2672/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2673 with active network interface */
2674static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2675{
2676 struct iscsi_cmd *cmd;
2677 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002678 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002679
2680 /*
2681 * Only send a Asynchronous Message on connections whos network
2682 * interface is still functional.
2683 */
2684 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2685 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2686 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002687 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002688 break;
2689 }
2690 }
2691
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002692 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002693 return;
2694
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002695 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002696 if (!cmd) {
2697 iscsit_dec_conn_usage_count(conn_p);
2698 return;
2699 }
2700
2701 cmd->logout_cid = conn->cid;
2702 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2703 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2704
2705 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002706 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002707 spin_unlock_bh(&conn_p->cmd_lock);
2708
2709 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2710 iscsit_dec_conn_usage_count(conn_p);
2711}
2712
2713static int iscsit_send_conn_drop_async_message(
2714 struct iscsi_cmd *cmd,
2715 struct iscsi_conn *conn)
2716{
2717 struct iscsi_async *hdr;
2718
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002719 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2720
2721 hdr = (struct iscsi_async *) cmd->pdu;
2722 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2723 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002724 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002725 cmd->targ_xfer_tag = 0xFFFFFFFF;
2726 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2727 cmd->stat_sn = conn->stat_sn++;
2728 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2729 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002730 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002731 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2732 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2733 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2734 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2735
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002736 pr_debug("Sending Connection Dropped Async Message StatSN:"
2737 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2738 cmd->logout_cid, conn->cid);
Varun Prakash2854bb22016-04-20 00:00:08 +05302739
2740 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002741}
2742
Andy Grover6f3c0e62012-04-03 15:51:09 -07002743static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2744{
2745 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2746 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2747 wait_for_completion_interruptible_timeout(
2748 &conn->tx_half_close_comp,
2749 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2750 }
2751}
2752
Varun Prakashd2faaef2016-04-20 00:00:19 +05302753void
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002754iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2755 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2756 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002757{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002758 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2759 hdr->flags = datain->flags;
2760 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2761 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2762 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2763 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2764 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2765 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2766 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2767 }
2768 }
2769 hton24(hdr->dlength, datain->length);
2770 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2771 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2772 (struct scsi_lun *)&hdr->lun);
2773 else
2774 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2775
2776 hdr->itt = cmd->init_task_tag;
2777
2778 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2779 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2780 else
2781 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2782 if (set_statsn)
2783 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2784 else
2785 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2786
2787 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002788 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002789 hdr->datasn = cpu_to_be32(datain->data_sn);
2790 hdr->offset = cpu_to_be32(datain->offset);
2791
2792 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2793 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2794 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2795 ntohl(hdr->offset), datain->length, conn->cid);
2796}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302797EXPORT_SYMBOL(iscsit_build_datain_pdu);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002798
2799static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2800{
2801 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002802 struct iscsi_datain datain;
2803 struct iscsi_datain_req *dr;
Varun Prakash2854bb22016-04-20 00:00:08 +05302804 int eodr = 0, ret;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002805 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002806
2807 memset(&datain, 0, sizeof(struct iscsi_datain));
2808 dr = iscsit_get_datain_values(cmd, &datain);
2809 if (!dr) {
2810 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2811 cmd->init_task_tag);
2812 return -1;
2813 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002814 /*
2815 * Be paranoid and double check the logic for now.
2816 */
Andy Groverebf1d952012-04-03 15:51:24 -07002817 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002818 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2819 " datain.length: %u exceeds cmd->data_length: %u\n",
2820 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002821 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002822 return -1;
2823 }
2824
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002825 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002826 /*
2827 * Special case for successfully execution w/ both DATAIN
2828 * and Sense Data.
2829 */
2830 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2831 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2832 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2833 else {
2834 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2835 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2836 iscsit_increment_maxcmdsn(cmd, conn->sess);
2837 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002838 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002839 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002840 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2841 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002842 }
2843
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002844 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002845
Varun Prakash2854bb22016-04-20 00:00:08 +05302846 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2847 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07002848 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002849
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002850 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002851 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002852 2 : 1;
2853 iscsit_free_datain_req(cmd, dr);
2854 }
2855
Andy Grover6f3c0e62012-04-03 15:51:09 -07002856 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002857}
2858
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002859int
2860iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2861 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002862{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002863 struct iscsi_conn *logout_conn = NULL;
2864 struct iscsi_conn_recovery *cr = NULL;
2865 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002866 /*
2867 * The actual shutting down of Sessions and/or Connections
2868 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2869 * is done in scsi_logout_post_handler().
2870 */
2871 switch (cmd->logout_reason) {
2872 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2873 pr_debug("iSCSI session logout successful, setting"
2874 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2875 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2876 break;
2877 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2878 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2879 break;
2880 /*
2881 * For CLOSECONNECTION logout requests carrying
2882 * a matching logout CID -> local CID, the reference
2883 * for the local CID will have been incremented in
2884 * iscsi_logout_closeconnection().
2885 *
2886 * For CLOSECONNECTION logout requests carrying
2887 * a different CID than the connection it arrived
2888 * on, the connection responding to cmd->logout_cid
2889 * is stopped in iscsit_logout_post_handler_diffcid().
2890 */
2891
2892 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2893 " successful.\n", cmd->logout_cid, conn->cid);
2894 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2895 break;
2896 case ISCSI_LOGOUT_REASON_RECOVERY:
2897 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2898 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2899 break;
2900 /*
2901 * If the connection is still active from our point of view
2902 * force connection recovery to occur.
2903 */
2904 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2905 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002906 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002907 iscsit_connection_reinstatement_rcfr(logout_conn);
2908 iscsit_dec_conn_usage_count(logout_conn);
2909 }
2910
2911 cr = iscsit_get_inactive_connection_recovery_entry(
2912 conn->sess, cmd->logout_cid);
2913 if (!cr) {
2914 pr_err("Unable to locate CID: %hu for"
2915 " REMOVECONNFORRECOVERY Logout Request.\n",
2916 cmd->logout_cid);
2917 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2918 break;
2919 }
2920
2921 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2922
2923 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2924 " for recovery for CID: %hu on CID: %hu successful.\n",
2925 cmd->logout_cid, conn->cid);
2926 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2927 break;
2928 default:
2929 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2930 cmd->logout_reason);
2931 return -1;
2932 }
2933
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002934 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2935 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2936 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002937 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002938 cmd->stat_sn = conn->stat_sn++;
2939 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2940
2941 iscsit_increment_maxcmdsn(cmd, conn->sess);
2942 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002943 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002944
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002945 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2946 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2947 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2948 cmd->logout_cid, conn->cid);
2949
2950 return 0;
2951}
2952EXPORT_SYMBOL(iscsit_build_logout_rsp);
2953
2954static int
2955iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2956{
Varun Prakash2854bb22016-04-20 00:00:08 +05302957 int rc;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002958
2959 rc = iscsit_build_logout_rsp(cmd, conn,
2960 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2961 if (rc < 0)
2962 return rc;
2963
Varun Prakash2854bb22016-04-20 00:00:08 +05302964 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002965}
2966
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002967void
2968iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2969 struct iscsi_nopin *hdr, bool nopout_response)
2970{
2971 hdr->opcode = ISCSI_OP_NOOP_IN;
2972 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2973 hton24(hdr->dlength, cmd->buf_ptr_size);
2974 if (nopout_response)
2975 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2976 hdr->itt = cmd->init_task_tag;
2977 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2978 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2979 conn->stat_sn;
2980 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2981
2982 if (nopout_response)
2983 iscsit_increment_maxcmdsn(cmd, conn->sess);
2984
2985 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002986 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002987
2988 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2989 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
Colin Ian King3fc6a642016-09-02 15:30:34 +01002990 "Solicited" : "Unsolicited", cmd->init_task_tag,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002991 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2992}
2993EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2994
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002995/*
2996 * Unsolicited NOPIN, either requesting a response or not.
2997 */
2998static int iscsit_send_unsolicited_nopin(
2999 struct iscsi_cmd *cmd,
3000 struct iscsi_conn *conn,
3001 int want_response)
3002{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003003 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Varun Prakash2854bb22016-04-20 00:00:08 +05303004 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003005
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003006 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003007
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003008 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3009 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
3010
Varun Prakash2854bb22016-04-20 00:00:08 +05303011 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3012 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003013 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003014
3015 spin_lock_bh(&cmd->istate_lock);
3016 cmd->i_state = want_response ?
3017 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3018 spin_unlock_bh(&cmd->istate_lock);
3019
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003020 return 0;
3021}
3022
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003023static int
3024iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003025{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003026 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003027
3028 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003029
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003030 /*
3031 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3032 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3033 */
Varun Prakash2854bb22016-04-20 00:00:08 +05303034 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003035
Varun Prakash2854bb22016-04-20 00:00:08 +05303036 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3037 cmd->buf_ptr,
3038 cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003039}
3040
Andy Grover6f3c0e62012-04-03 15:51:09 -07003041static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003042 struct iscsi_cmd *cmd,
3043 struct iscsi_conn *conn)
3044{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003045 struct iscsi_r2t *r2t;
3046 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003047 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003048
3049 r2t = iscsit_get_r2t_from_list(cmd);
3050 if (!r2t)
3051 return -1;
3052
3053 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3054 memset(hdr, 0, ISCSI_HDR_LEN);
3055 hdr->opcode = ISCSI_OP_R2T;
3056 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3057 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3058 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003059 hdr->itt = cmd->init_task_tag;
Varun Prakash85672702016-04-20 00:00:13 +05303060 if (conn->conn_transport->iscsit_get_r2t_ttt)
3061 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3062 else
3063 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003064 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3065 hdr->statsn = cpu_to_be32(conn->stat_sn);
3066 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003067 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003068 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3069 hdr->data_offset = cpu_to_be32(r2t->offset);
3070 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3071
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003072 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3073 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3074 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3075 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3076 r2t->offset, r2t->xfer_len, conn->cid);
3077
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003078 spin_lock_bh(&cmd->r2t_lock);
3079 r2t->sent_r2t = 1;
3080 spin_unlock_bh(&cmd->r2t_lock);
3081
Varun Prakash2854bb22016-04-20 00:00:08 +05303082 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003083 if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003084 return ret;
3085 }
3086
3087 spin_lock_bh(&cmd->dataout_timeout_lock);
3088 iscsit_start_dataout_timer(cmd, conn);
3089 spin_unlock_bh(&cmd->dataout_timeout_lock);
3090
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003091 return 0;
3092}
3093
3094/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003095 * @recovery: If called from iscsi_task_reassign_complete_write() for
3096 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003097 */
3098int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003099 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003100 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003101 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003102{
3103 int first_r2t = 1;
3104 u32 offset = 0, xfer_len = 0;
3105
3106 spin_lock_bh(&cmd->r2t_lock);
3107 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3108 spin_unlock_bh(&cmd->r2t_lock);
3109 return 0;
3110 }
3111
Andy Grover8b1e1242012-04-03 15:51:12 -07003112 if (conn->sess->sess_ops->DataSequenceInOrder &&
3113 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003114 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003115
3116 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3117 if (conn->sess->sess_ops->DataSequenceInOrder) {
3118 offset = cmd->r2t_offset;
3119
Andy Grover8b1e1242012-04-03 15:51:12 -07003120 if (first_r2t && recovery) {
3121 int new_data_end = offset +
3122 conn->sess->sess_ops->MaxBurstLength -
3123 cmd->next_burst_len;
3124
Andy Groverebf1d952012-04-03 15:51:24 -07003125 if (new_data_end > cmd->se_cmd.data_length)
3126 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003127 else
3128 xfer_len =
3129 conn->sess->sess_ops->MaxBurstLength -
3130 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003131 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003132 int new_data_end = offset +
3133 conn->sess->sess_ops->MaxBurstLength;
3134
Andy Groverebf1d952012-04-03 15:51:24 -07003135 if (new_data_end > cmd->se_cmd.data_length)
3136 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003137 else
3138 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003139 }
3140 cmd->r2t_offset += xfer_len;
3141
Andy Groverebf1d952012-04-03 15:51:24 -07003142 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003143 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3144 } else {
3145 struct iscsi_seq *seq;
3146
3147 seq = iscsit_get_seq_holder_for_r2t(cmd);
3148 if (!seq) {
3149 spin_unlock_bh(&cmd->r2t_lock);
3150 return -1;
3151 }
3152
3153 offset = seq->offset;
3154 xfer_len = seq->xfer_len;
3155
3156 if (cmd->seq_send_order == cmd->seq_count)
3157 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3158 }
3159 cmd->outstanding_r2ts++;
3160 first_r2t = 0;
3161
3162 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3163 spin_unlock_bh(&cmd->r2t_lock);
3164 return -1;
3165 }
3166
3167 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3168 break;
3169 }
3170 spin_unlock_bh(&cmd->r2t_lock);
3171
3172 return 0;
3173}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303174EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003175
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003176void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3177 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003178{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003179 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003180 cmd->stat_sn = conn->stat_sn++;
3181
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003182 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003183
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003184 memset(hdr, 0, ISCSI_HDR_LEN);
3185 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3186 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3187 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3188 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003189 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003190 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3191 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003192 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003193 }
3194 hdr->response = cmd->iscsi_response;
3195 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003196 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003197 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3198
3199 iscsit_increment_maxcmdsn(cmd, conn->sess);
3200 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003201 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003202
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003203 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3204 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3205 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3206 cmd->se_cmd.scsi_status, conn->cid);
3207}
3208EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3209
3210static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3211{
3212 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003213 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
Varun Prakash2854bb22016-04-20 00:00:08 +05303214 void *data_buf = NULL;
3215 u32 padding = 0, data_buf_len = 0;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003216
3217 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3218
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003219 /*
3220 * Attach SENSE DATA payload to iSCSI Response PDU
3221 */
3222 if (cmd->se_cmd.sense_buffer &&
3223 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3224 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003225 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3226 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3227
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003228 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003229 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Varun Prakash2854bb22016-04-20 00:00:08 +05303230 data_buf = cmd->sense_buffer;
3231 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003232
3233 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003234 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003235 cmd->se_cmd.scsi_sense_length, 0, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003236 pr_debug("Adding %u bytes of padding to"
3237 " SENSE.\n", padding);
3238 }
3239
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003240 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3241 " Response PDU\n",
3242 cmd->se_cmd.scsi_sense_length);
3243 }
3244
Varun Prakash2854bb22016-04-20 00:00:08 +05303245 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3246 data_buf_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003247}
3248
3249static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3250{
3251 switch (se_tmr->response) {
3252 case TMR_FUNCTION_COMPLETE:
3253 return ISCSI_TMF_RSP_COMPLETE;
3254 case TMR_TASK_DOES_NOT_EXIST:
3255 return ISCSI_TMF_RSP_NO_TASK;
3256 case TMR_LUN_DOES_NOT_EXIST:
3257 return ISCSI_TMF_RSP_NO_LUN;
3258 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3259 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003260 case TMR_FUNCTION_REJECTED:
3261 default:
3262 return ISCSI_TMF_RSP_REJECTED;
3263 }
3264}
3265
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003266void
3267iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3268 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003269{
3270 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003271
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003272 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003273 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003274 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003275 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003276 cmd->stat_sn = conn->stat_sn++;
3277 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3278
3279 iscsit_increment_maxcmdsn(cmd, conn->sess);
3280 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003281 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003282
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003283 pr_debug("Built Task Management Response ITT: 0x%08x,"
3284 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3285 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3286}
3287EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3288
3289static int
3290iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3291{
3292 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003293
3294 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3295
Varun Prakash2854bb22016-04-20 00:00:08 +05303296 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003297}
3298
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003299static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3300{
3301 bool ret = false;
3302
3303 if (np->np_sockaddr.ss_family == AF_INET6) {
3304 const struct sockaddr_in6 sin6 = {
3305 .sin6_addr = IN6ADDR_ANY_INIT };
3306 struct sockaddr_in6 *sock_in6 =
3307 (struct sockaddr_in6 *)&np->np_sockaddr;
3308
3309 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3310 sin6.sin6_addr.s6_addr, 16))
3311 ret = true;
3312 } else {
3313 struct sockaddr_in * sock_in =
3314 (struct sockaddr_in *)&np->np_sockaddr;
3315
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003316 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003317 ret = true;
3318 }
3319
3320 return ret;
3321}
3322
Andy Grover8b1e1242012-04-03 15:51:12 -07003323#define SENDTARGETS_BUF_LIMIT 32768U
3324
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003325static int
3326iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003327 enum iscsit_transport_type network_transport,
3328 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003329{
3330 char *payload = NULL;
3331 struct iscsi_conn *conn = cmd->conn;
3332 struct iscsi_portal_group *tpg;
3333 struct iscsi_tiqn *tiqn;
3334 struct iscsi_tpg_np *tpg_np;
3335 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003336 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003337 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003338 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
David Disseldorpa6415cd2015-08-01 00:10:12 -07003339 bool active;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003340
Sagi Grimbergbe7dcfb2015-01-26 12:49:06 +02003341 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003342 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003343
3344 payload = kzalloc(buffer_len, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02003345 if (!payload)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003346 return -ENOMEM;
Markus Elfringc46e22f2017-04-09 15:34:50 +02003347
Nicholas Bellinger66658892013-06-19 22:45:42 -07003348 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003349 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003350 * explicit case..
3351 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003352 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003353 text_ptr = strchr(text_in, '=');
3354 if (!text_ptr) {
3355 pr_err("Unable to locate '=' string in text_in:"
3356 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003357 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003358 return -EINVAL;
3359 }
3360 /*
3361 * Skip over '=' character..
3362 */
3363 text_ptr += 1;
3364 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003365
3366 spin_lock(&tiqn_lock);
3367 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003368 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003369 strcmp(tiqn->tiqn, text_ptr)) {
3370 continue;
3371 }
3372
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003373 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003374
3375 spin_lock(&tiqn->tiqn_tpg_lock);
3376 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3377
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003378 /* If demo_mode_discovery=0 and generate_node_acls=0
3379 * (demo mode dislabed) do not return
3380 * TargetName+TargetAddress unless a NodeACL exists.
3381 */
3382
3383 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3384 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
Nicholas Bellinger21aaa232016-01-07 22:09:27 -08003385 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003386 cmd->conn->sess->sess_ops->InitiatorName))) {
3387 continue;
3388 }
3389
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003390 spin_lock(&tpg->tpg_state_lock);
David Disseldorpa6415cd2015-08-01 00:10:12 -07003391 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003392 spin_unlock(&tpg->tpg_state_lock);
3393
David Disseldorpa6415cd2015-08-01 00:10:12 -07003394 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3395 continue;
3396
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003397 spin_lock(&tpg->tpg_np_lock);
3398 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3399 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003400 struct iscsi_np *np = tpg_np->tpg_np;
3401 bool inaddr_any = iscsit_check_inaddr_any(np);
Andy Grover13a3cf02015-08-24 10:26:06 -07003402 struct sockaddr_storage *sockaddr;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003403
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003404 if (np->np_network_transport != network_transport)
3405 continue;
3406
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003407 if (!target_name_printed) {
3408 len = sprintf(buf, "TargetName=%s",
3409 tiqn->tiqn);
3410 len += 1;
3411
3412 if ((len + payload_len) > buffer_len) {
3413 spin_unlock(&tpg->tpg_np_lock);
3414 spin_unlock(&tiqn->tiqn_tpg_lock);
3415 end_of_buf = 1;
3416 goto eob;
3417 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003418
3419 if (skip_bytes && len <= skip_bytes) {
3420 skip_bytes -= len;
3421 } else {
3422 memcpy(payload + payload_len, buf, len);
3423 payload_len += len;
3424 target_name_printed = 1;
3425 if (len > skip_bytes)
3426 skip_bytes = 0;
3427 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003428 }
3429
Andy Grover69d75572015-08-24 10:26:04 -07003430 if (inaddr_any)
3431 sockaddr = &conn->local_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003432 else
Andy Grover69d75572015-08-24 10:26:04 -07003433 sockaddr = &np->np_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003434
Andy Grover69d75572015-08-24 10:26:04 -07003435 len = sprintf(buf, "TargetAddress="
3436 "%pISpc,%hu",
3437 sockaddr,
3438 tpg->tpgt);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003439 len += 1;
3440
3441 if ((len + payload_len) > buffer_len) {
3442 spin_unlock(&tpg->tpg_np_lock);
3443 spin_unlock(&tiqn->tiqn_tpg_lock);
3444 end_of_buf = 1;
3445 goto eob;
3446 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003447
3448 if (skip_bytes && len <= skip_bytes) {
3449 skip_bytes -= len;
3450 } else {
3451 memcpy(payload + payload_len, buf, len);
3452 payload_len += len;
3453 if (len > skip_bytes)
3454 skip_bytes = 0;
3455 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003456 }
3457 spin_unlock(&tpg->tpg_np_lock);
3458 }
3459 spin_unlock(&tiqn->tiqn_tpg_lock);
3460eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003461 if (end_of_buf) {
3462 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003463 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003464 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003465
Andy Grover8060b8d2015-01-09 15:13:08 -08003466 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003467 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003468 }
3469 spin_unlock(&tiqn_lock);
3470
3471 cmd->buf_ptr = payload;
3472
3473 return payload_len;
3474}
3475
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003476int
3477iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003478 struct iscsi_text_rsp *hdr,
3479 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003480{
3481 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003482 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003483
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003484 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3485 cmd->read_data_done,
3486 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003487 if (text_length < 0)
3488 return text_length;
3489
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003490 if (completed) {
Varun Prakash310d40a2017-07-23 20:03:45 +05303491 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003492 } else {
Varun Prakash310d40a2017-07-23 20:03:45 +05303493 hdr->flags = ISCSI_FLAG_TEXT_CONTINUE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003494 cmd->read_data_done += text_length;
3495 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3496 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3497 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003498 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003499 padding = ((-text_length) & 3);
3500 hton24(hdr->dlength, text_length);
3501 hdr->itt = cmd->init_task_tag;
3502 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3503 cmd->stat_sn = conn->stat_sn++;
3504 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3505
3506 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003507 /*
3508 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3509 * correctly increment MaxCmdSN for each response answering a
3510 * non immediate text request with a valid CmdSN.
3511 */
3512 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003513 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003514 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003515
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003516 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3517 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3518 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3519 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3520 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003521
3522 return text_length + padding;
3523}
3524EXPORT_SYMBOL(iscsit_build_text_rsp);
3525
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003526static int iscsit_send_text_rsp(
3527 struct iscsi_cmd *cmd,
3528 struct iscsi_conn *conn)
3529{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003530 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Varun Prakash2854bb22016-04-20 00:00:08 +05303531 int text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003532
Varun Prakash864e5042016-04-20 00:00:15 +05303533 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3534 conn->conn_transport->transport_type);
Varun Prakash2854bb22016-04-20 00:00:08 +05303535 if (text_length < 0)
3536 return text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003537
Varun Prakash2854bb22016-04-20 00:00:08 +05303538 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3539 cmd->buf_ptr,
3540 text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003541}
3542
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003543void
3544iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3545 struct iscsi_reject *hdr)
3546{
3547 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003548 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003549 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3550 hton24(hdr->dlength, ISCSI_HDR_LEN);
3551 hdr->ffffffff = cpu_to_be32(0xffffffff);
3552 cmd->stat_sn = conn->stat_sn++;
3553 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3554 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003555 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003556
3557}
3558EXPORT_SYMBOL(iscsit_build_reject);
3559
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003560static int iscsit_send_reject(
3561 struct iscsi_cmd *cmd,
3562 struct iscsi_conn *conn)
3563{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003564 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003565
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003566 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003567
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003568 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3569 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3570
Varun Prakash2854bb22016-04-20 00:00:08 +05303571 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3572 cmd->buf_ptr,
3573 ISCSI_HDR_LEN);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003574}
3575
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003576void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3577{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003578 int ord, cpu;
3579 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003580 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3581 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003582 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003583 * Here we use bitmap_id to determine which CPU that this
3584 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003585 * execute upon.
3586 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003587 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003588 for_each_online_cpu(cpu) {
3589 if (ord-- == 0) {
3590 cpumask_set_cpu(cpu, conn->conn_cpumask);
3591 return;
3592 }
3593 }
3594 /*
3595 * This should never be reached..
3596 */
3597 dump_stack();
3598 cpumask_setall(conn->conn_cpumask);
3599}
3600
Varun Prakashd2faaef2016-04-20 00:00:19 +05303601int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003602iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003603{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003604 int ret;
3605
3606 switch (state) {
3607 case ISTATE_SEND_R2T:
3608 ret = iscsit_send_r2t(cmd, conn);
3609 if (ret < 0)
3610 goto err;
3611 break;
3612 case ISTATE_REMOVE:
3613 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003614 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003615 spin_unlock_bh(&conn->cmd_lock);
3616
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003617 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003618 break;
3619 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3620 iscsit_mod_nopin_response_timer(conn);
3621 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3622 if (ret < 0)
3623 goto err;
3624 break;
3625 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3626 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3627 if (ret < 0)
3628 goto err;
3629 break;
3630 default:
3631 pr_err("Unknown Opcode: 0x%02x ITT:"
3632 " 0x%08x, i_state: %d on CID: %hu\n",
3633 cmd->iscsi_opcode, cmd->init_task_tag, state,
3634 conn->cid);
3635 goto err;
3636 }
3637
3638 return 0;
3639
3640err:
3641 return -1;
3642}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303643EXPORT_SYMBOL(iscsit_immediate_queue);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003644
3645static int
3646iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3647{
3648 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003649 struct iscsi_queue_req *qr;
3650 struct iscsi_cmd *cmd;
3651 u8 state;
3652 int ret;
3653
3654 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3655 atomic_set(&conn->check_immediate_queue, 0);
3656 cmd = qr->cmd;
3657 state = qr->state;
3658 kmem_cache_free(lio_qr_cache, qr);
3659
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003660 ret = t->iscsit_immediate_queue(conn, cmd, state);
3661 if (ret < 0)
3662 return ret;
3663 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003664
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003665 return 0;
3666}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003667
Varun Prakashd2faaef2016-04-20 00:00:19 +05303668int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003669iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3670{
3671 int ret;
3672
3673check_rsp_state:
3674 switch (state) {
3675 case ISTATE_SEND_DATAIN:
3676 ret = iscsit_send_datain(cmd, conn);
3677 if (ret < 0)
3678 goto err;
3679 else if (!ret)
3680 /* more drs */
3681 goto check_rsp_state;
3682 else if (ret == 1) {
3683 /* all done */
3684 spin_lock_bh(&cmd->istate_lock);
3685 cmd->i_state = ISTATE_SENT_STATUS;
3686 spin_unlock_bh(&cmd->istate_lock);
3687
3688 if (atomic_read(&conn->check_immediate_queue))
3689 return 1;
3690
3691 return 0;
3692 } else if (ret == 2) {
3693 /* Still must send status,
3694 SCF_TRANSPORT_TASK_SENSE was set */
3695 spin_lock_bh(&cmd->istate_lock);
3696 cmd->i_state = ISTATE_SEND_STATUS;
3697 spin_unlock_bh(&cmd->istate_lock);
3698 state = ISTATE_SEND_STATUS;
3699 goto check_rsp_state;
3700 }
3701
3702 break;
3703 case ISTATE_SEND_STATUS:
3704 case ISTATE_SEND_STATUS_RECOVERY:
3705 ret = iscsit_send_response(cmd, conn);
3706 break;
3707 case ISTATE_SEND_LOGOUTRSP:
3708 ret = iscsit_send_logout(cmd, conn);
3709 break;
3710 case ISTATE_SEND_ASYNCMSG:
3711 ret = iscsit_send_conn_drop_async_message(
3712 cmd, conn);
3713 break;
3714 case ISTATE_SEND_NOPIN:
3715 ret = iscsit_send_nopin(cmd, conn);
3716 break;
3717 case ISTATE_SEND_REJECT:
3718 ret = iscsit_send_reject(cmd, conn);
3719 break;
3720 case ISTATE_SEND_TASKMGTRSP:
3721 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3722 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003723 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003724 ret = iscsit_tmr_post_handler(cmd, conn);
3725 if (ret != 0)
3726 iscsit_fall_back_to_erl0(conn->sess);
3727 break;
3728 case ISTATE_SEND_TEXTRSP:
3729 ret = iscsit_send_text_rsp(cmd, conn);
3730 break;
3731 default:
3732 pr_err("Unknown Opcode: 0x%02x ITT:"
3733 " 0x%08x, i_state: %d on CID: %hu\n",
3734 cmd->iscsi_opcode, cmd->init_task_tag,
3735 state, conn->cid);
3736 goto err;
3737 }
3738 if (ret < 0)
3739 goto err;
3740
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003741 switch (state) {
3742 case ISTATE_SEND_LOGOUTRSP:
3743 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003744 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003745 /* fall through */
3746 case ISTATE_SEND_STATUS:
3747 case ISTATE_SEND_ASYNCMSG:
3748 case ISTATE_SEND_NOPIN:
3749 case ISTATE_SEND_STATUS_RECOVERY:
3750 case ISTATE_SEND_TEXTRSP:
3751 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003752 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003753 spin_lock_bh(&cmd->istate_lock);
3754 cmd->i_state = ISTATE_SENT_STATUS;
3755 spin_unlock_bh(&cmd->istate_lock);
3756 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003757 default:
3758 pr_err("Unknown Opcode: 0x%02x ITT:"
3759 " 0x%08x, i_state: %d on CID: %hu\n",
3760 cmd->iscsi_opcode, cmd->init_task_tag,
3761 cmd->i_state, conn->cid);
3762 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003763 }
3764
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003765 if (atomic_read(&conn->check_immediate_queue))
3766 return 1;
3767
Andy Grover6f3c0e62012-04-03 15:51:09 -07003768 return 0;
3769
3770err:
3771 return -1;
3772}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303773EXPORT_SYMBOL(iscsit_response_queue);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003774
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003775static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003776{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003777 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003778 struct iscsi_queue_req *qr;
3779 struct iscsi_cmd *cmd;
3780 u8 state;
3781 int ret;
3782
3783 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3784 cmd = qr->cmd;
3785 state = qr->state;
3786 kmem_cache_free(lio_qr_cache, qr);
3787
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003788 ret = t->iscsit_response_queue(conn, cmd, state);
3789 if (ret == 1 || ret < 0)
3790 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003791 }
3792
3793 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003794}
3795
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003796int iscsi_target_tx_thread(void *arg)
3797{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003798 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003799 struct iscsi_conn *conn = arg;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003800 bool conn_freed = false;
3801
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003802 /*
3803 * Allow ourselves to be interrupted by SIGINT so that a
3804 * connection recovery / failure event can be triggered externally.
3805 */
3806 allow_signal(SIGINT);
3807
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003808 while (!kthread_should_stop()) {
3809 /*
3810 * Ensure that both TX and RX per connection kthreads
3811 * are scheduled to run on the same CPU.
3812 */
3813 iscsit_thread_check_cpumask(conn, current, 1);
3814
Roland Dreierd5627ac2012-10-31 09:16:46 -07003815 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003816 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003817
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003818 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003819 goto transport_err;
3820
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003821get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003822 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003823 if (ret < 0)
3824 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003825
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003826 ret = iscsit_handle_response_queue(conn);
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003827 if (ret == 1) {
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003828 goto get_immediate;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003829 } else if (ret == -ECONNRESET) {
3830 conn_freed = true;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003831 goto out;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003832 } else if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003833 goto transport_err;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003834 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003835 }
3836
3837transport_err:
Nicholas Bellingere5419862015-07-22 23:14:19 -07003838 /*
3839 * Avoid the normal connection failure code-path if this connection
3840 * is still within LOGIN mode, and iscsi_np process context is
3841 * responsible for cleaning up the early connection failure.
3842 */
3843 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003844 iscsit_take_action_for_connection_exit(conn, &conn_freed);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003845out:
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003846 if (!conn_freed) {
3847 while (!kthread_should_stop()) {
3848 msleep(100);
3849 }
3850 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003851 return 0;
3852}
3853
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003854static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3855{
3856 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3857 struct iscsi_cmd *cmd;
3858 int ret = 0;
3859
3860 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3861 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003862 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003863 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003864 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003865
3866 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3867 break;
3868 case ISCSI_OP_SCSI_DATA_OUT:
3869 ret = iscsit_handle_data_out(conn, buf);
3870 break;
3871 case ISCSI_OP_NOOP_OUT:
3872 cmd = NULL;
3873 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003874 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003875 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003876 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003877 }
3878 ret = iscsit_handle_nop_out(conn, cmd, buf);
3879 break;
3880 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003881 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003882 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003883 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003884
3885 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3886 break;
3887 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003888 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3889 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3890 if (!cmd)
3891 goto reject;
3892 } else {
3893 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3894 if (!cmd)
3895 goto reject;
3896 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003897
3898 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003899 break;
3900 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003901 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003902 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003903 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003904
3905 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3906 if (ret > 0)
3907 wait_for_completion_timeout(&conn->conn_logout_comp,
3908 SECONDS_FOR_LOGOUT_COMP * HZ);
3909 break;
3910 case ISCSI_OP_SNACK:
3911 ret = iscsit_handle_snack(conn, buf);
3912 break;
3913 default:
3914 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3915 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3916 pr_err("Cannot recover from unknown"
3917 " opcode while ERL=0, closing iSCSI connection.\n");
3918 return -1;
3919 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02003920 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3921 " closing iSCSI connection.\n");
3922 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003923 break;
3924 }
3925
3926 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003927reject:
3928 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003929}
3930
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08003931static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3932{
3933 bool ret;
3934
3935 spin_lock_bh(&conn->state_lock);
3936 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3937 spin_unlock_bh(&conn->state_lock);
3938
3939 return ret;
3940}
3941
Varun Prakashe8205cc2016-04-20 00:00:11 +05303942static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003943{
Varun Prakashe8205cc2016-04-20 00:00:11 +05303944 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003945 u8 buffer[ISCSI_HDR_LEN], opcode;
3946 u32 checksum = 0, digest = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003947 struct kvec iov;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003948
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003949 while (!kthread_should_stop()) {
3950 /*
3951 * Ensure that both TX and RX per connection kthreads
3952 * are scheduled to run on the same CPU.
3953 */
3954 iscsit_thread_check_cpumask(conn, current, 0);
3955
3956 memset(buffer, 0, ISCSI_HDR_LEN);
3957 memset(&iov, 0, sizeof(struct kvec));
3958
3959 iov.iov_base = buffer;
3960 iov.iov_len = ISCSI_HDR_LEN;
3961
3962 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3963 if (ret != ISCSI_HDR_LEN) {
3964 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303965 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003966 }
3967
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003968 if (conn->conn_ops->HeaderDigest) {
3969 iov.iov_base = &digest;
3970 iov.iov_len = ISCSI_CRC_LEN;
3971
3972 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3973 if (ret != ISCSI_CRC_LEN) {
3974 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303975 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003976 }
3977
Herbert Xu69110e32016-01-24 21:19:52 +08003978 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003979 buffer, ISCSI_HDR_LEN,
3980 0, NULL, (u8 *)&checksum);
3981
3982 if (digest != checksum) {
3983 pr_err("HeaderDigest CRC32C failed,"
3984 " received 0x%08x, computed 0x%08x\n",
3985 digest, checksum);
3986 /*
3987 * Set the PDU to 0xff so it will intentionally
3988 * hit default in the switch below.
3989 */
3990 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003991 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003992 } else {
3993 pr_debug("Got HeaderDigest CRC32C"
3994 " 0x%08x\n", checksum);
3995 }
3996 }
3997
3998 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
Varun Prakashe8205cc2016-04-20 00:00:11 +05303999 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004000
4001 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4002
4003 if (conn->sess->sess_ops->SessionType &&
4004 ((!(opcode & ISCSI_OP_TEXT)) ||
4005 (!(opcode & ISCSI_OP_LOGOUT)))) {
4006 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4007 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004008 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4009 buffer);
Varun Prakashe8205cc2016-04-20 00:00:11 +05304010 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004011 }
4012
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004013 ret = iscsi_target_rx_opcode(conn, buffer);
4014 if (ret < 0)
Varun Prakashe8205cc2016-04-20 00:00:11 +05304015 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004016 }
Varun Prakashe8205cc2016-04-20 00:00:11 +05304017}
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004018
Varun Prakashe8205cc2016-04-20 00:00:11 +05304019int iscsi_target_rx_thread(void *arg)
4020{
4021 int rc;
4022 struct iscsi_conn *conn = arg;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004023 bool conn_freed = false;
Varun Prakashe8205cc2016-04-20 00:00:11 +05304024
4025 /*
4026 * Allow ourselves to be interrupted by SIGINT so that a
4027 * connection recovery / failure event can be triggered externally.
4028 */
4029 allow_signal(SIGINT);
4030 /*
4031 * Wait for iscsi_post_login_handler() to complete before allowing
4032 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4033 */
4034 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4035 if (rc < 0 || iscsi_target_check_conn_state(conn))
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004036 goto out;
Varun Prakashe8205cc2016-04-20 00:00:11 +05304037
4038 if (!conn->conn_transport->iscsit_get_rx_pdu)
4039 return 0;
4040
4041 conn->conn_transport->iscsit_get_rx_pdu(conn);
4042
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004043 if (!signal_pending(current))
4044 atomic_set(&conn->transport_failed, 1);
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004045 iscsit_take_action_for_connection_exit(conn, &conn_freed);
4046
4047out:
4048 if (!conn_freed) {
4049 while (!kthread_should_stop()) {
4050 msleep(100);
4051 }
4052 }
4053
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004054 return 0;
4055}
4056
4057static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4058{
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004059 LIST_HEAD(tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004060 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4061 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004062 /*
4063 * We expect this function to only ever be called from either RX or TX
4064 * thread context via iscsit_close_connection() once the other context
4065 * has been reset -> returned sleeping pre-handler state.
4066 */
4067 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004068 list_splice_init(&conn->conn_cmd_list, &tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004069
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004070 list_for_each_entry(cmd, &tmp_list, i_conn_node) {
4071 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004072
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004073 if (se_cmd->se_tfo != NULL) {
4074 spin_lock(&se_cmd->t_state_lock);
4075 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4076 spin_unlock(&se_cmd->t_state_lock);
4077 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004078 }
4079 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004080
4081 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4082 list_del_init(&cmd->i_conn_node);
4083
4084 iscsit_increment_maxcmdsn(cmd, sess);
4085 iscsit_free_cmd(cmd, true);
4086
4087 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004088}
4089
4090static void iscsit_stop_timers_for_cmds(
4091 struct iscsi_conn *conn)
4092{
4093 struct iscsi_cmd *cmd;
4094
4095 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004096 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004097 if (cmd->data_direction == DMA_TO_DEVICE)
4098 iscsit_stop_dataout_timer(cmd);
4099 }
4100 spin_unlock_bh(&conn->cmd_lock);
4101}
4102
4103int iscsit_close_connection(
4104 struct iscsi_conn *conn)
4105{
4106 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4107 struct iscsi_session *sess = conn->sess;
4108
4109 pr_debug("Closing iSCSI connection CID %hu on SID:"
4110 " %u\n", conn->cid, sess->sid);
4111 /*
Varun Prakashb4869ee2016-04-20 00:00:18 +05304112 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4113 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4114 * sleeping and the logout response never got sent because the
4115 * connection failed.
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004116 *
4117 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4118 * to signal logout response TX interrupt completion. Go ahead and skip
4119 * this for iser since isert_rx_opcode() does not wait on logout failure,
4120 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004121 */
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07004122 if (!conn->conn_transport->rdma_shutdown)
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004123 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004124
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004125 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4126 if (conn->tx_thread &&
4127 cmpxchg(&conn->tx_thread_active, true, false)) {
4128 send_sig(SIGINT, conn->tx_thread, 1);
4129 kthread_stop(conn->tx_thread);
4130 }
4131 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4132 if (conn->rx_thread &&
4133 cmpxchg(&conn->rx_thread_active, true, false)) {
4134 send_sig(SIGINT, conn->rx_thread, 1);
4135 kthread_stop(conn->rx_thread);
4136 }
4137 }
4138
4139 spin_lock(&iscsit_global->ts_bitmap_lock);
4140 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4141 get_order(1));
4142 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004143
4144 iscsit_stop_timers_for_cmds(conn);
4145 iscsit_stop_nopin_response_timer(conn);
4146 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004147
4148 if (conn->conn_transport->iscsit_wait_conn)
4149 conn->conn_transport->iscsit_wait_conn(conn);
4150
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004151 /*
4152 * During Connection recovery drop unacknowledged out of order
4153 * commands for this connection, and prepare the other commands
Bart Van Assche53c561d2016-12-23 14:40:24 +01004154 * for reallegiance.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004155 *
4156 * During normal operation clear the out of order commands (but
4157 * do not free the struct iscsi_ooo_cmdsn's) and release all
4158 * struct iscsi_cmds.
4159 */
4160 if (atomic_read(&conn->connection_recovery)) {
4161 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
Bart Van Assche53c561d2016-12-23 14:40:24 +01004162 iscsit_prepare_cmds_for_reallegiance(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004163 } else {
4164 iscsit_clear_ooo_cmdsns_for_conn(conn);
4165 iscsit_release_commands_from_conn(conn);
4166 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004167 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004168
4169 /*
4170 * Handle decrementing session or connection usage count if
4171 * a logout response was not able to be sent because the
4172 * connection failed. Fall back to Session Recovery here.
4173 */
4174 if (atomic_read(&conn->conn_logout_remove)) {
4175 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4176 iscsit_dec_conn_usage_count(conn);
4177 iscsit_dec_session_usage_count(sess);
4178 }
4179 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4180 iscsit_dec_conn_usage_count(conn);
4181
4182 atomic_set(&conn->conn_logout_remove, 0);
4183 atomic_set(&sess->session_reinstatement, 0);
4184 atomic_set(&sess->session_fall_back_to_erl0, 1);
4185 }
4186
4187 spin_lock_bh(&sess->conn_lock);
4188 list_del(&conn->conn_list);
4189
4190 /*
4191 * Attempt to let the Initiator know this connection failed by
4192 * sending an Connection Dropped Async Message on another
4193 * active connection.
4194 */
4195 if (atomic_read(&conn->connection_recovery))
4196 iscsit_build_conn_drop_async_message(conn);
4197
4198 spin_unlock_bh(&sess->conn_lock);
4199
4200 /*
4201 * If connection reinstatement is being performed on this connection,
4202 * up the connection reinstatement semaphore that is being blocked on
4203 * in iscsit_cause_connection_reinstatement().
4204 */
4205 spin_lock_bh(&conn->state_lock);
4206 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4207 spin_unlock_bh(&conn->state_lock);
4208 complete(&conn->conn_wait_comp);
4209 wait_for_completion(&conn->conn_post_wait_comp);
4210 spin_lock_bh(&conn->state_lock);
4211 }
4212
4213 /*
4214 * If connection reinstatement is being performed on this connection
4215 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4216 * connection wait rcfr semaphore that is being blocked on
4217 * an iscsit_connection_reinstatement_rcfr().
4218 */
4219 if (atomic_read(&conn->connection_wait_rcfr)) {
4220 spin_unlock_bh(&conn->state_lock);
4221 complete(&conn->conn_wait_rcfr_comp);
4222 wait_for_completion(&conn->conn_post_wait_comp);
4223 spin_lock_bh(&conn->state_lock);
4224 }
4225 atomic_set(&conn->connection_reinstatement, 1);
4226 spin_unlock_bh(&conn->state_lock);
4227
4228 /*
4229 * If any other processes are accessing this connection pointer we
4230 * must wait until they have completed.
4231 */
4232 iscsit_check_conn_usage_count(conn);
4233
Herbert Xu69110e32016-01-24 21:19:52 +08004234 ahash_request_free(conn->conn_tx_hash);
4235 if (conn->conn_rx_hash) {
4236 struct crypto_ahash *tfm;
4237
4238 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4239 ahash_request_free(conn->conn_rx_hash);
4240 crypto_free_ahash(tfm);
4241 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004242
Joern Engelfbecb652014-09-02 17:49:47 -04004243 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004244
4245 kfree(conn->conn_ops);
4246 conn->conn_ops = NULL;
4247
Al Virobf6932f2012-07-21 08:55:18 +01004248 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004249 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004250
4251 if (conn->conn_transport->iscsit_free_conn)
4252 conn->conn_transport->iscsit_free_conn(conn);
4253
4254 iscsit_put_transport(conn->conn_transport);
4255
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004256 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4257 conn->conn_state = TARG_CONN_STATE_FREE;
4258 kfree(conn);
4259
4260 spin_lock_bh(&sess->conn_lock);
4261 atomic_dec(&sess->nconn);
4262 pr_debug("Decremented iSCSI connection count to %hu from node:"
4263 " %s\n", atomic_read(&sess->nconn),
4264 sess->sess_ops->InitiatorName);
4265 /*
4266 * Make sure that if one connection fails in an non ERL=2 iSCSI
4267 * Session that they all fail.
4268 */
4269 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4270 !atomic_read(&sess->session_logout))
4271 atomic_set(&sess->session_fall_back_to_erl0, 1);
4272
4273 /*
4274 * If this was not the last connection in the session, and we are
4275 * performing session reinstatement or falling back to ERL=0, call
4276 * iscsit_stop_session() without sleeping to shutdown the other
4277 * active connections.
4278 */
4279 if (atomic_read(&sess->nconn)) {
4280 if (!atomic_read(&sess->session_reinstatement) &&
4281 !atomic_read(&sess->session_fall_back_to_erl0)) {
4282 spin_unlock_bh(&sess->conn_lock);
4283 return 0;
4284 }
4285 if (!atomic_read(&sess->session_stop_active)) {
4286 atomic_set(&sess->session_stop_active, 1);
4287 spin_unlock_bh(&sess->conn_lock);
4288 iscsit_stop_session(sess, 0, 0);
4289 return 0;
4290 }
4291 spin_unlock_bh(&sess->conn_lock);
4292 return 0;
4293 }
4294
4295 /*
4296 * If this was the last connection in the session and one of the
4297 * following is occurring:
4298 *
4299 * Session Reinstatement is not being performed, and are falling back
4300 * to ERL=0 call iscsit_close_session().
4301 *
4302 * Session Logout was requested. iscsit_close_session() will be called
4303 * elsewhere.
4304 *
4305 * Session Continuation is not being performed, start the Time2Retain
4306 * handler and check if sleep_on_sess_wait_sem is active.
4307 */
4308 if (!atomic_read(&sess->session_reinstatement) &&
4309 atomic_read(&sess->session_fall_back_to_erl0)) {
4310 spin_unlock_bh(&sess->conn_lock);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004311 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004312
4313 return 0;
4314 } else if (atomic_read(&sess->session_logout)) {
4315 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4316 sess->session_state = TARG_SESS_STATE_FREE;
4317 spin_unlock_bh(&sess->conn_lock);
4318
4319 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4320 complete(&sess->session_wait_comp);
4321
4322 return 0;
4323 } else {
4324 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4325 sess->session_state = TARG_SESS_STATE_FAILED;
4326
4327 if (!atomic_read(&sess->session_continuation)) {
4328 spin_unlock_bh(&sess->conn_lock);
4329 iscsit_start_time2retain_handler(sess);
4330 } else
4331 spin_unlock_bh(&sess->conn_lock);
4332
4333 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4334 complete(&sess->session_wait_comp);
4335
4336 return 0;
4337 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004338}
4339
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004340/*
4341 * If the iSCSI Session for the iSCSI Initiator Node exists,
4342 * forcefully shutdown the iSCSI NEXUS.
4343 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004344int iscsit_close_session(struct iscsi_session *sess)
4345{
Andy Grover60bfcf82013-10-09 11:05:58 -07004346 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004347 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4348
4349 if (atomic_read(&sess->nconn)) {
4350 pr_err("%d connection(s) still exist for iSCSI session"
4351 " to %s\n", atomic_read(&sess->nconn),
4352 sess->sess_ops->InitiatorName);
4353 BUG();
4354 }
4355
4356 spin_lock_bh(&se_tpg->session_lock);
4357 atomic_set(&sess->session_logout, 1);
4358 atomic_set(&sess->session_reinstatement, 1);
4359 iscsit_stop_time2retain_timer(sess);
4360 spin_unlock_bh(&se_tpg->session_lock);
4361
4362 /*
4363 * transport_deregister_session_configfs() will clear the
4364 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4365 * can be setting it again with __transport_register_session() in
4366 * iscsi_post_login_handler() again after the iscsit_stop_session()
4367 * completes in iscsi_np context.
4368 */
4369 transport_deregister_session_configfs(sess->se_sess);
4370
4371 /*
4372 * If any other processes are accessing this session pointer we must
4373 * wait until they have completed. If we are in an interrupt (the
4374 * time2retain handler) and contain and active session usage count we
4375 * restart the timer and exit.
4376 */
4377 if (!in_interrupt()) {
4378 if (iscsit_check_session_usage_count(sess) == 1)
4379 iscsit_stop_session(sess, 1, 1);
4380 } else {
4381 if (iscsit_check_session_usage_count(sess) == 2) {
4382 atomic_set(&sess->session_logout, 0);
4383 iscsit_start_time2retain_handler(sess);
4384 return 0;
4385 }
4386 }
4387
4388 transport_deregister_session(sess->se_sess);
4389
4390 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4391 iscsit_free_connection_recovery_entires(sess);
4392
4393 iscsit_free_all_ooo_cmdsns(sess);
4394
4395 spin_lock_bh(&se_tpg->session_lock);
4396 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4397 sess->session_state = TARG_SESS_STATE_FREE;
4398 pr_debug("Released iSCSI session from node: %s\n",
4399 sess->sess_ops->InitiatorName);
4400 tpg->nsessions--;
4401 if (tpg->tpg_tiqn)
4402 tpg->tpg_tiqn->tiqn_nsessions--;
4403
4404 pr_debug("Decremented number of active iSCSI Sessions on"
4405 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4406
4407 spin_lock(&sess_idr_lock);
4408 idr_remove(&sess_idr, sess->session_index);
4409 spin_unlock(&sess_idr_lock);
4410
4411 kfree(sess->sess_ops);
4412 sess->sess_ops = NULL;
4413 spin_unlock_bh(&se_tpg->session_lock);
4414
4415 kfree(sess);
4416 return 0;
4417}
4418
4419static void iscsit_logout_post_handler_closesession(
4420 struct iscsi_conn *conn)
4421{
4422 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004423 int sleep = 1;
4424 /*
4425 * Traditional iscsi/tcp will invoke this logic from TX thread
4426 * context during session logout, so clear tx_thread_active and
4427 * sleep if iscsit_close_connection() has not already occured.
4428 *
4429 * Since iser-target invokes this logic from it's own workqueue,
4430 * always sleep waiting for RX/TX thread shutdown to complete
4431 * within iscsit_close_connection().
4432 */
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004433 if (!conn->conn_transport->rdma_shutdown) {
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004434 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004435 if (!sleep)
4436 return;
4437 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004438
4439 atomic_set(&conn->conn_logout_remove, 0);
4440 complete(&conn->conn_logout_comp);
4441
4442 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004443 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004444 iscsit_dec_session_usage_count(sess);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004445 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004446}
4447
4448static void iscsit_logout_post_handler_samecid(
4449 struct iscsi_conn *conn)
4450{
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004451 int sleep = 1;
4452
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004453 if (!conn->conn_transport->rdma_shutdown) {
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004454 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004455 if (!sleep)
4456 return;
4457 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004458
4459 atomic_set(&conn->conn_logout_remove, 0);
4460 complete(&conn->conn_logout_comp);
4461
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004462 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004463 iscsit_dec_conn_usage_count(conn);
4464}
4465
4466static void iscsit_logout_post_handler_diffcid(
4467 struct iscsi_conn *conn,
4468 u16 cid)
4469{
4470 struct iscsi_conn *l_conn;
4471 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004472 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004473
4474 if (!sess)
4475 return;
4476
4477 spin_lock_bh(&sess->conn_lock);
4478 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4479 if (l_conn->cid == cid) {
4480 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004481 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004482 break;
4483 }
4484 }
4485 spin_unlock_bh(&sess->conn_lock);
4486
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004487 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004488 return;
4489
4490 if (l_conn->sock)
4491 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4492
4493 spin_lock_bh(&l_conn->state_lock);
4494 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4495 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4496 spin_unlock_bh(&l_conn->state_lock);
4497
4498 iscsit_cause_connection_reinstatement(l_conn, 1);
4499 iscsit_dec_conn_usage_count(l_conn);
4500}
4501
4502/*
4503 * Return of 0 causes the TX thread to restart.
4504 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004505int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004506 struct iscsi_cmd *cmd,
4507 struct iscsi_conn *conn)
4508{
4509 int ret = 0;
4510
4511 switch (cmd->logout_reason) {
4512 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4513 switch (cmd->logout_response) {
4514 case ISCSI_LOGOUT_SUCCESS:
4515 case ISCSI_LOGOUT_CLEANUP_FAILED:
4516 default:
4517 iscsit_logout_post_handler_closesession(conn);
4518 break;
4519 }
4520 ret = 0;
4521 break;
4522 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4523 if (conn->cid == cmd->logout_cid) {
4524 switch (cmd->logout_response) {
4525 case ISCSI_LOGOUT_SUCCESS:
4526 case ISCSI_LOGOUT_CLEANUP_FAILED:
4527 default:
4528 iscsit_logout_post_handler_samecid(conn);
4529 break;
4530 }
4531 ret = 0;
4532 } else {
4533 switch (cmd->logout_response) {
4534 case ISCSI_LOGOUT_SUCCESS:
4535 iscsit_logout_post_handler_diffcid(conn,
4536 cmd->logout_cid);
4537 break;
4538 case ISCSI_LOGOUT_CID_NOT_FOUND:
4539 case ISCSI_LOGOUT_CLEANUP_FAILED:
4540 default:
4541 break;
4542 }
4543 ret = 1;
4544 }
4545 break;
4546 case ISCSI_LOGOUT_REASON_RECOVERY:
4547 switch (cmd->logout_response) {
4548 case ISCSI_LOGOUT_SUCCESS:
4549 case ISCSI_LOGOUT_CID_NOT_FOUND:
4550 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4551 case ISCSI_LOGOUT_CLEANUP_FAILED:
4552 default:
4553 break;
4554 }
4555 ret = 1;
4556 break;
4557 default:
4558 break;
4559
4560 }
4561 return ret;
4562}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004563EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004564
4565void iscsit_fail_session(struct iscsi_session *sess)
4566{
4567 struct iscsi_conn *conn;
4568
4569 spin_lock_bh(&sess->conn_lock);
4570 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4571 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4572 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4573 }
4574 spin_unlock_bh(&sess->conn_lock);
4575
4576 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4577 sess->session_state = TARG_SESS_STATE_FAILED;
4578}
4579
4580int iscsit_free_session(struct iscsi_session *sess)
4581{
4582 u16 conn_count = atomic_read(&sess->nconn);
4583 struct iscsi_conn *conn, *conn_tmp = NULL;
4584 int is_last;
4585
4586 spin_lock_bh(&sess->conn_lock);
4587 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4588
4589 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4590 conn_list) {
4591 if (conn_count == 0)
4592 break;
4593
4594 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4595 is_last = 1;
4596 } else {
4597 iscsit_inc_conn_usage_count(conn_tmp);
4598 is_last = 0;
4599 }
4600 iscsit_inc_conn_usage_count(conn);
4601
4602 spin_unlock_bh(&sess->conn_lock);
4603 iscsit_cause_connection_reinstatement(conn, 1);
4604 spin_lock_bh(&sess->conn_lock);
4605
4606 iscsit_dec_conn_usage_count(conn);
4607 if (is_last == 0)
4608 iscsit_dec_conn_usage_count(conn_tmp);
4609
4610 conn_count--;
4611 }
4612
4613 if (atomic_read(&sess->nconn)) {
4614 spin_unlock_bh(&sess->conn_lock);
4615 wait_for_completion(&sess->session_wait_comp);
4616 } else
4617 spin_unlock_bh(&sess->conn_lock);
4618
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004619 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004620 return 0;
4621}
4622
4623void iscsit_stop_session(
4624 struct iscsi_session *sess,
4625 int session_sleep,
4626 int connection_sleep)
4627{
4628 u16 conn_count = atomic_read(&sess->nconn);
4629 struct iscsi_conn *conn, *conn_tmp = NULL;
4630 int is_last;
4631
4632 spin_lock_bh(&sess->conn_lock);
4633 if (session_sleep)
4634 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4635
4636 if (connection_sleep) {
4637 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4638 conn_list) {
4639 if (conn_count == 0)
4640 break;
4641
4642 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4643 is_last = 1;
4644 } else {
4645 iscsit_inc_conn_usage_count(conn_tmp);
4646 is_last = 0;
4647 }
4648 iscsit_inc_conn_usage_count(conn);
4649
4650 spin_unlock_bh(&sess->conn_lock);
4651 iscsit_cause_connection_reinstatement(conn, 1);
4652 spin_lock_bh(&sess->conn_lock);
4653
4654 iscsit_dec_conn_usage_count(conn);
4655 if (is_last == 0)
4656 iscsit_dec_conn_usage_count(conn_tmp);
4657 conn_count--;
4658 }
4659 } else {
4660 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4661 iscsit_cause_connection_reinstatement(conn, 0);
4662 }
4663
4664 if (session_sleep && atomic_read(&sess->nconn)) {
4665 spin_unlock_bh(&sess->conn_lock);
4666 wait_for_completion(&sess->session_wait_comp);
4667 } else
4668 spin_unlock_bh(&sess->conn_lock);
4669}
4670
4671int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4672{
4673 struct iscsi_session *sess;
4674 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4675 struct se_session *se_sess, *se_sess_tmp;
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004676 LIST_HEAD(free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004677 int session_count = 0;
4678
4679 spin_lock_bh(&se_tpg->session_lock);
4680 if (tpg->nsessions && !force) {
4681 spin_unlock_bh(&se_tpg->session_lock);
4682 return -1;
4683 }
4684
4685 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4686 sess_list) {
4687 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4688
4689 spin_lock(&sess->conn_lock);
4690 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4691 atomic_read(&sess->session_logout) ||
4692 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4693 spin_unlock(&sess->conn_lock);
4694 continue;
4695 }
4696 atomic_set(&sess->session_reinstatement, 1);
Nicholas Bellinger197b8062017-04-25 10:55:12 -07004697 atomic_set(&sess->session_fall_back_to_erl0, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004698 spin_unlock(&sess->conn_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004699
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004700 list_move_tail(&se_sess->sess_list, &free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004701 }
4702 spin_unlock_bh(&se_tpg->session_lock);
4703
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004704 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4705 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4706
4707 iscsit_free_session(sess);
4708 session_count++;
4709 }
4710
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004711 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4712 " Group: %hu\n", session_count, tpg->tpgt);
4713 return 0;
4714}
4715
4716MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4717MODULE_VERSION("4.1.x");
4718MODULE_AUTHOR("nab@Linux-iSCSI.org");
4719MODULE_LICENSE("GPL");
4720
4721module_init(iscsi_target_init_module);
4722module_exit(iscsi_target_cleanup_module);