blob: bd15a564fe246c462a8c04628c52cfb9bf66b80b [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>
Sagi Grimberg7bfca0c2018-01-25 13:56:46 +020029#include <linux/inet.h>
Bart Van Assche8dcf07b2016-11-14 15:47:14 -080030#include <net/ipv6.h>
Bart Van Asscheba929992015-05-08 10:11:12 +020031#include <scsi/scsi_proto.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000032#include <scsi/iscsi_proto.h>
Andy Groverd28b11692012-04-03 15:51:22 -070033#include <scsi/scsi_tcq.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000034#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050035#include <target/target_core_fabric.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000036
Sagi Grimberg67f091f2015-01-07 14:57:31 +020037#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000038#include "iscsi_target_parameters.h"
39#include "iscsi_target_seq_pdu_list.h"
Nicholas Bellingere48354c2011-07-23 06:43:04 +000040#include "iscsi_target_datain_values.h"
41#include "iscsi_target_erl0.h"
42#include "iscsi_target_erl1.h"
43#include "iscsi_target_erl2.h"
44#include "iscsi_target_login.h"
45#include "iscsi_target_tmr.h"
46#include "iscsi_target_tpg.h"
47#include "iscsi_target_util.h"
48#include "iscsi_target.h"
49#include "iscsi_target_device.h"
Sagi Grimberg67f091f2015-01-07 14:57:31 +020050#include <target/iscsi/iscsi_target_stat.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000051
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080052#include <target/iscsi/iscsi_transport.h>
53
Nicholas Bellingere48354c2011-07-23 06:43:04 +000054static LIST_HEAD(g_tiqn_list);
55static LIST_HEAD(g_np_list);
56static DEFINE_SPINLOCK(tiqn_lock);
Andy Groveree291e62014-01-24 16:18:54 -080057static DEFINE_MUTEX(np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000058
59static struct idr tiqn_idr;
Matthew Wilcox31ff0ce2018-06-19 01:23:04 -040060DEFINE_IDA(sess_ida);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000061struct mutex auth_id_lock;
Nicholas Bellingere48354c2011-07-23 06:43:04 +000062
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
Kees Cookf7c95642017-10-22 14:58:45 -0700375 timer_setup(&np->np_login_timer, iscsi_handle_login_thread_timeout, 0);
Bart Van Assche8a47aa92017-05-23 16:48:50 -0700376
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000377 ret = iscsi_target_setup_login_socket(np, sockaddr);
378 if (ret != 0) {
379 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800380 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000381 return ERR_PTR(ret);
382 }
383
384 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
385 if (IS_ERR(np->np_thread)) {
386 pr_err("Unable to create kthread: iscsi_np\n");
387 ret = PTR_ERR(np->np_thread);
388 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800389 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000390 return ERR_PTR(ret);
391 }
392 /*
393 * Increment the np_exports reference count now to prevent
394 * iscsit_del_np() below from being run while a new call to
395 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
396 * active. We don't need to hold np->np_thread_lock at this
397 * point because iscsi_np has not been added to g_np_list yet.
398 */
399 np->np_exports = 1;
Andy Groveree291e62014-01-24 16:18:54 -0800400 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000401
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000402 list_add_tail(&np->np_list, &g_np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800403 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000404
Andy Grover69d75572015-08-24 10:26:04 -0700405 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
406 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000407
408 return np;
409}
410
411int iscsit_reset_np_thread(
412 struct iscsi_np *np,
413 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700414 struct iscsi_portal_group *tpg,
415 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000416{
417 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000418 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
419 spin_unlock_bh(&np->np_thread_lock);
420 return 0;
421 }
422 np->np_thread_state = ISCSI_NP_THREAD_RESET;
Nicholas Bellinger978d13d2017-08-04 23:59:31 -0700423 atomic_inc(&np->np_reset_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000424
425 if (np->np_thread) {
426 spin_unlock_bh(&np->np_thread_lock);
427 send_sig(SIGINT, np->np_thread, 1);
428 wait_for_completion(&np->np_restart_comp);
429 spin_lock_bh(&np->np_thread_lock);
430 }
431 spin_unlock_bh(&np->np_thread_lock);
432
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700433 if (tpg_np && shutdown) {
434 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
435
436 wait_for_completion(&tpg_np->tpg_np_comp);
437 }
438
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000439 return 0;
440}
441
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800442static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000443{
Al Virobf6932f2012-07-21 08:55:18 +0100444 if (np->np_socket)
445 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000446}
447
448int iscsit_del_np(struct iscsi_np *np)
449{
450 spin_lock_bh(&np->np_thread_lock);
451 np->np_exports--;
452 if (np->np_exports) {
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700453 np->enabled = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000454 spin_unlock_bh(&np->np_thread_lock);
455 return 0;
456 }
457 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
458 spin_unlock_bh(&np->np_thread_lock);
459
460 if (np->np_thread) {
461 /*
462 * We need to send the signal to wakeup Linux/Net
463 * which may be sleeping in sock_accept()..
464 */
465 send_sig(SIGINT, np->np_thread, 1);
466 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800467 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000468 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800469
470 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000471
Andy Groveree291e62014-01-24 16:18:54 -0800472 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000473 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800474 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000475
Andy Grover69d75572015-08-24 10:26:04 -0700476 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
477 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000478
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800479 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000480 kfree(np);
481 return 0;
482}
483
Varun Prakashe8205cc2016-04-20 00:00:11 +0530484static void iscsit_get_rx_pdu(struct iscsi_conn *);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700485
Varun Prakashd2faaef2016-04-20 00:00:19 +0530486int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700487{
Nicholas Bellingera4467012016-10-30 17:30:08 -0700488 return iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700489}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530490EXPORT_SYMBOL(iscsit_queue_rsp);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700491
Varun Prakashd2faaef2016-04-20 00:00:19 +0530492void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700493{
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700494 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -0700495 if (!list_empty(&cmd->i_conn_node) &&
496 !(cmd->se_cmd.transport_state & CMD_T_FABRIC_STOP))
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700497 list_del_init(&cmd->i_conn_node);
498 spin_unlock_bh(&conn->cmd_lock);
499
Bart Van Assche4412a672017-05-23 16:48:43 -0700500 __iscsit_free_cmd(cmd, true);
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700501}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530502EXPORT_SYMBOL(iscsit_aborted_task);
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700503
Varun Prakash2854bb22016-04-20 00:00:08 +0530504static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
Bart Van Asschee1dfb212017-10-31 11:03:16 -0700505 u32, u32, const void *, void *);
Varun Prakash2854bb22016-04-20 00:00:08 +0530506static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
507
508static int
509iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
510 const void *data_buf, u32 data_buf_len)
511{
512 struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
513 struct kvec *iov;
514 u32 niov = 0, tx_size = ISCSI_HDR_LEN;
515 int ret;
516
517 iov = &cmd->iov_misc[0];
518 iov[niov].iov_base = cmd->pdu;
519 iov[niov++].iov_len = ISCSI_HDR_LEN;
520
521 if (conn->conn_ops->HeaderDigest) {
522 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
523
524 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
525 ISCSI_HDR_LEN, 0, NULL,
Bart Van Asschee1dfb212017-10-31 11:03:16 -0700526 header_digest);
Varun Prakash2854bb22016-04-20 00:00:08 +0530527
528 iov[0].iov_len += ISCSI_CRC_LEN;
529 tx_size += ISCSI_CRC_LEN;
530 pr_debug("Attaching CRC32C HeaderDigest"
531 " to opcode 0x%x 0x%08x\n",
532 hdr->opcode, *header_digest);
533 }
534
535 if (data_buf_len) {
536 u32 padding = ((-data_buf_len) & 3);
537
538 iov[niov].iov_base = (void *)data_buf;
539 iov[niov++].iov_len = data_buf_len;
540 tx_size += data_buf_len;
541
542 if (padding != 0) {
543 iov[niov].iov_base = &cmd->pad_bytes;
544 iov[niov++].iov_len = padding;
545 tx_size += padding;
546 pr_debug("Attaching %u additional"
547 " padding bytes.\n", padding);
548 }
549
550 if (conn->conn_ops->DataDigest) {
551 iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
552 data_buf, data_buf_len,
Bart Van Asschee1dfb212017-10-31 11:03:16 -0700553 padding, &cmd->pad_bytes,
554 &cmd->data_crc);
Varun Prakash2854bb22016-04-20 00:00:08 +0530555
556 iov[niov].iov_base = &cmd->data_crc;
557 iov[niov++].iov_len = ISCSI_CRC_LEN;
558 tx_size += ISCSI_CRC_LEN;
559 pr_debug("Attached DataDigest for %u"
560 " bytes opcode 0x%x, CRC 0x%08x\n",
561 data_buf_len, hdr->opcode, cmd->data_crc);
562 }
563 }
564
565 cmd->iov_misc_count = niov;
566 cmd->tx_size = tx_size;
567
568 ret = iscsit_send_tx_data(cmd, conn, 1);
569 if (ret < 0) {
570 iscsit_tx_thread_wait_for_tcp(conn);
571 return ret;
572 }
573
574 return 0;
575}
576
577static int iscsit_map_iovec(struct iscsi_cmd *, struct kvec *, u32, u32);
578static void iscsit_unmap_iovec(struct iscsi_cmd *);
579static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
580 u32, u32, u32, u8 *);
581static int
582iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
583 const struct iscsi_datain *datain)
584{
585 struct kvec *iov;
586 u32 iov_count = 0, tx_size = 0;
587 int ret, iov_ret;
588
589 iov = &cmd->iov_data[0];
590 iov[iov_count].iov_base = cmd->pdu;
591 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
592 tx_size += ISCSI_HDR_LEN;
593
594 if (conn->conn_ops->HeaderDigest) {
595 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
596
597 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
598 ISCSI_HDR_LEN, 0, NULL,
Bart Van Asschee1dfb212017-10-31 11:03:16 -0700599 header_digest);
Varun Prakash2854bb22016-04-20 00:00:08 +0530600
601 iov[0].iov_len += ISCSI_CRC_LEN;
602 tx_size += ISCSI_CRC_LEN;
603
604 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
605 *header_digest);
606 }
607
608 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
609 datain->offset, datain->length);
610 if (iov_ret < 0)
611 return -1;
612
613 iov_count += iov_ret;
614 tx_size += datain->length;
615
616 cmd->padding = ((-datain->length) & 3);
617 if (cmd->padding) {
618 iov[iov_count].iov_base = cmd->pad_bytes;
619 iov[iov_count++].iov_len = cmd->padding;
620 tx_size += cmd->padding;
621
622 pr_debug("Attaching %u padding bytes\n", cmd->padding);
623 }
624
625 if (conn->conn_ops->DataDigest) {
626 cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
627 cmd, datain->offset,
628 datain->length,
629 cmd->padding,
630 cmd->pad_bytes);
631
632 iov[iov_count].iov_base = &cmd->data_crc;
633 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
634 tx_size += ISCSI_CRC_LEN;
635
636 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
637 datain->length + cmd->padding, cmd->data_crc);
638 }
639
640 cmd->iov_data_count = iov_count;
641 cmd->tx_size = tx_size;
642
643 ret = iscsit_fe_sendpage_sg(cmd, conn);
644
645 iscsit_unmap_iovec(cmd);
646
647 if (ret < 0) {
648 iscsit_tx_thread_wait_for_tcp(conn);
649 return ret;
650 }
651
652 return 0;
653}
654
655static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
656 struct iscsi_datain_req *dr, const void *buf,
657 u32 buf_len)
658{
659 if (dr)
660 return iscsit_xmit_datain_pdu(conn, cmd, buf);
661 else
662 return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
663}
664
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700665static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
666{
667 return TARGET_PROT_NORMAL;
668}
669
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800670static struct iscsit_transport iscsi_target_transport = {
671 .name = "iSCSI/TCP",
672 .transport_type = ISCSI_TCP,
Nicholas Bellingerbd027d82016-05-14 22:23:34 -0700673 .rdma_shutdown = false,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800674 .owner = NULL,
675 .iscsit_setup_np = iscsit_setup_np,
676 .iscsit_accept_np = iscsit_accept_np,
677 .iscsit_free_np = iscsit_free_np,
678 .iscsit_get_login_rx = iscsit_get_login_rx,
679 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800680 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700681 .iscsit_immediate_queue = iscsit_immediate_queue,
682 .iscsit_response_queue = iscsit_response_queue,
683 .iscsit_queue_data_in = iscsit_queue_rsp,
684 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700685 .iscsit_aborted_task = iscsit_aborted_task,
Varun Prakash2854bb22016-04-20 00:00:08 +0530686 .iscsit_xmit_pdu = iscsit_xmit_pdu,
Varun Prakashe8205cc2016-04-20 00:00:11 +0530687 .iscsit_get_rx_pdu = iscsit_get_rx_pdu,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700688 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800689};
690
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000691static int __init iscsi_target_init_module(void)
692{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800693 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000694
695 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
Markus Elfring3829f382017-04-09 16:00:39 +0200696 iscsit_global = kzalloc(sizeof(*iscsit_global), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200697 if (!iscsit_global)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000698 return -1;
Markus Elfringc46e22f2017-04-09 15:34:50 +0200699
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800700 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000701 mutex_init(&auth_id_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000702 idr_init(&tiqn_idr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000703
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) {
Leo Zhang621a4362018-12-24 00:18:27 +0800717 pr_err("Unable to kmem_cache_create() for"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000718 " 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;
Bart Van Asschecfe2b622017-10-31 11:03:17 -0700836 const bool do_put = cmd->se_cmd.se_tfo != NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000837
838 if (!cmd->conn) {
839 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
840 cmd->init_task_tag);
841 return -1;
842 }
843 conn = cmd->conn;
844
845 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700846 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000847
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100848 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000849 if (!cmd->buf_ptr) {
850 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700851 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000852 return -1;
853 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000854
855 if (add_to_conn) {
856 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700857 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000858 spin_unlock_bh(&conn->cmd_lock);
859 }
860
861 cmd->i_state = ISTATE_SEND_REJECT;
862 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800863 /*
864 * Perform the kref_put now if se_cmd has already been setup by
865 * scsit_setup_scsi_cmd()
866 */
Bart Van Asschecfe2b622017-10-31 11:03:17 -0700867 if (do_put) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800868 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +0200869 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800870 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700871 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000872}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700873
874static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
875 unsigned char *buf)
876{
877 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
878}
879
880int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
881{
882 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
883}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530884EXPORT_SYMBOL(iscsit_reject_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000885
886/*
887 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700888 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000889 */
890static int iscsit_map_iovec(
891 struct iscsi_cmd *cmd,
892 struct kvec *iov,
893 u32 data_offset,
894 u32 data_length)
895{
896 u32 i = 0;
897 struct scatterlist *sg;
898 unsigned int page_off;
899
900 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700901 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000902 */
Imran Haider2b165092016-05-08 11:17:54 -0400903 u32 ent = data_offset / PAGE_SIZE;
904
905 if (ent >= cmd->se_cmd.t_data_nents) {
906 pr_err("Initial page entry out-of-bounds\n");
907 return -1;
908 }
909
910 sg = &cmd->se_cmd.t_data_sg[ent];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000911 page_off = (data_offset % PAGE_SIZE);
912
913 cmd->first_data_sg = sg;
914 cmd->first_data_sg_off = page_off;
915
916 while (data_length) {
917 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
918
919 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
920 iov[i].iov_len = cur_len;
921
922 data_length -= cur_len;
923 page_off = 0;
924 sg = sg_next(sg);
925 i++;
926 }
927
928 cmd->kmapped_nents = i;
929
930 return i;
931}
932
933static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
934{
935 u32 i;
936 struct scatterlist *sg;
937
938 sg = cmd->first_data_sg;
939
940 for (i = 0; i < cmd->kmapped_nents; i++)
941 kunmap(sg_page(&sg[i]));
942}
943
944static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
945{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700946 LIST_HEAD(ack_list);
947 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000948
949 conn->exp_statsn = exp_statsn;
950
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800951 if (conn->sess->sess_ops->RDMAExtensions)
952 return;
953
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000954 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700955 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000956 spin_lock(&cmd->istate_lock);
957 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800958 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000959 cmd->i_state = ISTATE_REMOVE;
960 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700961 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000962 continue;
963 }
964 spin_unlock(&cmd->istate_lock);
965 }
966 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700967
968 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800969 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700970 iscsit_free_cmd(cmd, false);
971 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000972}
973
974static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
975{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700976 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000977
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400978 iov_count += ISCSI_IOV_DATA_BUFFER;
Markus Elfringf1725112017-04-09 15:06:00 +0200979 cmd->iov_data = kcalloc(iov_count, sizeof(*cmd->iov_data), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200980 if (!cmd->iov_data)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000981 return -ENOMEM;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000982
983 cmd->orig_iov_data_count = iov_count;
984 return 0;
985}
986
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800987int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
988 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000989{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800990 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000991 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700992 int iscsi_task_attr;
993 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000994
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800995 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000996
997 hdr = (struct iscsi_scsi_req *) buf;
998 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000999
1000 /* FIXME; Add checks for AdditionalHeaderSegment */
1001
1002 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
1003 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1004 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1005 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001006 return iscsit_add_reject_cmd(cmd,
1007 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001008 }
1009
1010 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1011 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1012 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001013 * From RFC-3720 Section 10.3.1:
1014 *
1015 * "Either or both of R and W MAY be 1 when either the
1016 * Expected Data Transfer Length and/or Bidirectional Read
1017 * Expected Data Transfer Length are 0"
1018 *
1019 * For this case, go ahead and clear the unnecssary bits
1020 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001021 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001022 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1023 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001024
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001025 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001026 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001027 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001028 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001029
1030 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1031 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1032 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1033 " MUST be set if Expected Data Transfer Length is not 0."
1034 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001035 return iscsit_add_reject_cmd(cmd,
1036 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001037 }
1038
1039 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1040 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1041 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001042 return iscsit_add_reject_cmd(cmd,
1043 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001044 }
1045
1046 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1047 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1048 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001049 return iscsit_add_reject_cmd(cmd,
1050 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001051 }
1052
1053 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1054 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1055 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001056 return iscsit_add_reject_cmd(cmd,
1057 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001058 }
1059
Nicholas Bellingerba159912013-07-03 03:48:24 -07001060 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001061 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1062 pr_err("Expected Data Transfer Length and Length of"
1063 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1064 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001065 return iscsit_add_reject_cmd(cmd,
1066 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001067 }
1068
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001069 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001070 pr_err("DataSegmentLength: %u is greater than"
1071 " EDTL: %u, protocol error.\n", payload_length,
1072 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001073 return iscsit_add_reject_cmd(cmd,
1074 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001075 }
1076
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001077 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001078 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001079 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1080 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001081 return iscsit_add_reject_cmd(cmd,
1082 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001083 }
1084
1085 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1086 pr_err("DataSegmentLength: %u is greater than"
1087 " FirstBurstLength: %u, protocol error.\n",
1088 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001089 return iscsit_add_reject_cmd(cmd,
1090 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001091 }
1092
1093 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1094 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1095 DMA_NONE;
1096
Andy Groverd28b11692012-04-03 15:51:22 -07001097 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -07001098 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1099 /*
1100 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1101 */
1102 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1103 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001104 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001105 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001106 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001107 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001108 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001109 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001110 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001111 else {
1112 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001113 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
1114 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001115 }
1116
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001117 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
1118 cmd->i_state = ISTATE_NEW_CMD;
1119 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1120 cmd->immediate_data = (payload_length) ? 1 : 0;
1121 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1122 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1123 if (cmd->unsolicited_data)
1124 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1125
1126 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
Alexei Potashnik95473082015-07-21 15:07:56 -07001127 if (hdr->flags & ISCSI_FLAG_CMD_READ)
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02001128 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Alexei Potashnik95473082015-07-21 15:07:56 -07001129 else
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001130 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001131 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1132 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001133 cmd->first_burst_len = payload_length;
1134
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001135 if (!conn->sess->sess_ops->RDMAExtensions &&
1136 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001137 struct iscsi_datain_req *dr;
1138
1139 dr = iscsit_allocate_datain_req();
1140 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001141 return iscsit_add_reject_cmd(cmd,
1142 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001143
1144 iscsit_attach_datain_req(cmd, dr);
1145 }
1146
1147 /*
Andy Grover065ca1e2012-04-03 15:51:23 -07001148 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1149 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001150 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001151 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1152 cmd->data_direction, sam_task_attr,
1153 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -07001154
1155 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1156 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001157 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1158 conn->cid);
1159
Bart Van Asscheafc16602015-04-27 13:52:36 +02001160 target_get_sess_cmd(&cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001161
Christoph Hellwigde103c92012-11-06 12:24:09 -08001162 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1163 scsilun_to_int(&hdr->lun));
1164 if (cmd->sense_reason)
1165 goto attach_cmd;
1166
Bart Van Assche649ee052015-04-14 13:26:44 +02001167 /* only used for printks or comparing with ->ref_task_tag */
1168 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001169 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1170 if (cmd->sense_reason) {
1171 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001172 return iscsit_add_reject_cmd(cmd,
1173 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001174 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001175
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001176 goto attach_cmd;
1177 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001178
Christoph Hellwigde103c92012-11-06 12:24:09 -08001179 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001180 return iscsit_add_reject_cmd(cmd,
1181 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001182 }
1183
1184attach_cmd:
1185 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001186 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001187 spin_unlock_bh(&conn->cmd_lock);
1188 /*
1189 * Check if we need to delay processing because of ALUA
1190 * Active/NonOptimized primary access state..
1191 */
1192 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001193
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001194 return 0;
1195}
1196EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001197
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001198void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1199{
1200 iscsit_set_dataout_sequence_values(cmd);
1201
1202 spin_lock_bh(&cmd->dataout_timeout_lock);
1203 iscsit_start_dataout_timer(cmd, cmd->conn);
1204 spin_unlock_bh(&cmd->dataout_timeout_lock);
1205}
1206EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1207
1208int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1209 struct iscsi_scsi_req *hdr)
1210{
1211 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001212 /*
1213 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1214 * the Immediate Bit is not set, and no Immediate
1215 * Data is attached.
1216 *
1217 * A PDU/CmdSN carrying Immediate Data can only
1218 * be processed after the DataCRC has passed.
1219 * If the DataCRC fails, the CmdSN MUST NOT
1220 * be acknowledged. (See below)
1221 */
1222 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001223 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1224 (unsigned char *)hdr, hdr->cmdsn);
1225 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1226 return -1;
1227 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001228 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001229 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001230 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001231 }
1232
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001233 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001234
1235 /*
1236 * If no Immediate Data is attached, it's OK to return now.
1237 */
1238 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001239 if (!cmd->sense_reason && cmd->unsolicited_data)
1240 iscsit_set_unsoliticed_dataout(cmd);
1241 if (!cmd->sense_reason)
1242 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001243
Bart Van Asscheafc16602015-04-27 13:52:36 +02001244 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001245 return 0;
1246 }
1247
1248 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001249 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1250 * execution. These exceptions are processed in CmdSN order using
1251 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001252 */
Bart Van Assche8fa40112017-05-23 16:48:45 -07001253 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001254 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001255 /*
1256 * Call directly into transport_generic_new_cmd() to perform
1257 * the backend memory allocation.
1258 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001259 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001260 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001261 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001262
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001263 return 0;
1264}
1265EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1266
1267static int
1268iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1269 bool dump_payload)
1270{
1271 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1272 /*
1273 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1274 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001275 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001276 goto after_immediate_data;
Nicholas Bellingerabb85a92017-06-07 20:29:50 -07001277 /*
1278 * Check for underflow case where both EDTL and immediate data payload
1279 * exceeds what is presented by CDB's TRANSFER LENGTH, and what has
1280 * already been set in target_cmd_size_check() as se_cmd->data_length.
1281 *
1282 * For this special case, fail the command and dump the immediate data
1283 * payload.
1284 */
1285 if (cmd->first_burst_len > cmd->se_cmd.data_length) {
1286 cmd->sense_reason = TCM_INVALID_CDB_FIELD;
1287 goto after_immediate_data;
1288 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001289
1290 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1291 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001292after_immediate_data:
1293 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1294 /*
1295 * A PDU/CmdSN carrying Immediate Data passed
1296 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1297 * Immediate Bit is not set.
1298 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001299 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1300 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001301 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001302 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001303
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001304 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001305 int rc;
1306
1307 rc = iscsit_dump_data_payload(cmd->conn,
1308 cmd->first_burst_len, 1);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001309 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001310 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001311 } else if (cmd->unsolicited_data)
1312 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001313
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001314 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1315 /*
1316 * Immediate Data failed DataCRC and ERL>=1,
1317 * silently drop this PDU and let the initiator
1318 * plug the CmdSN gap.
1319 *
1320 * FIXME: Send Unsolicited NOPIN with reserved
1321 * TTT here to help the initiator figure out
1322 * the missing CmdSN, although they should be
1323 * intelligent enough to determine the missing
1324 * CmdSN and issue a retry to plug the sequence.
1325 */
1326 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001327 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001328 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1329 return -1;
1330
1331 return 0;
1332}
1333
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001334static int
1335iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1336 unsigned char *buf)
1337{
1338 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1339 int rc, immed_data;
1340 bool dump_payload = false;
1341
1342 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1343 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001344 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001345 /*
1346 * Allocation iovecs needed for struct socket operations for
1347 * traditional iSCSI block I/O.
1348 */
1349 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001350 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001351 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001352 }
1353 immed_data = cmd->immediate_data;
1354
1355 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1356 if (rc < 0)
1357 return rc;
1358 else if (rc > 0)
1359 dump_payload = true;
1360
1361 if (!immed_data)
1362 return 0;
1363
1364 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1365}
1366
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001367static u32 iscsit_do_crypto_hash_sg(
Herbert Xu69110e32016-01-24 21:19:52 +08001368 struct ahash_request *hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001369 struct iscsi_cmd *cmd,
1370 u32 data_offset,
1371 u32 data_length,
1372 u32 padding,
1373 u8 *pad_bytes)
1374{
1375 u32 data_crc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001376 struct scatterlist *sg;
1377 unsigned int page_off;
1378
Herbert Xu69110e32016-01-24 21:19:52 +08001379 crypto_ahash_init(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001380
1381 sg = cmd->first_data_sg;
1382 page_off = cmd->first_data_sg_off;
1383
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001384 while (data_length) {
Alexei Potashnikaa756792015-07-20 17:12:12 -07001385 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001386
Herbert Xu69110e32016-01-24 21:19:52 +08001387 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1388 crypto_ahash_update(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001389
1390 data_length -= cur_len;
1391 page_off = 0;
Alexei Potashnikaa756792015-07-20 17:12:12 -07001392 /* iscsit_map_iovec has already checked for invalid sg pointers */
1393 sg = sg_next(sg);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001394 }
1395
1396 if (padding) {
1397 struct scatterlist pad_sg;
1398
1399 sg_init_one(&pad_sg, pad_bytes, padding);
Herbert Xu69110e32016-01-24 21:19:52 +08001400 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1401 padding);
1402 crypto_ahash_finup(hash);
1403 } else {
1404 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1405 crypto_ahash_final(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001406 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001407
1408 return data_crc;
1409}
1410
Bart Van Asschee1dfb212017-10-31 11:03:16 -07001411static void iscsit_do_crypto_hash_buf(struct ahash_request *hash,
1412 const void *buf, u32 payload_length, u32 padding,
1413 const void *pad_bytes, void *data_crc)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001414{
Herbert Xu69110e32016-01-24 21:19:52 +08001415 struct scatterlist sg[2];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001416
Herbert Xu69110e32016-01-24 21:19:52 +08001417 sg_init_table(sg, ARRAY_SIZE(sg));
1418 sg_set_buf(sg, buf, payload_length);
Laura Abbott679fcae2018-09-04 11:47:40 -07001419 if (padding)
1420 sg_set_buf(sg + 1, pad_bytes, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001421
Herbert Xu69110e32016-01-24 21:19:52 +08001422 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001423
Herbert Xu69110e32016-01-24 21:19:52 +08001424 crypto_ahash_digest(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001425}
1426
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001427int
Varun Prakash9a584bf2017-01-13 20:53:21 +05301428__iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1429 struct iscsi_cmd *cmd, u32 payload_length,
1430 bool *success)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001431{
Varun Prakash9a584bf2017-01-13 20:53:21 +05301432 struct iscsi_data *hdr = buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001433 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001434 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001435
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001436 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001437 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001438
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001439 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1440 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001441 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001442 payload_length, conn->cid);
1443
1444 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1445 pr_err("Command ITT: 0x%08x received DataOUT after"
1446 " last DataOUT received, dumping payload\n",
1447 cmd->init_task_tag);
1448 return iscsit_dump_data_payload(conn, payload_length, 1);
1449 }
1450
1451 if (cmd->data_direction != DMA_TO_DEVICE) {
1452 pr_err("Command ITT: 0x%08x received DataOUT for a"
1453 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001454 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001455 }
1456 se_cmd = &cmd->se_cmd;
1457 iscsit_mod_dataout_timer(cmd);
1458
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001459 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Bart Van Asschede3493a2017-10-31 11:03:15 -07001460 pr_err("DataOut Offset: %u, Length %u greater than iSCSI Command EDTL %u, protocol error.\n",
1461 be32_to_cpu(hdr->offset), payload_length,
1462 cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001463 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001464 }
1465
1466 if (cmd->unsolicited_data) {
1467 int dump_unsolicited_data = 0;
1468
1469 if (conn->sess->sess_ops->InitialR2T) {
1470 pr_err("Received unexpected unsolicited data"
1471 " while InitialR2T=Yes, protocol error.\n");
1472 transport_send_check_condition_and_sense(&cmd->se_cmd,
1473 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1474 return -1;
1475 }
1476 /*
1477 * Special case for dealing with Unsolicited DataOUT
1478 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1479 * failures;
1480 */
1481
1482 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001483 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001484 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001485 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001486
1487 if (dump_unsolicited_data) {
1488 /*
1489 * Check if a delayed TASK_ABORTED status needs to
1490 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
Bart Van Assche5a342522015-10-22 15:53:22 -07001491 * received with the unsolicited data out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001492 */
1493 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1494 iscsit_stop_dataout_timer(cmd);
1495
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001496 return iscsit_dump_data_payload(conn, payload_length, 1);
1497 }
1498 } else {
1499 /*
1500 * For the normal solicited data path:
1501 *
1502 * Check for a delayed TASK_ABORTED status and dump any
1503 * incoming data out payload if one exists. Also, when the
1504 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1505 * data out sequence, we decrement outstanding_r2ts. Once
1506 * outstanding_r2ts reaches zero, go ahead and send the delayed
1507 * TASK_ABORTED status.
1508 */
Christoph Hellwig7d680f3b2011-12-21 14:13:47 -05001509 if (se_cmd->transport_state & CMD_T_ABORTED) {
Bart Van Asscheaaa00cc2018-11-27 15:52:02 -08001510 if (hdr->flags & ISCSI_FLAG_CMD_FINAL &&
1511 --cmd->outstanding_r2ts < 1)
1512 iscsit_stop_dataout_timer(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001513
1514 return iscsit_dump_data_payload(conn, payload_length, 1);
1515 }
1516 }
1517 /*
Bart Van Assche0d5efb82016-12-23 14:37:52 +01001518 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001519 * within-command recovery checks before receiving the payload.
1520 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001521 rc = iscsit_check_pre_dataout(cmd, buf);
1522 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001523 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001524 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001525 return -1;
Varun Prakash9a584bf2017-01-13 20:53:21 +05301526 *success = true;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001527 return 0;
1528}
Varun Prakash9a584bf2017-01-13 20:53:21 +05301529EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
1530
1531int
1532iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1533 struct iscsi_cmd **out_cmd)
1534{
1535 struct iscsi_data *hdr = buf;
1536 struct iscsi_cmd *cmd;
1537 u32 payload_length = ntoh24(hdr->dlength);
1538 int rc;
1539 bool success = false;
1540
1541 if (!payload_length) {
1542 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1543 return 0;
1544 }
1545
1546 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1547 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1548 " MaxXmitDataSegmentLength: %u\n", payload_length,
1549 conn->conn_ops->MaxXmitDataSegmentLength);
1550 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
1551 }
1552
1553 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
1554 if (!cmd)
1555 return 0;
1556
1557 rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
1558
1559 if (success)
1560 *out_cmd = cmd;
1561
1562 return rc;
1563}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001564EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1565
1566static int
1567iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1568 struct iscsi_data *hdr)
1569{
1570 struct kvec *iov;
1571 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1572 u32 payload_length = ntoh24(hdr->dlength);
1573 int iov_ret, data_crc_failed = 0;
1574
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001575 rx_size += payload_length;
1576 iov = &cmd->iov_data[0];
1577
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001578 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1579 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001580 if (iov_ret < 0)
1581 return -1;
1582
1583 iov_count += iov_ret;
1584
1585 padding = ((-payload_length) & 3);
1586 if (padding != 0) {
1587 iov[iov_count].iov_base = cmd->pad_bytes;
1588 iov[iov_count++].iov_len = padding;
1589 rx_size += padding;
1590 pr_debug("Receiving %u padding bytes.\n", padding);
1591 }
1592
1593 if (conn->conn_ops->DataDigest) {
1594 iov[iov_count].iov_base = &checksum;
1595 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1596 rx_size += ISCSI_CRC_LEN;
1597 }
1598
1599 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1600
1601 iscsit_unmap_iovec(cmd);
1602
1603 if (rx_got != rx_size)
1604 return -1;
1605
1606 if (conn->conn_ops->DataDigest) {
1607 u32 data_crc;
1608
Herbert Xu69110e32016-01-24 21:19:52 +08001609 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001610 be32_to_cpu(hdr->offset),
1611 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001612 cmd->pad_bytes);
1613
1614 if (checksum != data_crc) {
1615 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1616 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1617 " does not match computed 0x%08x\n",
1618 hdr->itt, hdr->offset, payload_length,
1619 hdr->datasn, checksum, data_crc);
1620 data_crc_failed = 1;
1621 } else {
1622 pr_debug("Got CRC32C DataDigest 0x%08x for"
1623 " %u bytes of Data Out\n", checksum,
1624 payload_length);
1625 }
1626 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001627
1628 return data_crc_failed;
1629}
1630
1631int
1632iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1633 bool data_crc_failed)
1634{
1635 struct iscsi_conn *conn = cmd->conn;
1636 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001637 /*
1638 * Increment post receive data and CRC values or perform
1639 * within-command recovery.
1640 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001641 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1642 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001643 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001644 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001645 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001646 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1647 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001648 /*
1649 * Handle extra special case for out of order
1650 * Unsolicited Data Out.
1651 */
1652 spin_lock_bh(&cmd->istate_lock);
1653 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1654 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1655 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1656 spin_unlock_bh(&cmd->istate_lock);
1657
1658 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001659 if (ooo_cmdsn)
1660 return 0;
1661 target_execute_cmd(&cmd->se_cmd);
1662 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001663 } else /* DATAOUT_CANNOT_RECOVER */
1664 return -1;
1665
1666 return 0;
1667}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001668EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001669
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001670static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1671{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001672 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001673 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1674 int rc;
1675 bool data_crc_failed = false;
1676
1677 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1678 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001679 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001680 else if (!cmd)
1681 return 0;
1682
1683 rc = iscsit_get_dataout(conn, cmd, hdr);
1684 if (rc < 0)
1685 return rc;
1686 else if (rc > 0)
1687 data_crc_failed = true;
1688
1689 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1690}
1691
Nicholas Bellinger778de362013-06-14 16:07:47 -07001692int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1693 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001694{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001695 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001696
Arshad Hussaina3662602014-03-14 15:28:59 -07001697 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1698 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1699 if (!cmd)
1700 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1701 (unsigned char *)hdr);
1702
1703 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1704 (unsigned char *)hdr);
1705 }
1706
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001707 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001708 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1709 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001710 if (!cmd)
1711 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1712 (unsigned char *)hdr);
1713
Nicholas Bellingerba159912013-07-03 03:48:24 -07001714 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1715 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001716 }
1717
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001718 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001719 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001720 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001721 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001722 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001723 if (!cmd)
1724 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1725 (unsigned char *)hdr);
1726
Nicholas Bellingerba159912013-07-03 03:48:24 -07001727 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1728 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001729 }
1730
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001731 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001732 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001733 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001734 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1735 payload_length);
1736 /*
1737 * This is not a response to a Unsolicited NopIN, which means
1738 * it can either be a NOPOUT ping request (with a valid ITT),
1739 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1740 * Either way, make sure we allocate an struct iscsi_cmd, as both
1741 * can contain ping data.
1742 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001743 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001744 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1745 cmd->i_state = ISTATE_SEND_NOPIN;
1746 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1747 1 : 0);
1748 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1749 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001750 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1751 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001752 cmd->data_direction = DMA_NONE;
1753 }
1754
Nicholas Bellinger778de362013-06-14 16:07:47 -07001755 return 0;
1756}
1757EXPORT_SYMBOL(iscsit_setup_nop_out);
1758
1759int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1760 struct iscsi_nopout *hdr)
1761{
1762 struct iscsi_cmd *cmd_p = NULL;
1763 int cmdsn_ret = 0;
1764 /*
1765 * Initiator is expecting a NopIN ping reply..
1766 */
1767 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001768 if (!cmd)
1769 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1770 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001771
1772 spin_lock_bh(&conn->cmd_lock);
1773 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1774 spin_unlock_bh(&conn->cmd_lock);
1775
1776 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1777
1778 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1779 iscsit_add_cmd_to_response_queue(cmd, conn,
1780 cmd->i_state);
1781 return 0;
1782 }
1783
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001784 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1785 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001786 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1787 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001788 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001789 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001790
1791 return 0;
1792 }
1793 /*
1794 * This was a response to a unsolicited NOPIN ping.
1795 */
1796 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1797 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1798 if (!cmd_p)
1799 return -EINVAL;
1800
1801 iscsit_stop_nopin_response_timer(conn);
1802
1803 cmd_p->i_state = ISTATE_REMOVE;
1804 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1805
1806 iscsit_start_nopin_timer(conn);
1807 return 0;
1808 }
1809 /*
1810 * Otherwise, initiator is not expecting a NOPIN is response.
1811 * Just ignore for now.
1812 */
Varun Prakash1a40f0a2016-09-15 21:20:11 +05301813
1814 if (cmd)
1815 iscsit_free_cmd(cmd, false);
1816
Nicholas Bellinger778de362013-06-14 16:07:47 -07001817 return 0;
1818}
1819EXPORT_SYMBOL(iscsit_process_nop_out);
1820
1821static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1822 unsigned char *buf)
1823{
1824 unsigned char *ping_data = NULL;
1825 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1826 struct kvec *iov = NULL;
1827 u32 payload_length = ntoh24(hdr->dlength);
1828 int ret;
1829
1830 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1831 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001832 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001833 /*
1834 * Handle NOP-OUT payload for traditional iSCSI sockets
1835 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001836 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001837 u32 checksum, data_crc, padding = 0;
1838 int niov = 0, rx_got, rx_size = payload_length;
1839
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001840 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1841 if (!ping_data) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001842 ret = -1;
1843 goto out;
1844 }
1845
1846 iov = &cmd->iov_misc[0];
1847 iov[niov].iov_base = ping_data;
1848 iov[niov++].iov_len = payload_length;
1849
1850 padding = ((-payload_length) & 3);
1851 if (padding != 0) {
1852 pr_debug("Receiving %u additional bytes"
1853 " for padding.\n", padding);
1854 iov[niov].iov_base = &cmd->pad_bytes;
1855 iov[niov++].iov_len = padding;
1856 rx_size += padding;
1857 }
1858 if (conn->conn_ops->DataDigest) {
1859 iov[niov].iov_base = &checksum;
1860 iov[niov++].iov_len = ISCSI_CRC_LEN;
1861 rx_size += ISCSI_CRC_LEN;
1862 }
1863
1864 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1865 if (rx_got != rx_size) {
1866 ret = -1;
1867 goto out;
1868 }
1869
1870 if (conn->conn_ops->DataDigest) {
Bart Van Asschee1dfb212017-10-31 11:03:16 -07001871 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, ping_data,
1872 payload_length, padding,
1873 cmd->pad_bytes, &data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001874
1875 if (checksum != data_crc) {
1876 pr_err("Ping data CRC32C DataDigest"
1877 " 0x%08x does not match computed 0x%08x\n",
1878 checksum, data_crc);
1879 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1880 pr_err("Unable to recover from"
1881 " NOPOUT Ping DataCRC failure while in"
1882 " ERL=0.\n");
1883 ret = -1;
1884 goto out;
1885 } else {
1886 /*
1887 * Silently drop this PDU and let the
1888 * initiator plug the CmdSN gap.
1889 */
1890 pr_debug("Dropping NOPOUT"
1891 " Command CmdSN: 0x%08x due to"
1892 " DataCRC error.\n", hdr->cmdsn);
1893 ret = 0;
1894 goto out;
1895 }
1896 } else {
1897 pr_debug("Got CRC32C DataDigest"
1898 " 0x%08x for %u bytes of ping data.\n",
1899 checksum, payload_length);
1900 }
1901 }
1902
1903 ping_data[payload_length] = '\0';
1904 /*
1905 * Attach ping data to struct iscsi_cmd->buf_ptr.
1906 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001907 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001908 cmd->buf_ptr_size = payload_length;
1909
1910 pr_debug("Got %u bytes of NOPOUT ping"
1911 " data.\n", payload_length);
1912 pr_debug("Ping Data: \"%s\"\n", ping_data);
1913 }
1914
Nicholas Bellinger778de362013-06-14 16:07:47 -07001915 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001916out:
1917 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001918 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001919
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001920 kfree(ping_data);
1921 return ret;
1922}
1923
Bart Van Asschee381fe92017-01-06 11:32:08 +01001924static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
1925{
1926 switch (iscsi_tmf) {
1927 case ISCSI_TM_FUNC_ABORT_TASK:
1928 return TMR_ABORT_TASK;
1929 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1930 return TMR_ABORT_TASK_SET;
1931 case ISCSI_TM_FUNC_CLEAR_ACA:
1932 return TMR_CLEAR_ACA;
1933 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1934 return TMR_CLEAR_TASK_SET;
1935 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1936 return TMR_LUN_RESET;
1937 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1938 return TMR_TARGET_WARM_RESET;
1939 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1940 return TMR_TARGET_COLD_RESET;
1941 default:
1942 return TMR_UNKNOWN;
1943 }
1944}
1945
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001946int
1947iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1948 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001949{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001950 struct se_tmr_req *se_tmr;
1951 struct iscsi_tmr_req *tmr_req;
1952 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001953 int out_of_order_cmdsn = 0, ret;
Bart Van Assche59b69862017-01-05 12:39:57 +01001954 u8 function, tcm_function = TMR_UNKNOWN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001955
1956 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001957 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1958 function = hdr->flags;
1959
1960 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1961 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1962 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1963 hdr->rtt, hdr->refcmdsn, conn->cid);
1964
1965 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1966 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001967 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001968 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001969 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001970 }
1971
1972 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1973 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1974 pr_err("Task Management Request TASK_REASSIGN not"
1975 " issued as immediate command, bad iSCSI Initiator"
1976 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001977 return iscsit_add_reject_cmd(cmd,
1978 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001979 }
1980 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001981 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1982 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001983
Andy Groverd28b11692012-04-03 15:51:22 -07001984 cmd->data_direction = DMA_NONE;
Markus Elfring3829f382017-04-09 16:00:39 +02001985 cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL);
Nicholas Bellingerae072722017-10-27 12:32:59 -07001986 if (!cmd->tmr_req) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001987 return iscsit_add_reject_cmd(cmd,
1988 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1989 buf);
Nicholas Bellingerae072722017-10-27 12:32:59 -07001990 }
1991
1992 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
1993 conn->sess->se_sess, 0, DMA_NONE,
1994 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
1995
1996 target_get_sess_cmd(&cmd->se_cmd, true);
Andy Groverd28b11692012-04-03 15:51:22 -07001997
1998 /*
1999 * TASK_REASSIGN for ERL=2 / connection stays inside of
2000 * LIO-Target $FABRIC_MOD
2001 */
2002 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Bart Van Asschee381fe92017-01-06 11:32:08 +01002003 tcm_function = iscsit_convert_tmf(function);
2004 if (tcm_function == TMR_UNKNOWN) {
Andy Groverd28b11692012-04-03 15:51:22 -07002005 pr_err("Unknown iSCSI TMR Function:"
2006 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002007 return iscsit_add_reject_cmd(cmd,
2008 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002009 }
Bart Van Assche59b69862017-01-05 12:39:57 +01002010 }
2011 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
2012 GFP_KERNEL);
2013 if (ret < 0)
2014 return iscsit_add_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002015 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002016
Bart Van Assche59b69862017-01-05 12:39:57 +01002017 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002018
2019 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2020 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2021 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2022 cmd->init_task_tag = hdr->itt;
2023 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002024 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2025 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002026 se_tmr = cmd->se_cmd.se_tmr_req;
2027 tmr_req = cmd->tmr_req;
2028 /*
2029 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2030 */
2031 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08002032 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
2033 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002034 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002035 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2036 goto attach;
2037 }
2038 }
2039
2040 switch (function) {
2041 case ISCSI_TM_FUNC_ABORT_TASK:
2042 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002043 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002044 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002045 break;
2046 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2047 case ISCSI_TM_FUNC_CLEAR_ACA:
2048 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2049 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2050 break;
2051 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2052 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002053 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2054 goto attach;
2055 }
2056 break;
2057 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2058 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002059 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2060 goto attach;
2061 }
2062 break;
2063 case ISCSI_TM_FUNC_TASK_REASSIGN:
2064 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2065 /*
2066 * Perform sanity checks on the ExpDataSN only if the
2067 * TASK_REASSIGN was successful.
2068 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002069 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002070 break;
2071
2072 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002073 return iscsit_add_reject_cmd(cmd,
2074 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002075 break;
2076 default:
2077 pr_err("Unknown TMR function: 0x%02x, protocol"
2078 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002079 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2080 goto attach;
2081 }
2082
2083 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2084 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2085 se_tmr->call_transport = 1;
2086attach:
2087 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002088 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002089 spin_unlock_bh(&conn->cmd_lock);
2090
2091 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002092 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellinger3fc9fb12017-10-27 20:52:56 -07002093 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002094 out_of_order_cmdsn = 1;
Nicholas Bellinger3fc9fb12017-10-27 20:52:56 -07002095 } else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2096 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002097 return 0;
Nicholas Bellinger3fc9fb12017-10-27 20:52:56 -07002098 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002099 return -1;
Nicholas Bellinger3fc9fb12017-10-27 20:52:56 -07002100 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002101 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002102 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002103
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002104 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002105 return 0;
2106 /*
2107 * Found the referenced task, send to transport for processing.
2108 */
2109 if (se_tmr->call_transport)
2110 return transport_generic_handle_tmr(&cmd->se_cmd);
2111
2112 /*
2113 * Could not find the referenced LUN, task, or Task Management
2114 * command not authorized or supported. Change state and
2115 * let the tx_thread send the response.
2116 *
2117 * For connection recovery, this is also the default action for
2118 * TMR TASK_REASSIGN.
2119 */
2120 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellingerae072722017-10-27 12:32:59 -07002121 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002122 return 0;
2123}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002124EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002125
2126/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002127int
2128iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2129 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002130{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002131 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002132
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002133 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002134 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002135 "greater than MaxXmitDataSegmentLength %u.\n",
2136 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002137 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2138 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002139 }
2140
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08002141 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2142 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2143 pr_err("Multi sequence text commands currently not supported\n");
2144 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2145 (unsigned char *)hdr);
2146 }
2147
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002148 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2149 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2150 hdr->exp_statsn, payload_length);
2151
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002152 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2153 cmd->i_state = ISTATE_SEND_TEXTRSP;
2154 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2155 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2156 cmd->targ_xfer_tag = 0xFFFFFFFF;
2157 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2158 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2159 cmd->data_direction = DMA_NONE;
Varun Prakashea8dc5b2017-07-23 20:03:33 +05302160 kfree(cmd->text_in_ptr);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002161 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002162
2163 return 0;
2164}
2165EXPORT_SYMBOL(iscsit_setup_text_cmd);
2166
2167int
2168iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2169 struct iscsi_text *hdr)
2170{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002171 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002172 int cmdsn_ret;
2173
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002174 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002175 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2176 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2177 pr_err("Unable to locate text_in buffer for sendtargets"
2178 " discovery\n");
2179 goto reject;
2180 }
2181 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002182 }
2183 if (strncmp("SendTargets", text_in, 11) != 0) {
2184 pr_err("Received Text Data that is not"
2185 " SendTargets, cannot continue.\n");
2186 goto reject;
2187 }
2188 text_ptr = strchr(text_in, '=');
2189 if (!text_ptr) {
2190 pr_err("No \"=\" separator found in Text Data,"
2191 " cannot continue.\n");
2192 goto reject;
2193 }
2194 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002195 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002196 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2197 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002198 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002199 } else {
2200 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2201 goto reject;
2202 }
2203
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002204 spin_lock_bh(&conn->cmd_lock);
2205 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2206 spin_unlock_bh(&conn->cmd_lock);
2207
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002208empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002209 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2210
2211 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002212 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2213 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002214 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002215 return -1;
2216
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002217 return 0;
2218 }
2219
2220 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002221
2222reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002223 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2224 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002225}
2226EXPORT_SYMBOL(iscsit_process_text_cmd);
2227
2228static int
2229iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2230 unsigned char *buf)
2231{
2232 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2233 char *text_in = NULL;
2234 u32 payload_length = ntoh24(hdr->dlength);
2235 int rx_size, rc;
2236
2237 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2238 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002239 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002240
2241 rx_size = payload_length;
2242 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002243 u32 checksum = 0, data_crc = 0;
2244 u32 padding = 0, pad_bytes = 0;
2245 int niov = 0, rx_got;
2246 struct kvec iov[3];
2247
2248 text_in = kzalloc(payload_length, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02002249 if (!text_in)
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002250 goto reject;
Markus Elfringc46e22f2017-04-09 15:34:50 +02002251
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002252 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002253
2254 memset(iov, 0, 3 * sizeof(struct kvec));
2255 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002256 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002257
2258 padding = ((-payload_length) & 3);
2259 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002260 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002261 iov[niov++].iov_len = padding;
2262 rx_size += padding;
2263 pr_debug("Receiving %u additional bytes"
2264 " for padding.\n", padding);
2265 }
2266 if (conn->conn_ops->DataDigest) {
2267 iov[niov].iov_base = &checksum;
2268 iov[niov++].iov_len = ISCSI_CRC_LEN;
2269 rx_size += ISCSI_CRC_LEN;
2270 }
2271
2272 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002273 if (rx_got != rx_size)
2274 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002275
2276 if (conn->conn_ops->DataDigest) {
Bart Van Asschee1dfb212017-10-31 11:03:16 -07002277 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, text_in,
2278 payload_length, padding,
2279 &pad_bytes, &data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002280
2281 if (checksum != data_crc) {
2282 pr_err("Text data CRC32C DataDigest"
2283 " 0x%08x does not match computed"
2284 " 0x%08x\n", checksum, data_crc);
2285 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2286 pr_err("Unable to recover from"
2287 " Text Data digest failure while in"
2288 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002289 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002290 } else {
2291 /*
2292 * Silently drop this PDU and let the
2293 * initiator plug the CmdSN gap.
2294 */
2295 pr_debug("Dropping Text"
2296 " Command CmdSN: 0x%08x due to"
2297 " DataCRC error.\n", hdr->cmdsn);
2298 kfree(text_in);
2299 return 0;
2300 }
2301 } else {
2302 pr_debug("Got CRC32C DataDigest"
2303 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002304 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002305 }
2306 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002307 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002308 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002309 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002310 }
2311
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002312 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002313
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002314reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002315 kfree(cmd->text_in_ptr);
2316 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002317 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002318}
2319
2320int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2321{
2322 struct iscsi_conn *conn_p;
2323 struct iscsi_session *sess = conn->sess;
2324
2325 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2326 " for SID: %u.\n", conn->cid, conn->sess->sid);
2327
2328 atomic_set(&sess->session_logout, 1);
2329 atomic_set(&conn->conn_logout_remove, 1);
2330 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2331
2332 iscsit_inc_conn_usage_count(conn);
2333 iscsit_inc_session_usage_count(sess);
2334
2335 spin_lock_bh(&sess->conn_lock);
2336 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2337 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2338 continue;
2339
2340 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2341 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2342 }
2343 spin_unlock_bh(&sess->conn_lock);
2344
2345 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2346
2347 return 0;
2348}
2349
2350int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2351{
2352 struct iscsi_conn *l_conn;
2353 struct iscsi_session *sess = conn->sess;
2354
2355 pr_debug("Received logout request CLOSECONNECTION for CID:"
2356 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2357
2358 /*
2359 * A Logout Request with a CLOSECONNECTION reason code for a CID
2360 * can arrive on a connection with a differing CID.
2361 */
2362 if (conn->cid == cmd->logout_cid) {
2363 spin_lock_bh(&conn->state_lock);
2364 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2365 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2366
2367 atomic_set(&conn->conn_logout_remove, 1);
2368 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2369 iscsit_inc_conn_usage_count(conn);
2370
2371 spin_unlock_bh(&conn->state_lock);
2372 } else {
2373 /*
2374 * Handle all different cid CLOSECONNECTION requests in
2375 * iscsit_logout_post_handler_diffcid() as to give enough
2376 * time for any non immediate command's CmdSN to be
2377 * acknowledged on the connection in question.
2378 *
2379 * Here we simply make sure the CID is still around.
2380 */
2381 l_conn = iscsit_get_conn_from_cid(sess,
2382 cmd->logout_cid);
2383 if (!l_conn) {
2384 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2385 iscsit_add_cmd_to_response_queue(cmd, conn,
2386 cmd->i_state);
2387 return 0;
2388 }
2389
2390 iscsit_dec_conn_usage_count(l_conn);
2391 }
2392
2393 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2394
2395 return 0;
2396}
2397
2398int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2399{
2400 struct iscsi_session *sess = conn->sess;
2401
2402 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2403 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2404
2405 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2406 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2407 " while ERL!=2.\n");
2408 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2409 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2410 return 0;
2411 }
2412
2413 if (conn->cid == cmd->logout_cid) {
2414 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2415 " with CID: %hu on CID: %hu, implementation error.\n",
2416 cmd->logout_cid, conn->cid);
2417 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2418 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2419 return 0;
2420 }
2421
2422 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2423
2424 return 0;
2425}
2426
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002427int
2428iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2429 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002430{
2431 int cmdsn_ret, logout_remove = 0;
2432 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002433 struct iscsi_logout *hdr;
2434 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2435
2436 hdr = (struct iscsi_logout *) buf;
2437 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002438
2439 if (tiqn) {
2440 spin_lock(&tiqn->logout_stats.lock);
2441 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2442 tiqn->logout_stats.normal_logouts++;
2443 else
2444 tiqn->logout_stats.abnormal_logouts++;
2445 spin_unlock(&tiqn->logout_stats.lock);
2446 }
2447
2448 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2449 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2450 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2451 hdr->cid, conn->cid);
2452
2453 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2454 pr_err("Received logout request on connection that"
2455 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002456 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002457 return 0;
2458 }
2459
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002460 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2461 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2462 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2463 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2464 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002465 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2466 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2467 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002468 cmd->logout_reason = reason_code;
2469 cmd->data_direction = DMA_NONE;
2470
2471 /*
2472 * We need to sleep in these cases (by returning 1) until the Logout
2473 * Response gets sent in the tx thread.
2474 */
2475 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2476 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002477 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002478 logout_remove = 1;
2479
2480 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002481 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002482 spin_unlock_bh(&conn->cmd_lock);
2483
2484 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002485 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002486
2487 /*
2488 * Immediate commands are executed, well, immediately.
2489 * Non-Immediate Logout Commands are executed in CmdSN order.
2490 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002491 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002492 int ret = iscsit_execute_cmd(cmd, 0);
2493
2494 if (ret < 0)
2495 return ret;
2496 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002497 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002498 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002499 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002500 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2501 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002502 }
2503
2504 return logout_remove;
2505}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002506EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002507
Varun Prakashd2faaef2016-04-20 00:00:19 +05302508int iscsit_handle_snack(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002509 struct iscsi_conn *conn,
2510 unsigned char *buf)
2511{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002512 struct iscsi_snack *hdr;
2513
2514 hdr = (struct iscsi_snack *) buf;
2515 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002516
2517 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2518 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2519 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2520 hdr->begrun, hdr->runlength, conn->cid);
2521
2522 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2523 pr_err("Initiator sent SNACK request while in"
2524 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002525 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2526 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002527 }
2528 /*
2529 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2530 * call from inside iscsi_send_recovery_datain_or_r2t().
2531 */
2532 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2533 case 0:
2534 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002535 hdr->itt,
2536 be32_to_cpu(hdr->ttt),
2537 be32_to_cpu(hdr->begrun),
2538 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002539 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002540 return iscsit_handle_status_snack(conn, hdr->itt,
2541 be32_to_cpu(hdr->ttt),
2542 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002543 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002544 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2545 be32_to_cpu(hdr->begrun),
2546 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002547 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2548 /* FIXME: Support R-Data SNACK */
2549 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002550 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2551 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002552 default:
2553 pr_err("Unknown SNACK type 0x%02x, protocol"
2554 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002555 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2556 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002557 }
2558
2559 return 0;
2560}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302561EXPORT_SYMBOL(iscsit_handle_snack);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002562
2563static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2564{
2565 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2566 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2567 wait_for_completion_interruptible_timeout(
2568 &conn->rx_half_close_comp,
2569 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2570 }
2571}
2572
2573static int iscsit_handle_immediate_data(
2574 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002575 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002576 u32 length)
2577{
2578 int iov_ret, rx_got = 0, rx_size = 0;
2579 u32 checksum, iov_count = 0, padding = 0;
2580 struct iscsi_conn *conn = cmd->conn;
2581 struct kvec *iov;
2582
2583 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2584 if (iov_ret < 0)
2585 return IMMEDIATE_DATA_CANNOT_RECOVER;
2586
2587 rx_size = length;
2588 iov_count = iov_ret;
2589 iov = &cmd->iov_data[0];
2590
2591 padding = ((-length) & 3);
2592 if (padding != 0) {
2593 iov[iov_count].iov_base = cmd->pad_bytes;
2594 iov[iov_count++].iov_len = padding;
2595 rx_size += padding;
2596 }
2597
2598 if (conn->conn_ops->DataDigest) {
2599 iov[iov_count].iov_base = &checksum;
2600 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2601 rx_size += ISCSI_CRC_LEN;
2602 }
2603
2604 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2605
2606 iscsit_unmap_iovec(cmd);
2607
2608 if (rx_got != rx_size) {
2609 iscsit_rx_thread_wait_for_tcp(conn);
2610 return IMMEDIATE_DATA_CANNOT_RECOVER;
2611 }
2612
2613 if (conn->conn_ops->DataDigest) {
2614 u32 data_crc;
2615
Herbert Xu69110e32016-01-24 21:19:52 +08002616 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002617 cmd->write_data_done, length, padding,
2618 cmd->pad_bytes);
2619
2620 if (checksum != data_crc) {
2621 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2622 " does not match computed 0x%08x\n", checksum,
2623 data_crc);
2624
2625 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2626 pr_err("Unable to recover from"
2627 " Immediate Data digest failure while"
2628 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002629 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002630 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002631 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002632 return IMMEDIATE_DATA_CANNOT_RECOVER;
2633 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002634 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002635 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002636 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002637 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2638 }
2639 } else {
2640 pr_debug("Got CRC32C DataDigest 0x%08x for"
2641 " %u bytes of Immediate Data\n", checksum,
2642 length);
2643 }
2644 }
2645
2646 cmd->write_data_done += length;
2647
Andy Groverebf1d952012-04-03 15:51:24 -07002648 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002649 spin_lock_bh(&cmd->istate_lock);
2650 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2651 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2652 spin_unlock_bh(&cmd->istate_lock);
2653 }
2654
2655 return IMMEDIATE_DATA_NORMAL_OPERATION;
2656}
2657
2658/*
2659 * Called with sess->conn_lock held.
2660 */
2661/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2662 with active network interface */
2663static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2664{
2665 struct iscsi_cmd *cmd;
2666 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002667 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002668
2669 /*
2670 * Only send a Asynchronous Message on connections whos network
2671 * interface is still functional.
2672 */
2673 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2674 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2675 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002676 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002677 break;
2678 }
2679 }
2680
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002681 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002682 return;
2683
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002684 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002685 if (!cmd) {
2686 iscsit_dec_conn_usage_count(conn_p);
2687 return;
2688 }
2689
2690 cmd->logout_cid = conn->cid;
2691 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2692 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2693
2694 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002695 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002696 spin_unlock_bh(&conn_p->cmd_lock);
2697
2698 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2699 iscsit_dec_conn_usage_count(conn_p);
2700}
2701
2702static int iscsit_send_conn_drop_async_message(
2703 struct iscsi_cmd *cmd,
2704 struct iscsi_conn *conn)
2705{
2706 struct iscsi_async *hdr;
2707
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002708 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2709
2710 hdr = (struct iscsi_async *) cmd->pdu;
2711 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2712 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002713 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002714 cmd->targ_xfer_tag = 0xFFFFFFFF;
2715 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2716 cmd->stat_sn = conn->stat_sn++;
2717 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2718 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002719 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002720 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2721 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2722 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2723 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2724
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002725 pr_debug("Sending Connection Dropped Async Message StatSN:"
2726 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2727 cmd->logout_cid, conn->cid);
Varun Prakash2854bb22016-04-20 00:00:08 +05302728
2729 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002730}
2731
Andy Grover6f3c0e62012-04-03 15:51:09 -07002732static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2733{
2734 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2735 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2736 wait_for_completion_interruptible_timeout(
2737 &conn->tx_half_close_comp,
2738 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2739 }
2740}
2741
Varun Prakashd2faaef2016-04-20 00:00:19 +05302742void
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002743iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2744 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2745 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002746{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002747 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2748 hdr->flags = datain->flags;
2749 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2750 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2751 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2752 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2753 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2754 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2755 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2756 }
2757 }
2758 hton24(hdr->dlength, datain->length);
2759 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2760 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2761 (struct scsi_lun *)&hdr->lun);
2762 else
2763 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2764
2765 hdr->itt = cmd->init_task_tag;
2766
2767 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2768 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2769 else
2770 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2771 if (set_statsn)
2772 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2773 else
2774 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2775
2776 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002777 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002778 hdr->datasn = cpu_to_be32(datain->data_sn);
2779 hdr->offset = cpu_to_be32(datain->offset);
2780
2781 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2782 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2783 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2784 ntohl(hdr->offset), datain->length, conn->cid);
2785}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302786EXPORT_SYMBOL(iscsit_build_datain_pdu);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002787
2788static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2789{
2790 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002791 struct iscsi_datain datain;
2792 struct iscsi_datain_req *dr;
Varun Prakash2854bb22016-04-20 00:00:08 +05302793 int eodr = 0, ret;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002794 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002795
2796 memset(&datain, 0, sizeof(struct iscsi_datain));
2797 dr = iscsit_get_datain_values(cmd, &datain);
2798 if (!dr) {
2799 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2800 cmd->init_task_tag);
2801 return -1;
2802 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002803 /*
2804 * Be paranoid and double check the logic for now.
2805 */
Andy Groverebf1d952012-04-03 15:51:24 -07002806 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002807 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2808 " datain.length: %u exceeds cmd->data_length: %u\n",
2809 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002810 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002811 return -1;
2812 }
2813
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002814 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002815 /*
2816 * Special case for successfully execution w/ both DATAIN
2817 * and Sense Data.
2818 */
2819 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2820 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2821 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2822 else {
2823 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2824 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2825 iscsit_increment_maxcmdsn(cmd, conn->sess);
2826 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002827 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002828 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002829 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2830 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002831 }
2832
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002833 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002834
Varun Prakash2854bb22016-04-20 00:00:08 +05302835 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2836 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07002837 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002838
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002839 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002840 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002841 2 : 1;
2842 iscsit_free_datain_req(cmd, dr);
2843 }
2844
Andy Grover6f3c0e62012-04-03 15:51:09 -07002845 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002846}
2847
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002848int
2849iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2850 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002851{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002852 struct iscsi_conn *logout_conn = NULL;
2853 struct iscsi_conn_recovery *cr = NULL;
2854 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002855 /*
2856 * The actual shutting down of Sessions and/or Connections
2857 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2858 * is done in scsi_logout_post_handler().
2859 */
2860 switch (cmd->logout_reason) {
2861 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2862 pr_debug("iSCSI session logout successful, setting"
2863 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2864 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2865 break;
2866 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2867 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2868 break;
2869 /*
2870 * For CLOSECONNECTION logout requests carrying
2871 * a matching logout CID -> local CID, the reference
2872 * for the local CID will have been incremented in
2873 * iscsi_logout_closeconnection().
2874 *
2875 * For CLOSECONNECTION logout requests carrying
2876 * a different CID than the connection it arrived
2877 * on, the connection responding to cmd->logout_cid
2878 * is stopped in iscsit_logout_post_handler_diffcid().
2879 */
2880
2881 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2882 " successful.\n", cmd->logout_cid, conn->cid);
2883 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2884 break;
2885 case ISCSI_LOGOUT_REASON_RECOVERY:
2886 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2887 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2888 break;
2889 /*
2890 * If the connection is still active from our point of view
2891 * force connection recovery to occur.
2892 */
2893 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2894 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002895 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002896 iscsit_connection_reinstatement_rcfr(logout_conn);
2897 iscsit_dec_conn_usage_count(logout_conn);
2898 }
2899
2900 cr = iscsit_get_inactive_connection_recovery_entry(
2901 conn->sess, cmd->logout_cid);
2902 if (!cr) {
2903 pr_err("Unable to locate CID: %hu for"
2904 " REMOVECONNFORRECOVERY Logout Request.\n",
2905 cmd->logout_cid);
2906 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2907 break;
2908 }
2909
2910 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2911
2912 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2913 " for recovery for CID: %hu on CID: %hu successful.\n",
2914 cmd->logout_cid, conn->cid);
2915 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2916 break;
2917 default:
2918 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2919 cmd->logout_reason);
2920 return -1;
2921 }
2922
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002923 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2924 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2925 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002926 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002927 cmd->stat_sn = conn->stat_sn++;
2928 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2929
2930 iscsit_increment_maxcmdsn(cmd, conn->sess);
2931 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002932 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002933
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002934 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2935 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2936 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2937 cmd->logout_cid, conn->cid);
2938
2939 return 0;
2940}
2941EXPORT_SYMBOL(iscsit_build_logout_rsp);
2942
2943static int
2944iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2945{
Varun Prakash2854bb22016-04-20 00:00:08 +05302946 int rc;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002947
2948 rc = iscsit_build_logout_rsp(cmd, conn,
2949 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2950 if (rc < 0)
2951 return rc;
2952
Varun Prakash2854bb22016-04-20 00:00:08 +05302953 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002954}
2955
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002956void
2957iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2958 struct iscsi_nopin *hdr, bool nopout_response)
2959{
2960 hdr->opcode = ISCSI_OP_NOOP_IN;
2961 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2962 hton24(hdr->dlength, cmd->buf_ptr_size);
2963 if (nopout_response)
2964 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2965 hdr->itt = cmd->init_task_tag;
2966 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2967 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2968 conn->stat_sn;
2969 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2970
2971 if (nopout_response)
2972 iscsit_increment_maxcmdsn(cmd, conn->sess);
2973
2974 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002975 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002976
2977 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2978 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
Colin Ian King3fc6a642016-09-02 15:30:34 +01002979 "Solicited" : "Unsolicited", cmd->init_task_tag,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002980 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2981}
2982EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2983
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002984/*
2985 * Unsolicited NOPIN, either requesting a response or not.
2986 */
2987static int iscsit_send_unsolicited_nopin(
2988 struct iscsi_cmd *cmd,
2989 struct iscsi_conn *conn,
2990 int want_response)
2991{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002992 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Varun Prakash2854bb22016-04-20 00:00:08 +05302993 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002994
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002995 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002996
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002997 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2998 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2999
Varun Prakash2854bb22016-04-20 00:00:08 +05303000 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3001 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003002 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003003
3004 spin_lock_bh(&cmd->istate_lock);
3005 cmd->i_state = want_response ?
3006 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3007 spin_unlock_bh(&cmd->istate_lock);
3008
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003009 return 0;
3010}
3011
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003012static int
3013iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003014{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003015 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003016
3017 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003018
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003019 /*
3020 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3021 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3022 */
Varun Prakash2854bb22016-04-20 00:00:08 +05303023 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003024
Varun Prakash2854bb22016-04-20 00:00:08 +05303025 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3026 cmd->buf_ptr,
3027 cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028}
3029
Andy Grover6f3c0e62012-04-03 15:51:09 -07003030static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003031 struct iscsi_cmd *cmd,
3032 struct iscsi_conn *conn)
3033{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003034 struct iscsi_r2t *r2t;
3035 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003036 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003037
3038 r2t = iscsit_get_r2t_from_list(cmd);
3039 if (!r2t)
3040 return -1;
3041
3042 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3043 memset(hdr, 0, ISCSI_HDR_LEN);
3044 hdr->opcode = ISCSI_OP_R2T;
3045 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3046 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3047 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003048 hdr->itt = cmd->init_task_tag;
Varun Prakash85672702016-04-20 00:00:13 +05303049 if (conn->conn_transport->iscsit_get_r2t_ttt)
3050 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3051 else
3052 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003053 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3054 hdr->statsn = cpu_to_be32(conn->stat_sn);
3055 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003056 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003057 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3058 hdr->data_offset = cpu_to_be32(r2t->offset);
3059 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3060
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003061 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3062 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3063 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3064 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3065 r2t->offset, r2t->xfer_len, conn->cid);
3066
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003067 spin_lock_bh(&cmd->r2t_lock);
3068 r2t->sent_r2t = 1;
3069 spin_unlock_bh(&cmd->r2t_lock);
3070
Varun Prakash2854bb22016-04-20 00:00:08 +05303071 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003072 if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003073 return ret;
3074 }
3075
3076 spin_lock_bh(&cmd->dataout_timeout_lock);
3077 iscsit_start_dataout_timer(cmd, conn);
3078 spin_unlock_bh(&cmd->dataout_timeout_lock);
3079
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003080 return 0;
3081}
3082
3083/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003084 * @recovery: If called from iscsi_task_reassign_complete_write() for
3085 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003086 */
3087int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003088 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003089 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003090 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003091{
3092 int first_r2t = 1;
3093 u32 offset = 0, xfer_len = 0;
3094
3095 spin_lock_bh(&cmd->r2t_lock);
3096 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3097 spin_unlock_bh(&cmd->r2t_lock);
3098 return 0;
3099 }
3100
Andy Grover8b1e1242012-04-03 15:51:12 -07003101 if (conn->sess->sess_ops->DataSequenceInOrder &&
3102 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003103 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003104
3105 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3106 if (conn->sess->sess_ops->DataSequenceInOrder) {
3107 offset = cmd->r2t_offset;
3108
Andy Grover8b1e1242012-04-03 15:51:12 -07003109 if (first_r2t && recovery) {
3110 int new_data_end = offset +
3111 conn->sess->sess_ops->MaxBurstLength -
3112 cmd->next_burst_len;
3113
Andy Groverebf1d952012-04-03 15:51:24 -07003114 if (new_data_end > cmd->se_cmd.data_length)
3115 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003116 else
3117 xfer_len =
3118 conn->sess->sess_ops->MaxBurstLength -
3119 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003120 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003121 int new_data_end = offset +
3122 conn->sess->sess_ops->MaxBurstLength;
3123
Andy Groverebf1d952012-04-03 15:51:24 -07003124 if (new_data_end > cmd->se_cmd.data_length)
3125 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003126 else
3127 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003128 }
3129 cmd->r2t_offset += xfer_len;
3130
Andy Groverebf1d952012-04-03 15:51:24 -07003131 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003132 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3133 } else {
3134 struct iscsi_seq *seq;
3135
3136 seq = iscsit_get_seq_holder_for_r2t(cmd);
3137 if (!seq) {
3138 spin_unlock_bh(&cmd->r2t_lock);
3139 return -1;
3140 }
3141
3142 offset = seq->offset;
3143 xfer_len = seq->xfer_len;
3144
3145 if (cmd->seq_send_order == cmd->seq_count)
3146 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3147 }
3148 cmd->outstanding_r2ts++;
3149 first_r2t = 0;
3150
3151 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3152 spin_unlock_bh(&cmd->r2t_lock);
3153 return -1;
3154 }
3155
3156 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3157 break;
3158 }
3159 spin_unlock_bh(&cmd->r2t_lock);
3160
3161 return 0;
3162}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303163EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003164
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003165void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3166 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003167{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003168 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003169 cmd->stat_sn = conn->stat_sn++;
3170
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003171 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003172
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003173 memset(hdr, 0, ISCSI_HDR_LEN);
3174 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3175 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3176 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3177 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003178 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003179 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3180 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003181 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003182 }
3183 hdr->response = cmd->iscsi_response;
3184 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003185 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003186 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3187
3188 iscsit_increment_maxcmdsn(cmd, conn->sess);
3189 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003190 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003191
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003192 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3193 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3194 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3195 cmd->se_cmd.scsi_status, conn->cid);
3196}
3197EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3198
3199static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3200{
3201 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003202 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
Varun Prakash2854bb22016-04-20 00:00:08 +05303203 void *data_buf = NULL;
3204 u32 padding = 0, data_buf_len = 0;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003205
3206 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3207
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003208 /*
3209 * Attach SENSE DATA payload to iSCSI Response PDU
3210 */
3211 if (cmd->se_cmd.sense_buffer &&
3212 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3213 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003214 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3215 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3216
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003217 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003218 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Varun Prakash2854bb22016-04-20 00:00:08 +05303219 data_buf = cmd->sense_buffer;
3220 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003221
3222 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003223 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003224 cmd->se_cmd.scsi_sense_length, 0, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003225 pr_debug("Adding %u bytes of padding to"
3226 " SENSE.\n", padding);
3227 }
3228
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003229 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3230 " Response PDU\n",
3231 cmd->se_cmd.scsi_sense_length);
3232 }
3233
Varun Prakash2854bb22016-04-20 00:00:08 +05303234 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3235 data_buf_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003236}
3237
3238static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3239{
3240 switch (se_tmr->response) {
3241 case TMR_FUNCTION_COMPLETE:
3242 return ISCSI_TMF_RSP_COMPLETE;
3243 case TMR_TASK_DOES_NOT_EXIST:
3244 return ISCSI_TMF_RSP_NO_TASK;
3245 case TMR_LUN_DOES_NOT_EXIST:
3246 return ISCSI_TMF_RSP_NO_LUN;
3247 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3248 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003249 case TMR_FUNCTION_REJECTED:
3250 default:
3251 return ISCSI_TMF_RSP_REJECTED;
3252 }
3253}
3254
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003255void
3256iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3257 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003258{
3259 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003260
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003261 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003262 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003263 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003264 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003265 cmd->stat_sn = conn->stat_sn++;
3266 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3267
3268 iscsit_increment_maxcmdsn(cmd, conn->sess);
3269 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003270 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003271
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003272 pr_debug("Built Task Management Response ITT: 0x%08x,"
3273 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3274 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3275}
3276EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3277
3278static int
3279iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3280{
3281 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003282
3283 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3284
Varun Prakash2854bb22016-04-20 00:00:08 +05303285 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003286}
3287
Andy Grover8b1e1242012-04-03 15:51:12 -07003288#define SENDTARGETS_BUF_LIMIT 32768U
3289
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003290static int
3291iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003292 enum iscsit_transport_type network_transport,
3293 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003294{
3295 char *payload = NULL;
3296 struct iscsi_conn *conn = cmd->conn;
3297 struct iscsi_portal_group *tpg;
3298 struct iscsi_tiqn *tiqn;
3299 struct iscsi_tpg_np *tpg_np;
3300 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003301 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003302 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003303 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
David Disseldorpa6415cd2015-08-01 00:10:12 -07003304 bool active;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003305
Sagi Grimbergbe7dcfb2015-01-26 12:49:06 +02003306 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003307 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003308
3309 payload = kzalloc(buffer_len, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02003310 if (!payload)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003311 return -ENOMEM;
Markus Elfringc46e22f2017-04-09 15:34:50 +02003312
Nicholas Bellinger66658892013-06-19 22:45:42 -07003313 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003314 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003315 * explicit case..
3316 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003317 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003318 text_ptr = strchr(text_in, '=');
3319 if (!text_ptr) {
3320 pr_err("Unable to locate '=' string in text_in:"
3321 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003322 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003323 return -EINVAL;
3324 }
3325 /*
3326 * Skip over '=' character..
3327 */
3328 text_ptr += 1;
3329 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003330
3331 spin_lock(&tiqn_lock);
3332 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003333 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003334 strcmp(tiqn->tiqn, text_ptr)) {
3335 continue;
3336 }
3337
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003338 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003339
3340 spin_lock(&tiqn->tiqn_tpg_lock);
3341 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3342
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003343 /* If demo_mode_discovery=0 and generate_node_acls=0
3344 * (demo mode dislabed) do not return
3345 * TargetName+TargetAddress unless a NodeACL exists.
3346 */
3347
3348 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3349 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
Nicholas Bellinger21aaa232016-01-07 22:09:27 -08003350 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003351 cmd->conn->sess->sess_ops->InitiatorName))) {
3352 continue;
3353 }
3354
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003355 spin_lock(&tpg->tpg_state_lock);
David Disseldorpa6415cd2015-08-01 00:10:12 -07003356 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003357 spin_unlock(&tpg->tpg_state_lock);
3358
David Disseldorpa6415cd2015-08-01 00:10:12 -07003359 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3360 continue;
3361
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003362 spin_lock(&tpg->tpg_np_lock);
3363 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3364 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003365 struct iscsi_np *np = tpg_np->tpg_np;
Andy Grover13a3cf02015-08-24 10:26:06 -07003366 struct sockaddr_storage *sockaddr;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003367
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003368 if (np->np_network_transport != network_transport)
3369 continue;
3370
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003371 if (!target_name_printed) {
3372 len = sprintf(buf, "TargetName=%s",
3373 tiqn->tiqn);
3374 len += 1;
3375
3376 if ((len + payload_len) > buffer_len) {
3377 spin_unlock(&tpg->tpg_np_lock);
3378 spin_unlock(&tiqn->tiqn_tpg_lock);
3379 end_of_buf = 1;
3380 goto eob;
3381 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003382
3383 if (skip_bytes && len <= skip_bytes) {
3384 skip_bytes -= len;
3385 } else {
3386 memcpy(payload + payload_len, buf, len);
3387 payload_len += len;
3388 target_name_printed = 1;
3389 if (len > skip_bytes)
3390 skip_bytes = 0;
3391 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003392 }
3393
Sagi Grimberg7bfca0c2018-01-25 13:56:46 +02003394 if (inet_addr_is_any((struct sockaddr *)&np->np_sockaddr))
Andy Grover69d75572015-08-24 10:26:04 -07003395 sockaddr = &conn->local_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003396 else
Andy Grover69d75572015-08-24 10:26:04 -07003397 sockaddr = &np->np_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003398
Andy Grover69d75572015-08-24 10:26:04 -07003399 len = sprintf(buf, "TargetAddress="
3400 "%pISpc,%hu",
3401 sockaddr,
3402 tpg->tpgt);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003403 len += 1;
3404
3405 if ((len + payload_len) > buffer_len) {
3406 spin_unlock(&tpg->tpg_np_lock);
3407 spin_unlock(&tiqn->tiqn_tpg_lock);
3408 end_of_buf = 1;
3409 goto eob;
3410 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003411
3412 if (skip_bytes && len <= skip_bytes) {
3413 skip_bytes -= len;
3414 } else {
3415 memcpy(payload + payload_len, buf, len);
3416 payload_len += len;
3417 if (len > skip_bytes)
3418 skip_bytes = 0;
3419 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003420 }
3421 spin_unlock(&tpg->tpg_np_lock);
3422 }
3423 spin_unlock(&tiqn->tiqn_tpg_lock);
3424eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003425 if (end_of_buf) {
3426 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003427 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003428 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003429
Andy Grover8060b8d2015-01-09 15:13:08 -08003430 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003431 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003432 }
3433 spin_unlock(&tiqn_lock);
3434
3435 cmd->buf_ptr = payload;
3436
3437 return payload_len;
3438}
3439
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003440int
3441iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003442 struct iscsi_text_rsp *hdr,
3443 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003444{
3445 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003446 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003447
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003448 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3449 cmd->read_data_done,
3450 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003451 if (text_length < 0)
3452 return text_length;
3453
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003454 if (completed) {
Varun Prakash310d40a2017-07-23 20:03:45 +05303455 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003456 } else {
Varun Prakash310d40a2017-07-23 20:03:45 +05303457 hdr->flags = ISCSI_FLAG_TEXT_CONTINUE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003458 cmd->read_data_done += text_length;
3459 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3460 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3461 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003462 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003463 padding = ((-text_length) & 3);
3464 hton24(hdr->dlength, text_length);
3465 hdr->itt = cmd->init_task_tag;
3466 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3467 cmd->stat_sn = conn->stat_sn++;
3468 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3469
3470 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003471 /*
3472 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3473 * correctly increment MaxCmdSN for each response answering a
3474 * non immediate text request with a valid CmdSN.
3475 */
3476 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003477 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003478 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003479
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003480 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3481 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3482 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3483 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3484 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003485
3486 return text_length + padding;
3487}
3488EXPORT_SYMBOL(iscsit_build_text_rsp);
3489
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003490static int iscsit_send_text_rsp(
3491 struct iscsi_cmd *cmd,
3492 struct iscsi_conn *conn)
3493{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003494 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Varun Prakash2854bb22016-04-20 00:00:08 +05303495 int text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003496
Varun Prakash864e5042016-04-20 00:00:15 +05303497 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3498 conn->conn_transport->transport_type);
Varun Prakash2854bb22016-04-20 00:00:08 +05303499 if (text_length < 0)
3500 return text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003501
Varun Prakash2854bb22016-04-20 00:00:08 +05303502 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3503 cmd->buf_ptr,
3504 text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003505}
3506
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003507void
3508iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3509 struct iscsi_reject *hdr)
3510{
3511 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003512 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003513 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3514 hton24(hdr->dlength, ISCSI_HDR_LEN);
3515 hdr->ffffffff = cpu_to_be32(0xffffffff);
3516 cmd->stat_sn = conn->stat_sn++;
3517 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3518 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003519 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003520
3521}
3522EXPORT_SYMBOL(iscsit_build_reject);
3523
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003524static int iscsit_send_reject(
3525 struct iscsi_cmd *cmd,
3526 struct iscsi_conn *conn)
3527{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003528 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003529
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003530 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003531
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003532 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3533 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3534
Varun Prakash2854bb22016-04-20 00:00:08 +05303535 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3536 cmd->buf_ptr,
3537 ISCSI_HDR_LEN);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003538}
3539
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003540void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3541{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003542 int ord, cpu;
3543 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003544 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3545 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003546 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003547 * Here we use bitmap_id to determine which CPU that this
3548 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003549 * execute upon.
3550 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003551 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003552 for_each_online_cpu(cpu) {
3553 if (ord-- == 0) {
3554 cpumask_set_cpu(cpu, conn->conn_cpumask);
3555 return;
3556 }
3557 }
3558 /*
3559 * This should never be reached..
3560 */
3561 dump_stack();
3562 cpumask_setall(conn->conn_cpumask);
3563}
3564
Varun Prakashd2faaef2016-04-20 00:00:19 +05303565int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003566iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003567{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003568 int ret;
3569
3570 switch (state) {
3571 case ISTATE_SEND_R2T:
3572 ret = iscsit_send_r2t(cmd, conn);
3573 if (ret < 0)
3574 goto err;
3575 break;
3576 case ISTATE_REMOVE:
3577 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003578 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003579 spin_unlock_bh(&conn->cmd_lock);
3580
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003581 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003582 break;
3583 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3584 iscsit_mod_nopin_response_timer(conn);
3585 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3586 if (ret < 0)
3587 goto err;
3588 break;
3589 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3590 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3591 if (ret < 0)
3592 goto err;
3593 break;
3594 default:
3595 pr_err("Unknown Opcode: 0x%02x ITT:"
3596 " 0x%08x, i_state: %d on CID: %hu\n",
3597 cmd->iscsi_opcode, cmd->init_task_tag, state,
3598 conn->cid);
3599 goto err;
3600 }
3601
3602 return 0;
3603
3604err:
3605 return -1;
3606}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303607EXPORT_SYMBOL(iscsit_immediate_queue);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003608
3609static int
3610iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3611{
3612 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003613 struct iscsi_queue_req *qr;
3614 struct iscsi_cmd *cmd;
3615 u8 state;
3616 int ret;
3617
3618 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3619 atomic_set(&conn->check_immediate_queue, 0);
3620 cmd = qr->cmd;
3621 state = qr->state;
3622 kmem_cache_free(lio_qr_cache, qr);
3623
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003624 ret = t->iscsit_immediate_queue(conn, cmd, state);
3625 if (ret < 0)
3626 return ret;
3627 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003628
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003629 return 0;
3630}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003631
Varun Prakashd2faaef2016-04-20 00:00:19 +05303632int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003633iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3634{
3635 int ret;
3636
3637check_rsp_state:
3638 switch (state) {
3639 case ISTATE_SEND_DATAIN:
3640 ret = iscsit_send_datain(cmd, conn);
3641 if (ret < 0)
3642 goto err;
3643 else if (!ret)
3644 /* more drs */
3645 goto check_rsp_state;
3646 else if (ret == 1) {
3647 /* all done */
3648 spin_lock_bh(&cmd->istate_lock);
3649 cmd->i_state = ISTATE_SENT_STATUS;
3650 spin_unlock_bh(&cmd->istate_lock);
3651
3652 if (atomic_read(&conn->check_immediate_queue))
3653 return 1;
3654
3655 return 0;
3656 } else if (ret == 2) {
3657 /* Still must send status,
3658 SCF_TRANSPORT_TASK_SENSE was set */
3659 spin_lock_bh(&cmd->istate_lock);
3660 cmd->i_state = ISTATE_SEND_STATUS;
3661 spin_unlock_bh(&cmd->istate_lock);
3662 state = ISTATE_SEND_STATUS;
3663 goto check_rsp_state;
3664 }
3665
3666 break;
3667 case ISTATE_SEND_STATUS:
3668 case ISTATE_SEND_STATUS_RECOVERY:
3669 ret = iscsit_send_response(cmd, conn);
3670 break;
3671 case ISTATE_SEND_LOGOUTRSP:
3672 ret = iscsit_send_logout(cmd, conn);
3673 break;
3674 case ISTATE_SEND_ASYNCMSG:
3675 ret = iscsit_send_conn_drop_async_message(
3676 cmd, conn);
3677 break;
3678 case ISTATE_SEND_NOPIN:
3679 ret = iscsit_send_nopin(cmd, conn);
3680 break;
3681 case ISTATE_SEND_REJECT:
3682 ret = iscsit_send_reject(cmd, conn);
3683 break;
3684 case ISTATE_SEND_TASKMGTRSP:
3685 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3686 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003687 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003688 ret = iscsit_tmr_post_handler(cmd, conn);
3689 if (ret != 0)
3690 iscsit_fall_back_to_erl0(conn->sess);
3691 break;
3692 case ISTATE_SEND_TEXTRSP:
3693 ret = iscsit_send_text_rsp(cmd, conn);
3694 break;
3695 default:
3696 pr_err("Unknown Opcode: 0x%02x ITT:"
3697 " 0x%08x, i_state: %d on CID: %hu\n",
3698 cmd->iscsi_opcode, cmd->init_task_tag,
3699 state, conn->cid);
3700 goto err;
3701 }
3702 if (ret < 0)
3703 goto err;
3704
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003705 switch (state) {
3706 case ISTATE_SEND_LOGOUTRSP:
3707 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003708 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003709 /* fall through */
3710 case ISTATE_SEND_STATUS:
3711 case ISTATE_SEND_ASYNCMSG:
3712 case ISTATE_SEND_NOPIN:
3713 case ISTATE_SEND_STATUS_RECOVERY:
3714 case ISTATE_SEND_TEXTRSP:
3715 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003716 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003717 spin_lock_bh(&cmd->istate_lock);
3718 cmd->i_state = ISTATE_SENT_STATUS;
3719 spin_unlock_bh(&cmd->istate_lock);
3720 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003721 default:
3722 pr_err("Unknown Opcode: 0x%02x ITT:"
3723 " 0x%08x, i_state: %d on CID: %hu\n",
3724 cmd->iscsi_opcode, cmd->init_task_tag,
3725 cmd->i_state, conn->cid);
3726 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003727 }
3728
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003729 if (atomic_read(&conn->check_immediate_queue))
3730 return 1;
3731
Andy Grover6f3c0e62012-04-03 15:51:09 -07003732 return 0;
3733
3734err:
3735 return -1;
3736}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303737EXPORT_SYMBOL(iscsit_response_queue);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003738
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003739static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003740{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003741 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003742 struct iscsi_queue_req *qr;
3743 struct iscsi_cmd *cmd;
3744 u8 state;
3745 int ret;
3746
3747 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3748 cmd = qr->cmd;
3749 state = qr->state;
3750 kmem_cache_free(lio_qr_cache, qr);
3751
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003752 ret = t->iscsit_response_queue(conn, cmd, state);
3753 if (ret == 1 || ret < 0)
3754 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003755 }
3756
3757 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003758}
3759
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003760int iscsi_target_tx_thread(void *arg)
3761{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003762 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003763 struct iscsi_conn *conn = arg;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003764 bool conn_freed = false;
3765
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003766 /*
3767 * Allow ourselves to be interrupted by SIGINT so that a
3768 * connection recovery / failure event can be triggered externally.
3769 */
3770 allow_signal(SIGINT);
3771
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003772 while (!kthread_should_stop()) {
3773 /*
3774 * Ensure that both TX and RX per connection kthreads
3775 * are scheduled to run on the same CPU.
3776 */
3777 iscsit_thread_check_cpumask(conn, current, 1);
3778
Roland Dreierd5627ac2012-10-31 09:16:46 -07003779 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003780 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003781
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003782 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003783 goto transport_err;
3784
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003785get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003786 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003787 if (ret < 0)
3788 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003789
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003790 ret = iscsit_handle_response_queue(conn);
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003791 if (ret == 1) {
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003792 goto get_immediate;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003793 } else if (ret == -ECONNRESET) {
3794 conn_freed = true;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003795 goto out;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003796 } else if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003797 goto transport_err;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003798 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003799 }
3800
3801transport_err:
Nicholas Bellingere5419862015-07-22 23:14:19 -07003802 /*
3803 * Avoid the normal connection failure code-path if this connection
3804 * is still within LOGIN mode, and iscsi_np process context is
3805 * responsible for cleaning up the early connection failure.
3806 */
3807 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003808 iscsit_take_action_for_connection_exit(conn, &conn_freed);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003809out:
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003810 if (!conn_freed) {
3811 while (!kthread_should_stop()) {
3812 msleep(100);
3813 }
3814 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003815 return 0;
3816}
3817
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003818static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3819{
3820 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3821 struct iscsi_cmd *cmd;
3822 int ret = 0;
3823
3824 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3825 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003826 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003827 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003828 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003829
3830 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3831 break;
3832 case ISCSI_OP_SCSI_DATA_OUT:
3833 ret = iscsit_handle_data_out(conn, buf);
3834 break;
3835 case ISCSI_OP_NOOP_OUT:
3836 cmd = NULL;
3837 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003838 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003839 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003840 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003841 }
3842 ret = iscsit_handle_nop_out(conn, cmd, buf);
3843 break;
3844 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003845 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003846 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003847 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003848
3849 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3850 break;
3851 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003852 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3853 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3854 if (!cmd)
3855 goto reject;
3856 } else {
3857 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3858 if (!cmd)
3859 goto reject;
3860 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003861
3862 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003863 break;
3864 case ISCSI_OP_LOGOUT:
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_logout_cmd(conn, cmd, buf);
3870 if (ret > 0)
3871 wait_for_completion_timeout(&conn->conn_logout_comp,
3872 SECONDS_FOR_LOGOUT_COMP * HZ);
3873 break;
3874 case ISCSI_OP_SNACK:
3875 ret = iscsit_handle_snack(conn, buf);
3876 break;
3877 default:
3878 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3879 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3880 pr_err("Cannot recover from unknown"
3881 " opcode while ERL=0, closing iSCSI connection.\n");
3882 return -1;
3883 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02003884 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3885 " closing iSCSI connection.\n");
3886 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003887 break;
3888 }
3889
3890 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003891reject:
3892 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003893}
3894
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08003895static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3896{
3897 bool ret;
3898
3899 spin_lock_bh(&conn->state_lock);
3900 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3901 spin_unlock_bh(&conn->state_lock);
3902
3903 return ret;
3904}
3905
Varun Prakashe8205cc2016-04-20 00:00:11 +05303906static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003907{
Varun Prakashe8205cc2016-04-20 00:00:11 +05303908 int ret;
Laura Abbott679fcae2018-09-04 11:47:40 -07003909 u8 *buffer, opcode;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003910 u32 checksum = 0, digest = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003911 struct kvec iov;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003912
Laura Abbott679fcae2018-09-04 11:47:40 -07003913 buffer = kcalloc(ISCSI_HDR_LEN, sizeof(*buffer), GFP_KERNEL);
3914 if (!buffer)
3915 return;
3916
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003917 while (!kthread_should_stop()) {
3918 /*
3919 * Ensure that both TX and RX per connection kthreads
3920 * are scheduled to run on the same CPU.
3921 */
3922 iscsit_thread_check_cpumask(conn, current, 0);
3923
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003924 memset(&iov, 0, sizeof(struct kvec));
3925
3926 iov.iov_base = buffer;
3927 iov.iov_len = ISCSI_HDR_LEN;
3928
3929 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3930 if (ret != ISCSI_HDR_LEN) {
3931 iscsit_rx_thread_wait_for_tcp(conn);
Laura Abbott679fcae2018-09-04 11:47:40 -07003932 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003933 }
3934
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003935 if (conn->conn_ops->HeaderDigest) {
3936 iov.iov_base = &digest;
3937 iov.iov_len = ISCSI_CRC_LEN;
3938
3939 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3940 if (ret != ISCSI_CRC_LEN) {
3941 iscsit_rx_thread_wait_for_tcp(conn);
Laura Abbott679fcae2018-09-04 11:47:40 -07003942 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003943 }
3944
Bart Van Asschee1dfb212017-10-31 11:03:16 -07003945 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer,
3946 ISCSI_HDR_LEN, 0, NULL,
3947 &checksum);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003948
3949 if (digest != checksum) {
3950 pr_err("HeaderDigest CRC32C failed,"
3951 " received 0x%08x, computed 0x%08x\n",
3952 digest, checksum);
3953 /*
3954 * Set the PDU to 0xff so it will intentionally
3955 * hit default in the switch below.
3956 */
3957 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003958 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003959 } else {
3960 pr_debug("Got HeaderDigest CRC32C"
3961 " 0x%08x\n", checksum);
3962 }
3963 }
3964
3965 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
Laura Abbott679fcae2018-09-04 11:47:40 -07003966 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003967
3968 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3969
3970 if (conn->sess->sess_ops->SessionType &&
3971 ((!(opcode & ISCSI_OP_TEXT)) ||
3972 (!(opcode & ISCSI_OP_LOGOUT)))) {
3973 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3974 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07003975 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
3976 buffer);
Laura Abbott679fcae2018-09-04 11:47:40 -07003977 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003978 }
3979
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003980 ret = iscsi_target_rx_opcode(conn, buffer);
3981 if (ret < 0)
Laura Abbott679fcae2018-09-04 11:47:40 -07003982 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003983 }
Laura Abbott679fcae2018-09-04 11:47:40 -07003984
3985 kfree(buffer);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303986}
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003987
Varun Prakashe8205cc2016-04-20 00:00:11 +05303988int iscsi_target_rx_thread(void *arg)
3989{
3990 int rc;
3991 struct iscsi_conn *conn = arg;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003992 bool conn_freed = false;
Varun Prakashe8205cc2016-04-20 00:00:11 +05303993
3994 /*
3995 * Allow ourselves to be interrupted by SIGINT so that a
3996 * connection recovery / failure event can be triggered externally.
3997 */
3998 allow_signal(SIGINT);
3999 /*
4000 * Wait for iscsi_post_login_handler() to complete before allowing
4001 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4002 */
4003 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4004 if (rc < 0 || iscsi_target_check_conn_state(conn))
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004005 goto out;
Varun Prakashe8205cc2016-04-20 00:00:11 +05304006
4007 if (!conn->conn_transport->iscsit_get_rx_pdu)
4008 return 0;
4009
4010 conn->conn_transport->iscsit_get_rx_pdu(conn);
4011
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004012 if (!signal_pending(current))
4013 atomic_set(&conn->transport_failed, 1);
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004014 iscsit_take_action_for_connection_exit(conn, &conn_freed);
4015
4016out:
4017 if (!conn_freed) {
4018 while (!kthread_should_stop()) {
4019 msleep(100);
4020 }
4021 }
4022
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004023 return 0;
4024}
4025
4026static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4027{
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004028 LIST_HEAD(tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004029 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4030 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004031 /*
4032 * We expect this function to only ever be called from either RX or TX
4033 * thread context via iscsit_close_connection() once the other context
4034 * has been reset -> returned sleeping pre-handler state.
4035 */
4036 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004037 list_splice_init(&conn->conn_cmd_list, &tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004038
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004039 list_for_each_entry(cmd, &tmp_list, i_conn_node) {
4040 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004041
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004042 if (se_cmd->se_tfo != NULL) {
4043 spin_lock(&se_cmd->t_state_lock);
4044 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4045 spin_unlock(&se_cmd->t_state_lock);
4046 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004047 }
4048 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004049
4050 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4051 list_del_init(&cmd->i_conn_node);
4052
4053 iscsit_increment_maxcmdsn(cmd, sess);
4054 iscsit_free_cmd(cmd, true);
4055
4056 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004057}
4058
4059static void iscsit_stop_timers_for_cmds(
4060 struct iscsi_conn *conn)
4061{
4062 struct iscsi_cmd *cmd;
4063
4064 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004065 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004066 if (cmd->data_direction == DMA_TO_DEVICE)
4067 iscsit_stop_dataout_timer(cmd);
4068 }
4069 spin_unlock_bh(&conn->cmd_lock);
4070}
4071
4072int iscsit_close_connection(
4073 struct iscsi_conn *conn)
4074{
4075 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4076 struct iscsi_session *sess = conn->sess;
4077
4078 pr_debug("Closing iSCSI connection CID %hu on SID:"
4079 " %u\n", conn->cid, sess->sid);
4080 /*
Varun Prakashb4869ee2016-04-20 00:00:18 +05304081 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4082 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4083 * sleeping and the logout response never got sent because the
4084 * connection failed.
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004085 *
4086 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4087 * to signal logout response TX interrupt completion. Go ahead and skip
4088 * this for iser since isert_rx_opcode() does not wait on logout failure,
4089 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004090 */
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07004091 if (!conn->conn_transport->rdma_shutdown)
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004092 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004093
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004094 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4095 if (conn->tx_thread &&
4096 cmpxchg(&conn->tx_thread_active, true, false)) {
4097 send_sig(SIGINT, conn->tx_thread, 1);
4098 kthread_stop(conn->tx_thread);
4099 }
4100 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4101 if (conn->rx_thread &&
4102 cmpxchg(&conn->rx_thread_active, true, false)) {
4103 send_sig(SIGINT, conn->rx_thread, 1);
4104 kthread_stop(conn->rx_thread);
4105 }
4106 }
4107
4108 spin_lock(&iscsit_global->ts_bitmap_lock);
4109 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4110 get_order(1));
4111 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004112
4113 iscsit_stop_timers_for_cmds(conn);
4114 iscsit_stop_nopin_response_timer(conn);
4115 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004116
4117 if (conn->conn_transport->iscsit_wait_conn)
4118 conn->conn_transport->iscsit_wait_conn(conn);
4119
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004120 /*
4121 * During Connection recovery drop unacknowledged out of order
4122 * commands for this connection, and prepare the other commands
Bart Van Assche53c561d2016-12-23 14:40:24 +01004123 * for reallegiance.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004124 *
4125 * During normal operation clear the out of order commands (but
4126 * do not free the struct iscsi_ooo_cmdsn's) and release all
4127 * struct iscsi_cmds.
4128 */
4129 if (atomic_read(&conn->connection_recovery)) {
4130 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
Bart Van Assche53c561d2016-12-23 14:40:24 +01004131 iscsit_prepare_cmds_for_reallegiance(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004132 } else {
4133 iscsit_clear_ooo_cmdsns_for_conn(conn);
4134 iscsit_release_commands_from_conn(conn);
4135 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004136 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004137
4138 /*
4139 * Handle decrementing session or connection usage count if
4140 * a logout response was not able to be sent because the
4141 * connection failed. Fall back to Session Recovery here.
4142 */
4143 if (atomic_read(&conn->conn_logout_remove)) {
4144 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4145 iscsit_dec_conn_usage_count(conn);
4146 iscsit_dec_session_usage_count(sess);
4147 }
4148 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4149 iscsit_dec_conn_usage_count(conn);
4150
4151 atomic_set(&conn->conn_logout_remove, 0);
4152 atomic_set(&sess->session_reinstatement, 0);
4153 atomic_set(&sess->session_fall_back_to_erl0, 1);
4154 }
4155
4156 spin_lock_bh(&sess->conn_lock);
4157 list_del(&conn->conn_list);
4158
4159 /*
4160 * Attempt to let the Initiator know this connection failed by
4161 * sending an Connection Dropped Async Message on another
4162 * active connection.
4163 */
4164 if (atomic_read(&conn->connection_recovery))
4165 iscsit_build_conn_drop_async_message(conn);
4166
4167 spin_unlock_bh(&sess->conn_lock);
4168
4169 /*
4170 * If connection reinstatement is being performed on this connection,
4171 * up the connection reinstatement semaphore that is being blocked on
4172 * in iscsit_cause_connection_reinstatement().
4173 */
4174 spin_lock_bh(&conn->state_lock);
4175 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4176 spin_unlock_bh(&conn->state_lock);
4177 complete(&conn->conn_wait_comp);
4178 wait_for_completion(&conn->conn_post_wait_comp);
4179 spin_lock_bh(&conn->state_lock);
4180 }
4181
4182 /*
4183 * If connection reinstatement is being performed on this connection
4184 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4185 * connection wait rcfr semaphore that is being blocked on
4186 * an iscsit_connection_reinstatement_rcfr().
4187 */
4188 if (atomic_read(&conn->connection_wait_rcfr)) {
4189 spin_unlock_bh(&conn->state_lock);
4190 complete(&conn->conn_wait_rcfr_comp);
4191 wait_for_completion(&conn->conn_post_wait_comp);
4192 spin_lock_bh(&conn->state_lock);
4193 }
4194 atomic_set(&conn->connection_reinstatement, 1);
4195 spin_unlock_bh(&conn->state_lock);
4196
4197 /*
4198 * If any other processes are accessing this connection pointer we
4199 * must wait until they have completed.
4200 */
4201 iscsit_check_conn_usage_count(conn);
4202
Herbert Xu69110e32016-01-24 21:19:52 +08004203 ahash_request_free(conn->conn_tx_hash);
4204 if (conn->conn_rx_hash) {
4205 struct crypto_ahash *tfm;
4206
4207 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4208 ahash_request_free(conn->conn_rx_hash);
4209 crypto_free_ahash(tfm);
4210 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004211
Al Virobf6932f2012-07-21 08:55:18 +01004212 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004213 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004214
4215 if (conn->conn_transport->iscsit_free_conn)
4216 conn->conn_transport->iscsit_free_conn(conn);
4217
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004218 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4219 conn->conn_state = TARG_CONN_STATE_FREE;
Mike Christie05a86e72018-08-27 14:45:16 -05004220 iscsit_free_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004221
4222 spin_lock_bh(&sess->conn_lock);
4223 atomic_dec(&sess->nconn);
4224 pr_debug("Decremented iSCSI connection count to %hu from node:"
4225 " %s\n", atomic_read(&sess->nconn),
4226 sess->sess_ops->InitiatorName);
4227 /*
4228 * Make sure that if one connection fails in an non ERL=2 iSCSI
4229 * Session that they all fail.
4230 */
4231 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4232 !atomic_read(&sess->session_logout))
4233 atomic_set(&sess->session_fall_back_to_erl0, 1);
4234
4235 /*
4236 * If this was not the last connection in the session, and we are
4237 * performing session reinstatement or falling back to ERL=0, call
4238 * iscsit_stop_session() without sleeping to shutdown the other
4239 * active connections.
4240 */
4241 if (atomic_read(&sess->nconn)) {
4242 if (!atomic_read(&sess->session_reinstatement) &&
4243 !atomic_read(&sess->session_fall_back_to_erl0)) {
4244 spin_unlock_bh(&sess->conn_lock);
4245 return 0;
4246 }
4247 if (!atomic_read(&sess->session_stop_active)) {
4248 atomic_set(&sess->session_stop_active, 1);
4249 spin_unlock_bh(&sess->conn_lock);
4250 iscsit_stop_session(sess, 0, 0);
4251 return 0;
4252 }
4253 spin_unlock_bh(&sess->conn_lock);
4254 return 0;
4255 }
4256
4257 /*
4258 * If this was the last connection in the session and one of the
4259 * following is occurring:
4260 *
4261 * Session Reinstatement is not being performed, and are falling back
4262 * to ERL=0 call iscsit_close_session().
4263 *
4264 * Session Logout was requested. iscsit_close_session() will be called
4265 * elsewhere.
4266 *
4267 * Session Continuation is not being performed, start the Time2Retain
4268 * handler and check if sleep_on_sess_wait_sem is active.
4269 */
4270 if (!atomic_read(&sess->session_reinstatement) &&
4271 atomic_read(&sess->session_fall_back_to_erl0)) {
4272 spin_unlock_bh(&sess->conn_lock);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004273 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004274
4275 return 0;
4276 } else if (atomic_read(&sess->session_logout)) {
4277 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4278 sess->session_state = TARG_SESS_STATE_FREE;
4279 spin_unlock_bh(&sess->conn_lock);
4280
4281 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4282 complete(&sess->session_wait_comp);
4283
4284 return 0;
4285 } else {
4286 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4287 sess->session_state = TARG_SESS_STATE_FAILED;
4288
4289 if (!atomic_read(&sess->session_continuation)) {
4290 spin_unlock_bh(&sess->conn_lock);
4291 iscsit_start_time2retain_handler(sess);
4292 } else
4293 spin_unlock_bh(&sess->conn_lock);
4294
4295 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4296 complete(&sess->session_wait_comp);
4297
4298 return 0;
4299 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004300}
4301
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004302/*
4303 * If the iSCSI Session for the iSCSI Initiator Node exists,
4304 * forcefully shutdown the iSCSI NEXUS.
4305 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004306int iscsit_close_session(struct iscsi_session *sess)
4307{
Andy Grover60bfcf82013-10-09 11:05:58 -07004308 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004309 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4310
4311 if (atomic_read(&sess->nconn)) {
4312 pr_err("%d connection(s) still exist for iSCSI session"
4313 " to %s\n", atomic_read(&sess->nconn),
4314 sess->sess_ops->InitiatorName);
4315 BUG();
4316 }
4317
4318 spin_lock_bh(&se_tpg->session_lock);
4319 atomic_set(&sess->session_logout, 1);
4320 atomic_set(&sess->session_reinstatement, 1);
4321 iscsit_stop_time2retain_timer(sess);
4322 spin_unlock_bh(&se_tpg->session_lock);
4323
4324 /*
4325 * transport_deregister_session_configfs() will clear the
4326 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4327 * can be setting it again with __transport_register_session() in
4328 * iscsi_post_login_handler() again after the iscsit_stop_session()
4329 * completes in iscsi_np context.
4330 */
4331 transport_deregister_session_configfs(sess->se_sess);
4332
4333 /*
4334 * If any other processes are accessing this session pointer we must
4335 * wait until they have completed. If we are in an interrupt (the
4336 * time2retain handler) and contain and active session usage count we
4337 * restart the timer and exit.
4338 */
4339 if (!in_interrupt()) {
4340 if (iscsit_check_session_usage_count(sess) == 1)
4341 iscsit_stop_session(sess, 1, 1);
4342 } else {
4343 if (iscsit_check_session_usage_count(sess) == 2) {
4344 atomic_set(&sess->session_logout, 0);
4345 iscsit_start_time2retain_handler(sess);
4346 return 0;
4347 }
4348 }
4349
4350 transport_deregister_session(sess->se_sess);
4351
4352 if (sess->sess_ops->ErrorRecoveryLevel == 2)
Colin Ian King902ff862018-09-14 13:28:08 +01004353 iscsit_free_connection_recovery_entries(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004354
4355 iscsit_free_all_ooo_cmdsns(sess);
4356
4357 spin_lock_bh(&se_tpg->session_lock);
4358 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4359 sess->session_state = TARG_SESS_STATE_FREE;
4360 pr_debug("Released iSCSI session from node: %s\n",
4361 sess->sess_ops->InitiatorName);
4362 tpg->nsessions--;
4363 if (tpg->tpg_tiqn)
4364 tpg->tpg_tiqn->tiqn_nsessions--;
4365
4366 pr_debug("Decremented number of active iSCSI Sessions on"
4367 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4368
Matthew Wilcox31ff0ce2018-06-19 01:23:04 -04004369 ida_free(&sess_ida, sess->session_index);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004370 kfree(sess->sess_ops);
4371 sess->sess_ops = NULL;
4372 spin_unlock_bh(&se_tpg->session_lock);
4373
4374 kfree(sess);
4375 return 0;
4376}
4377
4378static void iscsit_logout_post_handler_closesession(
4379 struct iscsi_conn *conn)
4380{
4381 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004382 int sleep = 1;
4383 /*
4384 * Traditional iscsi/tcp will invoke this logic from TX thread
4385 * context during session logout, so clear tx_thread_active and
4386 * sleep if iscsit_close_connection() has not already occured.
4387 *
4388 * Since iser-target invokes this logic from it's own workqueue,
4389 * always sleep waiting for RX/TX thread shutdown to complete
4390 * within iscsit_close_connection().
4391 */
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004392 if (!conn->conn_transport->rdma_shutdown) {
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004393 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004394 if (!sleep)
4395 return;
4396 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004397
4398 atomic_set(&conn->conn_logout_remove, 0);
4399 complete(&conn->conn_logout_comp);
4400
4401 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004402 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004403 iscsit_dec_session_usage_count(sess);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004404 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004405}
4406
4407static void iscsit_logout_post_handler_samecid(
4408 struct iscsi_conn *conn)
4409{
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004410 int sleep = 1;
4411
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004412 if (!conn->conn_transport->rdma_shutdown) {
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004413 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004414 if (!sleep)
4415 return;
4416 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004417
4418 atomic_set(&conn->conn_logout_remove, 0);
4419 complete(&conn->conn_logout_comp);
4420
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004421 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004422 iscsit_dec_conn_usage_count(conn);
4423}
4424
4425static void iscsit_logout_post_handler_diffcid(
4426 struct iscsi_conn *conn,
4427 u16 cid)
4428{
4429 struct iscsi_conn *l_conn;
4430 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004431 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004432
4433 if (!sess)
4434 return;
4435
4436 spin_lock_bh(&sess->conn_lock);
4437 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4438 if (l_conn->cid == cid) {
4439 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004440 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004441 break;
4442 }
4443 }
4444 spin_unlock_bh(&sess->conn_lock);
4445
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004446 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004447 return;
4448
4449 if (l_conn->sock)
4450 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4451
4452 spin_lock_bh(&l_conn->state_lock);
4453 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4454 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4455 spin_unlock_bh(&l_conn->state_lock);
4456
4457 iscsit_cause_connection_reinstatement(l_conn, 1);
4458 iscsit_dec_conn_usage_count(l_conn);
4459}
4460
4461/*
4462 * Return of 0 causes the TX thread to restart.
4463 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004464int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004465 struct iscsi_cmd *cmd,
4466 struct iscsi_conn *conn)
4467{
4468 int ret = 0;
4469
4470 switch (cmd->logout_reason) {
4471 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4472 switch (cmd->logout_response) {
4473 case ISCSI_LOGOUT_SUCCESS:
4474 case ISCSI_LOGOUT_CLEANUP_FAILED:
4475 default:
4476 iscsit_logout_post_handler_closesession(conn);
4477 break;
4478 }
4479 ret = 0;
4480 break;
4481 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4482 if (conn->cid == cmd->logout_cid) {
4483 switch (cmd->logout_response) {
4484 case ISCSI_LOGOUT_SUCCESS:
4485 case ISCSI_LOGOUT_CLEANUP_FAILED:
4486 default:
4487 iscsit_logout_post_handler_samecid(conn);
4488 break;
4489 }
4490 ret = 0;
4491 } else {
4492 switch (cmd->logout_response) {
4493 case ISCSI_LOGOUT_SUCCESS:
4494 iscsit_logout_post_handler_diffcid(conn,
4495 cmd->logout_cid);
4496 break;
4497 case ISCSI_LOGOUT_CID_NOT_FOUND:
4498 case ISCSI_LOGOUT_CLEANUP_FAILED:
4499 default:
4500 break;
4501 }
4502 ret = 1;
4503 }
4504 break;
4505 case ISCSI_LOGOUT_REASON_RECOVERY:
4506 switch (cmd->logout_response) {
4507 case ISCSI_LOGOUT_SUCCESS:
4508 case ISCSI_LOGOUT_CID_NOT_FOUND:
4509 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4510 case ISCSI_LOGOUT_CLEANUP_FAILED:
4511 default:
4512 break;
4513 }
4514 ret = 1;
4515 break;
4516 default:
4517 break;
4518
4519 }
4520 return ret;
4521}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004522EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004523
4524void iscsit_fail_session(struct iscsi_session *sess)
4525{
4526 struct iscsi_conn *conn;
4527
4528 spin_lock_bh(&sess->conn_lock);
4529 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4530 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4531 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4532 }
4533 spin_unlock_bh(&sess->conn_lock);
4534
4535 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4536 sess->session_state = TARG_SESS_STATE_FAILED;
4537}
4538
4539int iscsit_free_session(struct iscsi_session *sess)
4540{
4541 u16 conn_count = atomic_read(&sess->nconn);
4542 struct iscsi_conn *conn, *conn_tmp = NULL;
4543 int is_last;
4544
4545 spin_lock_bh(&sess->conn_lock);
4546 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4547
4548 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4549 conn_list) {
4550 if (conn_count == 0)
4551 break;
4552
4553 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4554 is_last = 1;
4555 } else {
4556 iscsit_inc_conn_usage_count(conn_tmp);
4557 is_last = 0;
4558 }
4559 iscsit_inc_conn_usage_count(conn);
4560
4561 spin_unlock_bh(&sess->conn_lock);
4562 iscsit_cause_connection_reinstatement(conn, 1);
4563 spin_lock_bh(&sess->conn_lock);
4564
4565 iscsit_dec_conn_usage_count(conn);
4566 if (is_last == 0)
4567 iscsit_dec_conn_usage_count(conn_tmp);
4568
4569 conn_count--;
4570 }
4571
4572 if (atomic_read(&sess->nconn)) {
4573 spin_unlock_bh(&sess->conn_lock);
4574 wait_for_completion(&sess->session_wait_comp);
4575 } else
4576 spin_unlock_bh(&sess->conn_lock);
4577
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004578 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004579 return 0;
4580}
4581
4582void iscsit_stop_session(
4583 struct iscsi_session *sess,
4584 int session_sleep,
4585 int connection_sleep)
4586{
4587 u16 conn_count = atomic_read(&sess->nconn);
4588 struct iscsi_conn *conn, *conn_tmp = NULL;
4589 int is_last;
4590
4591 spin_lock_bh(&sess->conn_lock);
4592 if (session_sleep)
4593 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4594
4595 if (connection_sleep) {
4596 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4597 conn_list) {
4598 if (conn_count == 0)
4599 break;
4600
4601 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4602 is_last = 1;
4603 } else {
4604 iscsit_inc_conn_usage_count(conn_tmp);
4605 is_last = 0;
4606 }
4607 iscsit_inc_conn_usage_count(conn);
4608
4609 spin_unlock_bh(&sess->conn_lock);
4610 iscsit_cause_connection_reinstatement(conn, 1);
4611 spin_lock_bh(&sess->conn_lock);
4612
4613 iscsit_dec_conn_usage_count(conn);
4614 if (is_last == 0)
4615 iscsit_dec_conn_usage_count(conn_tmp);
4616 conn_count--;
4617 }
4618 } else {
4619 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4620 iscsit_cause_connection_reinstatement(conn, 0);
4621 }
4622
4623 if (session_sleep && atomic_read(&sess->nconn)) {
4624 spin_unlock_bh(&sess->conn_lock);
4625 wait_for_completion(&sess->session_wait_comp);
4626 } else
4627 spin_unlock_bh(&sess->conn_lock);
4628}
4629
4630int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4631{
4632 struct iscsi_session *sess;
4633 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4634 struct se_session *se_sess, *se_sess_tmp;
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004635 LIST_HEAD(free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004636 int session_count = 0;
4637
4638 spin_lock_bh(&se_tpg->session_lock);
4639 if (tpg->nsessions && !force) {
4640 spin_unlock_bh(&se_tpg->session_lock);
4641 return -1;
4642 }
4643
4644 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4645 sess_list) {
4646 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4647
4648 spin_lock(&sess->conn_lock);
4649 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4650 atomic_read(&sess->session_logout) ||
4651 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4652 spin_unlock(&sess->conn_lock);
4653 continue;
4654 }
4655 atomic_set(&sess->session_reinstatement, 1);
Nicholas Bellinger197b8062017-04-25 10:55:12 -07004656 atomic_set(&sess->session_fall_back_to_erl0, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004657 spin_unlock(&sess->conn_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004658
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004659 list_move_tail(&se_sess->sess_list, &free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004660 }
4661 spin_unlock_bh(&se_tpg->session_lock);
4662
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004663 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4664 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4665
4666 iscsit_free_session(sess);
4667 session_count++;
4668 }
4669
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004670 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4671 " Group: %hu\n", session_count, tpg->tpgt);
4672 return 0;
4673}
4674
4675MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4676MODULE_VERSION("4.1.x");
4677MODULE_AUTHOR("nab@Linux-iSCSI.org");
4678MODULE_LICENSE("GPL");
4679
4680module_init(iscsi_target_init_module);
4681module_exit(iscsi_target_cleanup_module);