blob: 9a4f8957725b7d4ed216ae27be2e2472a40bad36 [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
Varun Prakashe8205cc2016-04-20 00:00:11 +0530481static void iscsit_get_rx_pdu(struct iscsi_conn *);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700482
Varun Prakashd2faaef2016-04-20 00:00:19 +0530483int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700484{
485 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
486 return 0;
487}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530488EXPORT_SYMBOL(iscsit_queue_rsp);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700489
Varun Prakashd2faaef2016-04-20 00:00:19 +0530490void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700491{
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}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530501EXPORT_SYMBOL(iscsit_aborted_task);
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700502
Varun Prakash2854bb22016-04-20 00:00:08 +0530503static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
504 u32, u32, u8 *, u8 *);
505static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
506
507static int
508iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
509 const void *data_buf, u32 data_buf_len)
510{
511 struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
512 struct kvec *iov;
513 u32 niov = 0, tx_size = ISCSI_HDR_LEN;
514 int ret;
515
516 iov = &cmd->iov_misc[0];
517 iov[niov].iov_base = cmd->pdu;
518 iov[niov++].iov_len = ISCSI_HDR_LEN;
519
520 if (conn->conn_ops->HeaderDigest) {
521 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
522
523 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
524 ISCSI_HDR_LEN, 0, NULL,
525 (u8 *)header_digest);
526
527 iov[0].iov_len += ISCSI_CRC_LEN;
528 tx_size += ISCSI_CRC_LEN;
529 pr_debug("Attaching CRC32C HeaderDigest"
530 " to opcode 0x%x 0x%08x\n",
531 hdr->opcode, *header_digest);
532 }
533
534 if (data_buf_len) {
535 u32 padding = ((-data_buf_len) & 3);
536
537 iov[niov].iov_base = (void *)data_buf;
538 iov[niov++].iov_len = data_buf_len;
539 tx_size += data_buf_len;
540
541 if (padding != 0) {
542 iov[niov].iov_base = &cmd->pad_bytes;
543 iov[niov++].iov_len = padding;
544 tx_size += padding;
545 pr_debug("Attaching %u additional"
546 " padding bytes.\n", padding);
547 }
548
549 if (conn->conn_ops->DataDigest) {
550 iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
551 data_buf, data_buf_len,
552 padding,
553 (u8 *)&cmd->pad_bytes,
554 (u8 *)&cmd->data_crc);
555
556 iov[niov].iov_base = &cmd->data_crc;
557 iov[niov++].iov_len = ISCSI_CRC_LEN;
558 tx_size += ISCSI_CRC_LEN;
559 pr_debug("Attached DataDigest for %u"
560 " bytes opcode 0x%x, CRC 0x%08x\n",
561 data_buf_len, hdr->opcode, cmd->data_crc);
562 }
563 }
564
565 cmd->iov_misc_count = niov;
566 cmd->tx_size = tx_size;
567
568 ret = iscsit_send_tx_data(cmd, conn, 1);
569 if (ret < 0) {
570 iscsit_tx_thread_wait_for_tcp(conn);
571 return ret;
572 }
573
574 return 0;
575}
576
577static int iscsit_map_iovec(struct iscsi_cmd *, struct kvec *, u32, u32);
578static void iscsit_unmap_iovec(struct iscsi_cmd *);
579static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
580 u32, u32, u32, u8 *);
581static int
582iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
583 const struct iscsi_datain *datain)
584{
585 struct kvec *iov;
586 u32 iov_count = 0, tx_size = 0;
587 int ret, iov_ret;
588
589 iov = &cmd->iov_data[0];
590 iov[iov_count].iov_base = cmd->pdu;
591 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
592 tx_size += ISCSI_HDR_LEN;
593
594 if (conn->conn_ops->HeaderDigest) {
595 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
596
597 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
598 ISCSI_HDR_LEN, 0, NULL,
599 (u8 *)header_digest);
600
601 iov[0].iov_len += ISCSI_CRC_LEN;
602 tx_size += ISCSI_CRC_LEN;
603
604 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
605 *header_digest);
606 }
607
608 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
609 datain->offset, datain->length);
610 if (iov_ret < 0)
611 return -1;
612
613 iov_count += iov_ret;
614 tx_size += datain->length;
615
616 cmd->padding = ((-datain->length) & 3);
617 if (cmd->padding) {
618 iov[iov_count].iov_base = cmd->pad_bytes;
619 iov[iov_count++].iov_len = cmd->padding;
620 tx_size += cmd->padding;
621
622 pr_debug("Attaching %u padding bytes\n", cmd->padding);
623 }
624
625 if (conn->conn_ops->DataDigest) {
626 cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
627 cmd, datain->offset,
628 datain->length,
629 cmd->padding,
630 cmd->pad_bytes);
631
632 iov[iov_count].iov_base = &cmd->data_crc;
633 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
634 tx_size += ISCSI_CRC_LEN;
635
636 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
637 datain->length + cmd->padding, cmd->data_crc);
638 }
639
640 cmd->iov_data_count = iov_count;
641 cmd->tx_size = tx_size;
642
643 ret = iscsit_fe_sendpage_sg(cmd, conn);
644
645 iscsit_unmap_iovec(cmd);
646
647 if (ret < 0) {
648 iscsit_tx_thread_wait_for_tcp(conn);
649 return ret;
650 }
651
652 return 0;
653}
654
655static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
656 struct iscsi_datain_req *dr, const void *buf,
657 u32 buf_len)
658{
659 if (dr)
660 return iscsit_xmit_datain_pdu(conn, cmd, buf);
661 else
662 return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
663}
664
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700665static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
666{
667 return TARGET_PROT_NORMAL;
668}
669
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800670static struct iscsit_transport iscsi_target_transport = {
671 .name = "iSCSI/TCP",
672 .transport_type = ISCSI_TCP,
673 .owner = NULL,
674 .iscsit_setup_np = iscsit_setup_np,
675 .iscsit_accept_np = iscsit_accept_np,
676 .iscsit_free_np = iscsit_free_np,
677 .iscsit_get_login_rx = iscsit_get_login_rx,
678 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800679 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700680 .iscsit_immediate_queue = iscsit_immediate_queue,
681 .iscsit_response_queue = iscsit_response_queue,
682 .iscsit_queue_data_in = iscsit_queue_rsp,
683 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700684 .iscsit_aborted_task = iscsit_aborted_task,
Varun Prakash2854bb22016-04-20 00:00:08 +0530685 .iscsit_xmit_pdu = iscsit_xmit_pdu,
Varun Prakashe8205cc2016-04-20 00:00:11 +0530686 .iscsit_get_rx_pdu = iscsit_get_rx_pdu,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700687 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800688};
689
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000690static int __init iscsi_target_init_module(void)
691{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800692 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000693
694 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
695
696 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
697 if (!iscsit_global) {
698 pr_err("Unable to allocate memory for iscsit_global\n");
699 return -1;
700 }
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800701 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000702 mutex_init(&auth_id_lock);
703 spin_lock_init(&sess_idr_lock);
704 idr_init(&tiqn_idr);
705 idr_init(&sess_idr);
706
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200707 ret = target_register_template(&iscsi_ops);
708 if (ret)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000709 goto out;
710
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800711 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
712 iscsit_global->ts_bitmap = vzalloc(size);
713 if (!iscsit_global->ts_bitmap) {
714 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000715 goto configfs_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000716 }
717
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000718 lio_qr_cache = kmem_cache_create("lio_qr_cache",
719 sizeof(struct iscsi_queue_req),
720 __alignof__(struct iscsi_queue_req), 0, NULL);
721 if (!lio_qr_cache) {
722 pr_err("nable to kmem_cache_create() for"
723 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800724 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000725 }
726
727 lio_dr_cache = kmem_cache_create("lio_dr_cache",
728 sizeof(struct iscsi_datain_req),
729 __alignof__(struct iscsi_datain_req), 0, NULL);
730 if (!lio_dr_cache) {
731 pr_err("Unable to kmem_cache_create() for"
732 " lio_dr_cache\n");
733 goto qr_out;
734 }
735
736 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
737 sizeof(struct iscsi_ooo_cmdsn),
738 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
739 if (!lio_ooo_cache) {
740 pr_err("Unable to kmem_cache_create() for"
741 " lio_ooo_cache\n");
742 goto dr_out;
743 }
744
745 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
746 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
747 0, NULL);
748 if (!lio_r2t_cache) {
749 pr_err("Unable to kmem_cache_create() for"
750 " lio_r2t_cache\n");
751 goto ooo_out;
752 }
753
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800754 iscsit_register_transport(&iscsi_target_transport);
755
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000756 if (iscsit_load_discovery_tpg() < 0)
757 goto r2t_out;
758
759 return ret;
760r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100761 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000762 kmem_cache_destroy(lio_r2t_cache);
763ooo_out:
764 kmem_cache_destroy(lio_ooo_cache);
765dr_out:
766 kmem_cache_destroy(lio_dr_cache);
767qr_out:
768 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800769bitmap_out:
770 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000771configfs_out:
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200772 /* XXX: this probably wants it to be it's own unwind step.. */
773 if (iscsit_global->discovery_tpg)
774 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
775 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000776out:
777 kfree(iscsit_global);
778 return -ENOMEM;
779}
780
781static void __exit iscsi_target_cleanup_module(void)
782{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000783 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800784 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000785 kmem_cache_destroy(lio_qr_cache);
786 kmem_cache_destroy(lio_dr_cache);
787 kmem_cache_destroy(lio_ooo_cache);
788 kmem_cache_destroy(lio_r2t_cache);
789
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200790 /*
791 * Shutdown discovery sessions and disable discovery TPG
792 */
793 if (iscsit_global->discovery_tpg)
794 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000795
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200796 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000797
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800798 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000799 kfree(iscsit_global);
800}
801
Varun Prakashd2faaef2016-04-20 00:00:19 +0530802int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700803 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000804 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700805 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000806{
807 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000808
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000809 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000810 if (!cmd)
811 return -1;
812
813 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700814 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000815
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100816 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000817 if (!cmd->buf_ptr) {
818 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700819 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000820 return -1;
821 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000822
823 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700824 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000825 spin_unlock_bh(&conn->cmd_lock);
826
827 cmd->i_state = ISTATE_SEND_REJECT;
828 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
829
Nicholas Bellingerba159912013-07-03 03:48:24 -0700830 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000831}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530832EXPORT_SYMBOL(iscsit_add_reject);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000833
Nicholas Bellingerba159912013-07-03 03:48:24 -0700834static int iscsit_add_reject_from_cmd(
835 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000836 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700837 bool add_to_conn,
838 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000839{
840 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000841
842 if (!cmd->conn) {
843 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
844 cmd->init_task_tag);
845 return -1;
846 }
847 conn = cmd->conn;
848
849 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700850 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000851
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100852 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000853 if (!cmd->buf_ptr) {
854 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700855 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000856 return -1;
857 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000858
859 if (add_to_conn) {
860 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700861 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000862 spin_unlock_bh(&conn->cmd_lock);
863 }
864
865 cmd->i_state = ISTATE_SEND_REJECT;
866 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800867 /*
868 * Perform the kref_put now if se_cmd has already been setup by
869 * scsit_setup_scsi_cmd()
870 */
871 if (cmd->se_cmd.se_tfo != NULL) {
872 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +0200873 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800874 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700875 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000876}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700877
878static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
879 unsigned char *buf)
880{
881 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
882}
883
884int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
885{
886 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
887}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530888EXPORT_SYMBOL(iscsit_reject_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000889
890/*
891 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700892 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000893 */
894static int iscsit_map_iovec(
895 struct iscsi_cmd *cmd,
896 struct kvec *iov,
897 u32 data_offset,
898 u32 data_length)
899{
900 u32 i = 0;
901 struct scatterlist *sg;
902 unsigned int page_off;
903
904 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700905 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000906 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700907 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000908 page_off = (data_offset % PAGE_SIZE);
909
910 cmd->first_data_sg = sg;
911 cmd->first_data_sg_off = page_off;
912
913 while (data_length) {
914 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
915
916 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
917 iov[i].iov_len = cur_len;
918
919 data_length -= cur_len;
920 page_off = 0;
921 sg = sg_next(sg);
922 i++;
923 }
924
925 cmd->kmapped_nents = i;
926
927 return i;
928}
929
930static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
931{
932 u32 i;
933 struct scatterlist *sg;
934
935 sg = cmd->first_data_sg;
936
937 for (i = 0; i < cmd->kmapped_nents; i++)
938 kunmap(sg_page(&sg[i]));
939}
940
941static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
942{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700943 LIST_HEAD(ack_list);
944 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000945
946 conn->exp_statsn = exp_statsn;
947
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800948 if (conn->sess->sess_ops->RDMAExtensions)
949 return;
950
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000951 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700952 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000953 spin_lock(&cmd->istate_lock);
954 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800955 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000956 cmd->i_state = ISTATE_REMOVE;
957 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700958 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000959 continue;
960 }
961 spin_unlock(&cmd->istate_lock);
962 }
963 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700964
965 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800966 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700967 iscsit_free_cmd(cmd, false);
968 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000969}
970
971static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
972{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700973 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000974
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400975 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000976
977 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
978 if (!cmd->iov_data) {
979 pr_err("Unable to allocate cmd->iov_data\n");
980 return -ENOMEM;
981 }
982
983 cmd->orig_iov_data_count = iov_count;
984 return 0;
985}
986
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800987int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
988 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000989{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800990 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000991 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700992 int iscsi_task_attr;
993 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000994
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800995 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000996
997 hdr = (struct iscsi_scsi_req *) buf;
998 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000999
1000 /* FIXME; Add checks for AdditionalHeaderSegment */
1001
1002 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
1003 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1004 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1005 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001006 return iscsit_add_reject_cmd(cmd,
1007 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001008 }
1009
1010 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1011 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1012 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001013 * From RFC-3720 Section 10.3.1:
1014 *
1015 * "Either or both of R and W MAY be 1 when either the
1016 * Expected Data Transfer Length and/or Bidirectional Read
1017 * Expected Data Transfer Length are 0"
1018 *
1019 * For this case, go ahead and clear the unnecssary bits
1020 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001021 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001022 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1023 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001024
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001025 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001026 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001027 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001028 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001029
1030 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1031 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1032 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1033 " MUST be set if Expected Data Transfer Length is not 0."
1034 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001035 return iscsit_add_reject_cmd(cmd,
1036 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001037 }
1038
1039 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1040 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1041 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001042 return iscsit_add_reject_cmd(cmd,
1043 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001044 }
1045
1046 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1047 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1048 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001049 return iscsit_add_reject_cmd(cmd,
1050 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001051 }
1052
1053 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1054 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1055 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001056 return iscsit_add_reject_cmd(cmd,
1057 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001058 }
1059
Nicholas Bellingerba159912013-07-03 03:48:24 -07001060 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001061 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1062 pr_err("Expected Data Transfer Length and Length of"
1063 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1064 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001065 return iscsit_add_reject_cmd(cmd,
1066 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001067 }
1068
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001069 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001070 pr_err("DataSegmentLength: %u is greater than"
1071 " EDTL: %u, protocol error.\n", payload_length,
1072 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001073 return iscsit_add_reject_cmd(cmd,
1074 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001075 }
1076
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001077 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001078 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001079 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1080 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001081 return iscsit_add_reject_cmd(cmd,
1082 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001083 }
1084
1085 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1086 pr_err("DataSegmentLength: %u is greater than"
1087 " FirstBurstLength: %u, protocol error.\n",
1088 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001089 return iscsit_add_reject_cmd(cmd,
1090 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001091 }
1092
1093 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1094 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1095 DMA_NONE;
1096
Andy Groverd28b11692012-04-03 15:51:22 -07001097 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -07001098 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1099 /*
1100 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1101 */
1102 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1103 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001104 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001105 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001106 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001107 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001108 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001109 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001110 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001111 else {
1112 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001113 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
1114 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001115 }
1116
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001117 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
1118 cmd->i_state = ISTATE_NEW_CMD;
1119 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1120 cmd->immediate_data = (payload_length) ? 1 : 0;
1121 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1122 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1123 if (cmd->unsolicited_data)
1124 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1125
1126 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
Alexei Potashnik95473082015-07-21 15:07:56 -07001127 if (hdr->flags & ISCSI_FLAG_CMD_READ)
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02001128 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Alexei Potashnik95473082015-07-21 15:07:56 -07001129 else
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001130 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001131 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1132 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001133 cmd->first_burst_len = payload_length;
1134
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001135 if (!conn->sess->sess_ops->RDMAExtensions &&
1136 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001137 struct iscsi_datain_req *dr;
1138
1139 dr = iscsit_allocate_datain_req();
1140 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001141 return iscsit_add_reject_cmd(cmd,
1142 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001143
1144 iscsit_attach_datain_req(cmd, dr);
1145 }
1146
1147 /*
Andy Grover065ca1e2012-04-03 15:51:23 -07001148 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1149 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001150 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001151 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1152 cmd->data_direction, sam_task_attr,
1153 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -07001154
1155 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1156 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001157 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1158 conn->cid);
1159
Bart Van Asscheafc16602015-04-27 13:52:36 +02001160 target_get_sess_cmd(&cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001161
Christoph Hellwigde103c92012-11-06 12:24:09 -08001162 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1163 scsilun_to_int(&hdr->lun));
1164 if (cmd->sense_reason)
1165 goto attach_cmd;
1166
Bart Van Assche649ee052015-04-14 13:26:44 +02001167 /* only used for printks or comparing with ->ref_task_tag */
1168 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001169 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1170 if (cmd->sense_reason) {
1171 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001172 return iscsit_add_reject_cmd(cmd,
1173 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001174 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001175
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001176 goto attach_cmd;
1177 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001178
Christoph Hellwigde103c92012-11-06 12:24:09 -08001179 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001180 return iscsit_add_reject_cmd(cmd,
1181 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001182 }
1183
1184attach_cmd:
1185 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001186 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001187 spin_unlock_bh(&conn->cmd_lock);
1188 /*
1189 * Check if we need to delay processing because of ALUA
1190 * Active/NonOptimized primary access state..
1191 */
1192 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001193
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001194 return 0;
1195}
1196EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001197
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001198void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1199{
1200 iscsit_set_dataout_sequence_values(cmd);
1201
1202 spin_lock_bh(&cmd->dataout_timeout_lock);
1203 iscsit_start_dataout_timer(cmd, cmd->conn);
1204 spin_unlock_bh(&cmd->dataout_timeout_lock);
1205}
1206EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1207
1208int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1209 struct iscsi_scsi_req *hdr)
1210{
1211 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001212 /*
1213 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1214 * the Immediate Bit is not set, and no Immediate
1215 * Data is attached.
1216 *
1217 * A PDU/CmdSN carrying Immediate Data can only
1218 * be processed after the DataCRC has passed.
1219 * If the DataCRC fails, the CmdSN MUST NOT
1220 * be acknowledged. (See below)
1221 */
1222 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001223 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1224 (unsigned char *)hdr, hdr->cmdsn);
1225 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1226 return -1;
1227 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001228 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001229 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001230 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001231 }
1232
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001233 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001234
1235 /*
1236 * If no Immediate Data is attached, it's OK to return now.
1237 */
1238 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001239 if (!cmd->sense_reason && cmd->unsolicited_data)
1240 iscsit_set_unsoliticed_dataout(cmd);
1241 if (!cmd->sense_reason)
1242 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001243
Bart Van Asscheafc16602015-04-27 13:52:36 +02001244 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001245 return 0;
1246 }
1247
1248 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001249 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1250 * execution. These exceptions are processed in CmdSN order using
1251 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001252 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001253 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001254 if (cmd->reject_reason)
1255 return 0;
1256
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001257 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001258 }
1259 /*
1260 * Call directly into transport_generic_new_cmd() to perform
1261 * the backend memory allocation.
1262 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001263 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001264 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001265 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001266
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001267 return 0;
1268}
1269EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1270
1271static int
1272iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1273 bool dump_payload)
1274{
1275 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1276 /*
1277 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1278 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001279 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001280 goto after_immediate_data;
1281
1282 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1283 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001284after_immediate_data:
1285 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1286 /*
1287 * A PDU/CmdSN carrying Immediate Data passed
1288 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1289 * Immediate Bit is not set.
1290 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001291 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1292 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001293 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001294 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001295
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001296 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001297 int rc;
1298
1299 rc = iscsit_dump_data_payload(cmd->conn,
1300 cmd->first_burst_len, 1);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001301 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001302 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001303 } else if (cmd->unsolicited_data)
1304 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001305
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001306 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1307 /*
1308 * Immediate Data failed DataCRC and ERL>=1,
1309 * silently drop this PDU and let the initiator
1310 * plug the CmdSN gap.
1311 *
1312 * FIXME: Send Unsolicited NOPIN with reserved
1313 * TTT here to help the initiator figure out
1314 * the missing CmdSN, although they should be
1315 * intelligent enough to determine the missing
1316 * CmdSN and issue a retry to plug the sequence.
1317 */
1318 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001319 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001320 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1321 return -1;
1322
1323 return 0;
1324}
1325
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001326static int
1327iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1328 unsigned char *buf)
1329{
1330 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1331 int rc, immed_data;
1332 bool dump_payload = false;
1333
1334 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1335 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001336 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001337 /*
1338 * Allocation iovecs needed for struct socket operations for
1339 * traditional iSCSI block I/O.
1340 */
1341 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001342 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001343 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001344 }
1345 immed_data = cmd->immediate_data;
1346
1347 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1348 if (rc < 0)
1349 return rc;
1350 else if (rc > 0)
1351 dump_payload = true;
1352
1353 if (!immed_data)
1354 return 0;
1355
1356 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1357}
1358
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001359static u32 iscsit_do_crypto_hash_sg(
Herbert Xu69110e32016-01-24 21:19:52 +08001360 struct ahash_request *hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001361 struct iscsi_cmd *cmd,
1362 u32 data_offset,
1363 u32 data_length,
1364 u32 padding,
1365 u8 *pad_bytes)
1366{
1367 u32 data_crc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001368 struct scatterlist *sg;
1369 unsigned int page_off;
1370
Herbert Xu69110e32016-01-24 21:19:52 +08001371 crypto_ahash_init(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001372
1373 sg = cmd->first_data_sg;
1374 page_off = cmd->first_data_sg_off;
1375
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001376 while (data_length) {
Alexei Potashnikaa756792015-07-20 17:12:12 -07001377 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001378
Herbert Xu69110e32016-01-24 21:19:52 +08001379 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1380 crypto_ahash_update(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001381
1382 data_length -= cur_len;
1383 page_off = 0;
Alexei Potashnikaa756792015-07-20 17:12:12 -07001384 /* iscsit_map_iovec has already checked for invalid sg pointers */
1385 sg = sg_next(sg);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001386 }
1387
1388 if (padding) {
1389 struct scatterlist pad_sg;
1390
1391 sg_init_one(&pad_sg, pad_bytes, padding);
Herbert Xu69110e32016-01-24 21:19:52 +08001392 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1393 padding);
1394 crypto_ahash_finup(hash);
1395 } else {
1396 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1397 crypto_ahash_final(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001398 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001399
1400 return data_crc;
1401}
1402
1403static void iscsit_do_crypto_hash_buf(
Herbert Xu69110e32016-01-24 21:19:52 +08001404 struct ahash_request *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001405 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001406 u32 payload_length,
1407 u32 padding,
1408 u8 *pad_bytes,
1409 u8 *data_crc)
1410{
Herbert Xu69110e32016-01-24 21:19:52 +08001411 struct scatterlist sg[2];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001412
Herbert Xu69110e32016-01-24 21:19:52 +08001413 sg_init_table(sg, ARRAY_SIZE(sg));
1414 sg_set_buf(sg, buf, payload_length);
1415 sg_set_buf(sg + 1, pad_bytes, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001416
Herbert Xu69110e32016-01-24 21:19:52 +08001417 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001418
Herbert Xu69110e32016-01-24 21:19:52 +08001419 crypto_ahash_digest(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001420}
1421
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001422int
1423iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1424 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001425{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001426 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001427 struct iscsi_cmd *cmd = NULL;
1428 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001429 u32 payload_length = ntoh24(hdr->dlength);
1430 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001431
1432 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001433 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1434 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001435 }
1436
1437 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001438 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001439
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001440 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001441 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001442 " MaxXmitDataSegmentLength: %u\n", payload_length,
1443 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001444 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1445 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001446 }
1447
1448 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1449 payload_length);
1450 if (!cmd)
1451 return 0;
1452
1453 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1454 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001455 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001456 payload_length, conn->cid);
1457
1458 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1459 pr_err("Command ITT: 0x%08x received DataOUT after"
1460 " last DataOUT received, dumping payload\n",
1461 cmd->init_task_tag);
1462 return iscsit_dump_data_payload(conn, payload_length, 1);
1463 }
1464
1465 if (cmd->data_direction != DMA_TO_DEVICE) {
1466 pr_err("Command ITT: 0x%08x received DataOUT for a"
1467 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001468 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001469 }
1470 se_cmd = &cmd->se_cmd;
1471 iscsit_mod_dataout_timer(cmd);
1472
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001473 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001474 pr_err("DataOut Offset: %u, Length %u greater than"
1475 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001476 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001477 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001478 }
1479
1480 if (cmd->unsolicited_data) {
1481 int dump_unsolicited_data = 0;
1482
1483 if (conn->sess->sess_ops->InitialR2T) {
1484 pr_err("Received unexpected unsolicited data"
1485 " while InitialR2T=Yes, protocol error.\n");
1486 transport_send_check_condition_and_sense(&cmd->se_cmd,
1487 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1488 return -1;
1489 }
1490 /*
1491 * Special case for dealing with Unsolicited DataOUT
1492 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1493 * failures;
1494 */
1495
1496 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001497 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001498 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001499 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001500
1501 if (dump_unsolicited_data) {
1502 /*
1503 * Check if a delayed TASK_ABORTED status needs to
1504 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
Bart Van Assche5a342522015-10-22 15:53:22 -07001505 * received with the unsolicited data out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001506 */
1507 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1508 iscsit_stop_dataout_timer(cmd);
1509
1510 transport_check_aborted_status(se_cmd,
1511 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1512 return iscsit_dump_data_payload(conn, payload_length, 1);
1513 }
1514 } else {
1515 /*
1516 * For the normal solicited data path:
1517 *
1518 * Check for a delayed TASK_ABORTED status and dump any
1519 * incoming data out payload if one exists. Also, when the
1520 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1521 * data out sequence, we decrement outstanding_r2ts. Once
1522 * outstanding_r2ts reaches zero, go ahead and send the delayed
1523 * TASK_ABORTED status.
1524 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001525 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001526 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1527 if (--cmd->outstanding_r2ts < 1) {
1528 iscsit_stop_dataout_timer(cmd);
1529 transport_check_aborted_status(
1530 se_cmd, 1);
1531 }
1532
1533 return iscsit_dump_data_payload(conn, payload_length, 1);
1534 }
1535 }
1536 /*
1537 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1538 * within-command recovery checks before receiving the payload.
1539 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001540 rc = iscsit_check_pre_dataout(cmd, buf);
1541 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001542 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001543 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001544 return -1;
1545
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001546 *out_cmd = cmd;
1547 return 0;
1548}
1549EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1550
1551static int
1552iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1553 struct iscsi_data *hdr)
1554{
1555 struct kvec *iov;
1556 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1557 u32 payload_length = ntoh24(hdr->dlength);
1558 int iov_ret, data_crc_failed = 0;
1559
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001560 rx_size += payload_length;
1561 iov = &cmd->iov_data[0];
1562
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001563 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1564 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001565 if (iov_ret < 0)
1566 return -1;
1567
1568 iov_count += iov_ret;
1569
1570 padding = ((-payload_length) & 3);
1571 if (padding != 0) {
1572 iov[iov_count].iov_base = cmd->pad_bytes;
1573 iov[iov_count++].iov_len = padding;
1574 rx_size += padding;
1575 pr_debug("Receiving %u padding bytes.\n", padding);
1576 }
1577
1578 if (conn->conn_ops->DataDigest) {
1579 iov[iov_count].iov_base = &checksum;
1580 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1581 rx_size += ISCSI_CRC_LEN;
1582 }
1583
1584 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1585
1586 iscsit_unmap_iovec(cmd);
1587
1588 if (rx_got != rx_size)
1589 return -1;
1590
1591 if (conn->conn_ops->DataDigest) {
1592 u32 data_crc;
1593
Herbert Xu69110e32016-01-24 21:19:52 +08001594 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001595 be32_to_cpu(hdr->offset),
1596 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001597 cmd->pad_bytes);
1598
1599 if (checksum != data_crc) {
1600 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1601 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1602 " does not match computed 0x%08x\n",
1603 hdr->itt, hdr->offset, payload_length,
1604 hdr->datasn, checksum, data_crc);
1605 data_crc_failed = 1;
1606 } else {
1607 pr_debug("Got CRC32C DataDigest 0x%08x for"
1608 " %u bytes of Data Out\n", checksum,
1609 payload_length);
1610 }
1611 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001612
1613 return data_crc_failed;
1614}
1615
1616int
1617iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1618 bool data_crc_failed)
1619{
1620 struct iscsi_conn *conn = cmd->conn;
1621 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001622 /*
1623 * Increment post receive data and CRC values or perform
1624 * within-command recovery.
1625 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001626 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1627 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001628 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001629 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001630 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001631 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1632 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001633 /*
1634 * Handle extra special case for out of order
1635 * Unsolicited Data Out.
1636 */
1637 spin_lock_bh(&cmd->istate_lock);
1638 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1639 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1640 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1641 spin_unlock_bh(&cmd->istate_lock);
1642
1643 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001644 if (ooo_cmdsn)
1645 return 0;
1646 target_execute_cmd(&cmd->se_cmd);
1647 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001648 } else /* DATAOUT_CANNOT_RECOVER */
1649 return -1;
1650
1651 return 0;
1652}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001653EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001654
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001655static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1656{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001657 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001658 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1659 int rc;
1660 bool data_crc_failed = false;
1661
1662 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1663 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001664 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001665 else if (!cmd)
1666 return 0;
1667
1668 rc = iscsit_get_dataout(conn, cmd, hdr);
1669 if (rc < 0)
1670 return rc;
1671 else if (rc > 0)
1672 data_crc_failed = true;
1673
1674 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1675}
1676
Nicholas Bellinger778de362013-06-14 16:07:47 -07001677int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1678 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001679{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001680 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001681
Arshad Hussaina3662602014-03-14 15:28:59 -07001682 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1683 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1684 if (!cmd)
1685 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1686 (unsigned char *)hdr);
1687
1688 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1689 (unsigned char *)hdr);
1690 }
1691
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001692 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001693 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1694 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001695 if (!cmd)
1696 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1697 (unsigned char *)hdr);
1698
Nicholas Bellingerba159912013-07-03 03:48:24 -07001699 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1700 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001701 }
1702
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001703 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001704 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001705 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001706 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001707 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001708 if (!cmd)
1709 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1710 (unsigned char *)hdr);
1711
Nicholas Bellingerba159912013-07-03 03:48:24 -07001712 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1713 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001714 }
1715
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001716 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001717 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001718 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001719 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1720 payload_length);
1721 /*
1722 * This is not a response to a Unsolicited NopIN, which means
1723 * it can either be a NOPOUT ping request (with a valid ITT),
1724 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1725 * Either way, make sure we allocate an struct iscsi_cmd, as both
1726 * can contain ping data.
1727 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001728 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001729 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1730 cmd->i_state = ISTATE_SEND_NOPIN;
1731 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1732 1 : 0);
1733 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1734 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001735 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1736 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001737 cmd->data_direction = DMA_NONE;
1738 }
1739
Nicholas Bellinger778de362013-06-14 16:07:47 -07001740 return 0;
1741}
1742EXPORT_SYMBOL(iscsit_setup_nop_out);
1743
1744int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1745 struct iscsi_nopout *hdr)
1746{
1747 struct iscsi_cmd *cmd_p = NULL;
1748 int cmdsn_ret = 0;
1749 /*
1750 * Initiator is expecting a NopIN ping reply..
1751 */
1752 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001753 if (!cmd)
1754 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1755 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001756
1757 spin_lock_bh(&conn->cmd_lock);
1758 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1759 spin_unlock_bh(&conn->cmd_lock);
1760
1761 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1762
1763 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1764 iscsit_add_cmd_to_response_queue(cmd, conn,
1765 cmd->i_state);
1766 return 0;
1767 }
1768
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001769 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1770 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001771 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1772 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001773 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001774 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001775
1776 return 0;
1777 }
1778 /*
1779 * This was a response to a unsolicited NOPIN ping.
1780 */
1781 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1782 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1783 if (!cmd_p)
1784 return -EINVAL;
1785
1786 iscsit_stop_nopin_response_timer(conn);
1787
1788 cmd_p->i_state = ISTATE_REMOVE;
1789 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1790
1791 iscsit_start_nopin_timer(conn);
1792 return 0;
1793 }
1794 /*
1795 * Otherwise, initiator is not expecting a NOPIN is response.
1796 * Just ignore for now.
1797 */
1798 return 0;
1799}
1800EXPORT_SYMBOL(iscsit_process_nop_out);
1801
1802static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1803 unsigned char *buf)
1804{
1805 unsigned char *ping_data = NULL;
1806 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1807 struct kvec *iov = NULL;
1808 u32 payload_length = ntoh24(hdr->dlength);
1809 int ret;
1810
1811 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1812 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001813 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001814 /*
1815 * Handle NOP-OUT payload for traditional iSCSI sockets
1816 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001817 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001818 u32 checksum, data_crc, padding = 0;
1819 int niov = 0, rx_got, rx_size = payload_length;
1820
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001821 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1822 if (!ping_data) {
1823 pr_err("Unable to allocate memory for"
1824 " NOPOUT ping data.\n");
1825 ret = -1;
1826 goto out;
1827 }
1828
1829 iov = &cmd->iov_misc[0];
1830 iov[niov].iov_base = ping_data;
1831 iov[niov++].iov_len = payload_length;
1832
1833 padding = ((-payload_length) & 3);
1834 if (padding != 0) {
1835 pr_debug("Receiving %u additional bytes"
1836 " for padding.\n", padding);
1837 iov[niov].iov_base = &cmd->pad_bytes;
1838 iov[niov++].iov_len = padding;
1839 rx_size += padding;
1840 }
1841 if (conn->conn_ops->DataDigest) {
1842 iov[niov].iov_base = &checksum;
1843 iov[niov++].iov_len = ISCSI_CRC_LEN;
1844 rx_size += ISCSI_CRC_LEN;
1845 }
1846
1847 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1848 if (rx_got != rx_size) {
1849 ret = -1;
1850 goto out;
1851 }
1852
1853 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08001854 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001855 ping_data, payload_length,
1856 padding, cmd->pad_bytes,
1857 (u8 *)&data_crc);
1858
1859 if (checksum != data_crc) {
1860 pr_err("Ping data CRC32C DataDigest"
1861 " 0x%08x does not match computed 0x%08x\n",
1862 checksum, data_crc);
1863 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1864 pr_err("Unable to recover from"
1865 " NOPOUT Ping DataCRC failure while in"
1866 " ERL=0.\n");
1867 ret = -1;
1868 goto out;
1869 } else {
1870 /*
1871 * Silently drop this PDU and let the
1872 * initiator plug the CmdSN gap.
1873 */
1874 pr_debug("Dropping NOPOUT"
1875 " Command CmdSN: 0x%08x due to"
1876 " DataCRC error.\n", hdr->cmdsn);
1877 ret = 0;
1878 goto out;
1879 }
1880 } else {
1881 pr_debug("Got CRC32C DataDigest"
1882 " 0x%08x for %u bytes of ping data.\n",
1883 checksum, payload_length);
1884 }
1885 }
1886
1887 ping_data[payload_length] = '\0';
1888 /*
1889 * Attach ping data to struct iscsi_cmd->buf_ptr.
1890 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001891 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001892 cmd->buf_ptr_size = payload_length;
1893
1894 pr_debug("Got %u bytes of NOPOUT ping"
1895 " data.\n", payload_length);
1896 pr_debug("Ping Data: \"%s\"\n", ping_data);
1897 }
1898
Nicholas Bellinger778de362013-06-14 16:07:47 -07001899 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001900out:
1901 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001902 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001903
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001904 kfree(ping_data);
1905 return ret;
1906}
1907
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001908int
1909iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1910 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001911{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001912 struct se_tmr_req *se_tmr;
1913 struct iscsi_tmr_req *tmr_req;
1914 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001915 int out_of_order_cmdsn = 0, ret;
1916 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001917 u8 function;
1918
1919 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001920 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1921 function = hdr->flags;
1922
1923 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1924 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1925 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1926 hdr->rtt, hdr->refcmdsn, conn->cid);
1927
1928 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1929 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001930 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001931 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001932 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001933 }
1934
1935 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1936 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1937 pr_err("Task Management Request TASK_REASSIGN not"
1938 " issued as immediate command, bad iSCSI Initiator"
1939 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001940 return iscsit_add_reject_cmd(cmd,
1941 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001942 }
1943 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001944 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1945 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001946
Andy Groverd28b11692012-04-03 15:51:22 -07001947 cmd->data_direction = DMA_NONE;
1948
1949 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1950 if (!cmd->tmr_req) {
1951 pr_err("Unable to allocate memory for"
1952 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001953 return iscsit_add_reject_cmd(cmd,
1954 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1955 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001956 }
1957
1958 /*
1959 * TASK_REASSIGN for ERL=2 / connection stays inside of
1960 * LIO-Target $FABRIC_MOD
1961 */
1962 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1963
1964 u8 tcm_function;
1965 int ret;
1966
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001967 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Andy Groverd28b11692012-04-03 15:51:22 -07001968 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001969 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001970
Bart Van Asscheafc16602015-04-27 13:52:36 +02001971 target_get_sess_cmd(&cmd->se_cmd, true);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001972 sess_ref = true;
1973
Andy Groverd28b11692012-04-03 15:51:22 -07001974 switch (function) {
1975 case ISCSI_TM_FUNC_ABORT_TASK:
1976 tcm_function = TMR_ABORT_TASK;
1977 break;
1978 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1979 tcm_function = TMR_ABORT_TASK_SET;
1980 break;
1981 case ISCSI_TM_FUNC_CLEAR_ACA:
1982 tcm_function = TMR_CLEAR_ACA;
1983 break;
1984 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1985 tcm_function = TMR_CLEAR_TASK_SET;
1986 break;
1987 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1988 tcm_function = TMR_LUN_RESET;
1989 break;
1990 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1991 tcm_function = TMR_TARGET_WARM_RESET;
1992 break;
1993 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1994 tcm_function = TMR_TARGET_COLD_RESET;
1995 break;
1996 default:
1997 pr_err("Unknown iSCSI TMR Function:"
1998 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001999 return iscsit_add_reject_cmd(cmd,
2000 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002001 }
2002
2003 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
2004 tcm_function, GFP_KERNEL);
2005 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002006 return iscsit_add_reject_cmd(cmd,
2007 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002008
2009 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
2010 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002011
2012 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2013 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2014 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2015 cmd->init_task_tag = hdr->itt;
2016 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002017 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2018 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002019 se_tmr = cmd->se_cmd.se_tmr_req;
2020 tmr_req = cmd->tmr_req;
2021 /*
2022 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2023 */
2024 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08002025 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
2026 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002027 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002028 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2029 goto attach;
2030 }
2031 }
2032
2033 switch (function) {
2034 case ISCSI_TM_FUNC_ABORT_TASK:
2035 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002036 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002037 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002038 break;
2039 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2040 case ISCSI_TM_FUNC_CLEAR_ACA:
2041 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2042 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2043 break;
2044 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2045 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002046 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2047 goto attach;
2048 }
2049 break;
2050 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2051 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002052 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2053 goto attach;
2054 }
2055 break;
2056 case ISCSI_TM_FUNC_TASK_REASSIGN:
2057 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2058 /*
2059 * Perform sanity checks on the ExpDataSN only if the
2060 * TASK_REASSIGN was successful.
2061 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002062 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002063 break;
2064
2065 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002066 return iscsit_add_reject_cmd(cmd,
2067 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002068 break;
2069 default:
2070 pr_err("Unknown TMR function: 0x%02x, protocol"
2071 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002072 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2073 goto attach;
2074 }
2075
2076 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2077 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2078 se_tmr->call_transport = 1;
2079attach:
2080 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002081 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002082 spin_unlock_bh(&conn->cmd_lock);
2083
2084 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002085 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002086 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
2087 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002088 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002089 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002090 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002091 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002092 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002093 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002094
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002095 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002096 return 0;
2097 /*
2098 * Found the referenced task, send to transport for processing.
2099 */
2100 if (se_tmr->call_transport)
2101 return transport_generic_handle_tmr(&cmd->se_cmd);
2102
2103 /*
2104 * Could not find the referenced LUN, task, or Task Management
2105 * command not authorized or supported. Change state and
2106 * let the tx_thread send the response.
2107 *
2108 * For connection recovery, this is also the default action for
2109 * TMR TASK_REASSIGN.
2110 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002111 if (sess_ref) {
2112 pr_debug("Handle TMR, using sess_ref=true check\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +02002113 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002114 }
2115
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002116 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2117 return 0;
2118}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002119EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002120
2121/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002122int
2123iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2124 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002125{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002126 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002127
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002128 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002129 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002130 "greater than MaxXmitDataSegmentLength %u.\n",
2131 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002132 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2133 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002134 }
2135
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08002136 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2137 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2138 pr_err("Multi sequence text commands currently not supported\n");
2139 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2140 (unsigned char *)hdr);
2141 }
2142
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002143 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2144 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2145 hdr->exp_statsn, payload_length);
2146
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002147 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2148 cmd->i_state = ISTATE_SEND_TEXTRSP;
2149 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2150 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2151 cmd->targ_xfer_tag = 0xFFFFFFFF;
2152 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2153 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2154 cmd->data_direction = DMA_NONE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002155 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002156
2157 return 0;
2158}
2159EXPORT_SYMBOL(iscsit_setup_text_cmd);
2160
2161int
2162iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2163 struct iscsi_text *hdr)
2164{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002165 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002166 int cmdsn_ret;
2167
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002168 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002169 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2170 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2171 pr_err("Unable to locate text_in buffer for sendtargets"
2172 " discovery\n");
2173 goto reject;
2174 }
2175 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002176 }
2177 if (strncmp("SendTargets", text_in, 11) != 0) {
2178 pr_err("Received Text Data that is not"
2179 " SendTargets, cannot continue.\n");
2180 goto reject;
2181 }
2182 text_ptr = strchr(text_in, '=');
2183 if (!text_ptr) {
2184 pr_err("No \"=\" separator found in Text Data,"
2185 " cannot continue.\n");
2186 goto reject;
2187 }
2188 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002189 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002190 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2191 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002192 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002193 } else {
2194 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2195 goto reject;
2196 }
2197
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002198 spin_lock_bh(&conn->cmd_lock);
2199 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2200 spin_unlock_bh(&conn->cmd_lock);
2201
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002202empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002203 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2204
2205 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002206 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2207 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002208 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002209 return -1;
2210
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002211 return 0;
2212 }
2213
2214 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002215
2216reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002217 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2218 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002219}
2220EXPORT_SYMBOL(iscsit_process_text_cmd);
2221
2222static int
2223iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2224 unsigned char *buf)
2225{
2226 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2227 char *text_in = NULL;
2228 u32 payload_length = ntoh24(hdr->dlength);
2229 int rx_size, rc;
2230
2231 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2232 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002233 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002234
2235 rx_size = payload_length;
2236 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002237 u32 checksum = 0, data_crc = 0;
2238 u32 padding = 0, pad_bytes = 0;
2239 int niov = 0, rx_got;
2240 struct kvec iov[3];
2241
2242 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002243 if (!text_in) {
2244 pr_err("Unable to allocate memory for"
2245 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002246 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002247 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002248 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002249
2250 memset(iov, 0, 3 * sizeof(struct kvec));
2251 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002252 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002253
2254 padding = ((-payload_length) & 3);
2255 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002256 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002257 iov[niov++].iov_len = padding;
2258 rx_size += padding;
2259 pr_debug("Receiving %u additional bytes"
2260 " for padding.\n", padding);
2261 }
2262 if (conn->conn_ops->DataDigest) {
2263 iov[niov].iov_base = &checksum;
2264 iov[niov++].iov_len = ISCSI_CRC_LEN;
2265 rx_size += ISCSI_CRC_LEN;
2266 }
2267
2268 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002269 if (rx_got != rx_size)
2270 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002271
2272 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08002273 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002274 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002275 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002276 (u8 *)&data_crc);
2277
2278 if (checksum != data_crc) {
2279 pr_err("Text data CRC32C DataDigest"
2280 " 0x%08x does not match computed"
2281 " 0x%08x\n", checksum, data_crc);
2282 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2283 pr_err("Unable to recover from"
2284 " Text Data digest failure while in"
2285 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002286 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002287 } else {
2288 /*
2289 * Silently drop this PDU and let the
2290 * initiator plug the CmdSN gap.
2291 */
2292 pr_debug("Dropping Text"
2293 " Command CmdSN: 0x%08x due to"
2294 " DataCRC error.\n", hdr->cmdsn);
2295 kfree(text_in);
2296 return 0;
2297 }
2298 } else {
2299 pr_debug("Got CRC32C DataDigest"
2300 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002301 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002302 }
2303 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002304 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002305 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002306 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002307 }
2308
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002309 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002310
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002311reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002312 kfree(cmd->text_in_ptr);
2313 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002314 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002315}
2316
2317int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2318{
2319 struct iscsi_conn *conn_p;
2320 struct iscsi_session *sess = conn->sess;
2321
2322 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2323 " for SID: %u.\n", conn->cid, conn->sess->sid);
2324
2325 atomic_set(&sess->session_logout, 1);
2326 atomic_set(&conn->conn_logout_remove, 1);
2327 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2328
2329 iscsit_inc_conn_usage_count(conn);
2330 iscsit_inc_session_usage_count(sess);
2331
2332 spin_lock_bh(&sess->conn_lock);
2333 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2334 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2335 continue;
2336
2337 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2338 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2339 }
2340 spin_unlock_bh(&sess->conn_lock);
2341
2342 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2343
2344 return 0;
2345}
2346
2347int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2348{
2349 struct iscsi_conn *l_conn;
2350 struct iscsi_session *sess = conn->sess;
2351
2352 pr_debug("Received logout request CLOSECONNECTION for CID:"
2353 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2354
2355 /*
2356 * A Logout Request with a CLOSECONNECTION reason code for a CID
2357 * can arrive on a connection with a differing CID.
2358 */
2359 if (conn->cid == cmd->logout_cid) {
2360 spin_lock_bh(&conn->state_lock);
2361 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2362 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2363
2364 atomic_set(&conn->conn_logout_remove, 1);
2365 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2366 iscsit_inc_conn_usage_count(conn);
2367
2368 spin_unlock_bh(&conn->state_lock);
2369 } else {
2370 /*
2371 * Handle all different cid CLOSECONNECTION requests in
2372 * iscsit_logout_post_handler_diffcid() as to give enough
2373 * time for any non immediate command's CmdSN to be
2374 * acknowledged on the connection in question.
2375 *
2376 * Here we simply make sure the CID is still around.
2377 */
2378 l_conn = iscsit_get_conn_from_cid(sess,
2379 cmd->logout_cid);
2380 if (!l_conn) {
2381 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2382 iscsit_add_cmd_to_response_queue(cmd, conn,
2383 cmd->i_state);
2384 return 0;
2385 }
2386
2387 iscsit_dec_conn_usage_count(l_conn);
2388 }
2389
2390 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2391
2392 return 0;
2393}
2394
2395int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2396{
2397 struct iscsi_session *sess = conn->sess;
2398
2399 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2400 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2401
2402 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2403 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2404 " while ERL!=2.\n");
2405 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2406 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2407 return 0;
2408 }
2409
2410 if (conn->cid == cmd->logout_cid) {
2411 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2412 " with CID: %hu on CID: %hu, implementation error.\n",
2413 cmd->logout_cid, conn->cid);
2414 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2415 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2416 return 0;
2417 }
2418
2419 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2420
2421 return 0;
2422}
2423
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002424int
2425iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2426 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002427{
2428 int cmdsn_ret, logout_remove = 0;
2429 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002430 struct iscsi_logout *hdr;
2431 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2432
2433 hdr = (struct iscsi_logout *) buf;
2434 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002435
2436 if (tiqn) {
2437 spin_lock(&tiqn->logout_stats.lock);
2438 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2439 tiqn->logout_stats.normal_logouts++;
2440 else
2441 tiqn->logout_stats.abnormal_logouts++;
2442 spin_unlock(&tiqn->logout_stats.lock);
2443 }
2444
2445 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2446 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2447 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2448 hdr->cid, conn->cid);
2449
2450 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2451 pr_err("Received logout request on connection that"
2452 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002453 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002454 return 0;
2455 }
2456
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002457 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2458 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2459 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2460 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2461 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002462 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2463 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2464 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002465 cmd->logout_reason = reason_code;
2466 cmd->data_direction = DMA_NONE;
2467
2468 /*
2469 * We need to sleep in these cases (by returning 1) until the Logout
2470 * Response gets sent in the tx thread.
2471 */
2472 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2473 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002474 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002475 logout_remove = 1;
2476
2477 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002478 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002479 spin_unlock_bh(&conn->cmd_lock);
2480
2481 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002482 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002483
2484 /*
2485 * Immediate commands are executed, well, immediately.
2486 * Non-Immediate Logout Commands are executed in CmdSN order.
2487 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002488 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002489 int ret = iscsit_execute_cmd(cmd, 0);
2490
2491 if (ret < 0)
2492 return ret;
2493 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002494 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002495 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002496 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002497 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2498 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002499 }
2500
2501 return logout_remove;
2502}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002503EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002504
Varun Prakashd2faaef2016-04-20 00:00:19 +05302505int iscsit_handle_snack(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002506 struct iscsi_conn *conn,
2507 unsigned char *buf)
2508{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002509 struct iscsi_snack *hdr;
2510
2511 hdr = (struct iscsi_snack *) buf;
2512 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002513
2514 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2515 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2516 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2517 hdr->begrun, hdr->runlength, conn->cid);
2518
2519 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2520 pr_err("Initiator sent SNACK request while in"
2521 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002522 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2523 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002524 }
2525 /*
2526 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2527 * call from inside iscsi_send_recovery_datain_or_r2t().
2528 */
2529 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2530 case 0:
2531 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002532 hdr->itt,
2533 be32_to_cpu(hdr->ttt),
2534 be32_to_cpu(hdr->begrun),
2535 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002536 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002537 return iscsit_handle_status_snack(conn, hdr->itt,
2538 be32_to_cpu(hdr->ttt),
2539 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002540 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002541 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2542 be32_to_cpu(hdr->begrun),
2543 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002544 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2545 /* FIXME: Support R-Data SNACK */
2546 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002547 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2548 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002549 default:
2550 pr_err("Unknown SNACK type 0x%02x, protocol"
2551 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002552 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2553 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002554 }
2555
2556 return 0;
2557}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302558EXPORT_SYMBOL(iscsit_handle_snack);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002559
2560static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2561{
2562 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2563 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2564 wait_for_completion_interruptible_timeout(
2565 &conn->rx_half_close_comp,
2566 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2567 }
2568}
2569
2570static int iscsit_handle_immediate_data(
2571 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002572 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002573 u32 length)
2574{
2575 int iov_ret, rx_got = 0, rx_size = 0;
2576 u32 checksum, iov_count = 0, padding = 0;
2577 struct iscsi_conn *conn = cmd->conn;
2578 struct kvec *iov;
2579
2580 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2581 if (iov_ret < 0)
2582 return IMMEDIATE_DATA_CANNOT_RECOVER;
2583
2584 rx_size = length;
2585 iov_count = iov_ret;
2586 iov = &cmd->iov_data[0];
2587
2588 padding = ((-length) & 3);
2589 if (padding != 0) {
2590 iov[iov_count].iov_base = cmd->pad_bytes;
2591 iov[iov_count++].iov_len = padding;
2592 rx_size += padding;
2593 }
2594
2595 if (conn->conn_ops->DataDigest) {
2596 iov[iov_count].iov_base = &checksum;
2597 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2598 rx_size += ISCSI_CRC_LEN;
2599 }
2600
2601 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2602
2603 iscsit_unmap_iovec(cmd);
2604
2605 if (rx_got != rx_size) {
2606 iscsit_rx_thread_wait_for_tcp(conn);
2607 return IMMEDIATE_DATA_CANNOT_RECOVER;
2608 }
2609
2610 if (conn->conn_ops->DataDigest) {
2611 u32 data_crc;
2612
Herbert Xu69110e32016-01-24 21:19:52 +08002613 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002614 cmd->write_data_done, length, padding,
2615 cmd->pad_bytes);
2616
2617 if (checksum != data_crc) {
2618 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2619 " does not match computed 0x%08x\n", checksum,
2620 data_crc);
2621
2622 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2623 pr_err("Unable to recover from"
2624 " Immediate Data digest failure while"
2625 " in ERL=0.\n");
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_CANNOT_RECOVER;
2630 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002631 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002632 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002633 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002634 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2635 }
2636 } else {
2637 pr_debug("Got CRC32C DataDigest 0x%08x for"
2638 " %u bytes of Immediate Data\n", checksum,
2639 length);
2640 }
2641 }
2642
2643 cmd->write_data_done += length;
2644
Andy Groverebf1d952012-04-03 15:51:24 -07002645 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002646 spin_lock_bh(&cmd->istate_lock);
2647 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2648 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2649 spin_unlock_bh(&cmd->istate_lock);
2650 }
2651
2652 return IMMEDIATE_DATA_NORMAL_OPERATION;
2653}
2654
2655/*
2656 * Called with sess->conn_lock held.
2657 */
2658/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2659 with active network interface */
2660static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2661{
2662 struct iscsi_cmd *cmd;
2663 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002664 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002665
2666 /*
2667 * Only send a Asynchronous Message on connections whos network
2668 * interface is still functional.
2669 */
2670 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2671 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2672 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002673 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002674 break;
2675 }
2676 }
2677
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002678 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002679 return;
2680
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002681 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002682 if (!cmd) {
2683 iscsit_dec_conn_usage_count(conn_p);
2684 return;
2685 }
2686
2687 cmd->logout_cid = conn->cid;
2688 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2689 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2690
2691 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002692 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002693 spin_unlock_bh(&conn_p->cmd_lock);
2694
2695 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2696 iscsit_dec_conn_usage_count(conn_p);
2697}
2698
2699static int iscsit_send_conn_drop_async_message(
2700 struct iscsi_cmd *cmd,
2701 struct iscsi_conn *conn)
2702{
2703 struct iscsi_async *hdr;
2704
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002705 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2706
2707 hdr = (struct iscsi_async *) cmd->pdu;
2708 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2709 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002710 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002711 cmd->targ_xfer_tag = 0xFFFFFFFF;
2712 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2713 cmd->stat_sn = conn->stat_sn++;
2714 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2715 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002716 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002717 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2718 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2719 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2720 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2721
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002722 pr_debug("Sending Connection Dropped Async Message StatSN:"
2723 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2724 cmd->logout_cid, conn->cid);
Varun Prakash2854bb22016-04-20 00:00:08 +05302725
2726 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002727}
2728
Andy Grover6f3c0e62012-04-03 15:51:09 -07002729static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2730{
2731 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2732 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2733 wait_for_completion_interruptible_timeout(
2734 &conn->tx_half_close_comp,
2735 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2736 }
2737}
2738
Varun Prakashd2faaef2016-04-20 00:00:19 +05302739void
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002740iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2741 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2742 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002743{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002744 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2745 hdr->flags = datain->flags;
2746 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2747 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2748 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2749 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2750 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2751 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2752 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2753 }
2754 }
2755 hton24(hdr->dlength, datain->length);
2756 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2757 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2758 (struct scsi_lun *)&hdr->lun);
2759 else
2760 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2761
2762 hdr->itt = cmd->init_task_tag;
2763
2764 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2765 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2766 else
2767 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2768 if (set_statsn)
2769 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2770 else
2771 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2772
2773 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002774 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002775 hdr->datasn = cpu_to_be32(datain->data_sn);
2776 hdr->offset = cpu_to_be32(datain->offset);
2777
2778 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2779 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2780 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2781 ntohl(hdr->offset), datain->length, conn->cid);
2782}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302783EXPORT_SYMBOL(iscsit_build_datain_pdu);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002784
2785static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2786{
2787 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002788 struct iscsi_datain datain;
2789 struct iscsi_datain_req *dr;
Varun Prakash2854bb22016-04-20 00:00:08 +05302790 int eodr = 0, ret;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002791 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002792
2793 memset(&datain, 0, sizeof(struct iscsi_datain));
2794 dr = iscsit_get_datain_values(cmd, &datain);
2795 if (!dr) {
2796 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2797 cmd->init_task_tag);
2798 return -1;
2799 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002800 /*
2801 * Be paranoid and double check the logic for now.
2802 */
Andy Groverebf1d952012-04-03 15:51:24 -07002803 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002804 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2805 " datain.length: %u exceeds cmd->data_length: %u\n",
2806 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002807 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002808 return -1;
2809 }
2810
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002811 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002812 /*
2813 * Special case for successfully execution w/ both DATAIN
2814 * and Sense Data.
2815 */
2816 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2817 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2818 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2819 else {
2820 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2821 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2822 iscsit_increment_maxcmdsn(cmd, conn->sess);
2823 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002824 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002825 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002826 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2827 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002828 }
2829
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002830 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002831
Varun Prakash2854bb22016-04-20 00:00:08 +05302832 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2833 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07002834 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002835
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002836 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002837 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002838 2 : 1;
2839 iscsit_free_datain_req(cmd, dr);
2840 }
2841
Andy Grover6f3c0e62012-04-03 15:51:09 -07002842 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002843}
2844
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002845int
2846iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2847 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002848{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002849 struct iscsi_conn *logout_conn = NULL;
2850 struct iscsi_conn_recovery *cr = NULL;
2851 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002852 /*
2853 * The actual shutting down of Sessions and/or Connections
2854 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2855 * is done in scsi_logout_post_handler().
2856 */
2857 switch (cmd->logout_reason) {
2858 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2859 pr_debug("iSCSI session logout successful, setting"
2860 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2861 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2862 break;
2863 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2864 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2865 break;
2866 /*
2867 * For CLOSECONNECTION logout requests carrying
2868 * a matching logout CID -> local CID, the reference
2869 * for the local CID will have been incremented in
2870 * iscsi_logout_closeconnection().
2871 *
2872 * For CLOSECONNECTION logout requests carrying
2873 * a different CID than the connection it arrived
2874 * on, the connection responding to cmd->logout_cid
2875 * is stopped in iscsit_logout_post_handler_diffcid().
2876 */
2877
2878 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2879 " successful.\n", cmd->logout_cid, conn->cid);
2880 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2881 break;
2882 case ISCSI_LOGOUT_REASON_RECOVERY:
2883 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2884 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2885 break;
2886 /*
2887 * If the connection is still active from our point of view
2888 * force connection recovery to occur.
2889 */
2890 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2891 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002892 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002893 iscsit_connection_reinstatement_rcfr(logout_conn);
2894 iscsit_dec_conn_usage_count(logout_conn);
2895 }
2896
2897 cr = iscsit_get_inactive_connection_recovery_entry(
2898 conn->sess, cmd->logout_cid);
2899 if (!cr) {
2900 pr_err("Unable to locate CID: %hu for"
2901 " REMOVECONNFORRECOVERY Logout Request.\n",
2902 cmd->logout_cid);
2903 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2904 break;
2905 }
2906
2907 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2908
2909 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2910 " for recovery for CID: %hu on CID: %hu successful.\n",
2911 cmd->logout_cid, conn->cid);
2912 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2913 break;
2914 default:
2915 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2916 cmd->logout_reason);
2917 return -1;
2918 }
2919
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002920 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2921 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2922 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002923 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002924 cmd->stat_sn = conn->stat_sn++;
2925 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2926
2927 iscsit_increment_maxcmdsn(cmd, conn->sess);
2928 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002929 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002930
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002931 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2932 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2933 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2934 cmd->logout_cid, conn->cid);
2935
2936 return 0;
2937}
2938EXPORT_SYMBOL(iscsit_build_logout_rsp);
2939
2940static int
2941iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2942{
Varun Prakash2854bb22016-04-20 00:00:08 +05302943 int rc;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002944
2945 rc = iscsit_build_logout_rsp(cmd, conn,
2946 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2947 if (rc < 0)
2948 return rc;
2949
Varun Prakash2854bb22016-04-20 00:00:08 +05302950 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002951}
2952
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002953void
2954iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2955 struct iscsi_nopin *hdr, bool nopout_response)
2956{
2957 hdr->opcode = ISCSI_OP_NOOP_IN;
2958 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2959 hton24(hdr->dlength, cmd->buf_ptr_size);
2960 if (nopout_response)
2961 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2962 hdr->itt = cmd->init_task_tag;
2963 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2964 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2965 conn->stat_sn;
2966 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2967
2968 if (nopout_response)
2969 iscsit_increment_maxcmdsn(cmd, conn->sess);
2970
2971 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002972 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002973
2974 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2975 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2976 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2977 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2978}
2979EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2980
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002981/*
2982 * Unsolicited NOPIN, either requesting a response or not.
2983 */
2984static int iscsit_send_unsolicited_nopin(
2985 struct iscsi_cmd *cmd,
2986 struct iscsi_conn *conn,
2987 int want_response)
2988{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002989 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Varun Prakash2854bb22016-04-20 00:00:08 +05302990 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002991
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002992 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002993
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002994 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2995 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2996
Varun Prakash2854bb22016-04-20 00:00:08 +05302997 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
2998 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07002999 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003000
3001 spin_lock_bh(&cmd->istate_lock);
3002 cmd->i_state = want_response ?
3003 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3004 spin_unlock_bh(&cmd->istate_lock);
3005
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003006 return 0;
3007}
3008
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003009static int
3010iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003011{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003012 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003013
3014 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003015
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003016 /*
3017 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3018 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3019 */
Varun Prakash2854bb22016-04-20 00:00:08 +05303020 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003021
Varun Prakash2854bb22016-04-20 00:00:08 +05303022 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3023 cmd->buf_ptr,
3024 cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003025}
3026
Andy Grover6f3c0e62012-04-03 15:51:09 -07003027static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028 struct iscsi_cmd *cmd,
3029 struct iscsi_conn *conn)
3030{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003031 struct iscsi_r2t *r2t;
3032 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003033 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003034
3035 r2t = iscsit_get_r2t_from_list(cmd);
3036 if (!r2t)
3037 return -1;
3038
3039 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3040 memset(hdr, 0, ISCSI_HDR_LEN);
3041 hdr->opcode = ISCSI_OP_R2T;
3042 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3043 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3044 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003045 hdr->itt = cmd->init_task_tag;
Varun Prakash85672702016-04-20 00:00:13 +05303046 if (conn->conn_transport->iscsit_get_r2t_ttt)
3047 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3048 else
3049 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003050 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3051 hdr->statsn = cpu_to_be32(conn->stat_sn);
3052 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003053 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003054 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3055 hdr->data_offset = cpu_to_be32(r2t->offset);
3056 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3057
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003058 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3059 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3060 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3061 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3062 r2t->offset, r2t->xfer_len, conn->cid);
3063
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003064 spin_lock_bh(&cmd->r2t_lock);
3065 r2t->sent_r2t = 1;
3066 spin_unlock_bh(&cmd->r2t_lock);
3067
Varun Prakash2854bb22016-04-20 00:00:08 +05303068 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003069 if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003070 return ret;
3071 }
3072
3073 spin_lock_bh(&cmd->dataout_timeout_lock);
3074 iscsit_start_dataout_timer(cmd, conn);
3075 spin_unlock_bh(&cmd->dataout_timeout_lock);
3076
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003077 return 0;
3078}
3079
3080/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003081 * @recovery: If called from iscsi_task_reassign_complete_write() for
3082 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003083 */
3084int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003085 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003086 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003087 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003088{
3089 int first_r2t = 1;
3090 u32 offset = 0, xfer_len = 0;
3091
3092 spin_lock_bh(&cmd->r2t_lock);
3093 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3094 spin_unlock_bh(&cmd->r2t_lock);
3095 return 0;
3096 }
3097
Andy Grover8b1e1242012-04-03 15:51:12 -07003098 if (conn->sess->sess_ops->DataSequenceInOrder &&
3099 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003100 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003101
3102 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3103 if (conn->sess->sess_ops->DataSequenceInOrder) {
3104 offset = cmd->r2t_offset;
3105
Andy Grover8b1e1242012-04-03 15:51:12 -07003106 if (first_r2t && recovery) {
3107 int new_data_end = offset +
3108 conn->sess->sess_ops->MaxBurstLength -
3109 cmd->next_burst_len;
3110
Andy Groverebf1d952012-04-03 15:51:24 -07003111 if (new_data_end > cmd->se_cmd.data_length)
3112 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003113 else
3114 xfer_len =
3115 conn->sess->sess_ops->MaxBurstLength -
3116 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003117 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003118 int new_data_end = offset +
3119 conn->sess->sess_ops->MaxBurstLength;
3120
Andy Groverebf1d952012-04-03 15:51:24 -07003121 if (new_data_end > cmd->se_cmd.data_length)
3122 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003123 else
3124 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003125 }
3126 cmd->r2t_offset += xfer_len;
3127
Andy Groverebf1d952012-04-03 15:51:24 -07003128 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003129 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3130 } else {
3131 struct iscsi_seq *seq;
3132
3133 seq = iscsit_get_seq_holder_for_r2t(cmd);
3134 if (!seq) {
3135 spin_unlock_bh(&cmd->r2t_lock);
3136 return -1;
3137 }
3138
3139 offset = seq->offset;
3140 xfer_len = seq->xfer_len;
3141
3142 if (cmd->seq_send_order == cmd->seq_count)
3143 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3144 }
3145 cmd->outstanding_r2ts++;
3146 first_r2t = 0;
3147
3148 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3149 spin_unlock_bh(&cmd->r2t_lock);
3150 return -1;
3151 }
3152
3153 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3154 break;
3155 }
3156 spin_unlock_bh(&cmd->r2t_lock);
3157
3158 return 0;
3159}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303160EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003161
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003162void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3163 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003164{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003165 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003166 cmd->stat_sn = conn->stat_sn++;
3167
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003168 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003169
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003170 memset(hdr, 0, ISCSI_HDR_LEN);
3171 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3172 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3173 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3174 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003175 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003176 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3177 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003178 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003179 }
3180 hdr->response = cmd->iscsi_response;
3181 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003182 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003183 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3184
3185 iscsit_increment_maxcmdsn(cmd, conn->sess);
3186 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003187 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003188
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003189 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3190 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3191 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3192 cmd->se_cmd.scsi_status, conn->cid);
3193}
3194EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3195
3196static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3197{
3198 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003199 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
Varun Prakash2854bb22016-04-20 00:00:08 +05303200 void *data_buf = NULL;
3201 u32 padding = 0, data_buf_len = 0;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003202
3203 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3204
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003205 /*
3206 * Attach SENSE DATA payload to iSCSI Response PDU
3207 */
3208 if (cmd->se_cmd.sense_buffer &&
3209 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3210 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003211 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3212 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3213
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003214 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003215 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Varun Prakash2854bb22016-04-20 00:00:08 +05303216 data_buf = cmd->sense_buffer;
3217 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003218
3219 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003220 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003221 cmd->se_cmd.scsi_sense_length, 0, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003222 pr_debug("Adding %u bytes of padding to"
3223 " SENSE.\n", padding);
3224 }
3225
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003226 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3227 " Response PDU\n",
3228 cmd->se_cmd.scsi_sense_length);
3229 }
3230
Varun Prakash2854bb22016-04-20 00:00:08 +05303231 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3232 data_buf_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003233}
3234
3235static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3236{
3237 switch (se_tmr->response) {
3238 case TMR_FUNCTION_COMPLETE:
3239 return ISCSI_TMF_RSP_COMPLETE;
3240 case TMR_TASK_DOES_NOT_EXIST:
3241 return ISCSI_TMF_RSP_NO_TASK;
3242 case TMR_LUN_DOES_NOT_EXIST:
3243 return ISCSI_TMF_RSP_NO_LUN;
3244 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3245 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003246 case TMR_FUNCTION_REJECTED:
3247 default:
3248 return ISCSI_TMF_RSP_REJECTED;
3249 }
3250}
3251
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003252void
3253iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3254 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003255{
3256 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003257
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003258 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003259 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003260 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003261 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003262 cmd->stat_sn = conn->stat_sn++;
3263 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3264
3265 iscsit_increment_maxcmdsn(cmd, conn->sess);
3266 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003267 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003268
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003269 pr_debug("Built Task Management Response ITT: 0x%08x,"
3270 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3271 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3272}
3273EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3274
3275static int
3276iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3277{
3278 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003279
3280 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3281
Varun Prakash2854bb22016-04-20 00:00:08 +05303282 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003283}
3284
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003285static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3286{
3287 bool ret = false;
3288
3289 if (np->np_sockaddr.ss_family == AF_INET6) {
3290 const struct sockaddr_in6 sin6 = {
3291 .sin6_addr = IN6ADDR_ANY_INIT };
3292 struct sockaddr_in6 *sock_in6 =
3293 (struct sockaddr_in6 *)&np->np_sockaddr;
3294
3295 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3296 sin6.sin6_addr.s6_addr, 16))
3297 ret = true;
3298 } else {
3299 struct sockaddr_in * sock_in =
3300 (struct sockaddr_in *)&np->np_sockaddr;
3301
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003302 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003303 ret = true;
3304 }
3305
3306 return ret;
3307}
3308
Andy Grover8b1e1242012-04-03 15:51:12 -07003309#define SENDTARGETS_BUF_LIMIT 32768U
3310
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003311static int
3312iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003313 enum iscsit_transport_type network_transport,
3314 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003315{
3316 char *payload = NULL;
3317 struct iscsi_conn *conn = cmd->conn;
3318 struct iscsi_portal_group *tpg;
3319 struct iscsi_tiqn *tiqn;
3320 struct iscsi_tpg_np *tpg_np;
3321 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003322 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003323 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003324 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
David Disseldorpa6415cd2015-08-01 00:10:12 -07003325 bool active;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003326
Sagi Grimbergbe7dcfb2015-01-26 12:49:06 +02003327 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003328 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003329
3330 payload = kzalloc(buffer_len, GFP_KERNEL);
3331 if (!payload) {
3332 pr_err("Unable to allocate memory for sendtargets"
3333 " response.\n");
3334 return -ENOMEM;
3335 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003336 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003337 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003338 * explicit case..
3339 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003340 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003341 text_ptr = strchr(text_in, '=');
3342 if (!text_ptr) {
3343 pr_err("Unable to locate '=' string in text_in:"
3344 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003345 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003346 return -EINVAL;
3347 }
3348 /*
3349 * Skip over '=' character..
3350 */
3351 text_ptr += 1;
3352 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003353
3354 spin_lock(&tiqn_lock);
3355 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003356 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003357 strcmp(tiqn->tiqn, text_ptr)) {
3358 continue;
3359 }
3360
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003361 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003362
3363 spin_lock(&tiqn->tiqn_tpg_lock);
3364 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3365
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003366 /* If demo_mode_discovery=0 and generate_node_acls=0
3367 * (demo mode dislabed) do not return
3368 * TargetName+TargetAddress unless a NodeACL exists.
3369 */
3370
3371 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3372 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
Nicholas Bellinger21aaa232016-01-07 22:09:27 -08003373 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003374 cmd->conn->sess->sess_ops->InitiatorName))) {
3375 continue;
3376 }
3377
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003378 spin_lock(&tpg->tpg_state_lock);
David Disseldorpa6415cd2015-08-01 00:10:12 -07003379 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003380 spin_unlock(&tpg->tpg_state_lock);
3381
David Disseldorpa6415cd2015-08-01 00:10:12 -07003382 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3383 continue;
3384
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003385 spin_lock(&tpg->tpg_np_lock);
3386 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3387 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003388 struct iscsi_np *np = tpg_np->tpg_np;
3389 bool inaddr_any = iscsit_check_inaddr_any(np);
Andy Grover13a3cf02015-08-24 10:26:06 -07003390 struct sockaddr_storage *sockaddr;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003391
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003392 if (np->np_network_transport != network_transport)
3393 continue;
3394
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003395 if (!target_name_printed) {
3396 len = sprintf(buf, "TargetName=%s",
3397 tiqn->tiqn);
3398 len += 1;
3399
3400 if ((len + payload_len) > buffer_len) {
3401 spin_unlock(&tpg->tpg_np_lock);
3402 spin_unlock(&tiqn->tiqn_tpg_lock);
3403 end_of_buf = 1;
3404 goto eob;
3405 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003406
3407 if (skip_bytes && len <= skip_bytes) {
3408 skip_bytes -= len;
3409 } else {
3410 memcpy(payload + payload_len, buf, len);
3411 payload_len += len;
3412 target_name_printed = 1;
3413 if (len > skip_bytes)
3414 skip_bytes = 0;
3415 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003416 }
3417
Andy Grover69d75572015-08-24 10:26:04 -07003418 if (inaddr_any)
3419 sockaddr = &conn->local_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003420 else
Andy Grover69d75572015-08-24 10:26:04 -07003421 sockaddr = &np->np_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003422
Andy Grover69d75572015-08-24 10:26:04 -07003423 len = sprintf(buf, "TargetAddress="
3424 "%pISpc,%hu",
3425 sockaddr,
3426 tpg->tpgt);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003427 len += 1;
3428
3429 if ((len + payload_len) > buffer_len) {
3430 spin_unlock(&tpg->tpg_np_lock);
3431 spin_unlock(&tiqn->tiqn_tpg_lock);
3432 end_of_buf = 1;
3433 goto eob;
3434 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003435
3436 if (skip_bytes && len <= skip_bytes) {
3437 skip_bytes -= len;
3438 } else {
3439 memcpy(payload + payload_len, buf, len);
3440 payload_len += len;
3441 if (len > skip_bytes)
3442 skip_bytes = 0;
3443 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003444 }
3445 spin_unlock(&tpg->tpg_np_lock);
3446 }
3447 spin_unlock(&tiqn->tiqn_tpg_lock);
3448eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003449 if (end_of_buf) {
3450 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003451 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003452 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003453
Andy Grover8060b8d2015-01-09 15:13:08 -08003454 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003455 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003456 }
3457 spin_unlock(&tiqn_lock);
3458
3459 cmd->buf_ptr = payload;
3460
3461 return payload_len;
3462}
3463
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003464int
3465iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003466 struct iscsi_text_rsp *hdr,
3467 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003468{
3469 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003470 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003471
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003472 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3473 cmd->read_data_done,
3474 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003475 if (text_length < 0)
3476 return text_length;
3477
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003478 if (completed) {
3479 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3480 } else {
3481 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3482 cmd->read_data_done += text_length;
3483 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3484 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3485 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003486 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003487 padding = ((-text_length) & 3);
3488 hton24(hdr->dlength, text_length);
3489 hdr->itt = cmd->init_task_tag;
3490 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3491 cmd->stat_sn = conn->stat_sn++;
3492 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3493
3494 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003495 /*
3496 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3497 * correctly increment MaxCmdSN for each response answering a
3498 * non immediate text request with a valid CmdSN.
3499 */
3500 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003501 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003502 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003503
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003504 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3505 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3506 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3507 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3508 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003509
3510 return text_length + padding;
3511}
3512EXPORT_SYMBOL(iscsit_build_text_rsp);
3513
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003514static int iscsit_send_text_rsp(
3515 struct iscsi_cmd *cmd,
3516 struct iscsi_conn *conn)
3517{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003518 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Varun Prakash2854bb22016-04-20 00:00:08 +05303519 int text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003520
Varun Prakash864e5042016-04-20 00:00:15 +05303521 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3522 conn->conn_transport->transport_type);
Varun Prakash2854bb22016-04-20 00:00:08 +05303523 if (text_length < 0)
3524 return text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003525
Varun Prakash2854bb22016-04-20 00:00:08 +05303526 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3527 cmd->buf_ptr,
3528 text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003529}
3530
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003531void
3532iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3533 struct iscsi_reject *hdr)
3534{
3535 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003536 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003537 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3538 hton24(hdr->dlength, ISCSI_HDR_LEN);
3539 hdr->ffffffff = cpu_to_be32(0xffffffff);
3540 cmd->stat_sn = conn->stat_sn++;
3541 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3542 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003543 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003544
3545}
3546EXPORT_SYMBOL(iscsit_build_reject);
3547
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003548static int iscsit_send_reject(
3549 struct iscsi_cmd *cmd,
3550 struct iscsi_conn *conn)
3551{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003552 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003553
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003554 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003555
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003556 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3557 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3558
Varun Prakash2854bb22016-04-20 00:00:08 +05303559 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3560 cmd->buf_ptr,
3561 ISCSI_HDR_LEN);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003562}
3563
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003564void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3565{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003566 int ord, cpu;
3567 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003568 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3569 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003570 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003571 * Here we use bitmap_id to determine which CPU that this
3572 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003573 * execute upon.
3574 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003575 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003576 for_each_online_cpu(cpu) {
3577 if (ord-- == 0) {
3578 cpumask_set_cpu(cpu, conn->conn_cpumask);
3579 return;
3580 }
3581 }
3582 /*
3583 * This should never be reached..
3584 */
3585 dump_stack();
3586 cpumask_setall(conn->conn_cpumask);
3587}
3588
Varun Prakashd2faaef2016-04-20 00:00:19 +05303589int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003590iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003591{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003592 int ret;
3593
3594 switch (state) {
3595 case ISTATE_SEND_R2T:
3596 ret = iscsit_send_r2t(cmd, conn);
3597 if (ret < 0)
3598 goto err;
3599 break;
3600 case ISTATE_REMOVE:
3601 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003602 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003603 spin_unlock_bh(&conn->cmd_lock);
3604
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003605 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003606 break;
3607 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3608 iscsit_mod_nopin_response_timer(conn);
3609 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3610 if (ret < 0)
3611 goto err;
3612 break;
3613 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3614 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3615 if (ret < 0)
3616 goto err;
3617 break;
3618 default:
3619 pr_err("Unknown Opcode: 0x%02x ITT:"
3620 " 0x%08x, i_state: %d on CID: %hu\n",
3621 cmd->iscsi_opcode, cmd->init_task_tag, state,
3622 conn->cid);
3623 goto err;
3624 }
3625
3626 return 0;
3627
3628err:
3629 return -1;
3630}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303631EXPORT_SYMBOL(iscsit_immediate_queue);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003632
3633static int
3634iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3635{
3636 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003637 struct iscsi_queue_req *qr;
3638 struct iscsi_cmd *cmd;
3639 u8 state;
3640 int ret;
3641
3642 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3643 atomic_set(&conn->check_immediate_queue, 0);
3644 cmd = qr->cmd;
3645 state = qr->state;
3646 kmem_cache_free(lio_qr_cache, qr);
3647
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003648 ret = t->iscsit_immediate_queue(conn, cmd, state);
3649 if (ret < 0)
3650 return ret;
3651 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003652
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003653 return 0;
3654}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003655
Varun Prakashd2faaef2016-04-20 00:00:19 +05303656int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003657iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3658{
3659 int ret;
3660
3661check_rsp_state:
3662 switch (state) {
3663 case ISTATE_SEND_DATAIN:
3664 ret = iscsit_send_datain(cmd, conn);
3665 if (ret < 0)
3666 goto err;
3667 else if (!ret)
3668 /* more drs */
3669 goto check_rsp_state;
3670 else if (ret == 1) {
3671 /* all done */
3672 spin_lock_bh(&cmd->istate_lock);
3673 cmd->i_state = ISTATE_SENT_STATUS;
3674 spin_unlock_bh(&cmd->istate_lock);
3675
3676 if (atomic_read(&conn->check_immediate_queue))
3677 return 1;
3678
3679 return 0;
3680 } else if (ret == 2) {
3681 /* Still must send status,
3682 SCF_TRANSPORT_TASK_SENSE was set */
3683 spin_lock_bh(&cmd->istate_lock);
3684 cmd->i_state = ISTATE_SEND_STATUS;
3685 spin_unlock_bh(&cmd->istate_lock);
3686 state = ISTATE_SEND_STATUS;
3687 goto check_rsp_state;
3688 }
3689
3690 break;
3691 case ISTATE_SEND_STATUS:
3692 case ISTATE_SEND_STATUS_RECOVERY:
3693 ret = iscsit_send_response(cmd, conn);
3694 break;
3695 case ISTATE_SEND_LOGOUTRSP:
3696 ret = iscsit_send_logout(cmd, conn);
3697 break;
3698 case ISTATE_SEND_ASYNCMSG:
3699 ret = iscsit_send_conn_drop_async_message(
3700 cmd, conn);
3701 break;
3702 case ISTATE_SEND_NOPIN:
3703 ret = iscsit_send_nopin(cmd, conn);
3704 break;
3705 case ISTATE_SEND_REJECT:
3706 ret = iscsit_send_reject(cmd, conn);
3707 break;
3708 case ISTATE_SEND_TASKMGTRSP:
3709 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3710 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003711 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003712 ret = iscsit_tmr_post_handler(cmd, conn);
3713 if (ret != 0)
3714 iscsit_fall_back_to_erl0(conn->sess);
3715 break;
3716 case ISTATE_SEND_TEXTRSP:
3717 ret = iscsit_send_text_rsp(cmd, conn);
3718 break;
3719 default:
3720 pr_err("Unknown Opcode: 0x%02x ITT:"
3721 " 0x%08x, i_state: %d on CID: %hu\n",
3722 cmd->iscsi_opcode, cmd->init_task_tag,
3723 state, conn->cid);
3724 goto err;
3725 }
3726 if (ret < 0)
3727 goto err;
3728
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003729 switch (state) {
3730 case ISTATE_SEND_LOGOUTRSP:
3731 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003732 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003733 /* fall through */
3734 case ISTATE_SEND_STATUS:
3735 case ISTATE_SEND_ASYNCMSG:
3736 case ISTATE_SEND_NOPIN:
3737 case ISTATE_SEND_STATUS_RECOVERY:
3738 case ISTATE_SEND_TEXTRSP:
3739 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003740 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003741 spin_lock_bh(&cmd->istate_lock);
3742 cmd->i_state = ISTATE_SENT_STATUS;
3743 spin_unlock_bh(&cmd->istate_lock);
3744 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003745 default:
3746 pr_err("Unknown Opcode: 0x%02x ITT:"
3747 " 0x%08x, i_state: %d on CID: %hu\n",
3748 cmd->iscsi_opcode, cmd->init_task_tag,
3749 cmd->i_state, conn->cid);
3750 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003751 }
3752
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003753 if (atomic_read(&conn->check_immediate_queue))
3754 return 1;
3755
Andy Grover6f3c0e62012-04-03 15:51:09 -07003756 return 0;
3757
3758err:
3759 return -1;
3760}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303761EXPORT_SYMBOL(iscsit_response_queue);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003762
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003763static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003764{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003765 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003766 struct iscsi_queue_req *qr;
3767 struct iscsi_cmd *cmd;
3768 u8 state;
3769 int ret;
3770
3771 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3772 cmd = qr->cmd;
3773 state = qr->state;
3774 kmem_cache_free(lio_qr_cache, qr);
3775
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003776 ret = t->iscsit_response_queue(conn, cmd, state);
3777 if (ret == 1 || ret < 0)
3778 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003779 }
3780
3781 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003782}
3783
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003784int iscsi_target_tx_thread(void *arg)
3785{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003786 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003787 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003788 /*
3789 * Allow ourselves to be interrupted by SIGINT so that a
3790 * connection recovery / failure event can be triggered externally.
3791 */
3792 allow_signal(SIGINT);
3793
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003794 while (!kthread_should_stop()) {
3795 /*
3796 * Ensure that both TX and RX per connection kthreads
3797 * are scheduled to run on the same CPU.
3798 */
3799 iscsit_thread_check_cpumask(conn, current, 1);
3800
Roland Dreierd5627ac2012-10-31 09:16:46 -07003801 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003802 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003803
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003804 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003805 goto transport_err;
3806
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003807get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003808 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003809 if (ret < 0)
3810 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003811
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003812 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003813 if (ret == 1)
3814 goto get_immediate;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003815 else if (ret == -ECONNRESET)
3816 goto out;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003817 else if (ret < 0)
3818 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003819 }
3820
3821transport_err:
Nicholas Bellingere5419862015-07-22 23:14:19 -07003822 /*
3823 * Avoid the normal connection failure code-path if this connection
3824 * is still within LOGIN mode, and iscsi_np process context is
3825 * responsible for cleaning up the early connection failure.
3826 */
3827 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
3828 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003829out:
3830 return 0;
3831}
3832
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003833static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3834{
3835 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3836 struct iscsi_cmd *cmd;
3837 int ret = 0;
3838
3839 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3840 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003841 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003842 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003843 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003844
3845 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3846 break;
3847 case ISCSI_OP_SCSI_DATA_OUT:
3848 ret = iscsit_handle_data_out(conn, buf);
3849 break;
3850 case ISCSI_OP_NOOP_OUT:
3851 cmd = NULL;
3852 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003853 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003854 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003855 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003856 }
3857 ret = iscsit_handle_nop_out(conn, cmd, buf);
3858 break;
3859 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003860 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003861 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003862 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003863
3864 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3865 break;
3866 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003867 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3868 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3869 if (!cmd)
3870 goto reject;
3871 } else {
3872 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3873 if (!cmd)
3874 goto reject;
3875 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003876
3877 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003878 break;
3879 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003880 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003881 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003882 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003883
3884 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3885 if (ret > 0)
3886 wait_for_completion_timeout(&conn->conn_logout_comp,
3887 SECONDS_FOR_LOGOUT_COMP * HZ);
3888 break;
3889 case ISCSI_OP_SNACK:
3890 ret = iscsit_handle_snack(conn, buf);
3891 break;
3892 default:
3893 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3894 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3895 pr_err("Cannot recover from unknown"
3896 " opcode while ERL=0, closing iSCSI connection.\n");
3897 return -1;
3898 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02003899 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3900 " closing iSCSI connection.\n");
3901 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003902 break;
3903 }
3904
3905 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003906reject:
3907 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003908}
3909
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08003910static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3911{
3912 bool ret;
3913
3914 spin_lock_bh(&conn->state_lock);
3915 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3916 spin_unlock_bh(&conn->state_lock);
3917
3918 return ret;
3919}
3920
Varun Prakashe8205cc2016-04-20 00:00:11 +05303921static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003922{
Varun Prakashe8205cc2016-04-20 00:00:11 +05303923 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003924 u8 buffer[ISCSI_HDR_LEN], opcode;
3925 u32 checksum = 0, digest = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003926 struct kvec iov;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003927
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003928 while (!kthread_should_stop()) {
3929 /*
3930 * Ensure that both TX and RX per connection kthreads
3931 * are scheduled to run on the same CPU.
3932 */
3933 iscsit_thread_check_cpumask(conn, current, 0);
3934
3935 memset(buffer, 0, ISCSI_HDR_LEN);
3936 memset(&iov, 0, sizeof(struct kvec));
3937
3938 iov.iov_base = buffer;
3939 iov.iov_len = ISCSI_HDR_LEN;
3940
3941 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3942 if (ret != ISCSI_HDR_LEN) {
3943 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303944 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003945 }
3946
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003947 if (conn->conn_ops->HeaderDigest) {
3948 iov.iov_base = &digest;
3949 iov.iov_len = ISCSI_CRC_LEN;
3950
3951 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3952 if (ret != ISCSI_CRC_LEN) {
3953 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303954 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003955 }
3956
Herbert Xu69110e32016-01-24 21:19:52 +08003957 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003958 buffer, ISCSI_HDR_LEN,
3959 0, NULL, (u8 *)&checksum);
3960
3961 if (digest != checksum) {
3962 pr_err("HeaderDigest CRC32C failed,"
3963 " received 0x%08x, computed 0x%08x\n",
3964 digest, checksum);
3965 /*
3966 * Set the PDU to 0xff so it will intentionally
3967 * hit default in the switch below.
3968 */
3969 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003970 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003971 } else {
3972 pr_debug("Got HeaderDigest CRC32C"
3973 " 0x%08x\n", checksum);
3974 }
3975 }
3976
3977 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
Varun Prakashe8205cc2016-04-20 00:00:11 +05303978 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003979
3980 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3981
3982 if (conn->sess->sess_ops->SessionType &&
3983 ((!(opcode & ISCSI_OP_TEXT)) ||
3984 (!(opcode & ISCSI_OP_LOGOUT)))) {
3985 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3986 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07003987 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
3988 buffer);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303989 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003990 }
3991
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003992 ret = iscsi_target_rx_opcode(conn, buffer);
3993 if (ret < 0)
Varun Prakashe8205cc2016-04-20 00:00:11 +05303994 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003995 }
Varun Prakashe8205cc2016-04-20 00:00:11 +05303996}
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003997
Varun Prakashe8205cc2016-04-20 00:00:11 +05303998int iscsi_target_rx_thread(void *arg)
3999{
4000 int rc;
4001 struct iscsi_conn *conn = arg;
4002
4003 /*
4004 * Allow ourselves to be interrupted by SIGINT so that a
4005 * connection recovery / failure event can be triggered externally.
4006 */
4007 allow_signal(SIGINT);
4008 /*
4009 * Wait for iscsi_post_login_handler() to complete before allowing
4010 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4011 */
4012 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4013 if (rc < 0 || iscsi_target_check_conn_state(conn))
4014 return 0;
4015
4016 if (!conn->conn_transport->iscsit_get_rx_pdu)
4017 return 0;
4018
4019 conn->conn_transport->iscsit_get_rx_pdu(conn);
4020
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004021 if (!signal_pending(current))
4022 atomic_set(&conn->transport_failed, 1);
4023 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004024 return 0;
4025}
4026
4027static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4028{
4029 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4030 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004031 /*
4032 * We expect this function to only ever be called from either RX or TX
4033 * thread context via iscsit_close_connection() once the other context
4034 * has been reset -> returned sleeping pre-handler state.
4035 */
4036 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004037 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004038
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004039 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004040 spin_unlock_bh(&conn->cmd_lock);
4041
4042 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004043
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004044 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004045
4046 spin_lock_bh(&conn->cmd_lock);
4047 }
4048 spin_unlock_bh(&conn->cmd_lock);
4049}
4050
4051static void iscsit_stop_timers_for_cmds(
4052 struct iscsi_conn *conn)
4053{
4054 struct iscsi_cmd *cmd;
4055
4056 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004057 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004058 if (cmd->data_direction == DMA_TO_DEVICE)
4059 iscsit_stop_dataout_timer(cmd);
4060 }
4061 spin_unlock_bh(&conn->cmd_lock);
4062}
4063
4064int iscsit_close_connection(
4065 struct iscsi_conn *conn)
4066{
4067 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4068 struct iscsi_session *sess = conn->sess;
4069
4070 pr_debug("Closing iSCSI connection CID %hu on SID:"
4071 " %u\n", conn->cid, sess->sid);
4072 /*
Varun Prakashb4869ee2016-04-20 00:00:18 +05304073 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4074 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4075 * sleeping and the logout response never got sent because the
4076 * connection failed.
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004077 *
4078 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4079 * to signal logout response TX interrupt completion. Go ahead and skip
4080 * this for iser since isert_rx_opcode() does not wait on logout failure,
4081 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004082 */
Varun Prakashb4869ee2016-04-20 00:00:18 +05304083 if ((conn->conn_transport->transport_type == ISCSI_TCP) ||
4084 (conn->conn_transport->transport_type == ISCSI_HW_OFFLOAD))
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004085 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004086
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004087 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4088 if (conn->tx_thread &&
4089 cmpxchg(&conn->tx_thread_active, true, false)) {
4090 send_sig(SIGINT, conn->tx_thread, 1);
4091 kthread_stop(conn->tx_thread);
4092 }
4093 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4094 if (conn->rx_thread &&
4095 cmpxchg(&conn->rx_thread_active, true, false)) {
4096 send_sig(SIGINT, conn->rx_thread, 1);
4097 kthread_stop(conn->rx_thread);
4098 }
4099 }
4100
4101 spin_lock(&iscsit_global->ts_bitmap_lock);
4102 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4103 get_order(1));
4104 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004105
4106 iscsit_stop_timers_for_cmds(conn);
4107 iscsit_stop_nopin_response_timer(conn);
4108 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004109
4110 if (conn->conn_transport->iscsit_wait_conn)
4111 conn->conn_transport->iscsit_wait_conn(conn);
4112
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004113 /*
4114 * During Connection recovery drop unacknowledged out of order
4115 * commands for this connection, and prepare the other commands
4116 * for realligence.
4117 *
4118 * During normal operation clear the out of order commands (but
4119 * do not free the struct iscsi_ooo_cmdsn's) and release all
4120 * struct iscsi_cmds.
4121 */
4122 if (atomic_read(&conn->connection_recovery)) {
4123 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4124 iscsit_prepare_cmds_for_realligance(conn);
4125 } else {
4126 iscsit_clear_ooo_cmdsns_for_conn(conn);
4127 iscsit_release_commands_from_conn(conn);
4128 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004129 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004130
4131 /*
4132 * Handle decrementing session or connection usage count if
4133 * a logout response was not able to be sent because the
4134 * connection failed. Fall back to Session Recovery here.
4135 */
4136 if (atomic_read(&conn->conn_logout_remove)) {
4137 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4138 iscsit_dec_conn_usage_count(conn);
4139 iscsit_dec_session_usage_count(sess);
4140 }
4141 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4142 iscsit_dec_conn_usage_count(conn);
4143
4144 atomic_set(&conn->conn_logout_remove, 0);
4145 atomic_set(&sess->session_reinstatement, 0);
4146 atomic_set(&sess->session_fall_back_to_erl0, 1);
4147 }
4148
4149 spin_lock_bh(&sess->conn_lock);
4150 list_del(&conn->conn_list);
4151
4152 /*
4153 * Attempt to let the Initiator know this connection failed by
4154 * sending an Connection Dropped Async Message on another
4155 * active connection.
4156 */
4157 if (atomic_read(&conn->connection_recovery))
4158 iscsit_build_conn_drop_async_message(conn);
4159
4160 spin_unlock_bh(&sess->conn_lock);
4161
4162 /*
4163 * If connection reinstatement is being performed on this connection,
4164 * up the connection reinstatement semaphore that is being blocked on
4165 * in iscsit_cause_connection_reinstatement().
4166 */
4167 spin_lock_bh(&conn->state_lock);
4168 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4169 spin_unlock_bh(&conn->state_lock);
4170 complete(&conn->conn_wait_comp);
4171 wait_for_completion(&conn->conn_post_wait_comp);
4172 spin_lock_bh(&conn->state_lock);
4173 }
4174
4175 /*
4176 * If connection reinstatement is being performed on this connection
4177 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4178 * connection wait rcfr semaphore that is being blocked on
4179 * an iscsit_connection_reinstatement_rcfr().
4180 */
4181 if (atomic_read(&conn->connection_wait_rcfr)) {
4182 spin_unlock_bh(&conn->state_lock);
4183 complete(&conn->conn_wait_rcfr_comp);
4184 wait_for_completion(&conn->conn_post_wait_comp);
4185 spin_lock_bh(&conn->state_lock);
4186 }
4187 atomic_set(&conn->connection_reinstatement, 1);
4188 spin_unlock_bh(&conn->state_lock);
4189
4190 /*
4191 * If any other processes are accessing this connection pointer we
4192 * must wait until they have completed.
4193 */
4194 iscsit_check_conn_usage_count(conn);
4195
Herbert Xu69110e32016-01-24 21:19:52 +08004196 ahash_request_free(conn->conn_tx_hash);
4197 if (conn->conn_rx_hash) {
4198 struct crypto_ahash *tfm;
4199
4200 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4201 ahash_request_free(conn->conn_rx_hash);
4202 crypto_free_ahash(tfm);
4203 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004204
Joern Engelfbecb652014-09-02 17:49:47 -04004205 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004206
4207 kfree(conn->conn_ops);
4208 conn->conn_ops = NULL;
4209
Al Virobf6932f2012-07-21 08:55:18 +01004210 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004211 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004212
4213 if (conn->conn_transport->iscsit_free_conn)
4214 conn->conn_transport->iscsit_free_conn(conn);
4215
4216 iscsit_put_transport(conn->conn_transport);
4217
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004218 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4219 conn->conn_state = TARG_CONN_STATE_FREE;
4220 kfree(conn);
4221
4222 spin_lock_bh(&sess->conn_lock);
4223 atomic_dec(&sess->nconn);
4224 pr_debug("Decremented iSCSI connection count to %hu from node:"
4225 " %s\n", atomic_read(&sess->nconn),
4226 sess->sess_ops->InitiatorName);
4227 /*
4228 * Make sure that if one connection fails in an non ERL=2 iSCSI
4229 * Session that they all fail.
4230 */
4231 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4232 !atomic_read(&sess->session_logout))
4233 atomic_set(&sess->session_fall_back_to_erl0, 1);
4234
4235 /*
4236 * If this was not the last connection in the session, and we are
4237 * performing session reinstatement or falling back to ERL=0, call
4238 * iscsit_stop_session() without sleeping to shutdown the other
4239 * active connections.
4240 */
4241 if (atomic_read(&sess->nconn)) {
4242 if (!atomic_read(&sess->session_reinstatement) &&
4243 !atomic_read(&sess->session_fall_back_to_erl0)) {
4244 spin_unlock_bh(&sess->conn_lock);
4245 return 0;
4246 }
4247 if (!atomic_read(&sess->session_stop_active)) {
4248 atomic_set(&sess->session_stop_active, 1);
4249 spin_unlock_bh(&sess->conn_lock);
4250 iscsit_stop_session(sess, 0, 0);
4251 return 0;
4252 }
4253 spin_unlock_bh(&sess->conn_lock);
4254 return 0;
4255 }
4256
4257 /*
4258 * If this was the last connection in the session and one of the
4259 * following is occurring:
4260 *
4261 * Session Reinstatement is not being performed, and are falling back
4262 * to ERL=0 call iscsit_close_session().
4263 *
4264 * Session Logout was requested. iscsit_close_session() will be called
4265 * elsewhere.
4266 *
4267 * Session Continuation is not being performed, start the Time2Retain
4268 * handler and check if sleep_on_sess_wait_sem is active.
4269 */
4270 if (!atomic_read(&sess->session_reinstatement) &&
4271 atomic_read(&sess->session_fall_back_to_erl0)) {
4272 spin_unlock_bh(&sess->conn_lock);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004273 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004274
4275 return 0;
4276 } else if (atomic_read(&sess->session_logout)) {
4277 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4278 sess->session_state = TARG_SESS_STATE_FREE;
4279 spin_unlock_bh(&sess->conn_lock);
4280
4281 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4282 complete(&sess->session_wait_comp);
4283
4284 return 0;
4285 } else {
4286 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4287 sess->session_state = TARG_SESS_STATE_FAILED;
4288
4289 if (!atomic_read(&sess->session_continuation)) {
4290 spin_unlock_bh(&sess->conn_lock);
4291 iscsit_start_time2retain_handler(sess);
4292 } else
4293 spin_unlock_bh(&sess->conn_lock);
4294
4295 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4296 complete(&sess->session_wait_comp);
4297
4298 return 0;
4299 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004300}
4301
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004302/*
4303 * If the iSCSI Session for the iSCSI Initiator Node exists,
4304 * forcefully shutdown the iSCSI NEXUS.
4305 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004306int iscsit_close_session(struct iscsi_session *sess)
4307{
Andy Grover60bfcf82013-10-09 11:05:58 -07004308 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004309 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4310
4311 if (atomic_read(&sess->nconn)) {
4312 pr_err("%d connection(s) still exist for iSCSI session"
4313 " to %s\n", atomic_read(&sess->nconn),
4314 sess->sess_ops->InitiatorName);
4315 BUG();
4316 }
4317
4318 spin_lock_bh(&se_tpg->session_lock);
4319 atomic_set(&sess->session_logout, 1);
4320 atomic_set(&sess->session_reinstatement, 1);
4321 iscsit_stop_time2retain_timer(sess);
4322 spin_unlock_bh(&se_tpg->session_lock);
4323
4324 /*
4325 * transport_deregister_session_configfs() will clear the
4326 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4327 * can be setting it again with __transport_register_session() in
4328 * iscsi_post_login_handler() again after the iscsit_stop_session()
4329 * completes in iscsi_np context.
4330 */
4331 transport_deregister_session_configfs(sess->se_sess);
4332
4333 /*
4334 * If any other processes are accessing this session pointer we must
4335 * wait until they have completed. If we are in an interrupt (the
4336 * time2retain handler) and contain and active session usage count we
4337 * restart the timer and exit.
4338 */
4339 if (!in_interrupt()) {
4340 if (iscsit_check_session_usage_count(sess) == 1)
4341 iscsit_stop_session(sess, 1, 1);
4342 } else {
4343 if (iscsit_check_session_usage_count(sess) == 2) {
4344 atomic_set(&sess->session_logout, 0);
4345 iscsit_start_time2retain_handler(sess);
4346 return 0;
4347 }
4348 }
4349
4350 transport_deregister_session(sess->se_sess);
4351
4352 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4353 iscsit_free_connection_recovery_entires(sess);
4354
4355 iscsit_free_all_ooo_cmdsns(sess);
4356
4357 spin_lock_bh(&se_tpg->session_lock);
4358 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4359 sess->session_state = TARG_SESS_STATE_FREE;
4360 pr_debug("Released iSCSI session from node: %s\n",
4361 sess->sess_ops->InitiatorName);
4362 tpg->nsessions--;
4363 if (tpg->tpg_tiqn)
4364 tpg->tpg_tiqn->tiqn_nsessions--;
4365
4366 pr_debug("Decremented number of active iSCSI Sessions on"
4367 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4368
4369 spin_lock(&sess_idr_lock);
4370 idr_remove(&sess_idr, sess->session_index);
4371 spin_unlock(&sess_idr_lock);
4372
4373 kfree(sess->sess_ops);
4374 sess->sess_ops = NULL;
4375 spin_unlock_bh(&se_tpg->session_lock);
4376
4377 kfree(sess);
4378 return 0;
4379}
4380
4381static void iscsit_logout_post_handler_closesession(
4382 struct iscsi_conn *conn)
4383{
4384 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004385 int sleep = 1;
4386 /*
4387 * Traditional iscsi/tcp will invoke this logic from TX thread
4388 * context during session logout, so clear tx_thread_active and
4389 * sleep if iscsit_close_connection() has not already occured.
4390 *
4391 * Since iser-target invokes this logic from it's own workqueue,
4392 * always sleep waiting for RX/TX thread shutdown to complete
4393 * within iscsit_close_connection().
4394 */
Varun Prakash1c50ff42016-04-20 00:00:17 +05304395 if ((conn->conn_transport->transport_type == ISCSI_TCP) ||
4396 (conn->conn_transport->transport_type == ISCSI_HW_OFFLOAD))
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004397 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004398
4399 atomic_set(&conn->conn_logout_remove, 0);
4400 complete(&conn->conn_logout_comp);
4401
4402 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004403 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004404 iscsit_dec_session_usage_count(sess);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004405 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004406}
4407
4408static void iscsit_logout_post_handler_samecid(
4409 struct iscsi_conn *conn)
4410{
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004411 int sleep = 1;
4412
Varun Prakash1c50ff42016-04-20 00:00:17 +05304413 if ((conn->conn_transport->transport_type == ISCSI_TCP) ||
4414 (conn->conn_transport->transport_type == ISCSI_HW_OFFLOAD))
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004415 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004416
4417 atomic_set(&conn->conn_logout_remove, 0);
4418 complete(&conn->conn_logout_comp);
4419
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004420 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004421 iscsit_dec_conn_usage_count(conn);
4422}
4423
4424static void iscsit_logout_post_handler_diffcid(
4425 struct iscsi_conn *conn,
4426 u16 cid)
4427{
4428 struct iscsi_conn *l_conn;
4429 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004430 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004431
4432 if (!sess)
4433 return;
4434
4435 spin_lock_bh(&sess->conn_lock);
4436 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4437 if (l_conn->cid == cid) {
4438 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004439 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004440 break;
4441 }
4442 }
4443 spin_unlock_bh(&sess->conn_lock);
4444
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004445 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004446 return;
4447
4448 if (l_conn->sock)
4449 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4450
4451 spin_lock_bh(&l_conn->state_lock);
4452 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4453 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4454 spin_unlock_bh(&l_conn->state_lock);
4455
4456 iscsit_cause_connection_reinstatement(l_conn, 1);
4457 iscsit_dec_conn_usage_count(l_conn);
4458}
4459
4460/*
4461 * Return of 0 causes the TX thread to restart.
4462 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004463int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004464 struct iscsi_cmd *cmd,
4465 struct iscsi_conn *conn)
4466{
4467 int ret = 0;
4468
4469 switch (cmd->logout_reason) {
4470 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4471 switch (cmd->logout_response) {
4472 case ISCSI_LOGOUT_SUCCESS:
4473 case ISCSI_LOGOUT_CLEANUP_FAILED:
4474 default:
4475 iscsit_logout_post_handler_closesession(conn);
4476 break;
4477 }
4478 ret = 0;
4479 break;
4480 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4481 if (conn->cid == cmd->logout_cid) {
4482 switch (cmd->logout_response) {
4483 case ISCSI_LOGOUT_SUCCESS:
4484 case ISCSI_LOGOUT_CLEANUP_FAILED:
4485 default:
4486 iscsit_logout_post_handler_samecid(conn);
4487 break;
4488 }
4489 ret = 0;
4490 } else {
4491 switch (cmd->logout_response) {
4492 case ISCSI_LOGOUT_SUCCESS:
4493 iscsit_logout_post_handler_diffcid(conn,
4494 cmd->logout_cid);
4495 break;
4496 case ISCSI_LOGOUT_CID_NOT_FOUND:
4497 case ISCSI_LOGOUT_CLEANUP_FAILED:
4498 default:
4499 break;
4500 }
4501 ret = 1;
4502 }
4503 break;
4504 case ISCSI_LOGOUT_REASON_RECOVERY:
4505 switch (cmd->logout_response) {
4506 case ISCSI_LOGOUT_SUCCESS:
4507 case ISCSI_LOGOUT_CID_NOT_FOUND:
4508 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4509 case ISCSI_LOGOUT_CLEANUP_FAILED:
4510 default:
4511 break;
4512 }
4513 ret = 1;
4514 break;
4515 default:
4516 break;
4517
4518 }
4519 return ret;
4520}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004521EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004522
4523void iscsit_fail_session(struct iscsi_session *sess)
4524{
4525 struct iscsi_conn *conn;
4526
4527 spin_lock_bh(&sess->conn_lock);
4528 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4529 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4530 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4531 }
4532 spin_unlock_bh(&sess->conn_lock);
4533
4534 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4535 sess->session_state = TARG_SESS_STATE_FAILED;
4536}
4537
4538int iscsit_free_session(struct iscsi_session *sess)
4539{
4540 u16 conn_count = atomic_read(&sess->nconn);
4541 struct iscsi_conn *conn, *conn_tmp = NULL;
4542 int is_last;
4543
4544 spin_lock_bh(&sess->conn_lock);
4545 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4546
4547 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4548 conn_list) {
4549 if (conn_count == 0)
4550 break;
4551
4552 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4553 is_last = 1;
4554 } else {
4555 iscsit_inc_conn_usage_count(conn_tmp);
4556 is_last = 0;
4557 }
4558 iscsit_inc_conn_usage_count(conn);
4559
4560 spin_unlock_bh(&sess->conn_lock);
4561 iscsit_cause_connection_reinstatement(conn, 1);
4562 spin_lock_bh(&sess->conn_lock);
4563
4564 iscsit_dec_conn_usage_count(conn);
4565 if (is_last == 0)
4566 iscsit_dec_conn_usage_count(conn_tmp);
4567
4568 conn_count--;
4569 }
4570
4571 if (atomic_read(&sess->nconn)) {
4572 spin_unlock_bh(&sess->conn_lock);
4573 wait_for_completion(&sess->session_wait_comp);
4574 } else
4575 spin_unlock_bh(&sess->conn_lock);
4576
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004577 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004578 return 0;
4579}
4580
4581void iscsit_stop_session(
4582 struct iscsi_session *sess,
4583 int session_sleep,
4584 int connection_sleep)
4585{
4586 u16 conn_count = atomic_read(&sess->nconn);
4587 struct iscsi_conn *conn, *conn_tmp = NULL;
4588 int is_last;
4589
4590 spin_lock_bh(&sess->conn_lock);
4591 if (session_sleep)
4592 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4593
4594 if (connection_sleep) {
4595 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4596 conn_list) {
4597 if (conn_count == 0)
4598 break;
4599
4600 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4601 is_last = 1;
4602 } else {
4603 iscsit_inc_conn_usage_count(conn_tmp);
4604 is_last = 0;
4605 }
4606 iscsit_inc_conn_usage_count(conn);
4607
4608 spin_unlock_bh(&sess->conn_lock);
4609 iscsit_cause_connection_reinstatement(conn, 1);
4610 spin_lock_bh(&sess->conn_lock);
4611
4612 iscsit_dec_conn_usage_count(conn);
4613 if (is_last == 0)
4614 iscsit_dec_conn_usage_count(conn_tmp);
4615 conn_count--;
4616 }
4617 } else {
4618 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4619 iscsit_cause_connection_reinstatement(conn, 0);
4620 }
4621
4622 if (session_sleep && atomic_read(&sess->nconn)) {
4623 spin_unlock_bh(&sess->conn_lock);
4624 wait_for_completion(&sess->session_wait_comp);
4625 } else
4626 spin_unlock_bh(&sess->conn_lock);
4627}
4628
4629int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4630{
4631 struct iscsi_session *sess;
4632 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4633 struct se_session *se_sess, *se_sess_tmp;
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004634 LIST_HEAD(free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004635 int session_count = 0;
4636
4637 spin_lock_bh(&se_tpg->session_lock);
4638 if (tpg->nsessions && !force) {
4639 spin_unlock_bh(&se_tpg->session_lock);
4640 return -1;
4641 }
4642
4643 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4644 sess_list) {
4645 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4646
4647 spin_lock(&sess->conn_lock);
4648 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4649 atomic_read(&sess->session_logout) ||
4650 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4651 spin_unlock(&sess->conn_lock);
4652 continue;
4653 }
4654 atomic_set(&sess->session_reinstatement, 1);
4655 spin_unlock(&sess->conn_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004656
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004657 list_move_tail(&se_sess->sess_list, &free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004658 }
4659 spin_unlock_bh(&se_tpg->session_lock);
4660
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004661 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4662 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4663
4664 iscsit_free_session(sess);
4665 session_count++;
4666 }
4667
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004668 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4669 " Group: %hu\n", session_count, tpg->tpgt);
4670 return 0;
4671}
4672
4673MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4674MODULE_VERSION("4.1.x");
4675MODULE_AUTHOR("nab@Linux-iSCSI.org");
4676MODULE_LICENSE("GPL");
4677
4678module_init(iscsi_target_init_module);
4679module_exit(iscsi_target_cleanup_module);