blob: 317d0f3f7a14cb7bb1bc66a7c25883af94f98c92 [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) {
717 pr_err("nable to kmem_cache_create() for"
718 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800719 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000720 }
721
722 lio_dr_cache = kmem_cache_create("lio_dr_cache",
723 sizeof(struct iscsi_datain_req),
724 __alignof__(struct iscsi_datain_req), 0, NULL);
725 if (!lio_dr_cache) {
726 pr_err("Unable to kmem_cache_create() for"
727 " lio_dr_cache\n");
728 goto qr_out;
729 }
730
731 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
732 sizeof(struct iscsi_ooo_cmdsn),
733 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
734 if (!lio_ooo_cache) {
735 pr_err("Unable to kmem_cache_create() for"
736 " lio_ooo_cache\n");
737 goto dr_out;
738 }
739
740 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
741 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
742 0, NULL);
743 if (!lio_r2t_cache) {
744 pr_err("Unable to kmem_cache_create() for"
745 " lio_r2t_cache\n");
746 goto ooo_out;
747 }
748
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800749 iscsit_register_transport(&iscsi_target_transport);
750
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000751 if (iscsit_load_discovery_tpg() < 0)
752 goto r2t_out;
753
754 return ret;
755r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100756 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000757 kmem_cache_destroy(lio_r2t_cache);
758ooo_out:
759 kmem_cache_destroy(lio_ooo_cache);
760dr_out:
761 kmem_cache_destroy(lio_dr_cache);
762qr_out:
763 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800764bitmap_out:
765 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000766configfs_out:
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200767 /* XXX: this probably wants it to be it's own unwind step.. */
768 if (iscsit_global->discovery_tpg)
769 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
770 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000771out:
772 kfree(iscsit_global);
773 return -ENOMEM;
774}
775
776static void __exit iscsi_target_cleanup_module(void)
777{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000778 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800779 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000780 kmem_cache_destroy(lio_qr_cache);
781 kmem_cache_destroy(lio_dr_cache);
782 kmem_cache_destroy(lio_ooo_cache);
783 kmem_cache_destroy(lio_r2t_cache);
784
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200785 /*
786 * Shutdown discovery sessions and disable discovery TPG
787 */
788 if (iscsit_global->discovery_tpg)
789 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000790
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200791 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000792
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800793 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000794 kfree(iscsit_global);
795}
796
Varun Prakashd2faaef2016-04-20 00:00:19 +0530797int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700798 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000799 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700800 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000801{
802 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000803
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000804 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000805 if (!cmd)
806 return -1;
807
808 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700809 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000810
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100811 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000812 if (!cmd->buf_ptr) {
813 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700814 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000815 return -1;
816 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000817
818 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700819 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000820 spin_unlock_bh(&conn->cmd_lock);
821
822 cmd->i_state = ISTATE_SEND_REJECT;
823 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
824
Nicholas Bellingerba159912013-07-03 03:48:24 -0700825 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000826}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530827EXPORT_SYMBOL(iscsit_add_reject);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000828
Nicholas Bellingerba159912013-07-03 03:48:24 -0700829static int iscsit_add_reject_from_cmd(
830 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000831 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700832 bool add_to_conn,
833 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000834{
835 struct iscsi_conn *conn;
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 Asscheb0aede22019-11-13 14:05:08 -08001160 if (target_get_sess_cmd(&cmd->se_cmd, true) < 0)
1161 return iscsit_add_reject_cmd(cmd,
1162 ISCSI_REASON_WAITING_FOR_LOGOUT, buf);
Andy Grover065ca1e2012-04-03 15:51:23 -07001163
Christoph Hellwigde103c92012-11-06 12:24:09 -08001164 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1165 scsilun_to_int(&hdr->lun));
1166 if (cmd->sense_reason)
1167 goto attach_cmd;
1168
Bart Van Assche649ee052015-04-14 13:26:44 +02001169 /* only used for printks or comparing with ->ref_task_tag */
1170 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001171 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1172 if (cmd->sense_reason) {
1173 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001174 return iscsit_add_reject_cmd(cmd,
1175 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001176 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001177
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001178 goto attach_cmd;
1179 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001180
Christoph Hellwigde103c92012-11-06 12:24:09 -08001181 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001182 return iscsit_add_reject_cmd(cmd,
1183 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001184 }
1185
1186attach_cmd:
1187 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001188 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001189 spin_unlock_bh(&conn->cmd_lock);
1190 /*
1191 * Check if we need to delay processing because of ALUA
1192 * Active/NonOptimized primary access state..
1193 */
1194 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001195
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001196 return 0;
1197}
1198EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001199
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001200void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1201{
1202 iscsit_set_dataout_sequence_values(cmd);
1203
1204 spin_lock_bh(&cmd->dataout_timeout_lock);
1205 iscsit_start_dataout_timer(cmd, cmd->conn);
1206 spin_unlock_bh(&cmd->dataout_timeout_lock);
1207}
1208EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1209
1210int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1211 struct iscsi_scsi_req *hdr)
1212{
1213 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001214 /*
1215 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1216 * the Immediate Bit is not set, and no Immediate
1217 * Data is attached.
1218 *
1219 * A PDU/CmdSN carrying Immediate Data can only
1220 * be processed after the DataCRC has passed.
1221 * If the DataCRC fails, the CmdSN MUST NOT
1222 * be acknowledged. (See below)
1223 */
1224 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001225 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1226 (unsigned char *)hdr, hdr->cmdsn);
1227 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1228 return -1;
1229 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001230 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001231 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001232 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001233 }
1234
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001235 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001236
1237 /*
1238 * If no Immediate Data is attached, it's OK to return now.
1239 */
1240 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001241 if (!cmd->sense_reason && cmd->unsolicited_data)
1242 iscsit_set_unsoliticed_dataout(cmd);
1243 if (!cmd->sense_reason)
1244 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001245
Bart Van Asscheafc16602015-04-27 13:52:36 +02001246 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001247 return 0;
1248 }
1249
1250 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001251 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1252 * execution. These exceptions are processed in CmdSN order using
1253 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001254 */
Bart Van Assche8fa40112017-05-23 16:48:45 -07001255 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001256 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001257 /*
1258 * Call directly into transport_generic_new_cmd() to perform
1259 * the backend memory allocation.
1260 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001261 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001262 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001263 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001264
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001265 return 0;
1266}
1267EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1268
1269static int
1270iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1271 bool dump_payload)
1272{
1273 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1274 /*
1275 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1276 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001277 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001278 goto after_immediate_data;
Nicholas Bellingerabb85a92017-06-07 20:29:50 -07001279 /*
1280 * Check for underflow case where both EDTL and immediate data payload
1281 * exceeds what is presented by CDB's TRANSFER LENGTH, and what has
1282 * already been set in target_cmd_size_check() as se_cmd->data_length.
1283 *
1284 * For this special case, fail the command and dump the immediate data
1285 * payload.
1286 */
1287 if (cmd->first_burst_len > cmd->se_cmd.data_length) {
1288 cmd->sense_reason = TCM_INVALID_CDB_FIELD;
1289 goto after_immediate_data;
1290 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001291
1292 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1293 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001294after_immediate_data:
1295 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1296 /*
1297 * A PDU/CmdSN carrying Immediate Data passed
1298 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1299 * Immediate Bit is not set.
1300 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001301 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1302 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001303 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001304 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001305
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001306 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001307 int rc;
1308
1309 rc = iscsit_dump_data_payload(cmd->conn,
1310 cmd->first_burst_len, 1);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001311 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001312 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001313 } else if (cmd->unsolicited_data)
1314 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001315
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001316 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1317 /*
1318 * Immediate Data failed DataCRC and ERL>=1,
1319 * silently drop this PDU and let the initiator
1320 * plug the CmdSN gap.
1321 *
1322 * FIXME: Send Unsolicited NOPIN with reserved
1323 * TTT here to help the initiator figure out
1324 * the missing CmdSN, although they should be
1325 * intelligent enough to determine the missing
1326 * CmdSN and issue a retry to plug the sequence.
1327 */
1328 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001329 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001330 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1331 return -1;
1332
1333 return 0;
1334}
1335
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001336static int
1337iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1338 unsigned char *buf)
1339{
1340 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1341 int rc, immed_data;
1342 bool dump_payload = false;
1343
1344 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1345 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001346 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001347 /*
1348 * Allocation iovecs needed for struct socket operations for
1349 * traditional iSCSI block I/O.
1350 */
1351 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001352 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001353 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001354 }
1355 immed_data = cmd->immediate_data;
1356
1357 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1358 if (rc < 0)
1359 return rc;
1360 else if (rc > 0)
1361 dump_payload = true;
1362
1363 if (!immed_data)
1364 return 0;
1365
1366 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1367}
1368
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001369static u32 iscsit_do_crypto_hash_sg(
Herbert Xu69110e32016-01-24 21:19:52 +08001370 struct ahash_request *hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001371 struct iscsi_cmd *cmd,
1372 u32 data_offset,
1373 u32 data_length,
1374 u32 padding,
1375 u8 *pad_bytes)
1376{
1377 u32 data_crc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001378 struct scatterlist *sg;
1379 unsigned int page_off;
1380
Herbert Xu69110e32016-01-24 21:19:52 +08001381 crypto_ahash_init(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001382
1383 sg = cmd->first_data_sg;
1384 page_off = cmd->first_data_sg_off;
1385
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001386 while (data_length) {
Alexei Potashnikaa756792015-07-20 17:12:12 -07001387 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001388
Herbert Xu69110e32016-01-24 21:19:52 +08001389 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1390 crypto_ahash_update(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001391
1392 data_length -= cur_len;
1393 page_off = 0;
Alexei Potashnikaa756792015-07-20 17:12:12 -07001394 /* iscsit_map_iovec has already checked for invalid sg pointers */
1395 sg = sg_next(sg);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001396 }
1397
1398 if (padding) {
1399 struct scatterlist pad_sg;
1400
1401 sg_init_one(&pad_sg, pad_bytes, padding);
Herbert Xu69110e32016-01-24 21:19:52 +08001402 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1403 padding);
1404 crypto_ahash_finup(hash);
1405 } else {
1406 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1407 crypto_ahash_final(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001408 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001409
1410 return data_crc;
1411}
1412
Bart Van Asschee1dfb212017-10-31 11:03:16 -07001413static void iscsit_do_crypto_hash_buf(struct ahash_request *hash,
1414 const void *buf, u32 payload_length, u32 padding,
1415 const void *pad_bytes, void *data_crc)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001416{
Herbert Xu69110e32016-01-24 21:19:52 +08001417 struct scatterlist sg[2];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001418
Herbert Xu69110e32016-01-24 21:19:52 +08001419 sg_init_table(sg, ARRAY_SIZE(sg));
1420 sg_set_buf(sg, buf, payload_length);
Laura Abbott679fcae2018-09-04 11:47:40 -07001421 if (padding)
1422 sg_set_buf(sg + 1, pad_bytes, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001423
Herbert Xu69110e32016-01-24 21:19:52 +08001424 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001425
Herbert Xu69110e32016-01-24 21:19:52 +08001426 crypto_ahash_digest(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001427}
1428
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001429int
Varun Prakash9a584bf2017-01-13 20:53:21 +05301430__iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1431 struct iscsi_cmd *cmd, u32 payload_length,
1432 bool *success)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001433{
Varun Prakash9a584bf2017-01-13 20:53:21 +05301434 struct iscsi_data *hdr = buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001435 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001436 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001437
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001438 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001439 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001440
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001441 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1442 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001443 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001444 payload_length, conn->cid);
1445
1446 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1447 pr_err("Command ITT: 0x%08x received DataOUT after"
1448 " last DataOUT received, dumping payload\n",
1449 cmd->init_task_tag);
1450 return iscsit_dump_data_payload(conn, payload_length, 1);
1451 }
1452
1453 if (cmd->data_direction != DMA_TO_DEVICE) {
1454 pr_err("Command ITT: 0x%08x received DataOUT for a"
1455 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001456 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001457 }
1458 se_cmd = &cmd->se_cmd;
1459 iscsit_mod_dataout_timer(cmd);
1460
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001461 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Bart Van Asschede3493a2017-10-31 11:03:15 -07001462 pr_err("DataOut Offset: %u, Length %u greater than iSCSI Command EDTL %u, protocol error.\n",
1463 be32_to_cpu(hdr->offset), payload_length,
1464 cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001465 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001466 }
1467
1468 if (cmd->unsolicited_data) {
1469 int dump_unsolicited_data = 0;
1470
1471 if (conn->sess->sess_ops->InitialR2T) {
1472 pr_err("Received unexpected unsolicited data"
1473 " while InitialR2T=Yes, protocol error.\n");
1474 transport_send_check_condition_and_sense(&cmd->se_cmd,
1475 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1476 return -1;
1477 }
1478 /*
1479 * Special case for dealing with Unsolicited DataOUT
1480 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1481 * failures;
1482 */
1483
1484 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001485 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001486 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001487 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001488
1489 if (dump_unsolicited_data) {
1490 /*
1491 * Check if a delayed TASK_ABORTED status needs to
1492 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
Bart Van Assche5a342522015-10-22 15:53:22 -07001493 * received with the unsolicited data out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001494 */
1495 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1496 iscsit_stop_dataout_timer(cmd);
1497
1498 transport_check_aborted_status(se_cmd,
1499 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1500 return iscsit_dump_data_payload(conn, payload_length, 1);
1501 }
1502 } else {
1503 /*
1504 * For the normal solicited data path:
1505 *
1506 * Check for a delayed TASK_ABORTED status and dump any
1507 * incoming data out payload if one exists. Also, when the
1508 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1509 * data out sequence, we decrement outstanding_r2ts. Once
1510 * outstanding_r2ts reaches zero, go ahead and send the delayed
1511 * TASK_ABORTED status.
1512 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001513 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001514 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1515 if (--cmd->outstanding_r2ts < 1) {
1516 iscsit_stop_dataout_timer(cmd);
1517 transport_check_aborted_status(
1518 se_cmd, 1);
1519 }
1520
1521 return iscsit_dump_data_payload(conn, payload_length, 1);
1522 }
1523 }
1524 /*
Bart Van Assche0d5efb82016-12-23 14:37:52 +01001525 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001526 * within-command recovery checks before receiving the payload.
1527 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001528 rc = iscsit_check_pre_dataout(cmd, buf);
1529 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001530 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001531 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001532 return -1;
Varun Prakash9a584bf2017-01-13 20:53:21 +05301533 *success = true;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001534 return 0;
1535}
Varun Prakash9a584bf2017-01-13 20:53:21 +05301536EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
1537
1538int
1539iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1540 struct iscsi_cmd **out_cmd)
1541{
1542 struct iscsi_data *hdr = buf;
1543 struct iscsi_cmd *cmd;
1544 u32 payload_length = ntoh24(hdr->dlength);
1545 int rc;
1546 bool success = false;
1547
1548 if (!payload_length) {
1549 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1550 return 0;
1551 }
1552
1553 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1554 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1555 " MaxXmitDataSegmentLength: %u\n", payload_length,
1556 conn->conn_ops->MaxXmitDataSegmentLength);
1557 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
1558 }
1559
1560 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
1561 if (!cmd)
1562 return 0;
1563
1564 rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
1565
1566 if (success)
1567 *out_cmd = cmd;
1568
1569 return rc;
1570}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001571EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1572
1573static int
1574iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1575 struct iscsi_data *hdr)
1576{
1577 struct kvec *iov;
1578 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1579 u32 payload_length = ntoh24(hdr->dlength);
1580 int iov_ret, data_crc_failed = 0;
1581
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001582 rx_size += payload_length;
1583 iov = &cmd->iov_data[0];
1584
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001585 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1586 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001587 if (iov_ret < 0)
1588 return -1;
1589
1590 iov_count += iov_ret;
1591
1592 padding = ((-payload_length) & 3);
1593 if (padding != 0) {
1594 iov[iov_count].iov_base = cmd->pad_bytes;
1595 iov[iov_count++].iov_len = padding;
1596 rx_size += padding;
1597 pr_debug("Receiving %u padding bytes.\n", padding);
1598 }
1599
1600 if (conn->conn_ops->DataDigest) {
1601 iov[iov_count].iov_base = &checksum;
1602 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1603 rx_size += ISCSI_CRC_LEN;
1604 }
1605
1606 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1607
1608 iscsit_unmap_iovec(cmd);
1609
1610 if (rx_got != rx_size)
1611 return -1;
1612
1613 if (conn->conn_ops->DataDigest) {
1614 u32 data_crc;
1615
Herbert Xu69110e32016-01-24 21:19:52 +08001616 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001617 be32_to_cpu(hdr->offset),
1618 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001619 cmd->pad_bytes);
1620
1621 if (checksum != data_crc) {
1622 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1623 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1624 " does not match computed 0x%08x\n",
1625 hdr->itt, hdr->offset, payload_length,
1626 hdr->datasn, checksum, data_crc);
1627 data_crc_failed = 1;
1628 } else {
1629 pr_debug("Got CRC32C DataDigest 0x%08x for"
1630 " %u bytes of Data Out\n", checksum,
1631 payload_length);
1632 }
1633 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001634
1635 return data_crc_failed;
1636}
1637
1638int
1639iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1640 bool data_crc_failed)
1641{
1642 struct iscsi_conn *conn = cmd->conn;
1643 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001644 /*
1645 * Increment post receive data and CRC values or perform
1646 * within-command recovery.
1647 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001648 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1649 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001650 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001651 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001652 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001653 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1654 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001655 /*
1656 * Handle extra special case for out of order
1657 * Unsolicited Data Out.
1658 */
1659 spin_lock_bh(&cmd->istate_lock);
1660 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1661 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1662 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1663 spin_unlock_bh(&cmd->istate_lock);
1664
1665 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001666 if (ooo_cmdsn)
1667 return 0;
1668 target_execute_cmd(&cmd->se_cmd);
1669 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001670 } else /* DATAOUT_CANNOT_RECOVER */
1671 return -1;
1672
1673 return 0;
1674}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001675EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001676
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001677static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1678{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001679 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001680 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1681 int rc;
1682 bool data_crc_failed = false;
1683
1684 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1685 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001686 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001687 else if (!cmd)
1688 return 0;
1689
1690 rc = iscsit_get_dataout(conn, cmd, hdr);
1691 if (rc < 0)
1692 return rc;
1693 else if (rc > 0)
1694 data_crc_failed = true;
1695
1696 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1697}
1698
Nicholas Bellinger778de362013-06-14 16:07:47 -07001699int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1700 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001701{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001702 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001703
Arshad Hussaina3662602014-03-14 15:28:59 -07001704 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1705 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1706 if (!cmd)
1707 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1708 (unsigned char *)hdr);
1709
1710 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1711 (unsigned char *)hdr);
1712 }
1713
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001714 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001715 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1716 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001717 if (!cmd)
1718 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1719 (unsigned char *)hdr);
1720
Nicholas Bellingerba159912013-07-03 03:48:24 -07001721 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1722 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001723 }
1724
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001725 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001726 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001727 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001728 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001729 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001730 if (!cmd)
1731 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1732 (unsigned char *)hdr);
1733
Nicholas Bellingerba159912013-07-03 03:48:24 -07001734 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1735 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001736 }
1737
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001738 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001739 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001740 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001741 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1742 payload_length);
1743 /*
1744 * This is not a response to a Unsolicited NopIN, which means
1745 * it can either be a NOPOUT ping request (with a valid ITT),
1746 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1747 * Either way, make sure we allocate an struct iscsi_cmd, as both
1748 * can contain ping data.
1749 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001750 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001751 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1752 cmd->i_state = ISTATE_SEND_NOPIN;
1753 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1754 1 : 0);
1755 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1756 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001757 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1758 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001759 cmd->data_direction = DMA_NONE;
1760 }
1761
Nicholas Bellinger778de362013-06-14 16:07:47 -07001762 return 0;
1763}
1764EXPORT_SYMBOL(iscsit_setup_nop_out);
1765
1766int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1767 struct iscsi_nopout *hdr)
1768{
1769 struct iscsi_cmd *cmd_p = NULL;
1770 int cmdsn_ret = 0;
1771 /*
1772 * Initiator is expecting a NopIN ping reply..
1773 */
1774 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001775 if (!cmd)
1776 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1777 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001778
1779 spin_lock_bh(&conn->cmd_lock);
1780 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1781 spin_unlock_bh(&conn->cmd_lock);
1782
1783 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1784
1785 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1786 iscsit_add_cmd_to_response_queue(cmd, conn,
1787 cmd->i_state);
1788 return 0;
1789 }
1790
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001791 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1792 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001793 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1794 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001795 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001796 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001797
1798 return 0;
1799 }
1800 /*
1801 * This was a response to a unsolicited NOPIN ping.
1802 */
1803 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1804 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1805 if (!cmd_p)
1806 return -EINVAL;
1807
1808 iscsit_stop_nopin_response_timer(conn);
1809
1810 cmd_p->i_state = ISTATE_REMOVE;
1811 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1812
1813 iscsit_start_nopin_timer(conn);
1814 return 0;
1815 }
1816 /*
1817 * Otherwise, initiator is not expecting a NOPIN is response.
1818 * Just ignore for now.
1819 */
Varun Prakash1a40f0a2016-09-15 21:20:11 +05301820
1821 if (cmd)
1822 iscsit_free_cmd(cmd, false);
1823
Nicholas Bellinger778de362013-06-14 16:07:47 -07001824 return 0;
1825}
1826EXPORT_SYMBOL(iscsit_process_nop_out);
1827
1828static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1829 unsigned char *buf)
1830{
1831 unsigned char *ping_data = NULL;
1832 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1833 struct kvec *iov = NULL;
1834 u32 payload_length = ntoh24(hdr->dlength);
1835 int ret;
1836
1837 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1838 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001839 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001840 /*
1841 * Handle NOP-OUT payload for traditional iSCSI sockets
1842 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001843 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001844 u32 checksum, data_crc, padding = 0;
1845 int niov = 0, rx_got, rx_size = payload_length;
1846
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001847 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1848 if (!ping_data) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001849 ret = -1;
1850 goto out;
1851 }
1852
1853 iov = &cmd->iov_misc[0];
1854 iov[niov].iov_base = ping_data;
1855 iov[niov++].iov_len = payload_length;
1856
1857 padding = ((-payload_length) & 3);
1858 if (padding != 0) {
1859 pr_debug("Receiving %u additional bytes"
1860 " for padding.\n", padding);
1861 iov[niov].iov_base = &cmd->pad_bytes;
1862 iov[niov++].iov_len = padding;
1863 rx_size += padding;
1864 }
1865 if (conn->conn_ops->DataDigest) {
1866 iov[niov].iov_base = &checksum;
1867 iov[niov++].iov_len = ISCSI_CRC_LEN;
1868 rx_size += ISCSI_CRC_LEN;
1869 }
1870
1871 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1872 if (rx_got != rx_size) {
1873 ret = -1;
1874 goto out;
1875 }
1876
1877 if (conn->conn_ops->DataDigest) {
Bart Van Asschee1dfb212017-10-31 11:03:16 -07001878 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, ping_data,
1879 payload_length, padding,
1880 cmd->pad_bytes, &data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001881
1882 if (checksum != data_crc) {
1883 pr_err("Ping data CRC32C DataDigest"
1884 " 0x%08x does not match computed 0x%08x\n",
1885 checksum, data_crc);
1886 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1887 pr_err("Unable to recover from"
1888 " NOPOUT Ping DataCRC failure while in"
1889 " ERL=0.\n");
1890 ret = -1;
1891 goto out;
1892 } else {
1893 /*
1894 * Silently drop this PDU and let the
1895 * initiator plug the CmdSN gap.
1896 */
1897 pr_debug("Dropping NOPOUT"
1898 " Command CmdSN: 0x%08x due to"
1899 " DataCRC error.\n", hdr->cmdsn);
1900 ret = 0;
1901 goto out;
1902 }
1903 } else {
1904 pr_debug("Got CRC32C DataDigest"
1905 " 0x%08x for %u bytes of ping data.\n",
1906 checksum, payload_length);
1907 }
1908 }
1909
1910 ping_data[payload_length] = '\0';
1911 /*
1912 * Attach ping data to struct iscsi_cmd->buf_ptr.
1913 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001914 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001915 cmd->buf_ptr_size = payload_length;
1916
1917 pr_debug("Got %u bytes of NOPOUT ping"
1918 " data.\n", payload_length);
1919 pr_debug("Ping Data: \"%s\"\n", ping_data);
1920 }
1921
Nicholas Bellinger778de362013-06-14 16:07:47 -07001922 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001923out:
1924 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001925 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001926
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001927 kfree(ping_data);
1928 return ret;
1929}
1930
Bart Van Asschee381fe92017-01-06 11:32:08 +01001931static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
1932{
1933 switch (iscsi_tmf) {
1934 case ISCSI_TM_FUNC_ABORT_TASK:
1935 return TMR_ABORT_TASK;
1936 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1937 return TMR_ABORT_TASK_SET;
1938 case ISCSI_TM_FUNC_CLEAR_ACA:
1939 return TMR_CLEAR_ACA;
1940 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1941 return TMR_CLEAR_TASK_SET;
1942 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1943 return TMR_LUN_RESET;
1944 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1945 return TMR_TARGET_WARM_RESET;
1946 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1947 return TMR_TARGET_COLD_RESET;
1948 default:
1949 return TMR_UNKNOWN;
1950 }
1951}
1952
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001953int
1954iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1955 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001956{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001957 struct se_tmr_req *se_tmr;
1958 struct iscsi_tmr_req *tmr_req;
1959 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001960 int out_of_order_cmdsn = 0, ret;
Bart Van Assche59b69862017-01-05 12:39:57 +01001961 u8 function, tcm_function = TMR_UNKNOWN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001962
1963 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001964 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1965 function = hdr->flags;
1966
1967 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1968 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1969 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1970 hdr->rtt, hdr->refcmdsn, conn->cid);
1971
1972 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1973 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001974 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001975 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001976 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001977 }
1978
1979 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1980 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1981 pr_err("Task Management Request TASK_REASSIGN not"
1982 " issued as immediate command, bad iSCSI Initiator"
1983 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001984 return iscsit_add_reject_cmd(cmd,
1985 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001986 }
1987 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001988 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1989 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001990
Andy Groverd28b11692012-04-03 15:51:22 -07001991 cmd->data_direction = DMA_NONE;
Markus Elfring3829f382017-04-09 16:00:39 +02001992 cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL);
Nicholas Bellingerae072722017-10-27 12:32:59 -07001993 if (!cmd->tmr_req) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001994 return iscsit_add_reject_cmd(cmd,
1995 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1996 buf);
Nicholas Bellingerae072722017-10-27 12:32:59 -07001997 }
1998
1999 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
2000 conn->sess->se_sess, 0, DMA_NONE,
2001 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
2002
Bart Van Asscheb0aede22019-11-13 14:05:08 -08002003 if (target_get_sess_cmd(&cmd->se_cmd, true) < 0)
2004 return iscsit_add_reject_cmd(cmd,
2005 ISCSI_REASON_WAITING_FOR_LOGOUT, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002006
2007 /*
2008 * TASK_REASSIGN for ERL=2 / connection stays inside of
2009 * LIO-Target $FABRIC_MOD
2010 */
2011 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Bart Van Asschee381fe92017-01-06 11:32:08 +01002012 tcm_function = iscsit_convert_tmf(function);
2013 if (tcm_function == TMR_UNKNOWN) {
Andy Groverd28b11692012-04-03 15:51:22 -07002014 pr_err("Unknown iSCSI TMR Function:"
2015 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002016 return iscsit_add_reject_cmd(cmd,
2017 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002018 }
Bart Van Assche59b69862017-01-05 12:39:57 +01002019 }
2020 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
2021 GFP_KERNEL);
2022 if (ret < 0)
2023 return iscsit_add_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002024 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002025
Bart Van Assche59b69862017-01-05 12:39:57 +01002026 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002027
2028 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2029 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2030 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2031 cmd->init_task_tag = hdr->itt;
2032 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002033 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2034 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002035 se_tmr = cmd->se_cmd.se_tmr_req;
2036 tmr_req = cmd->tmr_req;
2037 /*
2038 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2039 */
2040 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08002041 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
2042 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002043 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002044 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2045 goto attach;
2046 }
2047 }
2048
2049 switch (function) {
2050 case ISCSI_TM_FUNC_ABORT_TASK:
2051 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002052 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002053 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002054 break;
2055 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2056 case ISCSI_TM_FUNC_CLEAR_ACA:
2057 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2058 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2059 break;
2060 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2061 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002062 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2063 goto attach;
2064 }
2065 break;
2066 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2067 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002068 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2069 goto attach;
2070 }
2071 break;
2072 case ISCSI_TM_FUNC_TASK_REASSIGN:
2073 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2074 /*
2075 * Perform sanity checks on the ExpDataSN only if the
2076 * TASK_REASSIGN was successful.
2077 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002078 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002079 break;
2080
2081 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002082 return iscsit_add_reject_cmd(cmd,
2083 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002084 break;
2085 default:
2086 pr_err("Unknown TMR function: 0x%02x, protocol"
2087 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002088 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2089 goto attach;
2090 }
2091
2092 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2093 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2094 se_tmr->call_transport = 1;
2095attach:
2096 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002097 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002098 spin_unlock_bh(&conn->cmd_lock);
2099
2100 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002101 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellinger3fc9fb12017-10-27 20:52:56 -07002102 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002103 out_of_order_cmdsn = 1;
Nicholas Bellinger3fc9fb12017-10-27 20:52:56 -07002104 } else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2105 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002106 return 0;
Nicholas Bellinger3fc9fb12017-10-27 20:52:56 -07002107 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002108 return -1;
Nicholas Bellinger3fc9fb12017-10-27 20:52:56 -07002109 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002110 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002111 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002112
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002113 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002114 return 0;
2115 /*
2116 * Found the referenced task, send to transport for processing.
2117 */
2118 if (se_tmr->call_transport)
2119 return transport_generic_handle_tmr(&cmd->se_cmd);
2120
2121 /*
2122 * Could not find the referenced LUN, task, or Task Management
2123 * command not authorized or supported. Change state and
2124 * let the tx_thread send the response.
2125 *
2126 * For connection recovery, this is also the default action for
2127 * TMR TASK_REASSIGN.
2128 */
2129 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellingerae072722017-10-27 12:32:59 -07002130 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002131 return 0;
2132}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002133EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002134
2135/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002136int
2137iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2138 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002139{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002140 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002141
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002142 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002143 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002144 "greater than MaxXmitDataSegmentLength %u.\n",
2145 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002146 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2147 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002148 }
2149
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08002150 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2151 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2152 pr_err("Multi sequence text commands currently not supported\n");
2153 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2154 (unsigned char *)hdr);
2155 }
2156
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002157 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2158 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2159 hdr->exp_statsn, payload_length);
2160
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002161 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2162 cmd->i_state = ISTATE_SEND_TEXTRSP;
2163 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2164 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2165 cmd->targ_xfer_tag = 0xFFFFFFFF;
2166 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2167 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2168 cmd->data_direction = DMA_NONE;
Varun Prakashea8dc5b2017-07-23 20:03:33 +05302169 kfree(cmd->text_in_ptr);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002170 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002171
2172 return 0;
2173}
2174EXPORT_SYMBOL(iscsit_setup_text_cmd);
2175
2176int
2177iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2178 struct iscsi_text *hdr)
2179{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002180 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002181 int cmdsn_ret;
2182
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002183 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002184 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2185 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2186 pr_err("Unable to locate text_in buffer for sendtargets"
2187 " discovery\n");
2188 goto reject;
2189 }
2190 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002191 }
2192 if (strncmp("SendTargets", text_in, 11) != 0) {
2193 pr_err("Received Text Data that is not"
2194 " SendTargets, cannot continue.\n");
2195 goto reject;
2196 }
2197 text_ptr = strchr(text_in, '=');
2198 if (!text_ptr) {
2199 pr_err("No \"=\" separator found in Text Data,"
2200 " cannot continue.\n");
2201 goto reject;
2202 }
2203 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002204 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002205 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2206 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002207 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002208 } else {
2209 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2210 goto reject;
2211 }
2212
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002213 spin_lock_bh(&conn->cmd_lock);
2214 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2215 spin_unlock_bh(&conn->cmd_lock);
2216
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002217empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002218 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2219
2220 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002221 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2222 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002223 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002224 return -1;
2225
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002226 return 0;
2227 }
2228
2229 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002230
2231reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002232 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2233 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002234}
2235EXPORT_SYMBOL(iscsit_process_text_cmd);
2236
2237static int
2238iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2239 unsigned char *buf)
2240{
2241 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2242 char *text_in = NULL;
2243 u32 payload_length = ntoh24(hdr->dlength);
2244 int rx_size, rc;
2245
2246 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2247 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002248 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002249
2250 rx_size = payload_length;
2251 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002252 u32 checksum = 0, data_crc = 0;
2253 u32 padding = 0, pad_bytes = 0;
2254 int niov = 0, rx_got;
2255 struct kvec iov[3];
2256
2257 text_in = kzalloc(payload_length, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02002258 if (!text_in)
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002259 goto reject;
Markus Elfringc46e22f2017-04-09 15:34:50 +02002260
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002261 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002262
2263 memset(iov, 0, 3 * sizeof(struct kvec));
2264 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002265 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002266
2267 padding = ((-payload_length) & 3);
2268 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002269 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002270 iov[niov++].iov_len = padding;
2271 rx_size += padding;
2272 pr_debug("Receiving %u additional bytes"
2273 " for padding.\n", padding);
2274 }
2275 if (conn->conn_ops->DataDigest) {
2276 iov[niov].iov_base = &checksum;
2277 iov[niov++].iov_len = ISCSI_CRC_LEN;
2278 rx_size += ISCSI_CRC_LEN;
2279 }
2280
2281 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002282 if (rx_got != rx_size)
2283 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002284
2285 if (conn->conn_ops->DataDigest) {
Bart Van Asschee1dfb212017-10-31 11:03:16 -07002286 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, text_in,
2287 payload_length, padding,
2288 &pad_bytes, &data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002289
2290 if (checksum != data_crc) {
2291 pr_err("Text data CRC32C DataDigest"
2292 " 0x%08x does not match computed"
2293 " 0x%08x\n", checksum, data_crc);
2294 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2295 pr_err("Unable to recover from"
2296 " Text Data digest failure while in"
2297 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002298 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002299 } else {
2300 /*
2301 * Silently drop this PDU and let the
2302 * initiator plug the CmdSN gap.
2303 */
2304 pr_debug("Dropping Text"
2305 " Command CmdSN: 0x%08x due to"
2306 " DataCRC error.\n", hdr->cmdsn);
2307 kfree(text_in);
2308 return 0;
2309 }
2310 } else {
2311 pr_debug("Got CRC32C DataDigest"
2312 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002313 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002314 }
2315 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002316 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002317 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002318 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002319 }
2320
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002321 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002322
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002323reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002324 kfree(cmd->text_in_ptr);
2325 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002326 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002327}
2328
2329int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2330{
2331 struct iscsi_conn *conn_p;
2332 struct iscsi_session *sess = conn->sess;
2333
2334 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2335 " for SID: %u.\n", conn->cid, conn->sess->sid);
2336
2337 atomic_set(&sess->session_logout, 1);
2338 atomic_set(&conn->conn_logout_remove, 1);
2339 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2340
2341 iscsit_inc_conn_usage_count(conn);
2342 iscsit_inc_session_usage_count(sess);
2343
2344 spin_lock_bh(&sess->conn_lock);
2345 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2346 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2347 continue;
2348
2349 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2350 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2351 }
2352 spin_unlock_bh(&sess->conn_lock);
2353
2354 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2355
2356 return 0;
2357}
2358
2359int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2360{
2361 struct iscsi_conn *l_conn;
2362 struct iscsi_session *sess = conn->sess;
2363
2364 pr_debug("Received logout request CLOSECONNECTION for CID:"
2365 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2366
2367 /*
2368 * A Logout Request with a CLOSECONNECTION reason code for a CID
2369 * can arrive on a connection with a differing CID.
2370 */
2371 if (conn->cid == cmd->logout_cid) {
2372 spin_lock_bh(&conn->state_lock);
2373 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2374 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2375
2376 atomic_set(&conn->conn_logout_remove, 1);
2377 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2378 iscsit_inc_conn_usage_count(conn);
2379
2380 spin_unlock_bh(&conn->state_lock);
2381 } else {
2382 /*
2383 * Handle all different cid CLOSECONNECTION requests in
2384 * iscsit_logout_post_handler_diffcid() as to give enough
2385 * time for any non immediate command's CmdSN to be
2386 * acknowledged on the connection in question.
2387 *
2388 * Here we simply make sure the CID is still around.
2389 */
2390 l_conn = iscsit_get_conn_from_cid(sess,
2391 cmd->logout_cid);
2392 if (!l_conn) {
2393 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2394 iscsit_add_cmd_to_response_queue(cmd, conn,
2395 cmd->i_state);
2396 return 0;
2397 }
2398
2399 iscsit_dec_conn_usage_count(l_conn);
2400 }
2401
2402 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2403
2404 return 0;
2405}
2406
2407int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2408{
2409 struct iscsi_session *sess = conn->sess;
2410
2411 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2412 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2413
2414 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2415 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2416 " while ERL!=2.\n");
2417 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2418 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2419 return 0;
2420 }
2421
2422 if (conn->cid == cmd->logout_cid) {
2423 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2424 " with CID: %hu on CID: %hu, implementation error.\n",
2425 cmd->logout_cid, conn->cid);
2426 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2427 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2428 return 0;
2429 }
2430
2431 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2432
2433 return 0;
2434}
2435
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002436int
2437iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2438 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002439{
2440 int cmdsn_ret, logout_remove = 0;
2441 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002442 struct iscsi_logout *hdr;
2443 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2444
2445 hdr = (struct iscsi_logout *) buf;
2446 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002447
2448 if (tiqn) {
2449 spin_lock(&tiqn->logout_stats.lock);
2450 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2451 tiqn->logout_stats.normal_logouts++;
2452 else
2453 tiqn->logout_stats.abnormal_logouts++;
2454 spin_unlock(&tiqn->logout_stats.lock);
2455 }
2456
2457 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2458 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2459 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2460 hdr->cid, conn->cid);
2461
2462 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2463 pr_err("Received logout request on connection that"
2464 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002465 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002466 return 0;
2467 }
2468
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002469 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2470 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2471 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2472 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2473 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002474 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2475 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2476 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002477 cmd->logout_reason = reason_code;
2478 cmd->data_direction = DMA_NONE;
2479
2480 /*
2481 * We need to sleep in these cases (by returning 1) until the Logout
2482 * Response gets sent in the tx thread.
2483 */
2484 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2485 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002486 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002487 logout_remove = 1;
2488
2489 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002490 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002491 spin_unlock_bh(&conn->cmd_lock);
2492
2493 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002494 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002495
2496 /*
2497 * Immediate commands are executed, well, immediately.
2498 * Non-Immediate Logout Commands are executed in CmdSN order.
2499 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002500 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002501 int ret = iscsit_execute_cmd(cmd, 0);
2502
2503 if (ret < 0)
2504 return ret;
2505 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002506 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002507 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002508 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002509 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2510 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002511 }
2512
2513 return logout_remove;
2514}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002515EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002516
Varun Prakashd2faaef2016-04-20 00:00:19 +05302517int iscsit_handle_snack(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002518 struct iscsi_conn *conn,
2519 unsigned char *buf)
2520{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002521 struct iscsi_snack *hdr;
2522
2523 hdr = (struct iscsi_snack *) buf;
2524 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002525
2526 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2527 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2528 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2529 hdr->begrun, hdr->runlength, conn->cid);
2530
2531 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2532 pr_err("Initiator sent SNACK request while in"
2533 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002534 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2535 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002536 }
2537 /*
2538 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2539 * call from inside iscsi_send_recovery_datain_or_r2t().
2540 */
2541 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2542 case 0:
2543 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002544 hdr->itt,
2545 be32_to_cpu(hdr->ttt),
2546 be32_to_cpu(hdr->begrun),
2547 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002548 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002549 return iscsit_handle_status_snack(conn, hdr->itt,
2550 be32_to_cpu(hdr->ttt),
2551 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002552 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002553 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2554 be32_to_cpu(hdr->begrun),
2555 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002556 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2557 /* FIXME: Support R-Data SNACK */
2558 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002559 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2560 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002561 default:
2562 pr_err("Unknown SNACK type 0x%02x, protocol"
2563 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002564 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2565 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002566 }
2567
2568 return 0;
2569}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302570EXPORT_SYMBOL(iscsit_handle_snack);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002571
2572static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2573{
2574 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2575 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2576 wait_for_completion_interruptible_timeout(
2577 &conn->rx_half_close_comp,
2578 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2579 }
2580}
2581
2582static int iscsit_handle_immediate_data(
2583 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002584 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002585 u32 length)
2586{
2587 int iov_ret, rx_got = 0, rx_size = 0;
2588 u32 checksum, iov_count = 0, padding = 0;
2589 struct iscsi_conn *conn = cmd->conn;
2590 struct kvec *iov;
2591
2592 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2593 if (iov_ret < 0)
2594 return IMMEDIATE_DATA_CANNOT_RECOVER;
2595
2596 rx_size = length;
2597 iov_count = iov_ret;
2598 iov = &cmd->iov_data[0];
2599
2600 padding = ((-length) & 3);
2601 if (padding != 0) {
2602 iov[iov_count].iov_base = cmd->pad_bytes;
2603 iov[iov_count++].iov_len = padding;
2604 rx_size += padding;
2605 }
2606
2607 if (conn->conn_ops->DataDigest) {
2608 iov[iov_count].iov_base = &checksum;
2609 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2610 rx_size += ISCSI_CRC_LEN;
2611 }
2612
2613 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2614
2615 iscsit_unmap_iovec(cmd);
2616
2617 if (rx_got != rx_size) {
2618 iscsit_rx_thread_wait_for_tcp(conn);
2619 return IMMEDIATE_DATA_CANNOT_RECOVER;
2620 }
2621
2622 if (conn->conn_ops->DataDigest) {
2623 u32 data_crc;
2624
Herbert Xu69110e32016-01-24 21:19:52 +08002625 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002626 cmd->write_data_done, length, padding,
2627 cmd->pad_bytes);
2628
2629 if (checksum != data_crc) {
2630 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2631 " does not match computed 0x%08x\n", checksum,
2632 data_crc);
2633
2634 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2635 pr_err("Unable to recover from"
2636 " Immediate Data digest failure while"
2637 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002638 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002639 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002640 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002641 return IMMEDIATE_DATA_CANNOT_RECOVER;
2642 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002643 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002644 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002645 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002646 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2647 }
2648 } else {
2649 pr_debug("Got CRC32C DataDigest 0x%08x for"
2650 " %u bytes of Immediate Data\n", checksum,
2651 length);
2652 }
2653 }
2654
2655 cmd->write_data_done += length;
2656
Andy Groverebf1d952012-04-03 15:51:24 -07002657 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002658 spin_lock_bh(&cmd->istate_lock);
2659 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2660 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2661 spin_unlock_bh(&cmd->istate_lock);
2662 }
2663
2664 return IMMEDIATE_DATA_NORMAL_OPERATION;
2665}
2666
2667/*
2668 * Called with sess->conn_lock held.
2669 */
2670/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2671 with active network interface */
2672static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2673{
2674 struct iscsi_cmd *cmd;
2675 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002676 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002677
2678 /*
2679 * Only send a Asynchronous Message on connections whos network
2680 * interface is still functional.
2681 */
2682 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2683 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2684 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002685 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002686 break;
2687 }
2688 }
2689
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002690 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002691 return;
2692
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002693 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002694 if (!cmd) {
2695 iscsit_dec_conn_usage_count(conn_p);
2696 return;
2697 }
2698
2699 cmd->logout_cid = conn->cid;
2700 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2701 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2702
2703 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002704 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002705 spin_unlock_bh(&conn_p->cmd_lock);
2706
2707 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2708 iscsit_dec_conn_usage_count(conn_p);
2709}
2710
2711static int iscsit_send_conn_drop_async_message(
2712 struct iscsi_cmd *cmd,
2713 struct iscsi_conn *conn)
2714{
2715 struct iscsi_async *hdr;
2716
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002717 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2718
2719 hdr = (struct iscsi_async *) cmd->pdu;
2720 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2721 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002722 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002723 cmd->targ_xfer_tag = 0xFFFFFFFF;
2724 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2725 cmd->stat_sn = conn->stat_sn++;
2726 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2727 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002728 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002729 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2730 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2731 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2732 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2733
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002734 pr_debug("Sending Connection Dropped Async Message StatSN:"
2735 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2736 cmd->logout_cid, conn->cid);
Varun Prakash2854bb22016-04-20 00:00:08 +05302737
2738 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002739}
2740
Andy Grover6f3c0e62012-04-03 15:51:09 -07002741static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2742{
2743 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2744 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2745 wait_for_completion_interruptible_timeout(
2746 &conn->tx_half_close_comp,
2747 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2748 }
2749}
2750
Varun Prakashd2faaef2016-04-20 00:00:19 +05302751void
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002752iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2753 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2754 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002755{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002756 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2757 hdr->flags = datain->flags;
2758 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2759 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2760 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2761 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2762 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2763 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2764 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2765 }
2766 }
2767 hton24(hdr->dlength, datain->length);
2768 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2769 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2770 (struct scsi_lun *)&hdr->lun);
2771 else
2772 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2773
2774 hdr->itt = cmd->init_task_tag;
2775
2776 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2777 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2778 else
2779 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2780 if (set_statsn)
2781 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2782 else
2783 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2784
2785 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002786 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002787 hdr->datasn = cpu_to_be32(datain->data_sn);
2788 hdr->offset = cpu_to_be32(datain->offset);
2789
2790 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2791 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2792 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2793 ntohl(hdr->offset), datain->length, conn->cid);
2794}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302795EXPORT_SYMBOL(iscsit_build_datain_pdu);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002796
2797static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2798{
2799 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002800 struct iscsi_datain datain;
2801 struct iscsi_datain_req *dr;
Varun Prakash2854bb22016-04-20 00:00:08 +05302802 int eodr = 0, ret;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002803 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002804
2805 memset(&datain, 0, sizeof(struct iscsi_datain));
2806 dr = iscsit_get_datain_values(cmd, &datain);
2807 if (!dr) {
2808 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2809 cmd->init_task_tag);
2810 return -1;
2811 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002812 /*
2813 * Be paranoid and double check the logic for now.
2814 */
Andy Groverebf1d952012-04-03 15:51:24 -07002815 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002816 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2817 " datain.length: %u exceeds cmd->data_length: %u\n",
2818 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002819 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002820 return -1;
2821 }
2822
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002823 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002824 /*
2825 * Special case for successfully execution w/ both DATAIN
2826 * and Sense Data.
2827 */
2828 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2829 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2830 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2831 else {
2832 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2833 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2834 iscsit_increment_maxcmdsn(cmd, conn->sess);
2835 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002836 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002837 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002838 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2839 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002840 }
2841
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002842 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002843
Varun Prakash2854bb22016-04-20 00:00:08 +05302844 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2845 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07002846 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002847
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002848 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002849 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002850 2 : 1;
2851 iscsit_free_datain_req(cmd, dr);
2852 }
2853
Andy Grover6f3c0e62012-04-03 15:51:09 -07002854 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002855}
2856
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002857int
2858iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2859 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002860{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002861 struct iscsi_conn *logout_conn = NULL;
2862 struct iscsi_conn_recovery *cr = NULL;
2863 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002864 /*
2865 * The actual shutting down of Sessions and/or Connections
2866 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2867 * is done in scsi_logout_post_handler().
2868 */
2869 switch (cmd->logout_reason) {
2870 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2871 pr_debug("iSCSI session logout successful, setting"
2872 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2873 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2874 break;
2875 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2876 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2877 break;
2878 /*
2879 * For CLOSECONNECTION logout requests carrying
2880 * a matching logout CID -> local CID, the reference
2881 * for the local CID will have been incremented in
2882 * iscsi_logout_closeconnection().
2883 *
2884 * For CLOSECONNECTION logout requests carrying
2885 * a different CID than the connection it arrived
2886 * on, the connection responding to cmd->logout_cid
2887 * is stopped in iscsit_logout_post_handler_diffcid().
2888 */
2889
2890 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2891 " successful.\n", cmd->logout_cid, conn->cid);
2892 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2893 break;
2894 case ISCSI_LOGOUT_REASON_RECOVERY:
2895 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2896 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2897 break;
2898 /*
2899 * If the connection is still active from our point of view
2900 * force connection recovery to occur.
2901 */
2902 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2903 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002904 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002905 iscsit_connection_reinstatement_rcfr(logout_conn);
2906 iscsit_dec_conn_usage_count(logout_conn);
2907 }
2908
2909 cr = iscsit_get_inactive_connection_recovery_entry(
2910 conn->sess, cmd->logout_cid);
2911 if (!cr) {
2912 pr_err("Unable to locate CID: %hu for"
2913 " REMOVECONNFORRECOVERY Logout Request.\n",
2914 cmd->logout_cid);
2915 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2916 break;
2917 }
2918
2919 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2920
2921 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2922 " for recovery for CID: %hu on CID: %hu successful.\n",
2923 cmd->logout_cid, conn->cid);
2924 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2925 break;
2926 default:
2927 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2928 cmd->logout_reason);
2929 return -1;
2930 }
2931
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002932 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2933 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2934 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002935 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002936 cmd->stat_sn = conn->stat_sn++;
2937 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2938
2939 iscsit_increment_maxcmdsn(cmd, conn->sess);
2940 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002941 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002942
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002943 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2944 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2945 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2946 cmd->logout_cid, conn->cid);
2947
2948 return 0;
2949}
2950EXPORT_SYMBOL(iscsit_build_logout_rsp);
2951
2952static int
2953iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2954{
Varun Prakash2854bb22016-04-20 00:00:08 +05302955 int rc;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002956
2957 rc = iscsit_build_logout_rsp(cmd, conn,
2958 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2959 if (rc < 0)
2960 return rc;
2961
Varun Prakash2854bb22016-04-20 00:00:08 +05302962 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002963}
2964
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002965void
2966iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2967 struct iscsi_nopin *hdr, bool nopout_response)
2968{
2969 hdr->opcode = ISCSI_OP_NOOP_IN;
2970 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2971 hton24(hdr->dlength, cmd->buf_ptr_size);
2972 if (nopout_response)
2973 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2974 hdr->itt = cmd->init_task_tag;
2975 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2976 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2977 conn->stat_sn;
2978 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2979
2980 if (nopout_response)
2981 iscsit_increment_maxcmdsn(cmd, conn->sess);
2982
2983 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002984 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002985
2986 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2987 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
Colin Ian King3fc6a642016-09-02 15:30:34 +01002988 "Solicited" : "Unsolicited", cmd->init_task_tag,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002989 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2990}
2991EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2992
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002993/*
2994 * Unsolicited NOPIN, either requesting a response or not.
2995 */
2996static int iscsit_send_unsolicited_nopin(
2997 struct iscsi_cmd *cmd,
2998 struct iscsi_conn *conn,
2999 int want_response)
3000{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003001 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Varun Prakash2854bb22016-04-20 00:00:08 +05303002 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003003
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003004 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003005
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003006 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3007 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
3008
Varun Prakash2854bb22016-04-20 00:00:08 +05303009 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3010 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003011 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003012
3013 spin_lock_bh(&cmd->istate_lock);
3014 cmd->i_state = want_response ?
3015 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3016 spin_unlock_bh(&cmd->istate_lock);
3017
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003018 return 0;
3019}
3020
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003021static int
3022iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003023{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003024 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003025
3026 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003027
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028 /*
3029 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3030 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3031 */
Varun Prakash2854bb22016-04-20 00:00:08 +05303032 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003033
Varun Prakash2854bb22016-04-20 00:00:08 +05303034 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3035 cmd->buf_ptr,
3036 cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003037}
3038
Andy Grover6f3c0e62012-04-03 15:51:09 -07003039static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003040 struct iscsi_cmd *cmd,
3041 struct iscsi_conn *conn)
3042{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003043 struct iscsi_r2t *r2t;
3044 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003045 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003046
3047 r2t = iscsit_get_r2t_from_list(cmd);
3048 if (!r2t)
3049 return -1;
3050
3051 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3052 memset(hdr, 0, ISCSI_HDR_LEN);
3053 hdr->opcode = ISCSI_OP_R2T;
3054 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3055 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3056 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003057 hdr->itt = cmd->init_task_tag;
Varun Prakash85672702016-04-20 00:00:13 +05303058 if (conn->conn_transport->iscsit_get_r2t_ttt)
3059 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3060 else
3061 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003062 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3063 hdr->statsn = cpu_to_be32(conn->stat_sn);
3064 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003065 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003066 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3067 hdr->data_offset = cpu_to_be32(r2t->offset);
3068 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3069
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003070 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3071 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3072 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3073 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3074 r2t->offset, r2t->xfer_len, conn->cid);
3075
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003076 spin_lock_bh(&cmd->r2t_lock);
3077 r2t->sent_r2t = 1;
3078 spin_unlock_bh(&cmd->r2t_lock);
3079
Varun Prakash2854bb22016-04-20 00:00:08 +05303080 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003081 if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003082 return ret;
3083 }
3084
3085 spin_lock_bh(&cmd->dataout_timeout_lock);
3086 iscsit_start_dataout_timer(cmd, conn);
3087 spin_unlock_bh(&cmd->dataout_timeout_lock);
3088
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003089 return 0;
3090}
3091
3092/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003093 * @recovery: If called from iscsi_task_reassign_complete_write() for
3094 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003095 */
3096int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003097 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003098 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003099 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003100{
3101 int first_r2t = 1;
3102 u32 offset = 0, xfer_len = 0;
3103
3104 spin_lock_bh(&cmd->r2t_lock);
3105 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3106 spin_unlock_bh(&cmd->r2t_lock);
3107 return 0;
3108 }
3109
Andy Grover8b1e1242012-04-03 15:51:12 -07003110 if (conn->sess->sess_ops->DataSequenceInOrder &&
3111 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003112 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003113
3114 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3115 if (conn->sess->sess_ops->DataSequenceInOrder) {
3116 offset = cmd->r2t_offset;
3117
Andy Grover8b1e1242012-04-03 15:51:12 -07003118 if (first_r2t && recovery) {
3119 int new_data_end = offset +
3120 conn->sess->sess_ops->MaxBurstLength -
3121 cmd->next_burst_len;
3122
Andy Groverebf1d952012-04-03 15:51:24 -07003123 if (new_data_end > cmd->se_cmd.data_length)
3124 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003125 else
3126 xfer_len =
3127 conn->sess->sess_ops->MaxBurstLength -
3128 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003129 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003130 int new_data_end = offset +
3131 conn->sess->sess_ops->MaxBurstLength;
3132
Andy Groverebf1d952012-04-03 15:51:24 -07003133 if (new_data_end > cmd->se_cmd.data_length)
3134 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003135 else
3136 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003137 }
3138 cmd->r2t_offset += xfer_len;
3139
Andy Groverebf1d952012-04-03 15:51:24 -07003140 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003141 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3142 } else {
3143 struct iscsi_seq *seq;
3144
3145 seq = iscsit_get_seq_holder_for_r2t(cmd);
3146 if (!seq) {
3147 spin_unlock_bh(&cmd->r2t_lock);
3148 return -1;
3149 }
3150
3151 offset = seq->offset;
3152 xfer_len = seq->xfer_len;
3153
3154 if (cmd->seq_send_order == cmd->seq_count)
3155 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3156 }
3157 cmd->outstanding_r2ts++;
3158 first_r2t = 0;
3159
3160 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3161 spin_unlock_bh(&cmd->r2t_lock);
3162 return -1;
3163 }
3164
3165 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3166 break;
3167 }
3168 spin_unlock_bh(&cmd->r2t_lock);
3169
3170 return 0;
3171}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303172EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003173
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003174void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3175 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003176{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003177 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003178 cmd->stat_sn = conn->stat_sn++;
3179
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003180 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003181
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003182 memset(hdr, 0, ISCSI_HDR_LEN);
3183 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3184 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3185 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3186 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003187 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003188 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3189 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003190 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003191 }
3192 hdr->response = cmd->iscsi_response;
3193 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003194 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003195 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3196
3197 iscsit_increment_maxcmdsn(cmd, conn->sess);
3198 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003199 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003200
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003201 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3202 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3203 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3204 cmd->se_cmd.scsi_status, conn->cid);
3205}
3206EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3207
3208static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3209{
3210 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003211 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
Varun Prakash2854bb22016-04-20 00:00:08 +05303212 void *data_buf = NULL;
3213 u32 padding = 0, data_buf_len = 0;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003214
3215 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3216
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003217 /*
3218 * Attach SENSE DATA payload to iSCSI Response PDU
3219 */
3220 if (cmd->se_cmd.sense_buffer &&
3221 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3222 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003223 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3224 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3225
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003226 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003227 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Varun Prakash2854bb22016-04-20 00:00:08 +05303228 data_buf = cmd->sense_buffer;
3229 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003230
3231 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003232 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003233 cmd->se_cmd.scsi_sense_length, 0, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003234 pr_debug("Adding %u bytes of padding to"
3235 " SENSE.\n", padding);
3236 }
3237
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003238 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3239 " Response PDU\n",
3240 cmd->se_cmd.scsi_sense_length);
3241 }
3242
Varun Prakash2854bb22016-04-20 00:00:08 +05303243 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3244 data_buf_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003245}
3246
3247static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3248{
3249 switch (se_tmr->response) {
3250 case TMR_FUNCTION_COMPLETE:
3251 return ISCSI_TMF_RSP_COMPLETE;
3252 case TMR_TASK_DOES_NOT_EXIST:
3253 return ISCSI_TMF_RSP_NO_TASK;
3254 case TMR_LUN_DOES_NOT_EXIST:
3255 return ISCSI_TMF_RSP_NO_LUN;
3256 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3257 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003258 case TMR_FUNCTION_REJECTED:
3259 default:
3260 return ISCSI_TMF_RSP_REJECTED;
3261 }
3262}
3263
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003264void
3265iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3266 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003267{
3268 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003269
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003270 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003271 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003272 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003273 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003274 cmd->stat_sn = conn->stat_sn++;
3275 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3276
3277 iscsit_increment_maxcmdsn(cmd, conn->sess);
3278 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003279 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003280
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003281 pr_debug("Built Task Management Response ITT: 0x%08x,"
3282 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3283 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3284}
3285EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3286
3287static int
3288iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3289{
3290 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003291
3292 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3293
Varun Prakash2854bb22016-04-20 00:00:08 +05303294 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003295}
3296
Andy Grover8b1e1242012-04-03 15:51:12 -07003297#define SENDTARGETS_BUF_LIMIT 32768U
3298
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003299static int
3300iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003301 enum iscsit_transport_type network_transport,
3302 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003303{
3304 char *payload = NULL;
3305 struct iscsi_conn *conn = cmd->conn;
3306 struct iscsi_portal_group *tpg;
3307 struct iscsi_tiqn *tiqn;
3308 struct iscsi_tpg_np *tpg_np;
3309 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003310 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003311 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003312 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
David Disseldorpa6415cd2015-08-01 00:10:12 -07003313 bool active;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003314
Sagi Grimbergbe7dcfb2015-01-26 12:49:06 +02003315 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003316 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003317
3318 payload = kzalloc(buffer_len, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02003319 if (!payload)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003320 return -ENOMEM;
Markus Elfringc46e22f2017-04-09 15:34:50 +02003321
Nicholas Bellinger66658892013-06-19 22:45:42 -07003322 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003323 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003324 * explicit case..
3325 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003326 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003327 text_ptr = strchr(text_in, '=');
3328 if (!text_ptr) {
3329 pr_err("Unable to locate '=' string in text_in:"
3330 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003331 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003332 return -EINVAL;
3333 }
3334 /*
3335 * Skip over '=' character..
3336 */
3337 text_ptr += 1;
3338 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003339
3340 spin_lock(&tiqn_lock);
3341 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003342 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003343 strcmp(tiqn->tiqn, text_ptr)) {
3344 continue;
3345 }
3346
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003347 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003348
3349 spin_lock(&tiqn->tiqn_tpg_lock);
3350 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3351
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003352 /* If demo_mode_discovery=0 and generate_node_acls=0
3353 * (demo mode dislabed) do not return
3354 * TargetName+TargetAddress unless a NodeACL exists.
3355 */
3356
3357 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3358 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
Nicholas Bellinger21aaa232016-01-07 22:09:27 -08003359 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003360 cmd->conn->sess->sess_ops->InitiatorName))) {
3361 continue;
3362 }
3363
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003364 spin_lock(&tpg->tpg_state_lock);
David Disseldorpa6415cd2015-08-01 00:10:12 -07003365 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003366 spin_unlock(&tpg->tpg_state_lock);
3367
David Disseldorpa6415cd2015-08-01 00:10:12 -07003368 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3369 continue;
3370
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003371 spin_lock(&tpg->tpg_np_lock);
3372 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3373 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003374 struct iscsi_np *np = tpg_np->tpg_np;
Andy Grover13a3cf02015-08-24 10:26:06 -07003375 struct sockaddr_storage *sockaddr;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003376
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003377 if (np->np_network_transport != network_transport)
3378 continue;
3379
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003380 if (!target_name_printed) {
3381 len = sprintf(buf, "TargetName=%s",
3382 tiqn->tiqn);
3383 len += 1;
3384
3385 if ((len + payload_len) > buffer_len) {
3386 spin_unlock(&tpg->tpg_np_lock);
3387 spin_unlock(&tiqn->tiqn_tpg_lock);
3388 end_of_buf = 1;
3389 goto eob;
3390 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003391
3392 if (skip_bytes && len <= skip_bytes) {
3393 skip_bytes -= len;
3394 } else {
3395 memcpy(payload + payload_len, buf, len);
3396 payload_len += len;
3397 target_name_printed = 1;
3398 if (len > skip_bytes)
3399 skip_bytes = 0;
3400 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003401 }
3402
Sagi Grimberg7bfca0c2018-01-25 13:56:46 +02003403 if (inet_addr_is_any((struct sockaddr *)&np->np_sockaddr))
Andy Grover69d75572015-08-24 10:26:04 -07003404 sockaddr = &conn->local_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003405 else
Andy Grover69d75572015-08-24 10:26:04 -07003406 sockaddr = &np->np_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003407
Andy Grover69d75572015-08-24 10:26:04 -07003408 len = sprintf(buf, "TargetAddress="
3409 "%pISpc,%hu",
3410 sockaddr,
3411 tpg->tpgt);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003412 len += 1;
3413
3414 if ((len + payload_len) > buffer_len) {
3415 spin_unlock(&tpg->tpg_np_lock);
3416 spin_unlock(&tiqn->tiqn_tpg_lock);
3417 end_of_buf = 1;
3418 goto eob;
3419 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003420
3421 if (skip_bytes && len <= skip_bytes) {
3422 skip_bytes -= len;
3423 } else {
3424 memcpy(payload + payload_len, buf, len);
3425 payload_len += len;
3426 if (len > skip_bytes)
3427 skip_bytes = 0;
3428 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003429 }
3430 spin_unlock(&tpg->tpg_np_lock);
3431 }
3432 spin_unlock(&tiqn->tiqn_tpg_lock);
3433eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003434 if (end_of_buf) {
3435 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003436 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003437 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003438
Andy Grover8060b8d2015-01-09 15:13:08 -08003439 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003440 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003441 }
3442 spin_unlock(&tiqn_lock);
3443
3444 cmd->buf_ptr = payload;
3445
3446 return payload_len;
3447}
3448
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003449int
3450iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003451 struct iscsi_text_rsp *hdr,
3452 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003453{
3454 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003455 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003456
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003457 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3458 cmd->read_data_done,
3459 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003460 if (text_length < 0)
3461 return text_length;
3462
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003463 if (completed) {
Varun Prakash310d40a2017-07-23 20:03:45 +05303464 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003465 } else {
Varun Prakash310d40a2017-07-23 20:03:45 +05303466 hdr->flags = ISCSI_FLAG_TEXT_CONTINUE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003467 cmd->read_data_done += text_length;
3468 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3469 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3470 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003471 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003472 padding = ((-text_length) & 3);
3473 hton24(hdr->dlength, text_length);
3474 hdr->itt = cmd->init_task_tag;
3475 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3476 cmd->stat_sn = conn->stat_sn++;
3477 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3478
3479 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003480 /*
3481 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3482 * correctly increment MaxCmdSN for each response answering a
3483 * non immediate text request with a valid CmdSN.
3484 */
3485 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003486 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003487 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003488
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003489 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3490 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3491 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3492 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3493 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003494
3495 return text_length + padding;
3496}
3497EXPORT_SYMBOL(iscsit_build_text_rsp);
3498
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003499static int iscsit_send_text_rsp(
3500 struct iscsi_cmd *cmd,
3501 struct iscsi_conn *conn)
3502{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003503 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Varun Prakash2854bb22016-04-20 00:00:08 +05303504 int text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003505
Varun Prakash864e5042016-04-20 00:00:15 +05303506 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3507 conn->conn_transport->transport_type);
Varun Prakash2854bb22016-04-20 00:00:08 +05303508 if (text_length < 0)
3509 return text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003510
Varun Prakash2854bb22016-04-20 00:00:08 +05303511 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3512 cmd->buf_ptr,
3513 text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003514}
3515
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003516void
3517iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3518 struct iscsi_reject *hdr)
3519{
3520 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003521 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003522 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3523 hton24(hdr->dlength, ISCSI_HDR_LEN);
3524 hdr->ffffffff = cpu_to_be32(0xffffffff);
3525 cmd->stat_sn = conn->stat_sn++;
3526 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3527 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003528 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003529
3530}
3531EXPORT_SYMBOL(iscsit_build_reject);
3532
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003533static int iscsit_send_reject(
3534 struct iscsi_cmd *cmd,
3535 struct iscsi_conn *conn)
3536{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003537 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003538
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003539 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003540
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003541 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3542 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3543
Varun Prakash2854bb22016-04-20 00:00:08 +05303544 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3545 cmd->buf_ptr,
3546 ISCSI_HDR_LEN);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003547}
3548
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003549void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3550{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003551 int ord, cpu;
3552 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003553 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3554 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003555 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003556 * Here we use bitmap_id to determine which CPU that this
3557 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003558 * execute upon.
3559 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003560 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003561 for_each_online_cpu(cpu) {
3562 if (ord-- == 0) {
3563 cpumask_set_cpu(cpu, conn->conn_cpumask);
3564 return;
3565 }
3566 }
3567 /*
3568 * This should never be reached..
3569 */
3570 dump_stack();
3571 cpumask_setall(conn->conn_cpumask);
3572}
3573
Varun Prakashd2faaef2016-04-20 00:00:19 +05303574int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003575iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003576{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003577 int ret;
3578
3579 switch (state) {
3580 case ISTATE_SEND_R2T:
3581 ret = iscsit_send_r2t(cmd, conn);
3582 if (ret < 0)
3583 goto err;
3584 break;
3585 case ISTATE_REMOVE:
3586 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003587 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003588 spin_unlock_bh(&conn->cmd_lock);
3589
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003590 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003591 break;
3592 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3593 iscsit_mod_nopin_response_timer(conn);
3594 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3595 if (ret < 0)
3596 goto err;
3597 break;
3598 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3599 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3600 if (ret < 0)
3601 goto err;
3602 break;
3603 default:
3604 pr_err("Unknown Opcode: 0x%02x ITT:"
3605 " 0x%08x, i_state: %d on CID: %hu\n",
3606 cmd->iscsi_opcode, cmd->init_task_tag, state,
3607 conn->cid);
3608 goto err;
3609 }
3610
3611 return 0;
3612
3613err:
3614 return -1;
3615}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303616EXPORT_SYMBOL(iscsit_immediate_queue);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003617
3618static int
3619iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3620{
3621 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003622 struct iscsi_queue_req *qr;
3623 struct iscsi_cmd *cmd;
3624 u8 state;
3625 int ret;
3626
3627 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3628 atomic_set(&conn->check_immediate_queue, 0);
3629 cmd = qr->cmd;
3630 state = qr->state;
3631 kmem_cache_free(lio_qr_cache, qr);
3632
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003633 ret = t->iscsit_immediate_queue(conn, cmd, state);
3634 if (ret < 0)
3635 return ret;
3636 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003637
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003638 return 0;
3639}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003640
Varun Prakashd2faaef2016-04-20 00:00:19 +05303641int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003642iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3643{
3644 int ret;
3645
3646check_rsp_state:
3647 switch (state) {
3648 case ISTATE_SEND_DATAIN:
3649 ret = iscsit_send_datain(cmd, conn);
3650 if (ret < 0)
3651 goto err;
3652 else if (!ret)
3653 /* more drs */
3654 goto check_rsp_state;
3655 else if (ret == 1) {
3656 /* all done */
3657 spin_lock_bh(&cmd->istate_lock);
3658 cmd->i_state = ISTATE_SENT_STATUS;
3659 spin_unlock_bh(&cmd->istate_lock);
3660
3661 if (atomic_read(&conn->check_immediate_queue))
3662 return 1;
3663
3664 return 0;
3665 } else if (ret == 2) {
3666 /* Still must send status,
3667 SCF_TRANSPORT_TASK_SENSE was set */
3668 spin_lock_bh(&cmd->istate_lock);
3669 cmd->i_state = ISTATE_SEND_STATUS;
3670 spin_unlock_bh(&cmd->istate_lock);
3671 state = ISTATE_SEND_STATUS;
3672 goto check_rsp_state;
3673 }
3674
3675 break;
3676 case ISTATE_SEND_STATUS:
3677 case ISTATE_SEND_STATUS_RECOVERY:
3678 ret = iscsit_send_response(cmd, conn);
3679 break;
3680 case ISTATE_SEND_LOGOUTRSP:
3681 ret = iscsit_send_logout(cmd, conn);
3682 break;
3683 case ISTATE_SEND_ASYNCMSG:
3684 ret = iscsit_send_conn_drop_async_message(
3685 cmd, conn);
3686 break;
3687 case ISTATE_SEND_NOPIN:
3688 ret = iscsit_send_nopin(cmd, conn);
3689 break;
3690 case ISTATE_SEND_REJECT:
3691 ret = iscsit_send_reject(cmd, conn);
3692 break;
3693 case ISTATE_SEND_TASKMGTRSP:
3694 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3695 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003696 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003697 ret = iscsit_tmr_post_handler(cmd, conn);
3698 if (ret != 0)
3699 iscsit_fall_back_to_erl0(conn->sess);
3700 break;
3701 case ISTATE_SEND_TEXTRSP:
3702 ret = iscsit_send_text_rsp(cmd, conn);
3703 break;
3704 default:
3705 pr_err("Unknown Opcode: 0x%02x ITT:"
3706 " 0x%08x, i_state: %d on CID: %hu\n",
3707 cmd->iscsi_opcode, cmd->init_task_tag,
3708 state, conn->cid);
3709 goto err;
3710 }
3711 if (ret < 0)
3712 goto err;
3713
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003714 switch (state) {
3715 case ISTATE_SEND_LOGOUTRSP:
3716 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003717 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003718 /* fall through */
3719 case ISTATE_SEND_STATUS:
3720 case ISTATE_SEND_ASYNCMSG:
3721 case ISTATE_SEND_NOPIN:
3722 case ISTATE_SEND_STATUS_RECOVERY:
3723 case ISTATE_SEND_TEXTRSP:
3724 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003725 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003726 spin_lock_bh(&cmd->istate_lock);
3727 cmd->i_state = ISTATE_SENT_STATUS;
3728 spin_unlock_bh(&cmd->istate_lock);
3729 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003730 default:
3731 pr_err("Unknown Opcode: 0x%02x ITT:"
3732 " 0x%08x, i_state: %d on CID: %hu\n",
3733 cmd->iscsi_opcode, cmd->init_task_tag,
3734 cmd->i_state, conn->cid);
3735 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003736 }
3737
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003738 if (atomic_read(&conn->check_immediate_queue))
3739 return 1;
3740
Andy Grover6f3c0e62012-04-03 15:51:09 -07003741 return 0;
3742
3743err:
3744 return -1;
3745}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303746EXPORT_SYMBOL(iscsit_response_queue);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003747
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003748static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003749{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003750 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003751 struct iscsi_queue_req *qr;
3752 struct iscsi_cmd *cmd;
3753 u8 state;
3754 int ret;
3755
3756 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3757 cmd = qr->cmd;
3758 state = qr->state;
3759 kmem_cache_free(lio_qr_cache, qr);
3760
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003761 ret = t->iscsit_response_queue(conn, cmd, state);
3762 if (ret == 1 || ret < 0)
3763 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003764 }
3765
3766 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003767}
3768
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003769int iscsi_target_tx_thread(void *arg)
3770{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003771 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003772 struct iscsi_conn *conn = arg;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003773 bool conn_freed = false;
3774
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003775 /*
3776 * Allow ourselves to be interrupted by SIGINT so that a
3777 * connection recovery / failure event can be triggered externally.
3778 */
3779 allow_signal(SIGINT);
3780
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003781 while (!kthread_should_stop()) {
3782 /*
3783 * Ensure that both TX and RX per connection kthreads
3784 * are scheduled to run on the same CPU.
3785 */
3786 iscsit_thread_check_cpumask(conn, current, 1);
3787
Roland Dreierd5627ac2012-10-31 09:16:46 -07003788 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003789 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003790
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003791 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003792 goto transport_err;
3793
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003794get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003795 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003796 if (ret < 0)
3797 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003798
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003799 ret = iscsit_handle_response_queue(conn);
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003800 if (ret == 1) {
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003801 goto get_immediate;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003802 } else if (ret == -ECONNRESET) {
3803 conn_freed = true;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003804 goto out;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003805 } else if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003806 goto transport_err;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003807 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003808 }
3809
3810transport_err:
Nicholas Bellingere5419862015-07-22 23:14:19 -07003811 /*
3812 * Avoid the normal connection failure code-path if this connection
3813 * is still within LOGIN mode, and iscsi_np process context is
3814 * responsible for cleaning up the early connection failure.
3815 */
3816 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003817 iscsit_take_action_for_connection_exit(conn, &conn_freed);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003818out:
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08003819 if (!conn_freed) {
3820 while (!kthread_should_stop()) {
3821 msleep(100);
3822 }
3823 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003824 return 0;
3825}
3826
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003827static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3828{
3829 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3830 struct iscsi_cmd *cmd;
3831 int ret = 0;
3832
3833 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3834 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003835 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003836 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003837 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003838
3839 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3840 break;
3841 case ISCSI_OP_SCSI_DATA_OUT:
3842 ret = iscsit_handle_data_out(conn, buf);
3843 break;
3844 case ISCSI_OP_NOOP_OUT:
3845 cmd = NULL;
3846 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003847 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003848 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003849 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003850 }
3851 ret = iscsit_handle_nop_out(conn, cmd, buf);
3852 break;
3853 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003854 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003855 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003856 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003857
3858 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3859 break;
3860 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003861 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3862 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3863 if (!cmd)
3864 goto reject;
3865 } else {
3866 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3867 if (!cmd)
3868 goto reject;
3869 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003870
3871 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003872 break;
3873 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003874 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003875 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003876 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003877
3878 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3879 if (ret > 0)
3880 wait_for_completion_timeout(&conn->conn_logout_comp,
3881 SECONDS_FOR_LOGOUT_COMP * HZ);
3882 break;
3883 case ISCSI_OP_SNACK:
3884 ret = iscsit_handle_snack(conn, buf);
3885 break;
3886 default:
3887 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3888 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3889 pr_err("Cannot recover from unknown"
3890 " opcode while ERL=0, closing iSCSI connection.\n");
3891 return -1;
3892 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02003893 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3894 " closing iSCSI connection.\n");
3895 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003896 break;
3897 }
3898
3899 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003900reject:
3901 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003902}
3903
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08003904static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3905{
3906 bool ret;
3907
3908 spin_lock_bh(&conn->state_lock);
3909 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3910 spin_unlock_bh(&conn->state_lock);
3911
3912 return ret;
3913}
3914
Varun Prakashe8205cc2016-04-20 00:00:11 +05303915static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003916{
Varun Prakashe8205cc2016-04-20 00:00:11 +05303917 int ret;
Laura Abbott679fcae2018-09-04 11:47:40 -07003918 u8 *buffer, opcode;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003919 u32 checksum = 0, digest = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003920 struct kvec iov;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003921
Laura Abbott679fcae2018-09-04 11:47:40 -07003922 buffer = kcalloc(ISCSI_HDR_LEN, sizeof(*buffer), GFP_KERNEL);
3923 if (!buffer)
3924 return;
3925
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003926 while (!kthread_should_stop()) {
3927 /*
3928 * Ensure that both TX and RX per connection kthreads
3929 * are scheduled to run on the same CPU.
3930 */
3931 iscsit_thread_check_cpumask(conn, current, 0);
3932
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003933 memset(&iov, 0, sizeof(struct kvec));
3934
3935 iov.iov_base = buffer;
3936 iov.iov_len = ISCSI_HDR_LEN;
3937
3938 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3939 if (ret != ISCSI_HDR_LEN) {
3940 iscsit_rx_thread_wait_for_tcp(conn);
Laura Abbott679fcae2018-09-04 11:47:40 -07003941 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003942 }
3943
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003944 if (conn->conn_ops->HeaderDigest) {
3945 iov.iov_base = &digest;
3946 iov.iov_len = ISCSI_CRC_LEN;
3947
3948 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3949 if (ret != ISCSI_CRC_LEN) {
3950 iscsit_rx_thread_wait_for_tcp(conn);
Laura Abbott679fcae2018-09-04 11:47:40 -07003951 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003952 }
3953
Bart Van Asschee1dfb212017-10-31 11:03:16 -07003954 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer,
3955 ISCSI_HDR_LEN, 0, NULL,
3956 &checksum);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003957
3958 if (digest != checksum) {
3959 pr_err("HeaderDigest CRC32C failed,"
3960 " received 0x%08x, computed 0x%08x\n",
3961 digest, checksum);
3962 /*
3963 * Set the PDU to 0xff so it will intentionally
3964 * hit default in the switch below.
3965 */
3966 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003967 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003968 } else {
3969 pr_debug("Got HeaderDigest CRC32C"
3970 " 0x%08x\n", checksum);
3971 }
3972 }
3973
3974 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
Laura Abbott679fcae2018-09-04 11:47:40 -07003975 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003976
3977 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3978
3979 if (conn->sess->sess_ops->SessionType &&
3980 ((!(opcode & ISCSI_OP_TEXT)) ||
3981 (!(opcode & ISCSI_OP_LOGOUT)))) {
3982 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3983 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07003984 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
3985 buffer);
Laura Abbott679fcae2018-09-04 11:47:40 -07003986 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003987 }
3988
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003989 ret = iscsi_target_rx_opcode(conn, buffer);
3990 if (ret < 0)
Laura Abbott679fcae2018-09-04 11:47:40 -07003991 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003992 }
Laura Abbott679fcae2018-09-04 11:47:40 -07003993
3994 kfree(buffer);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303995}
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003996
Varun Prakashe8205cc2016-04-20 00:00:11 +05303997int iscsi_target_rx_thread(void *arg)
3998{
3999 int rc;
4000 struct iscsi_conn *conn = arg;
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004001 bool conn_freed = false;
Varun Prakashe8205cc2016-04-20 00:00:11 +05304002
4003 /*
4004 * Allow ourselves to be interrupted by SIGINT so that a
4005 * connection recovery / failure event can be triggered externally.
4006 */
4007 allow_signal(SIGINT);
4008 /*
4009 * Wait for iscsi_post_login_handler() to complete before allowing
4010 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4011 */
4012 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4013 if (rc < 0 || iscsi_target_check_conn_state(conn))
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004014 goto out;
Varun Prakashe8205cc2016-04-20 00:00:11 +05304015
4016 if (!conn->conn_transport->iscsit_get_rx_pdu)
4017 return 0;
4018
4019 conn->conn_transport->iscsit_get_rx_pdu(conn);
4020
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004021 if (!signal_pending(current))
4022 atomic_set(&conn->transport_failed, 1);
Jiang Yi5e0cf5e2017-05-16 17:57:55 +08004023 iscsit_take_action_for_connection_exit(conn, &conn_freed);
4024
4025out:
4026 if (!conn_freed) {
4027 while (!kthread_should_stop()) {
4028 msleep(100);
4029 }
4030 }
4031
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004032 return 0;
4033}
4034
4035static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4036{
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004037 LIST_HEAD(tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004038 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4039 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004040 /*
4041 * We expect this function to only ever be called from either RX or TX
4042 * thread context via iscsit_close_connection() once the other context
4043 * has been reset -> returned sleeping pre-handler state.
4044 */
4045 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004046 list_splice_init(&conn->conn_cmd_list, &tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004047
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004048 list_for_each_entry(cmd, &tmp_list, i_conn_node) {
4049 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004050
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004051 if (se_cmd->se_tfo != NULL) {
Bart Van Asschef4a9fd52019-01-25 10:34:56 -08004052 spin_lock_irq(&se_cmd->t_state_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004053 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
Bart Van Asschef4a9fd52019-01-25 10:34:56 -08004054 spin_unlock_irq(&se_cmd->t_state_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004055 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004056 }
4057 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004058
4059 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4060 list_del_init(&cmd->i_conn_node);
4061
4062 iscsit_increment_maxcmdsn(cmd, sess);
4063 iscsit_free_cmd(cmd, true);
4064
4065 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004066}
4067
4068static void iscsit_stop_timers_for_cmds(
4069 struct iscsi_conn *conn)
4070{
4071 struct iscsi_cmd *cmd;
4072
4073 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004074 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004075 if (cmd->data_direction == DMA_TO_DEVICE)
4076 iscsit_stop_dataout_timer(cmd);
4077 }
4078 spin_unlock_bh(&conn->cmd_lock);
4079}
4080
4081int iscsit_close_connection(
4082 struct iscsi_conn *conn)
4083{
4084 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4085 struct iscsi_session *sess = conn->sess;
4086
4087 pr_debug("Closing iSCSI connection CID %hu on SID:"
4088 " %u\n", conn->cid, sess->sid);
4089 /*
Varun Prakashb4869ee2016-04-20 00:00:18 +05304090 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4091 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4092 * sleeping and the logout response never got sent because the
4093 * connection failed.
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004094 *
4095 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4096 * to signal logout response TX interrupt completion. Go ahead and skip
4097 * this for iser since isert_rx_opcode() does not wait on logout failure,
4098 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004099 */
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07004100 if (!conn->conn_transport->rdma_shutdown)
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004101 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004102
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004103 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4104 if (conn->tx_thread &&
4105 cmpxchg(&conn->tx_thread_active, true, false)) {
4106 send_sig(SIGINT, conn->tx_thread, 1);
4107 kthread_stop(conn->tx_thread);
4108 }
4109 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4110 if (conn->rx_thread &&
4111 cmpxchg(&conn->rx_thread_active, true, false)) {
4112 send_sig(SIGINT, conn->rx_thread, 1);
4113 kthread_stop(conn->rx_thread);
4114 }
4115 }
4116
4117 spin_lock(&iscsit_global->ts_bitmap_lock);
4118 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4119 get_order(1));
4120 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004121
4122 iscsit_stop_timers_for_cmds(conn);
4123 iscsit_stop_nopin_response_timer(conn);
4124 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004125
Bart Van Asschec66b2b52020-02-12 21:08:59 -08004126 if (conn->conn_transport->iscsit_wait_conn)
4127 conn->conn_transport->iscsit_wait_conn(conn);
4128
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004129 /*
4130 * During Connection recovery drop unacknowledged out of order
4131 * commands for this connection, and prepare the other commands
Bart Van Assche53c561d2016-12-23 14:40:24 +01004132 * for reallegiance.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004133 *
4134 * During normal operation clear the out of order commands (but
4135 * do not free the struct iscsi_ooo_cmdsn's) and release all
4136 * struct iscsi_cmds.
4137 */
4138 if (atomic_read(&conn->connection_recovery)) {
4139 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
Bart Van Assche53c561d2016-12-23 14:40:24 +01004140 iscsit_prepare_cmds_for_reallegiance(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004141 } else {
4142 iscsit_clear_ooo_cmdsns_for_conn(conn);
4143 iscsit_release_commands_from_conn(conn);
4144 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004145 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004146
4147 /*
4148 * Handle decrementing session or connection usage count if
4149 * a logout response was not able to be sent because the
4150 * connection failed. Fall back to Session Recovery here.
4151 */
4152 if (atomic_read(&conn->conn_logout_remove)) {
4153 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4154 iscsit_dec_conn_usage_count(conn);
4155 iscsit_dec_session_usage_count(sess);
4156 }
4157 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4158 iscsit_dec_conn_usage_count(conn);
4159
4160 atomic_set(&conn->conn_logout_remove, 0);
4161 atomic_set(&sess->session_reinstatement, 0);
4162 atomic_set(&sess->session_fall_back_to_erl0, 1);
4163 }
4164
4165 spin_lock_bh(&sess->conn_lock);
4166 list_del(&conn->conn_list);
4167
4168 /*
4169 * Attempt to let the Initiator know this connection failed by
4170 * sending an Connection Dropped Async Message on another
4171 * active connection.
4172 */
4173 if (atomic_read(&conn->connection_recovery))
4174 iscsit_build_conn_drop_async_message(conn);
4175
4176 spin_unlock_bh(&sess->conn_lock);
4177
4178 /*
4179 * If connection reinstatement is being performed on this connection,
4180 * up the connection reinstatement semaphore that is being blocked on
4181 * in iscsit_cause_connection_reinstatement().
4182 */
4183 spin_lock_bh(&conn->state_lock);
4184 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4185 spin_unlock_bh(&conn->state_lock);
4186 complete(&conn->conn_wait_comp);
4187 wait_for_completion(&conn->conn_post_wait_comp);
4188 spin_lock_bh(&conn->state_lock);
4189 }
4190
4191 /*
4192 * If connection reinstatement is being performed on this connection
4193 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4194 * connection wait rcfr semaphore that is being blocked on
4195 * an iscsit_connection_reinstatement_rcfr().
4196 */
4197 if (atomic_read(&conn->connection_wait_rcfr)) {
4198 spin_unlock_bh(&conn->state_lock);
4199 complete(&conn->conn_wait_rcfr_comp);
4200 wait_for_completion(&conn->conn_post_wait_comp);
4201 spin_lock_bh(&conn->state_lock);
4202 }
4203 atomic_set(&conn->connection_reinstatement, 1);
4204 spin_unlock_bh(&conn->state_lock);
4205
4206 /*
4207 * If any other processes are accessing this connection pointer we
4208 * must wait until they have completed.
4209 */
4210 iscsit_check_conn_usage_count(conn);
Bart Van Asscheb0aede22019-11-13 14:05:08 -08004211 target_sess_cmd_list_set_waiting(sess->se_sess);
4212 target_wait_for_sess_cmds(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004213
Herbert Xu69110e32016-01-24 21:19:52 +08004214 ahash_request_free(conn->conn_tx_hash);
4215 if (conn->conn_rx_hash) {
4216 struct crypto_ahash *tfm;
4217
4218 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4219 ahash_request_free(conn->conn_rx_hash);
4220 crypto_free_ahash(tfm);
4221 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004222
Al Virobf6932f2012-07-21 08:55:18 +01004223 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004224 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004225
4226 if (conn->conn_transport->iscsit_free_conn)
4227 conn->conn_transport->iscsit_free_conn(conn);
4228
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004229 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4230 conn->conn_state = TARG_CONN_STATE_FREE;
Mike Christie05a86e72018-08-27 14:45:16 -05004231 iscsit_free_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004232
4233 spin_lock_bh(&sess->conn_lock);
4234 atomic_dec(&sess->nconn);
4235 pr_debug("Decremented iSCSI connection count to %hu from node:"
4236 " %s\n", atomic_read(&sess->nconn),
4237 sess->sess_ops->InitiatorName);
4238 /*
4239 * Make sure that if one connection fails in an non ERL=2 iSCSI
4240 * Session that they all fail.
4241 */
4242 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4243 !atomic_read(&sess->session_logout))
4244 atomic_set(&sess->session_fall_back_to_erl0, 1);
4245
4246 /*
4247 * If this was not the last connection in the session, and we are
4248 * performing session reinstatement or falling back to ERL=0, call
4249 * iscsit_stop_session() without sleeping to shutdown the other
4250 * active connections.
4251 */
4252 if (atomic_read(&sess->nconn)) {
4253 if (!atomic_read(&sess->session_reinstatement) &&
4254 !atomic_read(&sess->session_fall_back_to_erl0)) {
4255 spin_unlock_bh(&sess->conn_lock);
4256 return 0;
4257 }
4258 if (!atomic_read(&sess->session_stop_active)) {
4259 atomic_set(&sess->session_stop_active, 1);
4260 spin_unlock_bh(&sess->conn_lock);
4261 iscsit_stop_session(sess, 0, 0);
4262 return 0;
4263 }
4264 spin_unlock_bh(&sess->conn_lock);
4265 return 0;
4266 }
4267
4268 /*
4269 * If this was the last connection in the session and one of the
4270 * following is occurring:
4271 *
4272 * Session Reinstatement is not being performed, and are falling back
4273 * to ERL=0 call iscsit_close_session().
4274 *
4275 * Session Logout was requested. iscsit_close_session() will be called
4276 * elsewhere.
4277 *
4278 * Session Continuation is not being performed, start the Time2Retain
4279 * handler and check if sleep_on_sess_wait_sem is active.
4280 */
4281 if (!atomic_read(&sess->session_reinstatement) &&
4282 atomic_read(&sess->session_fall_back_to_erl0)) {
4283 spin_unlock_bh(&sess->conn_lock);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004284 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004285
4286 return 0;
4287 } else if (atomic_read(&sess->session_logout)) {
4288 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4289 sess->session_state = TARG_SESS_STATE_FREE;
4290 spin_unlock_bh(&sess->conn_lock);
4291
4292 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4293 complete(&sess->session_wait_comp);
4294
4295 return 0;
4296 } else {
4297 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4298 sess->session_state = TARG_SESS_STATE_FAILED;
4299
4300 if (!atomic_read(&sess->session_continuation)) {
4301 spin_unlock_bh(&sess->conn_lock);
4302 iscsit_start_time2retain_handler(sess);
4303 } else
4304 spin_unlock_bh(&sess->conn_lock);
4305
4306 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4307 complete(&sess->session_wait_comp);
4308
4309 return 0;
4310 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004311}
4312
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004313/*
4314 * If the iSCSI Session for the iSCSI Initiator Node exists,
4315 * forcefully shutdown the iSCSI NEXUS.
4316 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004317int iscsit_close_session(struct iscsi_session *sess)
4318{
Andy Grover60bfcf82013-10-09 11:05:58 -07004319 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004320 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4321
4322 if (atomic_read(&sess->nconn)) {
4323 pr_err("%d connection(s) still exist for iSCSI session"
4324 " to %s\n", atomic_read(&sess->nconn),
4325 sess->sess_ops->InitiatorName);
4326 BUG();
4327 }
4328
4329 spin_lock_bh(&se_tpg->session_lock);
4330 atomic_set(&sess->session_logout, 1);
4331 atomic_set(&sess->session_reinstatement, 1);
4332 iscsit_stop_time2retain_timer(sess);
4333 spin_unlock_bh(&se_tpg->session_lock);
4334
4335 /*
4336 * transport_deregister_session_configfs() will clear the
4337 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4338 * can be setting it again with __transport_register_session() in
4339 * iscsi_post_login_handler() again after the iscsit_stop_session()
4340 * completes in iscsi_np context.
4341 */
4342 transport_deregister_session_configfs(sess->se_sess);
4343
4344 /*
4345 * If any other processes are accessing this session pointer we must
4346 * wait until they have completed. If we are in an interrupt (the
4347 * time2retain handler) and contain and active session usage count we
4348 * restart the timer and exit.
4349 */
4350 if (!in_interrupt()) {
4351 if (iscsit_check_session_usage_count(sess) == 1)
4352 iscsit_stop_session(sess, 1, 1);
4353 } else {
4354 if (iscsit_check_session_usage_count(sess) == 2) {
4355 atomic_set(&sess->session_logout, 0);
4356 iscsit_start_time2retain_handler(sess);
4357 return 0;
4358 }
4359 }
4360
4361 transport_deregister_session(sess->se_sess);
4362
4363 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4364 iscsit_free_connection_recovery_entires(sess);
4365
4366 iscsit_free_all_ooo_cmdsns(sess);
4367
4368 spin_lock_bh(&se_tpg->session_lock);
4369 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4370 sess->session_state = TARG_SESS_STATE_FREE;
4371 pr_debug("Released iSCSI session from node: %s\n",
4372 sess->sess_ops->InitiatorName);
4373 tpg->nsessions--;
4374 if (tpg->tpg_tiqn)
4375 tpg->tpg_tiqn->tiqn_nsessions--;
4376
4377 pr_debug("Decremented number of active iSCSI Sessions on"
4378 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4379
Matthew Wilcox31ff0ce2018-06-19 01:23:04 -04004380 ida_free(&sess_ida, sess->session_index);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004381 kfree(sess->sess_ops);
4382 sess->sess_ops = NULL;
4383 spin_unlock_bh(&se_tpg->session_lock);
4384
4385 kfree(sess);
4386 return 0;
4387}
4388
4389static void iscsit_logout_post_handler_closesession(
4390 struct iscsi_conn *conn)
4391{
4392 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004393 int sleep = 1;
4394 /*
4395 * Traditional iscsi/tcp will invoke this logic from TX thread
4396 * context during session logout, so clear tx_thread_active and
4397 * sleep if iscsit_close_connection() has not already occured.
4398 *
4399 * Since iser-target invokes this logic from it's own workqueue,
4400 * always sleep waiting for RX/TX thread shutdown to complete
4401 * within iscsit_close_connection().
4402 */
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004403 if (!conn->conn_transport->rdma_shutdown) {
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004404 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004405 if (!sleep)
4406 return;
4407 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004408
4409 atomic_set(&conn->conn_logout_remove, 0);
4410 complete(&conn->conn_logout_comp);
4411
4412 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004413 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004414 iscsit_dec_session_usage_count(sess);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004415 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004416}
4417
4418static void iscsit_logout_post_handler_samecid(
4419 struct iscsi_conn *conn)
4420{
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004421 int sleep = 1;
4422
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004423 if (!conn->conn_transport->rdma_shutdown) {
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004424 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellinger105fa2f2017-06-03 05:35:47 -07004425 if (!sleep)
4426 return;
4427 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004428
4429 atomic_set(&conn->conn_logout_remove, 0);
4430 complete(&conn->conn_logout_comp);
4431
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004432 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004433 iscsit_dec_conn_usage_count(conn);
4434}
4435
4436static void iscsit_logout_post_handler_diffcid(
4437 struct iscsi_conn *conn,
4438 u16 cid)
4439{
4440 struct iscsi_conn *l_conn;
4441 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004442 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004443
4444 if (!sess)
4445 return;
4446
4447 spin_lock_bh(&sess->conn_lock);
4448 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4449 if (l_conn->cid == cid) {
4450 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004451 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004452 break;
4453 }
4454 }
4455 spin_unlock_bh(&sess->conn_lock);
4456
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004457 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004458 return;
4459
4460 if (l_conn->sock)
4461 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4462
4463 spin_lock_bh(&l_conn->state_lock);
4464 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4465 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4466 spin_unlock_bh(&l_conn->state_lock);
4467
4468 iscsit_cause_connection_reinstatement(l_conn, 1);
4469 iscsit_dec_conn_usage_count(l_conn);
4470}
4471
4472/*
4473 * Return of 0 causes the TX thread to restart.
4474 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004475int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004476 struct iscsi_cmd *cmd,
4477 struct iscsi_conn *conn)
4478{
4479 int ret = 0;
4480
4481 switch (cmd->logout_reason) {
4482 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4483 switch (cmd->logout_response) {
4484 case ISCSI_LOGOUT_SUCCESS:
4485 case ISCSI_LOGOUT_CLEANUP_FAILED:
4486 default:
4487 iscsit_logout_post_handler_closesession(conn);
4488 break;
4489 }
4490 ret = 0;
4491 break;
4492 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4493 if (conn->cid == cmd->logout_cid) {
4494 switch (cmd->logout_response) {
4495 case ISCSI_LOGOUT_SUCCESS:
4496 case ISCSI_LOGOUT_CLEANUP_FAILED:
4497 default:
4498 iscsit_logout_post_handler_samecid(conn);
4499 break;
4500 }
4501 ret = 0;
4502 } else {
4503 switch (cmd->logout_response) {
4504 case ISCSI_LOGOUT_SUCCESS:
4505 iscsit_logout_post_handler_diffcid(conn,
4506 cmd->logout_cid);
4507 break;
4508 case ISCSI_LOGOUT_CID_NOT_FOUND:
4509 case ISCSI_LOGOUT_CLEANUP_FAILED:
4510 default:
4511 break;
4512 }
4513 ret = 1;
4514 }
4515 break;
4516 case ISCSI_LOGOUT_REASON_RECOVERY:
4517 switch (cmd->logout_response) {
4518 case ISCSI_LOGOUT_SUCCESS:
4519 case ISCSI_LOGOUT_CID_NOT_FOUND:
4520 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4521 case ISCSI_LOGOUT_CLEANUP_FAILED:
4522 default:
4523 break;
4524 }
4525 ret = 1;
4526 break;
4527 default:
4528 break;
4529
4530 }
4531 return ret;
4532}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004533EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004534
4535void iscsit_fail_session(struct iscsi_session *sess)
4536{
4537 struct iscsi_conn *conn;
4538
4539 spin_lock_bh(&sess->conn_lock);
4540 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4541 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4542 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4543 }
4544 spin_unlock_bh(&sess->conn_lock);
4545
4546 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4547 sess->session_state = TARG_SESS_STATE_FAILED;
4548}
4549
4550int iscsit_free_session(struct iscsi_session *sess)
4551{
4552 u16 conn_count = atomic_read(&sess->nconn);
4553 struct iscsi_conn *conn, *conn_tmp = NULL;
4554 int is_last;
4555
4556 spin_lock_bh(&sess->conn_lock);
4557 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4558
4559 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4560 conn_list) {
4561 if (conn_count == 0)
4562 break;
4563
4564 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4565 is_last = 1;
4566 } else {
4567 iscsit_inc_conn_usage_count(conn_tmp);
4568 is_last = 0;
4569 }
4570 iscsit_inc_conn_usage_count(conn);
4571
4572 spin_unlock_bh(&sess->conn_lock);
4573 iscsit_cause_connection_reinstatement(conn, 1);
4574 spin_lock_bh(&sess->conn_lock);
4575
4576 iscsit_dec_conn_usage_count(conn);
4577 if (is_last == 0)
4578 iscsit_dec_conn_usage_count(conn_tmp);
4579
4580 conn_count--;
4581 }
4582
4583 if (atomic_read(&sess->nconn)) {
4584 spin_unlock_bh(&sess->conn_lock);
4585 wait_for_completion(&sess->session_wait_comp);
4586 } else
4587 spin_unlock_bh(&sess->conn_lock);
4588
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004589 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004590 return 0;
4591}
4592
4593void iscsit_stop_session(
4594 struct iscsi_session *sess,
4595 int session_sleep,
4596 int connection_sleep)
4597{
4598 u16 conn_count = atomic_read(&sess->nconn);
4599 struct iscsi_conn *conn, *conn_tmp = NULL;
4600 int is_last;
4601
4602 spin_lock_bh(&sess->conn_lock);
4603 if (session_sleep)
4604 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4605
4606 if (connection_sleep) {
4607 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4608 conn_list) {
4609 if (conn_count == 0)
4610 break;
4611
4612 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4613 is_last = 1;
4614 } else {
4615 iscsit_inc_conn_usage_count(conn_tmp);
4616 is_last = 0;
4617 }
4618 iscsit_inc_conn_usage_count(conn);
4619
4620 spin_unlock_bh(&sess->conn_lock);
4621 iscsit_cause_connection_reinstatement(conn, 1);
4622 spin_lock_bh(&sess->conn_lock);
4623
4624 iscsit_dec_conn_usage_count(conn);
4625 if (is_last == 0)
4626 iscsit_dec_conn_usage_count(conn_tmp);
4627 conn_count--;
4628 }
4629 } else {
4630 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4631 iscsit_cause_connection_reinstatement(conn, 0);
4632 }
4633
4634 if (session_sleep && atomic_read(&sess->nconn)) {
4635 spin_unlock_bh(&sess->conn_lock);
4636 wait_for_completion(&sess->session_wait_comp);
4637 } else
4638 spin_unlock_bh(&sess->conn_lock);
4639}
4640
4641int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4642{
4643 struct iscsi_session *sess;
4644 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4645 struct se_session *se_sess, *se_sess_tmp;
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004646 LIST_HEAD(free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004647 int session_count = 0;
4648
4649 spin_lock_bh(&se_tpg->session_lock);
4650 if (tpg->nsessions && !force) {
4651 spin_unlock_bh(&se_tpg->session_lock);
4652 return -1;
4653 }
4654
4655 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4656 sess_list) {
4657 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4658
4659 spin_lock(&sess->conn_lock);
4660 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4661 atomic_read(&sess->session_logout) ||
4662 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4663 spin_unlock(&sess->conn_lock);
4664 continue;
4665 }
4666 atomic_set(&sess->session_reinstatement, 1);
Nicholas Bellinger197b8062017-04-25 10:55:12 -07004667 atomic_set(&sess->session_fall_back_to_erl0, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004668 spin_unlock(&sess->conn_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004669
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004670 list_move_tail(&se_sess->sess_list, &free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004671 }
4672 spin_unlock_bh(&se_tpg->session_lock);
4673
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004674 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4675 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4676
4677 iscsit_free_session(sess);
4678 session_count++;
4679 }
4680
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004681 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4682 " Group: %hu\n", session_count, tpg->tpgt);
4683 return 0;
4684}
4685
4686MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4687MODULE_VERSION("4.1.x");
4688MODULE_AUTHOR("nab@Linux-iSCSI.org");
4689MODULE_LICENSE("GPL");
4690
4691module_init(iscsi_target_init_module);
4692module_exit(iscsi_target_cleanup_module);