blob: 0724c8355e2932ae381f5eae52e05cb0759c6aeb [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>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000026#include <asm/unaligned.h>
Bart Van Asscheba929992015-05-08 10:11:12 +020027#include <scsi/scsi_proto.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000028#include <scsi/iscsi_proto.h>
Andy Groverd28b11692012-04-03 15:51:22 -070029#include <scsi/scsi_tcq.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000030#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050031#include <target/target_core_fabric.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000032
Sagi Grimberg67f091f2015-01-07 14:57:31 +020033#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000034#include "iscsi_target_parameters.h"
35#include "iscsi_target_seq_pdu_list.h"
Nicholas Bellingere48354c2011-07-23 06:43:04 +000036#include "iscsi_target_datain_values.h"
37#include "iscsi_target_erl0.h"
38#include "iscsi_target_erl1.h"
39#include "iscsi_target_erl2.h"
40#include "iscsi_target_login.h"
41#include "iscsi_target_tmr.h"
42#include "iscsi_target_tpg.h"
43#include "iscsi_target_util.h"
44#include "iscsi_target.h"
45#include "iscsi_target_device.h"
Sagi Grimberg67f091f2015-01-07 14:57:31 +020046#include <target/iscsi/iscsi_target_stat.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000047
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080048#include <target/iscsi/iscsi_transport.h>
49
Nicholas Bellingere48354c2011-07-23 06:43:04 +000050static LIST_HEAD(g_tiqn_list);
51static LIST_HEAD(g_np_list);
52static DEFINE_SPINLOCK(tiqn_lock);
Andy Groveree291e62014-01-24 16:18:54 -080053static DEFINE_MUTEX(np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000054
55static struct idr tiqn_idr;
56struct idr sess_idr;
57struct mutex auth_id_lock;
58spinlock_t sess_idr_lock;
59
60struct iscsit_global *iscsit_global;
61
Nicholas Bellingere48354c2011-07-23 06:43:04 +000062struct kmem_cache *lio_qr_cache;
63struct kmem_cache *lio_dr_cache;
64struct kmem_cache *lio_ooo_cache;
65struct kmem_cache *lio_r2t_cache;
66
67static int iscsit_handle_immediate_data(struct iscsi_cmd *,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -070068 struct iscsi_scsi_req *, u32);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000069
70struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
71{
72 struct iscsi_tiqn *tiqn = NULL;
73
74 spin_lock(&tiqn_lock);
75 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
76 if (!strcmp(tiqn->tiqn, buf)) {
77
78 spin_lock(&tiqn->tiqn_state_lock);
79 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
80 tiqn->tiqn_access_count++;
81 spin_unlock(&tiqn->tiqn_state_lock);
82 spin_unlock(&tiqn_lock);
83 return tiqn;
84 }
85 spin_unlock(&tiqn->tiqn_state_lock);
86 }
87 }
88 spin_unlock(&tiqn_lock);
89
90 return NULL;
91}
92
93static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
94{
95 spin_lock(&tiqn->tiqn_state_lock);
96 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
97 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
98 spin_unlock(&tiqn->tiqn_state_lock);
99 return 0;
100 }
101 spin_unlock(&tiqn->tiqn_state_lock);
102
103 return -1;
104}
105
106void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
107{
108 spin_lock(&tiqn->tiqn_state_lock);
109 tiqn->tiqn_access_count--;
110 spin_unlock(&tiqn->tiqn_state_lock);
111}
112
113/*
114 * Note that IQN formatting is expected to be done in userspace, and
115 * no explict IQN format checks are done here.
116 */
117struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
118{
119 struct iscsi_tiqn *tiqn = NULL;
120 int ret;
121
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300122 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000123 pr_err("Target IQN exceeds %d bytes\n",
124 ISCSI_IQN_LEN);
125 return ERR_PTR(-EINVAL);
126 }
127
128 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
129 if (!tiqn) {
130 pr_err("Unable to allocate struct iscsi_tiqn\n");
131 return ERR_PTR(-ENOMEM);
132 }
133
134 sprintf(tiqn->tiqn, "%s", buf);
135 INIT_LIST_HEAD(&tiqn->tiqn_list);
136 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
137 spin_lock_init(&tiqn->tiqn_state_lock);
138 spin_lock_init(&tiqn->tiqn_tpg_lock);
139 spin_lock_init(&tiqn->sess_err_stats.lock);
140 spin_lock_init(&tiqn->login_stats.lock);
141 spin_lock_init(&tiqn->logout_stats.lock);
142
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000143 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
144
Tejun Heoc9365bd2013-02-27 17:04:43 -0800145 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000146 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800147
148 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000149 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800150 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000151 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800152 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000153 kfree(tiqn);
154 return ERR_PTR(ret);
155 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800156 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000157 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800158
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000159 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800160 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000161
162 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
163
164 return tiqn;
165
166}
167
168static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
169{
170 /*
171 * Wait for accesses to said struct iscsi_tiqn to end.
172 */
173 spin_lock(&tiqn->tiqn_state_lock);
174 while (tiqn->tiqn_access_count != 0) {
175 spin_unlock(&tiqn->tiqn_state_lock);
176 msleep(10);
177 spin_lock(&tiqn->tiqn_state_lock);
178 }
179 spin_unlock(&tiqn->tiqn_state_lock);
180}
181
182void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
183{
184 /*
185 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
186 * while holding tiqn->tiqn_state_lock. This means that all subsequent
187 * attempts to access this struct iscsi_tiqn will fail from both transport
188 * fabric and control code paths.
189 */
190 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
191 pr_err("iscsit_set_tiqn_shutdown() failed\n");
192 return;
193 }
194
195 iscsit_wait_for_tiqn(tiqn);
196
197 spin_lock(&tiqn_lock);
198 list_del(&tiqn->tiqn_list);
199 idr_remove(&tiqn_idr, tiqn->tiqn_index);
200 spin_unlock(&tiqn_lock);
201
202 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
203 tiqn->tiqn);
204 kfree(tiqn);
205}
206
207int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
208{
209 int ret;
210 /*
211 * Determine if the network portal is accepting storage traffic.
212 */
213 spin_lock_bh(&np->np_thread_lock);
214 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
215 spin_unlock_bh(&np->np_thread_lock);
216 return -1;
217 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000218 spin_unlock_bh(&np->np_thread_lock);
219 /*
220 * Determine if the portal group is accepting storage traffic.
221 */
222 spin_lock_bh(&tpg->tpg_state_lock);
223 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
224 spin_unlock_bh(&tpg->tpg_state_lock);
225 return -1;
226 }
227 spin_unlock_bh(&tpg->tpg_state_lock);
228
229 /*
230 * Here we serialize access across the TIQN+TPG Tuple.
231 */
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700232 ret = down_interruptible(&tpg->np_login_sem);
Nicholas Bellingeree7619f2015-05-19 15:10:44 -0700233 if (ret != 0)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000234 return -1;
235
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700236 spin_lock_bh(&tpg->tpg_state_lock);
237 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
238 spin_unlock_bh(&tpg->tpg_state_lock);
239 up(&tpg->np_login_sem);
240 return -1;
241 }
242 spin_unlock_bh(&tpg->tpg_state_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000243
244 return 0;
245}
246
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700247void iscsit_login_kref_put(struct kref *kref)
248{
249 struct iscsi_tpg_np *tpg_np = container_of(kref,
250 struct iscsi_tpg_np, tpg_np_kref);
251
252 complete(&tpg_np->tpg_np_comp);
253}
254
255int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
256 struct iscsi_tpg_np *tpg_np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000257{
258 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
259
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700260 up(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000261
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700262 if (tpg_np)
263 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000264
265 if (tiqn)
266 iscsit_put_tiqn_for_login(tiqn);
267
268 return 0;
269}
270
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800271bool iscsit_check_np_match(
Andy Grover13a3cf02015-08-24 10:26:06 -0700272 struct sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800273 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000274 int network_transport)
275{
276 struct sockaddr_in *sock_in, *sock_in_e;
277 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800278 bool ip_match = false;
Andy Grover69d75572015-08-24 10:26:04 -0700279 u16 port, port_e;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000280
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800281 if (sockaddr->ss_family == AF_INET6) {
282 sock_in6 = (struct sockaddr_in6 *)sockaddr;
283 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
284
285 if (!memcmp(&sock_in6->sin6_addr.in6_u,
286 &sock_in6_e->sin6_addr.in6_u,
287 sizeof(struct in6_addr)))
288 ip_match = true;
289
290 port = ntohs(sock_in6->sin6_port);
Andy Grover69d75572015-08-24 10:26:04 -0700291 port_e = ntohs(sock_in6_e->sin6_port);
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800292 } else {
293 sock_in = (struct sockaddr_in *)sockaddr;
294 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
295
296 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
297 ip_match = true;
298
299 port = ntohs(sock_in->sin_port);
Andy Grover69d75572015-08-24 10:26:04 -0700300 port_e = ntohs(sock_in_e->sin_port);
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800301 }
302
Andy Grover69d75572015-08-24 10:26:04 -0700303 if (ip_match && (port_e == port) &&
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800304 (np->np_network_transport == network_transport))
305 return true;
306
307 return false;
308}
309
Andy Groveree291e62014-01-24 16:18:54 -0800310/*
311 * Called with mutex np_lock held
312 */
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800313static struct iscsi_np *iscsit_get_np(
Andy Grover13a3cf02015-08-24 10:26:06 -0700314 struct sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800315 int network_transport)
316{
317 struct iscsi_np *np;
318 bool match;
319
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000320 list_for_each_entry(np, &g_np_list, np_list) {
Andy Groveree291e62014-01-24 16:18:54 -0800321 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000322 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
Andy Groveree291e62014-01-24 16:18:54 -0800323 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000324 continue;
325 }
326
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800327 match = iscsit_check_np_match(sockaddr, np, network_transport);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200328 if (match) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000329 /*
330 * Increment the np_exports reference count now to
331 * prevent iscsit_del_np() below from being called
332 * while iscsi_tpg_add_network_portal() is called.
333 */
334 np->np_exports++;
Andy Groveree291e62014-01-24 16:18:54 -0800335 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000336 return np;
337 }
Andy Groveree291e62014-01-24 16:18:54 -0800338 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000339 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000340
341 return NULL;
342}
343
344struct iscsi_np *iscsit_add_np(
Andy Grover13a3cf02015-08-24 10:26:06 -0700345 struct sockaddr_storage *sockaddr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000346 int network_transport)
347{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000348 struct iscsi_np *np;
349 int ret;
Andy Groveree291e62014-01-24 16:18:54 -0800350
351 mutex_lock(&np_lock);
352
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000353 /*
354 * Locate the existing struct iscsi_np if already active..
355 */
356 np = iscsit_get_np(sockaddr, network_transport);
Andy Groveree291e62014-01-24 16:18:54 -0800357 if (np) {
358 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000359 return np;
Andy Groveree291e62014-01-24 16:18:54 -0800360 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000361
362 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
363 if (!np) {
364 pr_err("Unable to allocate memory for struct iscsi_np\n");
Andy Groveree291e62014-01-24 16:18:54 -0800365 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000366 return ERR_PTR(-ENOMEM);
367 }
368
369 np->np_flags |= NPF_IP_NETWORK;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000370 np->np_network_transport = network_transport;
371 spin_lock_init(&np->np_thread_lock);
372 init_completion(&np->np_restart_comp);
373 INIT_LIST_HEAD(&np->np_list);
374
375 ret = iscsi_target_setup_login_socket(np, sockaddr);
376 if (ret != 0) {
377 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800378 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000379 return ERR_PTR(ret);
380 }
381
382 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
383 if (IS_ERR(np->np_thread)) {
384 pr_err("Unable to create kthread: iscsi_np\n");
385 ret = PTR_ERR(np->np_thread);
386 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800387 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000388 return ERR_PTR(ret);
389 }
390 /*
391 * Increment the np_exports reference count now to prevent
392 * iscsit_del_np() below from being run while a new call to
393 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
394 * active. We don't need to hold np->np_thread_lock at this
395 * point because iscsi_np has not been added to g_np_list yet.
396 */
397 np->np_exports = 1;
Andy Groveree291e62014-01-24 16:18:54 -0800398 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000399
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000400 list_add_tail(&np->np_list, &g_np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800401 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000402
Andy Grover69d75572015-08-24 10:26:04 -0700403 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
404 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000405
406 return np;
407}
408
409int iscsit_reset_np_thread(
410 struct iscsi_np *np,
411 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700412 struct iscsi_portal_group *tpg,
413 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000414{
415 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000416 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
417 spin_unlock_bh(&np->np_thread_lock);
418 return 0;
419 }
420 np->np_thread_state = ISCSI_NP_THREAD_RESET;
421
422 if (np->np_thread) {
423 spin_unlock_bh(&np->np_thread_lock);
424 send_sig(SIGINT, np->np_thread, 1);
425 wait_for_completion(&np->np_restart_comp);
426 spin_lock_bh(&np->np_thread_lock);
427 }
428 spin_unlock_bh(&np->np_thread_lock);
429
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700430 if (tpg_np && shutdown) {
431 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
432
433 wait_for_completion(&tpg_np->tpg_np_comp);
434 }
435
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000436 return 0;
437}
438
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800439static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000440{
Al Virobf6932f2012-07-21 08:55:18 +0100441 if (np->np_socket)
442 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000443}
444
445int iscsit_del_np(struct iscsi_np *np)
446{
447 spin_lock_bh(&np->np_thread_lock);
448 np->np_exports--;
449 if (np->np_exports) {
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700450 np->enabled = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000451 spin_unlock_bh(&np->np_thread_lock);
452 return 0;
453 }
454 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
455 spin_unlock_bh(&np->np_thread_lock);
456
457 if (np->np_thread) {
458 /*
459 * We need to send the signal to wakeup Linux/Net
460 * which may be sleeping in sock_accept()..
461 */
462 send_sig(SIGINT, np->np_thread, 1);
463 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800464 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000465 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800466
467 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000468
Andy Groveree291e62014-01-24 16:18:54 -0800469 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000470 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800471 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000472
Andy Grover69d75572015-08-24 10:26:04 -0700473 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
474 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000475
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800476 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000477 kfree(np);
478 return 0;
479}
480
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700481static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
482static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
483
484static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
485{
486 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
487 return 0;
488}
489
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700490static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
491{
492 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
493
494 spin_lock_bh(&conn->cmd_lock);
495 if (!list_empty(&cmd->i_conn_node))
496 list_del_init(&cmd->i_conn_node);
497 spin_unlock_bh(&conn->cmd_lock);
498
499 __iscsit_free_cmd(cmd, scsi_cmd, true);
500}
501
Varun Prakash2854bb22016-04-20 00:00:08 +0530502static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
503 u32, u32, u8 *, u8 *);
504static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
505
506static int
507iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
508 const void *data_buf, u32 data_buf_len)
509{
510 struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
511 struct kvec *iov;
512 u32 niov = 0, tx_size = ISCSI_HDR_LEN;
513 int ret;
514
515 iov = &cmd->iov_misc[0];
516 iov[niov].iov_base = cmd->pdu;
517 iov[niov++].iov_len = ISCSI_HDR_LEN;
518
519 if (conn->conn_ops->HeaderDigest) {
520 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
521
522 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
523 ISCSI_HDR_LEN, 0, NULL,
524 (u8 *)header_digest);
525
526 iov[0].iov_len += ISCSI_CRC_LEN;
527 tx_size += ISCSI_CRC_LEN;
528 pr_debug("Attaching CRC32C HeaderDigest"
529 " to opcode 0x%x 0x%08x\n",
530 hdr->opcode, *header_digest);
531 }
532
533 if (data_buf_len) {
534 u32 padding = ((-data_buf_len) & 3);
535
536 iov[niov].iov_base = (void *)data_buf;
537 iov[niov++].iov_len = data_buf_len;
538 tx_size += data_buf_len;
539
540 if (padding != 0) {
541 iov[niov].iov_base = &cmd->pad_bytes;
542 iov[niov++].iov_len = padding;
543 tx_size += padding;
544 pr_debug("Attaching %u additional"
545 " padding bytes.\n", padding);
546 }
547
548 if (conn->conn_ops->DataDigest) {
549 iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
550 data_buf, data_buf_len,
551 padding,
552 (u8 *)&cmd->pad_bytes,
553 (u8 *)&cmd->data_crc);
554
555 iov[niov].iov_base = &cmd->data_crc;
556 iov[niov++].iov_len = ISCSI_CRC_LEN;
557 tx_size += ISCSI_CRC_LEN;
558 pr_debug("Attached DataDigest for %u"
559 " bytes opcode 0x%x, CRC 0x%08x\n",
560 data_buf_len, hdr->opcode, cmd->data_crc);
561 }
562 }
563
564 cmd->iov_misc_count = niov;
565 cmd->tx_size = tx_size;
566
567 ret = iscsit_send_tx_data(cmd, conn, 1);
568 if (ret < 0) {
569 iscsit_tx_thread_wait_for_tcp(conn);
570 return ret;
571 }
572
573 return 0;
574}
575
576static int iscsit_map_iovec(struct iscsi_cmd *, struct kvec *, u32, u32);
577static void iscsit_unmap_iovec(struct iscsi_cmd *);
578static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
579 u32, u32, u32, u8 *);
580static int
581iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
582 const struct iscsi_datain *datain)
583{
584 struct kvec *iov;
585 u32 iov_count = 0, tx_size = 0;
586 int ret, iov_ret;
587
588 iov = &cmd->iov_data[0];
589 iov[iov_count].iov_base = cmd->pdu;
590 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
591 tx_size += ISCSI_HDR_LEN;
592
593 if (conn->conn_ops->HeaderDigest) {
594 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
595
596 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
597 ISCSI_HDR_LEN, 0, NULL,
598 (u8 *)header_digest);
599
600 iov[0].iov_len += ISCSI_CRC_LEN;
601 tx_size += ISCSI_CRC_LEN;
602
603 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
604 *header_digest);
605 }
606
607 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
608 datain->offset, datain->length);
609 if (iov_ret < 0)
610 return -1;
611
612 iov_count += iov_ret;
613 tx_size += datain->length;
614
615 cmd->padding = ((-datain->length) & 3);
616 if (cmd->padding) {
617 iov[iov_count].iov_base = cmd->pad_bytes;
618 iov[iov_count++].iov_len = cmd->padding;
619 tx_size += cmd->padding;
620
621 pr_debug("Attaching %u padding bytes\n", cmd->padding);
622 }
623
624 if (conn->conn_ops->DataDigest) {
625 cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
626 cmd, datain->offset,
627 datain->length,
628 cmd->padding,
629 cmd->pad_bytes);
630
631 iov[iov_count].iov_base = &cmd->data_crc;
632 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
633 tx_size += ISCSI_CRC_LEN;
634
635 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
636 datain->length + cmd->padding, cmd->data_crc);
637 }
638
639 cmd->iov_data_count = iov_count;
640 cmd->tx_size = tx_size;
641
642 ret = iscsit_fe_sendpage_sg(cmd, conn);
643
644 iscsit_unmap_iovec(cmd);
645
646 if (ret < 0) {
647 iscsit_tx_thread_wait_for_tcp(conn);
648 return ret;
649 }
650
651 return 0;
652}
653
654static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
655 struct iscsi_datain_req *dr, const void *buf,
656 u32 buf_len)
657{
658 if (dr)
659 return iscsit_xmit_datain_pdu(conn, cmd, buf);
660 else
661 return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
662}
663
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700664static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
665{
666 return TARGET_PROT_NORMAL;
667}
668
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800669static struct iscsit_transport iscsi_target_transport = {
670 .name = "iSCSI/TCP",
671 .transport_type = ISCSI_TCP,
672 .owner = NULL,
673 .iscsit_setup_np = iscsit_setup_np,
674 .iscsit_accept_np = iscsit_accept_np,
675 .iscsit_free_np = iscsit_free_np,
676 .iscsit_get_login_rx = iscsit_get_login_rx,
677 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800678 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700679 .iscsit_immediate_queue = iscsit_immediate_queue,
680 .iscsit_response_queue = iscsit_response_queue,
681 .iscsit_queue_data_in = iscsit_queue_rsp,
682 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700683 .iscsit_aborted_task = iscsit_aborted_task,
Varun Prakash2854bb22016-04-20 00:00:08 +0530684 .iscsit_xmit_pdu = iscsit_xmit_pdu,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700685 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800686};
687
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000688static int __init iscsi_target_init_module(void)
689{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800690 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000691
692 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
693
694 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
695 if (!iscsit_global) {
696 pr_err("Unable to allocate memory for iscsit_global\n");
697 return -1;
698 }
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800699 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000700 mutex_init(&auth_id_lock);
701 spin_lock_init(&sess_idr_lock);
702 idr_init(&tiqn_idr);
703 idr_init(&sess_idr);
704
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200705 ret = target_register_template(&iscsi_ops);
706 if (ret)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000707 goto out;
708
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800709 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
710 iscsit_global->ts_bitmap = vzalloc(size);
711 if (!iscsit_global->ts_bitmap) {
712 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000713 goto configfs_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000714 }
715
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000716 lio_qr_cache = kmem_cache_create("lio_qr_cache",
717 sizeof(struct iscsi_queue_req),
718 __alignof__(struct iscsi_queue_req), 0, NULL);
719 if (!lio_qr_cache) {
720 pr_err("nable to kmem_cache_create() for"
721 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800722 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000723 }
724
725 lio_dr_cache = kmem_cache_create("lio_dr_cache",
726 sizeof(struct iscsi_datain_req),
727 __alignof__(struct iscsi_datain_req), 0, NULL);
728 if (!lio_dr_cache) {
729 pr_err("Unable to kmem_cache_create() for"
730 " lio_dr_cache\n");
731 goto qr_out;
732 }
733
734 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
735 sizeof(struct iscsi_ooo_cmdsn),
736 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
737 if (!lio_ooo_cache) {
738 pr_err("Unable to kmem_cache_create() for"
739 " lio_ooo_cache\n");
740 goto dr_out;
741 }
742
743 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
744 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
745 0, NULL);
746 if (!lio_r2t_cache) {
747 pr_err("Unable to kmem_cache_create() for"
748 " lio_r2t_cache\n");
749 goto ooo_out;
750 }
751
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800752 iscsit_register_transport(&iscsi_target_transport);
753
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000754 if (iscsit_load_discovery_tpg() < 0)
755 goto r2t_out;
756
757 return ret;
758r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100759 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000760 kmem_cache_destroy(lio_r2t_cache);
761ooo_out:
762 kmem_cache_destroy(lio_ooo_cache);
763dr_out:
764 kmem_cache_destroy(lio_dr_cache);
765qr_out:
766 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800767bitmap_out:
768 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000769configfs_out:
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200770 /* XXX: this probably wants it to be it's own unwind step.. */
771 if (iscsit_global->discovery_tpg)
772 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
773 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000774out:
775 kfree(iscsit_global);
776 return -ENOMEM;
777}
778
779static void __exit iscsi_target_cleanup_module(void)
780{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000781 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800782 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000783 kmem_cache_destroy(lio_qr_cache);
784 kmem_cache_destroy(lio_dr_cache);
785 kmem_cache_destroy(lio_ooo_cache);
786 kmem_cache_destroy(lio_r2t_cache);
787
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200788 /*
789 * Shutdown discovery sessions and disable discovery TPG
790 */
791 if (iscsit_global->discovery_tpg)
792 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000793
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200794 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000795
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800796 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000797 kfree(iscsit_global);
798}
799
Andy Grover8b1e1242012-04-03 15:51:12 -0700800static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700801 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000802 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700803 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000804{
805 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000806
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000807 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000808 if (!cmd)
809 return -1;
810
811 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700812 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000813
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100814 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000815 if (!cmd->buf_ptr) {
816 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700817 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000818 return -1;
819 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000820
821 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700822 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000823 spin_unlock_bh(&conn->cmd_lock);
824
825 cmd->i_state = ISTATE_SEND_REJECT;
826 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
827
Nicholas Bellingerba159912013-07-03 03:48:24 -0700828 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000829}
830
Nicholas Bellingerba159912013-07-03 03:48:24 -0700831static int iscsit_add_reject_from_cmd(
832 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000833 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700834 bool add_to_conn,
835 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000836{
837 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000838
839 if (!cmd->conn) {
840 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
841 cmd->init_task_tag);
842 return -1;
843 }
844 conn = cmd->conn;
845
846 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700847 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000848
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100849 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000850 if (!cmd->buf_ptr) {
851 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700852 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000853 return -1;
854 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000855
856 if (add_to_conn) {
857 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700858 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000859 spin_unlock_bh(&conn->cmd_lock);
860 }
861
862 cmd->i_state = ISTATE_SEND_REJECT;
863 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800864 /*
865 * Perform the kref_put now if se_cmd has already been setup by
866 * scsit_setup_scsi_cmd()
867 */
868 if (cmd->se_cmd.se_tfo != NULL) {
869 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +0200870 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800871 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700872 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000873}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700874
875static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
876 unsigned char *buf)
877{
878 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
879}
880
881int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
882{
883 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
884}
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 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700903 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000904 page_off = (data_offset % PAGE_SIZE);
905
906 cmd->first_data_sg = sg;
907 cmd->first_data_sg_off = page_off;
908
909 while (data_length) {
910 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
911
912 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
913 iov[i].iov_len = cur_len;
914
915 data_length -= cur_len;
916 page_off = 0;
917 sg = sg_next(sg);
918 i++;
919 }
920
921 cmd->kmapped_nents = i;
922
923 return i;
924}
925
926static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
927{
928 u32 i;
929 struct scatterlist *sg;
930
931 sg = cmd->first_data_sg;
932
933 for (i = 0; i < cmd->kmapped_nents; i++)
934 kunmap(sg_page(&sg[i]));
935}
936
937static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
938{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700939 LIST_HEAD(ack_list);
940 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000941
942 conn->exp_statsn = exp_statsn;
943
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800944 if (conn->sess->sess_ops->RDMAExtensions)
945 return;
946
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000947 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700948 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000949 spin_lock(&cmd->istate_lock);
950 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800951 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000952 cmd->i_state = ISTATE_REMOVE;
953 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700954 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000955 continue;
956 }
957 spin_unlock(&cmd->istate_lock);
958 }
959 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700960
961 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800962 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700963 iscsit_free_cmd(cmd, false);
964 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000965}
966
967static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
968{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700969 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000970
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400971 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000972
973 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
974 if (!cmd->iov_data) {
975 pr_err("Unable to allocate cmd->iov_data\n");
976 return -ENOMEM;
977 }
978
979 cmd->orig_iov_data_count = iov_count;
980 return 0;
981}
982
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800983int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
984 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000985{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800986 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000987 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700988 int iscsi_task_attr;
989 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000990
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800991 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000992
993 hdr = (struct iscsi_scsi_req *) buf;
994 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000995
996 /* FIXME; Add checks for AdditionalHeaderSegment */
997
998 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
999 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1000 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1001 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001002 return iscsit_add_reject_cmd(cmd,
1003 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001004 }
1005
1006 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1007 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1008 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001009 * From RFC-3720 Section 10.3.1:
1010 *
1011 * "Either or both of R and W MAY be 1 when either the
1012 * Expected Data Transfer Length and/or Bidirectional Read
1013 * Expected Data Transfer Length are 0"
1014 *
1015 * For this case, go ahead and clear the unnecssary bits
1016 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001017 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001018 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1019 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001020
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001021 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001022 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001023 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001024 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001025
1026 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1027 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1028 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1029 " MUST be set if Expected Data Transfer Length is not 0."
1030 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001031 return iscsit_add_reject_cmd(cmd,
1032 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001033 }
1034
1035 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1036 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1037 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001038 return iscsit_add_reject_cmd(cmd,
1039 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001040 }
1041
1042 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1043 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1044 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001045 return iscsit_add_reject_cmd(cmd,
1046 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001047 }
1048
1049 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1050 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1051 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001052 return iscsit_add_reject_cmd(cmd,
1053 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001054 }
1055
Nicholas Bellingerba159912013-07-03 03:48:24 -07001056 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001057 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1058 pr_err("Expected Data Transfer Length and Length of"
1059 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1060 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001061 return iscsit_add_reject_cmd(cmd,
1062 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001063 }
1064
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001065 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001066 pr_err("DataSegmentLength: %u is greater than"
1067 " EDTL: %u, protocol error.\n", payload_length,
1068 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001069 return iscsit_add_reject_cmd(cmd,
1070 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001071 }
1072
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001073 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001074 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001075 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1076 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001077 return iscsit_add_reject_cmd(cmd,
1078 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001079 }
1080
1081 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1082 pr_err("DataSegmentLength: %u is greater than"
1083 " FirstBurstLength: %u, protocol error.\n",
1084 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001085 return iscsit_add_reject_cmd(cmd,
1086 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001087 }
1088
1089 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1090 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1091 DMA_NONE;
1092
Andy Groverd28b11692012-04-03 15:51:22 -07001093 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -07001094 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1095 /*
1096 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1097 */
1098 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1099 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001100 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001101 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001102 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001103 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001104 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001105 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001106 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001107 else {
1108 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001109 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
1110 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001111 }
1112
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001113 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
1114 cmd->i_state = ISTATE_NEW_CMD;
1115 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1116 cmd->immediate_data = (payload_length) ? 1 : 0;
1117 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1118 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1119 if (cmd->unsolicited_data)
1120 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1121
1122 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
Alexei Potashnik95473082015-07-21 15:07:56 -07001123 if (hdr->flags & ISCSI_FLAG_CMD_READ)
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02001124 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Alexei Potashnik95473082015-07-21 15:07:56 -07001125 else
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001126 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001127 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1128 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001129 cmd->first_burst_len = payload_length;
1130
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001131 if (!conn->sess->sess_ops->RDMAExtensions &&
1132 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001133 struct iscsi_datain_req *dr;
1134
1135 dr = iscsit_allocate_datain_req();
1136 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001137 return iscsit_add_reject_cmd(cmd,
1138 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001139
1140 iscsit_attach_datain_req(cmd, dr);
1141 }
1142
1143 /*
Andy Grover065ca1e2012-04-03 15:51:23 -07001144 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1145 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001146 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001147 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1148 cmd->data_direction, sam_task_attr,
1149 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -07001150
1151 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1152 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001153 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1154 conn->cid);
1155
Bart Van Asscheafc16602015-04-27 13:52:36 +02001156 target_get_sess_cmd(&cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001157
Christoph Hellwigde103c92012-11-06 12:24:09 -08001158 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1159 scsilun_to_int(&hdr->lun));
1160 if (cmd->sense_reason)
1161 goto attach_cmd;
1162
Bart Van Assche649ee052015-04-14 13:26:44 +02001163 /* only used for printks or comparing with ->ref_task_tag */
1164 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001165 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1166 if (cmd->sense_reason) {
1167 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001168 return iscsit_add_reject_cmd(cmd,
1169 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001170 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001171
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001172 goto attach_cmd;
1173 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001174
Christoph Hellwigde103c92012-11-06 12:24:09 -08001175 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001176 return iscsit_add_reject_cmd(cmd,
1177 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001178 }
1179
1180attach_cmd:
1181 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001182 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001183 spin_unlock_bh(&conn->cmd_lock);
1184 /*
1185 * Check if we need to delay processing because of ALUA
1186 * Active/NonOptimized primary access state..
1187 */
1188 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001189
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001190 return 0;
1191}
1192EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001193
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001194void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1195{
1196 iscsit_set_dataout_sequence_values(cmd);
1197
1198 spin_lock_bh(&cmd->dataout_timeout_lock);
1199 iscsit_start_dataout_timer(cmd, cmd->conn);
1200 spin_unlock_bh(&cmd->dataout_timeout_lock);
1201}
1202EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1203
1204int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1205 struct iscsi_scsi_req *hdr)
1206{
1207 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001208 /*
1209 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1210 * the Immediate Bit is not set, and no Immediate
1211 * Data is attached.
1212 *
1213 * A PDU/CmdSN carrying Immediate Data can only
1214 * be processed after the DataCRC has passed.
1215 * If the DataCRC fails, the CmdSN MUST NOT
1216 * be acknowledged. (See below)
1217 */
1218 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001219 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1220 (unsigned char *)hdr, hdr->cmdsn);
1221 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1222 return -1;
1223 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001224 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001225 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001226 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001227 }
1228
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001229 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001230
1231 /*
1232 * If no Immediate Data is attached, it's OK to return now.
1233 */
1234 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001235 if (!cmd->sense_reason && cmd->unsolicited_data)
1236 iscsit_set_unsoliticed_dataout(cmd);
1237 if (!cmd->sense_reason)
1238 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001239
Bart Van Asscheafc16602015-04-27 13:52:36 +02001240 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001241 return 0;
1242 }
1243
1244 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001245 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1246 * execution. These exceptions are processed in CmdSN order using
1247 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001248 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001249 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001250 if (cmd->reject_reason)
1251 return 0;
1252
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001253 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001254 }
1255 /*
1256 * Call directly into transport_generic_new_cmd() to perform
1257 * the backend memory allocation.
1258 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001259 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001260 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001261 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001262
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001263 return 0;
1264}
1265EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1266
1267static int
1268iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1269 bool dump_payload)
1270{
1271 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1272 /*
1273 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1274 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001275 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001276 goto after_immediate_data;
1277
1278 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1279 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001280after_immediate_data:
1281 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1282 /*
1283 * A PDU/CmdSN carrying Immediate Data passed
1284 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1285 * Immediate Bit is not set.
1286 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001287 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1288 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001289 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001290 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001291
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001292 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001293 int rc;
1294
1295 rc = iscsit_dump_data_payload(cmd->conn,
1296 cmd->first_burst_len, 1);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001297 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001298 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001299 } else if (cmd->unsolicited_data)
1300 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001301
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001302 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1303 /*
1304 * Immediate Data failed DataCRC and ERL>=1,
1305 * silently drop this PDU and let the initiator
1306 * plug the CmdSN gap.
1307 *
1308 * FIXME: Send Unsolicited NOPIN with reserved
1309 * TTT here to help the initiator figure out
1310 * the missing CmdSN, although they should be
1311 * intelligent enough to determine the missing
1312 * CmdSN and issue a retry to plug the sequence.
1313 */
1314 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001315 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001316 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1317 return -1;
1318
1319 return 0;
1320}
1321
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001322static int
1323iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1324 unsigned char *buf)
1325{
1326 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1327 int rc, immed_data;
1328 bool dump_payload = false;
1329
1330 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1331 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001332 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001333 /*
1334 * Allocation iovecs needed for struct socket operations for
1335 * traditional iSCSI block I/O.
1336 */
1337 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001338 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001339 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001340 }
1341 immed_data = cmd->immediate_data;
1342
1343 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1344 if (rc < 0)
1345 return rc;
1346 else if (rc > 0)
1347 dump_payload = true;
1348
1349 if (!immed_data)
1350 return 0;
1351
1352 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1353}
1354
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001355static u32 iscsit_do_crypto_hash_sg(
Herbert Xu69110e32016-01-24 21:19:52 +08001356 struct ahash_request *hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001357 struct iscsi_cmd *cmd,
1358 u32 data_offset,
1359 u32 data_length,
1360 u32 padding,
1361 u8 *pad_bytes)
1362{
1363 u32 data_crc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001364 struct scatterlist *sg;
1365 unsigned int page_off;
1366
Herbert Xu69110e32016-01-24 21:19:52 +08001367 crypto_ahash_init(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001368
1369 sg = cmd->first_data_sg;
1370 page_off = cmd->first_data_sg_off;
1371
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001372 while (data_length) {
Alexei Potashnikaa756792015-07-20 17:12:12 -07001373 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001374
Herbert Xu69110e32016-01-24 21:19:52 +08001375 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1376 crypto_ahash_update(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001377
1378 data_length -= cur_len;
1379 page_off = 0;
Alexei Potashnikaa756792015-07-20 17:12:12 -07001380 /* iscsit_map_iovec has already checked for invalid sg pointers */
1381 sg = sg_next(sg);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001382 }
1383
1384 if (padding) {
1385 struct scatterlist pad_sg;
1386
1387 sg_init_one(&pad_sg, pad_bytes, padding);
Herbert Xu69110e32016-01-24 21:19:52 +08001388 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1389 padding);
1390 crypto_ahash_finup(hash);
1391 } else {
1392 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1393 crypto_ahash_final(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001394 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001395
1396 return data_crc;
1397}
1398
1399static void iscsit_do_crypto_hash_buf(
Herbert Xu69110e32016-01-24 21:19:52 +08001400 struct ahash_request *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001401 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001402 u32 payload_length,
1403 u32 padding,
1404 u8 *pad_bytes,
1405 u8 *data_crc)
1406{
Herbert Xu69110e32016-01-24 21:19:52 +08001407 struct scatterlist sg[2];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001408
Herbert Xu69110e32016-01-24 21:19:52 +08001409 sg_init_table(sg, ARRAY_SIZE(sg));
1410 sg_set_buf(sg, buf, payload_length);
1411 sg_set_buf(sg + 1, pad_bytes, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001412
Herbert Xu69110e32016-01-24 21:19:52 +08001413 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001414
Herbert Xu69110e32016-01-24 21:19:52 +08001415 crypto_ahash_digest(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001416}
1417
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001418int
1419iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1420 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001421{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001422 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001423 struct iscsi_cmd *cmd = NULL;
1424 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001425 u32 payload_length = ntoh24(hdr->dlength);
1426 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001427
1428 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001429 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1430 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001431 }
1432
1433 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001434 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001435
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001436 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001437 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001438 " MaxXmitDataSegmentLength: %u\n", payload_length,
1439 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001440 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1441 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001442 }
1443
1444 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1445 payload_length);
1446 if (!cmd)
1447 return 0;
1448
1449 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1450 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001451 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001452 payload_length, conn->cid);
1453
1454 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1455 pr_err("Command ITT: 0x%08x received DataOUT after"
1456 " last DataOUT received, dumping payload\n",
1457 cmd->init_task_tag);
1458 return iscsit_dump_data_payload(conn, payload_length, 1);
1459 }
1460
1461 if (cmd->data_direction != DMA_TO_DEVICE) {
1462 pr_err("Command ITT: 0x%08x received DataOUT for a"
1463 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001464 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001465 }
1466 se_cmd = &cmd->se_cmd;
1467 iscsit_mod_dataout_timer(cmd);
1468
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001469 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001470 pr_err("DataOut Offset: %u, Length %u greater than"
1471 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001472 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001473 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001474 }
1475
1476 if (cmd->unsolicited_data) {
1477 int dump_unsolicited_data = 0;
1478
1479 if (conn->sess->sess_ops->InitialR2T) {
1480 pr_err("Received unexpected unsolicited data"
1481 " while InitialR2T=Yes, protocol error.\n");
1482 transport_send_check_condition_and_sense(&cmd->se_cmd,
1483 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1484 return -1;
1485 }
1486 /*
1487 * Special case for dealing with Unsolicited DataOUT
1488 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1489 * failures;
1490 */
1491
1492 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001493 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001494 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001495 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001496
1497 if (dump_unsolicited_data) {
1498 /*
1499 * Check if a delayed TASK_ABORTED status needs to
1500 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
Bart Van Assche5a342522015-10-22 15:53:22 -07001501 * received with the unsolicited data out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001502 */
1503 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1504 iscsit_stop_dataout_timer(cmd);
1505
1506 transport_check_aborted_status(se_cmd,
1507 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1508 return iscsit_dump_data_payload(conn, payload_length, 1);
1509 }
1510 } else {
1511 /*
1512 * For the normal solicited data path:
1513 *
1514 * Check for a delayed TASK_ABORTED status and dump any
1515 * incoming data out payload if one exists. Also, when the
1516 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1517 * data out sequence, we decrement outstanding_r2ts. Once
1518 * outstanding_r2ts reaches zero, go ahead and send the delayed
1519 * TASK_ABORTED status.
1520 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001521 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001522 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1523 if (--cmd->outstanding_r2ts < 1) {
1524 iscsit_stop_dataout_timer(cmd);
1525 transport_check_aborted_status(
1526 se_cmd, 1);
1527 }
1528
1529 return iscsit_dump_data_payload(conn, payload_length, 1);
1530 }
1531 }
1532 /*
1533 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1534 * within-command recovery checks before receiving the payload.
1535 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001536 rc = iscsit_check_pre_dataout(cmd, buf);
1537 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001538 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001539 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001540 return -1;
1541
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001542 *out_cmd = cmd;
1543 return 0;
1544}
1545EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1546
1547static int
1548iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1549 struct iscsi_data *hdr)
1550{
1551 struct kvec *iov;
1552 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1553 u32 payload_length = ntoh24(hdr->dlength);
1554 int iov_ret, data_crc_failed = 0;
1555
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001556 rx_size += payload_length;
1557 iov = &cmd->iov_data[0];
1558
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001559 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1560 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001561 if (iov_ret < 0)
1562 return -1;
1563
1564 iov_count += iov_ret;
1565
1566 padding = ((-payload_length) & 3);
1567 if (padding != 0) {
1568 iov[iov_count].iov_base = cmd->pad_bytes;
1569 iov[iov_count++].iov_len = padding;
1570 rx_size += padding;
1571 pr_debug("Receiving %u padding bytes.\n", padding);
1572 }
1573
1574 if (conn->conn_ops->DataDigest) {
1575 iov[iov_count].iov_base = &checksum;
1576 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1577 rx_size += ISCSI_CRC_LEN;
1578 }
1579
1580 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1581
1582 iscsit_unmap_iovec(cmd);
1583
1584 if (rx_got != rx_size)
1585 return -1;
1586
1587 if (conn->conn_ops->DataDigest) {
1588 u32 data_crc;
1589
Herbert Xu69110e32016-01-24 21:19:52 +08001590 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001591 be32_to_cpu(hdr->offset),
1592 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001593 cmd->pad_bytes);
1594
1595 if (checksum != data_crc) {
1596 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1597 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1598 " does not match computed 0x%08x\n",
1599 hdr->itt, hdr->offset, payload_length,
1600 hdr->datasn, checksum, data_crc);
1601 data_crc_failed = 1;
1602 } else {
1603 pr_debug("Got CRC32C DataDigest 0x%08x for"
1604 " %u bytes of Data Out\n", checksum,
1605 payload_length);
1606 }
1607 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001608
1609 return data_crc_failed;
1610}
1611
1612int
1613iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1614 bool data_crc_failed)
1615{
1616 struct iscsi_conn *conn = cmd->conn;
1617 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001618 /*
1619 * Increment post receive data and CRC values or perform
1620 * within-command recovery.
1621 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001622 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1623 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001624 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001625 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001626 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001627 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1628 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001629 /*
1630 * Handle extra special case for out of order
1631 * Unsolicited Data Out.
1632 */
1633 spin_lock_bh(&cmd->istate_lock);
1634 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1635 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1636 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1637 spin_unlock_bh(&cmd->istate_lock);
1638
1639 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001640 if (ooo_cmdsn)
1641 return 0;
1642 target_execute_cmd(&cmd->se_cmd);
1643 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001644 } else /* DATAOUT_CANNOT_RECOVER */
1645 return -1;
1646
1647 return 0;
1648}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001649EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001650
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001651static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1652{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001653 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001654 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1655 int rc;
1656 bool data_crc_failed = false;
1657
1658 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1659 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001660 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001661 else if (!cmd)
1662 return 0;
1663
1664 rc = iscsit_get_dataout(conn, cmd, hdr);
1665 if (rc < 0)
1666 return rc;
1667 else if (rc > 0)
1668 data_crc_failed = true;
1669
1670 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1671}
1672
Nicholas Bellinger778de362013-06-14 16:07:47 -07001673int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1674 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001675{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001676 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001677
Arshad Hussaina3662602014-03-14 15:28:59 -07001678 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1679 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1680 if (!cmd)
1681 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1682 (unsigned char *)hdr);
1683
1684 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1685 (unsigned char *)hdr);
1686 }
1687
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001688 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001689 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1690 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001691 if (!cmd)
1692 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1693 (unsigned char *)hdr);
1694
Nicholas Bellingerba159912013-07-03 03:48:24 -07001695 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1696 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001697 }
1698
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001699 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001700 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001701 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001702 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001703 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001704 if (!cmd)
1705 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1706 (unsigned char *)hdr);
1707
Nicholas Bellingerba159912013-07-03 03:48:24 -07001708 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1709 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001710 }
1711
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001712 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001713 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001714 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001715 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1716 payload_length);
1717 /*
1718 * This is not a response to a Unsolicited NopIN, which means
1719 * it can either be a NOPOUT ping request (with a valid ITT),
1720 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1721 * Either way, make sure we allocate an struct iscsi_cmd, as both
1722 * can contain ping data.
1723 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001724 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001725 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1726 cmd->i_state = ISTATE_SEND_NOPIN;
1727 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1728 1 : 0);
1729 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1730 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001731 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1732 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001733 cmd->data_direction = DMA_NONE;
1734 }
1735
Nicholas Bellinger778de362013-06-14 16:07:47 -07001736 return 0;
1737}
1738EXPORT_SYMBOL(iscsit_setup_nop_out);
1739
1740int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1741 struct iscsi_nopout *hdr)
1742{
1743 struct iscsi_cmd *cmd_p = NULL;
1744 int cmdsn_ret = 0;
1745 /*
1746 * Initiator is expecting a NopIN ping reply..
1747 */
1748 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001749 if (!cmd)
1750 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1751 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001752
1753 spin_lock_bh(&conn->cmd_lock);
1754 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1755 spin_unlock_bh(&conn->cmd_lock);
1756
1757 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1758
1759 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1760 iscsit_add_cmd_to_response_queue(cmd, conn,
1761 cmd->i_state);
1762 return 0;
1763 }
1764
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001765 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1766 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001767 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1768 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001769 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001770 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001771
1772 return 0;
1773 }
1774 /*
1775 * This was a response to a unsolicited NOPIN ping.
1776 */
1777 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1778 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1779 if (!cmd_p)
1780 return -EINVAL;
1781
1782 iscsit_stop_nopin_response_timer(conn);
1783
1784 cmd_p->i_state = ISTATE_REMOVE;
1785 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1786
1787 iscsit_start_nopin_timer(conn);
1788 return 0;
1789 }
1790 /*
1791 * Otherwise, initiator is not expecting a NOPIN is response.
1792 * Just ignore for now.
1793 */
1794 return 0;
1795}
1796EXPORT_SYMBOL(iscsit_process_nop_out);
1797
1798static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1799 unsigned char *buf)
1800{
1801 unsigned char *ping_data = NULL;
1802 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1803 struct kvec *iov = NULL;
1804 u32 payload_length = ntoh24(hdr->dlength);
1805 int ret;
1806
1807 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1808 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001809 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001810 /*
1811 * Handle NOP-OUT payload for traditional iSCSI sockets
1812 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001813 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001814 u32 checksum, data_crc, padding = 0;
1815 int niov = 0, rx_got, rx_size = payload_length;
1816
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001817 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1818 if (!ping_data) {
1819 pr_err("Unable to allocate memory for"
1820 " NOPOUT ping data.\n");
1821 ret = -1;
1822 goto out;
1823 }
1824
1825 iov = &cmd->iov_misc[0];
1826 iov[niov].iov_base = ping_data;
1827 iov[niov++].iov_len = payload_length;
1828
1829 padding = ((-payload_length) & 3);
1830 if (padding != 0) {
1831 pr_debug("Receiving %u additional bytes"
1832 " for padding.\n", padding);
1833 iov[niov].iov_base = &cmd->pad_bytes;
1834 iov[niov++].iov_len = padding;
1835 rx_size += padding;
1836 }
1837 if (conn->conn_ops->DataDigest) {
1838 iov[niov].iov_base = &checksum;
1839 iov[niov++].iov_len = ISCSI_CRC_LEN;
1840 rx_size += ISCSI_CRC_LEN;
1841 }
1842
1843 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1844 if (rx_got != rx_size) {
1845 ret = -1;
1846 goto out;
1847 }
1848
1849 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08001850 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001851 ping_data, payload_length,
1852 padding, cmd->pad_bytes,
1853 (u8 *)&data_crc);
1854
1855 if (checksum != data_crc) {
1856 pr_err("Ping data CRC32C DataDigest"
1857 " 0x%08x does not match computed 0x%08x\n",
1858 checksum, data_crc);
1859 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1860 pr_err("Unable to recover from"
1861 " NOPOUT Ping DataCRC failure while in"
1862 " ERL=0.\n");
1863 ret = -1;
1864 goto out;
1865 } else {
1866 /*
1867 * Silently drop this PDU and let the
1868 * initiator plug the CmdSN gap.
1869 */
1870 pr_debug("Dropping NOPOUT"
1871 " Command CmdSN: 0x%08x due to"
1872 " DataCRC error.\n", hdr->cmdsn);
1873 ret = 0;
1874 goto out;
1875 }
1876 } else {
1877 pr_debug("Got CRC32C DataDigest"
1878 " 0x%08x for %u bytes of ping data.\n",
1879 checksum, payload_length);
1880 }
1881 }
1882
1883 ping_data[payload_length] = '\0';
1884 /*
1885 * Attach ping data to struct iscsi_cmd->buf_ptr.
1886 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001887 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001888 cmd->buf_ptr_size = payload_length;
1889
1890 pr_debug("Got %u bytes of NOPOUT ping"
1891 " data.\n", payload_length);
1892 pr_debug("Ping Data: \"%s\"\n", ping_data);
1893 }
1894
Nicholas Bellinger778de362013-06-14 16:07:47 -07001895 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001896out:
1897 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001898 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001899
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001900 kfree(ping_data);
1901 return ret;
1902}
1903
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001904int
1905iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1906 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001907{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001908 struct se_tmr_req *se_tmr;
1909 struct iscsi_tmr_req *tmr_req;
1910 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001911 int out_of_order_cmdsn = 0, ret;
1912 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001913 u8 function;
1914
1915 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001916 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1917 function = hdr->flags;
1918
1919 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1920 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1921 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1922 hdr->rtt, hdr->refcmdsn, conn->cid);
1923
1924 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1925 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001926 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001927 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001928 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001929 }
1930
1931 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1932 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1933 pr_err("Task Management Request TASK_REASSIGN not"
1934 " issued as immediate command, bad iSCSI Initiator"
1935 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001936 return iscsit_add_reject_cmd(cmd,
1937 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001938 }
1939 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001940 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1941 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001942
Andy Groverd28b11692012-04-03 15:51:22 -07001943 cmd->data_direction = DMA_NONE;
1944
1945 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1946 if (!cmd->tmr_req) {
1947 pr_err("Unable to allocate memory for"
1948 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001949 return iscsit_add_reject_cmd(cmd,
1950 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1951 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001952 }
1953
1954 /*
1955 * TASK_REASSIGN for ERL=2 / connection stays inside of
1956 * LIO-Target $FABRIC_MOD
1957 */
1958 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1959
1960 u8 tcm_function;
1961 int ret;
1962
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001963 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Andy Groverd28b11692012-04-03 15:51:22 -07001964 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001965 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001966
Bart Van Asscheafc16602015-04-27 13:52:36 +02001967 target_get_sess_cmd(&cmd->se_cmd, true);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001968 sess_ref = true;
1969
Andy Groverd28b11692012-04-03 15:51:22 -07001970 switch (function) {
1971 case ISCSI_TM_FUNC_ABORT_TASK:
1972 tcm_function = TMR_ABORT_TASK;
1973 break;
1974 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1975 tcm_function = TMR_ABORT_TASK_SET;
1976 break;
1977 case ISCSI_TM_FUNC_CLEAR_ACA:
1978 tcm_function = TMR_CLEAR_ACA;
1979 break;
1980 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1981 tcm_function = TMR_CLEAR_TASK_SET;
1982 break;
1983 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1984 tcm_function = TMR_LUN_RESET;
1985 break;
1986 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1987 tcm_function = TMR_TARGET_WARM_RESET;
1988 break;
1989 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1990 tcm_function = TMR_TARGET_COLD_RESET;
1991 break;
1992 default:
1993 pr_err("Unknown iSCSI TMR Function:"
1994 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001995 return iscsit_add_reject_cmd(cmd,
1996 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001997 }
1998
1999 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
2000 tcm_function, GFP_KERNEL);
2001 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002002 return iscsit_add_reject_cmd(cmd,
2003 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002004
2005 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
2006 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002007
2008 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2009 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2010 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2011 cmd->init_task_tag = hdr->itt;
2012 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002013 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2014 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002015 se_tmr = cmd->se_cmd.se_tmr_req;
2016 tmr_req = cmd->tmr_req;
2017 /*
2018 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2019 */
2020 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08002021 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
2022 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002023 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002024 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2025 goto attach;
2026 }
2027 }
2028
2029 switch (function) {
2030 case ISCSI_TM_FUNC_ABORT_TASK:
2031 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002032 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002033 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002034 break;
2035 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2036 case ISCSI_TM_FUNC_CLEAR_ACA:
2037 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2038 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2039 break;
2040 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2041 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002042 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2043 goto attach;
2044 }
2045 break;
2046 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2047 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002048 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2049 goto attach;
2050 }
2051 break;
2052 case ISCSI_TM_FUNC_TASK_REASSIGN:
2053 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2054 /*
2055 * Perform sanity checks on the ExpDataSN only if the
2056 * TASK_REASSIGN was successful.
2057 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002058 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002059 break;
2060
2061 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002062 return iscsit_add_reject_cmd(cmd,
2063 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002064 break;
2065 default:
2066 pr_err("Unknown TMR function: 0x%02x, protocol"
2067 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002068 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2069 goto attach;
2070 }
2071
2072 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2073 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2074 se_tmr->call_transport = 1;
2075attach:
2076 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002077 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002078 spin_unlock_bh(&conn->cmd_lock);
2079
2080 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002081 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002082 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
2083 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002084 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002085 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002086 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002087 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002088 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002089 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002090
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002091 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002092 return 0;
2093 /*
2094 * Found the referenced task, send to transport for processing.
2095 */
2096 if (se_tmr->call_transport)
2097 return transport_generic_handle_tmr(&cmd->se_cmd);
2098
2099 /*
2100 * Could not find the referenced LUN, task, or Task Management
2101 * command not authorized or supported. Change state and
2102 * let the tx_thread send the response.
2103 *
2104 * For connection recovery, this is also the default action for
2105 * TMR TASK_REASSIGN.
2106 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002107 if (sess_ref) {
2108 pr_debug("Handle TMR, using sess_ref=true check\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +02002109 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002110 }
2111
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002112 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2113 return 0;
2114}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002115EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002116
2117/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002118int
2119iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2120 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002121{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002122 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002123
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002124 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002125 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002126 "greater than MaxXmitDataSegmentLength %u.\n",
2127 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002128 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2129 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002130 }
2131
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08002132 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2133 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2134 pr_err("Multi sequence text commands currently not supported\n");
2135 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2136 (unsigned char *)hdr);
2137 }
2138
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002139 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2140 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2141 hdr->exp_statsn, payload_length);
2142
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002143 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2144 cmd->i_state = ISTATE_SEND_TEXTRSP;
2145 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2146 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2147 cmd->targ_xfer_tag = 0xFFFFFFFF;
2148 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2149 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2150 cmd->data_direction = DMA_NONE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002151 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002152
2153 return 0;
2154}
2155EXPORT_SYMBOL(iscsit_setup_text_cmd);
2156
2157int
2158iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2159 struct iscsi_text *hdr)
2160{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002161 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002162 int cmdsn_ret;
2163
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002164 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002165 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2166 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2167 pr_err("Unable to locate text_in buffer for sendtargets"
2168 " discovery\n");
2169 goto reject;
2170 }
2171 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002172 }
2173 if (strncmp("SendTargets", text_in, 11) != 0) {
2174 pr_err("Received Text Data that is not"
2175 " SendTargets, cannot continue.\n");
2176 goto reject;
2177 }
2178 text_ptr = strchr(text_in, '=');
2179 if (!text_ptr) {
2180 pr_err("No \"=\" separator found in Text Data,"
2181 " cannot continue.\n");
2182 goto reject;
2183 }
2184 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002185 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002186 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2187 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002188 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002189 } else {
2190 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2191 goto reject;
2192 }
2193
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002194 spin_lock_bh(&conn->cmd_lock);
2195 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2196 spin_unlock_bh(&conn->cmd_lock);
2197
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002198empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002199 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2200
2201 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002202 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2203 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002204 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002205 return -1;
2206
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002207 return 0;
2208 }
2209
2210 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002211
2212reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002213 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2214 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002215}
2216EXPORT_SYMBOL(iscsit_process_text_cmd);
2217
2218static int
2219iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2220 unsigned char *buf)
2221{
2222 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2223 char *text_in = NULL;
2224 u32 payload_length = ntoh24(hdr->dlength);
2225 int rx_size, rc;
2226
2227 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2228 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002229 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002230
2231 rx_size = payload_length;
2232 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002233 u32 checksum = 0, data_crc = 0;
2234 u32 padding = 0, pad_bytes = 0;
2235 int niov = 0, rx_got;
2236 struct kvec iov[3];
2237
2238 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002239 if (!text_in) {
2240 pr_err("Unable to allocate memory for"
2241 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002242 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002243 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002244 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002245
2246 memset(iov, 0, 3 * sizeof(struct kvec));
2247 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002248 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002249
2250 padding = ((-payload_length) & 3);
2251 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002252 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002253 iov[niov++].iov_len = padding;
2254 rx_size += padding;
2255 pr_debug("Receiving %u additional bytes"
2256 " for padding.\n", padding);
2257 }
2258 if (conn->conn_ops->DataDigest) {
2259 iov[niov].iov_base = &checksum;
2260 iov[niov++].iov_len = ISCSI_CRC_LEN;
2261 rx_size += ISCSI_CRC_LEN;
2262 }
2263
2264 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002265 if (rx_got != rx_size)
2266 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002267
2268 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08002269 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002270 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002271 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002272 (u8 *)&data_crc);
2273
2274 if (checksum != data_crc) {
2275 pr_err("Text data CRC32C DataDigest"
2276 " 0x%08x does not match computed"
2277 " 0x%08x\n", checksum, data_crc);
2278 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2279 pr_err("Unable to recover from"
2280 " Text Data digest failure while in"
2281 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002282 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002283 } else {
2284 /*
2285 * Silently drop this PDU and let the
2286 * initiator plug the CmdSN gap.
2287 */
2288 pr_debug("Dropping Text"
2289 " Command CmdSN: 0x%08x due to"
2290 " DataCRC error.\n", hdr->cmdsn);
2291 kfree(text_in);
2292 return 0;
2293 }
2294 } else {
2295 pr_debug("Got CRC32C DataDigest"
2296 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002297 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002298 }
2299 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002300 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002301 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002302 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002303 }
2304
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002305 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002306
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002307reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002308 kfree(cmd->text_in_ptr);
2309 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002310 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002311}
2312
2313int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2314{
2315 struct iscsi_conn *conn_p;
2316 struct iscsi_session *sess = conn->sess;
2317
2318 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2319 " for SID: %u.\n", conn->cid, conn->sess->sid);
2320
2321 atomic_set(&sess->session_logout, 1);
2322 atomic_set(&conn->conn_logout_remove, 1);
2323 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2324
2325 iscsit_inc_conn_usage_count(conn);
2326 iscsit_inc_session_usage_count(sess);
2327
2328 spin_lock_bh(&sess->conn_lock);
2329 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2330 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2331 continue;
2332
2333 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2334 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2335 }
2336 spin_unlock_bh(&sess->conn_lock);
2337
2338 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2339
2340 return 0;
2341}
2342
2343int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2344{
2345 struct iscsi_conn *l_conn;
2346 struct iscsi_session *sess = conn->sess;
2347
2348 pr_debug("Received logout request CLOSECONNECTION for CID:"
2349 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2350
2351 /*
2352 * A Logout Request with a CLOSECONNECTION reason code for a CID
2353 * can arrive on a connection with a differing CID.
2354 */
2355 if (conn->cid == cmd->logout_cid) {
2356 spin_lock_bh(&conn->state_lock);
2357 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2358 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2359
2360 atomic_set(&conn->conn_logout_remove, 1);
2361 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2362 iscsit_inc_conn_usage_count(conn);
2363
2364 spin_unlock_bh(&conn->state_lock);
2365 } else {
2366 /*
2367 * Handle all different cid CLOSECONNECTION requests in
2368 * iscsit_logout_post_handler_diffcid() as to give enough
2369 * time for any non immediate command's CmdSN to be
2370 * acknowledged on the connection in question.
2371 *
2372 * Here we simply make sure the CID is still around.
2373 */
2374 l_conn = iscsit_get_conn_from_cid(sess,
2375 cmd->logout_cid);
2376 if (!l_conn) {
2377 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2378 iscsit_add_cmd_to_response_queue(cmd, conn,
2379 cmd->i_state);
2380 return 0;
2381 }
2382
2383 iscsit_dec_conn_usage_count(l_conn);
2384 }
2385
2386 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2387
2388 return 0;
2389}
2390
2391int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2392{
2393 struct iscsi_session *sess = conn->sess;
2394
2395 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2396 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2397
2398 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2399 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2400 " while ERL!=2.\n");
2401 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2402 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2403 return 0;
2404 }
2405
2406 if (conn->cid == cmd->logout_cid) {
2407 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2408 " with CID: %hu on CID: %hu, implementation error.\n",
2409 cmd->logout_cid, conn->cid);
2410 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2411 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2412 return 0;
2413 }
2414
2415 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2416
2417 return 0;
2418}
2419
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002420int
2421iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2422 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002423{
2424 int cmdsn_ret, logout_remove = 0;
2425 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002426 struct iscsi_logout *hdr;
2427 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2428
2429 hdr = (struct iscsi_logout *) buf;
2430 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002431
2432 if (tiqn) {
2433 spin_lock(&tiqn->logout_stats.lock);
2434 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2435 tiqn->logout_stats.normal_logouts++;
2436 else
2437 tiqn->logout_stats.abnormal_logouts++;
2438 spin_unlock(&tiqn->logout_stats.lock);
2439 }
2440
2441 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2442 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2443 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2444 hdr->cid, conn->cid);
2445
2446 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2447 pr_err("Received logout request on connection that"
2448 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002449 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002450 return 0;
2451 }
2452
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002453 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2454 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2455 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2456 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2457 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002458 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2459 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2460 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002461 cmd->logout_reason = reason_code;
2462 cmd->data_direction = DMA_NONE;
2463
2464 /*
2465 * We need to sleep in these cases (by returning 1) until the Logout
2466 * Response gets sent in the tx thread.
2467 */
2468 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2469 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002470 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002471 logout_remove = 1;
2472
2473 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002474 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002475 spin_unlock_bh(&conn->cmd_lock);
2476
2477 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002478 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002479
2480 /*
2481 * Immediate commands are executed, well, immediately.
2482 * Non-Immediate Logout Commands are executed in CmdSN order.
2483 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002484 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002485 int ret = iscsit_execute_cmd(cmd, 0);
2486
2487 if (ret < 0)
2488 return ret;
2489 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002490 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002491 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002492 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002493 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2494 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002495 }
2496
2497 return logout_remove;
2498}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002499EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002500
2501static int iscsit_handle_snack(
2502 struct iscsi_conn *conn,
2503 unsigned char *buf)
2504{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002505 struct iscsi_snack *hdr;
2506
2507 hdr = (struct iscsi_snack *) buf;
2508 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002509
2510 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2511 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2512 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2513 hdr->begrun, hdr->runlength, conn->cid);
2514
2515 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2516 pr_err("Initiator sent SNACK request while in"
2517 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002518 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2519 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002520 }
2521 /*
2522 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2523 * call from inside iscsi_send_recovery_datain_or_r2t().
2524 */
2525 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2526 case 0:
2527 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002528 hdr->itt,
2529 be32_to_cpu(hdr->ttt),
2530 be32_to_cpu(hdr->begrun),
2531 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002532 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002533 return iscsit_handle_status_snack(conn, hdr->itt,
2534 be32_to_cpu(hdr->ttt),
2535 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002536 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002537 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2538 be32_to_cpu(hdr->begrun),
2539 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002540 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2541 /* FIXME: Support R-Data SNACK */
2542 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002543 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2544 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002545 default:
2546 pr_err("Unknown SNACK type 0x%02x, protocol"
2547 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002548 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2549 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002550 }
2551
2552 return 0;
2553}
2554
2555static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2556{
2557 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2558 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2559 wait_for_completion_interruptible_timeout(
2560 &conn->rx_half_close_comp,
2561 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2562 }
2563}
2564
2565static int iscsit_handle_immediate_data(
2566 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002567 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002568 u32 length)
2569{
2570 int iov_ret, rx_got = 0, rx_size = 0;
2571 u32 checksum, iov_count = 0, padding = 0;
2572 struct iscsi_conn *conn = cmd->conn;
2573 struct kvec *iov;
2574
2575 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2576 if (iov_ret < 0)
2577 return IMMEDIATE_DATA_CANNOT_RECOVER;
2578
2579 rx_size = length;
2580 iov_count = iov_ret;
2581 iov = &cmd->iov_data[0];
2582
2583 padding = ((-length) & 3);
2584 if (padding != 0) {
2585 iov[iov_count].iov_base = cmd->pad_bytes;
2586 iov[iov_count++].iov_len = padding;
2587 rx_size += padding;
2588 }
2589
2590 if (conn->conn_ops->DataDigest) {
2591 iov[iov_count].iov_base = &checksum;
2592 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2593 rx_size += ISCSI_CRC_LEN;
2594 }
2595
2596 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2597
2598 iscsit_unmap_iovec(cmd);
2599
2600 if (rx_got != rx_size) {
2601 iscsit_rx_thread_wait_for_tcp(conn);
2602 return IMMEDIATE_DATA_CANNOT_RECOVER;
2603 }
2604
2605 if (conn->conn_ops->DataDigest) {
2606 u32 data_crc;
2607
Herbert Xu69110e32016-01-24 21:19:52 +08002608 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002609 cmd->write_data_done, length, padding,
2610 cmd->pad_bytes);
2611
2612 if (checksum != data_crc) {
2613 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2614 " does not match computed 0x%08x\n", checksum,
2615 data_crc);
2616
2617 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2618 pr_err("Unable to recover from"
2619 " Immediate Data digest failure while"
2620 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002621 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002622 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002623 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002624 return IMMEDIATE_DATA_CANNOT_RECOVER;
2625 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002626 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002627 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002628 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002629 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2630 }
2631 } else {
2632 pr_debug("Got CRC32C DataDigest 0x%08x for"
2633 " %u bytes of Immediate Data\n", checksum,
2634 length);
2635 }
2636 }
2637
2638 cmd->write_data_done += length;
2639
Andy Groverebf1d952012-04-03 15:51:24 -07002640 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002641 spin_lock_bh(&cmd->istate_lock);
2642 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2643 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2644 spin_unlock_bh(&cmd->istate_lock);
2645 }
2646
2647 return IMMEDIATE_DATA_NORMAL_OPERATION;
2648}
2649
2650/*
2651 * Called with sess->conn_lock held.
2652 */
2653/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2654 with active network interface */
2655static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2656{
2657 struct iscsi_cmd *cmd;
2658 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002659 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002660
2661 /*
2662 * Only send a Asynchronous Message on connections whos network
2663 * interface is still functional.
2664 */
2665 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2666 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2667 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002668 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002669 break;
2670 }
2671 }
2672
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002673 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002674 return;
2675
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002676 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002677 if (!cmd) {
2678 iscsit_dec_conn_usage_count(conn_p);
2679 return;
2680 }
2681
2682 cmd->logout_cid = conn->cid;
2683 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2684 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2685
2686 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002687 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002688 spin_unlock_bh(&conn_p->cmd_lock);
2689
2690 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2691 iscsit_dec_conn_usage_count(conn_p);
2692}
2693
2694static int iscsit_send_conn_drop_async_message(
2695 struct iscsi_cmd *cmd,
2696 struct iscsi_conn *conn)
2697{
2698 struct iscsi_async *hdr;
2699
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002700 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2701
2702 hdr = (struct iscsi_async *) cmd->pdu;
2703 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2704 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002705 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002706 cmd->targ_xfer_tag = 0xFFFFFFFF;
2707 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2708 cmd->stat_sn = conn->stat_sn++;
2709 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2710 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002711 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002712 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2713 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2714 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2715 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2716
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002717 pr_debug("Sending Connection Dropped Async Message StatSN:"
2718 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2719 cmd->logout_cid, conn->cid);
Varun Prakash2854bb22016-04-20 00:00:08 +05302720
2721 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002722}
2723
Andy Grover6f3c0e62012-04-03 15:51:09 -07002724static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2725{
2726 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2727 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2728 wait_for_completion_interruptible_timeout(
2729 &conn->tx_half_close_comp,
2730 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2731 }
2732}
2733
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002734static void
2735iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2736 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2737 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002738{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002739 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2740 hdr->flags = datain->flags;
2741 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2742 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2743 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2744 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2745 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2746 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2747 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2748 }
2749 }
2750 hton24(hdr->dlength, datain->length);
2751 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2752 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2753 (struct scsi_lun *)&hdr->lun);
2754 else
2755 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2756
2757 hdr->itt = cmd->init_task_tag;
2758
2759 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2760 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2761 else
2762 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2763 if (set_statsn)
2764 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2765 else
2766 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2767
2768 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002769 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002770 hdr->datasn = cpu_to_be32(datain->data_sn);
2771 hdr->offset = cpu_to_be32(datain->offset);
2772
2773 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2774 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2775 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2776 ntohl(hdr->offset), datain->length, conn->cid);
2777}
2778
2779static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2780{
2781 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002782 struct iscsi_datain datain;
2783 struct iscsi_datain_req *dr;
Varun Prakash2854bb22016-04-20 00:00:08 +05302784 int eodr = 0, ret;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002785 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002786
2787 memset(&datain, 0, sizeof(struct iscsi_datain));
2788 dr = iscsit_get_datain_values(cmd, &datain);
2789 if (!dr) {
2790 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2791 cmd->init_task_tag);
2792 return -1;
2793 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002794 /*
2795 * Be paranoid and double check the logic for now.
2796 */
Andy Groverebf1d952012-04-03 15:51:24 -07002797 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002798 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2799 " datain.length: %u exceeds cmd->data_length: %u\n",
2800 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002801 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002802 return -1;
2803 }
2804
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002805 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002806 /*
2807 * Special case for successfully execution w/ both DATAIN
2808 * and Sense Data.
2809 */
2810 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2811 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2812 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2813 else {
2814 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2815 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2816 iscsit_increment_maxcmdsn(cmd, conn->sess);
2817 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002818 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002819 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002820 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2821 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002822 }
2823
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002824 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002825
Varun Prakash2854bb22016-04-20 00:00:08 +05302826 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2827 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07002828 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002829
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002830 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002831 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002832 2 : 1;
2833 iscsit_free_datain_req(cmd, dr);
2834 }
2835
Andy Grover6f3c0e62012-04-03 15:51:09 -07002836 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002837}
2838
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002839int
2840iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2841 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002842{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002843 struct iscsi_conn *logout_conn = NULL;
2844 struct iscsi_conn_recovery *cr = NULL;
2845 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002846 /*
2847 * The actual shutting down of Sessions and/or Connections
2848 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2849 * is done in scsi_logout_post_handler().
2850 */
2851 switch (cmd->logout_reason) {
2852 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2853 pr_debug("iSCSI session logout successful, setting"
2854 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2855 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2856 break;
2857 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2858 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2859 break;
2860 /*
2861 * For CLOSECONNECTION logout requests carrying
2862 * a matching logout CID -> local CID, the reference
2863 * for the local CID will have been incremented in
2864 * iscsi_logout_closeconnection().
2865 *
2866 * For CLOSECONNECTION logout requests carrying
2867 * a different CID than the connection it arrived
2868 * on, the connection responding to cmd->logout_cid
2869 * is stopped in iscsit_logout_post_handler_diffcid().
2870 */
2871
2872 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2873 " successful.\n", cmd->logout_cid, conn->cid);
2874 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2875 break;
2876 case ISCSI_LOGOUT_REASON_RECOVERY:
2877 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2878 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2879 break;
2880 /*
2881 * If the connection is still active from our point of view
2882 * force connection recovery to occur.
2883 */
2884 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2885 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002886 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002887 iscsit_connection_reinstatement_rcfr(logout_conn);
2888 iscsit_dec_conn_usage_count(logout_conn);
2889 }
2890
2891 cr = iscsit_get_inactive_connection_recovery_entry(
2892 conn->sess, cmd->logout_cid);
2893 if (!cr) {
2894 pr_err("Unable to locate CID: %hu for"
2895 " REMOVECONNFORRECOVERY Logout Request.\n",
2896 cmd->logout_cid);
2897 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2898 break;
2899 }
2900
2901 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2902
2903 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2904 " for recovery for CID: %hu on CID: %hu successful.\n",
2905 cmd->logout_cid, conn->cid);
2906 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2907 break;
2908 default:
2909 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2910 cmd->logout_reason);
2911 return -1;
2912 }
2913
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002914 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2915 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2916 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002917 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002918 cmd->stat_sn = conn->stat_sn++;
2919 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2920
2921 iscsit_increment_maxcmdsn(cmd, conn->sess);
2922 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002923 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002924
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002925 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2926 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2927 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2928 cmd->logout_cid, conn->cid);
2929
2930 return 0;
2931}
2932EXPORT_SYMBOL(iscsit_build_logout_rsp);
2933
2934static int
2935iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2936{
Varun Prakash2854bb22016-04-20 00:00:08 +05302937 int rc;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002938
2939 rc = iscsit_build_logout_rsp(cmd, conn,
2940 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2941 if (rc < 0)
2942 return rc;
2943
Varun Prakash2854bb22016-04-20 00:00:08 +05302944 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002945}
2946
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002947void
2948iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2949 struct iscsi_nopin *hdr, bool nopout_response)
2950{
2951 hdr->opcode = ISCSI_OP_NOOP_IN;
2952 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2953 hton24(hdr->dlength, cmd->buf_ptr_size);
2954 if (nopout_response)
2955 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2956 hdr->itt = cmd->init_task_tag;
2957 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2958 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2959 conn->stat_sn;
2960 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2961
2962 if (nopout_response)
2963 iscsit_increment_maxcmdsn(cmd, conn->sess);
2964
2965 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002966 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002967
2968 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2969 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2970 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2971 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2972}
2973EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2974
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002975/*
2976 * Unsolicited NOPIN, either requesting a response or not.
2977 */
2978static int iscsit_send_unsolicited_nopin(
2979 struct iscsi_cmd *cmd,
2980 struct iscsi_conn *conn,
2981 int want_response)
2982{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002983 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Varun Prakash2854bb22016-04-20 00:00:08 +05302984 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002985
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002986 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002987
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002988 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2989 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2990
Varun Prakash2854bb22016-04-20 00:00:08 +05302991 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
2992 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07002993 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002994
2995 spin_lock_bh(&cmd->istate_lock);
2996 cmd->i_state = want_response ?
2997 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2998 spin_unlock_bh(&cmd->istate_lock);
2999
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003000 return 0;
3001}
3002
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003003static int
3004iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003005{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003006 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003007
3008 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003009
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003010 /*
3011 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3012 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3013 */
Varun Prakash2854bb22016-04-20 00:00:08 +05303014 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003015
Varun Prakash2854bb22016-04-20 00:00:08 +05303016 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3017 cmd->buf_ptr,
3018 cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003019}
3020
Andy Grover6f3c0e62012-04-03 15:51:09 -07003021static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003022 struct iscsi_cmd *cmd,
3023 struct iscsi_conn *conn)
3024{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003025 struct iscsi_r2t *r2t;
3026 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003027 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028
3029 r2t = iscsit_get_r2t_from_list(cmd);
3030 if (!r2t)
3031 return -1;
3032
3033 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3034 memset(hdr, 0, ISCSI_HDR_LEN);
3035 hdr->opcode = ISCSI_OP_R2T;
3036 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3037 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3038 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003039 hdr->itt = cmd->init_task_tag;
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02003040 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003041 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3042 hdr->statsn = cpu_to_be32(conn->stat_sn);
3043 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003044 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003045 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3046 hdr->data_offset = cpu_to_be32(r2t->offset);
3047 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3048
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003049 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3050 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3051 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3052 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3053 r2t->offset, r2t->xfer_len, conn->cid);
3054
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003055 spin_lock_bh(&cmd->r2t_lock);
3056 r2t->sent_r2t = 1;
3057 spin_unlock_bh(&cmd->r2t_lock);
3058
Varun Prakash2854bb22016-04-20 00:00:08 +05303059 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003060 if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003061 return ret;
3062 }
3063
3064 spin_lock_bh(&cmd->dataout_timeout_lock);
3065 iscsit_start_dataout_timer(cmd, conn);
3066 spin_unlock_bh(&cmd->dataout_timeout_lock);
3067
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003068 return 0;
3069}
3070
3071/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003072 * @recovery: If called from iscsi_task_reassign_complete_write() for
3073 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003074 */
3075int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003076 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003077 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003078 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003079{
3080 int first_r2t = 1;
3081 u32 offset = 0, xfer_len = 0;
3082
3083 spin_lock_bh(&cmd->r2t_lock);
3084 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3085 spin_unlock_bh(&cmd->r2t_lock);
3086 return 0;
3087 }
3088
Andy Grover8b1e1242012-04-03 15:51:12 -07003089 if (conn->sess->sess_ops->DataSequenceInOrder &&
3090 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003091 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003092
3093 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3094 if (conn->sess->sess_ops->DataSequenceInOrder) {
3095 offset = cmd->r2t_offset;
3096
Andy Grover8b1e1242012-04-03 15:51:12 -07003097 if (first_r2t && recovery) {
3098 int new_data_end = offset +
3099 conn->sess->sess_ops->MaxBurstLength -
3100 cmd->next_burst_len;
3101
Andy Groverebf1d952012-04-03 15:51:24 -07003102 if (new_data_end > cmd->se_cmd.data_length)
3103 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003104 else
3105 xfer_len =
3106 conn->sess->sess_ops->MaxBurstLength -
3107 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003108 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003109 int new_data_end = offset +
3110 conn->sess->sess_ops->MaxBurstLength;
3111
Andy Groverebf1d952012-04-03 15:51:24 -07003112 if (new_data_end > cmd->se_cmd.data_length)
3113 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003114 else
3115 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003116 }
3117 cmd->r2t_offset += xfer_len;
3118
Andy Groverebf1d952012-04-03 15:51:24 -07003119 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003120 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3121 } else {
3122 struct iscsi_seq *seq;
3123
3124 seq = iscsit_get_seq_holder_for_r2t(cmd);
3125 if (!seq) {
3126 spin_unlock_bh(&cmd->r2t_lock);
3127 return -1;
3128 }
3129
3130 offset = seq->offset;
3131 xfer_len = seq->xfer_len;
3132
3133 if (cmd->seq_send_order == cmd->seq_count)
3134 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3135 }
3136 cmd->outstanding_r2ts++;
3137 first_r2t = 0;
3138
3139 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3140 spin_unlock_bh(&cmd->r2t_lock);
3141 return -1;
3142 }
3143
3144 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3145 break;
3146 }
3147 spin_unlock_bh(&cmd->r2t_lock);
3148
3149 return 0;
3150}
3151
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003152void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3153 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003154{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003155 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003156 cmd->stat_sn = conn->stat_sn++;
3157
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003158 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003159
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003160 memset(hdr, 0, ISCSI_HDR_LEN);
3161 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3162 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3163 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3164 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003165 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003166 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3167 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003168 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003169 }
3170 hdr->response = cmd->iscsi_response;
3171 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003172 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003173 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3174
3175 iscsit_increment_maxcmdsn(cmd, conn->sess);
3176 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003177 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003178
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003179 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3180 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3181 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3182 cmd->se_cmd.scsi_status, conn->cid);
3183}
3184EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3185
3186static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3187{
3188 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003189 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
Varun Prakash2854bb22016-04-20 00:00:08 +05303190 void *data_buf = NULL;
3191 u32 padding = 0, data_buf_len = 0;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003192
3193 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3194
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003195 /*
3196 * Attach SENSE DATA payload to iSCSI Response PDU
3197 */
3198 if (cmd->se_cmd.sense_buffer &&
3199 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3200 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003201 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3202 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3203
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003204 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003205 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Varun Prakash2854bb22016-04-20 00:00:08 +05303206 data_buf = cmd->sense_buffer;
3207 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003208
3209 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003210 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003211 cmd->se_cmd.scsi_sense_length, 0, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003212 pr_debug("Adding %u bytes of padding to"
3213 " SENSE.\n", padding);
3214 }
3215
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003216 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3217 " Response PDU\n",
3218 cmd->se_cmd.scsi_sense_length);
3219 }
3220
Varun Prakash2854bb22016-04-20 00:00:08 +05303221 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3222 data_buf_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003223}
3224
3225static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3226{
3227 switch (se_tmr->response) {
3228 case TMR_FUNCTION_COMPLETE:
3229 return ISCSI_TMF_RSP_COMPLETE;
3230 case TMR_TASK_DOES_NOT_EXIST:
3231 return ISCSI_TMF_RSP_NO_TASK;
3232 case TMR_LUN_DOES_NOT_EXIST:
3233 return ISCSI_TMF_RSP_NO_LUN;
3234 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3235 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003236 case TMR_FUNCTION_REJECTED:
3237 default:
3238 return ISCSI_TMF_RSP_REJECTED;
3239 }
3240}
3241
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003242void
3243iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3244 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003245{
3246 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003247
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003248 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003249 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003250 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003251 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003252 cmd->stat_sn = conn->stat_sn++;
3253 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3254
3255 iscsit_increment_maxcmdsn(cmd, conn->sess);
3256 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003257 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003258
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003259 pr_debug("Built Task Management Response ITT: 0x%08x,"
3260 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3261 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3262}
3263EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3264
3265static int
3266iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3267{
3268 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003269
3270 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3271
Varun Prakash2854bb22016-04-20 00:00:08 +05303272 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003273}
3274
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003275static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3276{
3277 bool ret = false;
3278
3279 if (np->np_sockaddr.ss_family == AF_INET6) {
3280 const struct sockaddr_in6 sin6 = {
3281 .sin6_addr = IN6ADDR_ANY_INIT };
3282 struct sockaddr_in6 *sock_in6 =
3283 (struct sockaddr_in6 *)&np->np_sockaddr;
3284
3285 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3286 sin6.sin6_addr.s6_addr, 16))
3287 ret = true;
3288 } else {
3289 struct sockaddr_in * sock_in =
3290 (struct sockaddr_in *)&np->np_sockaddr;
3291
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003292 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003293 ret = true;
3294 }
3295
3296 return ret;
3297}
3298
Andy Grover8b1e1242012-04-03 15:51:12 -07003299#define SENDTARGETS_BUF_LIMIT 32768U
3300
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003301static int
3302iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003303 enum iscsit_transport_type network_transport,
3304 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003305{
3306 char *payload = NULL;
3307 struct iscsi_conn *conn = cmd->conn;
3308 struct iscsi_portal_group *tpg;
3309 struct iscsi_tiqn *tiqn;
3310 struct iscsi_tpg_np *tpg_np;
3311 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003312 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003313 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003314 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
David Disseldorpa6415cd2015-08-01 00:10:12 -07003315 bool active;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003316
Sagi Grimbergbe7dcfb2015-01-26 12:49:06 +02003317 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003318 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003319
3320 payload = kzalloc(buffer_len, GFP_KERNEL);
3321 if (!payload) {
3322 pr_err("Unable to allocate memory for sendtargets"
3323 " response.\n");
3324 return -ENOMEM;
3325 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003326 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003327 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003328 * explicit case..
3329 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003330 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003331 text_ptr = strchr(text_in, '=');
3332 if (!text_ptr) {
3333 pr_err("Unable to locate '=' string in text_in:"
3334 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003335 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003336 return -EINVAL;
3337 }
3338 /*
3339 * Skip over '=' character..
3340 */
3341 text_ptr += 1;
3342 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003343
3344 spin_lock(&tiqn_lock);
3345 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003346 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003347 strcmp(tiqn->tiqn, text_ptr)) {
3348 continue;
3349 }
3350
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003351 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003352
3353 spin_lock(&tiqn->tiqn_tpg_lock);
3354 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3355
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003356 /* If demo_mode_discovery=0 and generate_node_acls=0
3357 * (demo mode dislabed) do not return
3358 * TargetName+TargetAddress unless a NodeACL exists.
3359 */
3360
3361 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3362 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
Nicholas Bellinger21aaa232016-01-07 22:09:27 -08003363 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003364 cmd->conn->sess->sess_ops->InitiatorName))) {
3365 continue;
3366 }
3367
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003368 spin_lock(&tpg->tpg_state_lock);
David Disseldorpa6415cd2015-08-01 00:10:12 -07003369 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003370 spin_unlock(&tpg->tpg_state_lock);
3371
David Disseldorpa6415cd2015-08-01 00:10:12 -07003372 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3373 continue;
3374
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003375 spin_lock(&tpg->tpg_np_lock);
3376 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3377 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003378 struct iscsi_np *np = tpg_np->tpg_np;
3379 bool inaddr_any = iscsit_check_inaddr_any(np);
Andy Grover13a3cf02015-08-24 10:26:06 -07003380 struct sockaddr_storage *sockaddr;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003381
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003382 if (np->np_network_transport != network_transport)
3383 continue;
3384
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003385 if (!target_name_printed) {
3386 len = sprintf(buf, "TargetName=%s",
3387 tiqn->tiqn);
3388 len += 1;
3389
3390 if ((len + payload_len) > buffer_len) {
3391 spin_unlock(&tpg->tpg_np_lock);
3392 spin_unlock(&tiqn->tiqn_tpg_lock);
3393 end_of_buf = 1;
3394 goto eob;
3395 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003396
3397 if (skip_bytes && len <= skip_bytes) {
3398 skip_bytes -= len;
3399 } else {
3400 memcpy(payload + payload_len, buf, len);
3401 payload_len += len;
3402 target_name_printed = 1;
3403 if (len > skip_bytes)
3404 skip_bytes = 0;
3405 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003406 }
3407
Andy Grover69d75572015-08-24 10:26:04 -07003408 if (inaddr_any)
3409 sockaddr = &conn->local_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003410 else
Andy Grover69d75572015-08-24 10:26:04 -07003411 sockaddr = &np->np_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003412
Andy Grover69d75572015-08-24 10:26:04 -07003413 len = sprintf(buf, "TargetAddress="
3414 "%pISpc,%hu",
3415 sockaddr,
3416 tpg->tpgt);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003417 len += 1;
3418
3419 if ((len + payload_len) > buffer_len) {
3420 spin_unlock(&tpg->tpg_np_lock);
3421 spin_unlock(&tiqn->tiqn_tpg_lock);
3422 end_of_buf = 1;
3423 goto eob;
3424 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003425
3426 if (skip_bytes && len <= skip_bytes) {
3427 skip_bytes -= len;
3428 } else {
3429 memcpy(payload + payload_len, buf, len);
3430 payload_len += len;
3431 if (len > skip_bytes)
3432 skip_bytes = 0;
3433 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003434 }
3435 spin_unlock(&tpg->tpg_np_lock);
3436 }
3437 spin_unlock(&tiqn->tiqn_tpg_lock);
3438eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003439 if (end_of_buf) {
3440 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003441 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003442 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003443
Andy Grover8060b8d2015-01-09 15:13:08 -08003444 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003445 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003446 }
3447 spin_unlock(&tiqn_lock);
3448
3449 cmd->buf_ptr = payload;
3450
3451 return payload_len;
3452}
3453
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003454int
3455iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003456 struct iscsi_text_rsp *hdr,
3457 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003458{
3459 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003460 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003461
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003462 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3463 cmd->read_data_done,
3464 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003465 if (text_length < 0)
3466 return text_length;
3467
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003468 if (completed) {
3469 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3470 } else {
3471 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3472 cmd->read_data_done += text_length;
3473 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3474 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3475 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003476 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003477 padding = ((-text_length) & 3);
3478 hton24(hdr->dlength, text_length);
3479 hdr->itt = cmd->init_task_tag;
3480 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3481 cmd->stat_sn = conn->stat_sn++;
3482 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3483
3484 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003485 /*
3486 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3487 * correctly increment MaxCmdSN for each response answering a
3488 * non immediate text request with a valid CmdSN.
3489 */
3490 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003491 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003492 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003493
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003494 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3495 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3496 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3497 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3498 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003499
3500 return text_length + padding;
3501}
3502EXPORT_SYMBOL(iscsit_build_text_rsp);
3503
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003504static int iscsit_send_text_rsp(
3505 struct iscsi_cmd *cmd,
3506 struct iscsi_conn *conn)
3507{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003508 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Varun Prakash2854bb22016-04-20 00:00:08 +05303509 int text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003510
Varun Prakash2854bb22016-04-20 00:00:08 +05303511 text_length = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP);
3512 if (text_length < 0)
3513 return text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003514
Varun Prakash2854bb22016-04-20 00:00:08 +05303515 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3516 cmd->buf_ptr,
3517 text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003518}
3519
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003520void
3521iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3522 struct iscsi_reject *hdr)
3523{
3524 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003525 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003526 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3527 hton24(hdr->dlength, ISCSI_HDR_LEN);
3528 hdr->ffffffff = cpu_to_be32(0xffffffff);
3529 cmd->stat_sn = conn->stat_sn++;
3530 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3531 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003532 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003533
3534}
3535EXPORT_SYMBOL(iscsit_build_reject);
3536
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003537static int iscsit_send_reject(
3538 struct iscsi_cmd *cmd,
3539 struct iscsi_conn *conn)
3540{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003541 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003542
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003543 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003544
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003545 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3546 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3547
Varun Prakash2854bb22016-04-20 00:00:08 +05303548 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3549 cmd->buf_ptr,
3550 ISCSI_HDR_LEN);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003551}
3552
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003553void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3554{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003555 int ord, cpu;
3556 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003557 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3558 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003559 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003560 * Here we use bitmap_id to determine which CPU that this
3561 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003562 * execute upon.
3563 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003564 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003565 for_each_online_cpu(cpu) {
3566 if (ord-- == 0) {
3567 cpumask_set_cpu(cpu, conn->conn_cpumask);
3568 return;
3569 }
3570 }
3571 /*
3572 * This should never be reached..
3573 */
3574 dump_stack();
3575 cpumask_setall(conn->conn_cpumask);
3576}
3577
3578static inline void iscsit_thread_check_cpumask(
3579 struct iscsi_conn *conn,
3580 struct task_struct *p,
3581 int mode)
3582{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003583 /*
3584 * mode == 1 signals iscsi_target_tx_thread() usage.
3585 * mode == 0 signals iscsi_target_rx_thread() usage.
3586 */
3587 if (mode == 1) {
3588 if (!conn->conn_tx_reset_cpumask)
3589 return;
3590 conn->conn_tx_reset_cpumask = 0;
3591 } else {
3592 if (!conn->conn_rx_reset_cpumask)
3593 return;
3594 conn->conn_rx_reset_cpumask = 0;
3595 }
3596 /*
3597 * Update the CPU mask for this single kthread so that
3598 * both TX and RX kthreads are scheduled to run on the
3599 * same CPU.
3600 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003601 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3602}
3603
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003604static int
3605iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003606{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003607 int ret;
3608
3609 switch (state) {
3610 case ISTATE_SEND_R2T:
3611 ret = iscsit_send_r2t(cmd, conn);
3612 if (ret < 0)
3613 goto err;
3614 break;
3615 case ISTATE_REMOVE:
3616 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003617 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003618 spin_unlock_bh(&conn->cmd_lock);
3619
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003620 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003621 break;
3622 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3623 iscsit_mod_nopin_response_timer(conn);
3624 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3625 if (ret < 0)
3626 goto err;
3627 break;
3628 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3629 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3630 if (ret < 0)
3631 goto err;
3632 break;
3633 default:
3634 pr_err("Unknown Opcode: 0x%02x ITT:"
3635 " 0x%08x, i_state: %d on CID: %hu\n",
3636 cmd->iscsi_opcode, cmd->init_task_tag, state,
3637 conn->cid);
3638 goto err;
3639 }
3640
3641 return 0;
3642
3643err:
3644 return -1;
3645}
3646
3647static int
3648iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3649{
3650 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003651 struct iscsi_queue_req *qr;
3652 struct iscsi_cmd *cmd;
3653 u8 state;
3654 int ret;
3655
3656 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3657 atomic_set(&conn->check_immediate_queue, 0);
3658 cmd = qr->cmd;
3659 state = qr->state;
3660 kmem_cache_free(lio_qr_cache, qr);
3661
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003662 ret = t->iscsit_immediate_queue(conn, cmd, state);
3663 if (ret < 0)
3664 return ret;
3665 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003666
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003667 return 0;
3668}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003669
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003670static int
3671iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3672{
3673 int ret;
3674
3675check_rsp_state:
3676 switch (state) {
3677 case ISTATE_SEND_DATAIN:
3678 ret = iscsit_send_datain(cmd, conn);
3679 if (ret < 0)
3680 goto err;
3681 else if (!ret)
3682 /* more drs */
3683 goto check_rsp_state;
3684 else if (ret == 1) {
3685 /* all done */
3686 spin_lock_bh(&cmd->istate_lock);
3687 cmd->i_state = ISTATE_SENT_STATUS;
3688 spin_unlock_bh(&cmd->istate_lock);
3689
3690 if (atomic_read(&conn->check_immediate_queue))
3691 return 1;
3692
3693 return 0;
3694 } else if (ret == 2) {
3695 /* Still must send status,
3696 SCF_TRANSPORT_TASK_SENSE was set */
3697 spin_lock_bh(&cmd->istate_lock);
3698 cmd->i_state = ISTATE_SEND_STATUS;
3699 spin_unlock_bh(&cmd->istate_lock);
3700 state = ISTATE_SEND_STATUS;
3701 goto check_rsp_state;
3702 }
3703
3704 break;
3705 case ISTATE_SEND_STATUS:
3706 case ISTATE_SEND_STATUS_RECOVERY:
3707 ret = iscsit_send_response(cmd, conn);
3708 break;
3709 case ISTATE_SEND_LOGOUTRSP:
3710 ret = iscsit_send_logout(cmd, conn);
3711 break;
3712 case ISTATE_SEND_ASYNCMSG:
3713 ret = iscsit_send_conn_drop_async_message(
3714 cmd, conn);
3715 break;
3716 case ISTATE_SEND_NOPIN:
3717 ret = iscsit_send_nopin(cmd, conn);
3718 break;
3719 case ISTATE_SEND_REJECT:
3720 ret = iscsit_send_reject(cmd, conn);
3721 break;
3722 case ISTATE_SEND_TASKMGTRSP:
3723 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3724 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003725 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003726 ret = iscsit_tmr_post_handler(cmd, conn);
3727 if (ret != 0)
3728 iscsit_fall_back_to_erl0(conn->sess);
3729 break;
3730 case ISTATE_SEND_TEXTRSP:
3731 ret = iscsit_send_text_rsp(cmd, conn);
3732 break;
3733 default:
3734 pr_err("Unknown Opcode: 0x%02x ITT:"
3735 " 0x%08x, i_state: %d on CID: %hu\n",
3736 cmd->iscsi_opcode, cmd->init_task_tag,
3737 state, conn->cid);
3738 goto err;
3739 }
3740 if (ret < 0)
3741 goto err;
3742
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003743 switch (state) {
3744 case ISTATE_SEND_LOGOUTRSP:
3745 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003746 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003747 /* fall through */
3748 case ISTATE_SEND_STATUS:
3749 case ISTATE_SEND_ASYNCMSG:
3750 case ISTATE_SEND_NOPIN:
3751 case ISTATE_SEND_STATUS_RECOVERY:
3752 case ISTATE_SEND_TEXTRSP:
3753 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003754 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003755 spin_lock_bh(&cmd->istate_lock);
3756 cmd->i_state = ISTATE_SENT_STATUS;
3757 spin_unlock_bh(&cmd->istate_lock);
3758 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003759 default:
3760 pr_err("Unknown Opcode: 0x%02x ITT:"
3761 " 0x%08x, i_state: %d on CID: %hu\n",
3762 cmd->iscsi_opcode, cmd->init_task_tag,
3763 cmd->i_state, conn->cid);
3764 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003765 }
3766
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003767 if (atomic_read(&conn->check_immediate_queue))
3768 return 1;
3769
Andy Grover6f3c0e62012-04-03 15:51:09 -07003770 return 0;
3771
3772err:
3773 return -1;
3774}
3775
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003776static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003777{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003778 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003779 struct iscsi_queue_req *qr;
3780 struct iscsi_cmd *cmd;
3781 u8 state;
3782 int ret;
3783
3784 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3785 cmd = qr->cmd;
3786 state = qr->state;
3787 kmem_cache_free(lio_qr_cache, qr);
3788
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003789 ret = t->iscsit_response_queue(conn, cmd, state);
3790 if (ret == 1 || ret < 0)
3791 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003792 }
3793
3794 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003795}
3796
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003797int iscsi_target_tx_thread(void *arg)
3798{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003799 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003800 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003801 /*
3802 * Allow ourselves to be interrupted by SIGINT so that a
3803 * connection recovery / failure event can be triggered externally.
3804 */
3805 allow_signal(SIGINT);
3806
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003807 while (!kthread_should_stop()) {
3808 /*
3809 * Ensure that both TX and RX per connection kthreads
3810 * are scheduled to run on the same CPU.
3811 */
3812 iscsit_thread_check_cpumask(conn, current, 1);
3813
Roland Dreierd5627ac2012-10-31 09:16:46 -07003814 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003815 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003816
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003817 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003818 goto transport_err;
3819
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003820get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003821 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003822 if (ret < 0)
3823 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003824
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003825 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003826 if (ret == 1)
3827 goto get_immediate;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003828 else if (ret == -ECONNRESET)
3829 goto out;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003830 else if (ret < 0)
3831 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003832 }
3833
3834transport_err:
Nicholas Bellingere5419862015-07-22 23:14:19 -07003835 /*
3836 * Avoid the normal connection failure code-path if this connection
3837 * is still within LOGIN mode, and iscsi_np process context is
3838 * responsible for cleaning up the early connection failure.
3839 */
3840 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
3841 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003842out:
3843 return 0;
3844}
3845
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003846static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3847{
3848 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3849 struct iscsi_cmd *cmd;
3850 int ret = 0;
3851
3852 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3853 case ISCSI_OP_SCSI_CMD:
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_scsi_cmd(conn, cmd, buf);
3859 break;
3860 case ISCSI_OP_SCSI_DATA_OUT:
3861 ret = iscsit_handle_data_out(conn, buf);
3862 break;
3863 case ISCSI_OP_NOOP_OUT:
3864 cmd = NULL;
3865 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003866 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003867 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003868 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003869 }
3870 ret = iscsit_handle_nop_out(conn, cmd, buf);
3871 break;
3872 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003873 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003874 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003875 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003876
3877 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3878 break;
3879 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003880 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3881 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3882 if (!cmd)
3883 goto reject;
3884 } else {
3885 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3886 if (!cmd)
3887 goto reject;
3888 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003889
3890 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003891 break;
3892 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003893 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003894 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003895 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003896
3897 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3898 if (ret > 0)
3899 wait_for_completion_timeout(&conn->conn_logout_comp,
3900 SECONDS_FOR_LOGOUT_COMP * HZ);
3901 break;
3902 case ISCSI_OP_SNACK:
3903 ret = iscsit_handle_snack(conn, buf);
3904 break;
3905 default:
3906 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3907 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3908 pr_err("Cannot recover from unknown"
3909 " opcode while ERL=0, closing iSCSI connection.\n");
3910 return -1;
3911 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02003912 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3913 " closing iSCSI connection.\n");
3914 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003915 break;
3916 }
3917
3918 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003919reject:
3920 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003921}
3922
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08003923static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3924{
3925 bool ret;
3926
3927 spin_lock_bh(&conn->state_lock);
3928 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3929 spin_unlock_bh(&conn->state_lock);
3930
3931 return ret;
3932}
3933
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003934int iscsi_target_rx_thread(void *arg)
3935{
Nicholas Bellingere5419862015-07-22 23:14:19 -07003936 int ret, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003937 u8 buffer[ISCSI_HDR_LEN], opcode;
3938 u32 checksum = 0, digest = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003939 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003940 struct kvec iov;
3941 /*
3942 * Allow ourselves to be interrupted by SIGINT so that a
3943 * connection recovery / failure event can be triggered externally.
3944 */
3945 allow_signal(SIGINT);
Nicholas Bellingere5419862015-07-22 23:14:19 -07003946 /*
3947 * Wait for iscsi_post_login_handler() to complete before allowing
3948 * incoming iscsi/tcp socket I/O, and/or failing the connection.
3949 */
3950 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08003951 if (rc < 0 || iscsi_target_check_conn_state(conn))
Nicholas Bellingere5419862015-07-22 23:14:19 -07003952 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003953
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003954 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
3955 struct completion comp;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003956
3957 init_completion(&comp);
3958 rc = wait_for_completion_interruptible(&comp);
3959 if (rc < 0)
3960 goto transport_err;
3961
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003962 goto transport_err;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003963 }
3964
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003965 while (!kthread_should_stop()) {
3966 /*
3967 * Ensure that both TX and RX per connection kthreads
3968 * are scheduled to run on the same CPU.
3969 */
3970 iscsit_thread_check_cpumask(conn, current, 0);
3971
3972 memset(buffer, 0, ISCSI_HDR_LEN);
3973 memset(&iov, 0, sizeof(struct kvec));
3974
3975 iov.iov_base = buffer;
3976 iov.iov_len = ISCSI_HDR_LEN;
3977
3978 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3979 if (ret != ISCSI_HDR_LEN) {
3980 iscsit_rx_thread_wait_for_tcp(conn);
3981 goto transport_err;
3982 }
3983
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003984 if (conn->conn_ops->HeaderDigest) {
3985 iov.iov_base = &digest;
3986 iov.iov_len = ISCSI_CRC_LEN;
3987
3988 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3989 if (ret != ISCSI_CRC_LEN) {
3990 iscsit_rx_thread_wait_for_tcp(conn);
3991 goto transport_err;
3992 }
3993
Herbert Xu69110e32016-01-24 21:19:52 +08003994 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003995 buffer, ISCSI_HDR_LEN,
3996 0, NULL, (u8 *)&checksum);
3997
3998 if (digest != checksum) {
3999 pr_err("HeaderDigest CRC32C failed,"
4000 " received 0x%08x, computed 0x%08x\n",
4001 digest, checksum);
4002 /*
4003 * Set the PDU to 0xff so it will intentionally
4004 * hit default in the switch below.
4005 */
4006 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004007 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004008 } else {
4009 pr_debug("Got HeaderDigest CRC32C"
4010 " 0x%08x\n", checksum);
4011 }
4012 }
4013
4014 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4015 goto transport_err;
4016
4017 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4018
4019 if (conn->sess->sess_ops->SessionType &&
4020 ((!(opcode & ISCSI_OP_TEXT)) ||
4021 (!(opcode & ISCSI_OP_LOGOUT)))) {
4022 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4023 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004024 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4025 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004026 goto transport_err;
4027 }
4028
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004029 ret = iscsi_target_rx_opcode(conn, buffer);
4030 if (ret < 0)
4031 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004032 }
4033
4034transport_err:
4035 if (!signal_pending(current))
4036 atomic_set(&conn->transport_failed, 1);
4037 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004038 return 0;
4039}
4040
4041static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4042{
4043 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4044 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004045 /*
4046 * We expect this function to only ever be called from either RX or TX
4047 * thread context via iscsit_close_connection() once the other context
4048 * has been reset -> returned sleeping pre-handler state.
4049 */
4050 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004051 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004052
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004053 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004054 spin_unlock_bh(&conn->cmd_lock);
4055
4056 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004057
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004058 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004059
4060 spin_lock_bh(&conn->cmd_lock);
4061 }
4062 spin_unlock_bh(&conn->cmd_lock);
4063}
4064
4065static void iscsit_stop_timers_for_cmds(
4066 struct iscsi_conn *conn)
4067{
4068 struct iscsi_cmd *cmd;
4069
4070 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004071 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004072 if (cmd->data_direction == DMA_TO_DEVICE)
4073 iscsit_stop_dataout_timer(cmd);
4074 }
4075 spin_unlock_bh(&conn->cmd_lock);
4076}
4077
4078int iscsit_close_connection(
4079 struct iscsi_conn *conn)
4080{
4081 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4082 struct iscsi_session *sess = conn->sess;
4083
4084 pr_debug("Closing iSCSI connection CID %hu on SID:"
4085 " %u\n", conn->cid, sess->sid);
4086 /*
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004087 * Always up conn_logout_comp for the traditional TCP case just in case
4088 * the RX Thread in iscsi_target_rx_opcode() is sleeping and the logout
4089 * response never got sent because the connection failed.
4090 *
4091 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4092 * to signal logout response TX interrupt completion. Go ahead and skip
4093 * this for iser since isert_rx_opcode() does not wait on logout failure,
4094 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004095 */
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004096 if (conn->conn_transport->transport_type == ISCSI_TCP)
4097 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004098
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004099 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4100 if (conn->tx_thread &&
4101 cmpxchg(&conn->tx_thread_active, true, false)) {
4102 send_sig(SIGINT, conn->tx_thread, 1);
4103 kthread_stop(conn->tx_thread);
4104 }
4105 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4106 if (conn->rx_thread &&
4107 cmpxchg(&conn->rx_thread_active, true, false)) {
4108 send_sig(SIGINT, conn->rx_thread, 1);
4109 kthread_stop(conn->rx_thread);
4110 }
4111 }
4112
4113 spin_lock(&iscsit_global->ts_bitmap_lock);
4114 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4115 get_order(1));
4116 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004117
4118 iscsit_stop_timers_for_cmds(conn);
4119 iscsit_stop_nopin_response_timer(conn);
4120 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004121
4122 if (conn->conn_transport->iscsit_wait_conn)
4123 conn->conn_transport->iscsit_wait_conn(conn);
4124
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004125 /*
4126 * During Connection recovery drop unacknowledged out of order
4127 * commands for this connection, and prepare the other commands
4128 * for realligence.
4129 *
4130 * During normal operation clear the out of order commands (but
4131 * do not free the struct iscsi_ooo_cmdsn's) and release all
4132 * struct iscsi_cmds.
4133 */
4134 if (atomic_read(&conn->connection_recovery)) {
4135 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4136 iscsit_prepare_cmds_for_realligance(conn);
4137 } else {
4138 iscsit_clear_ooo_cmdsns_for_conn(conn);
4139 iscsit_release_commands_from_conn(conn);
4140 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004141 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004142
4143 /*
4144 * Handle decrementing session or connection usage count if
4145 * a logout response was not able to be sent because the
4146 * connection failed. Fall back to Session Recovery here.
4147 */
4148 if (atomic_read(&conn->conn_logout_remove)) {
4149 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4150 iscsit_dec_conn_usage_count(conn);
4151 iscsit_dec_session_usage_count(sess);
4152 }
4153 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4154 iscsit_dec_conn_usage_count(conn);
4155
4156 atomic_set(&conn->conn_logout_remove, 0);
4157 atomic_set(&sess->session_reinstatement, 0);
4158 atomic_set(&sess->session_fall_back_to_erl0, 1);
4159 }
4160
4161 spin_lock_bh(&sess->conn_lock);
4162 list_del(&conn->conn_list);
4163
4164 /*
4165 * Attempt to let the Initiator know this connection failed by
4166 * sending an Connection Dropped Async Message on another
4167 * active connection.
4168 */
4169 if (atomic_read(&conn->connection_recovery))
4170 iscsit_build_conn_drop_async_message(conn);
4171
4172 spin_unlock_bh(&sess->conn_lock);
4173
4174 /*
4175 * If connection reinstatement is being performed on this connection,
4176 * up the connection reinstatement semaphore that is being blocked on
4177 * in iscsit_cause_connection_reinstatement().
4178 */
4179 spin_lock_bh(&conn->state_lock);
4180 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4181 spin_unlock_bh(&conn->state_lock);
4182 complete(&conn->conn_wait_comp);
4183 wait_for_completion(&conn->conn_post_wait_comp);
4184 spin_lock_bh(&conn->state_lock);
4185 }
4186
4187 /*
4188 * If connection reinstatement is being performed on this connection
4189 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4190 * connection wait rcfr semaphore that is being blocked on
4191 * an iscsit_connection_reinstatement_rcfr().
4192 */
4193 if (atomic_read(&conn->connection_wait_rcfr)) {
4194 spin_unlock_bh(&conn->state_lock);
4195 complete(&conn->conn_wait_rcfr_comp);
4196 wait_for_completion(&conn->conn_post_wait_comp);
4197 spin_lock_bh(&conn->state_lock);
4198 }
4199 atomic_set(&conn->connection_reinstatement, 1);
4200 spin_unlock_bh(&conn->state_lock);
4201
4202 /*
4203 * If any other processes are accessing this connection pointer we
4204 * must wait until they have completed.
4205 */
4206 iscsit_check_conn_usage_count(conn);
4207
Herbert Xu69110e32016-01-24 21:19:52 +08004208 ahash_request_free(conn->conn_tx_hash);
4209 if (conn->conn_rx_hash) {
4210 struct crypto_ahash *tfm;
4211
4212 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4213 ahash_request_free(conn->conn_rx_hash);
4214 crypto_free_ahash(tfm);
4215 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004216
Joern Engelfbecb652014-09-02 17:49:47 -04004217 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004218
4219 kfree(conn->conn_ops);
4220 conn->conn_ops = NULL;
4221
Al Virobf6932f2012-07-21 08:55:18 +01004222 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004223 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004224
4225 if (conn->conn_transport->iscsit_free_conn)
4226 conn->conn_transport->iscsit_free_conn(conn);
4227
4228 iscsit_put_transport(conn->conn_transport);
4229
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004230 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4231 conn->conn_state = TARG_CONN_STATE_FREE;
4232 kfree(conn);
4233
4234 spin_lock_bh(&sess->conn_lock);
4235 atomic_dec(&sess->nconn);
4236 pr_debug("Decremented iSCSI connection count to %hu from node:"
4237 " %s\n", atomic_read(&sess->nconn),
4238 sess->sess_ops->InitiatorName);
4239 /*
4240 * Make sure that if one connection fails in an non ERL=2 iSCSI
4241 * Session that they all fail.
4242 */
4243 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4244 !atomic_read(&sess->session_logout))
4245 atomic_set(&sess->session_fall_back_to_erl0, 1);
4246
4247 /*
4248 * If this was not the last connection in the session, and we are
4249 * performing session reinstatement or falling back to ERL=0, call
4250 * iscsit_stop_session() without sleeping to shutdown the other
4251 * active connections.
4252 */
4253 if (atomic_read(&sess->nconn)) {
4254 if (!atomic_read(&sess->session_reinstatement) &&
4255 !atomic_read(&sess->session_fall_back_to_erl0)) {
4256 spin_unlock_bh(&sess->conn_lock);
4257 return 0;
4258 }
4259 if (!atomic_read(&sess->session_stop_active)) {
4260 atomic_set(&sess->session_stop_active, 1);
4261 spin_unlock_bh(&sess->conn_lock);
4262 iscsit_stop_session(sess, 0, 0);
4263 return 0;
4264 }
4265 spin_unlock_bh(&sess->conn_lock);
4266 return 0;
4267 }
4268
4269 /*
4270 * If this was the last connection in the session and one of the
4271 * following is occurring:
4272 *
4273 * Session Reinstatement is not being performed, and are falling back
4274 * to ERL=0 call iscsit_close_session().
4275 *
4276 * Session Logout was requested. iscsit_close_session() will be called
4277 * elsewhere.
4278 *
4279 * Session Continuation is not being performed, start the Time2Retain
4280 * handler and check if sleep_on_sess_wait_sem is active.
4281 */
4282 if (!atomic_read(&sess->session_reinstatement) &&
4283 atomic_read(&sess->session_fall_back_to_erl0)) {
4284 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004285 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004286
4287 return 0;
4288 } else if (atomic_read(&sess->session_logout)) {
4289 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4290 sess->session_state = TARG_SESS_STATE_FREE;
4291 spin_unlock_bh(&sess->conn_lock);
4292
4293 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4294 complete(&sess->session_wait_comp);
4295
4296 return 0;
4297 } else {
4298 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4299 sess->session_state = TARG_SESS_STATE_FAILED;
4300
4301 if (!atomic_read(&sess->session_continuation)) {
4302 spin_unlock_bh(&sess->conn_lock);
4303 iscsit_start_time2retain_handler(sess);
4304 } else
4305 spin_unlock_bh(&sess->conn_lock);
4306
4307 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4308 complete(&sess->session_wait_comp);
4309
4310 return 0;
4311 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004312}
4313
4314int iscsit_close_session(struct iscsi_session *sess)
4315{
Andy Grover60bfcf82013-10-09 11:05:58 -07004316 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004317 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4318
4319 if (atomic_read(&sess->nconn)) {
4320 pr_err("%d connection(s) still exist for iSCSI session"
4321 " to %s\n", atomic_read(&sess->nconn),
4322 sess->sess_ops->InitiatorName);
4323 BUG();
4324 }
4325
4326 spin_lock_bh(&se_tpg->session_lock);
4327 atomic_set(&sess->session_logout, 1);
4328 atomic_set(&sess->session_reinstatement, 1);
4329 iscsit_stop_time2retain_timer(sess);
4330 spin_unlock_bh(&se_tpg->session_lock);
4331
4332 /*
4333 * transport_deregister_session_configfs() will clear the
4334 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4335 * can be setting it again with __transport_register_session() in
4336 * iscsi_post_login_handler() again after the iscsit_stop_session()
4337 * completes in iscsi_np context.
4338 */
4339 transport_deregister_session_configfs(sess->se_sess);
4340
4341 /*
4342 * If any other processes are accessing this session pointer we must
4343 * wait until they have completed. If we are in an interrupt (the
4344 * time2retain handler) and contain and active session usage count we
4345 * restart the timer and exit.
4346 */
4347 if (!in_interrupt()) {
4348 if (iscsit_check_session_usage_count(sess) == 1)
4349 iscsit_stop_session(sess, 1, 1);
4350 } else {
4351 if (iscsit_check_session_usage_count(sess) == 2) {
4352 atomic_set(&sess->session_logout, 0);
4353 iscsit_start_time2retain_handler(sess);
4354 return 0;
4355 }
4356 }
4357
4358 transport_deregister_session(sess->se_sess);
4359
4360 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4361 iscsit_free_connection_recovery_entires(sess);
4362
4363 iscsit_free_all_ooo_cmdsns(sess);
4364
4365 spin_lock_bh(&se_tpg->session_lock);
4366 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4367 sess->session_state = TARG_SESS_STATE_FREE;
4368 pr_debug("Released iSCSI session from node: %s\n",
4369 sess->sess_ops->InitiatorName);
4370 tpg->nsessions--;
4371 if (tpg->tpg_tiqn)
4372 tpg->tpg_tiqn->tiqn_nsessions--;
4373
4374 pr_debug("Decremented number of active iSCSI Sessions on"
4375 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4376
4377 spin_lock(&sess_idr_lock);
4378 idr_remove(&sess_idr, sess->session_index);
4379 spin_unlock(&sess_idr_lock);
4380
4381 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 */
4403 if (conn->conn_transport->transport_type == ISCSI_TCP)
4404 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004405
4406 atomic_set(&conn->conn_logout_remove, 0);
4407 complete(&conn->conn_logout_comp);
4408
4409 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004410 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004411 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004412 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004413}
4414
4415static void iscsit_logout_post_handler_samecid(
4416 struct iscsi_conn *conn)
4417{
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004418 int sleep = 1;
4419
4420 if (conn->conn_transport->transport_type == ISCSI_TCP)
4421 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004422
4423 atomic_set(&conn->conn_logout_remove, 0);
4424 complete(&conn->conn_logout_comp);
4425
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004426 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004427 iscsit_dec_conn_usage_count(conn);
4428}
4429
4430static void iscsit_logout_post_handler_diffcid(
4431 struct iscsi_conn *conn,
4432 u16 cid)
4433{
4434 struct iscsi_conn *l_conn;
4435 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004436 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004437
4438 if (!sess)
4439 return;
4440
4441 spin_lock_bh(&sess->conn_lock);
4442 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4443 if (l_conn->cid == cid) {
4444 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004445 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004446 break;
4447 }
4448 }
4449 spin_unlock_bh(&sess->conn_lock);
4450
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004451 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004452 return;
4453
4454 if (l_conn->sock)
4455 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4456
4457 spin_lock_bh(&l_conn->state_lock);
4458 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4459 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4460 spin_unlock_bh(&l_conn->state_lock);
4461
4462 iscsit_cause_connection_reinstatement(l_conn, 1);
4463 iscsit_dec_conn_usage_count(l_conn);
4464}
4465
4466/*
4467 * Return of 0 causes the TX thread to restart.
4468 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004469int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004470 struct iscsi_cmd *cmd,
4471 struct iscsi_conn *conn)
4472{
4473 int ret = 0;
4474
4475 switch (cmd->logout_reason) {
4476 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4477 switch (cmd->logout_response) {
4478 case ISCSI_LOGOUT_SUCCESS:
4479 case ISCSI_LOGOUT_CLEANUP_FAILED:
4480 default:
4481 iscsit_logout_post_handler_closesession(conn);
4482 break;
4483 }
4484 ret = 0;
4485 break;
4486 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4487 if (conn->cid == cmd->logout_cid) {
4488 switch (cmd->logout_response) {
4489 case ISCSI_LOGOUT_SUCCESS:
4490 case ISCSI_LOGOUT_CLEANUP_FAILED:
4491 default:
4492 iscsit_logout_post_handler_samecid(conn);
4493 break;
4494 }
4495 ret = 0;
4496 } else {
4497 switch (cmd->logout_response) {
4498 case ISCSI_LOGOUT_SUCCESS:
4499 iscsit_logout_post_handler_diffcid(conn,
4500 cmd->logout_cid);
4501 break;
4502 case ISCSI_LOGOUT_CID_NOT_FOUND:
4503 case ISCSI_LOGOUT_CLEANUP_FAILED:
4504 default:
4505 break;
4506 }
4507 ret = 1;
4508 }
4509 break;
4510 case ISCSI_LOGOUT_REASON_RECOVERY:
4511 switch (cmd->logout_response) {
4512 case ISCSI_LOGOUT_SUCCESS:
4513 case ISCSI_LOGOUT_CID_NOT_FOUND:
4514 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4515 case ISCSI_LOGOUT_CLEANUP_FAILED:
4516 default:
4517 break;
4518 }
4519 ret = 1;
4520 break;
4521 default:
4522 break;
4523
4524 }
4525 return ret;
4526}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004527EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004528
4529void iscsit_fail_session(struct iscsi_session *sess)
4530{
4531 struct iscsi_conn *conn;
4532
4533 spin_lock_bh(&sess->conn_lock);
4534 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4535 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4536 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4537 }
4538 spin_unlock_bh(&sess->conn_lock);
4539
4540 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4541 sess->session_state = TARG_SESS_STATE_FAILED;
4542}
4543
4544int iscsit_free_session(struct iscsi_session *sess)
4545{
4546 u16 conn_count = atomic_read(&sess->nconn);
4547 struct iscsi_conn *conn, *conn_tmp = NULL;
4548 int is_last;
4549
4550 spin_lock_bh(&sess->conn_lock);
4551 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4552
4553 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4554 conn_list) {
4555 if (conn_count == 0)
4556 break;
4557
4558 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4559 is_last = 1;
4560 } else {
4561 iscsit_inc_conn_usage_count(conn_tmp);
4562 is_last = 0;
4563 }
4564 iscsit_inc_conn_usage_count(conn);
4565
4566 spin_unlock_bh(&sess->conn_lock);
4567 iscsit_cause_connection_reinstatement(conn, 1);
4568 spin_lock_bh(&sess->conn_lock);
4569
4570 iscsit_dec_conn_usage_count(conn);
4571 if (is_last == 0)
4572 iscsit_dec_conn_usage_count(conn_tmp);
4573
4574 conn_count--;
4575 }
4576
4577 if (atomic_read(&sess->nconn)) {
4578 spin_unlock_bh(&sess->conn_lock);
4579 wait_for_completion(&sess->session_wait_comp);
4580 } else
4581 spin_unlock_bh(&sess->conn_lock);
4582
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004583 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004584 return 0;
4585}
4586
4587void iscsit_stop_session(
4588 struct iscsi_session *sess,
4589 int session_sleep,
4590 int connection_sleep)
4591{
4592 u16 conn_count = atomic_read(&sess->nconn);
4593 struct iscsi_conn *conn, *conn_tmp = NULL;
4594 int is_last;
4595
4596 spin_lock_bh(&sess->conn_lock);
4597 if (session_sleep)
4598 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4599
4600 if (connection_sleep) {
4601 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4602 conn_list) {
4603 if (conn_count == 0)
4604 break;
4605
4606 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4607 is_last = 1;
4608 } else {
4609 iscsit_inc_conn_usage_count(conn_tmp);
4610 is_last = 0;
4611 }
4612 iscsit_inc_conn_usage_count(conn);
4613
4614 spin_unlock_bh(&sess->conn_lock);
4615 iscsit_cause_connection_reinstatement(conn, 1);
4616 spin_lock_bh(&sess->conn_lock);
4617
4618 iscsit_dec_conn_usage_count(conn);
4619 if (is_last == 0)
4620 iscsit_dec_conn_usage_count(conn_tmp);
4621 conn_count--;
4622 }
4623 } else {
4624 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4625 iscsit_cause_connection_reinstatement(conn, 0);
4626 }
4627
4628 if (session_sleep && atomic_read(&sess->nconn)) {
4629 spin_unlock_bh(&sess->conn_lock);
4630 wait_for_completion(&sess->session_wait_comp);
4631 } else
4632 spin_unlock_bh(&sess->conn_lock);
4633}
4634
4635int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4636{
4637 struct iscsi_session *sess;
4638 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4639 struct se_session *se_sess, *se_sess_tmp;
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004640 LIST_HEAD(free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004641 int session_count = 0;
4642
4643 spin_lock_bh(&se_tpg->session_lock);
4644 if (tpg->nsessions && !force) {
4645 spin_unlock_bh(&se_tpg->session_lock);
4646 return -1;
4647 }
4648
4649 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4650 sess_list) {
4651 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4652
4653 spin_lock(&sess->conn_lock);
4654 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4655 atomic_read(&sess->session_logout) ||
4656 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4657 spin_unlock(&sess->conn_lock);
4658 continue;
4659 }
4660 atomic_set(&sess->session_reinstatement, 1);
4661 spin_unlock(&sess->conn_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004662
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004663 list_move_tail(&se_sess->sess_list, &free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004664 }
4665 spin_unlock_bh(&se_tpg->session_lock);
4666
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004667 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4668 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4669
4670 iscsit_free_session(sess);
4671 session_count++;
4672 }
4673
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004674 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4675 " Group: %hu\n", session_count, tpg->tpgt);
4676 return 0;
4677}
4678
4679MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4680MODULE_VERSION("4.1.x");
4681MODULE_AUTHOR("nab@Linux-iSCSI.org");
4682MODULE_LICENSE("GPL");
4683
4684module_init(iscsi_target_init_module);
4685module_exit(iscsi_target_cleanup_module);