blob: 202a42858f2537e7aa8f790f3969ba570ad93208 [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
19#include <linux/string.h>
20#include <linux/kthread.h>
21#include <linux/crypto.h>
22#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(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000272 struct __kernel_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;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000279 u16 port;
280
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);
291 } else {
292 sock_in = (struct sockaddr_in *)sockaddr;
293 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
294
295 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
296 ip_match = true;
297
298 port = ntohs(sock_in->sin_port);
299 }
300
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200301 if (ip_match && (np->np_port == port) &&
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800302 (np->np_network_transport == network_transport))
303 return true;
304
305 return false;
306}
307
Andy Groveree291e62014-01-24 16:18:54 -0800308/*
309 * Called with mutex np_lock held
310 */
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800311static struct iscsi_np *iscsit_get_np(
312 struct __kernel_sockaddr_storage *sockaddr,
313 int network_transport)
314{
315 struct iscsi_np *np;
316 bool match;
317
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000318 list_for_each_entry(np, &g_np_list, np_list) {
Andy Groveree291e62014-01-24 16:18:54 -0800319 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000320 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
Andy Groveree291e62014-01-24 16:18:54 -0800321 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000322 continue;
323 }
324
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800325 match = iscsit_check_np_match(sockaddr, np, network_transport);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200326 if (match) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000327 /*
328 * Increment the np_exports reference count now to
329 * prevent iscsit_del_np() below from being called
330 * while iscsi_tpg_add_network_portal() is called.
331 */
332 np->np_exports++;
Andy Groveree291e62014-01-24 16:18:54 -0800333 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000334 return np;
335 }
Andy Groveree291e62014-01-24 16:18:54 -0800336 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000337 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000338
339 return NULL;
340}
341
342struct iscsi_np *iscsit_add_np(
343 struct __kernel_sockaddr_storage *sockaddr,
344 char *ip_str,
345 int network_transport)
346{
347 struct sockaddr_in *sock_in;
348 struct sockaddr_in6 *sock_in6;
349 struct iscsi_np *np;
350 int ret;
Andy Groveree291e62014-01-24 16:18:54 -0800351
352 mutex_lock(&np_lock);
353
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000354 /*
355 * Locate the existing struct iscsi_np if already active..
356 */
357 np = iscsit_get_np(sockaddr, network_transport);
Andy Groveree291e62014-01-24 16:18:54 -0800358 if (np) {
359 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000360 return np;
Andy Groveree291e62014-01-24 16:18:54 -0800361 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000362
363 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
364 if (!np) {
365 pr_err("Unable to allocate memory for struct iscsi_np\n");
Andy Groveree291e62014-01-24 16:18:54 -0800366 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000367 return ERR_PTR(-ENOMEM);
368 }
369
370 np->np_flags |= NPF_IP_NETWORK;
371 if (sockaddr->ss_family == AF_INET6) {
372 sock_in6 = (struct sockaddr_in6 *)sockaddr;
373 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
374 np->np_port = ntohs(sock_in6->sin6_port);
375 } else {
376 sock_in = (struct sockaddr_in *)sockaddr;
377 sprintf(np->np_ip, "%s", ip_str);
378 np->np_port = ntohs(sock_in->sin_port);
379 }
380
381 np->np_network_transport = network_transport;
382 spin_lock_init(&np->np_thread_lock);
383 init_completion(&np->np_restart_comp);
384 INIT_LIST_HEAD(&np->np_list);
385
386 ret = iscsi_target_setup_login_socket(np, sockaddr);
387 if (ret != 0) {
388 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800389 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000390 return ERR_PTR(ret);
391 }
392
393 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
394 if (IS_ERR(np->np_thread)) {
395 pr_err("Unable to create kthread: iscsi_np\n");
396 ret = PTR_ERR(np->np_thread);
397 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800398 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000399 return ERR_PTR(ret);
400 }
401 /*
402 * Increment the np_exports reference count now to prevent
403 * iscsit_del_np() below from being run while a new call to
404 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
405 * active. We don't need to hold np->np_thread_lock at this
406 * point because iscsi_np has not been added to g_np_list yet.
407 */
408 np->np_exports = 1;
Andy Groveree291e62014-01-24 16:18:54 -0800409 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000410
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000411 list_add_tail(&np->np_list, &g_np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800412 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000413
414 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800415 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000416
417 return np;
418}
419
420int iscsit_reset_np_thread(
421 struct iscsi_np *np,
422 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700423 struct iscsi_portal_group *tpg,
424 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000425{
426 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000427 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
428 spin_unlock_bh(&np->np_thread_lock);
429 return 0;
430 }
431 np->np_thread_state = ISCSI_NP_THREAD_RESET;
432
433 if (np->np_thread) {
434 spin_unlock_bh(&np->np_thread_lock);
435 send_sig(SIGINT, np->np_thread, 1);
436 wait_for_completion(&np->np_restart_comp);
437 spin_lock_bh(&np->np_thread_lock);
438 }
439 spin_unlock_bh(&np->np_thread_lock);
440
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700441 if (tpg_np && shutdown) {
442 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
443
444 wait_for_completion(&tpg_np->tpg_np_comp);
445 }
446
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000447 return 0;
448}
449
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800450static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000451{
Al Virobf6932f2012-07-21 08:55:18 +0100452 if (np->np_socket)
453 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000454}
455
456int iscsit_del_np(struct iscsi_np *np)
457{
458 spin_lock_bh(&np->np_thread_lock);
459 np->np_exports--;
460 if (np->np_exports) {
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700461 np->enabled = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000462 spin_unlock_bh(&np->np_thread_lock);
463 return 0;
464 }
465 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
466 spin_unlock_bh(&np->np_thread_lock);
467
468 if (np->np_thread) {
469 /*
470 * We need to send the signal to wakeup Linux/Net
471 * which may be sleeping in sock_accept()..
472 */
473 send_sig(SIGINT, np->np_thread, 1);
474 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800475 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000476 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800477
478 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000479
Andy Groveree291e62014-01-24 16:18:54 -0800480 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000481 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800482 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000483
484 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800485 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000486
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800487 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000488 kfree(np);
489 return 0;
490}
491
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700492static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
493static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
494
495static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
496{
497 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
498 return 0;
499}
500
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700501static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
502{
503 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
504
505 spin_lock_bh(&conn->cmd_lock);
506 if (!list_empty(&cmd->i_conn_node))
507 list_del_init(&cmd->i_conn_node);
508 spin_unlock_bh(&conn->cmd_lock);
509
510 __iscsit_free_cmd(cmd, scsi_cmd, true);
511}
512
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700513static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
514{
515 return TARGET_PROT_NORMAL;
516}
517
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800518static struct iscsit_transport iscsi_target_transport = {
519 .name = "iSCSI/TCP",
520 .transport_type = ISCSI_TCP,
521 .owner = NULL,
522 .iscsit_setup_np = iscsit_setup_np,
523 .iscsit_accept_np = iscsit_accept_np,
524 .iscsit_free_np = iscsit_free_np,
525 .iscsit_get_login_rx = iscsit_get_login_rx,
526 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800527 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700528 .iscsit_immediate_queue = iscsit_immediate_queue,
529 .iscsit_response_queue = iscsit_response_queue,
530 .iscsit_queue_data_in = iscsit_queue_rsp,
531 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700532 .iscsit_aborted_task = iscsit_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700533 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800534};
535
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000536static int __init iscsi_target_init_module(void)
537{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800538 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000539
540 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
541
542 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
543 if (!iscsit_global) {
544 pr_err("Unable to allocate memory for iscsit_global\n");
545 return -1;
546 }
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800547 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000548 mutex_init(&auth_id_lock);
549 spin_lock_init(&sess_idr_lock);
550 idr_init(&tiqn_idr);
551 idr_init(&sess_idr);
552
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200553 ret = target_register_template(&iscsi_ops);
554 if (ret)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000555 goto out;
556
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800557 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
558 iscsit_global->ts_bitmap = vzalloc(size);
559 if (!iscsit_global->ts_bitmap) {
560 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000561 goto configfs_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000562 }
563
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000564 lio_qr_cache = kmem_cache_create("lio_qr_cache",
565 sizeof(struct iscsi_queue_req),
566 __alignof__(struct iscsi_queue_req), 0, NULL);
567 if (!lio_qr_cache) {
568 pr_err("nable to kmem_cache_create() for"
569 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800570 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000571 }
572
573 lio_dr_cache = kmem_cache_create("lio_dr_cache",
574 sizeof(struct iscsi_datain_req),
575 __alignof__(struct iscsi_datain_req), 0, NULL);
576 if (!lio_dr_cache) {
577 pr_err("Unable to kmem_cache_create() for"
578 " lio_dr_cache\n");
579 goto qr_out;
580 }
581
582 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
583 sizeof(struct iscsi_ooo_cmdsn),
584 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
585 if (!lio_ooo_cache) {
586 pr_err("Unable to kmem_cache_create() for"
587 " lio_ooo_cache\n");
588 goto dr_out;
589 }
590
591 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
592 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
593 0, NULL);
594 if (!lio_r2t_cache) {
595 pr_err("Unable to kmem_cache_create() for"
596 " lio_r2t_cache\n");
597 goto ooo_out;
598 }
599
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800600 iscsit_register_transport(&iscsi_target_transport);
601
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000602 if (iscsit_load_discovery_tpg() < 0)
603 goto r2t_out;
604
605 return ret;
606r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100607 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000608 kmem_cache_destroy(lio_r2t_cache);
609ooo_out:
610 kmem_cache_destroy(lio_ooo_cache);
611dr_out:
612 kmem_cache_destroy(lio_dr_cache);
613qr_out:
614 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800615bitmap_out:
616 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000617configfs_out:
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200618 /* XXX: this probably wants it to be it's own unwind step.. */
619 if (iscsit_global->discovery_tpg)
620 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
621 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000622out:
623 kfree(iscsit_global);
624 return -ENOMEM;
625}
626
627static void __exit iscsi_target_cleanup_module(void)
628{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000629 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800630 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000631 kmem_cache_destroy(lio_qr_cache);
632 kmem_cache_destroy(lio_dr_cache);
633 kmem_cache_destroy(lio_ooo_cache);
634 kmem_cache_destroy(lio_r2t_cache);
635
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200636 /*
637 * Shutdown discovery sessions and disable discovery TPG
638 */
639 if (iscsit_global->discovery_tpg)
640 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000641
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200642 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000643
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800644 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000645 kfree(iscsit_global);
646}
647
Andy Grover8b1e1242012-04-03 15:51:12 -0700648static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700649 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000650 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700651 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000652{
653 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000654
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000655 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000656 if (!cmd)
657 return -1;
658
659 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700660 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000661
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100662 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000663 if (!cmd->buf_ptr) {
664 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700665 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000666 return -1;
667 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000668
669 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700670 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000671 spin_unlock_bh(&conn->cmd_lock);
672
673 cmd->i_state = ISTATE_SEND_REJECT;
674 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
675
Nicholas Bellingerba159912013-07-03 03:48:24 -0700676 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000677}
678
Nicholas Bellingerba159912013-07-03 03:48:24 -0700679static int iscsit_add_reject_from_cmd(
680 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000681 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700682 bool add_to_conn,
683 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000684{
685 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000686
687 if (!cmd->conn) {
688 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
689 cmd->init_task_tag);
690 return -1;
691 }
692 conn = cmd->conn;
693
694 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700695 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000696
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100697 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000698 if (!cmd->buf_ptr) {
699 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700700 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000701 return -1;
702 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000703
704 if (add_to_conn) {
705 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700706 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000707 spin_unlock_bh(&conn->cmd_lock);
708 }
709
710 cmd->i_state = ISTATE_SEND_REJECT;
711 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800712 /*
713 * Perform the kref_put now if se_cmd has already been setup by
714 * scsit_setup_scsi_cmd()
715 */
716 if (cmd->se_cmd.se_tfo != NULL) {
717 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +0200718 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800719 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700720 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000721}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700722
723static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
724 unsigned char *buf)
725{
726 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
727}
728
729int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
730{
731 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
732}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000733
734/*
735 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700736 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000737 */
738static int iscsit_map_iovec(
739 struct iscsi_cmd *cmd,
740 struct kvec *iov,
741 u32 data_offset,
742 u32 data_length)
743{
744 u32 i = 0;
745 struct scatterlist *sg;
746 unsigned int page_off;
747
748 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700749 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000750 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700751 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000752 page_off = (data_offset % PAGE_SIZE);
753
754 cmd->first_data_sg = sg;
755 cmd->first_data_sg_off = page_off;
756
757 while (data_length) {
758 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
759
760 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
761 iov[i].iov_len = cur_len;
762
763 data_length -= cur_len;
764 page_off = 0;
765 sg = sg_next(sg);
766 i++;
767 }
768
769 cmd->kmapped_nents = i;
770
771 return i;
772}
773
774static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
775{
776 u32 i;
777 struct scatterlist *sg;
778
779 sg = cmd->first_data_sg;
780
781 for (i = 0; i < cmd->kmapped_nents; i++)
782 kunmap(sg_page(&sg[i]));
783}
784
785static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
786{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700787 LIST_HEAD(ack_list);
788 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000789
790 conn->exp_statsn = exp_statsn;
791
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800792 if (conn->sess->sess_ops->RDMAExtensions)
793 return;
794
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000795 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700796 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000797 spin_lock(&cmd->istate_lock);
798 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800799 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000800 cmd->i_state = ISTATE_REMOVE;
801 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700802 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000803 continue;
804 }
805 spin_unlock(&cmd->istate_lock);
806 }
807 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700808
809 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800810 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700811 iscsit_free_cmd(cmd, false);
812 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000813}
814
815static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
816{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700817 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000818
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400819 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000820
821 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
822 if (!cmd->iov_data) {
823 pr_err("Unable to allocate cmd->iov_data\n");
824 return -ENOMEM;
825 }
826
827 cmd->orig_iov_data_count = iov_count;
828 return 0;
829}
830
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800831int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
832 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000833{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800834 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000835 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700836 int iscsi_task_attr;
837 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000838
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800839 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000840
841 hdr = (struct iscsi_scsi_req *) buf;
842 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000843
844 /* FIXME; Add checks for AdditionalHeaderSegment */
845
846 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
847 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
848 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
849 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700850 return iscsit_add_reject_cmd(cmd,
851 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000852 }
853
854 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
855 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
856 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800857 * From RFC-3720 Section 10.3.1:
858 *
859 * "Either or both of R and W MAY be 1 when either the
860 * Expected Data Transfer Length and/or Bidirectional Read
861 * Expected Data Transfer Length are 0"
862 *
863 * For this case, go ahead and clear the unnecssary bits
864 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000865 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800866 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
867 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000868
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800869 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000870 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800871 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000872 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000873
874 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
875 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
876 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
877 " MUST be set if Expected Data Transfer Length is not 0."
878 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700879 return iscsit_add_reject_cmd(cmd,
880 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000881 }
882
883 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
884 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
885 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700886 return iscsit_add_reject_cmd(cmd,
887 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000888 }
889
890 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
891 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
892 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700893 return iscsit_add_reject_cmd(cmd,
894 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000895 }
896
897 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
898 pr_err("ImmediateData=No but DataSegmentLength=%u,"
899 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700900 return iscsit_add_reject_cmd(cmd,
901 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000902 }
903
Nicholas Bellingerba159912013-07-03 03:48:24 -0700904 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000905 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
906 pr_err("Expected Data Transfer Length and Length of"
907 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
908 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700909 return iscsit_add_reject_cmd(cmd,
910 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000911 }
912
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400913 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000914 pr_err("DataSegmentLength: %u is greater than"
915 " EDTL: %u, protocol error.\n", payload_length,
916 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700917 return iscsit_add_reject_cmd(cmd,
918 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000919 }
920
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700921 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000922 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700923 " MaxXmitDataSegmentLength: %u, protocol error.\n",
924 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700925 return iscsit_add_reject_cmd(cmd,
926 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000927 }
928
929 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
930 pr_err("DataSegmentLength: %u is greater than"
931 " FirstBurstLength: %u, protocol error.\n",
932 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700933 return iscsit_add_reject_cmd(cmd,
934 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000935 }
936
937 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
938 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
939 DMA_NONE;
940
Andy Groverd28b11692012-04-03 15:51:22 -0700941 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700942 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
943 /*
944 * Figure out the SAM Task Attribute for the incoming SCSI CDB
945 */
946 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
947 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800948 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700949 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800950 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700951 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800952 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700953 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800954 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700955 else {
956 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800957 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
958 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700959 }
960
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000961 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
962 cmd->i_state = ISTATE_NEW_CMD;
963 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
964 cmd->immediate_data = (payload_length) ? 1 : 0;
965 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
966 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
967 if (cmd->unsolicited_data)
968 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
969
970 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
971 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
Sagi Grimbergc1e34b62015-01-26 12:49:05 +0200972 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000973 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
974 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400975 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
976 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000977 cmd->first_burst_len = payload_length;
978
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800979 if (!conn->sess->sess_ops->RDMAExtensions &&
980 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000981 struct iscsi_datain_req *dr;
982
983 dr = iscsit_allocate_datain_req();
984 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700985 return iscsit_add_reject_cmd(cmd,
986 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000987
988 iscsit_attach_datain_req(cmd, dr);
989 }
990
991 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700992 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
993 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200994 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400995 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
996 cmd->data_direction, sam_task_attr,
997 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700998
999 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1000 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001001 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1002 conn->cid);
1003
Bart Van Asscheafc16602015-04-27 13:52:36 +02001004 target_get_sess_cmd(&cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001005
Christoph Hellwigde103c92012-11-06 12:24:09 -08001006 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1007 scsilun_to_int(&hdr->lun));
1008 if (cmd->sense_reason)
1009 goto attach_cmd;
1010
Bart Van Assche649ee052015-04-14 13:26:44 +02001011 /* only used for printks or comparing with ->ref_task_tag */
1012 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001013 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1014 if (cmd->sense_reason) {
1015 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001016 return iscsit_add_reject_cmd(cmd,
1017 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001018 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001019
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001020 goto attach_cmd;
1021 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001022
Christoph Hellwigde103c92012-11-06 12:24:09 -08001023 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001024 return iscsit_add_reject_cmd(cmd,
1025 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001026 }
1027
1028attach_cmd:
1029 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001030 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001031 spin_unlock_bh(&conn->cmd_lock);
1032 /*
1033 * Check if we need to delay processing because of ALUA
1034 * Active/NonOptimized primary access state..
1035 */
1036 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001037
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001038 return 0;
1039}
1040EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001041
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001042void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1043{
1044 iscsit_set_dataout_sequence_values(cmd);
1045
1046 spin_lock_bh(&cmd->dataout_timeout_lock);
1047 iscsit_start_dataout_timer(cmd, cmd->conn);
1048 spin_unlock_bh(&cmd->dataout_timeout_lock);
1049}
1050EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1051
1052int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1053 struct iscsi_scsi_req *hdr)
1054{
1055 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001056 /*
1057 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1058 * the Immediate Bit is not set, and no Immediate
1059 * Data is attached.
1060 *
1061 * A PDU/CmdSN carrying Immediate Data can only
1062 * be processed after the DataCRC has passed.
1063 * If the DataCRC fails, the CmdSN MUST NOT
1064 * be acknowledged. (See below)
1065 */
1066 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001067 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1068 (unsigned char *)hdr, hdr->cmdsn);
1069 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1070 return -1;
1071 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001072 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001073 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001074 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001075 }
1076
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001077 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001078
1079 /*
1080 * If no Immediate Data is attached, it's OK to return now.
1081 */
1082 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001083 if (!cmd->sense_reason && cmd->unsolicited_data)
1084 iscsit_set_unsoliticed_dataout(cmd);
1085 if (!cmd->sense_reason)
1086 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001087
Bart Van Asscheafc16602015-04-27 13:52:36 +02001088 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001089 return 0;
1090 }
1091
1092 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001093 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1094 * execution. These exceptions are processed in CmdSN order using
1095 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001096 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001097 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001098 if (cmd->reject_reason)
1099 return 0;
1100
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001101 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001102 }
1103 /*
1104 * Call directly into transport_generic_new_cmd() to perform
1105 * the backend memory allocation.
1106 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001107 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001108 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001109 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001110
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001111 return 0;
1112}
1113EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1114
1115static int
1116iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1117 bool dump_payload)
1118{
1119 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1120 /*
1121 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1122 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001123 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001124 goto after_immediate_data;
1125
1126 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1127 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001128after_immediate_data:
1129 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1130 /*
1131 * A PDU/CmdSN carrying Immediate Data passed
1132 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1133 * Immediate Bit is not set.
1134 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001135 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1136 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001137 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001138 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001139
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001140 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001141 int rc;
1142
1143 rc = iscsit_dump_data_payload(cmd->conn,
1144 cmd->first_burst_len, 1);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001145 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001146 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001147 } else if (cmd->unsolicited_data)
1148 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001149
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001150 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1151 /*
1152 * Immediate Data failed DataCRC and ERL>=1,
1153 * silently drop this PDU and let the initiator
1154 * plug the CmdSN gap.
1155 *
1156 * FIXME: Send Unsolicited NOPIN with reserved
1157 * TTT here to help the initiator figure out
1158 * the missing CmdSN, although they should be
1159 * intelligent enough to determine the missing
1160 * CmdSN and issue a retry to plug the sequence.
1161 */
1162 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001163 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001164 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1165 return -1;
1166
1167 return 0;
1168}
1169
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001170static int
1171iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1172 unsigned char *buf)
1173{
1174 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1175 int rc, immed_data;
1176 bool dump_payload = false;
1177
1178 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1179 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001180 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001181 /*
1182 * Allocation iovecs needed for struct socket operations for
1183 * traditional iSCSI block I/O.
1184 */
1185 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001186 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001187 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001188 }
1189 immed_data = cmd->immediate_data;
1190
1191 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1192 if (rc < 0)
1193 return rc;
1194 else if (rc > 0)
1195 dump_payload = true;
1196
1197 if (!immed_data)
1198 return 0;
1199
1200 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1201}
1202
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001203static u32 iscsit_do_crypto_hash_sg(
1204 struct hash_desc *hash,
1205 struct iscsi_cmd *cmd,
1206 u32 data_offset,
1207 u32 data_length,
1208 u32 padding,
1209 u8 *pad_bytes)
1210{
1211 u32 data_crc;
1212 u32 i;
1213 struct scatterlist *sg;
1214 unsigned int page_off;
1215
1216 crypto_hash_init(hash);
1217
1218 sg = cmd->first_data_sg;
1219 page_off = cmd->first_data_sg_off;
1220
1221 i = 0;
1222 while (data_length) {
1223 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1224
1225 crypto_hash_update(hash, &sg[i], cur_len);
1226
1227 data_length -= cur_len;
1228 page_off = 0;
1229 i++;
1230 }
1231
1232 if (padding) {
1233 struct scatterlist pad_sg;
1234
1235 sg_init_one(&pad_sg, pad_bytes, padding);
1236 crypto_hash_update(hash, &pad_sg, padding);
1237 }
1238 crypto_hash_final(hash, (u8 *) &data_crc);
1239
1240 return data_crc;
1241}
1242
1243static void iscsit_do_crypto_hash_buf(
1244 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001245 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001246 u32 payload_length,
1247 u32 padding,
1248 u8 *pad_bytes,
1249 u8 *data_crc)
1250{
1251 struct scatterlist sg;
1252
1253 crypto_hash_init(hash);
1254
Jörn Engel8359cf42011-11-24 02:05:51 +01001255 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001256 crypto_hash_update(hash, &sg, payload_length);
1257
1258 if (padding) {
1259 sg_init_one(&sg, pad_bytes, padding);
1260 crypto_hash_update(hash, &sg, padding);
1261 }
1262 crypto_hash_final(hash, data_crc);
1263}
1264
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001265int
1266iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1267 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001268{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001269 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001270 struct iscsi_cmd *cmd = NULL;
1271 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001272 u32 payload_length = ntoh24(hdr->dlength);
1273 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001274
1275 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001276 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1277 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001278 }
1279
1280 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001281 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001282
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001283 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001284 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001285 " MaxXmitDataSegmentLength: %u\n", payload_length,
1286 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001287 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1288 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001289 }
1290
1291 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1292 payload_length);
1293 if (!cmd)
1294 return 0;
1295
1296 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1297 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001298 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001299 payload_length, conn->cid);
1300
1301 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1302 pr_err("Command ITT: 0x%08x received DataOUT after"
1303 " last DataOUT received, dumping payload\n",
1304 cmd->init_task_tag);
1305 return iscsit_dump_data_payload(conn, payload_length, 1);
1306 }
1307
1308 if (cmd->data_direction != DMA_TO_DEVICE) {
1309 pr_err("Command ITT: 0x%08x received DataOUT for a"
1310 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001311 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001312 }
1313 se_cmd = &cmd->se_cmd;
1314 iscsit_mod_dataout_timer(cmd);
1315
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001316 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001317 pr_err("DataOut Offset: %u, Length %u greater than"
1318 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001319 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001320 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001321 }
1322
1323 if (cmd->unsolicited_data) {
1324 int dump_unsolicited_data = 0;
1325
1326 if (conn->sess->sess_ops->InitialR2T) {
1327 pr_err("Received unexpected unsolicited data"
1328 " while InitialR2T=Yes, protocol error.\n");
1329 transport_send_check_condition_and_sense(&cmd->se_cmd,
1330 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1331 return -1;
1332 }
1333 /*
1334 * Special case for dealing with Unsolicited DataOUT
1335 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1336 * failures;
1337 */
1338
1339 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001340 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001341 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001342 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001343
1344 if (dump_unsolicited_data) {
1345 /*
1346 * Check if a delayed TASK_ABORTED status needs to
1347 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1348 * received with the unsolicitied data out.
1349 */
1350 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1351 iscsit_stop_dataout_timer(cmd);
1352
1353 transport_check_aborted_status(se_cmd,
1354 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1355 return iscsit_dump_data_payload(conn, payload_length, 1);
1356 }
1357 } else {
1358 /*
1359 * For the normal solicited data path:
1360 *
1361 * Check for a delayed TASK_ABORTED status and dump any
1362 * incoming data out payload if one exists. Also, when the
1363 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1364 * data out sequence, we decrement outstanding_r2ts. Once
1365 * outstanding_r2ts reaches zero, go ahead and send the delayed
1366 * TASK_ABORTED status.
1367 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001368 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001369 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1370 if (--cmd->outstanding_r2ts < 1) {
1371 iscsit_stop_dataout_timer(cmd);
1372 transport_check_aborted_status(
1373 se_cmd, 1);
1374 }
1375
1376 return iscsit_dump_data_payload(conn, payload_length, 1);
1377 }
1378 }
1379 /*
1380 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1381 * within-command recovery checks before receiving the payload.
1382 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001383 rc = iscsit_check_pre_dataout(cmd, buf);
1384 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001385 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001386 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001387 return -1;
1388
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001389 *out_cmd = cmd;
1390 return 0;
1391}
1392EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1393
1394static int
1395iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1396 struct iscsi_data *hdr)
1397{
1398 struct kvec *iov;
1399 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1400 u32 payload_length = ntoh24(hdr->dlength);
1401 int iov_ret, data_crc_failed = 0;
1402
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001403 rx_size += payload_length;
1404 iov = &cmd->iov_data[0];
1405
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001406 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1407 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001408 if (iov_ret < 0)
1409 return -1;
1410
1411 iov_count += iov_ret;
1412
1413 padding = ((-payload_length) & 3);
1414 if (padding != 0) {
1415 iov[iov_count].iov_base = cmd->pad_bytes;
1416 iov[iov_count++].iov_len = padding;
1417 rx_size += padding;
1418 pr_debug("Receiving %u padding bytes.\n", padding);
1419 }
1420
1421 if (conn->conn_ops->DataDigest) {
1422 iov[iov_count].iov_base = &checksum;
1423 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1424 rx_size += ISCSI_CRC_LEN;
1425 }
1426
1427 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1428
1429 iscsit_unmap_iovec(cmd);
1430
1431 if (rx_got != rx_size)
1432 return -1;
1433
1434 if (conn->conn_ops->DataDigest) {
1435 u32 data_crc;
1436
1437 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001438 be32_to_cpu(hdr->offset),
1439 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001440 cmd->pad_bytes);
1441
1442 if (checksum != data_crc) {
1443 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1444 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1445 " does not match computed 0x%08x\n",
1446 hdr->itt, hdr->offset, payload_length,
1447 hdr->datasn, checksum, data_crc);
1448 data_crc_failed = 1;
1449 } else {
1450 pr_debug("Got CRC32C DataDigest 0x%08x for"
1451 " %u bytes of Data Out\n", checksum,
1452 payload_length);
1453 }
1454 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001455
1456 return data_crc_failed;
1457}
1458
1459int
1460iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1461 bool data_crc_failed)
1462{
1463 struct iscsi_conn *conn = cmd->conn;
1464 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001465 /*
1466 * Increment post receive data and CRC values or perform
1467 * within-command recovery.
1468 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001469 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1470 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001471 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001472 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001473 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001474 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1475 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001476 /*
1477 * Handle extra special case for out of order
1478 * Unsolicited Data Out.
1479 */
1480 spin_lock_bh(&cmd->istate_lock);
1481 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1482 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1483 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1484 spin_unlock_bh(&cmd->istate_lock);
1485
1486 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001487 if (ooo_cmdsn)
1488 return 0;
1489 target_execute_cmd(&cmd->se_cmd);
1490 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001491 } else /* DATAOUT_CANNOT_RECOVER */
1492 return -1;
1493
1494 return 0;
1495}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001496EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001497
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001498static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1499{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001500 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001501 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1502 int rc;
1503 bool data_crc_failed = false;
1504
1505 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1506 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001507 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001508 else if (!cmd)
1509 return 0;
1510
1511 rc = iscsit_get_dataout(conn, cmd, hdr);
1512 if (rc < 0)
1513 return rc;
1514 else if (rc > 0)
1515 data_crc_failed = true;
1516
1517 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1518}
1519
Nicholas Bellinger778de362013-06-14 16:07:47 -07001520int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1521 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001522{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001523 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001524
Arshad Hussaina3662602014-03-14 15:28:59 -07001525 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1526 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1527 if (!cmd)
1528 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1529 (unsigned char *)hdr);
1530
1531 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1532 (unsigned char *)hdr);
1533 }
1534
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001535 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001536 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1537 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001538 if (!cmd)
1539 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1540 (unsigned char *)hdr);
1541
Nicholas Bellingerba159912013-07-03 03:48:24 -07001542 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1543 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001544 }
1545
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001546 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001547 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001548 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001549 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001550 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001551 if (!cmd)
1552 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1553 (unsigned char *)hdr);
1554
Nicholas Bellingerba159912013-07-03 03:48:24 -07001555 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1556 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001557 }
1558
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001559 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001560 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001561 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001562 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1563 payload_length);
1564 /*
1565 * This is not a response to a Unsolicited NopIN, which means
1566 * it can either be a NOPOUT ping request (with a valid ITT),
1567 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1568 * Either way, make sure we allocate an struct iscsi_cmd, as both
1569 * can contain ping data.
1570 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001571 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001572 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1573 cmd->i_state = ISTATE_SEND_NOPIN;
1574 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1575 1 : 0);
1576 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1577 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001578 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1579 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001580 cmd->data_direction = DMA_NONE;
1581 }
1582
Nicholas Bellinger778de362013-06-14 16:07:47 -07001583 return 0;
1584}
1585EXPORT_SYMBOL(iscsit_setup_nop_out);
1586
1587int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1588 struct iscsi_nopout *hdr)
1589{
1590 struct iscsi_cmd *cmd_p = NULL;
1591 int cmdsn_ret = 0;
1592 /*
1593 * Initiator is expecting a NopIN ping reply..
1594 */
1595 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001596 if (!cmd)
1597 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1598 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001599
1600 spin_lock_bh(&conn->cmd_lock);
1601 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1602 spin_unlock_bh(&conn->cmd_lock);
1603
1604 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1605
1606 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1607 iscsit_add_cmd_to_response_queue(cmd, conn,
1608 cmd->i_state);
1609 return 0;
1610 }
1611
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001612 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1613 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001614 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1615 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001616 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001617 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001618
1619 return 0;
1620 }
1621 /*
1622 * This was a response to a unsolicited NOPIN ping.
1623 */
1624 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1625 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1626 if (!cmd_p)
1627 return -EINVAL;
1628
1629 iscsit_stop_nopin_response_timer(conn);
1630
1631 cmd_p->i_state = ISTATE_REMOVE;
1632 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1633
1634 iscsit_start_nopin_timer(conn);
1635 return 0;
1636 }
1637 /*
1638 * Otherwise, initiator is not expecting a NOPIN is response.
1639 * Just ignore for now.
1640 */
1641 return 0;
1642}
1643EXPORT_SYMBOL(iscsit_process_nop_out);
1644
1645static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1646 unsigned char *buf)
1647{
1648 unsigned char *ping_data = NULL;
1649 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1650 struct kvec *iov = NULL;
1651 u32 payload_length = ntoh24(hdr->dlength);
1652 int ret;
1653
1654 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1655 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001656 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001657 /*
1658 * Handle NOP-OUT payload for traditional iSCSI sockets
1659 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001660 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001661 u32 checksum, data_crc, padding = 0;
1662 int niov = 0, rx_got, rx_size = payload_length;
1663
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001664 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1665 if (!ping_data) {
1666 pr_err("Unable to allocate memory for"
1667 " NOPOUT ping data.\n");
1668 ret = -1;
1669 goto out;
1670 }
1671
1672 iov = &cmd->iov_misc[0];
1673 iov[niov].iov_base = ping_data;
1674 iov[niov++].iov_len = payload_length;
1675
1676 padding = ((-payload_length) & 3);
1677 if (padding != 0) {
1678 pr_debug("Receiving %u additional bytes"
1679 " for padding.\n", padding);
1680 iov[niov].iov_base = &cmd->pad_bytes;
1681 iov[niov++].iov_len = padding;
1682 rx_size += padding;
1683 }
1684 if (conn->conn_ops->DataDigest) {
1685 iov[niov].iov_base = &checksum;
1686 iov[niov++].iov_len = ISCSI_CRC_LEN;
1687 rx_size += ISCSI_CRC_LEN;
1688 }
1689
1690 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1691 if (rx_got != rx_size) {
1692 ret = -1;
1693 goto out;
1694 }
1695
1696 if (conn->conn_ops->DataDigest) {
1697 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1698 ping_data, payload_length,
1699 padding, cmd->pad_bytes,
1700 (u8 *)&data_crc);
1701
1702 if (checksum != data_crc) {
1703 pr_err("Ping data CRC32C DataDigest"
1704 " 0x%08x does not match computed 0x%08x\n",
1705 checksum, data_crc);
1706 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1707 pr_err("Unable to recover from"
1708 " NOPOUT Ping DataCRC failure while in"
1709 " ERL=0.\n");
1710 ret = -1;
1711 goto out;
1712 } else {
1713 /*
1714 * Silently drop this PDU and let the
1715 * initiator plug the CmdSN gap.
1716 */
1717 pr_debug("Dropping NOPOUT"
1718 " Command CmdSN: 0x%08x due to"
1719 " DataCRC error.\n", hdr->cmdsn);
1720 ret = 0;
1721 goto out;
1722 }
1723 } else {
1724 pr_debug("Got CRC32C DataDigest"
1725 " 0x%08x for %u bytes of ping data.\n",
1726 checksum, payload_length);
1727 }
1728 }
1729
1730 ping_data[payload_length] = '\0';
1731 /*
1732 * Attach ping data to struct iscsi_cmd->buf_ptr.
1733 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001734 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001735 cmd->buf_ptr_size = payload_length;
1736
1737 pr_debug("Got %u bytes of NOPOUT ping"
1738 " data.\n", payload_length);
1739 pr_debug("Ping Data: \"%s\"\n", ping_data);
1740 }
1741
Nicholas Bellinger778de362013-06-14 16:07:47 -07001742 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001743out:
1744 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001745 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001746
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001747 kfree(ping_data);
1748 return ret;
1749}
1750
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001751int
1752iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1753 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001754{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 struct se_tmr_req *se_tmr;
1756 struct iscsi_tmr_req *tmr_req;
1757 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001758 int out_of_order_cmdsn = 0, ret;
1759 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001760 u8 function;
1761
1762 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001763 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1764 function = hdr->flags;
1765
1766 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1767 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1768 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1769 hdr->rtt, hdr->refcmdsn, conn->cid);
1770
1771 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1772 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001773 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001774 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001775 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001776 }
1777
1778 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1779 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1780 pr_err("Task Management Request TASK_REASSIGN not"
1781 " issued as immediate command, bad iSCSI Initiator"
1782 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001783 return iscsit_add_reject_cmd(cmd,
1784 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001785 }
1786 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001787 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1788 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001789
Andy Groverd28b11692012-04-03 15:51:22 -07001790 cmd->data_direction = DMA_NONE;
1791
1792 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1793 if (!cmd->tmr_req) {
1794 pr_err("Unable to allocate memory for"
1795 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001796 return iscsit_add_reject_cmd(cmd,
1797 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1798 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001799 }
1800
1801 /*
1802 * TASK_REASSIGN for ERL=2 / connection stays inside of
1803 * LIO-Target $FABRIC_MOD
1804 */
1805 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1806
1807 u8 tcm_function;
1808 int ret;
1809
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001810 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Andy Groverd28b11692012-04-03 15:51:22 -07001811 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001812 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001813
Bart Van Asscheafc16602015-04-27 13:52:36 +02001814 target_get_sess_cmd(&cmd->se_cmd, true);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001815 sess_ref = true;
1816
Andy Groverd28b11692012-04-03 15:51:22 -07001817 switch (function) {
1818 case ISCSI_TM_FUNC_ABORT_TASK:
1819 tcm_function = TMR_ABORT_TASK;
1820 break;
1821 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1822 tcm_function = TMR_ABORT_TASK_SET;
1823 break;
1824 case ISCSI_TM_FUNC_CLEAR_ACA:
1825 tcm_function = TMR_CLEAR_ACA;
1826 break;
1827 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1828 tcm_function = TMR_CLEAR_TASK_SET;
1829 break;
1830 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1831 tcm_function = TMR_LUN_RESET;
1832 break;
1833 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1834 tcm_function = TMR_TARGET_WARM_RESET;
1835 break;
1836 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1837 tcm_function = TMR_TARGET_COLD_RESET;
1838 break;
1839 default:
1840 pr_err("Unknown iSCSI TMR Function:"
1841 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001842 return iscsit_add_reject_cmd(cmd,
1843 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001844 }
1845
1846 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1847 tcm_function, GFP_KERNEL);
1848 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001849 return iscsit_add_reject_cmd(cmd,
1850 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001851
1852 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1853 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001854
1855 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1856 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1857 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1858 cmd->init_task_tag = hdr->itt;
1859 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001860 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1861 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001862 se_tmr = cmd->se_cmd.se_tmr_req;
1863 tmr_req = cmd->tmr_req;
1864 /*
1865 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1866 */
1867 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001868 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1869 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001870 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001871 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1872 goto attach;
1873 }
1874 }
1875
1876 switch (function) {
1877 case ISCSI_TM_FUNC_ABORT_TASK:
1878 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001879 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001880 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001881 break;
1882 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1883 case ISCSI_TM_FUNC_CLEAR_ACA:
1884 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1885 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1886 break;
1887 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1888 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001889 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1890 goto attach;
1891 }
1892 break;
1893 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1894 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001895 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1896 goto attach;
1897 }
1898 break;
1899 case ISCSI_TM_FUNC_TASK_REASSIGN:
1900 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1901 /*
1902 * Perform sanity checks on the ExpDataSN only if the
1903 * TASK_REASSIGN was successful.
1904 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001905 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001906 break;
1907
1908 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001909 return iscsit_add_reject_cmd(cmd,
1910 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001911 break;
1912 default:
1913 pr_err("Unknown TMR function: 0x%02x, protocol"
1914 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001915 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1916 goto attach;
1917 }
1918
1919 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1920 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1921 se_tmr->call_transport = 1;
1922attach:
1923 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001924 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001925 spin_unlock_bh(&conn->cmd_lock);
1926
1927 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001928 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001929 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1930 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001931 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001932 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001933 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001934 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001935 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001936 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001937
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001938 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001939 return 0;
1940 /*
1941 * Found the referenced task, send to transport for processing.
1942 */
1943 if (se_tmr->call_transport)
1944 return transport_generic_handle_tmr(&cmd->se_cmd);
1945
1946 /*
1947 * Could not find the referenced LUN, task, or Task Management
1948 * command not authorized or supported. Change state and
1949 * let the tx_thread send the response.
1950 *
1951 * For connection recovery, this is also the default action for
1952 * TMR TASK_REASSIGN.
1953 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001954 if (sess_ref) {
1955 pr_debug("Handle TMR, using sess_ref=true check\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +02001956 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001957 }
1958
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001959 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1960 return 0;
1961}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001962EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001963
1964/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001965int
1966iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1967 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001968{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001969 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001970
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001971 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001972 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001973 "greater than MaxXmitDataSegmentLength %u.\n",
1974 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001975 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1976 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001977 }
1978
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08001979 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1980 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1981 pr_err("Multi sequence text commands currently not supported\n");
1982 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1983 (unsigned char *)hdr);
1984 }
1985
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001986 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1987 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1988 hdr->exp_statsn, payload_length);
1989
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001990 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1991 cmd->i_state = ISTATE_SEND_TEXTRSP;
1992 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1993 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1994 cmd->targ_xfer_tag = 0xFFFFFFFF;
1995 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1996 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1997 cmd->data_direction = DMA_NONE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001998 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001999
2000 return 0;
2001}
2002EXPORT_SYMBOL(iscsit_setup_text_cmd);
2003
2004int
2005iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2006 struct iscsi_text *hdr)
2007{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002008 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002009 int cmdsn_ret;
2010
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002011 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002012 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2013 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2014 pr_err("Unable to locate text_in buffer for sendtargets"
2015 " discovery\n");
2016 goto reject;
2017 }
2018 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002019 }
2020 if (strncmp("SendTargets", text_in, 11) != 0) {
2021 pr_err("Received Text Data that is not"
2022 " SendTargets, cannot continue.\n");
2023 goto reject;
2024 }
2025 text_ptr = strchr(text_in, '=');
2026 if (!text_ptr) {
2027 pr_err("No \"=\" separator found in Text Data,"
2028 " cannot continue.\n");
2029 goto reject;
2030 }
2031 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002032 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002033 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2034 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002035 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002036 } else {
2037 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2038 goto reject;
2039 }
2040
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002041 spin_lock_bh(&conn->cmd_lock);
2042 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2043 spin_unlock_bh(&conn->cmd_lock);
2044
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002045empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002046 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2047
2048 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002049 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2050 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002051 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002052 return -1;
2053
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002054 return 0;
2055 }
2056
2057 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002058
2059reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002060 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2061 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002062}
2063EXPORT_SYMBOL(iscsit_process_text_cmd);
2064
2065static int
2066iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2067 unsigned char *buf)
2068{
2069 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2070 char *text_in = NULL;
2071 u32 payload_length = ntoh24(hdr->dlength);
2072 int rx_size, rc;
2073
2074 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2075 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002076 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002077
2078 rx_size = payload_length;
2079 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002080 u32 checksum = 0, data_crc = 0;
2081 u32 padding = 0, pad_bytes = 0;
2082 int niov = 0, rx_got;
2083 struct kvec iov[3];
2084
2085 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002086 if (!text_in) {
2087 pr_err("Unable to allocate memory for"
2088 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002089 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002090 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002091 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002092
2093 memset(iov, 0, 3 * sizeof(struct kvec));
2094 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002095 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002096
2097 padding = ((-payload_length) & 3);
2098 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002099 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002100 iov[niov++].iov_len = padding;
2101 rx_size += padding;
2102 pr_debug("Receiving %u additional bytes"
2103 " for padding.\n", padding);
2104 }
2105 if (conn->conn_ops->DataDigest) {
2106 iov[niov].iov_base = &checksum;
2107 iov[niov++].iov_len = ISCSI_CRC_LEN;
2108 rx_size += ISCSI_CRC_LEN;
2109 }
2110
2111 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002112 if (rx_got != rx_size)
2113 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002114
2115 if (conn->conn_ops->DataDigest) {
2116 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002117 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002118 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002119 (u8 *)&data_crc);
2120
2121 if (checksum != data_crc) {
2122 pr_err("Text data CRC32C DataDigest"
2123 " 0x%08x does not match computed"
2124 " 0x%08x\n", checksum, data_crc);
2125 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2126 pr_err("Unable to recover from"
2127 " Text Data digest failure while in"
2128 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002129 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002130 } else {
2131 /*
2132 * Silently drop this PDU and let the
2133 * initiator plug the CmdSN gap.
2134 */
2135 pr_debug("Dropping Text"
2136 " Command CmdSN: 0x%08x due to"
2137 " DataCRC error.\n", hdr->cmdsn);
2138 kfree(text_in);
2139 return 0;
2140 }
2141 } else {
2142 pr_debug("Got CRC32C DataDigest"
2143 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002144 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002145 }
2146 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002147 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002148 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002149 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002150 }
2151
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002152 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002153
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002154reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002155 kfree(cmd->text_in_ptr);
2156 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002157 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002158}
2159
2160int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2161{
2162 struct iscsi_conn *conn_p;
2163 struct iscsi_session *sess = conn->sess;
2164
2165 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2166 " for SID: %u.\n", conn->cid, conn->sess->sid);
2167
2168 atomic_set(&sess->session_logout, 1);
2169 atomic_set(&conn->conn_logout_remove, 1);
2170 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2171
2172 iscsit_inc_conn_usage_count(conn);
2173 iscsit_inc_session_usage_count(sess);
2174
2175 spin_lock_bh(&sess->conn_lock);
2176 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2177 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2178 continue;
2179
2180 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2181 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2182 }
2183 spin_unlock_bh(&sess->conn_lock);
2184
2185 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2186
2187 return 0;
2188}
2189
2190int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2191{
2192 struct iscsi_conn *l_conn;
2193 struct iscsi_session *sess = conn->sess;
2194
2195 pr_debug("Received logout request CLOSECONNECTION for CID:"
2196 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2197
2198 /*
2199 * A Logout Request with a CLOSECONNECTION reason code for a CID
2200 * can arrive on a connection with a differing CID.
2201 */
2202 if (conn->cid == cmd->logout_cid) {
2203 spin_lock_bh(&conn->state_lock);
2204 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2205 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2206
2207 atomic_set(&conn->conn_logout_remove, 1);
2208 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2209 iscsit_inc_conn_usage_count(conn);
2210
2211 spin_unlock_bh(&conn->state_lock);
2212 } else {
2213 /*
2214 * Handle all different cid CLOSECONNECTION requests in
2215 * iscsit_logout_post_handler_diffcid() as to give enough
2216 * time for any non immediate command's CmdSN to be
2217 * acknowledged on the connection in question.
2218 *
2219 * Here we simply make sure the CID is still around.
2220 */
2221 l_conn = iscsit_get_conn_from_cid(sess,
2222 cmd->logout_cid);
2223 if (!l_conn) {
2224 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2225 iscsit_add_cmd_to_response_queue(cmd, conn,
2226 cmd->i_state);
2227 return 0;
2228 }
2229
2230 iscsit_dec_conn_usage_count(l_conn);
2231 }
2232
2233 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2234
2235 return 0;
2236}
2237
2238int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2239{
2240 struct iscsi_session *sess = conn->sess;
2241
2242 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2243 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2244
2245 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2246 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2247 " while ERL!=2.\n");
2248 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2249 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2250 return 0;
2251 }
2252
2253 if (conn->cid == cmd->logout_cid) {
2254 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2255 " with CID: %hu on CID: %hu, implementation error.\n",
2256 cmd->logout_cid, conn->cid);
2257 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2258 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2259 return 0;
2260 }
2261
2262 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2263
2264 return 0;
2265}
2266
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002267int
2268iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2269 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002270{
2271 int cmdsn_ret, logout_remove = 0;
2272 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002273 struct iscsi_logout *hdr;
2274 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2275
2276 hdr = (struct iscsi_logout *) buf;
2277 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002278
2279 if (tiqn) {
2280 spin_lock(&tiqn->logout_stats.lock);
2281 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2282 tiqn->logout_stats.normal_logouts++;
2283 else
2284 tiqn->logout_stats.abnormal_logouts++;
2285 spin_unlock(&tiqn->logout_stats.lock);
2286 }
2287
2288 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2289 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2290 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2291 hdr->cid, conn->cid);
2292
2293 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2294 pr_err("Received logout request on connection that"
2295 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002296 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002297 return 0;
2298 }
2299
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002300 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2301 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2302 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2303 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2304 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002305 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2306 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2307 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002308 cmd->logout_reason = reason_code;
2309 cmd->data_direction = DMA_NONE;
2310
2311 /*
2312 * We need to sleep in these cases (by returning 1) until the Logout
2313 * Response gets sent in the tx thread.
2314 */
2315 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2316 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002317 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002318 logout_remove = 1;
2319
2320 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002321 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002322 spin_unlock_bh(&conn->cmd_lock);
2323
2324 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002325 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002326
2327 /*
2328 * Immediate commands are executed, well, immediately.
2329 * Non-Immediate Logout Commands are executed in CmdSN order.
2330 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002331 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002332 int ret = iscsit_execute_cmd(cmd, 0);
2333
2334 if (ret < 0)
2335 return ret;
2336 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002337 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002338 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002339 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002340 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2341 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002342 }
2343
2344 return logout_remove;
2345}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002346EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002347
2348static int iscsit_handle_snack(
2349 struct iscsi_conn *conn,
2350 unsigned char *buf)
2351{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002352 struct iscsi_snack *hdr;
2353
2354 hdr = (struct iscsi_snack *) buf;
2355 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002356
2357 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2358 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2359 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2360 hdr->begrun, hdr->runlength, conn->cid);
2361
2362 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2363 pr_err("Initiator sent SNACK request while in"
2364 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002365 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2366 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002367 }
2368 /*
2369 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2370 * call from inside iscsi_send_recovery_datain_or_r2t().
2371 */
2372 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2373 case 0:
2374 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002375 hdr->itt,
2376 be32_to_cpu(hdr->ttt),
2377 be32_to_cpu(hdr->begrun),
2378 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002379 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002380 return iscsit_handle_status_snack(conn, hdr->itt,
2381 be32_to_cpu(hdr->ttt),
2382 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002383 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002384 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2385 be32_to_cpu(hdr->begrun),
2386 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002387 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2388 /* FIXME: Support R-Data SNACK */
2389 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002390 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2391 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002392 default:
2393 pr_err("Unknown SNACK type 0x%02x, protocol"
2394 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002395 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2396 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002397 }
2398
2399 return 0;
2400}
2401
2402static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2403{
2404 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2405 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2406 wait_for_completion_interruptible_timeout(
2407 &conn->rx_half_close_comp,
2408 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2409 }
2410}
2411
2412static int iscsit_handle_immediate_data(
2413 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002414 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002415 u32 length)
2416{
2417 int iov_ret, rx_got = 0, rx_size = 0;
2418 u32 checksum, iov_count = 0, padding = 0;
2419 struct iscsi_conn *conn = cmd->conn;
2420 struct kvec *iov;
2421
2422 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2423 if (iov_ret < 0)
2424 return IMMEDIATE_DATA_CANNOT_RECOVER;
2425
2426 rx_size = length;
2427 iov_count = iov_ret;
2428 iov = &cmd->iov_data[0];
2429
2430 padding = ((-length) & 3);
2431 if (padding != 0) {
2432 iov[iov_count].iov_base = cmd->pad_bytes;
2433 iov[iov_count++].iov_len = padding;
2434 rx_size += padding;
2435 }
2436
2437 if (conn->conn_ops->DataDigest) {
2438 iov[iov_count].iov_base = &checksum;
2439 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2440 rx_size += ISCSI_CRC_LEN;
2441 }
2442
2443 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2444
2445 iscsit_unmap_iovec(cmd);
2446
2447 if (rx_got != rx_size) {
2448 iscsit_rx_thread_wait_for_tcp(conn);
2449 return IMMEDIATE_DATA_CANNOT_RECOVER;
2450 }
2451
2452 if (conn->conn_ops->DataDigest) {
2453 u32 data_crc;
2454
2455 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2456 cmd->write_data_done, length, padding,
2457 cmd->pad_bytes);
2458
2459 if (checksum != data_crc) {
2460 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2461 " does not match computed 0x%08x\n", checksum,
2462 data_crc);
2463
2464 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2465 pr_err("Unable to recover from"
2466 " Immediate Data digest failure while"
2467 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002468 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002469 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002470 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002471 return IMMEDIATE_DATA_CANNOT_RECOVER;
2472 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002473 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002474 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002475 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002476 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2477 }
2478 } else {
2479 pr_debug("Got CRC32C DataDigest 0x%08x for"
2480 " %u bytes of Immediate Data\n", checksum,
2481 length);
2482 }
2483 }
2484
2485 cmd->write_data_done += length;
2486
Andy Groverebf1d952012-04-03 15:51:24 -07002487 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002488 spin_lock_bh(&cmd->istate_lock);
2489 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2490 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2491 spin_unlock_bh(&cmd->istate_lock);
2492 }
2493
2494 return IMMEDIATE_DATA_NORMAL_OPERATION;
2495}
2496
2497/*
2498 * Called with sess->conn_lock held.
2499 */
2500/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2501 with active network interface */
2502static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2503{
2504 struct iscsi_cmd *cmd;
2505 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002506 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002507
2508 /*
2509 * Only send a Asynchronous Message on connections whos network
2510 * interface is still functional.
2511 */
2512 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2513 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2514 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002515 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002516 break;
2517 }
2518 }
2519
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002520 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002521 return;
2522
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002523 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002524 if (!cmd) {
2525 iscsit_dec_conn_usage_count(conn_p);
2526 return;
2527 }
2528
2529 cmd->logout_cid = conn->cid;
2530 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2531 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2532
2533 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002534 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002535 spin_unlock_bh(&conn_p->cmd_lock);
2536
2537 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2538 iscsit_dec_conn_usage_count(conn_p);
2539}
2540
2541static int iscsit_send_conn_drop_async_message(
2542 struct iscsi_cmd *cmd,
2543 struct iscsi_conn *conn)
2544{
2545 struct iscsi_async *hdr;
2546
2547 cmd->tx_size = ISCSI_HDR_LEN;
2548 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2549
2550 hdr = (struct iscsi_async *) cmd->pdu;
2551 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2552 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002553 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002554 cmd->targ_xfer_tag = 0xFFFFFFFF;
2555 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2556 cmd->stat_sn = conn->stat_sn++;
2557 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2558 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2559 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2560 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2561 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2562 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2563 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2564
2565 if (conn->conn_ops->HeaderDigest) {
2566 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2567
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002568 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2569 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002570
2571 cmd->tx_size += ISCSI_CRC_LEN;
2572 pr_debug("Attaching CRC32C HeaderDigest to"
2573 " Async Message 0x%08x\n", *header_digest);
2574 }
2575
2576 cmd->iov_misc[0].iov_base = cmd->pdu;
2577 cmd->iov_misc[0].iov_len = cmd->tx_size;
2578 cmd->iov_misc_count = 1;
2579
2580 pr_debug("Sending Connection Dropped Async Message StatSN:"
2581 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2582 cmd->logout_cid, conn->cid);
2583 return 0;
2584}
2585
Andy Grover6f3c0e62012-04-03 15:51:09 -07002586static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2587{
2588 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2589 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2590 wait_for_completion_interruptible_timeout(
2591 &conn->tx_half_close_comp,
2592 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2593 }
2594}
2595
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002596static void
2597iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2598 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2599 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002600{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002601 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2602 hdr->flags = datain->flags;
2603 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2604 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2605 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2606 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2607 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2608 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2609 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2610 }
2611 }
2612 hton24(hdr->dlength, datain->length);
2613 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2614 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2615 (struct scsi_lun *)&hdr->lun);
2616 else
2617 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2618
2619 hdr->itt = cmd->init_task_tag;
2620
2621 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2622 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2623 else
2624 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2625 if (set_statsn)
2626 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2627 else
2628 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2629
2630 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2631 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2632 hdr->datasn = cpu_to_be32(datain->data_sn);
2633 hdr->offset = cpu_to_be32(datain->offset);
2634
2635 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2636 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2637 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2638 ntohl(hdr->offset), datain->length, conn->cid);
2639}
2640
2641static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2642{
2643 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002644 struct iscsi_datain datain;
2645 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002646 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002647 u32 iov_count = 0, tx_size = 0;
2648 int eodr = 0, ret, iov_ret;
2649 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002650
2651 memset(&datain, 0, sizeof(struct iscsi_datain));
2652 dr = iscsit_get_datain_values(cmd, &datain);
2653 if (!dr) {
2654 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2655 cmd->init_task_tag);
2656 return -1;
2657 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002658 /*
2659 * Be paranoid and double check the logic for now.
2660 */
Andy Groverebf1d952012-04-03 15:51:24 -07002661 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002662 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2663 " datain.length: %u exceeds cmd->data_length: %u\n",
2664 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002665 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002666 return -1;
2667 }
2668
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002669 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002670 /*
2671 * Special case for successfully execution w/ both DATAIN
2672 * and Sense Data.
2673 */
2674 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2675 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2676 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2677 else {
2678 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2679 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2680 iscsit_increment_maxcmdsn(cmd, conn->sess);
2681 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002682 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002683 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002684 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2685 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002686 }
2687
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002688 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002689
2690 iov = &cmd->iov_data[0];
2691 iov[iov_count].iov_base = cmd->pdu;
2692 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2693 tx_size += ISCSI_HDR_LEN;
2694
2695 if (conn->conn_ops->HeaderDigest) {
2696 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2697
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002698 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2699 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002700
2701 iov[0].iov_len += ISCSI_CRC_LEN;
2702 tx_size += ISCSI_CRC_LEN;
2703
2704 pr_debug("Attaching CRC32 HeaderDigest"
2705 " for DataIN PDU 0x%08x\n", *header_digest);
2706 }
2707
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002708 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2709 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002710 if (iov_ret < 0)
2711 return -1;
2712
2713 iov_count += iov_ret;
2714 tx_size += datain.length;
2715
2716 cmd->padding = ((-datain.length) & 3);
2717 if (cmd->padding) {
2718 iov[iov_count].iov_base = cmd->pad_bytes;
2719 iov[iov_count++].iov_len = cmd->padding;
2720 tx_size += cmd->padding;
2721
2722 pr_debug("Attaching %u padding bytes\n",
2723 cmd->padding);
2724 }
2725 if (conn->conn_ops->DataDigest) {
2726 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2727 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2728
2729 iov[iov_count].iov_base = &cmd->data_crc;
2730 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2731 tx_size += ISCSI_CRC_LEN;
2732
2733 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2734 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2735 }
2736
2737 cmd->iov_data_count = iov_count;
2738 cmd->tx_size = tx_size;
2739
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02002740 ret = iscsit_fe_sendpage_sg(cmd, conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07002741
2742 iscsit_unmap_iovec(cmd);
2743
2744 if (ret < 0) {
2745 iscsit_tx_thread_wait_for_tcp(conn);
2746 return ret;
2747 }
2748
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002749 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002750 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002751 2 : 1;
2752 iscsit_free_datain_req(cmd, dr);
2753 }
2754
Andy Grover6f3c0e62012-04-03 15:51:09 -07002755 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002756}
2757
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002758int
2759iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2760 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002761{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002762 struct iscsi_conn *logout_conn = NULL;
2763 struct iscsi_conn_recovery *cr = NULL;
2764 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002765 /*
2766 * The actual shutting down of Sessions and/or Connections
2767 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2768 * is done in scsi_logout_post_handler().
2769 */
2770 switch (cmd->logout_reason) {
2771 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2772 pr_debug("iSCSI session logout successful, setting"
2773 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2774 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2775 break;
2776 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2777 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2778 break;
2779 /*
2780 * For CLOSECONNECTION logout requests carrying
2781 * a matching logout CID -> local CID, the reference
2782 * for the local CID will have been incremented in
2783 * iscsi_logout_closeconnection().
2784 *
2785 * For CLOSECONNECTION logout requests carrying
2786 * a different CID than the connection it arrived
2787 * on, the connection responding to cmd->logout_cid
2788 * is stopped in iscsit_logout_post_handler_diffcid().
2789 */
2790
2791 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2792 " successful.\n", cmd->logout_cid, conn->cid);
2793 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2794 break;
2795 case ISCSI_LOGOUT_REASON_RECOVERY:
2796 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2797 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2798 break;
2799 /*
2800 * If the connection is still active from our point of view
2801 * force connection recovery to occur.
2802 */
2803 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2804 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002805 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002806 iscsit_connection_reinstatement_rcfr(logout_conn);
2807 iscsit_dec_conn_usage_count(logout_conn);
2808 }
2809
2810 cr = iscsit_get_inactive_connection_recovery_entry(
2811 conn->sess, cmd->logout_cid);
2812 if (!cr) {
2813 pr_err("Unable to locate CID: %hu for"
2814 " REMOVECONNFORRECOVERY Logout Request.\n",
2815 cmd->logout_cid);
2816 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2817 break;
2818 }
2819
2820 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2821
2822 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2823 " for recovery for CID: %hu on CID: %hu successful.\n",
2824 cmd->logout_cid, conn->cid);
2825 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2826 break;
2827 default:
2828 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2829 cmd->logout_reason);
2830 return -1;
2831 }
2832
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002833 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2834 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2835 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002836 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002837 cmd->stat_sn = conn->stat_sn++;
2838 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2839
2840 iscsit_increment_maxcmdsn(cmd, conn->sess);
2841 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2842 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2843
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002844 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2845 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2846 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2847 cmd->logout_cid, conn->cid);
2848
2849 return 0;
2850}
2851EXPORT_SYMBOL(iscsit_build_logout_rsp);
2852
2853static int
2854iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2855{
2856 struct kvec *iov;
2857 int niov = 0, tx_size, rc;
2858
2859 rc = iscsit_build_logout_rsp(cmd, conn,
2860 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2861 if (rc < 0)
2862 return rc;
2863
2864 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002865 iov = &cmd->iov_misc[0];
2866 iov[niov].iov_base = cmd->pdu;
2867 iov[niov++].iov_len = ISCSI_HDR_LEN;
2868
2869 if (conn->conn_ops->HeaderDigest) {
2870 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2871
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002872 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2873 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002874
2875 iov[0].iov_len += ISCSI_CRC_LEN;
2876 tx_size += ISCSI_CRC_LEN;
2877 pr_debug("Attaching CRC32C HeaderDigest to"
2878 " Logout Response 0x%08x\n", *header_digest);
2879 }
2880 cmd->iov_misc_count = niov;
2881 cmd->tx_size = tx_size;
2882
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002883 return 0;
2884}
2885
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002886void
2887iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2888 struct iscsi_nopin *hdr, bool nopout_response)
2889{
2890 hdr->opcode = ISCSI_OP_NOOP_IN;
2891 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2892 hton24(hdr->dlength, cmd->buf_ptr_size);
2893 if (nopout_response)
2894 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2895 hdr->itt = cmd->init_task_tag;
2896 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2897 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2898 conn->stat_sn;
2899 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2900
2901 if (nopout_response)
2902 iscsit_increment_maxcmdsn(cmd, conn->sess);
2903
2904 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2905 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2906
2907 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2908 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2909 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2910 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2911}
2912EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2913
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002914/*
2915 * Unsolicited NOPIN, either requesting a response or not.
2916 */
2917static int iscsit_send_unsolicited_nopin(
2918 struct iscsi_cmd *cmd,
2919 struct iscsi_conn *conn,
2920 int want_response)
2921{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002922 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2923 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002924
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002925 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002926
2927 if (conn->conn_ops->HeaderDigest) {
2928 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2929
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002930 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2931 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002932
2933 tx_size += ISCSI_CRC_LEN;
2934 pr_debug("Attaching CRC32C HeaderDigest to"
2935 " NopIN 0x%08x\n", *header_digest);
2936 }
2937
2938 cmd->iov_misc[0].iov_base = cmd->pdu;
2939 cmd->iov_misc[0].iov_len = tx_size;
2940 cmd->iov_misc_count = 1;
2941 cmd->tx_size = tx_size;
2942
2943 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2944 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2945
Andy Grover6f3c0e62012-04-03 15:51:09 -07002946 ret = iscsit_send_tx_data(cmd, conn, 1);
2947 if (ret < 0) {
2948 iscsit_tx_thread_wait_for_tcp(conn);
2949 return ret;
2950 }
2951
2952 spin_lock_bh(&cmd->istate_lock);
2953 cmd->i_state = want_response ?
2954 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2955 spin_unlock_bh(&cmd->istate_lock);
2956
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002957 return 0;
2958}
2959
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002960static int
2961iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002962{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002963 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002964 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002965 u32 padding = 0;
2966 int niov = 0, tx_size;
2967
2968 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002969
2970 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002971 iov = &cmd->iov_misc[0];
2972 iov[niov].iov_base = cmd->pdu;
2973 iov[niov++].iov_len = ISCSI_HDR_LEN;
2974
2975 if (conn->conn_ops->HeaderDigest) {
2976 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2977
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002978 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2979 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002980
2981 iov[0].iov_len += ISCSI_CRC_LEN;
2982 tx_size += ISCSI_CRC_LEN;
2983 pr_debug("Attaching CRC32C HeaderDigest"
2984 " to NopIn 0x%08x\n", *header_digest);
2985 }
2986
2987 /*
2988 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2989 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2990 */
2991 if (cmd->buf_ptr_size) {
2992 iov[niov].iov_base = cmd->buf_ptr;
2993 iov[niov++].iov_len = cmd->buf_ptr_size;
2994 tx_size += cmd->buf_ptr_size;
2995
2996 pr_debug("Echoing back %u bytes of ping"
2997 " data.\n", cmd->buf_ptr_size);
2998
2999 padding = ((-cmd->buf_ptr_size) & 3);
3000 if (padding != 0) {
3001 iov[niov].iov_base = &cmd->pad_bytes;
3002 iov[niov++].iov_len = padding;
3003 tx_size += padding;
3004 pr_debug("Attaching %u additional"
3005 " padding bytes.\n", padding);
3006 }
3007 if (conn->conn_ops->DataDigest) {
3008 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3009 cmd->buf_ptr, cmd->buf_ptr_size,
3010 padding, (u8 *)&cmd->pad_bytes,
3011 (u8 *)&cmd->data_crc);
3012
3013 iov[niov].iov_base = &cmd->data_crc;
3014 iov[niov++].iov_len = ISCSI_CRC_LEN;
3015 tx_size += ISCSI_CRC_LEN;
3016 pr_debug("Attached DataDigest for %u"
3017 " bytes of ping data, CRC 0x%08x\n",
3018 cmd->buf_ptr_size, cmd->data_crc);
3019 }
3020 }
3021
3022 cmd->iov_misc_count = niov;
3023 cmd->tx_size = tx_size;
3024
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003025 return 0;
3026}
3027
Andy Grover6f3c0e62012-04-03 15:51:09 -07003028static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003029 struct iscsi_cmd *cmd,
3030 struct iscsi_conn *conn)
3031{
3032 int tx_size = 0;
3033 struct iscsi_r2t *r2t;
3034 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003035 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003036
3037 r2t = iscsit_get_r2t_from_list(cmd);
3038 if (!r2t)
3039 return -1;
3040
3041 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3042 memset(hdr, 0, ISCSI_HDR_LEN);
3043 hdr->opcode = ISCSI_OP_R2T;
3044 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3045 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3046 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003047 hdr->itt = cmd->init_task_tag;
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02003048 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003049 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3050 hdr->statsn = cpu_to_be32(conn->stat_sn);
3051 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3052 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3053 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3054 hdr->data_offset = cpu_to_be32(r2t->offset);
3055 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3056
3057 cmd->iov_misc[0].iov_base = cmd->pdu;
3058 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3059 tx_size += ISCSI_HDR_LEN;
3060
3061 if (conn->conn_ops->HeaderDigest) {
3062 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3063
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003064 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3065 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003066
3067 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3068 tx_size += ISCSI_CRC_LEN;
3069 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3070 " PDU 0x%08x\n", *header_digest);
3071 }
3072
3073 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3074 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3075 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3076 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3077 r2t->offset, r2t->xfer_len, conn->cid);
3078
3079 cmd->iov_misc_count = 1;
3080 cmd->tx_size = tx_size;
3081
3082 spin_lock_bh(&cmd->r2t_lock);
3083 r2t->sent_r2t = 1;
3084 spin_unlock_bh(&cmd->r2t_lock);
3085
Andy Grover6f3c0e62012-04-03 15:51:09 -07003086 ret = iscsit_send_tx_data(cmd, conn, 1);
3087 if (ret < 0) {
3088 iscsit_tx_thread_wait_for_tcp(conn);
3089 return ret;
3090 }
3091
3092 spin_lock_bh(&cmd->dataout_timeout_lock);
3093 iscsit_start_dataout_timer(cmd, conn);
3094 spin_unlock_bh(&cmd->dataout_timeout_lock);
3095
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003096 return 0;
3097}
3098
3099/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003100 * @recovery: If called from iscsi_task_reassign_complete_write() for
3101 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003102 */
3103int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003104 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003105 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003106 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003107{
3108 int first_r2t = 1;
3109 u32 offset = 0, xfer_len = 0;
3110
3111 spin_lock_bh(&cmd->r2t_lock);
3112 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3113 spin_unlock_bh(&cmd->r2t_lock);
3114 return 0;
3115 }
3116
Andy Grover8b1e1242012-04-03 15:51:12 -07003117 if (conn->sess->sess_ops->DataSequenceInOrder &&
3118 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003119 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003120
3121 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3122 if (conn->sess->sess_ops->DataSequenceInOrder) {
3123 offset = cmd->r2t_offset;
3124
Andy Grover8b1e1242012-04-03 15:51:12 -07003125 if (first_r2t && recovery) {
3126 int new_data_end = offset +
3127 conn->sess->sess_ops->MaxBurstLength -
3128 cmd->next_burst_len;
3129
Andy Groverebf1d952012-04-03 15:51:24 -07003130 if (new_data_end > cmd->se_cmd.data_length)
3131 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003132 else
3133 xfer_len =
3134 conn->sess->sess_ops->MaxBurstLength -
3135 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003136 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003137 int new_data_end = offset +
3138 conn->sess->sess_ops->MaxBurstLength;
3139
Andy Groverebf1d952012-04-03 15:51:24 -07003140 if (new_data_end > cmd->se_cmd.data_length)
3141 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003142 else
3143 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003144 }
3145 cmd->r2t_offset += xfer_len;
3146
Andy Groverebf1d952012-04-03 15:51:24 -07003147 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003148 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3149 } else {
3150 struct iscsi_seq *seq;
3151
3152 seq = iscsit_get_seq_holder_for_r2t(cmd);
3153 if (!seq) {
3154 spin_unlock_bh(&cmd->r2t_lock);
3155 return -1;
3156 }
3157
3158 offset = seq->offset;
3159 xfer_len = seq->xfer_len;
3160
3161 if (cmd->seq_send_order == cmd->seq_count)
3162 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3163 }
3164 cmd->outstanding_r2ts++;
3165 first_r2t = 0;
3166
3167 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3168 spin_unlock_bh(&cmd->r2t_lock);
3169 return -1;
3170 }
3171
3172 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3173 break;
3174 }
3175 spin_unlock_bh(&cmd->r2t_lock);
3176
3177 return 0;
3178}
3179
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003180void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3181 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003182{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003183 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003184 cmd->stat_sn = conn->stat_sn++;
3185
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003186 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003187
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003188 memset(hdr, 0, ISCSI_HDR_LEN);
3189 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3190 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3191 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3192 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003193 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003194 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3195 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003196 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003197 }
3198 hdr->response = cmd->iscsi_response;
3199 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003200 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003201 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3202
3203 iscsit_increment_maxcmdsn(cmd, conn->sess);
3204 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3205 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3206
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003207 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3208 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3209 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3210 cmd->se_cmd.scsi_status, conn->cid);
3211}
3212EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3213
3214static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3215{
3216 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3217 struct kvec *iov;
3218 u32 padding = 0, tx_size = 0;
3219 int iov_count = 0;
3220 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3221
3222 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3223
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003224 iov = &cmd->iov_misc[0];
3225 iov[iov_count].iov_base = cmd->pdu;
3226 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3227 tx_size += ISCSI_HDR_LEN;
3228
3229 /*
3230 * Attach SENSE DATA payload to iSCSI Response PDU
3231 */
3232 if (cmd->se_cmd.sense_buffer &&
3233 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3234 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003235 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3236 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3237
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003238 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003239 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003240 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003241 iov[iov_count++].iov_len =
3242 (cmd->se_cmd.scsi_sense_length + padding);
3243 tx_size += cmd->se_cmd.scsi_sense_length;
3244
3245 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003246 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003247 cmd->se_cmd.scsi_sense_length, 0, padding);
3248 tx_size += padding;
3249 pr_debug("Adding %u bytes of padding to"
3250 " SENSE.\n", padding);
3251 }
3252
3253 if (conn->conn_ops->DataDigest) {
3254 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003255 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003256 (cmd->se_cmd.scsi_sense_length + padding),
3257 0, NULL, (u8 *)&cmd->data_crc);
3258
3259 iov[iov_count].iov_base = &cmd->data_crc;
3260 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3261 tx_size += ISCSI_CRC_LEN;
3262
3263 pr_debug("Attaching CRC32 DataDigest for"
3264 " SENSE, %u bytes CRC 0x%08x\n",
3265 (cmd->se_cmd.scsi_sense_length + padding),
3266 cmd->data_crc);
3267 }
3268
3269 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3270 " Response PDU\n",
3271 cmd->se_cmd.scsi_sense_length);
3272 }
3273
3274 if (conn->conn_ops->HeaderDigest) {
3275 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3276
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003277 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3278 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003279
3280 iov[0].iov_len += ISCSI_CRC_LEN;
3281 tx_size += ISCSI_CRC_LEN;
3282 pr_debug("Attaching CRC32 HeaderDigest for Response"
3283 " PDU 0x%08x\n", *header_digest);
3284 }
3285
3286 cmd->iov_misc_count = iov_count;
3287 cmd->tx_size = tx_size;
3288
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003289 return 0;
3290}
3291
3292static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3293{
3294 switch (se_tmr->response) {
3295 case TMR_FUNCTION_COMPLETE:
3296 return ISCSI_TMF_RSP_COMPLETE;
3297 case TMR_TASK_DOES_NOT_EXIST:
3298 return ISCSI_TMF_RSP_NO_TASK;
3299 case TMR_LUN_DOES_NOT_EXIST:
3300 return ISCSI_TMF_RSP_NO_LUN;
3301 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3302 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003303 case TMR_FUNCTION_REJECTED:
3304 default:
3305 return ISCSI_TMF_RSP_REJECTED;
3306 }
3307}
3308
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003309void
3310iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3311 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003312{
3313 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003314
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003315 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003316 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003317 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003318 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003319 cmd->stat_sn = conn->stat_sn++;
3320 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3321
3322 iscsit_increment_maxcmdsn(cmd, conn->sess);
3323 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3324 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3325
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003326 pr_debug("Built Task Management Response ITT: 0x%08x,"
3327 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3328 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3329}
3330EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3331
3332static int
3333iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3334{
3335 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3336 u32 tx_size = 0;
3337
3338 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3339
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003340 cmd->iov_misc[0].iov_base = cmd->pdu;
3341 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3342 tx_size += ISCSI_HDR_LEN;
3343
3344 if (conn->conn_ops->HeaderDigest) {
3345 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3346
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003347 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3348 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003349
3350 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3351 tx_size += ISCSI_CRC_LEN;
3352 pr_debug("Attaching CRC32 HeaderDigest for Task"
3353 " Mgmt Response PDU 0x%08x\n", *header_digest);
3354 }
3355
3356 cmd->iov_misc_count = 1;
3357 cmd->tx_size = tx_size;
3358
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003359 return 0;
3360}
3361
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003362static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3363{
3364 bool ret = false;
3365
3366 if (np->np_sockaddr.ss_family == AF_INET6) {
3367 const struct sockaddr_in6 sin6 = {
3368 .sin6_addr = IN6ADDR_ANY_INIT };
3369 struct sockaddr_in6 *sock_in6 =
3370 (struct sockaddr_in6 *)&np->np_sockaddr;
3371
3372 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3373 sin6.sin6_addr.s6_addr, 16))
3374 ret = true;
3375 } else {
3376 struct sockaddr_in * sock_in =
3377 (struct sockaddr_in *)&np->np_sockaddr;
3378
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003379 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003380 ret = true;
3381 }
3382
3383 return ret;
3384}
3385
Andy Grover8b1e1242012-04-03 15:51:12 -07003386#define SENDTARGETS_BUF_LIMIT 32768U
3387
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003388static int
3389iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003390 enum iscsit_transport_type network_transport,
3391 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003392{
3393 char *payload = NULL;
3394 struct iscsi_conn *conn = cmd->conn;
3395 struct iscsi_portal_group *tpg;
3396 struct iscsi_tiqn *tiqn;
3397 struct iscsi_tpg_np *tpg_np;
3398 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003399 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003400 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003401 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003402
Sagi Grimbergbe7dcfb2015-01-26 12:49:06 +02003403 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003404 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003405
3406 payload = kzalloc(buffer_len, GFP_KERNEL);
3407 if (!payload) {
3408 pr_err("Unable to allocate memory for sendtargets"
3409 " response.\n");
3410 return -ENOMEM;
3411 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003412 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003413 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003414 * explicit case..
3415 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003416 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003417 text_ptr = strchr(text_in, '=');
3418 if (!text_ptr) {
3419 pr_err("Unable to locate '=' string in text_in:"
3420 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003421 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003422 return -EINVAL;
3423 }
3424 /*
3425 * Skip over '=' character..
3426 */
3427 text_ptr += 1;
3428 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003429
3430 spin_lock(&tiqn_lock);
3431 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003432 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003433 strcmp(tiqn->tiqn, text_ptr)) {
3434 continue;
3435 }
3436
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003437 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003438
3439 spin_lock(&tiqn->tiqn_tpg_lock);
3440 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3441
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003442 /* If demo_mode_discovery=0 and generate_node_acls=0
3443 * (demo mode dislabed) do not return
3444 * TargetName+TargetAddress unless a NodeACL exists.
3445 */
3446
3447 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3448 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3449 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3450 cmd->conn->sess->sess_ops->InitiatorName))) {
3451 continue;
3452 }
3453
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003454 spin_lock(&tpg->tpg_state_lock);
3455 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3456 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3457 spin_unlock(&tpg->tpg_state_lock);
3458 continue;
3459 }
3460 spin_unlock(&tpg->tpg_state_lock);
3461
3462 spin_lock(&tpg->tpg_np_lock);
3463 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3464 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003465 struct iscsi_np *np = tpg_np->tpg_np;
3466 bool inaddr_any = iscsit_check_inaddr_any(np);
Andy Grover1997e622015-03-31 10:43:18 -07003467 char *fmt_str;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003468
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003469 if (np->np_network_transport != network_transport)
3470 continue;
3471
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003472 if (!target_name_printed) {
3473 len = sprintf(buf, "TargetName=%s",
3474 tiqn->tiqn);
3475 len += 1;
3476
3477 if ((len + payload_len) > buffer_len) {
3478 spin_unlock(&tpg->tpg_np_lock);
3479 spin_unlock(&tiqn->tiqn_tpg_lock);
3480 end_of_buf = 1;
3481 goto eob;
3482 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003483
3484 if (skip_bytes && len <= skip_bytes) {
3485 skip_bytes -= len;
3486 } else {
3487 memcpy(payload + payload_len, buf, len);
3488 payload_len += len;
3489 target_name_printed = 1;
3490 if (len > skip_bytes)
3491 skip_bytes = 0;
3492 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003493 }
3494
Andy Grover1997e622015-03-31 10:43:18 -07003495 if (np->np_sockaddr.ss_family == AF_INET6)
3496 fmt_str = "TargetAddress=[%s]:%hu,%hu";
3497 else
3498 fmt_str = "TargetAddress=%s:%hu,%hu";
3499
3500 len = sprintf(buf, fmt_str,
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02003501 inaddr_any ? conn->local_ip : np->np_ip,
Steven Allenf2774f42014-10-15 10:59:21 -07003502 np->np_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003503 tpg->tpgt);
3504 len += 1;
3505
3506 if ((len + payload_len) > buffer_len) {
3507 spin_unlock(&tpg->tpg_np_lock);
3508 spin_unlock(&tiqn->tiqn_tpg_lock);
3509 end_of_buf = 1;
3510 goto eob;
3511 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003512
3513 if (skip_bytes && len <= skip_bytes) {
3514 skip_bytes -= len;
3515 } else {
3516 memcpy(payload + payload_len, buf, len);
3517 payload_len += len;
3518 if (len > skip_bytes)
3519 skip_bytes = 0;
3520 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003521 }
3522 spin_unlock(&tpg->tpg_np_lock);
3523 }
3524 spin_unlock(&tiqn->tiqn_tpg_lock);
3525eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003526 if (end_of_buf) {
3527 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003528 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003529 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003530
Andy Grover8060b8d2015-01-09 15:13:08 -08003531 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003532 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003533 }
3534 spin_unlock(&tiqn_lock);
3535
3536 cmd->buf_ptr = payload;
3537
3538 return payload_len;
3539}
3540
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003541int
3542iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003543 struct iscsi_text_rsp *hdr,
3544 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003545{
3546 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003547 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003548
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003549 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3550 cmd->read_data_done,
3551 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003552 if (text_length < 0)
3553 return text_length;
3554
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003555 if (completed) {
3556 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3557 } else {
3558 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3559 cmd->read_data_done += text_length;
3560 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3561 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3562 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003563 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003564 padding = ((-text_length) & 3);
3565 hton24(hdr->dlength, text_length);
3566 hdr->itt = cmd->init_task_tag;
3567 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3568 cmd->stat_sn = conn->stat_sn++;
3569 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3570
3571 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003572 /*
3573 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3574 * correctly increment MaxCmdSN for each response answering a
3575 * non immediate text request with a valid CmdSN.
3576 */
3577 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003578 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3579 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3580
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003581 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3582 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3583 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3584 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3585 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003586
3587 return text_length + padding;
3588}
3589EXPORT_SYMBOL(iscsit_build_text_rsp);
3590
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003591static int iscsit_send_text_rsp(
3592 struct iscsi_cmd *cmd,
3593 struct iscsi_conn *conn)
3594{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003595 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003596 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003597 u32 tx_size = 0;
3598 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003599
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003600 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003601 if (rc < 0)
3602 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003603
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003604 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003605 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003606 iov[iov_count].iov_base = cmd->pdu;
3607 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3608 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003609 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003610
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003611 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003612
3613 if (conn->conn_ops->HeaderDigest) {
3614 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3615
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003616 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3617 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003618
3619 iov[0].iov_len += ISCSI_CRC_LEN;
3620 tx_size += ISCSI_CRC_LEN;
3621 pr_debug("Attaching CRC32 HeaderDigest for"
3622 " Text Response PDU 0x%08x\n", *header_digest);
3623 }
3624
3625 if (conn->conn_ops->DataDigest) {
3626 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003627 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003628 0, NULL, (u8 *)&cmd->data_crc);
3629
3630 iov[iov_count].iov_base = &cmd->data_crc;
3631 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3632 tx_size += ISCSI_CRC_LEN;
3633
3634 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003635 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003636 cmd->data_crc);
3637 }
3638
3639 cmd->iov_misc_count = iov_count;
3640 cmd->tx_size = tx_size;
3641
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003642 return 0;
3643}
3644
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003645void
3646iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3647 struct iscsi_reject *hdr)
3648{
3649 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003650 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003651 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3652 hton24(hdr->dlength, ISCSI_HDR_LEN);
3653 hdr->ffffffff = cpu_to_be32(0xffffffff);
3654 cmd->stat_sn = conn->stat_sn++;
3655 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3656 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3657 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3658
3659}
3660EXPORT_SYMBOL(iscsit_build_reject);
3661
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003662static int iscsit_send_reject(
3663 struct iscsi_cmd *cmd,
3664 struct iscsi_conn *conn)
3665{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003666 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003667 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003668 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003669
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003670 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003671
3672 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003673 iov[iov_count].iov_base = cmd->pdu;
3674 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3675 iov[iov_count].iov_base = cmd->buf_ptr;
3676 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3677
3678 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3679
3680 if (conn->conn_ops->HeaderDigest) {
3681 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3682
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003683 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3684 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003685
3686 iov[0].iov_len += ISCSI_CRC_LEN;
3687 tx_size += ISCSI_CRC_LEN;
3688 pr_debug("Attaching CRC32 HeaderDigest for"
3689 " REJECT PDU 0x%08x\n", *header_digest);
3690 }
3691
3692 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003693 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3694 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003695
3696 iov[iov_count].iov_base = &cmd->data_crc;
3697 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3698 tx_size += ISCSI_CRC_LEN;
3699 pr_debug("Attaching CRC32 DataDigest for REJECT"
3700 " PDU 0x%08x\n", cmd->data_crc);
3701 }
3702
3703 cmd->iov_misc_count = iov_count;
3704 cmd->tx_size = tx_size;
3705
3706 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3707 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3708
3709 return 0;
3710}
3711
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003712void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3713{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003714 int ord, cpu;
3715 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003716 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3717 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003718 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003719 * Here we use bitmap_id to determine which CPU that this
3720 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003721 * execute upon.
3722 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003723 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003724 for_each_online_cpu(cpu) {
3725 if (ord-- == 0) {
3726 cpumask_set_cpu(cpu, conn->conn_cpumask);
3727 return;
3728 }
3729 }
3730 /*
3731 * This should never be reached..
3732 */
3733 dump_stack();
3734 cpumask_setall(conn->conn_cpumask);
3735}
3736
3737static inline void iscsit_thread_check_cpumask(
3738 struct iscsi_conn *conn,
3739 struct task_struct *p,
3740 int mode)
3741{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003742 /*
3743 * mode == 1 signals iscsi_target_tx_thread() usage.
3744 * mode == 0 signals iscsi_target_rx_thread() usage.
3745 */
3746 if (mode == 1) {
3747 if (!conn->conn_tx_reset_cpumask)
3748 return;
3749 conn->conn_tx_reset_cpumask = 0;
3750 } else {
3751 if (!conn->conn_rx_reset_cpumask)
3752 return;
3753 conn->conn_rx_reset_cpumask = 0;
3754 }
3755 /*
3756 * Update the CPU mask for this single kthread so that
3757 * both TX and RX kthreads are scheduled to run on the
3758 * same CPU.
3759 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003760 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3761}
3762
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003763static int
3764iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003765{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003766 int ret;
3767
3768 switch (state) {
3769 case ISTATE_SEND_R2T:
3770 ret = iscsit_send_r2t(cmd, conn);
3771 if (ret < 0)
3772 goto err;
3773 break;
3774 case ISTATE_REMOVE:
3775 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003776 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003777 spin_unlock_bh(&conn->cmd_lock);
3778
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003779 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003780 break;
3781 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3782 iscsit_mod_nopin_response_timer(conn);
3783 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3784 if (ret < 0)
3785 goto err;
3786 break;
3787 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3788 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3789 if (ret < 0)
3790 goto err;
3791 break;
3792 default:
3793 pr_err("Unknown Opcode: 0x%02x ITT:"
3794 " 0x%08x, i_state: %d on CID: %hu\n",
3795 cmd->iscsi_opcode, cmd->init_task_tag, state,
3796 conn->cid);
3797 goto err;
3798 }
3799
3800 return 0;
3801
3802err:
3803 return -1;
3804}
3805
3806static int
3807iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3808{
3809 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003810 struct iscsi_queue_req *qr;
3811 struct iscsi_cmd *cmd;
3812 u8 state;
3813 int ret;
3814
3815 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3816 atomic_set(&conn->check_immediate_queue, 0);
3817 cmd = qr->cmd;
3818 state = qr->state;
3819 kmem_cache_free(lio_qr_cache, qr);
3820
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003821 ret = t->iscsit_immediate_queue(conn, cmd, state);
3822 if (ret < 0)
3823 return ret;
3824 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003825
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003826 return 0;
3827}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003828
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003829static int
3830iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3831{
3832 int ret;
3833
3834check_rsp_state:
3835 switch (state) {
3836 case ISTATE_SEND_DATAIN:
3837 ret = iscsit_send_datain(cmd, conn);
3838 if (ret < 0)
3839 goto err;
3840 else if (!ret)
3841 /* more drs */
3842 goto check_rsp_state;
3843 else if (ret == 1) {
3844 /* all done */
3845 spin_lock_bh(&cmd->istate_lock);
3846 cmd->i_state = ISTATE_SENT_STATUS;
3847 spin_unlock_bh(&cmd->istate_lock);
3848
3849 if (atomic_read(&conn->check_immediate_queue))
3850 return 1;
3851
3852 return 0;
3853 } else if (ret == 2) {
3854 /* Still must send status,
3855 SCF_TRANSPORT_TASK_SENSE was set */
3856 spin_lock_bh(&cmd->istate_lock);
3857 cmd->i_state = ISTATE_SEND_STATUS;
3858 spin_unlock_bh(&cmd->istate_lock);
3859 state = ISTATE_SEND_STATUS;
3860 goto check_rsp_state;
3861 }
3862
3863 break;
3864 case ISTATE_SEND_STATUS:
3865 case ISTATE_SEND_STATUS_RECOVERY:
3866 ret = iscsit_send_response(cmd, conn);
3867 break;
3868 case ISTATE_SEND_LOGOUTRSP:
3869 ret = iscsit_send_logout(cmd, conn);
3870 break;
3871 case ISTATE_SEND_ASYNCMSG:
3872 ret = iscsit_send_conn_drop_async_message(
3873 cmd, conn);
3874 break;
3875 case ISTATE_SEND_NOPIN:
3876 ret = iscsit_send_nopin(cmd, conn);
3877 break;
3878 case ISTATE_SEND_REJECT:
3879 ret = iscsit_send_reject(cmd, conn);
3880 break;
3881 case ISTATE_SEND_TASKMGTRSP:
3882 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3883 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003884 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003885 ret = iscsit_tmr_post_handler(cmd, conn);
3886 if (ret != 0)
3887 iscsit_fall_back_to_erl0(conn->sess);
3888 break;
3889 case ISTATE_SEND_TEXTRSP:
3890 ret = iscsit_send_text_rsp(cmd, conn);
3891 break;
3892 default:
3893 pr_err("Unknown Opcode: 0x%02x ITT:"
3894 " 0x%08x, i_state: %d on CID: %hu\n",
3895 cmd->iscsi_opcode, cmd->init_task_tag,
3896 state, conn->cid);
3897 goto err;
3898 }
3899 if (ret < 0)
3900 goto err;
3901
3902 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3903 iscsit_tx_thread_wait_for_tcp(conn);
3904 iscsit_unmap_iovec(cmd);
3905 goto err;
3906 }
3907 iscsit_unmap_iovec(cmd);
3908
3909 switch (state) {
3910 case ISTATE_SEND_LOGOUTRSP:
3911 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003912 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003913 /* fall through */
3914 case ISTATE_SEND_STATUS:
3915 case ISTATE_SEND_ASYNCMSG:
3916 case ISTATE_SEND_NOPIN:
3917 case ISTATE_SEND_STATUS_RECOVERY:
3918 case ISTATE_SEND_TEXTRSP:
3919 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003920 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003921 spin_lock_bh(&cmd->istate_lock);
3922 cmd->i_state = ISTATE_SENT_STATUS;
3923 spin_unlock_bh(&cmd->istate_lock);
3924 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003925 default:
3926 pr_err("Unknown Opcode: 0x%02x ITT:"
3927 " 0x%08x, i_state: %d on CID: %hu\n",
3928 cmd->iscsi_opcode, cmd->init_task_tag,
3929 cmd->i_state, conn->cid);
3930 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003931 }
3932
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003933 if (atomic_read(&conn->check_immediate_queue))
3934 return 1;
3935
Andy Grover6f3c0e62012-04-03 15:51:09 -07003936 return 0;
3937
3938err:
3939 return -1;
3940}
3941
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003942static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003943{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003944 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003945 struct iscsi_queue_req *qr;
3946 struct iscsi_cmd *cmd;
3947 u8 state;
3948 int ret;
3949
3950 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3951 cmd = qr->cmd;
3952 state = qr->state;
3953 kmem_cache_free(lio_qr_cache, qr);
3954
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003955 ret = t->iscsit_response_queue(conn, cmd, state);
3956 if (ret == 1 || ret < 0)
3957 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003958 }
3959
3960 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003961}
3962
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003963int iscsi_target_tx_thread(void *arg)
3964{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003965 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003966 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003967 /*
3968 * Allow ourselves to be interrupted by SIGINT so that a
3969 * connection recovery / failure event can be triggered externally.
3970 */
3971 allow_signal(SIGINT);
3972
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003973 while (!kthread_should_stop()) {
3974 /*
3975 * Ensure that both TX and RX per connection kthreads
3976 * are scheduled to run on the same CPU.
3977 */
3978 iscsit_thread_check_cpumask(conn, current, 1);
3979
Roland Dreierd5627ac2012-10-31 09:16:46 -07003980 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003981 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003982
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003983 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003984 goto transport_err;
3985
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003986get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003987 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003988 if (ret < 0)
3989 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003990
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003991 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003992 if (ret == 1)
3993 goto get_immediate;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003994 else if (ret == -ECONNRESET)
3995 goto out;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003996 else if (ret < 0)
3997 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003998 }
3999
4000transport_err:
Nicholas Bellingere5419862015-07-22 23:14:19 -07004001 /*
4002 * Avoid the normal connection failure code-path if this connection
4003 * is still within LOGIN mode, and iscsi_np process context is
4004 * responsible for cleaning up the early connection failure.
4005 */
4006 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
4007 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004008out:
4009 return 0;
4010}
4011
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004012static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
4013{
4014 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
4015 struct iscsi_cmd *cmd;
4016 int ret = 0;
4017
4018 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
4019 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004020 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004021 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004022 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004023
4024 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
4025 break;
4026 case ISCSI_OP_SCSI_DATA_OUT:
4027 ret = iscsit_handle_data_out(conn, buf);
4028 break;
4029 case ISCSI_OP_NOOP_OUT:
4030 cmd = NULL;
4031 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004032 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004033 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004034 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004035 }
4036 ret = iscsit_handle_nop_out(conn, cmd, buf);
4037 break;
4038 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004039 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004040 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004041 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004042
4043 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
4044 break;
4045 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02004046 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
4047 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
4048 if (!cmd)
4049 goto reject;
4050 } else {
4051 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4052 if (!cmd)
4053 goto reject;
4054 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004055
4056 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004057 break;
4058 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004059 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004060 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004061 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004062
4063 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4064 if (ret > 0)
4065 wait_for_completion_timeout(&conn->conn_logout_comp,
4066 SECONDS_FOR_LOGOUT_COMP * HZ);
4067 break;
4068 case ISCSI_OP_SNACK:
4069 ret = iscsit_handle_snack(conn, buf);
4070 break;
4071 default:
4072 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4073 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4074 pr_err("Cannot recover from unknown"
4075 " opcode while ERL=0, closing iSCSI connection.\n");
4076 return -1;
4077 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02004078 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
4079 " closing iSCSI connection.\n");
4080 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004081 break;
4082 }
4083
4084 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004085reject:
4086 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004087}
4088
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004089int iscsi_target_rx_thread(void *arg)
4090{
Nicholas Bellingere5419862015-07-22 23:14:19 -07004091 int ret, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004092 u8 buffer[ISCSI_HDR_LEN], opcode;
4093 u32 checksum = 0, digest = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004094 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004095 struct kvec iov;
4096 /*
4097 * Allow ourselves to be interrupted by SIGINT so that a
4098 * connection recovery / failure event can be triggered externally.
4099 */
4100 allow_signal(SIGINT);
Nicholas Bellingere5419862015-07-22 23:14:19 -07004101 /*
4102 * Wait for iscsi_post_login_handler() to complete before allowing
4103 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4104 */
4105 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4106 if (rc < 0)
4107 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004108
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004109 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4110 struct completion comp;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004111
4112 init_completion(&comp);
4113 rc = wait_for_completion_interruptible(&comp);
4114 if (rc < 0)
4115 goto transport_err;
4116
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004117 goto transport_err;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004118 }
4119
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004120 while (!kthread_should_stop()) {
4121 /*
4122 * Ensure that both TX and RX per connection kthreads
4123 * are scheduled to run on the same CPU.
4124 */
4125 iscsit_thread_check_cpumask(conn, current, 0);
4126
4127 memset(buffer, 0, ISCSI_HDR_LEN);
4128 memset(&iov, 0, sizeof(struct kvec));
4129
4130 iov.iov_base = buffer;
4131 iov.iov_len = ISCSI_HDR_LEN;
4132
4133 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4134 if (ret != ISCSI_HDR_LEN) {
4135 iscsit_rx_thread_wait_for_tcp(conn);
4136 goto transport_err;
4137 }
4138
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004139 if (conn->conn_ops->HeaderDigest) {
4140 iov.iov_base = &digest;
4141 iov.iov_len = ISCSI_CRC_LEN;
4142
4143 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4144 if (ret != ISCSI_CRC_LEN) {
4145 iscsit_rx_thread_wait_for_tcp(conn);
4146 goto transport_err;
4147 }
4148
4149 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4150 buffer, ISCSI_HDR_LEN,
4151 0, NULL, (u8 *)&checksum);
4152
4153 if (digest != checksum) {
4154 pr_err("HeaderDigest CRC32C failed,"
4155 " received 0x%08x, computed 0x%08x\n",
4156 digest, checksum);
4157 /*
4158 * Set the PDU to 0xff so it will intentionally
4159 * hit default in the switch below.
4160 */
4161 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004162 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004163 } else {
4164 pr_debug("Got HeaderDigest CRC32C"
4165 " 0x%08x\n", checksum);
4166 }
4167 }
4168
4169 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4170 goto transport_err;
4171
4172 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4173
4174 if (conn->sess->sess_ops->SessionType &&
4175 ((!(opcode & ISCSI_OP_TEXT)) ||
4176 (!(opcode & ISCSI_OP_LOGOUT)))) {
4177 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4178 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004179 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4180 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004181 goto transport_err;
4182 }
4183
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004184 ret = iscsi_target_rx_opcode(conn, buffer);
4185 if (ret < 0)
4186 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004187 }
4188
4189transport_err:
4190 if (!signal_pending(current))
4191 atomic_set(&conn->transport_failed, 1);
4192 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004193 return 0;
4194}
4195
4196static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4197{
4198 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4199 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004200 /*
4201 * We expect this function to only ever be called from either RX or TX
4202 * thread context via iscsit_close_connection() once the other context
4203 * has been reset -> returned sleeping pre-handler state.
4204 */
4205 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004206 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004207
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004208 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004209 spin_unlock_bh(&conn->cmd_lock);
4210
4211 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004212
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004213 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004214
4215 spin_lock_bh(&conn->cmd_lock);
4216 }
4217 spin_unlock_bh(&conn->cmd_lock);
4218}
4219
4220static void iscsit_stop_timers_for_cmds(
4221 struct iscsi_conn *conn)
4222{
4223 struct iscsi_cmd *cmd;
4224
4225 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004226 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004227 if (cmd->data_direction == DMA_TO_DEVICE)
4228 iscsit_stop_dataout_timer(cmd);
4229 }
4230 spin_unlock_bh(&conn->cmd_lock);
4231}
4232
4233int iscsit_close_connection(
4234 struct iscsi_conn *conn)
4235{
4236 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4237 struct iscsi_session *sess = conn->sess;
4238
4239 pr_debug("Closing iSCSI connection CID %hu on SID:"
4240 " %u\n", conn->cid, sess->sid);
4241 /*
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004242 * Always up conn_logout_comp for the traditional TCP case just in case
4243 * the RX Thread in iscsi_target_rx_opcode() is sleeping and the logout
4244 * response never got sent because the connection failed.
4245 *
4246 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4247 * to signal logout response TX interrupt completion. Go ahead and skip
4248 * this for iser since isert_rx_opcode() does not wait on logout failure,
4249 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004250 */
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004251 if (conn->conn_transport->transport_type == ISCSI_TCP)
4252 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004253
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004254 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4255 if (conn->tx_thread &&
4256 cmpxchg(&conn->tx_thread_active, true, false)) {
4257 send_sig(SIGINT, conn->tx_thread, 1);
4258 kthread_stop(conn->tx_thread);
4259 }
4260 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4261 if (conn->rx_thread &&
4262 cmpxchg(&conn->rx_thread_active, true, false)) {
4263 send_sig(SIGINT, conn->rx_thread, 1);
4264 kthread_stop(conn->rx_thread);
4265 }
4266 }
4267
4268 spin_lock(&iscsit_global->ts_bitmap_lock);
4269 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4270 get_order(1));
4271 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004272
4273 iscsit_stop_timers_for_cmds(conn);
4274 iscsit_stop_nopin_response_timer(conn);
4275 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004276
4277 if (conn->conn_transport->iscsit_wait_conn)
4278 conn->conn_transport->iscsit_wait_conn(conn);
4279
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004280 /*
4281 * During Connection recovery drop unacknowledged out of order
4282 * commands for this connection, and prepare the other commands
4283 * for realligence.
4284 *
4285 * During normal operation clear the out of order commands (but
4286 * do not free the struct iscsi_ooo_cmdsn's) and release all
4287 * struct iscsi_cmds.
4288 */
4289 if (atomic_read(&conn->connection_recovery)) {
4290 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4291 iscsit_prepare_cmds_for_realligance(conn);
4292 } else {
4293 iscsit_clear_ooo_cmdsns_for_conn(conn);
4294 iscsit_release_commands_from_conn(conn);
4295 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004296 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004297
4298 /*
4299 * Handle decrementing session or connection usage count if
4300 * a logout response was not able to be sent because the
4301 * connection failed. Fall back to Session Recovery here.
4302 */
4303 if (atomic_read(&conn->conn_logout_remove)) {
4304 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4305 iscsit_dec_conn_usage_count(conn);
4306 iscsit_dec_session_usage_count(sess);
4307 }
4308 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4309 iscsit_dec_conn_usage_count(conn);
4310
4311 atomic_set(&conn->conn_logout_remove, 0);
4312 atomic_set(&sess->session_reinstatement, 0);
4313 atomic_set(&sess->session_fall_back_to_erl0, 1);
4314 }
4315
4316 spin_lock_bh(&sess->conn_lock);
4317 list_del(&conn->conn_list);
4318
4319 /*
4320 * Attempt to let the Initiator know this connection failed by
4321 * sending an Connection Dropped Async Message on another
4322 * active connection.
4323 */
4324 if (atomic_read(&conn->connection_recovery))
4325 iscsit_build_conn_drop_async_message(conn);
4326
4327 spin_unlock_bh(&sess->conn_lock);
4328
4329 /*
4330 * If connection reinstatement is being performed on this connection,
4331 * up the connection reinstatement semaphore that is being blocked on
4332 * in iscsit_cause_connection_reinstatement().
4333 */
4334 spin_lock_bh(&conn->state_lock);
4335 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4336 spin_unlock_bh(&conn->state_lock);
4337 complete(&conn->conn_wait_comp);
4338 wait_for_completion(&conn->conn_post_wait_comp);
4339 spin_lock_bh(&conn->state_lock);
4340 }
4341
4342 /*
4343 * If connection reinstatement is being performed on this connection
4344 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4345 * connection wait rcfr semaphore that is being blocked on
4346 * an iscsit_connection_reinstatement_rcfr().
4347 */
4348 if (atomic_read(&conn->connection_wait_rcfr)) {
4349 spin_unlock_bh(&conn->state_lock);
4350 complete(&conn->conn_wait_rcfr_comp);
4351 wait_for_completion(&conn->conn_post_wait_comp);
4352 spin_lock_bh(&conn->state_lock);
4353 }
4354 atomic_set(&conn->connection_reinstatement, 1);
4355 spin_unlock_bh(&conn->state_lock);
4356
4357 /*
4358 * If any other processes are accessing this connection pointer we
4359 * must wait until they have completed.
4360 */
4361 iscsit_check_conn_usage_count(conn);
4362
4363 if (conn->conn_rx_hash.tfm)
4364 crypto_free_hash(conn->conn_rx_hash.tfm);
4365 if (conn->conn_tx_hash.tfm)
4366 crypto_free_hash(conn->conn_tx_hash.tfm);
4367
Joern Engelfbecb652014-09-02 17:49:47 -04004368 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004369
4370 kfree(conn->conn_ops);
4371 conn->conn_ops = NULL;
4372
Al Virobf6932f2012-07-21 08:55:18 +01004373 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004374 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004375
4376 if (conn->conn_transport->iscsit_free_conn)
4377 conn->conn_transport->iscsit_free_conn(conn);
4378
4379 iscsit_put_transport(conn->conn_transport);
4380
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004381 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4382 conn->conn_state = TARG_CONN_STATE_FREE;
4383 kfree(conn);
4384
4385 spin_lock_bh(&sess->conn_lock);
4386 atomic_dec(&sess->nconn);
4387 pr_debug("Decremented iSCSI connection count to %hu from node:"
4388 " %s\n", atomic_read(&sess->nconn),
4389 sess->sess_ops->InitiatorName);
4390 /*
4391 * Make sure that if one connection fails in an non ERL=2 iSCSI
4392 * Session that they all fail.
4393 */
4394 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4395 !atomic_read(&sess->session_logout))
4396 atomic_set(&sess->session_fall_back_to_erl0, 1);
4397
4398 /*
4399 * If this was not the last connection in the session, and we are
4400 * performing session reinstatement or falling back to ERL=0, call
4401 * iscsit_stop_session() without sleeping to shutdown the other
4402 * active connections.
4403 */
4404 if (atomic_read(&sess->nconn)) {
4405 if (!atomic_read(&sess->session_reinstatement) &&
4406 !atomic_read(&sess->session_fall_back_to_erl0)) {
4407 spin_unlock_bh(&sess->conn_lock);
4408 return 0;
4409 }
4410 if (!atomic_read(&sess->session_stop_active)) {
4411 atomic_set(&sess->session_stop_active, 1);
4412 spin_unlock_bh(&sess->conn_lock);
4413 iscsit_stop_session(sess, 0, 0);
4414 return 0;
4415 }
4416 spin_unlock_bh(&sess->conn_lock);
4417 return 0;
4418 }
4419
4420 /*
4421 * If this was the last connection in the session and one of the
4422 * following is occurring:
4423 *
4424 * Session Reinstatement is not being performed, and are falling back
4425 * to ERL=0 call iscsit_close_session().
4426 *
4427 * Session Logout was requested. iscsit_close_session() will be called
4428 * elsewhere.
4429 *
4430 * Session Continuation is not being performed, start the Time2Retain
4431 * handler and check if sleep_on_sess_wait_sem is active.
4432 */
4433 if (!atomic_read(&sess->session_reinstatement) &&
4434 atomic_read(&sess->session_fall_back_to_erl0)) {
4435 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004436 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004437
4438 return 0;
4439 } else if (atomic_read(&sess->session_logout)) {
4440 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4441 sess->session_state = TARG_SESS_STATE_FREE;
4442 spin_unlock_bh(&sess->conn_lock);
4443
4444 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4445 complete(&sess->session_wait_comp);
4446
4447 return 0;
4448 } else {
4449 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4450 sess->session_state = TARG_SESS_STATE_FAILED;
4451
4452 if (!atomic_read(&sess->session_continuation)) {
4453 spin_unlock_bh(&sess->conn_lock);
4454 iscsit_start_time2retain_handler(sess);
4455 } else
4456 spin_unlock_bh(&sess->conn_lock);
4457
4458 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4459 complete(&sess->session_wait_comp);
4460
4461 return 0;
4462 }
4463 spin_unlock_bh(&sess->conn_lock);
4464
4465 return 0;
4466}
4467
4468int iscsit_close_session(struct iscsi_session *sess)
4469{
Andy Grover60bfcf82013-10-09 11:05:58 -07004470 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004471 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4472
4473 if (atomic_read(&sess->nconn)) {
4474 pr_err("%d connection(s) still exist for iSCSI session"
4475 " to %s\n", atomic_read(&sess->nconn),
4476 sess->sess_ops->InitiatorName);
4477 BUG();
4478 }
4479
4480 spin_lock_bh(&se_tpg->session_lock);
4481 atomic_set(&sess->session_logout, 1);
4482 atomic_set(&sess->session_reinstatement, 1);
4483 iscsit_stop_time2retain_timer(sess);
4484 spin_unlock_bh(&se_tpg->session_lock);
4485
4486 /*
4487 * transport_deregister_session_configfs() will clear the
4488 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4489 * can be setting it again with __transport_register_session() in
4490 * iscsi_post_login_handler() again after the iscsit_stop_session()
4491 * completes in iscsi_np context.
4492 */
4493 transport_deregister_session_configfs(sess->se_sess);
4494
4495 /*
4496 * If any other processes are accessing this session pointer we must
4497 * wait until they have completed. If we are in an interrupt (the
4498 * time2retain handler) and contain and active session usage count we
4499 * restart the timer and exit.
4500 */
4501 if (!in_interrupt()) {
4502 if (iscsit_check_session_usage_count(sess) == 1)
4503 iscsit_stop_session(sess, 1, 1);
4504 } else {
4505 if (iscsit_check_session_usage_count(sess) == 2) {
4506 atomic_set(&sess->session_logout, 0);
4507 iscsit_start_time2retain_handler(sess);
4508 return 0;
4509 }
4510 }
4511
4512 transport_deregister_session(sess->se_sess);
4513
4514 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4515 iscsit_free_connection_recovery_entires(sess);
4516
4517 iscsit_free_all_ooo_cmdsns(sess);
4518
4519 spin_lock_bh(&se_tpg->session_lock);
4520 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4521 sess->session_state = TARG_SESS_STATE_FREE;
4522 pr_debug("Released iSCSI session from node: %s\n",
4523 sess->sess_ops->InitiatorName);
4524 tpg->nsessions--;
4525 if (tpg->tpg_tiqn)
4526 tpg->tpg_tiqn->tiqn_nsessions--;
4527
4528 pr_debug("Decremented number of active iSCSI Sessions on"
4529 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4530
4531 spin_lock(&sess_idr_lock);
4532 idr_remove(&sess_idr, sess->session_index);
4533 spin_unlock(&sess_idr_lock);
4534
4535 kfree(sess->sess_ops);
4536 sess->sess_ops = NULL;
4537 spin_unlock_bh(&se_tpg->session_lock);
4538
4539 kfree(sess);
4540 return 0;
4541}
4542
4543static void iscsit_logout_post_handler_closesession(
4544 struct iscsi_conn *conn)
4545{
4546 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004547 int sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004548
4549 atomic_set(&conn->conn_logout_remove, 0);
4550 complete(&conn->conn_logout_comp);
4551
4552 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004553 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004554 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004555 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004556}
4557
4558static void iscsit_logout_post_handler_samecid(
4559 struct iscsi_conn *conn)
4560{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004561 int sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004562
4563 atomic_set(&conn->conn_logout_remove, 0);
4564 complete(&conn->conn_logout_comp);
4565
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004566 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004567 iscsit_dec_conn_usage_count(conn);
4568}
4569
4570static void iscsit_logout_post_handler_diffcid(
4571 struct iscsi_conn *conn,
4572 u16 cid)
4573{
4574 struct iscsi_conn *l_conn;
4575 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004576 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004577
4578 if (!sess)
4579 return;
4580
4581 spin_lock_bh(&sess->conn_lock);
4582 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4583 if (l_conn->cid == cid) {
4584 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004585 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004586 break;
4587 }
4588 }
4589 spin_unlock_bh(&sess->conn_lock);
4590
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004591 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004592 return;
4593
4594 if (l_conn->sock)
4595 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4596
4597 spin_lock_bh(&l_conn->state_lock);
4598 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4599 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4600 spin_unlock_bh(&l_conn->state_lock);
4601
4602 iscsit_cause_connection_reinstatement(l_conn, 1);
4603 iscsit_dec_conn_usage_count(l_conn);
4604}
4605
4606/*
4607 * Return of 0 causes the TX thread to restart.
4608 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004609int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004610 struct iscsi_cmd *cmd,
4611 struct iscsi_conn *conn)
4612{
4613 int ret = 0;
4614
4615 switch (cmd->logout_reason) {
4616 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4617 switch (cmd->logout_response) {
4618 case ISCSI_LOGOUT_SUCCESS:
4619 case ISCSI_LOGOUT_CLEANUP_FAILED:
4620 default:
4621 iscsit_logout_post_handler_closesession(conn);
4622 break;
4623 }
4624 ret = 0;
4625 break;
4626 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4627 if (conn->cid == cmd->logout_cid) {
4628 switch (cmd->logout_response) {
4629 case ISCSI_LOGOUT_SUCCESS:
4630 case ISCSI_LOGOUT_CLEANUP_FAILED:
4631 default:
4632 iscsit_logout_post_handler_samecid(conn);
4633 break;
4634 }
4635 ret = 0;
4636 } else {
4637 switch (cmd->logout_response) {
4638 case ISCSI_LOGOUT_SUCCESS:
4639 iscsit_logout_post_handler_diffcid(conn,
4640 cmd->logout_cid);
4641 break;
4642 case ISCSI_LOGOUT_CID_NOT_FOUND:
4643 case ISCSI_LOGOUT_CLEANUP_FAILED:
4644 default:
4645 break;
4646 }
4647 ret = 1;
4648 }
4649 break;
4650 case ISCSI_LOGOUT_REASON_RECOVERY:
4651 switch (cmd->logout_response) {
4652 case ISCSI_LOGOUT_SUCCESS:
4653 case ISCSI_LOGOUT_CID_NOT_FOUND:
4654 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4655 case ISCSI_LOGOUT_CLEANUP_FAILED:
4656 default:
4657 break;
4658 }
4659 ret = 1;
4660 break;
4661 default:
4662 break;
4663
4664 }
4665 return ret;
4666}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004667EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004668
4669void iscsit_fail_session(struct iscsi_session *sess)
4670{
4671 struct iscsi_conn *conn;
4672
4673 spin_lock_bh(&sess->conn_lock);
4674 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4675 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4676 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4677 }
4678 spin_unlock_bh(&sess->conn_lock);
4679
4680 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4681 sess->session_state = TARG_SESS_STATE_FAILED;
4682}
4683
4684int iscsit_free_session(struct iscsi_session *sess)
4685{
4686 u16 conn_count = atomic_read(&sess->nconn);
4687 struct iscsi_conn *conn, *conn_tmp = NULL;
4688 int is_last;
4689
4690 spin_lock_bh(&sess->conn_lock);
4691 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4692
4693 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4694 conn_list) {
4695 if (conn_count == 0)
4696 break;
4697
4698 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4699 is_last = 1;
4700 } else {
4701 iscsit_inc_conn_usage_count(conn_tmp);
4702 is_last = 0;
4703 }
4704 iscsit_inc_conn_usage_count(conn);
4705
4706 spin_unlock_bh(&sess->conn_lock);
4707 iscsit_cause_connection_reinstatement(conn, 1);
4708 spin_lock_bh(&sess->conn_lock);
4709
4710 iscsit_dec_conn_usage_count(conn);
4711 if (is_last == 0)
4712 iscsit_dec_conn_usage_count(conn_tmp);
4713
4714 conn_count--;
4715 }
4716
4717 if (atomic_read(&sess->nconn)) {
4718 spin_unlock_bh(&sess->conn_lock);
4719 wait_for_completion(&sess->session_wait_comp);
4720 } else
4721 spin_unlock_bh(&sess->conn_lock);
4722
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004723 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004724 return 0;
4725}
4726
4727void iscsit_stop_session(
4728 struct iscsi_session *sess,
4729 int session_sleep,
4730 int connection_sleep)
4731{
4732 u16 conn_count = atomic_read(&sess->nconn);
4733 struct iscsi_conn *conn, *conn_tmp = NULL;
4734 int is_last;
4735
4736 spin_lock_bh(&sess->conn_lock);
4737 if (session_sleep)
4738 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4739
4740 if (connection_sleep) {
4741 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4742 conn_list) {
4743 if (conn_count == 0)
4744 break;
4745
4746 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4747 is_last = 1;
4748 } else {
4749 iscsit_inc_conn_usage_count(conn_tmp);
4750 is_last = 0;
4751 }
4752 iscsit_inc_conn_usage_count(conn);
4753
4754 spin_unlock_bh(&sess->conn_lock);
4755 iscsit_cause_connection_reinstatement(conn, 1);
4756 spin_lock_bh(&sess->conn_lock);
4757
4758 iscsit_dec_conn_usage_count(conn);
4759 if (is_last == 0)
4760 iscsit_dec_conn_usage_count(conn_tmp);
4761 conn_count--;
4762 }
4763 } else {
4764 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4765 iscsit_cause_connection_reinstatement(conn, 0);
4766 }
4767
4768 if (session_sleep && atomic_read(&sess->nconn)) {
4769 spin_unlock_bh(&sess->conn_lock);
4770 wait_for_completion(&sess->session_wait_comp);
4771 } else
4772 spin_unlock_bh(&sess->conn_lock);
4773}
4774
4775int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4776{
4777 struct iscsi_session *sess;
4778 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4779 struct se_session *se_sess, *se_sess_tmp;
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004780 LIST_HEAD(free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004781 int session_count = 0;
4782
4783 spin_lock_bh(&se_tpg->session_lock);
4784 if (tpg->nsessions && !force) {
4785 spin_unlock_bh(&se_tpg->session_lock);
4786 return -1;
4787 }
4788
4789 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4790 sess_list) {
4791 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4792
4793 spin_lock(&sess->conn_lock);
4794 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4795 atomic_read(&sess->session_logout) ||
4796 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4797 spin_unlock(&sess->conn_lock);
4798 continue;
4799 }
4800 atomic_set(&sess->session_reinstatement, 1);
4801 spin_unlock(&sess->conn_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004802
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004803 list_move_tail(&se_sess->sess_list, &free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004804 }
4805 spin_unlock_bh(&se_tpg->session_lock);
4806
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004807 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4808 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4809
4810 iscsit_free_session(sess);
4811 session_count++;
4812 }
4813
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004814 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4815 " Group: %hu\n", session_count, tpg->tpgt);
4816 return 0;
4817}
4818
4819MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4820MODULE_VERSION("4.1.x");
4821MODULE_AUTHOR("nab@Linux-iSCSI.org");
4822MODULE_LICENSE("GPL");
4823
4824module_init(iscsi_target_init_module);
4825module_exit(iscsi_target_cleanup_module);