blob: 735d4ff2d0cec1d6f794761354c106c452aa12ca [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 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001252 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001253 if (cmd->reject_reason)
1254 return 0;
1255
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001256 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001257 }
1258 /*
1259 * Call directly into transport_generic_new_cmd() to perform
1260 * the backend memory allocation.
1261 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001262 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001263 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001264 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001265
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001266 return 0;
1267}
1268EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1269
1270static int
1271iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1272 bool dump_payload)
1273{
1274 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1275 /*
1276 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1277 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001278 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001279 goto after_immediate_data;
Nicholas Bellingerabb85a92017-06-07 20:29:50 -07001280 /*
1281 * Check for underflow case where both EDTL and immediate data payload
1282 * exceeds what is presented by CDB's TRANSFER LENGTH, and what has
1283 * already been set in target_cmd_size_check() as se_cmd->data_length.
1284 *
1285 * For this special case, fail the command and dump the immediate data
1286 * payload.
1287 */
1288 if (cmd->first_burst_len > cmd->se_cmd.data_length) {
1289 cmd->sense_reason = TCM_INVALID_CDB_FIELD;
1290 goto after_immediate_data;
1291 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001292
1293 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1294 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001295after_immediate_data:
1296 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1297 /*
1298 * A PDU/CmdSN carrying Immediate Data passed
1299 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1300 * Immediate Bit is not set.
1301 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001302 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1303 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001304 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001305 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001306
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001307 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001308 int rc;
1309
1310 rc = iscsit_dump_data_payload(cmd->conn,
1311 cmd->first_burst_len, 1);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001312 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001313 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001314 } else if (cmd->unsolicited_data)
1315 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001316
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001317 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1318 /*
1319 * Immediate Data failed DataCRC and ERL>=1,
1320 * silently drop this PDU and let the initiator
1321 * plug the CmdSN gap.
1322 *
1323 * FIXME: Send Unsolicited NOPIN with reserved
1324 * TTT here to help the initiator figure out
1325 * the missing CmdSN, although they should be
1326 * intelligent enough to determine the missing
1327 * CmdSN and issue a retry to plug the sequence.
1328 */
1329 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001330 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001331 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1332 return -1;
1333
1334 return 0;
1335}
1336
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001337static int
1338iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1339 unsigned char *buf)
1340{
1341 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1342 int rc, immed_data;
1343 bool dump_payload = false;
1344
1345 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1346 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001347 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001348 /*
1349 * Allocation iovecs needed for struct socket operations for
1350 * traditional iSCSI block I/O.
1351 */
1352 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001353 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001354 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001355 }
1356 immed_data = cmd->immediate_data;
1357
1358 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1359 if (rc < 0)
1360 return rc;
1361 else if (rc > 0)
1362 dump_payload = true;
1363
1364 if (!immed_data)
1365 return 0;
1366
1367 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1368}
1369
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001370static u32 iscsit_do_crypto_hash_sg(
Herbert Xu69110e32016-01-24 21:19:52 +08001371 struct ahash_request *hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001372 struct iscsi_cmd *cmd,
1373 u32 data_offset,
1374 u32 data_length,
1375 u32 padding,
1376 u8 *pad_bytes)
1377{
1378 u32 data_crc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001379 struct scatterlist *sg;
1380 unsigned int page_off;
1381
Herbert Xu69110e32016-01-24 21:19:52 +08001382 crypto_ahash_init(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001383
1384 sg = cmd->first_data_sg;
1385 page_off = cmd->first_data_sg_off;
1386
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001387 while (data_length) {
Alexei Potashnikaa756792015-07-20 17:12:12 -07001388 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001389
Herbert Xu69110e32016-01-24 21:19:52 +08001390 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1391 crypto_ahash_update(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001392
1393 data_length -= cur_len;
1394 page_off = 0;
Alexei Potashnikaa756792015-07-20 17:12:12 -07001395 /* iscsit_map_iovec has already checked for invalid sg pointers */
1396 sg = sg_next(sg);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001397 }
1398
1399 if (padding) {
1400 struct scatterlist pad_sg;
1401
1402 sg_init_one(&pad_sg, pad_bytes, padding);
Herbert Xu69110e32016-01-24 21:19:52 +08001403 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1404 padding);
1405 crypto_ahash_finup(hash);
1406 } else {
1407 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1408 crypto_ahash_final(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001409 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001410
1411 return data_crc;
1412}
1413
1414static void iscsit_do_crypto_hash_buf(
Herbert Xu69110e32016-01-24 21:19:52 +08001415 struct ahash_request *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001416 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001417 u32 payload_length,
1418 u32 padding,
1419 u8 *pad_bytes,
1420 u8 *data_crc)
1421{
Herbert Xu69110e32016-01-24 21:19:52 +08001422 struct scatterlist sg[2];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001423
Herbert Xu69110e32016-01-24 21:19:52 +08001424 sg_init_table(sg, ARRAY_SIZE(sg));
1425 sg_set_buf(sg, buf, payload_length);
1426 sg_set_buf(sg + 1, pad_bytes, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001427
Herbert Xu69110e32016-01-24 21:19:52 +08001428 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001429
Herbert Xu69110e32016-01-24 21:19:52 +08001430 crypto_ahash_digest(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001431}
1432
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001433int
Varun Prakash9a584bf2017-01-13 20:53:21 +05301434__iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1435 struct iscsi_cmd *cmd, u32 payload_length,
1436 bool *success)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001437{
Varun Prakash9a584bf2017-01-13 20:53:21 +05301438 struct iscsi_data *hdr = buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001439 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001440 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001441
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001442 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001443 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001444
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001445 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1446 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001447 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001448 payload_length, conn->cid);
1449
1450 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1451 pr_err("Command ITT: 0x%08x received DataOUT after"
1452 " last DataOUT received, dumping payload\n",
1453 cmd->init_task_tag);
1454 return iscsit_dump_data_payload(conn, payload_length, 1);
1455 }
1456
1457 if (cmd->data_direction != DMA_TO_DEVICE) {
1458 pr_err("Command ITT: 0x%08x received DataOUT for a"
1459 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001460 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001461 }
1462 se_cmd = &cmd->se_cmd;
1463 iscsit_mod_dataout_timer(cmd);
1464
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001465 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001466 pr_err("DataOut Offset: %u, Length %u greater than"
1467 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001468 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001469 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001470 }
1471
1472 if (cmd->unsolicited_data) {
1473 int dump_unsolicited_data = 0;
1474
1475 if (conn->sess->sess_ops->InitialR2T) {
1476 pr_err("Received unexpected unsolicited data"
1477 " while InitialR2T=Yes, protocol error.\n");
1478 transport_send_check_condition_and_sense(&cmd->se_cmd,
1479 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1480 return -1;
1481 }
1482 /*
1483 * Special case for dealing with Unsolicited DataOUT
1484 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1485 * failures;
1486 */
1487
1488 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001489 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001490 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001491 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001492
1493 if (dump_unsolicited_data) {
1494 /*
1495 * Check if a delayed TASK_ABORTED status needs to
1496 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
Bart Van Assche5a342522015-10-22 15:53:22 -07001497 * received with the unsolicited data out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001498 */
1499 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1500 iscsit_stop_dataout_timer(cmd);
1501
1502 transport_check_aborted_status(se_cmd,
1503 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1504 return iscsit_dump_data_payload(conn, payload_length, 1);
1505 }
1506 } else {
1507 /*
1508 * For the normal solicited data path:
1509 *
1510 * Check for a delayed TASK_ABORTED status and dump any
1511 * incoming data out payload if one exists. Also, when the
1512 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1513 * data out sequence, we decrement outstanding_r2ts. Once
1514 * outstanding_r2ts reaches zero, go ahead and send the delayed
1515 * TASK_ABORTED status.
1516 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001517 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001518 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1519 if (--cmd->outstanding_r2ts < 1) {
1520 iscsit_stop_dataout_timer(cmd);
1521 transport_check_aborted_status(
1522 se_cmd, 1);
1523 }
1524
1525 return iscsit_dump_data_payload(conn, payload_length, 1);
1526 }
1527 }
1528 /*
Bart Van Assche0d5efb82016-12-23 14:37:52 +01001529 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001530 * within-command recovery checks before receiving the payload.
1531 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001532 rc = iscsit_check_pre_dataout(cmd, buf);
1533 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001534 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001535 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001536 return -1;
Varun Prakash9a584bf2017-01-13 20:53:21 +05301537 *success = true;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001538 return 0;
1539}
Varun Prakash9a584bf2017-01-13 20:53:21 +05301540EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
1541
1542int
1543iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1544 struct iscsi_cmd **out_cmd)
1545{
1546 struct iscsi_data *hdr = buf;
1547 struct iscsi_cmd *cmd;
1548 u32 payload_length = ntoh24(hdr->dlength);
1549 int rc;
1550 bool success = false;
1551
1552 if (!payload_length) {
1553 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1554 return 0;
1555 }
1556
1557 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1558 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1559 " MaxXmitDataSegmentLength: %u\n", payload_length,
1560 conn->conn_ops->MaxXmitDataSegmentLength);
1561 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
1562 }
1563
1564 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
1565 if (!cmd)
1566 return 0;
1567
1568 rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
1569
1570 if (success)
1571 *out_cmd = cmd;
1572
1573 return rc;
1574}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001575EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1576
1577static int
1578iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1579 struct iscsi_data *hdr)
1580{
1581 struct kvec *iov;
1582 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1583 u32 payload_length = ntoh24(hdr->dlength);
1584 int iov_ret, data_crc_failed = 0;
1585
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001586 rx_size += payload_length;
1587 iov = &cmd->iov_data[0];
1588
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001589 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1590 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001591 if (iov_ret < 0)
1592 return -1;
1593
1594 iov_count += iov_ret;
1595
1596 padding = ((-payload_length) & 3);
1597 if (padding != 0) {
1598 iov[iov_count].iov_base = cmd->pad_bytes;
1599 iov[iov_count++].iov_len = padding;
1600 rx_size += padding;
1601 pr_debug("Receiving %u padding bytes.\n", padding);
1602 }
1603
1604 if (conn->conn_ops->DataDigest) {
1605 iov[iov_count].iov_base = &checksum;
1606 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1607 rx_size += ISCSI_CRC_LEN;
1608 }
1609
1610 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1611
1612 iscsit_unmap_iovec(cmd);
1613
1614 if (rx_got != rx_size)
1615 return -1;
1616
1617 if (conn->conn_ops->DataDigest) {
1618 u32 data_crc;
1619
Herbert Xu69110e32016-01-24 21:19:52 +08001620 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001621 be32_to_cpu(hdr->offset),
1622 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001623 cmd->pad_bytes);
1624
1625 if (checksum != data_crc) {
1626 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1627 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1628 " does not match computed 0x%08x\n",
1629 hdr->itt, hdr->offset, payload_length,
1630 hdr->datasn, checksum, data_crc);
1631 data_crc_failed = 1;
1632 } else {
1633 pr_debug("Got CRC32C DataDigest 0x%08x for"
1634 " %u bytes of Data Out\n", checksum,
1635 payload_length);
1636 }
1637 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001638
1639 return data_crc_failed;
1640}
1641
1642int
1643iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1644 bool data_crc_failed)
1645{
1646 struct iscsi_conn *conn = cmd->conn;
1647 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001648 /*
1649 * Increment post receive data and CRC values or perform
1650 * within-command recovery.
1651 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001652 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1653 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001654 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001655 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001656 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001657 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1658 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001659 /*
1660 * Handle extra special case for out of order
1661 * Unsolicited Data Out.
1662 */
1663 spin_lock_bh(&cmd->istate_lock);
1664 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1665 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1666 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1667 spin_unlock_bh(&cmd->istate_lock);
1668
1669 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001670 if (ooo_cmdsn)
1671 return 0;
1672 target_execute_cmd(&cmd->se_cmd);
1673 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001674 } else /* DATAOUT_CANNOT_RECOVER */
1675 return -1;
1676
1677 return 0;
1678}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001679EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001680
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001681static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1682{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001683 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001684 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1685 int rc;
1686 bool data_crc_failed = false;
1687
1688 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1689 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001690 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001691 else if (!cmd)
1692 return 0;
1693
1694 rc = iscsit_get_dataout(conn, cmd, hdr);
1695 if (rc < 0)
1696 return rc;
1697 else if (rc > 0)
1698 data_crc_failed = true;
1699
1700 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1701}
1702
Nicholas Bellinger778de362013-06-14 16:07:47 -07001703int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1704 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001705{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001706 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001707
Arshad Hussaina3662602014-03-14 15:28:59 -07001708 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1709 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1710 if (!cmd)
1711 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1712 (unsigned char *)hdr);
1713
1714 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1715 (unsigned char *)hdr);
1716 }
1717
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001718 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001719 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1720 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001721 if (!cmd)
1722 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1723 (unsigned char *)hdr);
1724
Nicholas Bellingerba159912013-07-03 03:48:24 -07001725 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1726 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001727 }
1728
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001729 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001730 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001731 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001732 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001733 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001734 if (!cmd)
1735 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1736 (unsigned char *)hdr);
1737
Nicholas Bellingerba159912013-07-03 03:48:24 -07001738 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1739 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001740 }
1741
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001742 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001743 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001744 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001745 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1746 payload_length);
1747 /*
1748 * This is not a response to a Unsolicited NopIN, which means
1749 * it can either be a NOPOUT ping request (with a valid ITT),
1750 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1751 * Either way, make sure we allocate an struct iscsi_cmd, as both
1752 * can contain ping data.
1753 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001754 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1756 cmd->i_state = ISTATE_SEND_NOPIN;
1757 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1758 1 : 0);
1759 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1760 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001761 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1762 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001763 cmd->data_direction = DMA_NONE;
1764 }
1765
Nicholas Bellinger778de362013-06-14 16:07:47 -07001766 return 0;
1767}
1768EXPORT_SYMBOL(iscsit_setup_nop_out);
1769
1770int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1771 struct iscsi_nopout *hdr)
1772{
1773 struct iscsi_cmd *cmd_p = NULL;
1774 int cmdsn_ret = 0;
1775 /*
1776 * Initiator is expecting a NopIN ping reply..
1777 */
1778 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001779 if (!cmd)
1780 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1781 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001782
1783 spin_lock_bh(&conn->cmd_lock);
1784 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1785 spin_unlock_bh(&conn->cmd_lock);
1786
1787 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1788
1789 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1790 iscsit_add_cmd_to_response_queue(cmd, conn,
1791 cmd->i_state);
1792 return 0;
1793 }
1794
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001795 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1796 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001797 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1798 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001799 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001800 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001801
1802 return 0;
1803 }
1804 /*
1805 * This was a response to a unsolicited NOPIN ping.
1806 */
1807 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1808 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1809 if (!cmd_p)
1810 return -EINVAL;
1811
1812 iscsit_stop_nopin_response_timer(conn);
1813
1814 cmd_p->i_state = ISTATE_REMOVE;
1815 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1816
1817 iscsit_start_nopin_timer(conn);
1818 return 0;
1819 }
1820 /*
1821 * Otherwise, initiator is not expecting a NOPIN is response.
1822 * Just ignore for now.
1823 */
Varun Prakash1a40f0a2016-09-15 21:20:11 +05301824
1825 if (cmd)
1826 iscsit_free_cmd(cmd, false);
1827
Nicholas Bellinger778de362013-06-14 16:07:47 -07001828 return 0;
1829}
1830EXPORT_SYMBOL(iscsit_process_nop_out);
1831
1832static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1833 unsigned char *buf)
1834{
1835 unsigned char *ping_data = NULL;
1836 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1837 struct kvec *iov = NULL;
1838 u32 payload_length = ntoh24(hdr->dlength);
1839 int ret;
1840
1841 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1842 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001843 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001844 /*
1845 * Handle NOP-OUT payload for traditional iSCSI sockets
1846 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001847 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001848 u32 checksum, data_crc, padding = 0;
1849 int niov = 0, rx_got, rx_size = payload_length;
1850
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001851 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1852 if (!ping_data) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001853 ret = -1;
1854 goto out;
1855 }
1856
1857 iov = &cmd->iov_misc[0];
1858 iov[niov].iov_base = ping_data;
1859 iov[niov++].iov_len = payload_length;
1860
1861 padding = ((-payload_length) & 3);
1862 if (padding != 0) {
1863 pr_debug("Receiving %u additional bytes"
1864 " for padding.\n", padding);
1865 iov[niov].iov_base = &cmd->pad_bytes;
1866 iov[niov++].iov_len = padding;
1867 rx_size += padding;
1868 }
1869 if (conn->conn_ops->DataDigest) {
1870 iov[niov].iov_base = &checksum;
1871 iov[niov++].iov_len = ISCSI_CRC_LEN;
1872 rx_size += ISCSI_CRC_LEN;
1873 }
1874
1875 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1876 if (rx_got != rx_size) {
1877 ret = -1;
1878 goto out;
1879 }
1880
1881 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08001882 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001883 ping_data, payload_length,
1884 padding, cmd->pad_bytes,
1885 (u8 *)&data_crc);
1886
1887 if (checksum != data_crc) {
1888 pr_err("Ping data CRC32C DataDigest"
1889 " 0x%08x does not match computed 0x%08x\n",
1890 checksum, data_crc);
1891 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1892 pr_err("Unable to recover from"
1893 " NOPOUT Ping DataCRC failure while in"
1894 " ERL=0.\n");
1895 ret = -1;
1896 goto out;
1897 } else {
1898 /*
1899 * Silently drop this PDU and let the
1900 * initiator plug the CmdSN gap.
1901 */
1902 pr_debug("Dropping NOPOUT"
1903 " Command CmdSN: 0x%08x due to"
1904 " DataCRC error.\n", hdr->cmdsn);
1905 ret = 0;
1906 goto out;
1907 }
1908 } else {
1909 pr_debug("Got CRC32C DataDigest"
1910 " 0x%08x for %u bytes of ping data.\n",
1911 checksum, payload_length);
1912 }
1913 }
1914
1915 ping_data[payload_length] = '\0';
1916 /*
1917 * Attach ping data to struct iscsi_cmd->buf_ptr.
1918 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001919 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001920 cmd->buf_ptr_size = payload_length;
1921
1922 pr_debug("Got %u bytes of NOPOUT ping"
1923 " data.\n", payload_length);
1924 pr_debug("Ping Data: \"%s\"\n", ping_data);
1925 }
1926
Nicholas Bellinger778de362013-06-14 16:07:47 -07001927 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001928out:
1929 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001930 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001931
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001932 kfree(ping_data);
1933 return ret;
1934}
1935
Bart Van Asschee381fe92017-01-06 11:32:08 +01001936static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
1937{
1938 switch (iscsi_tmf) {
1939 case ISCSI_TM_FUNC_ABORT_TASK:
1940 return TMR_ABORT_TASK;
1941 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1942 return TMR_ABORT_TASK_SET;
1943 case ISCSI_TM_FUNC_CLEAR_ACA:
1944 return TMR_CLEAR_ACA;
1945 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1946 return TMR_CLEAR_TASK_SET;
1947 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1948 return TMR_LUN_RESET;
1949 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1950 return TMR_TARGET_WARM_RESET;
1951 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1952 return TMR_TARGET_COLD_RESET;
1953 default:
1954 return TMR_UNKNOWN;
1955 }
1956}
1957
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001958int
1959iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1960 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001961{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001962 struct se_tmr_req *se_tmr;
1963 struct iscsi_tmr_req *tmr_req;
1964 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001965 int out_of_order_cmdsn = 0, ret;
1966 bool sess_ref = false;
Bart Van Assche59b69862017-01-05 12:39:57 +01001967 u8 function, tcm_function = TMR_UNKNOWN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001968
1969 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001970 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1971 function = hdr->flags;
1972
1973 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1974 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1975 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1976 hdr->rtt, hdr->refcmdsn, conn->cid);
1977
1978 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1979 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001980 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001981 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001982 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001983 }
1984
1985 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1986 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1987 pr_err("Task Management Request TASK_REASSIGN not"
1988 " issued as immediate command, bad iSCSI Initiator"
1989 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001990 return iscsit_add_reject_cmd(cmd,
1991 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001992 }
1993 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001994 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1995 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001996
Andy Groverd28b11692012-04-03 15:51:22 -07001997 cmd->data_direction = DMA_NONE;
Markus Elfring3829f382017-04-09 16:00:39 +02001998 cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02001999 if (!cmd->tmr_req)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002000 return iscsit_add_reject_cmd(cmd,
2001 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
2002 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002003
2004 /*
2005 * TASK_REASSIGN for ERL=2 / connection stays inside of
2006 * LIO-Target $FABRIC_MOD
2007 */
2008 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002009 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Andy Groverd28b11692012-04-03 15:51:22 -07002010 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08002011 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07002012
Bart Van Asscheafc16602015-04-27 13:52:36 +02002013 target_get_sess_cmd(&cmd->se_cmd, true);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002014 sess_ref = true;
Bart Van Asschee381fe92017-01-06 11:32:08 +01002015 tcm_function = iscsit_convert_tmf(function);
2016 if (tcm_function == TMR_UNKNOWN) {
Andy Groverd28b11692012-04-03 15:51:22 -07002017 pr_err("Unknown iSCSI TMR Function:"
2018 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002019 return iscsit_add_reject_cmd(cmd,
2020 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002021 }
Bart Van Assche59b69862017-01-05 12:39:57 +01002022 }
2023 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
2024 GFP_KERNEL);
2025 if (ret < 0)
2026 return iscsit_add_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002027 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002028
Bart Van Assche59b69862017-01-05 12:39:57 +01002029 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002030
2031 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2032 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2033 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2034 cmd->init_task_tag = hdr->itt;
2035 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002036 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2037 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002038 se_tmr = cmd->se_cmd.se_tmr_req;
2039 tmr_req = cmd->tmr_req;
2040 /*
2041 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2042 */
2043 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08002044 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
2045 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002046 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002047 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2048 goto attach;
2049 }
2050 }
2051
2052 switch (function) {
2053 case ISCSI_TM_FUNC_ABORT_TASK:
2054 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002055 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002056 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002057 break;
2058 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2059 case ISCSI_TM_FUNC_CLEAR_ACA:
2060 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2061 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2062 break;
2063 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2064 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002065 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2066 goto attach;
2067 }
2068 break;
2069 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2070 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002071 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2072 goto attach;
2073 }
2074 break;
2075 case ISCSI_TM_FUNC_TASK_REASSIGN:
2076 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2077 /*
2078 * Perform sanity checks on the ExpDataSN only if the
2079 * TASK_REASSIGN was successful.
2080 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002081 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002082 break;
2083
2084 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002085 return iscsit_add_reject_cmd(cmd,
2086 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002087 break;
2088 default:
2089 pr_err("Unknown TMR function: 0x%02x, protocol"
2090 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002091 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2092 goto attach;
2093 }
2094
2095 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2096 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2097 se_tmr->call_transport = 1;
2098attach:
2099 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002100 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002101 spin_unlock_bh(&conn->cmd_lock);
2102
2103 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002104 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002105 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
2106 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002107 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002108 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002109 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002110 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002111 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002112 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002113
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002114 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002115 return 0;
2116 /*
2117 * Found the referenced task, send to transport for processing.
2118 */
2119 if (se_tmr->call_transport)
2120 return transport_generic_handle_tmr(&cmd->se_cmd);
2121
2122 /*
2123 * Could not find the referenced LUN, task, or Task Management
2124 * command not authorized or supported. Change state and
2125 * let the tx_thread send the response.
2126 *
2127 * For connection recovery, this is also the default action for
2128 * TMR TASK_REASSIGN.
2129 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002130 if (sess_ref) {
2131 pr_debug("Handle TMR, using sess_ref=true check\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +02002132 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002133 }
2134
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002135 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2136 return 0;
2137}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002138EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002139
2140/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002141int
2142iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2143 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002144{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002145 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002146
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002147 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002148 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002149 "greater than MaxXmitDataSegmentLength %u.\n",
2150 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002151 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2152 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002153 }
2154
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08002155 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2156 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2157 pr_err("Multi sequence text commands currently not supported\n");
2158 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2159 (unsigned char *)hdr);
2160 }
2161
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002162 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2163 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2164 hdr->exp_statsn, payload_length);
2165
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002166 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2167 cmd->i_state = ISTATE_SEND_TEXTRSP;
2168 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2169 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2170 cmd->targ_xfer_tag = 0xFFFFFFFF;
2171 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2172 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2173 cmd->data_direction = DMA_NONE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002174 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002175
2176 return 0;
2177}
2178EXPORT_SYMBOL(iscsit_setup_text_cmd);
2179
2180int
2181iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2182 struct iscsi_text *hdr)
2183{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002184 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002185 int cmdsn_ret;
2186
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002187 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002188 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2189 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2190 pr_err("Unable to locate text_in buffer for sendtargets"
2191 " discovery\n");
2192 goto reject;
2193 }
2194 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002195 }
2196 if (strncmp("SendTargets", text_in, 11) != 0) {
2197 pr_err("Received Text Data that is not"
2198 " SendTargets, cannot continue.\n");
2199 goto reject;
2200 }
2201 text_ptr = strchr(text_in, '=');
2202 if (!text_ptr) {
2203 pr_err("No \"=\" separator found in Text Data,"
2204 " cannot continue.\n");
2205 goto reject;
2206 }
2207 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002208 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002209 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2210 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002211 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002212 } else {
2213 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2214 goto reject;
2215 }
2216
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002217 spin_lock_bh(&conn->cmd_lock);
2218 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2219 spin_unlock_bh(&conn->cmd_lock);
2220
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002221empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002222 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2223
2224 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002225 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2226 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002227 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002228 return -1;
2229
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002230 return 0;
2231 }
2232
2233 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002234
2235reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002236 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2237 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002238}
2239EXPORT_SYMBOL(iscsit_process_text_cmd);
2240
2241static int
2242iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2243 unsigned char *buf)
2244{
2245 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2246 char *text_in = NULL;
2247 u32 payload_length = ntoh24(hdr->dlength);
2248 int rx_size, rc;
2249
2250 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2251 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002252 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002253
2254 rx_size = payload_length;
2255 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002256 u32 checksum = 0, data_crc = 0;
2257 u32 padding = 0, pad_bytes = 0;
2258 int niov = 0, rx_got;
2259 struct kvec iov[3];
2260
2261 text_in = kzalloc(payload_length, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02002262 if (!text_in)
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002263 goto reject;
Markus Elfringc46e22f2017-04-09 15:34:50 +02002264
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002265 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002266
2267 memset(iov, 0, 3 * sizeof(struct kvec));
2268 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002269 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002270
2271 padding = ((-payload_length) & 3);
2272 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002273 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002274 iov[niov++].iov_len = padding;
2275 rx_size += padding;
2276 pr_debug("Receiving %u additional bytes"
2277 " for padding.\n", padding);
2278 }
2279 if (conn->conn_ops->DataDigest) {
2280 iov[niov].iov_base = &checksum;
2281 iov[niov++].iov_len = ISCSI_CRC_LEN;
2282 rx_size += ISCSI_CRC_LEN;
2283 }
2284
2285 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002286 if (rx_got != rx_size)
2287 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002288
2289 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08002290 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002291 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002292 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002293 (u8 *)&data_crc);
2294
2295 if (checksum != data_crc) {
2296 pr_err("Text data CRC32C DataDigest"
2297 " 0x%08x does not match computed"
2298 " 0x%08x\n", checksum, data_crc);
2299 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2300 pr_err("Unable to recover from"
2301 " Text Data digest failure while in"
2302 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002303 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002304 } else {
2305 /*
2306 * Silently drop this PDU and let the
2307 * initiator plug the CmdSN gap.
2308 */
2309 pr_debug("Dropping Text"
2310 " Command CmdSN: 0x%08x due to"
2311 " DataCRC error.\n", hdr->cmdsn);
2312 kfree(text_in);
2313 return 0;
2314 }
2315 } else {
2316 pr_debug("Got CRC32C DataDigest"
2317 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002318 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002319 }
2320 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002321 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002322 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002323 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002324 }
2325
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002326 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002327
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002328reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002329 kfree(cmd->text_in_ptr);
2330 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002331 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002332}
2333
2334int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2335{
2336 struct iscsi_conn *conn_p;
2337 struct iscsi_session *sess = conn->sess;
2338
2339 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2340 " for SID: %u.\n", conn->cid, conn->sess->sid);
2341
2342 atomic_set(&sess->session_logout, 1);
2343 atomic_set(&conn->conn_logout_remove, 1);
2344 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2345
2346 iscsit_inc_conn_usage_count(conn);
2347 iscsit_inc_session_usage_count(sess);
2348
2349 spin_lock_bh(&sess->conn_lock);
2350 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2351 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2352 continue;
2353
2354 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2355 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2356 }
2357 spin_unlock_bh(&sess->conn_lock);
2358
2359 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2360
2361 return 0;
2362}
2363
2364int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2365{
2366 struct iscsi_conn *l_conn;
2367 struct iscsi_session *sess = conn->sess;
2368
2369 pr_debug("Received logout request CLOSECONNECTION for CID:"
2370 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2371
2372 /*
2373 * A Logout Request with a CLOSECONNECTION reason code for a CID
2374 * can arrive on a connection with a differing CID.
2375 */
2376 if (conn->cid == cmd->logout_cid) {
2377 spin_lock_bh(&conn->state_lock);
2378 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2379 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2380
2381 atomic_set(&conn->conn_logout_remove, 1);
2382 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2383 iscsit_inc_conn_usage_count(conn);
2384
2385 spin_unlock_bh(&conn->state_lock);
2386 } else {
2387 /*
2388 * Handle all different cid CLOSECONNECTION requests in
2389 * iscsit_logout_post_handler_diffcid() as to give enough
2390 * time for any non immediate command's CmdSN to be
2391 * acknowledged on the connection in question.
2392 *
2393 * Here we simply make sure the CID is still around.
2394 */
2395 l_conn = iscsit_get_conn_from_cid(sess,
2396 cmd->logout_cid);
2397 if (!l_conn) {
2398 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2399 iscsit_add_cmd_to_response_queue(cmd, conn,
2400 cmd->i_state);
2401 return 0;
2402 }
2403
2404 iscsit_dec_conn_usage_count(l_conn);
2405 }
2406
2407 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2408
2409 return 0;
2410}
2411
2412int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2413{
2414 struct iscsi_session *sess = conn->sess;
2415
2416 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2417 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2418
2419 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2420 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2421 " while ERL!=2.\n");
2422 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2423 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2424 return 0;
2425 }
2426
2427 if (conn->cid == cmd->logout_cid) {
2428 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2429 " with CID: %hu on CID: %hu, implementation error.\n",
2430 cmd->logout_cid, conn->cid);
2431 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2432 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2433 return 0;
2434 }
2435
2436 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2437
2438 return 0;
2439}
2440
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002441int
2442iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2443 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002444{
2445 int cmdsn_ret, logout_remove = 0;
2446 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002447 struct iscsi_logout *hdr;
2448 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2449
2450 hdr = (struct iscsi_logout *) buf;
2451 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002452
2453 if (tiqn) {
2454 spin_lock(&tiqn->logout_stats.lock);
2455 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2456 tiqn->logout_stats.normal_logouts++;
2457 else
2458 tiqn->logout_stats.abnormal_logouts++;
2459 spin_unlock(&tiqn->logout_stats.lock);
2460 }
2461
2462 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2463 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2464 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2465 hdr->cid, conn->cid);
2466
2467 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2468 pr_err("Received logout request on connection that"
2469 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002470 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002471 return 0;
2472 }
2473
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002474 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2475 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2476 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2477 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2478 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002479 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2480 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2481 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002482 cmd->logout_reason = reason_code;
2483 cmd->data_direction = DMA_NONE;
2484
2485 /*
2486 * We need to sleep in these cases (by returning 1) until the Logout
2487 * Response gets sent in the tx thread.
2488 */
2489 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2490 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002491 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002492 logout_remove = 1;
2493
2494 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002495 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002496 spin_unlock_bh(&conn->cmd_lock);
2497
2498 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002499 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002500
2501 /*
2502 * Immediate commands are executed, well, immediately.
2503 * Non-Immediate Logout Commands are executed in CmdSN order.
2504 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002505 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002506 int ret = iscsit_execute_cmd(cmd, 0);
2507
2508 if (ret < 0)
2509 return ret;
2510 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002511 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002512 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002513 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002514 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2515 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002516 }
2517
2518 return logout_remove;
2519}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002520EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002521
Varun Prakashd2faaef2016-04-20 00:00:19 +05302522int iscsit_handle_snack(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002523 struct iscsi_conn *conn,
2524 unsigned char *buf)
2525{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002526 struct iscsi_snack *hdr;
2527
2528 hdr = (struct iscsi_snack *) buf;
2529 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002530
2531 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2532 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2533 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2534 hdr->begrun, hdr->runlength, conn->cid);
2535
2536 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2537 pr_err("Initiator sent SNACK request while in"
2538 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002539 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2540 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002541 }
2542 /*
2543 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2544 * call from inside iscsi_send_recovery_datain_or_r2t().
2545 */
2546 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2547 case 0:
2548 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002549 hdr->itt,
2550 be32_to_cpu(hdr->ttt),
2551 be32_to_cpu(hdr->begrun),
2552 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002553 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002554 return iscsit_handle_status_snack(conn, hdr->itt,
2555 be32_to_cpu(hdr->ttt),
2556 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002557 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002558 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2559 be32_to_cpu(hdr->begrun),
2560 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002561 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2562 /* FIXME: Support R-Data SNACK */
2563 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002564 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2565 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002566 default:
2567 pr_err("Unknown SNACK type 0x%02x, protocol"
2568 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002569 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2570 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002571 }
2572
2573 return 0;
2574}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302575EXPORT_SYMBOL(iscsit_handle_snack);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002576
2577static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2578{
2579 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2580 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2581 wait_for_completion_interruptible_timeout(
2582 &conn->rx_half_close_comp,
2583 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2584 }
2585}
2586
2587static int iscsit_handle_immediate_data(
2588 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002589 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002590 u32 length)
2591{
2592 int iov_ret, rx_got = 0, rx_size = 0;
2593 u32 checksum, iov_count = 0, padding = 0;
2594 struct iscsi_conn *conn = cmd->conn;
2595 struct kvec *iov;
2596
2597 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2598 if (iov_ret < 0)
2599 return IMMEDIATE_DATA_CANNOT_RECOVER;
2600
2601 rx_size = length;
2602 iov_count = iov_ret;
2603 iov = &cmd->iov_data[0];
2604
2605 padding = ((-length) & 3);
2606 if (padding != 0) {
2607 iov[iov_count].iov_base = cmd->pad_bytes;
2608 iov[iov_count++].iov_len = padding;
2609 rx_size += padding;
2610 }
2611
2612 if (conn->conn_ops->DataDigest) {
2613 iov[iov_count].iov_base = &checksum;
2614 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2615 rx_size += ISCSI_CRC_LEN;
2616 }
2617
2618 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2619
2620 iscsit_unmap_iovec(cmd);
2621
2622 if (rx_got != rx_size) {
2623 iscsit_rx_thread_wait_for_tcp(conn);
2624 return IMMEDIATE_DATA_CANNOT_RECOVER;
2625 }
2626
2627 if (conn->conn_ops->DataDigest) {
2628 u32 data_crc;
2629
Herbert Xu69110e32016-01-24 21:19:52 +08002630 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002631 cmd->write_data_done, length, padding,
2632 cmd->pad_bytes);
2633
2634 if (checksum != data_crc) {
2635 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2636 " does not match computed 0x%08x\n", checksum,
2637 data_crc);
2638
2639 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2640 pr_err("Unable to recover from"
2641 " Immediate Data digest failure while"
2642 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002643 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002644 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002645 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002646 return IMMEDIATE_DATA_CANNOT_RECOVER;
2647 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002648 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002649 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002650 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002651 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2652 }
2653 } else {
2654 pr_debug("Got CRC32C DataDigest 0x%08x for"
2655 " %u bytes of Immediate Data\n", checksum,
2656 length);
2657 }
2658 }
2659
2660 cmd->write_data_done += length;
2661
Andy Groverebf1d952012-04-03 15:51:24 -07002662 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002663 spin_lock_bh(&cmd->istate_lock);
2664 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2665 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2666 spin_unlock_bh(&cmd->istate_lock);
2667 }
2668
2669 return IMMEDIATE_DATA_NORMAL_OPERATION;
2670}
2671
2672/*
2673 * Called with sess->conn_lock held.
2674 */
2675/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2676 with active network interface */
2677static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2678{
2679 struct iscsi_cmd *cmd;
2680 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002681 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002682
2683 /*
2684 * Only send a Asynchronous Message on connections whos network
2685 * interface is still functional.
2686 */
2687 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2688 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2689 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002690 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002691 break;
2692 }
2693 }
2694
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002695 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002696 return;
2697
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002698 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002699 if (!cmd) {
2700 iscsit_dec_conn_usage_count(conn_p);
2701 return;
2702 }
2703
2704 cmd->logout_cid = conn->cid;
2705 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2706 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2707
2708 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002709 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002710 spin_unlock_bh(&conn_p->cmd_lock);
2711
2712 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2713 iscsit_dec_conn_usage_count(conn_p);
2714}
2715
2716static int iscsit_send_conn_drop_async_message(
2717 struct iscsi_cmd *cmd,
2718 struct iscsi_conn *conn)
2719{
2720 struct iscsi_async *hdr;
2721
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002722 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2723
2724 hdr = (struct iscsi_async *) cmd->pdu;
2725 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2726 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002727 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002728 cmd->targ_xfer_tag = 0xFFFFFFFF;
2729 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2730 cmd->stat_sn = conn->stat_sn++;
2731 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2732 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002733 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002734 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2735 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2736 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2737 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2738
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002739 pr_debug("Sending Connection Dropped Async Message StatSN:"
2740 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2741 cmd->logout_cid, conn->cid);
Varun Prakash2854bb22016-04-20 00:00:08 +05302742
2743 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002744}
2745
Andy Grover6f3c0e62012-04-03 15:51:09 -07002746static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2747{
2748 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2749 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2750 wait_for_completion_interruptible_timeout(
2751 &conn->tx_half_close_comp,
2752 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2753 }
2754}
2755
Varun Prakashd2faaef2016-04-20 00:00:19 +05302756void
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002757iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2758 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2759 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002760{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002761 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2762 hdr->flags = datain->flags;
2763 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2764 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2765 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2766 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2767 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2768 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2769 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2770 }
2771 }
2772 hton24(hdr->dlength, datain->length);
2773 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2774 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2775 (struct scsi_lun *)&hdr->lun);
2776 else
2777 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2778
2779 hdr->itt = cmd->init_task_tag;
2780
2781 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2782 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2783 else
2784 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2785 if (set_statsn)
2786 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2787 else
2788 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2789
2790 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002791 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002792 hdr->datasn = cpu_to_be32(datain->data_sn);
2793 hdr->offset = cpu_to_be32(datain->offset);
2794
2795 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2796 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2797 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2798 ntohl(hdr->offset), datain->length, conn->cid);
2799}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302800EXPORT_SYMBOL(iscsit_build_datain_pdu);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002801
2802static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2803{
2804 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002805 struct iscsi_datain datain;
2806 struct iscsi_datain_req *dr;
Varun Prakash2854bb22016-04-20 00:00:08 +05302807 int eodr = 0, ret;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002808 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002809
2810 memset(&datain, 0, sizeof(struct iscsi_datain));
2811 dr = iscsit_get_datain_values(cmd, &datain);
2812 if (!dr) {
2813 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2814 cmd->init_task_tag);
2815 return -1;
2816 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002817 /*
2818 * Be paranoid and double check the logic for now.
2819 */
Andy Groverebf1d952012-04-03 15:51:24 -07002820 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002821 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2822 " datain.length: %u exceeds cmd->data_length: %u\n",
2823 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002824 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002825 return -1;
2826 }
2827
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002828 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002829 /*
2830 * Special case for successfully execution w/ both DATAIN
2831 * and Sense Data.
2832 */
2833 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2834 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2835 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2836 else {
2837 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2838 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2839 iscsit_increment_maxcmdsn(cmd, conn->sess);
2840 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002841 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002842 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002843 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2844 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002845 }
2846
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002847 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002848
Varun Prakash2854bb22016-04-20 00:00:08 +05302849 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2850 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07002851 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002852
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002853 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002854 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002855 2 : 1;
2856 iscsit_free_datain_req(cmd, dr);
2857 }
2858
Andy Grover6f3c0e62012-04-03 15:51:09 -07002859 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002860}
2861
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002862int
2863iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2864 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002865{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002866 struct iscsi_conn *logout_conn = NULL;
2867 struct iscsi_conn_recovery *cr = NULL;
2868 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002869 /*
2870 * The actual shutting down of Sessions and/or Connections
2871 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2872 * is done in scsi_logout_post_handler().
2873 */
2874 switch (cmd->logout_reason) {
2875 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2876 pr_debug("iSCSI session logout successful, setting"
2877 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2878 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2879 break;
2880 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2881 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2882 break;
2883 /*
2884 * For CLOSECONNECTION logout requests carrying
2885 * a matching logout CID -> local CID, the reference
2886 * for the local CID will have been incremented in
2887 * iscsi_logout_closeconnection().
2888 *
2889 * For CLOSECONNECTION logout requests carrying
2890 * a different CID than the connection it arrived
2891 * on, the connection responding to cmd->logout_cid
2892 * is stopped in iscsit_logout_post_handler_diffcid().
2893 */
2894
2895 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2896 " successful.\n", cmd->logout_cid, conn->cid);
2897 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2898 break;
2899 case ISCSI_LOGOUT_REASON_RECOVERY:
2900 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2901 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2902 break;
2903 /*
2904 * If the connection is still active from our point of view
2905 * force connection recovery to occur.
2906 */
2907 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2908 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002909 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002910 iscsit_connection_reinstatement_rcfr(logout_conn);
2911 iscsit_dec_conn_usage_count(logout_conn);
2912 }
2913
2914 cr = iscsit_get_inactive_connection_recovery_entry(
2915 conn->sess, cmd->logout_cid);
2916 if (!cr) {
2917 pr_err("Unable to locate CID: %hu for"
2918 " REMOVECONNFORRECOVERY Logout Request.\n",
2919 cmd->logout_cid);
2920 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2921 break;
2922 }
2923
2924 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2925
2926 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2927 " for recovery for CID: %hu on CID: %hu successful.\n",
2928 cmd->logout_cid, conn->cid);
2929 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2930 break;
2931 default:
2932 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2933 cmd->logout_reason);
2934 return -1;
2935 }
2936
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002937 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2938 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2939 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002940 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002941 cmd->stat_sn = conn->stat_sn++;
2942 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2943
2944 iscsit_increment_maxcmdsn(cmd, conn->sess);
2945 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002946 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002947
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002948 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2949 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2950 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2951 cmd->logout_cid, conn->cid);
2952
2953 return 0;
2954}
2955EXPORT_SYMBOL(iscsit_build_logout_rsp);
2956
2957static int
2958iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2959{
Varun Prakash2854bb22016-04-20 00:00:08 +05302960 int rc;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002961
2962 rc = iscsit_build_logout_rsp(cmd, conn,
2963 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2964 if (rc < 0)
2965 return rc;
2966
Varun Prakash2854bb22016-04-20 00:00:08 +05302967 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002968}
2969
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002970void
2971iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2972 struct iscsi_nopin *hdr, bool nopout_response)
2973{
2974 hdr->opcode = ISCSI_OP_NOOP_IN;
2975 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2976 hton24(hdr->dlength, cmd->buf_ptr_size);
2977 if (nopout_response)
2978 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2979 hdr->itt = cmd->init_task_tag;
2980 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2981 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2982 conn->stat_sn;
2983 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2984
2985 if (nopout_response)
2986 iscsit_increment_maxcmdsn(cmd, conn->sess);
2987
2988 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002989 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002990
2991 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2992 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
Colin Ian King3fc6a642016-09-02 15:30:34 +01002993 "Solicited" : "Unsolicited", cmd->init_task_tag,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002994 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2995}
2996EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2997
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002998/*
2999 * Unsolicited NOPIN, either requesting a response or not.
3000 */
3001static int iscsit_send_unsolicited_nopin(
3002 struct iscsi_cmd *cmd,
3003 struct iscsi_conn *conn,
3004 int want_response)
3005{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003006 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Varun Prakash2854bb22016-04-20 00:00:08 +05303007 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003008
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003009 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003010
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003011 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3012 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
3013
Varun Prakash2854bb22016-04-20 00:00:08 +05303014 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3015 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003016 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003017
3018 spin_lock_bh(&cmd->istate_lock);
3019 cmd->i_state = want_response ?
3020 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3021 spin_unlock_bh(&cmd->istate_lock);
3022
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003023 return 0;
3024}
3025
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003026static int
3027iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003029 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003030
3031 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003032
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003033 /*
3034 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3035 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3036 */
Varun Prakash2854bb22016-04-20 00:00:08 +05303037 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003038
Varun Prakash2854bb22016-04-20 00:00:08 +05303039 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3040 cmd->buf_ptr,
3041 cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003042}
3043
Andy Grover6f3c0e62012-04-03 15:51:09 -07003044static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003045 struct iscsi_cmd *cmd,
3046 struct iscsi_conn *conn)
3047{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003048 struct iscsi_r2t *r2t;
3049 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003050 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003051
3052 r2t = iscsit_get_r2t_from_list(cmd);
3053 if (!r2t)
3054 return -1;
3055
3056 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3057 memset(hdr, 0, ISCSI_HDR_LEN);
3058 hdr->opcode = ISCSI_OP_R2T;
3059 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3060 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3061 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003062 hdr->itt = cmd->init_task_tag;
Varun Prakash85672702016-04-20 00:00:13 +05303063 if (conn->conn_transport->iscsit_get_r2t_ttt)
3064 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3065 else
3066 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003067 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3068 hdr->statsn = cpu_to_be32(conn->stat_sn);
3069 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003070 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003071 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3072 hdr->data_offset = cpu_to_be32(r2t->offset);
3073 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3074
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003075 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3076 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3077 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3078 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3079 r2t->offset, r2t->xfer_len, conn->cid);
3080
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003081 spin_lock_bh(&cmd->r2t_lock);
3082 r2t->sent_r2t = 1;
3083 spin_unlock_bh(&cmd->r2t_lock);
3084
Varun Prakash2854bb22016-04-20 00:00:08 +05303085 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003086 if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003087 return ret;
3088 }
3089
3090 spin_lock_bh(&cmd->dataout_timeout_lock);
3091 iscsit_start_dataout_timer(cmd, conn);
3092 spin_unlock_bh(&cmd->dataout_timeout_lock);
3093
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003094 return 0;
3095}
3096
3097/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003098 * @recovery: If called from iscsi_task_reassign_complete_write() for
3099 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003100 */
3101int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003102 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003103 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003104 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003105{
3106 int first_r2t = 1;
3107 u32 offset = 0, xfer_len = 0;
3108
3109 spin_lock_bh(&cmd->r2t_lock);
3110 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3111 spin_unlock_bh(&cmd->r2t_lock);
3112 return 0;
3113 }
3114
Andy Grover8b1e1242012-04-03 15:51:12 -07003115 if (conn->sess->sess_ops->DataSequenceInOrder &&
3116 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003117 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003118
3119 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3120 if (conn->sess->sess_ops->DataSequenceInOrder) {
3121 offset = cmd->r2t_offset;
3122
Andy Grover8b1e1242012-04-03 15:51:12 -07003123 if (first_r2t && recovery) {
3124 int new_data_end = offset +
3125 conn->sess->sess_ops->MaxBurstLength -
3126 cmd->next_burst_len;
3127
Andy Groverebf1d952012-04-03 15:51:24 -07003128 if (new_data_end > cmd->se_cmd.data_length)
3129 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003130 else
3131 xfer_len =
3132 conn->sess->sess_ops->MaxBurstLength -
3133 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003134 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003135 int new_data_end = offset +
3136 conn->sess->sess_ops->MaxBurstLength;
3137
Andy Groverebf1d952012-04-03 15:51:24 -07003138 if (new_data_end > cmd->se_cmd.data_length)
3139 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003140 else
3141 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003142 }
3143 cmd->r2t_offset += xfer_len;
3144
Andy Groverebf1d952012-04-03 15:51:24 -07003145 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003146 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3147 } else {
3148 struct iscsi_seq *seq;
3149
3150 seq = iscsit_get_seq_holder_for_r2t(cmd);
3151 if (!seq) {
3152 spin_unlock_bh(&cmd->r2t_lock);
3153 return -1;
3154 }
3155
3156 offset = seq->offset;
3157 xfer_len = seq->xfer_len;
3158
3159 if (cmd->seq_send_order == cmd->seq_count)
3160 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3161 }
3162 cmd->outstanding_r2ts++;
3163 first_r2t = 0;
3164
3165 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3166 spin_unlock_bh(&cmd->r2t_lock);
3167 return -1;
3168 }
3169
3170 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3171 break;
3172 }
3173 spin_unlock_bh(&cmd->r2t_lock);
3174
3175 return 0;
3176}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303177EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003178
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003179void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3180 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003181{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003182 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003183 cmd->stat_sn = conn->stat_sn++;
3184
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003185 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003186
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003187 memset(hdr, 0, ISCSI_HDR_LEN);
3188 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3189 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3190 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3191 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
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 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3194 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003195 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003196 }
3197 hdr->response = cmd->iscsi_response;
3198 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003199 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003200 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3201
3202 iscsit_increment_maxcmdsn(cmd, conn->sess);
3203 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003204 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003205
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003206 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3207 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3208 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3209 cmd->se_cmd.scsi_status, conn->cid);
3210}
3211EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3212
3213static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3214{
3215 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003216 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
Varun Prakash2854bb22016-04-20 00:00:08 +05303217 void *data_buf = NULL;
3218 u32 padding = 0, data_buf_len = 0;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003219
3220 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3221
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003222 /*
3223 * Attach SENSE DATA payload to iSCSI Response PDU
3224 */
3225 if (cmd->se_cmd.sense_buffer &&
3226 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3227 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003228 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3229 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3230
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003231 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003232 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Varun Prakash2854bb22016-04-20 00:00:08 +05303233 data_buf = cmd->sense_buffer;
3234 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003235
3236 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003237 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003238 cmd->se_cmd.scsi_sense_length, 0, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003239 pr_debug("Adding %u bytes of padding to"
3240 " SENSE.\n", padding);
3241 }
3242
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003243 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3244 " Response PDU\n",
3245 cmd->se_cmd.scsi_sense_length);
3246 }
3247
Varun Prakash2854bb22016-04-20 00:00:08 +05303248 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3249 data_buf_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003250}
3251
3252static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3253{
3254 switch (se_tmr->response) {
3255 case TMR_FUNCTION_COMPLETE:
3256 return ISCSI_TMF_RSP_COMPLETE;
3257 case TMR_TASK_DOES_NOT_EXIST:
3258 return ISCSI_TMF_RSP_NO_TASK;
3259 case TMR_LUN_DOES_NOT_EXIST:
3260 return ISCSI_TMF_RSP_NO_LUN;
3261 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3262 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003263 case TMR_FUNCTION_REJECTED:
3264 default:
3265 return ISCSI_TMF_RSP_REJECTED;
3266 }
3267}
3268
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003269void
3270iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3271 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003272{
3273 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003274
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003275 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003276 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003277 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003278 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003279 cmd->stat_sn = conn->stat_sn++;
3280 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3281
3282 iscsit_increment_maxcmdsn(cmd, conn->sess);
3283 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003284 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003285
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003286 pr_debug("Built Task Management Response ITT: 0x%08x,"
3287 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3288 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3289}
3290EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3291
3292static int
3293iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3294{
3295 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003296
3297 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3298
Varun Prakash2854bb22016-04-20 00:00:08 +05303299 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003300}
3301
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003302static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3303{
3304 bool ret = false;
3305
3306 if (np->np_sockaddr.ss_family == AF_INET6) {
3307 const struct sockaddr_in6 sin6 = {
3308 .sin6_addr = IN6ADDR_ANY_INIT };
3309 struct sockaddr_in6 *sock_in6 =
3310 (struct sockaddr_in6 *)&np->np_sockaddr;
3311
3312 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3313 sin6.sin6_addr.s6_addr, 16))
3314 ret = true;
3315 } else {
3316 struct sockaddr_in * sock_in =
3317 (struct sockaddr_in *)&np->np_sockaddr;
3318
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003319 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003320 ret = true;
3321 }
3322
3323 return ret;
3324}
3325
Andy Grover8b1e1242012-04-03 15:51:12 -07003326#define SENDTARGETS_BUF_LIMIT 32768U
3327
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003328static int
3329iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003330 enum iscsit_transport_type network_transport,
3331 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003332{
3333 char *payload = NULL;
3334 struct iscsi_conn *conn = cmd->conn;
3335 struct iscsi_portal_group *tpg;
3336 struct iscsi_tiqn *tiqn;
3337 struct iscsi_tpg_np *tpg_np;
3338 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003339 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003340 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003341 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
David Disseldorpa6415cd2015-08-01 00:10:12 -07003342 bool active;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003343
Sagi Grimbergbe7dcfb2015-01-26 12:49:06 +02003344 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003345 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003346
3347 payload = kzalloc(buffer_len, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02003348 if (!payload)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003349 return -ENOMEM;
Markus Elfringc46e22f2017-04-09 15:34:50 +02003350
Nicholas Bellinger66658892013-06-19 22:45:42 -07003351 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003352 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003353 * explicit case..
3354 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003355 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003356 text_ptr = strchr(text_in, '=');
3357 if (!text_ptr) {
3358 pr_err("Unable to locate '=' string in text_in:"
3359 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003360 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003361 return -EINVAL;
3362 }
3363 /*
3364 * Skip over '=' character..
3365 */
3366 text_ptr += 1;
3367 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003368
3369 spin_lock(&tiqn_lock);
3370 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003371 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003372 strcmp(tiqn->tiqn, text_ptr)) {
3373 continue;
3374 }
3375
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003376 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003377
3378 spin_lock(&tiqn->tiqn_tpg_lock);
3379 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3380
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003381 /* If demo_mode_discovery=0 and generate_node_acls=0
3382 * (demo mode dislabed) do not return
3383 * TargetName+TargetAddress unless a NodeACL exists.
3384 */
3385
3386 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3387 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
Nicholas Bellinger21aaa232016-01-07 22:09:27 -08003388 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003389 cmd->conn->sess->sess_ops->InitiatorName))) {
3390 continue;
3391 }
3392
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003393 spin_lock(&tpg->tpg_state_lock);
David Disseldorpa6415cd2015-08-01 00:10:12 -07003394 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003395 spin_unlock(&tpg->tpg_state_lock);
3396
David Disseldorpa6415cd2015-08-01 00:10:12 -07003397 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3398 continue;
3399
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003400 spin_lock(&tpg->tpg_np_lock);
3401 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3402 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003403 struct iscsi_np *np = tpg_np->tpg_np;
3404 bool inaddr_any = iscsit_check_inaddr_any(np);
Andy Grover13a3cf02015-08-24 10:26:06 -07003405 struct sockaddr_storage *sockaddr;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003406
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003407 if (np->np_network_transport != network_transport)
3408 continue;
3409
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003410 if (!target_name_printed) {
3411 len = sprintf(buf, "TargetName=%s",
3412 tiqn->tiqn);
3413 len += 1;
3414
3415 if ((len + payload_len) > buffer_len) {
3416 spin_unlock(&tpg->tpg_np_lock);
3417 spin_unlock(&tiqn->tiqn_tpg_lock);
3418 end_of_buf = 1;
3419 goto eob;
3420 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003421
3422 if (skip_bytes && len <= skip_bytes) {
3423 skip_bytes -= len;
3424 } else {
3425 memcpy(payload + payload_len, buf, len);
3426 payload_len += len;
3427 target_name_printed = 1;
3428 if (len > skip_bytes)
3429 skip_bytes = 0;
3430 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003431 }
3432
Andy Grover69d75572015-08-24 10:26:04 -07003433 if (inaddr_any)
3434 sockaddr = &conn->local_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003435 else
Andy Grover69d75572015-08-24 10:26:04 -07003436 sockaddr = &np->np_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003437
Andy Grover69d75572015-08-24 10:26:04 -07003438 len = sprintf(buf, "TargetAddress="
3439 "%pISpc,%hu",
3440 sockaddr,
3441 tpg->tpgt);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003442 len += 1;
3443
3444 if ((len + payload_len) > buffer_len) {
3445 spin_unlock(&tpg->tpg_np_lock);
3446 spin_unlock(&tiqn->tiqn_tpg_lock);
3447 end_of_buf = 1;
3448 goto eob;
3449 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003450
3451 if (skip_bytes && len <= skip_bytes) {
3452 skip_bytes -= len;
3453 } else {
3454 memcpy(payload + payload_len, buf, len);
3455 payload_len += len;
3456 if (len > skip_bytes)
3457 skip_bytes = 0;
3458 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003459 }
3460 spin_unlock(&tpg->tpg_np_lock);
3461 }
3462 spin_unlock(&tiqn->tiqn_tpg_lock);
3463eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003464 if (end_of_buf) {
3465 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003466 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003467 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003468
Andy Grover8060b8d2015-01-09 15:13:08 -08003469 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003470 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003471 }
3472 spin_unlock(&tiqn_lock);
3473
3474 cmd->buf_ptr = payload;
3475
3476 return payload_len;
3477}
3478
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003479int
3480iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003481 struct iscsi_text_rsp *hdr,
3482 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003483{
3484 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003485 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003486
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003487 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3488 cmd->read_data_done,
3489 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003490 if (text_length < 0)
3491 return text_length;
3492
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003493 if (completed) {
3494 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3495 } else {
3496 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3497 cmd->read_data_done += text_length;
3498 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3499 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3500 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003501 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003502 padding = ((-text_length) & 3);
3503 hton24(hdr->dlength, text_length);
3504 hdr->itt = cmd->init_task_tag;
3505 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3506 cmd->stat_sn = conn->stat_sn++;
3507 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3508
3509 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003510 /*
3511 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3512 * correctly increment MaxCmdSN for each response answering a
3513 * non immediate text request with a valid CmdSN.
3514 */
3515 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003516 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003517 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003518
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003519 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3520 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3521 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3522 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3523 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003524
3525 return text_length + padding;
3526}
3527EXPORT_SYMBOL(iscsit_build_text_rsp);
3528
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003529static int iscsit_send_text_rsp(
3530 struct iscsi_cmd *cmd,
3531 struct iscsi_conn *conn)
3532{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003533 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Varun Prakash2854bb22016-04-20 00:00:08 +05303534 int text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003535
Varun Prakash864e5042016-04-20 00:00:15 +05303536 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3537 conn->conn_transport->transport_type);
Varun Prakash2854bb22016-04-20 00:00:08 +05303538 if (text_length < 0)
3539 return text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003540
Varun Prakash2854bb22016-04-20 00:00:08 +05303541 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3542 cmd->buf_ptr,
3543 text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003544}
3545
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003546void
3547iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3548 struct iscsi_reject *hdr)
3549{
3550 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003551 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003552 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3553 hton24(hdr->dlength, ISCSI_HDR_LEN);
3554 hdr->ffffffff = cpu_to_be32(0xffffffff);
3555 cmd->stat_sn = conn->stat_sn++;
3556 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3557 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003558 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003559
3560}
3561EXPORT_SYMBOL(iscsit_build_reject);
3562
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003563static int iscsit_send_reject(
3564 struct iscsi_cmd *cmd,
3565 struct iscsi_conn *conn)
3566{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003567 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003568
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003569 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003570
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003571 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3572 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3573
Varun Prakash2854bb22016-04-20 00:00:08 +05303574 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3575 cmd->buf_ptr,
3576 ISCSI_HDR_LEN);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003577}
3578
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003579void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3580{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003581 int ord, cpu;
3582 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003583 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3584 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003585 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003586 * Here we use bitmap_id to determine which CPU that this
3587 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003588 * execute upon.
3589 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003590 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003591 for_each_online_cpu(cpu) {
3592 if (ord-- == 0) {
3593 cpumask_set_cpu(cpu, conn->conn_cpumask);
3594 return;
3595 }
3596 }
3597 /*
3598 * This should never be reached..
3599 */
3600 dump_stack();
3601 cpumask_setall(conn->conn_cpumask);
3602}
3603
Varun Prakashd2faaef2016-04-20 00:00:19 +05303604int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003605iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003606{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003607 int ret;
3608
3609 switch (state) {
3610 case ISTATE_SEND_R2T:
3611 ret = iscsit_send_r2t(cmd, conn);
3612 if (ret < 0)
3613 goto err;
3614 break;
3615 case ISTATE_REMOVE:
3616 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003617 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003618 spin_unlock_bh(&conn->cmd_lock);
3619
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003620 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003621 break;
3622 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3623 iscsit_mod_nopin_response_timer(conn);
3624 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3625 if (ret < 0)
3626 goto err;
3627 break;
3628 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3629 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3630 if (ret < 0)
3631 goto err;
3632 break;
3633 default:
3634 pr_err("Unknown Opcode: 0x%02x ITT:"
3635 " 0x%08x, i_state: %d on CID: %hu\n",
3636 cmd->iscsi_opcode, cmd->init_task_tag, state,
3637 conn->cid);
3638 goto err;
3639 }
3640
3641 return 0;
3642
3643err:
3644 return -1;
3645}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303646EXPORT_SYMBOL(iscsit_immediate_queue);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003647
3648static int
3649iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3650{
3651 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003652 struct iscsi_queue_req *qr;
3653 struct iscsi_cmd *cmd;
3654 u8 state;
3655 int ret;
3656
3657 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3658 atomic_set(&conn->check_immediate_queue, 0);
3659 cmd = qr->cmd;
3660 state = qr->state;
3661 kmem_cache_free(lio_qr_cache, qr);
3662
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003663 ret = t->iscsit_immediate_queue(conn, cmd, state);
3664 if (ret < 0)
3665 return ret;
3666 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003667
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003668 return 0;
3669}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003670
Varun Prakashd2faaef2016-04-20 00:00:19 +05303671int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003672iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3673{
3674 int ret;
3675
3676check_rsp_state:
3677 switch (state) {
3678 case ISTATE_SEND_DATAIN:
3679 ret = iscsit_send_datain(cmd, conn);
3680 if (ret < 0)
3681 goto err;
3682 else if (!ret)
3683 /* more drs */
3684 goto check_rsp_state;
3685 else if (ret == 1) {
3686 /* all done */
3687 spin_lock_bh(&cmd->istate_lock);
3688 cmd->i_state = ISTATE_SENT_STATUS;
3689 spin_unlock_bh(&cmd->istate_lock);
3690
3691 if (atomic_read(&conn->check_immediate_queue))
3692 return 1;
3693
3694 return 0;
3695 } else if (ret == 2) {
3696 /* Still must send status,
3697 SCF_TRANSPORT_TASK_SENSE was set */
3698 spin_lock_bh(&cmd->istate_lock);
3699 cmd->i_state = ISTATE_SEND_STATUS;
3700 spin_unlock_bh(&cmd->istate_lock);
3701 state = ISTATE_SEND_STATUS;
3702 goto check_rsp_state;
3703 }
3704
3705 break;
3706 case ISTATE_SEND_STATUS:
3707 case ISTATE_SEND_STATUS_RECOVERY:
3708 ret = iscsit_send_response(cmd, conn);
3709 break;
3710 case ISTATE_SEND_LOGOUTRSP:
3711 ret = iscsit_send_logout(cmd, conn);
3712 break;
3713 case ISTATE_SEND_ASYNCMSG:
3714 ret = iscsit_send_conn_drop_async_message(
3715 cmd, conn);
3716 break;
3717 case ISTATE_SEND_NOPIN:
3718 ret = iscsit_send_nopin(cmd, conn);
3719 break;
3720 case ISTATE_SEND_REJECT:
3721 ret = iscsit_send_reject(cmd, conn);
3722 break;
3723 case ISTATE_SEND_TASKMGTRSP:
3724 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3725 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003726 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003727 ret = iscsit_tmr_post_handler(cmd, conn);
3728 if (ret != 0)
3729 iscsit_fall_back_to_erl0(conn->sess);
3730 break;
3731 case ISTATE_SEND_TEXTRSP:
3732 ret = iscsit_send_text_rsp(cmd, conn);
3733 break;
3734 default:
3735 pr_err("Unknown Opcode: 0x%02x ITT:"
3736 " 0x%08x, i_state: %d on CID: %hu\n",
3737 cmd->iscsi_opcode, cmd->init_task_tag,
3738 state, conn->cid);
3739 goto err;
3740 }
3741 if (ret < 0)
3742 goto err;
3743
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003744 switch (state) {
3745 case ISTATE_SEND_LOGOUTRSP:
3746 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003747 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003748 /* fall through */
3749 case ISTATE_SEND_STATUS:
3750 case ISTATE_SEND_ASYNCMSG:
3751 case ISTATE_SEND_NOPIN:
3752 case ISTATE_SEND_STATUS_RECOVERY:
3753 case ISTATE_SEND_TEXTRSP:
3754 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003755 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003756 spin_lock_bh(&cmd->istate_lock);
3757 cmd->i_state = ISTATE_SENT_STATUS;
3758 spin_unlock_bh(&cmd->istate_lock);
3759 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003760 default:
3761 pr_err("Unknown Opcode: 0x%02x ITT:"
3762 " 0x%08x, i_state: %d on CID: %hu\n",
3763 cmd->iscsi_opcode, cmd->init_task_tag,
3764 cmd->i_state, conn->cid);
3765 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003766 }
3767
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003768 if (atomic_read(&conn->check_immediate_queue))
3769 return 1;
3770
Andy Grover6f3c0e62012-04-03 15:51:09 -07003771 return 0;
3772
3773err:
3774 return -1;
3775}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303776EXPORT_SYMBOL(iscsit_response_queue);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003777
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003778static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003779{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003780 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003781 struct iscsi_queue_req *qr;
3782 struct iscsi_cmd *cmd;
3783 u8 state;
3784 int ret;
3785
3786 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3787 cmd = qr->cmd;
3788 state = qr->state;
3789 kmem_cache_free(lio_qr_cache, qr);
3790
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003791 ret = t->iscsit_response_queue(conn, cmd, state);
3792 if (ret == 1 || ret < 0)
3793 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003794 }
3795
3796 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003797}
3798
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003799int iscsi_target_tx_thread(void *arg)
3800{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003801 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003802 struct iscsi_conn *conn = arg;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003803 bool conn_freed = false;
3804
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003805 /*
3806 * Allow ourselves to be interrupted by SIGINT so that a
3807 * connection recovery / failure event can be triggered externally.
3808 */
3809 allow_signal(SIGINT);
3810
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003811 while (!kthread_should_stop()) {
3812 /*
3813 * Ensure that both TX and RX per connection kthreads
3814 * are scheduled to run on the same CPU.
3815 */
3816 iscsit_thread_check_cpumask(conn, current, 1);
3817
Roland Dreierd5627ac2012-10-31 09:16:46 -07003818 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003819 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003820
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003821 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003822 goto transport_err;
3823
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003824get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003825 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003826 if (ret < 0)
3827 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003828
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003829 ret = iscsit_handle_response_queue(conn);
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003830 if (ret == 1) {
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003831 goto get_immediate;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003832 } else if (ret == -ECONNRESET) {
3833 conn_freed = true;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003834 goto out;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003835 } else if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003836 goto transport_err;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003837 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003838 }
3839
3840transport_err:
Nicholas Bellingere5419862015-07-22 23:14:19 -07003841 /*
3842 * Avoid the normal connection failure code-path if this connection
3843 * is still within LOGIN mode, and iscsi_np process context is
3844 * responsible for cleaning up the early connection failure.
3845 */
3846 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003847 iscsit_take_action_for_connection_exit(conn, &conn_freed);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003848out:
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003849 if (!conn_freed) {
3850 while (!kthread_should_stop()) {
3851 msleep(100);
3852 }
3853 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003854 return 0;
3855}
3856
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003857static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3858{
3859 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3860 struct iscsi_cmd *cmd;
3861 int ret = 0;
3862
3863 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3864 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003865 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003866 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003867 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003868
3869 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3870 break;
3871 case ISCSI_OP_SCSI_DATA_OUT:
3872 ret = iscsit_handle_data_out(conn, buf);
3873 break;
3874 case ISCSI_OP_NOOP_OUT:
3875 cmd = NULL;
3876 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003877 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003878 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003879 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003880 }
3881 ret = iscsit_handle_nop_out(conn, cmd, buf);
3882 break;
3883 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003884 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003885 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003886 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003887
3888 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3889 break;
3890 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003891 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3892 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3893 if (!cmd)
3894 goto reject;
3895 } else {
3896 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3897 if (!cmd)
3898 goto reject;
3899 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003900
3901 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003902 break;
3903 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003904 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003905 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003906 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003907
3908 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3909 if (ret > 0)
3910 wait_for_completion_timeout(&conn->conn_logout_comp,
3911 SECONDS_FOR_LOGOUT_COMP * HZ);
3912 break;
3913 case ISCSI_OP_SNACK:
3914 ret = iscsit_handle_snack(conn, buf);
3915 break;
3916 default:
3917 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3918 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3919 pr_err("Cannot recover from unknown"
3920 " opcode while ERL=0, closing iSCSI connection.\n");
3921 return -1;
3922 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02003923 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3924 " closing iSCSI connection.\n");
3925 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003926 break;
3927 }
3928
3929 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003930reject:
3931 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003932}
3933
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08003934static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3935{
3936 bool ret;
3937
3938 spin_lock_bh(&conn->state_lock);
3939 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3940 spin_unlock_bh(&conn->state_lock);
3941
3942 return ret;
3943}
3944
Varun Prakashe8205cc2016-04-20 00:00:11 +05303945static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003946{
Varun Prakashe8205cc2016-04-20 00:00:11 +05303947 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003948 u8 buffer[ISCSI_HDR_LEN], opcode;
3949 u32 checksum = 0, digest = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003950 struct kvec iov;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003951
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003952 while (!kthread_should_stop()) {
3953 /*
3954 * Ensure that both TX and RX per connection kthreads
3955 * are scheduled to run on the same CPU.
3956 */
3957 iscsit_thread_check_cpumask(conn, current, 0);
3958
3959 memset(buffer, 0, ISCSI_HDR_LEN);
3960 memset(&iov, 0, sizeof(struct kvec));
3961
3962 iov.iov_base = buffer;
3963 iov.iov_len = ISCSI_HDR_LEN;
3964
3965 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3966 if (ret != ISCSI_HDR_LEN) {
3967 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303968 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003969 }
3970
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003971 if (conn->conn_ops->HeaderDigest) {
3972 iov.iov_base = &digest;
3973 iov.iov_len = ISCSI_CRC_LEN;
3974
3975 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3976 if (ret != ISCSI_CRC_LEN) {
3977 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303978 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003979 }
3980
Herbert Xu69110e32016-01-24 21:19:52 +08003981 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003982 buffer, ISCSI_HDR_LEN,
3983 0, NULL, (u8 *)&checksum);
3984
3985 if (digest != checksum) {
3986 pr_err("HeaderDigest CRC32C failed,"
3987 " received 0x%08x, computed 0x%08x\n",
3988 digest, checksum);
3989 /*
3990 * Set the PDU to 0xff so it will intentionally
3991 * hit default in the switch below.
3992 */
3993 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003994 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003995 } else {
3996 pr_debug("Got HeaderDigest CRC32C"
3997 " 0x%08x\n", checksum);
3998 }
3999 }
4000
4001 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
Varun Prakashe8205cc2016-04-20 00:00:11 +05304002 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004003
4004 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4005
4006 if (conn->sess->sess_ops->SessionType &&
4007 ((!(opcode & ISCSI_OP_TEXT)) ||
4008 (!(opcode & ISCSI_OP_LOGOUT)))) {
4009 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4010 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004011 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4012 buffer);
Varun Prakashe8205cc2016-04-20 00:00:11 +05304013 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004014 }
4015
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004016 ret = iscsi_target_rx_opcode(conn, buffer);
4017 if (ret < 0)
Varun Prakashe8205cc2016-04-20 00:00:11 +05304018 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004019 }
Varun Prakashe8205cc2016-04-20 00:00:11 +05304020}
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004021
Varun Prakashe8205cc2016-04-20 00:00:11 +05304022int iscsi_target_rx_thread(void *arg)
4023{
4024 int rc;
4025 struct iscsi_conn *conn = arg;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004026 bool conn_freed = false;
Varun Prakashe8205cc2016-04-20 00:00:11 +05304027
4028 /*
4029 * Allow ourselves to be interrupted by SIGINT so that a
4030 * connection recovery / failure event can be triggered externally.
4031 */
4032 allow_signal(SIGINT);
4033 /*
4034 * Wait for iscsi_post_login_handler() to complete before allowing
4035 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4036 */
4037 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4038 if (rc < 0 || iscsi_target_check_conn_state(conn))
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004039 goto out;
Varun Prakashe8205cc2016-04-20 00:00:11 +05304040
4041 if (!conn->conn_transport->iscsit_get_rx_pdu)
4042 return 0;
4043
4044 conn->conn_transport->iscsit_get_rx_pdu(conn);
4045
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004046 if (!signal_pending(current))
4047 atomic_set(&conn->transport_failed, 1);
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004048 iscsit_take_action_for_connection_exit(conn, &conn_freed);
4049
4050out:
4051 if (!conn_freed) {
4052 while (!kthread_should_stop()) {
4053 msleep(100);
4054 }
4055 }
4056
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004057 return 0;
4058}
4059
4060static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4061{
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004062 LIST_HEAD(tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004063 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4064 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004065 /*
4066 * We expect this function to only ever be called from either RX or TX
4067 * thread context via iscsit_close_connection() once the other context
4068 * has been reset -> returned sleeping pre-handler state.
4069 */
4070 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004071 list_splice_init(&conn->conn_cmd_list, &tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004072
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004073 list_for_each_entry(cmd, &tmp_list, i_conn_node) {
4074 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004075
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004076 if (se_cmd->se_tfo != NULL) {
4077 spin_lock(&se_cmd->t_state_lock);
4078 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4079 spin_unlock(&se_cmd->t_state_lock);
4080 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004081 }
4082 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004083
4084 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4085 list_del_init(&cmd->i_conn_node);
4086
4087 iscsit_increment_maxcmdsn(cmd, sess);
4088 iscsit_free_cmd(cmd, true);
4089
4090 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004091}
4092
4093static void iscsit_stop_timers_for_cmds(
4094 struct iscsi_conn *conn)
4095{
4096 struct iscsi_cmd *cmd;
4097
4098 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004099 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004100 if (cmd->data_direction == DMA_TO_DEVICE)
4101 iscsit_stop_dataout_timer(cmd);
4102 }
4103 spin_unlock_bh(&conn->cmd_lock);
4104}
4105
4106int iscsit_close_connection(
4107 struct iscsi_conn *conn)
4108{
4109 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4110 struct iscsi_session *sess = conn->sess;
4111
4112 pr_debug("Closing iSCSI connection CID %hu on SID:"
4113 " %u\n", conn->cid, sess->sid);
4114 /*
Varun Prakashb4869ee2016-04-20 00:00:18 +05304115 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4116 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4117 * sleeping and the logout response never got sent because the
4118 * connection failed.
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004119 *
4120 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4121 * to signal logout response TX interrupt completion. Go ahead and skip
4122 * this for iser since isert_rx_opcode() does not wait on logout failure,
4123 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004124 */
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07004125 if (!conn->conn_transport->rdma_shutdown)
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004126 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004127
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004128 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4129 if (conn->tx_thread &&
4130 cmpxchg(&conn->tx_thread_active, true, false)) {
4131 send_sig(SIGINT, conn->tx_thread, 1);
4132 kthread_stop(conn->tx_thread);
4133 }
4134 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4135 if (conn->rx_thread &&
4136 cmpxchg(&conn->rx_thread_active, true, false)) {
4137 send_sig(SIGINT, conn->rx_thread, 1);
4138 kthread_stop(conn->rx_thread);
4139 }
4140 }
4141
4142 spin_lock(&iscsit_global->ts_bitmap_lock);
4143 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4144 get_order(1));
4145 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004146
4147 iscsit_stop_timers_for_cmds(conn);
4148 iscsit_stop_nopin_response_timer(conn);
4149 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004150
4151 if (conn->conn_transport->iscsit_wait_conn)
4152 conn->conn_transport->iscsit_wait_conn(conn);
4153
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004154 /*
4155 * During Connection recovery drop unacknowledged out of order
4156 * commands for this connection, and prepare the other commands
Bart Van Assche53c561d2016-12-23 14:40:24 +01004157 * for reallegiance.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004158 *
4159 * During normal operation clear the out of order commands (but
4160 * do not free the struct iscsi_ooo_cmdsn's) and release all
4161 * struct iscsi_cmds.
4162 */
4163 if (atomic_read(&conn->connection_recovery)) {
4164 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
Bart Van Assche53c561d2016-12-23 14:40:24 +01004165 iscsit_prepare_cmds_for_reallegiance(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004166 } else {
4167 iscsit_clear_ooo_cmdsns_for_conn(conn);
4168 iscsit_release_commands_from_conn(conn);
4169 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004170 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004171
4172 /*
4173 * Handle decrementing session or connection usage count if
4174 * a logout response was not able to be sent because the
4175 * connection failed. Fall back to Session Recovery here.
4176 */
4177 if (atomic_read(&conn->conn_logout_remove)) {
4178 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4179 iscsit_dec_conn_usage_count(conn);
4180 iscsit_dec_session_usage_count(sess);
4181 }
4182 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4183 iscsit_dec_conn_usage_count(conn);
4184
4185 atomic_set(&conn->conn_logout_remove, 0);
4186 atomic_set(&sess->session_reinstatement, 0);
4187 atomic_set(&sess->session_fall_back_to_erl0, 1);
4188 }
4189
4190 spin_lock_bh(&sess->conn_lock);
4191 list_del(&conn->conn_list);
4192
4193 /*
4194 * Attempt to let the Initiator know this connection failed by
4195 * sending an Connection Dropped Async Message on another
4196 * active connection.
4197 */
4198 if (atomic_read(&conn->connection_recovery))
4199 iscsit_build_conn_drop_async_message(conn);
4200
4201 spin_unlock_bh(&sess->conn_lock);
4202
4203 /*
4204 * If connection reinstatement is being performed on this connection,
4205 * up the connection reinstatement semaphore that is being blocked on
4206 * in iscsit_cause_connection_reinstatement().
4207 */
4208 spin_lock_bh(&conn->state_lock);
4209 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4210 spin_unlock_bh(&conn->state_lock);
4211 complete(&conn->conn_wait_comp);
4212 wait_for_completion(&conn->conn_post_wait_comp);
4213 spin_lock_bh(&conn->state_lock);
4214 }
4215
4216 /*
4217 * If connection reinstatement is being performed on this connection
4218 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4219 * connection wait rcfr semaphore that is being blocked on
4220 * an iscsit_connection_reinstatement_rcfr().
4221 */
4222 if (atomic_read(&conn->connection_wait_rcfr)) {
4223 spin_unlock_bh(&conn->state_lock);
4224 complete(&conn->conn_wait_rcfr_comp);
4225 wait_for_completion(&conn->conn_post_wait_comp);
4226 spin_lock_bh(&conn->state_lock);
4227 }
4228 atomic_set(&conn->connection_reinstatement, 1);
4229 spin_unlock_bh(&conn->state_lock);
4230
4231 /*
4232 * If any other processes are accessing this connection pointer we
4233 * must wait until they have completed.
4234 */
4235 iscsit_check_conn_usage_count(conn);
4236
Herbert Xu69110e32016-01-24 21:19:52 +08004237 ahash_request_free(conn->conn_tx_hash);
4238 if (conn->conn_rx_hash) {
4239 struct crypto_ahash *tfm;
4240
4241 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4242 ahash_request_free(conn->conn_rx_hash);
4243 crypto_free_ahash(tfm);
4244 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004245
Joern Engelfbecb652014-09-02 17:49:47 -04004246 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004247
4248 kfree(conn->conn_ops);
4249 conn->conn_ops = NULL;
4250
Al Virobf6932f2012-07-21 08:55:18 +01004251 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004252 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004253
4254 if (conn->conn_transport->iscsit_free_conn)
4255 conn->conn_transport->iscsit_free_conn(conn);
4256
4257 iscsit_put_transport(conn->conn_transport);
4258
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004259 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4260 conn->conn_state = TARG_CONN_STATE_FREE;
4261 kfree(conn);
4262
4263 spin_lock_bh(&sess->conn_lock);
4264 atomic_dec(&sess->nconn);
4265 pr_debug("Decremented iSCSI connection count to %hu from node:"
4266 " %s\n", atomic_read(&sess->nconn),
4267 sess->sess_ops->InitiatorName);
4268 /*
4269 * Make sure that if one connection fails in an non ERL=2 iSCSI
4270 * Session that they all fail.
4271 */
4272 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4273 !atomic_read(&sess->session_logout))
4274 atomic_set(&sess->session_fall_back_to_erl0, 1);
4275
4276 /*
4277 * If this was not the last connection in the session, and we are
4278 * performing session reinstatement or falling back to ERL=0, call
4279 * iscsit_stop_session() without sleeping to shutdown the other
4280 * active connections.
4281 */
4282 if (atomic_read(&sess->nconn)) {
4283 if (!atomic_read(&sess->session_reinstatement) &&
4284 !atomic_read(&sess->session_fall_back_to_erl0)) {
4285 spin_unlock_bh(&sess->conn_lock);
4286 return 0;
4287 }
4288 if (!atomic_read(&sess->session_stop_active)) {
4289 atomic_set(&sess->session_stop_active, 1);
4290 spin_unlock_bh(&sess->conn_lock);
4291 iscsit_stop_session(sess, 0, 0);
4292 return 0;
4293 }
4294 spin_unlock_bh(&sess->conn_lock);
4295 return 0;
4296 }
4297
4298 /*
4299 * If this was the last connection in the session and one of the
4300 * following is occurring:
4301 *
4302 * Session Reinstatement is not being performed, and are falling back
4303 * to ERL=0 call iscsit_close_session().
4304 *
4305 * Session Logout was requested. iscsit_close_session() will be called
4306 * elsewhere.
4307 *
4308 * Session Continuation is not being performed, start the Time2Retain
4309 * handler and check if sleep_on_sess_wait_sem is active.
4310 */
4311 if (!atomic_read(&sess->session_reinstatement) &&
4312 atomic_read(&sess->session_fall_back_to_erl0)) {
4313 spin_unlock_bh(&sess->conn_lock);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004314 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004315
4316 return 0;
4317 } else if (atomic_read(&sess->session_logout)) {
4318 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4319 sess->session_state = TARG_SESS_STATE_FREE;
4320 spin_unlock_bh(&sess->conn_lock);
4321
4322 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4323 complete(&sess->session_wait_comp);
4324
4325 return 0;
4326 } else {
4327 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4328 sess->session_state = TARG_SESS_STATE_FAILED;
4329
4330 if (!atomic_read(&sess->session_continuation)) {
4331 spin_unlock_bh(&sess->conn_lock);
4332 iscsit_start_time2retain_handler(sess);
4333 } else
4334 spin_unlock_bh(&sess->conn_lock);
4335
4336 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4337 complete(&sess->session_wait_comp);
4338
4339 return 0;
4340 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004341}
4342
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004343/*
4344 * If the iSCSI Session for the iSCSI Initiator Node exists,
4345 * forcefully shutdown the iSCSI NEXUS.
4346 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004347int iscsit_close_session(struct iscsi_session *sess)
4348{
Andy Grover60bfcf82013-10-09 11:05:58 -07004349 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004350 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4351
4352 if (atomic_read(&sess->nconn)) {
4353 pr_err("%d connection(s) still exist for iSCSI session"
4354 " to %s\n", atomic_read(&sess->nconn),
4355 sess->sess_ops->InitiatorName);
4356 BUG();
4357 }
4358
4359 spin_lock_bh(&se_tpg->session_lock);
4360 atomic_set(&sess->session_logout, 1);
4361 atomic_set(&sess->session_reinstatement, 1);
4362 iscsit_stop_time2retain_timer(sess);
4363 spin_unlock_bh(&se_tpg->session_lock);
4364
4365 /*
4366 * transport_deregister_session_configfs() will clear the
4367 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4368 * can be setting it again with __transport_register_session() in
4369 * iscsi_post_login_handler() again after the iscsit_stop_session()
4370 * completes in iscsi_np context.
4371 */
4372 transport_deregister_session_configfs(sess->se_sess);
4373
4374 /*
4375 * If any other processes are accessing this session pointer we must
4376 * wait until they have completed. If we are in an interrupt (the
4377 * time2retain handler) and contain and active session usage count we
4378 * restart the timer and exit.
4379 */
4380 if (!in_interrupt()) {
4381 if (iscsit_check_session_usage_count(sess) == 1)
4382 iscsit_stop_session(sess, 1, 1);
4383 } else {
4384 if (iscsit_check_session_usage_count(sess) == 2) {
4385 atomic_set(&sess->session_logout, 0);
4386 iscsit_start_time2retain_handler(sess);
4387 return 0;
4388 }
4389 }
4390
4391 transport_deregister_session(sess->se_sess);
4392
4393 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4394 iscsit_free_connection_recovery_entires(sess);
4395
4396 iscsit_free_all_ooo_cmdsns(sess);
4397
4398 spin_lock_bh(&se_tpg->session_lock);
4399 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4400 sess->session_state = TARG_SESS_STATE_FREE;
4401 pr_debug("Released iSCSI session from node: %s\n",
4402 sess->sess_ops->InitiatorName);
4403 tpg->nsessions--;
4404 if (tpg->tpg_tiqn)
4405 tpg->tpg_tiqn->tiqn_nsessions--;
4406
4407 pr_debug("Decremented number of active iSCSI Sessions on"
4408 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4409
4410 spin_lock(&sess_idr_lock);
4411 idr_remove(&sess_idr, sess->session_index);
4412 spin_unlock(&sess_idr_lock);
4413
4414 kfree(sess->sess_ops);
4415 sess->sess_ops = NULL;
4416 spin_unlock_bh(&se_tpg->session_lock);
4417
4418 kfree(sess);
4419 return 0;
4420}
4421
4422static void iscsit_logout_post_handler_closesession(
4423 struct iscsi_conn *conn)
4424{
4425 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004426 int sleep = 1;
4427 /*
4428 * Traditional iscsi/tcp will invoke this logic from TX thread
4429 * context during session logout, so clear tx_thread_active and
4430 * sleep if iscsit_close_connection() has not already occured.
4431 *
4432 * Since iser-target invokes this logic from it's own workqueue,
4433 * always sleep waiting for RX/TX thread shutdown to complete
4434 * within iscsit_close_connection().
4435 */
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004436 if (!conn->conn_transport->rdma_shutdown) {
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004437 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004438 if (!sleep)
4439 return;
4440 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004441
4442 atomic_set(&conn->conn_logout_remove, 0);
4443 complete(&conn->conn_logout_comp);
4444
4445 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004446 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004447 iscsit_dec_session_usage_count(sess);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004448 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004449}
4450
4451static void iscsit_logout_post_handler_samecid(
4452 struct iscsi_conn *conn)
4453{
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004454 int sleep = 1;
4455
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004456 if (!conn->conn_transport->rdma_shutdown) {
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004457 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004458 if (!sleep)
4459 return;
4460 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004461
4462 atomic_set(&conn->conn_logout_remove, 0);
4463 complete(&conn->conn_logout_comp);
4464
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004465 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004466 iscsit_dec_conn_usage_count(conn);
4467}
4468
4469static void iscsit_logout_post_handler_diffcid(
4470 struct iscsi_conn *conn,
4471 u16 cid)
4472{
4473 struct iscsi_conn *l_conn;
4474 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004475 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004476
4477 if (!sess)
4478 return;
4479
4480 spin_lock_bh(&sess->conn_lock);
4481 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4482 if (l_conn->cid == cid) {
4483 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004484 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004485 break;
4486 }
4487 }
4488 spin_unlock_bh(&sess->conn_lock);
4489
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004490 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004491 return;
4492
4493 if (l_conn->sock)
4494 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4495
4496 spin_lock_bh(&l_conn->state_lock);
4497 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4498 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4499 spin_unlock_bh(&l_conn->state_lock);
4500
4501 iscsit_cause_connection_reinstatement(l_conn, 1);
4502 iscsit_dec_conn_usage_count(l_conn);
4503}
4504
4505/*
4506 * Return of 0 causes the TX thread to restart.
4507 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004508int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004509 struct iscsi_cmd *cmd,
4510 struct iscsi_conn *conn)
4511{
4512 int ret = 0;
4513
4514 switch (cmd->logout_reason) {
4515 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4516 switch (cmd->logout_response) {
4517 case ISCSI_LOGOUT_SUCCESS:
4518 case ISCSI_LOGOUT_CLEANUP_FAILED:
4519 default:
4520 iscsit_logout_post_handler_closesession(conn);
4521 break;
4522 }
4523 ret = 0;
4524 break;
4525 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4526 if (conn->cid == cmd->logout_cid) {
4527 switch (cmd->logout_response) {
4528 case ISCSI_LOGOUT_SUCCESS:
4529 case ISCSI_LOGOUT_CLEANUP_FAILED:
4530 default:
4531 iscsit_logout_post_handler_samecid(conn);
4532 break;
4533 }
4534 ret = 0;
4535 } else {
4536 switch (cmd->logout_response) {
4537 case ISCSI_LOGOUT_SUCCESS:
4538 iscsit_logout_post_handler_diffcid(conn,
4539 cmd->logout_cid);
4540 break;
4541 case ISCSI_LOGOUT_CID_NOT_FOUND:
4542 case ISCSI_LOGOUT_CLEANUP_FAILED:
4543 default:
4544 break;
4545 }
4546 ret = 1;
4547 }
4548 break;
4549 case ISCSI_LOGOUT_REASON_RECOVERY:
4550 switch (cmd->logout_response) {
4551 case ISCSI_LOGOUT_SUCCESS:
4552 case ISCSI_LOGOUT_CID_NOT_FOUND:
4553 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4554 case ISCSI_LOGOUT_CLEANUP_FAILED:
4555 default:
4556 break;
4557 }
4558 ret = 1;
4559 break;
4560 default:
4561 break;
4562
4563 }
4564 return ret;
4565}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004566EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004567
4568void iscsit_fail_session(struct iscsi_session *sess)
4569{
4570 struct iscsi_conn *conn;
4571
4572 spin_lock_bh(&sess->conn_lock);
4573 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4574 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4575 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4576 }
4577 spin_unlock_bh(&sess->conn_lock);
4578
4579 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4580 sess->session_state = TARG_SESS_STATE_FAILED;
4581}
4582
4583int iscsit_free_session(struct iscsi_session *sess)
4584{
4585 u16 conn_count = atomic_read(&sess->nconn);
4586 struct iscsi_conn *conn, *conn_tmp = NULL;
4587 int is_last;
4588
4589 spin_lock_bh(&sess->conn_lock);
4590 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4591
4592 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4593 conn_list) {
4594 if (conn_count == 0)
4595 break;
4596
4597 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4598 is_last = 1;
4599 } else {
4600 iscsit_inc_conn_usage_count(conn_tmp);
4601 is_last = 0;
4602 }
4603 iscsit_inc_conn_usage_count(conn);
4604
4605 spin_unlock_bh(&sess->conn_lock);
4606 iscsit_cause_connection_reinstatement(conn, 1);
4607 spin_lock_bh(&sess->conn_lock);
4608
4609 iscsit_dec_conn_usage_count(conn);
4610 if (is_last == 0)
4611 iscsit_dec_conn_usage_count(conn_tmp);
4612
4613 conn_count--;
4614 }
4615
4616 if (atomic_read(&sess->nconn)) {
4617 spin_unlock_bh(&sess->conn_lock);
4618 wait_for_completion(&sess->session_wait_comp);
4619 } else
4620 spin_unlock_bh(&sess->conn_lock);
4621
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004622 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004623 return 0;
4624}
4625
4626void iscsit_stop_session(
4627 struct iscsi_session *sess,
4628 int session_sleep,
4629 int connection_sleep)
4630{
4631 u16 conn_count = atomic_read(&sess->nconn);
4632 struct iscsi_conn *conn, *conn_tmp = NULL;
4633 int is_last;
4634
4635 spin_lock_bh(&sess->conn_lock);
4636 if (session_sleep)
4637 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4638
4639 if (connection_sleep) {
4640 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4641 conn_list) {
4642 if (conn_count == 0)
4643 break;
4644
4645 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4646 is_last = 1;
4647 } else {
4648 iscsit_inc_conn_usage_count(conn_tmp);
4649 is_last = 0;
4650 }
4651 iscsit_inc_conn_usage_count(conn);
4652
4653 spin_unlock_bh(&sess->conn_lock);
4654 iscsit_cause_connection_reinstatement(conn, 1);
4655 spin_lock_bh(&sess->conn_lock);
4656
4657 iscsit_dec_conn_usage_count(conn);
4658 if (is_last == 0)
4659 iscsit_dec_conn_usage_count(conn_tmp);
4660 conn_count--;
4661 }
4662 } else {
4663 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4664 iscsit_cause_connection_reinstatement(conn, 0);
4665 }
4666
4667 if (session_sleep && atomic_read(&sess->nconn)) {
4668 spin_unlock_bh(&sess->conn_lock);
4669 wait_for_completion(&sess->session_wait_comp);
4670 } else
4671 spin_unlock_bh(&sess->conn_lock);
4672}
4673
4674int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4675{
4676 struct iscsi_session *sess;
4677 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4678 struct se_session *se_sess, *se_sess_tmp;
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004679 LIST_HEAD(free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004680 int session_count = 0;
4681
4682 spin_lock_bh(&se_tpg->session_lock);
4683 if (tpg->nsessions && !force) {
4684 spin_unlock_bh(&se_tpg->session_lock);
4685 return -1;
4686 }
4687
4688 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4689 sess_list) {
4690 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4691
4692 spin_lock(&sess->conn_lock);
4693 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4694 atomic_read(&sess->session_logout) ||
4695 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4696 spin_unlock(&sess->conn_lock);
4697 continue;
4698 }
4699 atomic_set(&sess->session_reinstatement, 1);
Nicholas Bellinger197b8062017-04-25 10:55:12 -07004700 atomic_set(&sess->session_fall_back_to_erl0, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004701 spin_unlock(&sess->conn_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004702
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004703 list_move_tail(&se_sess->sess_list, &free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004704 }
4705 spin_unlock_bh(&se_tpg->session_lock);
4706
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004707 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4708 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4709
4710 iscsit_free_session(sess);
4711 session_count++;
4712 }
4713
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004714 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4715 " Group: %hu\n", session_count, tpg->tpgt);
4716 return 0;
4717}
4718
4719MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4720MODULE_VERSION("4.1.x");
4721MODULE_AUTHOR("nab@Linux-iSCSI.org");
4722MODULE_LICENSE("GPL");
4723
4724module_init(iscsi_target_init_module);
4725module_exit(iscsi_target_cleanup_module);