blob: 72204fbf2bb13be5cdb7211ca6001b58f0c4fbc6 [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(
Andy Grover13a3cf02015-08-24 10:26:06 -0700272 struct sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800273 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000274 int network_transport)
275{
276 struct sockaddr_in *sock_in, *sock_in_e;
277 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800278 bool ip_match = false;
Andy Grover69d75572015-08-24 10:26:04 -0700279 u16 port, port_e;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000280
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800281 if (sockaddr->ss_family == AF_INET6) {
282 sock_in6 = (struct sockaddr_in6 *)sockaddr;
283 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
284
285 if (!memcmp(&sock_in6->sin6_addr.in6_u,
286 &sock_in6_e->sin6_addr.in6_u,
287 sizeof(struct in6_addr)))
288 ip_match = true;
289
290 port = ntohs(sock_in6->sin6_port);
Andy Grover69d75572015-08-24 10:26:04 -0700291 port_e = ntohs(sock_in6_e->sin6_port);
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800292 } else {
293 sock_in = (struct sockaddr_in *)sockaddr;
294 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
295
296 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
297 ip_match = true;
298
299 port = ntohs(sock_in->sin_port);
Andy Grover69d75572015-08-24 10:26:04 -0700300 port_e = ntohs(sock_in_e->sin_port);
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800301 }
302
Andy Grover69d75572015-08-24 10:26:04 -0700303 if (ip_match && (port_e == port) &&
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800304 (np->np_network_transport == network_transport))
305 return true;
306
307 return false;
308}
309
Andy Groveree291e62014-01-24 16:18:54 -0800310/*
311 * Called with mutex np_lock held
312 */
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800313static struct iscsi_np *iscsit_get_np(
Andy Grover13a3cf02015-08-24 10:26:06 -0700314 struct sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800315 int network_transport)
316{
317 struct iscsi_np *np;
318 bool match;
319
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000320 list_for_each_entry(np, &g_np_list, np_list) {
Andy Groveree291e62014-01-24 16:18:54 -0800321 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000322 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
Andy Groveree291e62014-01-24 16:18:54 -0800323 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000324 continue;
325 }
326
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800327 match = iscsit_check_np_match(sockaddr, np, network_transport);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200328 if (match) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000329 /*
330 * Increment the np_exports reference count now to
331 * prevent iscsit_del_np() below from being called
332 * while iscsi_tpg_add_network_portal() is called.
333 */
334 np->np_exports++;
Andy Groveree291e62014-01-24 16:18:54 -0800335 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000336 return np;
337 }
Andy Groveree291e62014-01-24 16:18:54 -0800338 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000339 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000340
341 return NULL;
342}
343
344struct iscsi_np *iscsit_add_np(
Andy Grover13a3cf02015-08-24 10:26:06 -0700345 struct sockaddr_storage *sockaddr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000346 int network_transport)
347{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000348 struct iscsi_np *np;
349 int ret;
Andy Groveree291e62014-01-24 16:18:54 -0800350
351 mutex_lock(&np_lock);
352
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000353 /*
354 * Locate the existing struct iscsi_np if already active..
355 */
356 np = iscsit_get_np(sockaddr, network_transport);
Andy Groveree291e62014-01-24 16:18:54 -0800357 if (np) {
358 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000359 return np;
Andy Groveree291e62014-01-24 16:18:54 -0800360 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000361
362 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
363 if (!np) {
364 pr_err("Unable to allocate memory for struct iscsi_np\n");
Andy Groveree291e62014-01-24 16:18:54 -0800365 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000366 return ERR_PTR(-ENOMEM);
367 }
368
369 np->np_flags |= NPF_IP_NETWORK;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000370 np->np_network_transport = network_transport;
371 spin_lock_init(&np->np_thread_lock);
372 init_completion(&np->np_restart_comp);
373 INIT_LIST_HEAD(&np->np_list);
374
375 ret = iscsi_target_setup_login_socket(np, sockaddr);
376 if (ret != 0) {
377 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800378 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000379 return ERR_PTR(ret);
380 }
381
382 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
383 if (IS_ERR(np->np_thread)) {
384 pr_err("Unable to create kthread: iscsi_np\n");
385 ret = PTR_ERR(np->np_thread);
386 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800387 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000388 return ERR_PTR(ret);
389 }
390 /*
391 * Increment the np_exports reference count now to prevent
392 * iscsit_del_np() below from being run while a new call to
393 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
394 * active. We don't need to hold np->np_thread_lock at this
395 * point because iscsi_np has not been added to g_np_list yet.
396 */
397 np->np_exports = 1;
Andy Groveree291e62014-01-24 16:18:54 -0800398 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000399
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000400 list_add_tail(&np->np_list, &g_np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800401 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000402
Andy Grover69d75572015-08-24 10:26:04 -0700403 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
404 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000405
406 return np;
407}
408
409int iscsit_reset_np_thread(
410 struct iscsi_np *np,
411 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700412 struct iscsi_portal_group *tpg,
413 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000414{
415 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000416 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
417 spin_unlock_bh(&np->np_thread_lock);
418 return 0;
419 }
420 np->np_thread_state = ISCSI_NP_THREAD_RESET;
421
422 if (np->np_thread) {
423 spin_unlock_bh(&np->np_thread_lock);
424 send_sig(SIGINT, np->np_thread, 1);
425 wait_for_completion(&np->np_restart_comp);
426 spin_lock_bh(&np->np_thread_lock);
427 }
428 spin_unlock_bh(&np->np_thread_lock);
429
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700430 if (tpg_np && shutdown) {
431 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
432
433 wait_for_completion(&tpg_np->tpg_np_comp);
434 }
435
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000436 return 0;
437}
438
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800439static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000440{
Al Virobf6932f2012-07-21 08:55:18 +0100441 if (np->np_socket)
442 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000443}
444
445int iscsit_del_np(struct iscsi_np *np)
446{
447 spin_lock_bh(&np->np_thread_lock);
448 np->np_exports--;
449 if (np->np_exports) {
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700450 np->enabled = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000451 spin_unlock_bh(&np->np_thread_lock);
452 return 0;
453 }
454 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
455 spin_unlock_bh(&np->np_thread_lock);
456
457 if (np->np_thread) {
458 /*
459 * We need to send the signal to wakeup Linux/Net
460 * which may be sleeping in sock_accept()..
461 */
462 send_sig(SIGINT, np->np_thread, 1);
463 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800464 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000465 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800466
467 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000468
Andy Groveree291e62014-01-24 16:18:54 -0800469 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000470 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800471 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000472
Andy Grover69d75572015-08-24 10:26:04 -0700473 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
474 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000475
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800476 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000477 kfree(np);
478 return 0;
479}
480
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700481static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
482static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
483
484static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
485{
486 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
487 return 0;
488}
489
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700490static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
491{
492 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
493
494 spin_lock_bh(&conn->cmd_lock);
495 if (!list_empty(&cmd->i_conn_node))
496 list_del_init(&cmd->i_conn_node);
497 spin_unlock_bh(&conn->cmd_lock);
498
499 __iscsit_free_cmd(cmd, scsi_cmd, true);
500}
501
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700502static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
503{
504 return TARGET_PROT_NORMAL;
505}
506
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800507static struct iscsit_transport iscsi_target_transport = {
508 .name = "iSCSI/TCP",
509 .transport_type = ISCSI_TCP,
510 .owner = NULL,
511 .iscsit_setup_np = iscsit_setup_np,
512 .iscsit_accept_np = iscsit_accept_np,
513 .iscsit_free_np = iscsit_free_np,
514 .iscsit_get_login_rx = iscsit_get_login_rx,
515 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800516 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700517 .iscsit_immediate_queue = iscsit_immediate_queue,
518 .iscsit_response_queue = iscsit_response_queue,
519 .iscsit_queue_data_in = iscsit_queue_rsp,
520 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700521 .iscsit_aborted_task = iscsit_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700522 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800523};
524
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000525static int __init iscsi_target_init_module(void)
526{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800527 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000528
529 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
530
531 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
532 if (!iscsit_global) {
533 pr_err("Unable to allocate memory for iscsit_global\n");
534 return -1;
535 }
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800536 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000537 mutex_init(&auth_id_lock);
538 spin_lock_init(&sess_idr_lock);
539 idr_init(&tiqn_idr);
540 idr_init(&sess_idr);
541
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200542 ret = target_register_template(&iscsi_ops);
543 if (ret)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000544 goto out;
545
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800546 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
547 iscsit_global->ts_bitmap = vzalloc(size);
548 if (!iscsit_global->ts_bitmap) {
549 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000550 goto configfs_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000551 }
552
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000553 lio_qr_cache = kmem_cache_create("lio_qr_cache",
554 sizeof(struct iscsi_queue_req),
555 __alignof__(struct iscsi_queue_req), 0, NULL);
556 if (!lio_qr_cache) {
557 pr_err("nable to kmem_cache_create() for"
558 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800559 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000560 }
561
562 lio_dr_cache = kmem_cache_create("lio_dr_cache",
563 sizeof(struct iscsi_datain_req),
564 __alignof__(struct iscsi_datain_req), 0, NULL);
565 if (!lio_dr_cache) {
566 pr_err("Unable to kmem_cache_create() for"
567 " lio_dr_cache\n");
568 goto qr_out;
569 }
570
571 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
572 sizeof(struct iscsi_ooo_cmdsn),
573 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
574 if (!lio_ooo_cache) {
575 pr_err("Unable to kmem_cache_create() for"
576 " lio_ooo_cache\n");
577 goto dr_out;
578 }
579
580 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
581 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
582 0, NULL);
583 if (!lio_r2t_cache) {
584 pr_err("Unable to kmem_cache_create() for"
585 " lio_r2t_cache\n");
586 goto ooo_out;
587 }
588
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800589 iscsit_register_transport(&iscsi_target_transport);
590
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000591 if (iscsit_load_discovery_tpg() < 0)
592 goto r2t_out;
593
594 return ret;
595r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100596 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000597 kmem_cache_destroy(lio_r2t_cache);
598ooo_out:
599 kmem_cache_destroy(lio_ooo_cache);
600dr_out:
601 kmem_cache_destroy(lio_dr_cache);
602qr_out:
603 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800604bitmap_out:
605 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000606configfs_out:
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200607 /* XXX: this probably wants it to be it's own unwind step.. */
608 if (iscsit_global->discovery_tpg)
609 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
610 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000611out:
612 kfree(iscsit_global);
613 return -ENOMEM;
614}
615
616static void __exit iscsi_target_cleanup_module(void)
617{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000618 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800619 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000620 kmem_cache_destroy(lio_qr_cache);
621 kmem_cache_destroy(lio_dr_cache);
622 kmem_cache_destroy(lio_ooo_cache);
623 kmem_cache_destroy(lio_r2t_cache);
624
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200625 /*
626 * Shutdown discovery sessions and disable discovery TPG
627 */
628 if (iscsit_global->discovery_tpg)
629 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000630
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200631 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000632
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800633 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000634 kfree(iscsit_global);
635}
636
Andy Grover8b1e1242012-04-03 15:51:12 -0700637static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700638 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000639 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700640 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000641{
642 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000643
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000644 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000645 if (!cmd)
646 return -1;
647
648 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700649 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000650
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100651 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000652 if (!cmd->buf_ptr) {
653 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700654 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000655 return -1;
656 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000657
658 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700659 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000660 spin_unlock_bh(&conn->cmd_lock);
661
662 cmd->i_state = ISTATE_SEND_REJECT;
663 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
664
Nicholas Bellingerba159912013-07-03 03:48:24 -0700665 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000666}
667
Nicholas Bellingerba159912013-07-03 03:48:24 -0700668static int iscsit_add_reject_from_cmd(
669 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000670 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700671 bool add_to_conn,
672 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000673{
674 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000675
676 if (!cmd->conn) {
677 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
678 cmd->init_task_tag);
679 return -1;
680 }
681 conn = cmd->conn;
682
683 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700684 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000685
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100686 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000687 if (!cmd->buf_ptr) {
688 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700689 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000690 return -1;
691 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000692
693 if (add_to_conn) {
694 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700695 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000696 spin_unlock_bh(&conn->cmd_lock);
697 }
698
699 cmd->i_state = ISTATE_SEND_REJECT;
700 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800701 /*
702 * Perform the kref_put now if se_cmd has already been setup by
703 * scsit_setup_scsi_cmd()
704 */
705 if (cmd->se_cmd.se_tfo != NULL) {
706 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +0200707 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800708 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700709 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000710}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700711
712static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
713 unsigned char *buf)
714{
715 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
716}
717
718int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
719{
720 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
721}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000722
723/*
724 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700725 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000726 */
727static int iscsit_map_iovec(
728 struct iscsi_cmd *cmd,
729 struct kvec *iov,
730 u32 data_offset,
731 u32 data_length)
732{
733 u32 i = 0;
734 struct scatterlist *sg;
735 unsigned int page_off;
736
737 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700738 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000739 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700740 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000741 page_off = (data_offset % PAGE_SIZE);
742
743 cmd->first_data_sg = sg;
744 cmd->first_data_sg_off = page_off;
745
746 while (data_length) {
747 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
748
749 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
750 iov[i].iov_len = cur_len;
751
752 data_length -= cur_len;
753 page_off = 0;
754 sg = sg_next(sg);
755 i++;
756 }
757
758 cmd->kmapped_nents = i;
759
760 return i;
761}
762
763static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
764{
765 u32 i;
766 struct scatterlist *sg;
767
768 sg = cmd->first_data_sg;
769
770 for (i = 0; i < cmd->kmapped_nents; i++)
771 kunmap(sg_page(&sg[i]));
772}
773
774static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
775{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700776 LIST_HEAD(ack_list);
777 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000778
779 conn->exp_statsn = exp_statsn;
780
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800781 if (conn->sess->sess_ops->RDMAExtensions)
782 return;
783
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000784 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700785 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000786 spin_lock(&cmd->istate_lock);
787 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800788 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000789 cmd->i_state = ISTATE_REMOVE;
790 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700791 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000792 continue;
793 }
794 spin_unlock(&cmd->istate_lock);
795 }
796 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700797
798 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800799 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700800 iscsit_free_cmd(cmd, false);
801 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000802}
803
804static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
805{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700806 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000807
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400808 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000809
810 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
811 if (!cmd->iov_data) {
812 pr_err("Unable to allocate cmd->iov_data\n");
813 return -ENOMEM;
814 }
815
816 cmd->orig_iov_data_count = iov_count;
817 return 0;
818}
819
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800820int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
821 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000822{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800823 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000824 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700825 int iscsi_task_attr;
826 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000827
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800828 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000829
830 hdr = (struct iscsi_scsi_req *) buf;
831 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000832
833 /* FIXME; Add checks for AdditionalHeaderSegment */
834
835 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
836 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
837 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
838 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700839 return iscsit_add_reject_cmd(cmd,
840 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000841 }
842
843 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
844 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
845 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800846 * From RFC-3720 Section 10.3.1:
847 *
848 * "Either or both of R and W MAY be 1 when either the
849 * Expected Data Transfer Length and/or Bidirectional Read
850 * Expected Data Transfer Length are 0"
851 *
852 * For this case, go ahead and clear the unnecssary bits
853 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000854 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800855 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
856 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000857
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800858 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000859 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800860 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000861 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000862
863 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
864 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
865 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
866 " MUST be set if Expected Data Transfer Length is not 0."
867 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700868 return iscsit_add_reject_cmd(cmd,
869 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000870 }
871
872 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
873 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
874 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700875 return iscsit_add_reject_cmd(cmd,
876 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000877 }
878
879 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
880 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
881 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700882 return iscsit_add_reject_cmd(cmd,
883 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000884 }
885
886 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
887 pr_err("ImmediateData=No but DataSegmentLength=%u,"
888 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700889 return iscsit_add_reject_cmd(cmd,
890 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000891 }
892
Nicholas Bellingerba159912013-07-03 03:48:24 -0700893 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000894 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
895 pr_err("Expected Data Transfer Length and Length of"
896 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
897 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700898 return iscsit_add_reject_cmd(cmd,
899 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000900 }
901
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400902 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000903 pr_err("DataSegmentLength: %u is greater than"
904 " EDTL: %u, protocol error.\n", payload_length,
905 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700906 return iscsit_add_reject_cmd(cmd,
907 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000908 }
909
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700910 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000911 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700912 " MaxXmitDataSegmentLength: %u, protocol error.\n",
913 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700914 return iscsit_add_reject_cmd(cmd,
915 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000916 }
917
918 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
919 pr_err("DataSegmentLength: %u is greater than"
920 " FirstBurstLength: %u, protocol error.\n",
921 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700922 return iscsit_add_reject_cmd(cmd,
923 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000924 }
925
926 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
927 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
928 DMA_NONE;
929
Andy Groverd28b11692012-04-03 15:51:22 -0700930 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700931 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
932 /*
933 * Figure out the SAM Task Attribute for the incoming SCSI CDB
934 */
935 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
936 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800937 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700938 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800939 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700940 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800941 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700942 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800943 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700944 else {
945 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800946 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
947 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700948 }
949
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000950 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
951 cmd->i_state = ISTATE_NEW_CMD;
952 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
953 cmd->immediate_data = (payload_length) ? 1 : 0;
954 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
955 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
956 if (cmd->unsolicited_data)
957 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
958
959 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
Alexei Potashnik95473082015-07-21 15:07:56 -0700960 if (hdr->flags & ISCSI_FLAG_CMD_READ)
Sagi Grimbergc1e34b62015-01-26 12:49:05 +0200961 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Alexei Potashnik95473082015-07-21 15:07:56 -0700962 else
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000963 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400964 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
965 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000966 cmd->first_burst_len = payload_length;
967
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800968 if (!conn->sess->sess_ops->RDMAExtensions &&
969 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000970 struct iscsi_datain_req *dr;
971
972 dr = iscsit_allocate_datain_req();
973 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700974 return iscsit_add_reject_cmd(cmd,
975 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000976
977 iscsit_attach_datain_req(cmd, dr);
978 }
979
980 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700981 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
982 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200983 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400984 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
985 cmd->data_direction, sam_task_attr,
986 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700987
988 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
989 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800990 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
991 conn->cid);
992
Bart Van Asscheafc16602015-04-27 13:52:36 +0200993 target_get_sess_cmd(&cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -0700994
Christoph Hellwigde103c92012-11-06 12:24:09 -0800995 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
996 scsilun_to_int(&hdr->lun));
997 if (cmd->sense_reason)
998 goto attach_cmd;
999
Bart Van Assche649ee052015-04-14 13:26:44 +02001000 /* only used for printks or comparing with ->ref_task_tag */
1001 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001002 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1003 if (cmd->sense_reason) {
1004 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001005 return iscsit_add_reject_cmd(cmd,
1006 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001007 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001008
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001009 goto attach_cmd;
1010 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001011
Christoph Hellwigde103c92012-11-06 12:24:09 -08001012 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001013 return iscsit_add_reject_cmd(cmd,
1014 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001015 }
1016
1017attach_cmd:
1018 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001019 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001020 spin_unlock_bh(&conn->cmd_lock);
1021 /*
1022 * Check if we need to delay processing because of ALUA
1023 * Active/NonOptimized primary access state..
1024 */
1025 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001026
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001027 return 0;
1028}
1029EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001030
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001031void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1032{
1033 iscsit_set_dataout_sequence_values(cmd);
1034
1035 spin_lock_bh(&cmd->dataout_timeout_lock);
1036 iscsit_start_dataout_timer(cmd, cmd->conn);
1037 spin_unlock_bh(&cmd->dataout_timeout_lock);
1038}
1039EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1040
1041int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1042 struct iscsi_scsi_req *hdr)
1043{
1044 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001045 /*
1046 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1047 * the Immediate Bit is not set, and no Immediate
1048 * Data is attached.
1049 *
1050 * A PDU/CmdSN carrying Immediate Data can only
1051 * be processed after the DataCRC has passed.
1052 * If the DataCRC fails, the CmdSN MUST NOT
1053 * be acknowledged. (See below)
1054 */
1055 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001056 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1057 (unsigned char *)hdr, hdr->cmdsn);
1058 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1059 return -1;
1060 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001061 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001062 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001063 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001064 }
1065
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001066 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001067
1068 /*
1069 * If no Immediate Data is attached, it's OK to return now.
1070 */
1071 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001072 if (!cmd->sense_reason && cmd->unsolicited_data)
1073 iscsit_set_unsoliticed_dataout(cmd);
1074 if (!cmd->sense_reason)
1075 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001076
Bart Van Asscheafc16602015-04-27 13:52:36 +02001077 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001078 return 0;
1079 }
1080
1081 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001082 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1083 * execution. These exceptions are processed in CmdSN order using
1084 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001085 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001086 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001087 if (cmd->reject_reason)
1088 return 0;
1089
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001090 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001091 }
1092 /*
1093 * Call directly into transport_generic_new_cmd() to perform
1094 * the backend memory allocation.
1095 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001096 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001097 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001098 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001099
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001100 return 0;
1101}
1102EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1103
1104static int
1105iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1106 bool dump_payload)
1107{
1108 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1109 /*
1110 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1111 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001112 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001113 goto after_immediate_data;
1114
1115 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1116 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001117after_immediate_data:
1118 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1119 /*
1120 * A PDU/CmdSN carrying Immediate Data passed
1121 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1122 * Immediate Bit is not set.
1123 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001124 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1125 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001126 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001127 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001128
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001129 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001130 int rc;
1131
1132 rc = iscsit_dump_data_payload(cmd->conn,
1133 cmd->first_burst_len, 1);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001134 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001135 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001136 } else if (cmd->unsolicited_data)
1137 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001138
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001139 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1140 /*
1141 * Immediate Data failed DataCRC and ERL>=1,
1142 * silently drop this PDU and let the initiator
1143 * plug the CmdSN gap.
1144 *
1145 * FIXME: Send Unsolicited NOPIN with reserved
1146 * TTT here to help the initiator figure out
1147 * the missing CmdSN, although they should be
1148 * intelligent enough to determine the missing
1149 * CmdSN and issue a retry to plug the sequence.
1150 */
1151 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001152 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001153 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1154 return -1;
1155
1156 return 0;
1157}
1158
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001159static int
1160iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1161 unsigned char *buf)
1162{
1163 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1164 int rc, immed_data;
1165 bool dump_payload = false;
1166
1167 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1168 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001169 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001170 /*
1171 * Allocation iovecs needed for struct socket operations for
1172 * traditional iSCSI block I/O.
1173 */
1174 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001175 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001176 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001177 }
1178 immed_data = cmd->immediate_data;
1179
1180 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1181 if (rc < 0)
1182 return rc;
1183 else if (rc > 0)
1184 dump_payload = true;
1185
1186 if (!immed_data)
1187 return 0;
1188
1189 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1190}
1191
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001192static u32 iscsit_do_crypto_hash_sg(
1193 struct hash_desc *hash,
1194 struct iscsi_cmd *cmd,
1195 u32 data_offset,
1196 u32 data_length,
1197 u32 padding,
1198 u8 *pad_bytes)
1199{
1200 u32 data_crc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001201 struct scatterlist *sg;
1202 unsigned int page_off;
1203
1204 crypto_hash_init(hash);
1205
1206 sg = cmd->first_data_sg;
1207 page_off = cmd->first_data_sg_off;
1208
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001209 while (data_length) {
Alexei Potashnikaa756792015-07-20 17:12:12 -07001210 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001211
Alexei Potashnikaa756792015-07-20 17:12:12 -07001212 crypto_hash_update(hash, sg, cur_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001213
1214 data_length -= cur_len;
1215 page_off = 0;
Alexei Potashnikaa756792015-07-20 17:12:12 -07001216 /* iscsit_map_iovec has already checked for invalid sg pointers */
1217 sg = sg_next(sg);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001218 }
1219
1220 if (padding) {
1221 struct scatterlist pad_sg;
1222
1223 sg_init_one(&pad_sg, pad_bytes, padding);
1224 crypto_hash_update(hash, &pad_sg, padding);
1225 }
1226 crypto_hash_final(hash, (u8 *) &data_crc);
1227
1228 return data_crc;
1229}
1230
1231static void iscsit_do_crypto_hash_buf(
1232 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001233 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001234 u32 payload_length,
1235 u32 padding,
1236 u8 *pad_bytes,
1237 u8 *data_crc)
1238{
1239 struct scatterlist sg;
1240
1241 crypto_hash_init(hash);
1242
Jörn Engel8359cf42011-11-24 02:05:51 +01001243 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001244 crypto_hash_update(hash, &sg, payload_length);
1245
1246 if (padding) {
1247 sg_init_one(&sg, pad_bytes, padding);
1248 crypto_hash_update(hash, &sg, padding);
1249 }
1250 crypto_hash_final(hash, data_crc);
1251}
1252
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001253int
1254iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1255 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001256{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001257 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001258 struct iscsi_cmd *cmd = NULL;
1259 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001260 u32 payload_length = ntoh24(hdr->dlength);
1261 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001262
1263 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001264 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1265 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001266 }
1267
1268 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001269 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001270
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001271 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001272 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001273 " MaxXmitDataSegmentLength: %u\n", payload_length,
1274 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001275 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1276 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001277 }
1278
1279 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1280 payload_length);
1281 if (!cmd)
1282 return 0;
1283
1284 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1285 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001286 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001287 payload_length, conn->cid);
1288
1289 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1290 pr_err("Command ITT: 0x%08x received DataOUT after"
1291 " last DataOUT received, dumping payload\n",
1292 cmd->init_task_tag);
1293 return iscsit_dump_data_payload(conn, payload_length, 1);
1294 }
1295
1296 if (cmd->data_direction != DMA_TO_DEVICE) {
1297 pr_err("Command ITT: 0x%08x received DataOUT for a"
1298 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001299 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001300 }
1301 se_cmd = &cmd->se_cmd;
1302 iscsit_mod_dataout_timer(cmd);
1303
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001304 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001305 pr_err("DataOut Offset: %u, Length %u greater than"
1306 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001307 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001308 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001309 }
1310
1311 if (cmd->unsolicited_data) {
1312 int dump_unsolicited_data = 0;
1313
1314 if (conn->sess->sess_ops->InitialR2T) {
1315 pr_err("Received unexpected unsolicited data"
1316 " while InitialR2T=Yes, protocol error.\n");
1317 transport_send_check_condition_and_sense(&cmd->se_cmd,
1318 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1319 return -1;
1320 }
1321 /*
1322 * Special case for dealing with Unsolicited DataOUT
1323 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1324 * failures;
1325 */
1326
1327 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001328 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001329 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001330 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001331
1332 if (dump_unsolicited_data) {
1333 /*
1334 * Check if a delayed TASK_ABORTED status needs to
1335 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1336 * received with the unsolicitied data out.
1337 */
1338 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1339 iscsit_stop_dataout_timer(cmd);
1340
1341 transport_check_aborted_status(se_cmd,
1342 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1343 return iscsit_dump_data_payload(conn, payload_length, 1);
1344 }
1345 } else {
1346 /*
1347 * For the normal solicited data path:
1348 *
1349 * Check for a delayed TASK_ABORTED status and dump any
1350 * incoming data out payload if one exists. Also, when the
1351 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1352 * data out sequence, we decrement outstanding_r2ts. Once
1353 * outstanding_r2ts reaches zero, go ahead and send the delayed
1354 * TASK_ABORTED status.
1355 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001356 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001357 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1358 if (--cmd->outstanding_r2ts < 1) {
1359 iscsit_stop_dataout_timer(cmd);
1360 transport_check_aborted_status(
1361 se_cmd, 1);
1362 }
1363
1364 return iscsit_dump_data_payload(conn, payload_length, 1);
1365 }
1366 }
1367 /*
1368 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1369 * within-command recovery checks before receiving the payload.
1370 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001371 rc = iscsit_check_pre_dataout(cmd, buf);
1372 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001373 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001374 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001375 return -1;
1376
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001377 *out_cmd = cmd;
1378 return 0;
1379}
1380EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1381
1382static int
1383iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1384 struct iscsi_data *hdr)
1385{
1386 struct kvec *iov;
1387 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1388 u32 payload_length = ntoh24(hdr->dlength);
1389 int iov_ret, data_crc_failed = 0;
1390
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001391 rx_size += payload_length;
1392 iov = &cmd->iov_data[0];
1393
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001394 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1395 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001396 if (iov_ret < 0)
1397 return -1;
1398
1399 iov_count += iov_ret;
1400
1401 padding = ((-payload_length) & 3);
1402 if (padding != 0) {
1403 iov[iov_count].iov_base = cmd->pad_bytes;
1404 iov[iov_count++].iov_len = padding;
1405 rx_size += padding;
1406 pr_debug("Receiving %u padding bytes.\n", padding);
1407 }
1408
1409 if (conn->conn_ops->DataDigest) {
1410 iov[iov_count].iov_base = &checksum;
1411 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1412 rx_size += ISCSI_CRC_LEN;
1413 }
1414
1415 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1416
1417 iscsit_unmap_iovec(cmd);
1418
1419 if (rx_got != rx_size)
1420 return -1;
1421
1422 if (conn->conn_ops->DataDigest) {
1423 u32 data_crc;
1424
1425 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001426 be32_to_cpu(hdr->offset),
1427 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001428 cmd->pad_bytes);
1429
1430 if (checksum != data_crc) {
1431 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1432 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1433 " does not match computed 0x%08x\n",
1434 hdr->itt, hdr->offset, payload_length,
1435 hdr->datasn, checksum, data_crc);
1436 data_crc_failed = 1;
1437 } else {
1438 pr_debug("Got CRC32C DataDigest 0x%08x for"
1439 " %u bytes of Data Out\n", checksum,
1440 payload_length);
1441 }
1442 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001443
1444 return data_crc_failed;
1445}
1446
1447int
1448iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1449 bool data_crc_failed)
1450{
1451 struct iscsi_conn *conn = cmd->conn;
1452 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001453 /*
1454 * Increment post receive data and CRC values or perform
1455 * within-command recovery.
1456 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001457 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1458 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001459 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001460 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001461 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001462 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1463 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001464 /*
1465 * Handle extra special case for out of order
1466 * Unsolicited Data Out.
1467 */
1468 spin_lock_bh(&cmd->istate_lock);
1469 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1470 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1471 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1472 spin_unlock_bh(&cmd->istate_lock);
1473
1474 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001475 if (ooo_cmdsn)
1476 return 0;
1477 target_execute_cmd(&cmd->se_cmd);
1478 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001479 } else /* DATAOUT_CANNOT_RECOVER */
1480 return -1;
1481
1482 return 0;
1483}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001484EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001485
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001486static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1487{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001488 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001489 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1490 int rc;
1491 bool data_crc_failed = false;
1492
1493 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1494 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001495 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001496 else if (!cmd)
1497 return 0;
1498
1499 rc = iscsit_get_dataout(conn, cmd, hdr);
1500 if (rc < 0)
1501 return rc;
1502 else if (rc > 0)
1503 data_crc_failed = true;
1504
1505 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1506}
1507
Nicholas Bellinger778de362013-06-14 16:07:47 -07001508int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1509 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001510{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001511 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001512
Arshad Hussaina3662602014-03-14 15:28:59 -07001513 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1514 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1515 if (!cmd)
1516 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1517 (unsigned char *)hdr);
1518
1519 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1520 (unsigned char *)hdr);
1521 }
1522
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001523 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001524 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1525 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001526 if (!cmd)
1527 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1528 (unsigned char *)hdr);
1529
Nicholas Bellingerba159912013-07-03 03:48:24 -07001530 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1531 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001532 }
1533
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001534 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001535 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001536 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001537 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001538 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001539 if (!cmd)
1540 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1541 (unsigned char *)hdr);
1542
Nicholas Bellingerba159912013-07-03 03:48:24 -07001543 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1544 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001545 }
1546
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001547 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001548 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001549 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001550 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1551 payload_length);
1552 /*
1553 * This is not a response to a Unsolicited NopIN, which means
1554 * it can either be a NOPOUT ping request (with a valid ITT),
1555 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1556 * Either way, make sure we allocate an struct iscsi_cmd, as both
1557 * can contain ping data.
1558 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001559 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001560 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1561 cmd->i_state = ISTATE_SEND_NOPIN;
1562 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1563 1 : 0);
1564 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1565 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001566 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1567 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001568 cmd->data_direction = DMA_NONE;
1569 }
1570
Nicholas Bellinger778de362013-06-14 16:07:47 -07001571 return 0;
1572}
1573EXPORT_SYMBOL(iscsit_setup_nop_out);
1574
1575int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1576 struct iscsi_nopout *hdr)
1577{
1578 struct iscsi_cmd *cmd_p = NULL;
1579 int cmdsn_ret = 0;
1580 /*
1581 * Initiator is expecting a NopIN ping reply..
1582 */
1583 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001584 if (!cmd)
1585 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1586 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001587
1588 spin_lock_bh(&conn->cmd_lock);
1589 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1590 spin_unlock_bh(&conn->cmd_lock);
1591
1592 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1593
1594 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1595 iscsit_add_cmd_to_response_queue(cmd, conn,
1596 cmd->i_state);
1597 return 0;
1598 }
1599
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001600 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1601 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001602 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1603 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001604 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001605 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001606
1607 return 0;
1608 }
1609 /*
1610 * This was a response to a unsolicited NOPIN ping.
1611 */
1612 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1613 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1614 if (!cmd_p)
1615 return -EINVAL;
1616
1617 iscsit_stop_nopin_response_timer(conn);
1618
1619 cmd_p->i_state = ISTATE_REMOVE;
1620 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1621
1622 iscsit_start_nopin_timer(conn);
1623 return 0;
1624 }
1625 /*
1626 * Otherwise, initiator is not expecting a NOPIN is response.
1627 * Just ignore for now.
1628 */
1629 return 0;
1630}
1631EXPORT_SYMBOL(iscsit_process_nop_out);
1632
1633static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1634 unsigned char *buf)
1635{
1636 unsigned char *ping_data = NULL;
1637 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1638 struct kvec *iov = NULL;
1639 u32 payload_length = ntoh24(hdr->dlength);
1640 int ret;
1641
1642 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1643 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001644 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001645 /*
1646 * Handle NOP-OUT payload for traditional iSCSI sockets
1647 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001648 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001649 u32 checksum, data_crc, padding = 0;
1650 int niov = 0, rx_got, rx_size = payload_length;
1651
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001652 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1653 if (!ping_data) {
1654 pr_err("Unable to allocate memory for"
1655 " NOPOUT ping data.\n");
1656 ret = -1;
1657 goto out;
1658 }
1659
1660 iov = &cmd->iov_misc[0];
1661 iov[niov].iov_base = ping_data;
1662 iov[niov++].iov_len = payload_length;
1663
1664 padding = ((-payload_length) & 3);
1665 if (padding != 0) {
1666 pr_debug("Receiving %u additional bytes"
1667 " for padding.\n", padding);
1668 iov[niov].iov_base = &cmd->pad_bytes;
1669 iov[niov++].iov_len = padding;
1670 rx_size += padding;
1671 }
1672 if (conn->conn_ops->DataDigest) {
1673 iov[niov].iov_base = &checksum;
1674 iov[niov++].iov_len = ISCSI_CRC_LEN;
1675 rx_size += ISCSI_CRC_LEN;
1676 }
1677
1678 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1679 if (rx_got != rx_size) {
1680 ret = -1;
1681 goto out;
1682 }
1683
1684 if (conn->conn_ops->DataDigest) {
1685 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1686 ping_data, payload_length,
1687 padding, cmd->pad_bytes,
1688 (u8 *)&data_crc);
1689
1690 if (checksum != data_crc) {
1691 pr_err("Ping data CRC32C DataDigest"
1692 " 0x%08x does not match computed 0x%08x\n",
1693 checksum, data_crc);
1694 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1695 pr_err("Unable to recover from"
1696 " NOPOUT Ping DataCRC failure while in"
1697 " ERL=0.\n");
1698 ret = -1;
1699 goto out;
1700 } else {
1701 /*
1702 * Silently drop this PDU and let the
1703 * initiator plug the CmdSN gap.
1704 */
1705 pr_debug("Dropping NOPOUT"
1706 " Command CmdSN: 0x%08x due to"
1707 " DataCRC error.\n", hdr->cmdsn);
1708 ret = 0;
1709 goto out;
1710 }
1711 } else {
1712 pr_debug("Got CRC32C DataDigest"
1713 " 0x%08x for %u bytes of ping data.\n",
1714 checksum, payload_length);
1715 }
1716 }
1717
1718 ping_data[payload_length] = '\0';
1719 /*
1720 * Attach ping data to struct iscsi_cmd->buf_ptr.
1721 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001722 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001723 cmd->buf_ptr_size = payload_length;
1724
1725 pr_debug("Got %u bytes of NOPOUT ping"
1726 " data.\n", payload_length);
1727 pr_debug("Ping Data: \"%s\"\n", ping_data);
1728 }
1729
Nicholas Bellinger778de362013-06-14 16:07:47 -07001730 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001731out:
1732 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001733 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001734
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001735 kfree(ping_data);
1736 return ret;
1737}
1738
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001739int
1740iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1741 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001742{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001743 struct se_tmr_req *se_tmr;
1744 struct iscsi_tmr_req *tmr_req;
1745 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001746 int out_of_order_cmdsn = 0, ret;
1747 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001748 u8 function;
1749
1750 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001751 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1752 function = hdr->flags;
1753
1754 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1755 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1756 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1757 hdr->rtt, hdr->refcmdsn, conn->cid);
1758
1759 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1760 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001761 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001762 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001763 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001764 }
1765
1766 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1767 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1768 pr_err("Task Management Request TASK_REASSIGN not"
1769 " issued as immediate command, bad iSCSI Initiator"
1770 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001771 return iscsit_add_reject_cmd(cmd,
1772 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001773 }
1774 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001775 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1776 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001777
Andy Groverd28b11692012-04-03 15:51:22 -07001778 cmd->data_direction = DMA_NONE;
1779
1780 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1781 if (!cmd->tmr_req) {
1782 pr_err("Unable to allocate memory for"
1783 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001784 return iscsit_add_reject_cmd(cmd,
1785 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1786 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001787 }
1788
1789 /*
1790 * TASK_REASSIGN for ERL=2 / connection stays inside of
1791 * LIO-Target $FABRIC_MOD
1792 */
1793 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1794
1795 u8 tcm_function;
1796 int ret;
1797
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001798 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Andy Groverd28b11692012-04-03 15:51:22 -07001799 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001800 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001801
Bart Van Asscheafc16602015-04-27 13:52:36 +02001802 target_get_sess_cmd(&cmd->se_cmd, true);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001803 sess_ref = true;
1804
Andy Groverd28b11692012-04-03 15:51:22 -07001805 switch (function) {
1806 case ISCSI_TM_FUNC_ABORT_TASK:
1807 tcm_function = TMR_ABORT_TASK;
1808 break;
1809 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1810 tcm_function = TMR_ABORT_TASK_SET;
1811 break;
1812 case ISCSI_TM_FUNC_CLEAR_ACA:
1813 tcm_function = TMR_CLEAR_ACA;
1814 break;
1815 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1816 tcm_function = TMR_CLEAR_TASK_SET;
1817 break;
1818 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1819 tcm_function = TMR_LUN_RESET;
1820 break;
1821 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1822 tcm_function = TMR_TARGET_WARM_RESET;
1823 break;
1824 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1825 tcm_function = TMR_TARGET_COLD_RESET;
1826 break;
1827 default:
1828 pr_err("Unknown iSCSI TMR Function:"
1829 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001830 return iscsit_add_reject_cmd(cmd,
1831 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001832 }
1833
1834 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1835 tcm_function, GFP_KERNEL);
1836 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001837 return iscsit_add_reject_cmd(cmd,
1838 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001839
1840 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1841 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001842
1843 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1844 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1845 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1846 cmd->init_task_tag = hdr->itt;
1847 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001848 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1849 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001850 se_tmr = cmd->se_cmd.se_tmr_req;
1851 tmr_req = cmd->tmr_req;
1852 /*
1853 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1854 */
1855 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001856 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1857 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001858 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001859 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1860 goto attach;
1861 }
1862 }
1863
1864 switch (function) {
1865 case ISCSI_TM_FUNC_ABORT_TASK:
1866 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001867 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001868 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001869 break;
1870 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1871 case ISCSI_TM_FUNC_CLEAR_ACA:
1872 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1873 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1874 break;
1875 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1876 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001877 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1878 goto attach;
1879 }
1880 break;
1881 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1882 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001883 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1884 goto attach;
1885 }
1886 break;
1887 case ISCSI_TM_FUNC_TASK_REASSIGN:
1888 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1889 /*
1890 * Perform sanity checks on the ExpDataSN only if the
1891 * TASK_REASSIGN was successful.
1892 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001893 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001894 break;
1895
1896 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001897 return iscsit_add_reject_cmd(cmd,
1898 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001899 break;
1900 default:
1901 pr_err("Unknown TMR function: 0x%02x, protocol"
1902 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001903 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1904 goto attach;
1905 }
1906
1907 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1908 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1909 se_tmr->call_transport = 1;
1910attach:
1911 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001912 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001913 spin_unlock_bh(&conn->cmd_lock);
1914
1915 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001916 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001917 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1918 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001919 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001920 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001921 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001922 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001923 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001924 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001925
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001926 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001927 return 0;
1928 /*
1929 * Found the referenced task, send to transport for processing.
1930 */
1931 if (se_tmr->call_transport)
1932 return transport_generic_handle_tmr(&cmd->se_cmd);
1933
1934 /*
1935 * Could not find the referenced LUN, task, or Task Management
1936 * command not authorized or supported. Change state and
1937 * let the tx_thread send the response.
1938 *
1939 * For connection recovery, this is also the default action for
1940 * TMR TASK_REASSIGN.
1941 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001942 if (sess_ref) {
1943 pr_debug("Handle TMR, using sess_ref=true check\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +02001944 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001945 }
1946
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001947 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1948 return 0;
1949}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001950EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001951
1952/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001953int
1954iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1955 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001956{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001957 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001958
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001959 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001960 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001961 "greater than MaxXmitDataSegmentLength %u.\n",
1962 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001963 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1964 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001965 }
1966
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08001967 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1968 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1969 pr_err("Multi sequence text commands currently not supported\n");
1970 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1971 (unsigned char *)hdr);
1972 }
1973
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001974 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1975 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1976 hdr->exp_statsn, payload_length);
1977
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001978 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1979 cmd->i_state = ISTATE_SEND_TEXTRSP;
1980 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1981 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1982 cmd->targ_xfer_tag = 0xFFFFFFFF;
1983 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1984 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1985 cmd->data_direction = DMA_NONE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001986 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001987
1988 return 0;
1989}
1990EXPORT_SYMBOL(iscsit_setup_text_cmd);
1991
1992int
1993iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1994 struct iscsi_text *hdr)
1995{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001996 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001997 int cmdsn_ret;
1998
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001999 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002000 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2001 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2002 pr_err("Unable to locate text_in buffer for sendtargets"
2003 " discovery\n");
2004 goto reject;
2005 }
2006 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002007 }
2008 if (strncmp("SendTargets", text_in, 11) != 0) {
2009 pr_err("Received Text Data that is not"
2010 " SendTargets, cannot continue.\n");
2011 goto reject;
2012 }
2013 text_ptr = strchr(text_in, '=');
2014 if (!text_ptr) {
2015 pr_err("No \"=\" separator found in Text Data,"
2016 " cannot continue.\n");
2017 goto reject;
2018 }
2019 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002020 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002021 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2022 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002023 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002024 } else {
2025 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2026 goto reject;
2027 }
2028
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002029 spin_lock_bh(&conn->cmd_lock);
2030 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2031 spin_unlock_bh(&conn->cmd_lock);
2032
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002033empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002034 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2035
2036 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002037 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2038 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002039 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002040 return -1;
2041
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002042 return 0;
2043 }
2044
2045 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002046
2047reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002048 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2049 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002050}
2051EXPORT_SYMBOL(iscsit_process_text_cmd);
2052
2053static int
2054iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2055 unsigned char *buf)
2056{
2057 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2058 char *text_in = NULL;
2059 u32 payload_length = ntoh24(hdr->dlength);
2060 int rx_size, rc;
2061
2062 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2063 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002064 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002065
2066 rx_size = payload_length;
2067 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002068 u32 checksum = 0, data_crc = 0;
2069 u32 padding = 0, pad_bytes = 0;
2070 int niov = 0, rx_got;
2071 struct kvec iov[3];
2072
2073 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002074 if (!text_in) {
2075 pr_err("Unable to allocate memory for"
2076 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002077 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002078 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002079 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002080
2081 memset(iov, 0, 3 * sizeof(struct kvec));
2082 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002083 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002084
2085 padding = ((-payload_length) & 3);
2086 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002087 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002088 iov[niov++].iov_len = padding;
2089 rx_size += padding;
2090 pr_debug("Receiving %u additional bytes"
2091 " for padding.\n", padding);
2092 }
2093 if (conn->conn_ops->DataDigest) {
2094 iov[niov].iov_base = &checksum;
2095 iov[niov++].iov_len = ISCSI_CRC_LEN;
2096 rx_size += ISCSI_CRC_LEN;
2097 }
2098
2099 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002100 if (rx_got != rx_size)
2101 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002102
2103 if (conn->conn_ops->DataDigest) {
2104 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002105 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002106 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002107 (u8 *)&data_crc);
2108
2109 if (checksum != data_crc) {
2110 pr_err("Text data CRC32C DataDigest"
2111 " 0x%08x does not match computed"
2112 " 0x%08x\n", checksum, data_crc);
2113 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2114 pr_err("Unable to recover from"
2115 " Text Data digest failure while in"
2116 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002117 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002118 } else {
2119 /*
2120 * Silently drop this PDU and let the
2121 * initiator plug the CmdSN gap.
2122 */
2123 pr_debug("Dropping Text"
2124 " Command CmdSN: 0x%08x due to"
2125 " DataCRC error.\n", hdr->cmdsn);
2126 kfree(text_in);
2127 return 0;
2128 }
2129 } else {
2130 pr_debug("Got CRC32C DataDigest"
2131 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002132 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002133 }
2134 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002135 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002136 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002137 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002138 }
2139
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002140 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002141
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002142reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002143 kfree(cmd->text_in_ptr);
2144 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002145 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002146}
2147
2148int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2149{
2150 struct iscsi_conn *conn_p;
2151 struct iscsi_session *sess = conn->sess;
2152
2153 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2154 " for SID: %u.\n", conn->cid, conn->sess->sid);
2155
2156 atomic_set(&sess->session_logout, 1);
2157 atomic_set(&conn->conn_logout_remove, 1);
2158 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2159
2160 iscsit_inc_conn_usage_count(conn);
2161 iscsit_inc_session_usage_count(sess);
2162
2163 spin_lock_bh(&sess->conn_lock);
2164 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2165 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2166 continue;
2167
2168 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2169 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2170 }
2171 spin_unlock_bh(&sess->conn_lock);
2172
2173 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2174
2175 return 0;
2176}
2177
2178int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2179{
2180 struct iscsi_conn *l_conn;
2181 struct iscsi_session *sess = conn->sess;
2182
2183 pr_debug("Received logout request CLOSECONNECTION for CID:"
2184 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2185
2186 /*
2187 * A Logout Request with a CLOSECONNECTION reason code for a CID
2188 * can arrive on a connection with a differing CID.
2189 */
2190 if (conn->cid == cmd->logout_cid) {
2191 spin_lock_bh(&conn->state_lock);
2192 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2193 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2194
2195 atomic_set(&conn->conn_logout_remove, 1);
2196 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2197 iscsit_inc_conn_usage_count(conn);
2198
2199 spin_unlock_bh(&conn->state_lock);
2200 } else {
2201 /*
2202 * Handle all different cid CLOSECONNECTION requests in
2203 * iscsit_logout_post_handler_diffcid() as to give enough
2204 * time for any non immediate command's CmdSN to be
2205 * acknowledged on the connection in question.
2206 *
2207 * Here we simply make sure the CID is still around.
2208 */
2209 l_conn = iscsit_get_conn_from_cid(sess,
2210 cmd->logout_cid);
2211 if (!l_conn) {
2212 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2213 iscsit_add_cmd_to_response_queue(cmd, conn,
2214 cmd->i_state);
2215 return 0;
2216 }
2217
2218 iscsit_dec_conn_usage_count(l_conn);
2219 }
2220
2221 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2222
2223 return 0;
2224}
2225
2226int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2227{
2228 struct iscsi_session *sess = conn->sess;
2229
2230 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2231 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2232
2233 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2234 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2235 " while ERL!=2.\n");
2236 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2237 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2238 return 0;
2239 }
2240
2241 if (conn->cid == cmd->logout_cid) {
2242 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2243 " with CID: %hu on CID: %hu, implementation error.\n",
2244 cmd->logout_cid, conn->cid);
2245 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2246 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2247 return 0;
2248 }
2249
2250 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2251
2252 return 0;
2253}
2254
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002255int
2256iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2257 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002258{
2259 int cmdsn_ret, logout_remove = 0;
2260 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002261 struct iscsi_logout *hdr;
2262 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2263
2264 hdr = (struct iscsi_logout *) buf;
2265 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002266
2267 if (tiqn) {
2268 spin_lock(&tiqn->logout_stats.lock);
2269 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2270 tiqn->logout_stats.normal_logouts++;
2271 else
2272 tiqn->logout_stats.abnormal_logouts++;
2273 spin_unlock(&tiqn->logout_stats.lock);
2274 }
2275
2276 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2277 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2278 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2279 hdr->cid, conn->cid);
2280
2281 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2282 pr_err("Received logout request on connection that"
2283 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002284 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002285 return 0;
2286 }
2287
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002288 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2289 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2290 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2291 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2292 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002293 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2294 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2295 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002296 cmd->logout_reason = reason_code;
2297 cmd->data_direction = DMA_NONE;
2298
2299 /*
2300 * We need to sleep in these cases (by returning 1) until the Logout
2301 * Response gets sent in the tx thread.
2302 */
2303 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2304 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002305 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002306 logout_remove = 1;
2307
2308 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002309 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002310 spin_unlock_bh(&conn->cmd_lock);
2311
2312 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002313 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002314
2315 /*
2316 * Immediate commands are executed, well, immediately.
2317 * Non-Immediate Logout Commands are executed in CmdSN order.
2318 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002319 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002320 int ret = iscsit_execute_cmd(cmd, 0);
2321
2322 if (ret < 0)
2323 return ret;
2324 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002325 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002326 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002327 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002328 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2329 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002330 }
2331
2332 return logout_remove;
2333}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002334EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002335
2336static int iscsit_handle_snack(
2337 struct iscsi_conn *conn,
2338 unsigned char *buf)
2339{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002340 struct iscsi_snack *hdr;
2341
2342 hdr = (struct iscsi_snack *) buf;
2343 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002344
2345 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2346 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2347 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2348 hdr->begrun, hdr->runlength, conn->cid);
2349
2350 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2351 pr_err("Initiator sent SNACK request while in"
2352 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002353 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2354 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002355 }
2356 /*
2357 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2358 * call from inside iscsi_send_recovery_datain_or_r2t().
2359 */
2360 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2361 case 0:
2362 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002363 hdr->itt,
2364 be32_to_cpu(hdr->ttt),
2365 be32_to_cpu(hdr->begrun),
2366 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002367 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002368 return iscsit_handle_status_snack(conn, hdr->itt,
2369 be32_to_cpu(hdr->ttt),
2370 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002371 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002372 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2373 be32_to_cpu(hdr->begrun),
2374 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002375 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2376 /* FIXME: Support R-Data SNACK */
2377 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002378 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2379 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002380 default:
2381 pr_err("Unknown SNACK type 0x%02x, protocol"
2382 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002383 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2384 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002385 }
2386
2387 return 0;
2388}
2389
2390static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2391{
2392 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2393 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2394 wait_for_completion_interruptible_timeout(
2395 &conn->rx_half_close_comp,
2396 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2397 }
2398}
2399
2400static int iscsit_handle_immediate_data(
2401 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002402 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002403 u32 length)
2404{
2405 int iov_ret, rx_got = 0, rx_size = 0;
2406 u32 checksum, iov_count = 0, padding = 0;
2407 struct iscsi_conn *conn = cmd->conn;
2408 struct kvec *iov;
2409
2410 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2411 if (iov_ret < 0)
2412 return IMMEDIATE_DATA_CANNOT_RECOVER;
2413
2414 rx_size = length;
2415 iov_count = iov_ret;
2416 iov = &cmd->iov_data[0];
2417
2418 padding = ((-length) & 3);
2419 if (padding != 0) {
2420 iov[iov_count].iov_base = cmd->pad_bytes;
2421 iov[iov_count++].iov_len = padding;
2422 rx_size += padding;
2423 }
2424
2425 if (conn->conn_ops->DataDigest) {
2426 iov[iov_count].iov_base = &checksum;
2427 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2428 rx_size += ISCSI_CRC_LEN;
2429 }
2430
2431 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2432
2433 iscsit_unmap_iovec(cmd);
2434
2435 if (rx_got != rx_size) {
2436 iscsit_rx_thread_wait_for_tcp(conn);
2437 return IMMEDIATE_DATA_CANNOT_RECOVER;
2438 }
2439
2440 if (conn->conn_ops->DataDigest) {
2441 u32 data_crc;
2442
2443 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2444 cmd->write_data_done, length, padding,
2445 cmd->pad_bytes);
2446
2447 if (checksum != data_crc) {
2448 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2449 " does not match computed 0x%08x\n", checksum,
2450 data_crc);
2451
2452 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2453 pr_err("Unable to recover from"
2454 " Immediate Data digest failure while"
2455 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002456 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002457 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002458 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002459 return IMMEDIATE_DATA_CANNOT_RECOVER;
2460 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002461 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002462 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002463 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002464 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2465 }
2466 } else {
2467 pr_debug("Got CRC32C DataDigest 0x%08x for"
2468 " %u bytes of Immediate Data\n", checksum,
2469 length);
2470 }
2471 }
2472
2473 cmd->write_data_done += length;
2474
Andy Groverebf1d952012-04-03 15:51:24 -07002475 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002476 spin_lock_bh(&cmd->istate_lock);
2477 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2478 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2479 spin_unlock_bh(&cmd->istate_lock);
2480 }
2481
2482 return IMMEDIATE_DATA_NORMAL_OPERATION;
2483}
2484
2485/*
2486 * Called with sess->conn_lock held.
2487 */
2488/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2489 with active network interface */
2490static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2491{
2492 struct iscsi_cmd *cmd;
2493 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002494 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002495
2496 /*
2497 * Only send a Asynchronous Message on connections whos network
2498 * interface is still functional.
2499 */
2500 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2501 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2502 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002503 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002504 break;
2505 }
2506 }
2507
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002508 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002509 return;
2510
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002511 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002512 if (!cmd) {
2513 iscsit_dec_conn_usage_count(conn_p);
2514 return;
2515 }
2516
2517 cmd->logout_cid = conn->cid;
2518 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2519 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2520
2521 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002522 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002523 spin_unlock_bh(&conn_p->cmd_lock);
2524
2525 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2526 iscsit_dec_conn_usage_count(conn_p);
2527}
2528
2529static int iscsit_send_conn_drop_async_message(
2530 struct iscsi_cmd *cmd,
2531 struct iscsi_conn *conn)
2532{
2533 struct iscsi_async *hdr;
2534
2535 cmd->tx_size = ISCSI_HDR_LEN;
2536 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2537
2538 hdr = (struct iscsi_async *) cmd->pdu;
2539 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2540 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002541 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002542 cmd->targ_xfer_tag = 0xFFFFFFFF;
2543 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2544 cmd->stat_sn = conn->stat_sn++;
2545 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2546 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002547 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002548 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2549 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2550 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2551 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2552
2553 if (conn->conn_ops->HeaderDigest) {
2554 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2555
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002556 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2557 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002558
2559 cmd->tx_size += ISCSI_CRC_LEN;
2560 pr_debug("Attaching CRC32C HeaderDigest to"
2561 " Async Message 0x%08x\n", *header_digest);
2562 }
2563
2564 cmd->iov_misc[0].iov_base = cmd->pdu;
2565 cmd->iov_misc[0].iov_len = cmd->tx_size;
2566 cmd->iov_misc_count = 1;
2567
2568 pr_debug("Sending Connection Dropped Async Message StatSN:"
2569 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2570 cmd->logout_cid, conn->cid);
2571 return 0;
2572}
2573
Andy Grover6f3c0e62012-04-03 15:51:09 -07002574static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2575{
2576 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2577 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2578 wait_for_completion_interruptible_timeout(
2579 &conn->tx_half_close_comp,
2580 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2581 }
2582}
2583
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002584static void
2585iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2586 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2587 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002588{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002589 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2590 hdr->flags = datain->flags;
2591 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2592 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2593 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2594 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2595 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2596 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2597 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2598 }
2599 }
2600 hton24(hdr->dlength, datain->length);
2601 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2602 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2603 (struct scsi_lun *)&hdr->lun);
2604 else
2605 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2606
2607 hdr->itt = cmd->init_task_tag;
2608
2609 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2610 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2611 else
2612 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2613 if (set_statsn)
2614 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2615 else
2616 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2617
2618 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002619 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002620 hdr->datasn = cpu_to_be32(datain->data_sn);
2621 hdr->offset = cpu_to_be32(datain->offset);
2622
2623 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2624 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2625 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2626 ntohl(hdr->offset), datain->length, conn->cid);
2627}
2628
2629static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2630{
2631 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002632 struct iscsi_datain datain;
2633 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002634 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002635 u32 iov_count = 0, tx_size = 0;
2636 int eodr = 0, ret, iov_ret;
2637 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002638
2639 memset(&datain, 0, sizeof(struct iscsi_datain));
2640 dr = iscsit_get_datain_values(cmd, &datain);
2641 if (!dr) {
2642 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2643 cmd->init_task_tag);
2644 return -1;
2645 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002646 /*
2647 * Be paranoid and double check the logic for now.
2648 */
Andy Groverebf1d952012-04-03 15:51:24 -07002649 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002650 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2651 " datain.length: %u exceeds cmd->data_length: %u\n",
2652 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002653 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002654 return -1;
2655 }
2656
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002657 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002658 /*
2659 * Special case for successfully execution w/ both DATAIN
2660 * and Sense Data.
2661 */
2662 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2663 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2664 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2665 else {
2666 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2667 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2668 iscsit_increment_maxcmdsn(cmd, conn->sess);
2669 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002670 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002671 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002672 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2673 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002674 }
2675
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002676 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002677
2678 iov = &cmd->iov_data[0];
2679 iov[iov_count].iov_base = cmd->pdu;
2680 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2681 tx_size += ISCSI_HDR_LEN;
2682
2683 if (conn->conn_ops->HeaderDigest) {
2684 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2685
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002686 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2687 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002688
2689 iov[0].iov_len += ISCSI_CRC_LEN;
2690 tx_size += ISCSI_CRC_LEN;
2691
2692 pr_debug("Attaching CRC32 HeaderDigest"
2693 " for DataIN PDU 0x%08x\n", *header_digest);
2694 }
2695
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002696 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2697 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002698 if (iov_ret < 0)
2699 return -1;
2700
2701 iov_count += iov_ret;
2702 tx_size += datain.length;
2703
2704 cmd->padding = ((-datain.length) & 3);
2705 if (cmd->padding) {
2706 iov[iov_count].iov_base = cmd->pad_bytes;
2707 iov[iov_count++].iov_len = cmd->padding;
2708 tx_size += cmd->padding;
2709
2710 pr_debug("Attaching %u padding bytes\n",
2711 cmd->padding);
2712 }
2713 if (conn->conn_ops->DataDigest) {
2714 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2715 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2716
2717 iov[iov_count].iov_base = &cmd->data_crc;
2718 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2719 tx_size += ISCSI_CRC_LEN;
2720
2721 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2722 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2723 }
2724
2725 cmd->iov_data_count = iov_count;
2726 cmd->tx_size = tx_size;
2727
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02002728 ret = iscsit_fe_sendpage_sg(cmd, conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07002729
2730 iscsit_unmap_iovec(cmd);
2731
2732 if (ret < 0) {
2733 iscsit_tx_thread_wait_for_tcp(conn);
2734 return ret;
2735 }
2736
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002737 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002738 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002739 2 : 1;
2740 iscsit_free_datain_req(cmd, dr);
2741 }
2742
Andy Grover6f3c0e62012-04-03 15:51:09 -07002743 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002744}
2745
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002746int
2747iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2748 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002749{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002750 struct iscsi_conn *logout_conn = NULL;
2751 struct iscsi_conn_recovery *cr = NULL;
2752 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002753 /*
2754 * The actual shutting down of Sessions and/or Connections
2755 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2756 * is done in scsi_logout_post_handler().
2757 */
2758 switch (cmd->logout_reason) {
2759 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2760 pr_debug("iSCSI session logout successful, setting"
2761 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2762 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2763 break;
2764 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2765 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2766 break;
2767 /*
2768 * For CLOSECONNECTION logout requests carrying
2769 * a matching logout CID -> local CID, the reference
2770 * for the local CID will have been incremented in
2771 * iscsi_logout_closeconnection().
2772 *
2773 * For CLOSECONNECTION logout requests carrying
2774 * a different CID than the connection it arrived
2775 * on, the connection responding to cmd->logout_cid
2776 * is stopped in iscsit_logout_post_handler_diffcid().
2777 */
2778
2779 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2780 " successful.\n", cmd->logout_cid, conn->cid);
2781 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2782 break;
2783 case ISCSI_LOGOUT_REASON_RECOVERY:
2784 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2785 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2786 break;
2787 /*
2788 * If the connection is still active from our point of view
2789 * force connection recovery to occur.
2790 */
2791 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2792 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002793 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002794 iscsit_connection_reinstatement_rcfr(logout_conn);
2795 iscsit_dec_conn_usage_count(logout_conn);
2796 }
2797
2798 cr = iscsit_get_inactive_connection_recovery_entry(
2799 conn->sess, cmd->logout_cid);
2800 if (!cr) {
2801 pr_err("Unable to locate CID: %hu for"
2802 " REMOVECONNFORRECOVERY Logout Request.\n",
2803 cmd->logout_cid);
2804 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2805 break;
2806 }
2807
2808 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2809
2810 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2811 " for recovery for CID: %hu on CID: %hu successful.\n",
2812 cmd->logout_cid, conn->cid);
2813 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2814 break;
2815 default:
2816 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2817 cmd->logout_reason);
2818 return -1;
2819 }
2820
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002821 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2822 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2823 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002824 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002825 cmd->stat_sn = conn->stat_sn++;
2826 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2827
2828 iscsit_increment_maxcmdsn(cmd, conn->sess);
2829 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002830 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002831
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002832 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2833 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2834 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2835 cmd->logout_cid, conn->cid);
2836
2837 return 0;
2838}
2839EXPORT_SYMBOL(iscsit_build_logout_rsp);
2840
2841static int
2842iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2843{
2844 struct kvec *iov;
2845 int niov = 0, tx_size, rc;
2846
2847 rc = iscsit_build_logout_rsp(cmd, conn,
2848 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2849 if (rc < 0)
2850 return rc;
2851
2852 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002853 iov = &cmd->iov_misc[0];
2854 iov[niov].iov_base = cmd->pdu;
2855 iov[niov++].iov_len = ISCSI_HDR_LEN;
2856
2857 if (conn->conn_ops->HeaderDigest) {
2858 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2859
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002860 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2861 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002862
2863 iov[0].iov_len += ISCSI_CRC_LEN;
2864 tx_size += ISCSI_CRC_LEN;
2865 pr_debug("Attaching CRC32C HeaderDigest to"
2866 " Logout Response 0x%08x\n", *header_digest);
2867 }
2868 cmd->iov_misc_count = niov;
2869 cmd->tx_size = tx_size;
2870
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002871 return 0;
2872}
2873
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002874void
2875iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2876 struct iscsi_nopin *hdr, bool nopout_response)
2877{
2878 hdr->opcode = ISCSI_OP_NOOP_IN;
2879 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2880 hton24(hdr->dlength, cmd->buf_ptr_size);
2881 if (nopout_response)
2882 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2883 hdr->itt = cmd->init_task_tag;
2884 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2885 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2886 conn->stat_sn;
2887 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2888
2889 if (nopout_response)
2890 iscsit_increment_maxcmdsn(cmd, conn->sess);
2891
2892 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002893 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002894
2895 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2896 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2897 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2898 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2899}
2900EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2901
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002902/*
2903 * Unsolicited NOPIN, either requesting a response or not.
2904 */
2905static int iscsit_send_unsolicited_nopin(
2906 struct iscsi_cmd *cmd,
2907 struct iscsi_conn *conn,
2908 int want_response)
2909{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002910 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2911 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002912
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002913 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002914
2915 if (conn->conn_ops->HeaderDigest) {
2916 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2917
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002918 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2919 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002920
2921 tx_size += ISCSI_CRC_LEN;
2922 pr_debug("Attaching CRC32C HeaderDigest to"
2923 " NopIN 0x%08x\n", *header_digest);
2924 }
2925
2926 cmd->iov_misc[0].iov_base = cmd->pdu;
2927 cmd->iov_misc[0].iov_len = tx_size;
2928 cmd->iov_misc_count = 1;
2929 cmd->tx_size = tx_size;
2930
2931 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2932 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2933
Andy Grover6f3c0e62012-04-03 15:51:09 -07002934 ret = iscsit_send_tx_data(cmd, conn, 1);
2935 if (ret < 0) {
2936 iscsit_tx_thread_wait_for_tcp(conn);
2937 return ret;
2938 }
2939
2940 spin_lock_bh(&cmd->istate_lock);
2941 cmd->i_state = want_response ?
2942 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2943 spin_unlock_bh(&cmd->istate_lock);
2944
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002945 return 0;
2946}
2947
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002948static int
2949iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002950{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002951 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002952 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002953 u32 padding = 0;
2954 int niov = 0, tx_size;
2955
2956 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002957
2958 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002959 iov = &cmd->iov_misc[0];
2960 iov[niov].iov_base = cmd->pdu;
2961 iov[niov++].iov_len = ISCSI_HDR_LEN;
2962
2963 if (conn->conn_ops->HeaderDigest) {
2964 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2965
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002966 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2967 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002968
2969 iov[0].iov_len += ISCSI_CRC_LEN;
2970 tx_size += ISCSI_CRC_LEN;
2971 pr_debug("Attaching CRC32C HeaderDigest"
2972 " to NopIn 0x%08x\n", *header_digest);
2973 }
2974
2975 /*
2976 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2977 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2978 */
2979 if (cmd->buf_ptr_size) {
2980 iov[niov].iov_base = cmd->buf_ptr;
2981 iov[niov++].iov_len = cmd->buf_ptr_size;
2982 tx_size += cmd->buf_ptr_size;
2983
2984 pr_debug("Echoing back %u bytes of ping"
2985 " data.\n", cmd->buf_ptr_size);
2986
2987 padding = ((-cmd->buf_ptr_size) & 3);
2988 if (padding != 0) {
2989 iov[niov].iov_base = &cmd->pad_bytes;
2990 iov[niov++].iov_len = padding;
2991 tx_size += padding;
2992 pr_debug("Attaching %u additional"
2993 " padding bytes.\n", padding);
2994 }
2995 if (conn->conn_ops->DataDigest) {
2996 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2997 cmd->buf_ptr, cmd->buf_ptr_size,
2998 padding, (u8 *)&cmd->pad_bytes,
2999 (u8 *)&cmd->data_crc);
3000
3001 iov[niov].iov_base = &cmd->data_crc;
3002 iov[niov++].iov_len = ISCSI_CRC_LEN;
3003 tx_size += ISCSI_CRC_LEN;
3004 pr_debug("Attached DataDigest for %u"
3005 " bytes of ping data, CRC 0x%08x\n",
3006 cmd->buf_ptr_size, cmd->data_crc);
3007 }
3008 }
3009
3010 cmd->iov_misc_count = niov;
3011 cmd->tx_size = tx_size;
3012
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003013 return 0;
3014}
3015
Andy Grover6f3c0e62012-04-03 15:51:09 -07003016static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003017 struct iscsi_cmd *cmd,
3018 struct iscsi_conn *conn)
3019{
3020 int tx_size = 0;
3021 struct iscsi_r2t *r2t;
3022 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003023 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003024
3025 r2t = iscsit_get_r2t_from_list(cmd);
3026 if (!r2t)
3027 return -1;
3028
3029 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3030 memset(hdr, 0, ISCSI_HDR_LEN);
3031 hdr->opcode = ISCSI_OP_R2T;
3032 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3033 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3034 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003035 hdr->itt = cmd->init_task_tag;
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02003036 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003037 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3038 hdr->statsn = cpu_to_be32(conn->stat_sn);
3039 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003040 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003041 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3042 hdr->data_offset = cpu_to_be32(r2t->offset);
3043 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3044
3045 cmd->iov_misc[0].iov_base = cmd->pdu;
3046 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3047 tx_size += ISCSI_HDR_LEN;
3048
3049 if (conn->conn_ops->HeaderDigest) {
3050 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3051
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003052 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3053 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003054
3055 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3056 tx_size += ISCSI_CRC_LEN;
3057 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3058 " PDU 0x%08x\n", *header_digest);
3059 }
3060
3061 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3062 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3063 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3064 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3065 r2t->offset, r2t->xfer_len, conn->cid);
3066
3067 cmd->iov_misc_count = 1;
3068 cmd->tx_size = tx_size;
3069
3070 spin_lock_bh(&cmd->r2t_lock);
3071 r2t->sent_r2t = 1;
3072 spin_unlock_bh(&cmd->r2t_lock);
3073
Andy Grover6f3c0e62012-04-03 15:51:09 -07003074 ret = iscsit_send_tx_data(cmd, conn, 1);
3075 if (ret < 0) {
3076 iscsit_tx_thread_wait_for_tcp(conn);
3077 return ret;
3078 }
3079
3080 spin_lock_bh(&cmd->dataout_timeout_lock);
3081 iscsit_start_dataout_timer(cmd, conn);
3082 spin_unlock_bh(&cmd->dataout_timeout_lock);
3083
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003084 return 0;
3085}
3086
3087/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003088 * @recovery: If called from iscsi_task_reassign_complete_write() for
3089 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003090 */
3091int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003092 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003093 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003094 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003095{
3096 int first_r2t = 1;
3097 u32 offset = 0, xfer_len = 0;
3098
3099 spin_lock_bh(&cmd->r2t_lock);
3100 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3101 spin_unlock_bh(&cmd->r2t_lock);
3102 return 0;
3103 }
3104
Andy Grover8b1e1242012-04-03 15:51:12 -07003105 if (conn->sess->sess_ops->DataSequenceInOrder &&
3106 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003107 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003108
3109 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3110 if (conn->sess->sess_ops->DataSequenceInOrder) {
3111 offset = cmd->r2t_offset;
3112
Andy Grover8b1e1242012-04-03 15:51:12 -07003113 if (first_r2t && recovery) {
3114 int new_data_end = offset +
3115 conn->sess->sess_ops->MaxBurstLength -
3116 cmd->next_burst_len;
3117
Andy Groverebf1d952012-04-03 15:51:24 -07003118 if (new_data_end > cmd->se_cmd.data_length)
3119 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003120 else
3121 xfer_len =
3122 conn->sess->sess_ops->MaxBurstLength -
3123 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003124 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003125 int new_data_end = offset +
3126 conn->sess->sess_ops->MaxBurstLength;
3127
Andy Groverebf1d952012-04-03 15:51:24 -07003128 if (new_data_end > cmd->se_cmd.data_length)
3129 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003130 else
3131 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003132 }
3133 cmd->r2t_offset += xfer_len;
3134
Andy Groverebf1d952012-04-03 15:51:24 -07003135 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003136 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3137 } else {
3138 struct iscsi_seq *seq;
3139
3140 seq = iscsit_get_seq_holder_for_r2t(cmd);
3141 if (!seq) {
3142 spin_unlock_bh(&cmd->r2t_lock);
3143 return -1;
3144 }
3145
3146 offset = seq->offset;
3147 xfer_len = seq->xfer_len;
3148
3149 if (cmd->seq_send_order == cmd->seq_count)
3150 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3151 }
3152 cmd->outstanding_r2ts++;
3153 first_r2t = 0;
3154
3155 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3156 spin_unlock_bh(&cmd->r2t_lock);
3157 return -1;
3158 }
3159
3160 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3161 break;
3162 }
3163 spin_unlock_bh(&cmd->r2t_lock);
3164
3165 return 0;
3166}
3167
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003168void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3169 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003170{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003171 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003172 cmd->stat_sn = conn->stat_sn++;
3173
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003174 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003175
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003176 memset(hdr, 0, ISCSI_HDR_LEN);
3177 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3178 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3179 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3180 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003181 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003182 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3183 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003184 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003185 }
3186 hdr->response = cmd->iscsi_response;
3187 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003188 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003189 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3190
3191 iscsit_increment_maxcmdsn(cmd, conn->sess);
3192 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003193 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003194
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003195 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3196 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3197 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3198 cmd->se_cmd.scsi_status, conn->cid);
3199}
3200EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3201
3202static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3203{
3204 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3205 struct kvec *iov;
3206 u32 padding = 0, tx_size = 0;
3207 int iov_count = 0;
3208 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3209
3210 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3211
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003212 iov = &cmd->iov_misc[0];
3213 iov[iov_count].iov_base = cmd->pdu;
3214 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3215 tx_size += ISCSI_HDR_LEN;
3216
3217 /*
3218 * Attach SENSE DATA payload to iSCSI Response PDU
3219 */
3220 if (cmd->se_cmd.sense_buffer &&
3221 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3222 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003223 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3224 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3225
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003226 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003227 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003228 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003229 iov[iov_count++].iov_len =
3230 (cmd->se_cmd.scsi_sense_length + padding);
3231 tx_size += cmd->se_cmd.scsi_sense_length;
3232
3233 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003234 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003235 cmd->se_cmd.scsi_sense_length, 0, padding);
3236 tx_size += padding;
3237 pr_debug("Adding %u bytes of padding to"
3238 " SENSE.\n", padding);
3239 }
3240
3241 if (conn->conn_ops->DataDigest) {
3242 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003243 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003244 (cmd->se_cmd.scsi_sense_length + padding),
3245 0, NULL, (u8 *)&cmd->data_crc);
3246
3247 iov[iov_count].iov_base = &cmd->data_crc;
3248 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3249 tx_size += ISCSI_CRC_LEN;
3250
3251 pr_debug("Attaching CRC32 DataDigest for"
3252 " SENSE, %u bytes CRC 0x%08x\n",
3253 (cmd->se_cmd.scsi_sense_length + padding),
3254 cmd->data_crc);
3255 }
3256
3257 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3258 " Response PDU\n",
3259 cmd->se_cmd.scsi_sense_length);
3260 }
3261
3262 if (conn->conn_ops->HeaderDigest) {
3263 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3264
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003265 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3266 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003267
3268 iov[0].iov_len += ISCSI_CRC_LEN;
3269 tx_size += ISCSI_CRC_LEN;
3270 pr_debug("Attaching CRC32 HeaderDigest for Response"
3271 " PDU 0x%08x\n", *header_digest);
3272 }
3273
3274 cmd->iov_misc_count = iov_count;
3275 cmd->tx_size = tx_size;
3276
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003277 return 0;
3278}
3279
3280static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3281{
3282 switch (se_tmr->response) {
3283 case TMR_FUNCTION_COMPLETE:
3284 return ISCSI_TMF_RSP_COMPLETE;
3285 case TMR_TASK_DOES_NOT_EXIST:
3286 return ISCSI_TMF_RSP_NO_TASK;
3287 case TMR_LUN_DOES_NOT_EXIST:
3288 return ISCSI_TMF_RSP_NO_LUN;
3289 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3290 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003291 case TMR_FUNCTION_REJECTED:
3292 default:
3293 return ISCSI_TMF_RSP_REJECTED;
3294 }
3295}
3296
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003297void
3298iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3299 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003300{
3301 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003302
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003303 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003304 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003305 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003306 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003307 cmd->stat_sn = conn->stat_sn++;
3308 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3309
3310 iscsit_increment_maxcmdsn(cmd, conn->sess);
3311 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003312 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003313
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003314 pr_debug("Built Task Management Response ITT: 0x%08x,"
3315 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3316 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3317}
3318EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3319
3320static int
3321iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3322{
3323 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3324 u32 tx_size = 0;
3325
3326 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3327
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003328 cmd->iov_misc[0].iov_base = cmd->pdu;
3329 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3330 tx_size += ISCSI_HDR_LEN;
3331
3332 if (conn->conn_ops->HeaderDigest) {
3333 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3334
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003335 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3336 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003337
3338 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3339 tx_size += ISCSI_CRC_LEN;
3340 pr_debug("Attaching CRC32 HeaderDigest for Task"
3341 " Mgmt Response PDU 0x%08x\n", *header_digest);
3342 }
3343
3344 cmd->iov_misc_count = 1;
3345 cmd->tx_size = tx_size;
3346
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003347 return 0;
3348}
3349
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003350static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3351{
3352 bool ret = false;
3353
3354 if (np->np_sockaddr.ss_family == AF_INET6) {
3355 const struct sockaddr_in6 sin6 = {
3356 .sin6_addr = IN6ADDR_ANY_INIT };
3357 struct sockaddr_in6 *sock_in6 =
3358 (struct sockaddr_in6 *)&np->np_sockaddr;
3359
3360 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3361 sin6.sin6_addr.s6_addr, 16))
3362 ret = true;
3363 } else {
3364 struct sockaddr_in * sock_in =
3365 (struct sockaddr_in *)&np->np_sockaddr;
3366
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003367 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003368 ret = true;
3369 }
3370
3371 return ret;
3372}
3373
Andy Grover8b1e1242012-04-03 15:51:12 -07003374#define SENDTARGETS_BUF_LIMIT 32768U
3375
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003376static int
3377iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003378 enum iscsit_transport_type network_transport,
3379 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003380{
3381 char *payload = NULL;
3382 struct iscsi_conn *conn = cmd->conn;
3383 struct iscsi_portal_group *tpg;
3384 struct iscsi_tiqn *tiqn;
3385 struct iscsi_tpg_np *tpg_np;
3386 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003387 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003388 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003389 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
David Disseldorpa6415cd2015-08-01 00:10:12 -07003390 bool active;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003391
Sagi Grimbergbe7dcfb2015-01-26 12:49:06 +02003392 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003393 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003394
3395 payload = kzalloc(buffer_len, GFP_KERNEL);
3396 if (!payload) {
3397 pr_err("Unable to allocate memory for sendtargets"
3398 " response.\n");
3399 return -ENOMEM;
3400 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003401 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003402 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003403 * explicit case..
3404 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003405 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003406 text_ptr = strchr(text_in, '=');
3407 if (!text_ptr) {
3408 pr_err("Unable to locate '=' string in text_in:"
3409 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003410 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003411 return -EINVAL;
3412 }
3413 /*
3414 * Skip over '=' character..
3415 */
3416 text_ptr += 1;
3417 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003418
3419 spin_lock(&tiqn_lock);
3420 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003421 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003422 strcmp(tiqn->tiqn, text_ptr)) {
3423 continue;
3424 }
3425
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003426 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003427
3428 spin_lock(&tiqn->tiqn_tpg_lock);
3429 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3430
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003431 /* If demo_mode_discovery=0 and generate_node_acls=0
3432 * (demo mode dislabed) do not return
3433 * TargetName+TargetAddress unless a NodeACL exists.
3434 */
3435
3436 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3437 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3438 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3439 cmd->conn->sess->sess_ops->InitiatorName))) {
3440 continue;
3441 }
3442
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003443 spin_lock(&tpg->tpg_state_lock);
David Disseldorpa6415cd2015-08-01 00:10:12 -07003444 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003445 spin_unlock(&tpg->tpg_state_lock);
3446
David Disseldorpa6415cd2015-08-01 00:10:12 -07003447 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3448 continue;
3449
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003450 spin_lock(&tpg->tpg_np_lock);
3451 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3452 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003453 struct iscsi_np *np = tpg_np->tpg_np;
3454 bool inaddr_any = iscsit_check_inaddr_any(np);
Andy Grover13a3cf02015-08-24 10:26:06 -07003455 struct sockaddr_storage *sockaddr;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003456
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003457 if (np->np_network_transport != network_transport)
3458 continue;
3459
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003460 if (!target_name_printed) {
3461 len = sprintf(buf, "TargetName=%s",
3462 tiqn->tiqn);
3463 len += 1;
3464
3465 if ((len + payload_len) > buffer_len) {
3466 spin_unlock(&tpg->tpg_np_lock);
3467 spin_unlock(&tiqn->tiqn_tpg_lock);
3468 end_of_buf = 1;
3469 goto eob;
3470 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003471
3472 if (skip_bytes && len <= skip_bytes) {
3473 skip_bytes -= len;
3474 } else {
3475 memcpy(payload + payload_len, buf, len);
3476 payload_len += len;
3477 target_name_printed = 1;
3478 if (len > skip_bytes)
3479 skip_bytes = 0;
3480 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003481 }
3482
Andy Grover69d75572015-08-24 10:26:04 -07003483 if (inaddr_any)
3484 sockaddr = &conn->local_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003485 else
Andy Grover69d75572015-08-24 10:26:04 -07003486 sockaddr = &np->np_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003487
Andy Grover69d75572015-08-24 10:26:04 -07003488 len = sprintf(buf, "TargetAddress="
3489 "%pISpc,%hu",
3490 sockaddr,
3491 tpg->tpgt);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003492 len += 1;
3493
3494 if ((len + payload_len) > buffer_len) {
3495 spin_unlock(&tpg->tpg_np_lock);
3496 spin_unlock(&tiqn->tiqn_tpg_lock);
3497 end_of_buf = 1;
3498 goto eob;
3499 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003500
3501 if (skip_bytes && len <= skip_bytes) {
3502 skip_bytes -= len;
3503 } else {
3504 memcpy(payload + payload_len, buf, len);
3505 payload_len += len;
3506 if (len > skip_bytes)
3507 skip_bytes = 0;
3508 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003509 }
3510 spin_unlock(&tpg->tpg_np_lock);
3511 }
3512 spin_unlock(&tiqn->tiqn_tpg_lock);
3513eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003514 if (end_of_buf) {
3515 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003516 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003517 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003518
Andy Grover8060b8d2015-01-09 15:13:08 -08003519 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003520 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003521 }
3522 spin_unlock(&tiqn_lock);
3523
3524 cmd->buf_ptr = payload;
3525
3526 return payload_len;
3527}
3528
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003529int
3530iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003531 struct iscsi_text_rsp *hdr,
3532 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003533{
3534 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003535 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003536
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003537 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3538 cmd->read_data_done,
3539 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003540 if (text_length < 0)
3541 return text_length;
3542
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003543 if (completed) {
3544 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3545 } else {
3546 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3547 cmd->read_data_done += text_length;
3548 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3549 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3550 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003551 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003552 padding = ((-text_length) & 3);
3553 hton24(hdr->dlength, text_length);
3554 hdr->itt = cmd->init_task_tag;
3555 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3556 cmd->stat_sn = conn->stat_sn++;
3557 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3558
3559 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003560 /*
3561 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3562 * correctly increment MaxCmdSN for each response answering a
3563 * non immediate text request with a valid CmdSN.
3564 */
3565 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003566 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003567 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003568
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003569 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3570 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3571 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3572 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3573 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003574
3575 return text_length + padding;
3576}
3577EXPORT_SYMBOL(iscsit_build_text_rsp);
3578
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003579static int iscsit_send_text_rsp(
3580 struct iscsi_cmd *cmd,
3581 struct iscsi_conn *conn)
3582{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003583 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003584 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003585 u32 tx_size = 0;
3586 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003587
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003588 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003589 if (rc < 0)
3590 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003591
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003592 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003593 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003594 iov[iov_count].iov_base = cmd->pdu;
3595 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3596 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003597 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003598
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003599 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003600
3601 if (conn->conn_ops->HeaderDigest) {
3602 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3603
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003604 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3605 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003606
3607 iov[0].iov_len += ISCSI_CRC_LEN;
3608 tx_size += ISCSI_CRC_LEN;
3609 pr_debug("Attaching CRC32 HeaderDigest for"
3610 " Text Response PDU 0x%08x\n", *header_digest);
3611 }
3612
3613 if (conn->conn_ops->DataDigest) {
3614 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003615 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003616 0, NULL, (u8 *)&cmd->data_crc);
3617
3618 iov[iov_count].iov_base = &cmd->data_crc;
3619 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3620 tx_size += ISCSI_CRC_LEN;
3621
3622 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003623 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003624 cmd->data_crc);
3625 }
3626
3627 cmd->iov_misc_count = iov_count;
3628 cmd->tx_size = tx_size;
3629
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003630 return 0;
3631}
3632
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003633void
3634iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3635 struct iscsi_reject *hdr)
3636{
3637 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003638 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003639 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3640 hton24(hdr->dlength, ISCSI_HDR_LEN);
3641 hdr->ffffffff = cpu_to_be32(0xffffffff);
3642 cmd->stat_sn = conn->stat_sn++;
3643 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3644 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003645 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003646
3647}
3648EXPORT_SYMBOL(iscsit_build_reject);
3649
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003650static int iscsit_send_reject(
3651 struct iscsi_cmd *cmd,
3652 struct iscsi_conn *conn)
3653{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003654 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003655 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003656 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003657
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003658 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003659
3660 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003661 iov[iov_count].iov_base = cmd->pdu;
3662 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3663 iov[iov_count].iov_base = cmd->buf_ptr;
3664 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3665
3666 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3667
3668 if (conn->conn_ops->HeaderDigest) {
3669 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3670
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003671 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3672 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003673
3674 iov[0].iov_len += ISCSI_CRC_LEN;
3675 tx_size += ISCSI_CRC_LEN;
3676 pr_debug("Attaching CRC32 HeaderDigest for"
3677 " REJECT PDU 0x%08x\n", *header_digest);
3678 }
3679
3680 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003681 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3682 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003683
3684 iov[iov_count].iov_base = &cmd->data_crc;
3685 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3686 tx_size += ISCSI_CRC_LEN;
3687 pr_debug("Attaching CRC32 DataDigest for REJECT"
3688 " PDU 0x%08x\n", cmd->data_crc);
3689 }
3690
3691 cmd->iov_misc_count = iov_count;
3692 cmd->tx_size = tx_size;
3693
3694 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3695 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3696
3697 return 0;
3698}
3699
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003700void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3701{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003702 int ord, cpu;
3703 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003704 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3705 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003706 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003707 * Here we use bitmap_id to determine which CPU that this
3708 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003709 * execute upon.
3710 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003711 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003712 for_each_online_cpu(cpu) {
3713 if (ord-- == 0) {
3714 cpumask_set_cpu(cpu, conn->conn_cpumask);
3715 return;
3716 }
3717 }
3718 /*
3719 * This should never be reached..
3720 */
3721 dump_stack();
3722 cpumask_setall(conn->conn_cpumask);
3723}
3724
3725static inline void iscsit_thread_check_cpumask(
3726 struct iscsi_conn *conn,
3727 struct task_struct *p,
3728 int mode)
3729{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003730 /*
3731 * mode == 1 signals iscsi_target_tx_thread() usage.
3732 * mode == 0 signals iscsi_target_rx_thread() usage.
3733 */
3734 if (mode == 1) {
3735 if (!conn->conn_tx_reset_cpumask)
3736 return;
3737 conn->conn_tx_reset_cpumask = 0;
3738 } else {
3739 if (!conn->conn_rx_reset_cpumask)
3740 return;
3741 conn->conn_rx_reset_cpumask = 0;
3742 }
3743 /*
3744 * Update the CPU mask for this single kthread so that
3745 * both TX and RX kthreads are scheduled to run on the
3746 * same CPU.
3747 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003748 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3749}
3750
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003751static int
3752iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003753{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003754 int ret;
3755
3756 switch (state) {
3757 case ISTATE_SEND_R2T:
3758 ret = iscsit_send_r2t(cmd, conn);
3759 if (ret < 0)
3760 goto err;
3761 break;
3762 case ISTATE_REMOVE:
3763 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003764 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003765 spin_unlock_bh(&conn->cmd_lock);
3766
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003767 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003768 break;
3769 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3770 iscsit_mod_nopin_response_timer(conn);
3771 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3772 if (ret < 0)
3773 goto err;
3774 break;
3775 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3776 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3777 if (ret < 0)
3778 goto err;
3779 break;
3780 default:
3781 pr_err("Unknown Opcode: 0x%02x ITT:"
3782 " 0x%08x, i_state: %d on CID: %hu\n",
3783 cmd->iscsi_opcode, cmd->init_task_tag, state,
3784 conn->cid);
3785 goto err;
3786 }
3787
3788 return 0;
3789
3790err:
3791 return -1;
3792}
3793
3794static int
3795iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3796{
3797 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003798 struct iscsi_queue_req *qr;
3799 struct iscsi_cmd *cmd;
3800 u8 state;
3801 int ret;
3802
3803 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3804 atomic_set(&conn->check_immediate_queue, 0);
3805 cmd = qr->cmd;
3806 state = qr->state;
3807 kmem_cache_free(lio_qr_cache, qr);
3808
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003809 ret = t->iscsit_immediate_queue(conn, cmd, state);
3810 if (ret < 0)
3811 return ret;
3812 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003813
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003814 return 0;
3815}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003816
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003817static int
3818iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3819{
3820 int ret;
3821
3822check_rsp_state:
3823 switch (state) {
3824 case ISTATE_SEND_DATAIN:
3825 ret = iscsit_send_datain(cmd, conn);
3826 if (ret < 0)
3827 goto err;
3828 else if (!ret)
3829 /* more drs */
3830 goto check_rsp_state;
3831 else if (ret == 1) {
3832 /* all done */
3833 spin_lock_bh(&cmd->istate_lock);
3834 cmd->i_state = ISTATE_SENT_STATUS;
3835 spin_unlock_bh(&cmd->istate_lock);
3836
3837 if (atomic_read(&conn->check_immediate_queue))
3838 return 1;
3839
3840 return 0;
3841 } else if (ret == 2) {
3842 /* Still must send status,
3843 SCF_TRANSPORT_TASK_SENSE was set */
3844 spin_lock_bh(&cmd->istate_lock);
3845 cmd->i_state = ISTATE_SEND_STATUS;
3846 spin_unlock_bh(&cmd->istate_lock);
3847 state = ISTATE_SEND_STATUS;
3848 goto check_rsp_state;
3849 }
3850
3851 break;
3852 case ISTATE_SEND_STATUS:
3853 case ISTATE_SEND_STATUS_RECOVERY:
3854 ret = iscsit_send_response(cmd, conn);
3855 break;
3856 case ISTATE_SEND_LOGOUTRSP:
3857 ret = iscsit_send_logout(cmd, conn);
3858 break;
3859 case ISTATE_SEND_ASYNCMSG:
3860 ret = iscsit_send_conn_drop_async_message(
3861 cmd, conn);
3862 break;
3863 case ISTATE_SEND_NOPIN:
3864 ret = iscsit_send_nopin(cmd, conn);
3865 break;
3866 case ISTATE_SEND_REJECT:
3867 ret = iscsit_send_reject(cmd, conn);
3868 break;
3869 case ISTATE_SEND_TASKMGTRSP:
3870 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3871 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003872 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003873 ret = iscsit_tmr_post_handler(cmd, conn);
3874 if (ret != 0)
3875 iscsit_fall_back_to_erl0(conn->sess);
3876 break;
3877 case ISTATE_SEND_TEXTRSP:
3878 ret = iscsit_send_text_rsp(cmd, conn);
3879 break;
3880 default:
3881 pr_err("Unknown Opcode: 0x%02x ITT:"
3882 " 0x%08x, i_state: %d on CID: %hu\n",
3883 cmd->iscsi_opcode, cmd->init_task_tag,
3884 state, conn->cid);
3885 goto err;
3886 }
3887 if (ret < 0)
3888 goto err;
3889
3890 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3891 iscsit_tx_thread_wait_for_tcp(conn);
3892 iscsit_unmap_iovec(cmd);
3893 goto err;
3894 }
3895 iscsit_unmap_iovec(cmd);
3896
3897 switch (state) {
3898 case ISTATE_SEND_LOGOUTRSP:
3899 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003900 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003901 /* fall through */
3902 case ISTATE_SEND_STATUS:
3903 case ISTATE_SEND_ASYNCMSG:
3904 case ISTATE_SEND_NOPIN:
3905 case ISTATE_SEND_STATUS_RECOVERY:
3906 case ISTATE_SEND_TEXTRSP:
3907 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003908 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003909 spin_lock_bh(&cmd->istate_lock);
3910 cmd->i_state = ISTATE_SENT_STATUS;
3911 spin_unlock_bh(&cmd->istate_lock);
3912 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003913 default:
3914 pr_err("Unknown Opcode: 0x%02x ITT:"
3915 " 0x%08x, i_state: %d on CID: %hu\n",
3916 cmd->iscsi_opcode, cmd->init_task_tag,
3917 cmd->i_state, conn->cid);
3918 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003919 }
3920
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003921 if (atomic_read(&conn->check_immediate_queue))
3922 return 1;
3923
Andy Grover6f3c0e62012-04-03 15:51:09 -07003924 return 0;
3925
3926err:
3927 return -1;
3928}
3929
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003930static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003931{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003932 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003933 struct iscsi_queue_req *qr;
3934 struct iscsi_cmd *cmd;
3935 u8 state;
3936 int ret;
3937
3938 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3939 cmd = qr->cmd;
3940 state = qr->state;
3941 kmem_cache_free(lio_qr_cache, qr);
3942
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003943 ret = t->iscsit_response_queue(conn, cmd, state);
3944 if (ret == 1 || ret < 0)
3945 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003946 }
3947
3948 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003949}
3950
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003951int iscsi_target_tx_thread(void *arg)
3952{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003953 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003954 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003955 /*
3956 * Allow ourselves to be interrupted by SIGINT so that a
3957 * connection recovery / failure event can be triggered externally.
3958 */
3959 allow_signal(SIGINT);
3960
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003961 while (!kthread_should_stop()) {
3962 /*
3963 * Ensure that both TX and RX per connection kthreads
3964 * are scheduled to run on the same CPU.
3965 */
3966 iscsit_thread_check_cpumask(conn, current, 1);
3967
Roland Dreierd5627ac2012-10-31 09:16:46 -07003968 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003969 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003970
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003971 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003972 goto transport_err;
3973
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003974get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003975 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003976 if (ret < 0)
3977 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003978
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003979 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003980 if (ret == 1)
3981 goto get_immediate;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003982 else if (ret == -ECONNRESET)
3983 goto out;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003984 else if (ret < 0)
3985 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003986 }
3987
3988transport_err:
Nicholas Bellingere5419862015-07-22 23:14:19 -07003989 /*
3990 * Avoid the normal connection failure code-path if this connection
3991 * is still within LOGIN mode, and iscsi_np process context is
3992 * responsible for cleaning up the early connection failure.
3993 */
3994 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
3995 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003996out:
3997 return 0;
3998}
3999
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004000static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
4001{
4002 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
4003 struct iscsi_cmd *cmd;
4004 int ret = 0;
4005
4006 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
4007 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004008 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004009 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004010 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004011
4012 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
4013 break;
4014 case ISCSI_OP_SCSI_DATA_OUT:
4015 ret = iscsit_handle_data_out(conn, buf);
4016 break;
4017 case ISCSI_OP_NOOP_OUT:
4018 cmd = NULL;
4019 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
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_nop_out(conn, cmd, buf);
4025 break;
4026 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004027 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004028 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004029 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004030
4031 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
4032 break;
4033 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02004034 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
4035 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
4036 if (!cmd)
4037 goto reject;
4038 } else {
4039 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4040 if (!cmd)
4041 goto reject;
4042 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004043
4044 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004045 break;
4046 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004047 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004048 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004049 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004050
4051 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4052 if (ret > 0)
4053 wait_for_completion_timeout(&conn->conn_logout_comp,
4054 SECONDS_FOR_LOGOUT_COMP * HZ);
4055 break;
4056 case ISCSI_OP_SNACK:
4057 ret = iscsit_handle_snack(conn, buf);
4058 break;
4059 default:
4060 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4061 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4062 pr_err("Cannot recover from unknown"
4063 " opcode while ERL=0, closing iSCSI connection.\n");
4064 return -1;
4065 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02004066 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
4067 " closing iSCSI connection.\n");
4068 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004069 break;
4070 }
4071
4072 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004073reject:
4074 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004075}
4076
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08004077static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
4078{
4079 bool ret;
4080
4081 spin_lock_bh(&conn->state_lock);
4082 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
4083 spin_unlock_bh(&conn->state_lock);
4084
4085 return ret;
4086}
4087
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004088int iscsi_target_rx_thread(void *arg)
4089{
Nicholas Bellingere5419862015-07-22 23:14:19 -07004090 int ret, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004091 u8 buffer[ISCSI_HDR_LEN], opcode;
4092 u32 checksum = 0, digest = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004093 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004094 struct kvec iov;
4095 /*
4096 * Allow ourselves to be interrupted by SIGINT so that a
4097 * connection recovery / failure event can be triggered externally.
4098 */
4099 allow_signal(SIGINT);
Nicholas Bellingere5419862015-07-22 23:14:19 -07004100 /*
4101 * Wait for iscsi_post_login_handler() to complete before allowing
4102 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4103 */
4104 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08004105 if (rc < 0 || iscsi_target_check_conn_state(conn))
Nicholas Bellingere5419862015-07-22 23:14:19 -07004106 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004107
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004108 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4109 struct completion comp;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004110
4111 init_completion(&comp);
4112 rc = wait_for_completion_interruptible(&comp);
4113 if (rc < 0)
4114 goto transport_err;
4115
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004116 goto transport_err;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004117 }
4118
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004119 while (!kthread_should_stop()) {
4120 /*
4121 * Ensure that both TX and RX per connection kthreads
4122 * are scheduled to run on the same CPU.
4123 */
4124 iscsit_thread_check_cpumask(conn, current, 0);
4125
4126 memset(buffer, 0, ISCSI_HDR_LEN);
4127 memset(&iov, 0, sizeof(struct kvec));
4128
4129 iov.iov_base = buffer;
4130 iov.iov_len = ISCSI_HDR_LEN;
4131
4132 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4133 if (ret != ISCSI_HDR_LEN) {
4134 iscsit_rx_thread_wait_for_tcp(conn);
4135 goto transport_err;
4136 }
4137
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004138 if (conn->conn_ops->HeaderDigest) {
4139 iov.iov_base = &digest;
4140 iov.iov_len = ISCSI_CRC_LEN;
4141
4142 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4143 if (ret != ISCSI_CRC_LEN) {
4144 iscsit_rx_thread_wait_for_tcp(conn);
4145 goto transport_err;
4146 }
4147
4148 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4149 buffer, ISCSI_HDR_LEN,
4150 0, NULL, (u8 *)&checksum);
4151
4152 if (digest != checksum) {
4153 pr_err("HeaderDigest CRC32C failed,"
4154 " received 0x%08x, computed 0x%08x\n",
4155 digest, checksum);
4156 /*
4157 * Set the PDU to 0xff so it will intentionally
4158 * hit default in the switch below.
4159 */
4160 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004161 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004162 } else {
4163 pr_debug("Got HeaderDigest CRC32C"
4164 " 0x%08x\n", checksum);
4165 }
4166 }
4167
4168 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4169 goto transport_err;
4170
4171 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4172
4173 if (conn->sess->sess_ops->SessionType &&
4174 ((!(opcode & ISCSI_OP_TEXT)) ||
4175 (!(opcode & ISCSI_OP_LOGOUT)))) {
4176 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4177 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004178 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4179 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004180 goto transport_err;
4181 }
4182
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004183 ret = iscsi_target_rx_opcode(conn, buffer);
4184 if (ret < 0)
4185 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004186 }
4187
4188transport_err:
4189 if (!signal_pending(current))
4190 atomic_set(&conn->transport_failed, 1);
4191 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004192 return 0;
4193}
4194
4195static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4196{
4197 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4198 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004199 /*
4200 * We expect this function to only ever be called from either RX or TX
4201 * thread context via iscsit_close_connection() once the other context
4202 * has been reset -> returned sleeping pre-handler state.
4203 */
4204 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004205 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004206
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004207 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004208 spin_unlock_bh(&conn->cmd_lock);
4209
4210 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004211
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004212 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004213
4214 spin_lock_bh(&conn->cmd_lock);
4215 }
4216 spin_unlock_bh(&conn->cmd_lock);
4217}
4218
4219static void iscsit_stop_timers_for_cmds(
4220 struct iscsi_conn *conn)
4221{
4222 struct iscsi_cmd *cmd;
4223
4224 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004225 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004226 if (cmd->data_direction == DMA_TO_DEVICE)
4227 iscsit_stop_dataout_timer(cmd);
4228 }
4229 spin_unlock_bh(&conn->cmd_lock);
4230}
4231
4232int iscsit_close_connection(
4233 struct iscsi_conn *conn)
4234{
4235 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4236 struct iscsi_session *sess = conn->sess;
4237
4238 pr_debug("Closing iSCSI connection CID %hu on SID:"
4239 " %u\n", conn->cid, sess->sid);
4240 /*
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004241 * Always up conn_logout_comp for the traditional TCP case just in case
4242 * the RX Thread in iscsi_target_rx_opcode() is sleeping and the logout
4243 * response never got sent because the connection failed.
4244 *
4245 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4246 * to signal logout response TX interrupt completion. Go ahead and skip
4247 * this for iser since isert_rx_opcode() does not wait on logout failure,
4248 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004249 */
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004250 if (conn->conn_transport->transport_type == ISCSI_TCP)
4251 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004252
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004253 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4254 if (conn->tx_thread &&
4255 cmpxchg(&conn->tx_thread_active, true, false)) {
4256 send_sig(SIGINT, conn->tx_thread, 1);
4257 kthread_stop(conn->tx_thread);
4258 }
4259 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4260 if (conn->rx_thread &&
4261 cmpxchg(&conn->rx_thread_active, true, false)) {
4262 send_sig(SIGINT, conn->rx_thread, 1);
4263 kthread_stop(conn->rx_thread);
4264 }
4265 }
4266
4267 spin_lock(&iscsit_global->ts_bitmap_lock);
4268 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4269 get_order(1));
4270 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004271
4272 iscsit_stop_timers_for_cmds(conn);
4273 iscsit_stop_nopin_response_timer(conn);
4274 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004275
4276 if (conn->conn_transport->iscsit_wait_conn)
4277 conn->conn_transport->iscsit_wait_conn(conn);
4278
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004279 /*
4280 * During Connection recovery drop unacknowledged out of order
4281 * commands for this connection, and prepare the other commands
4282 * for realligence.
4283 *
4284 * During normal operation clear the out of order commands (but
4285 * do not free the struct iscsi_ooo_cmdsn's) and release all
4286 * struct iscsi_cmds.
4287 */
4288 if (atomic_read(&conn->connection_recovery)) {
4289 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4290 iscsit_prepare_cmds_for_realligance(conn);
4291 } else {
4292 iscsit_clear_ooo_cmdsns_for_conn(conn);
4293 iscsit_release_commands_from_conn(conn);
4294 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004295 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004296
4297 /*
4298 * Handle decrementing session or connection usage count if
4299 * a logout response was not able to be sent because the
4300 * connection failed. Fall back to Session Recovery here.
4301 */
4302 if (atomic_read(&conn->conn_logout_remove)) {
4303 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4304 iscsit_dec_conn_usage_count(conn);
4305 iscsit_dec_session_usage_count(sess);
4306 }
4307 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4308 iscsit_dec_conn_usage_count(conn);
4309
4310 atomic_set(&conn->conn_logout_remove, 0);
4311 atomic_set(&sess->session_reinstatement, 0);
4312 atomic_set(&sess->session_fall_back_to_erl0, 1);
4313 }
4314
4315 spin_lock_bh(&sess->conn_lock);
4316 list_del(&conn->conn_list);
4317
4318 /*
4319 * Attempt to let the Initiator know this connection failed by
4320 * sending an Connection Dropped Async Message on another
4321 * active connection.
4322 */
4323 if (atomic_read(&conn->connection_recovery))
4324 iscsit_build_conn_drop_async_message(conn);
4325
4326 spin_unlock_bh(&sess->conn_lock);
4327
4328 /*
4329 * If connection reinstatement is being performed on this connection,
4330 * up the connection reinstatement semaphore that is being blocked on
4331 * in iscsit_cause_connection_reinstatement().
4332 */
4333 spin_lock_bh(&conn->state_lock);
4334 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4335 spin_unlock_bh(&conn->state_lock);
4336 complete(&conn->conn_wait_comp);
4337 wait_for_completion(&conn->conn_post_wait_comp);
4338 spin_lock_bh(&conn->state_lock);
4339 }
4340
4341 /*
4342 * If connection reinstatement is being performed on this connection
4343 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4344 * connection wait rcfr semaphore that is being blocked on
4345 * an iscsit_connection_reinstatement_rcfr().
4346 */
4347 if (atomic_read(&conn->connection_wait_rcfr)) {
4348 spin_unlock_bh(&conn->state_lock);
4349 complete(&conn->conn_wait_rcfr_comp);
4350 wait_for_completion(&conn->conn_post_wait_comp);
4351 spin_lock_bh(&conn->state_lock);
4352 }
4353 atomic_set(&conn->connection_reinstatement, 1);
4354 spin_unlock_bh(&conn->state_lock);
4355
4356 /*
4357 * If any other processes are accessing this connection pointer we
4358 * must wait until they have completed.
4359 */
4360 iscsit_check_conn_usage_count(conn);
4361
4362 if (conn->conn_rx_hash.tfm)
4363 crypto_free_hash(conn->conn_rx_hash.tfm);
4364 if (conn->conn_tx_hash.tfm)
4365 crypto_free_hash(conn->conn_tx_hash.tfm);
4366
Joern Engelfbecb652014-09-02 17:49:47 -04004367 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004368
4369 kfree(conn->conn_ops);
4370 conn->conn_ops = NULL;
4371
Al Virobf6932f2012-07-21 08:55:18 +01004372 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004373 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004374
4375 if (conn->conn_transport->iscsit_free_conn)
4376 conn->conn_transport->iscsit_free_conn(conn);
4377
4378 iscsit_put_transport(conn->conn_transport);
4379
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004380 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4381 conn->conn_state = TARG_CONN_STATE_FREE;
4382 kfree(conn);
4383
4384 spin_lock_bh(&sess->conn_lock);
4385 atomic_dec(&sess->nconn);
4386 pr_debug("Decremented iSCSI connection count to %hu from node:"
4387 " %s\n", atomic_read(&sess->nconn),
4388 sess->sess_ops->InitiatorName);
4389 /*
4390 * Make sure that if one connection fails in an non ERL=2 iSCSI
4391 * Session that they all fail.
4392 */
4393 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4394 !atomic_read(&sess->session_logout))
4395 atomic_set(&sess->session_fall_back_to_erl0, 1);
4396
4397 /*
4398 * If this was not the last connection in the session, and we are
4399 * performing session reinstatement or falling back to ERL=0, call
4400 * iscsit_stop_session() without sleeping to shutdown the other
4401 * active connections.
4402 */
4403 if (atomic_read(&sess->nconn)) {
4404 if (!atomic_read(&sess->session_reinstatement) &&
4405 !atomic_read(&sess->session_fall_back_to_erl0)) {
4406 spin_unlock_bh(&sess->conn_lock);
4407 return 0;
4408 }
4409 if (!atomic_read(&sess->session_stop_active)) {
4410 atomic_set(&sess->session_stop_active, 1);
4411 spin_unlock_bh(&sess->conn_lock);
4412 iscsit_stop_session(sess, 0, 0);
4413 return 0;
4414 }
4415 spin_unlock_bh(&sess->conn_lock);
4416 return 0;
4417 }
4418
4419 /*
4420 * If this was the last connection in the session and one of the
4421 * following is occurring:
4422 *
4423 * Session Reinstatement is not being performed, and are falling back
4424 * to ERL=0 call iscsit_close_session().
4425 *
4426 * Session Logout was requested. iscsit_close_session() will be called
4427 * elsewhere.
4428 *
4429 * Session Continuation is not being performed, start the Time2Retain
4430 * handler and check if sleep_on_sess_wait_sem is active.
4431 */
4432 if (!atomic_read(&sess->session_reinstatement) &&
4433 atomic_read(&sess->session_fall_back_to_erl0)) {
4434 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004435 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004436
4437 return 0;
4438 } else if (atomic_read(&sess->session_logout)) {
4439 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4440 sess->session_state = TARG_SESS_STATE_FREE;
4441 spin_unlock_bh(&sess->conn_lock);
4442
4443 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4444 complete(&sess->session_wait_comp);
4445
4446 return 0;
4447 } else {
4448 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4449 sess->session_state = TARG_SESS_STATE_FAILED;
4450
4451 if (!atomic_read(&sess->session_continuation)) {
4452 spin_unlock_bh(&sess->conn_lock);
4453 iscsit_start_time2retain_handler(sess);
4454 } else
4455 spin_unlock_bh(&sess->conn_lock);
4456
4457 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4458 complete(&sess->session_wait_comp);
4459
4460 return 0;
4461 }
4462 spin_unlock_bh(&sess->conn_lock);
4463
4464 return 0;
4465}
4466
4467int iscsit_close_session(struct iscsi_session *sess)
4468{
Andy Grover60bfcf82013-10-09 11:05:58 -07004469 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004470 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4471
4472 if (atomic_read(&sess->nconn)) {
4473 pr_err("%d connection(s) still exist for iSCSI session"
4474 " to %s\n", atomic_read(&sess->nconn),
4475 sess->sess_ops->InitiatorName);
4476 BUG();
4477 }
4478
4479 spin_lock_bh(&se_tpg->session_lock);
4480 atomic_set(&sess->session_logout, 1);
4481 atomic_set(&sess->session_reinstatement, 1);
4482 iscsit_stop_time2retain_timer(sess);
4483 spin_unlock_bh(&se_tpg->session_lock);
4484
4485 /*
4486 * transport_deregister_session_configfs() will clear the
4487 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4488 * can be setting it again with __transport_register_session() in
4489 * iscsi_post_login_handler() again after the iscsit_stop_session()
4490 * completes in iscsi_np context.
4491 */
4492 transport_deregister_session_configfs(sess->se_sess);
4493
4494 /*
4495 * If any other processes are accessing this session pointer we must
4496 * wait until they have completed. If we are in an interrupt (the
4497 * time2retain handler) and contain and active session usage count we
4498 * restart the timer and exit.
4499 */
4500 if (!in_interrupt()) {
4501 if (iscsit_check_session_usage_count(sess) == 1)
4502 iscsit_stop_session(sess, 1, 1);
4503 } else {
4504 if (iscsit_check_session_usage_count(sess) == 2) {
4505 atomic_set(&sess->session_logout, 0);
4506 iscsit_start_time2retain_handler(sess);
4507 return 0;
4508 }
4509 }
4510
4511 transport_deregister_session(sess->se_sess);
4512
4513 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4514 iscsit_free_connection_recovery_entires(sess);
4515
4516 iscsit_free_all_ooo_cmdsns(sess);
4517
4518 spin_lock_bh(&se_tpg->session_lock);
4519 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4520 sess->session_state = TARG_SESS_STATE_FREE;
4521 pr_debug("Released iSCSI session from node: %s\n",
4522 sess->sess_ops->InitiatorName);
4523 tpg->nsessions--;
4524 if (tpg->tpg_tiqn)
4525 tpg->tpg_tiqn->tiqn_nsessions--;
4526
4527 pr_debug("Decremented number of active iSCSI Sessions on"
4528 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4529
4530 spin_lock(&sess_idr_lock);
4531 idr_remove(&sess_idr, sess->session_index);
4532 spin_unlock(&sess_idr_lock);
4533
4534 kfree(sess->sess_ops);
4535 sess->sess_ops = NULL;
4536 spin_unlock_bh(&se_tpg->session_lock);
4537
4538 kfree(sess);
4539 return 0;
4540}
4541
4542static void iscsit_logout_post_handler_closesession(
4543 struct iscsi_conn *conn)
4544{
4545 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004546 int sleep = 1;
4547 /*
4548 * Traditional iscsi/tcp will invoke this logic from TX thread
4549 * context during session logout, so clear tx_thread_active and
4550 * sleep if iscsit_close_connection() has not already occured.
4551 *
4552 * Since iser-target invokes this logic from it's own workqueue,
4553 * always sleep waiting for RX/TX thread shutdown to complete
4554 * within iscsit_close_connection().
4555 */
4556 if (conn->conn_transport->transport_type == ISCSI_TCP)
4557 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004558
4559 atomic_set(&conn->conn_logout_remove, 0);
4560 complete(&conn->conn_logout_comp);
4561
4562 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004563 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004564 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004565 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004566}
4567
4568static void iscsit_logout_post_handler_samecid(
4569 struct iscsi_conn *conn)
4570{
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004571 int sleep = 1;
4572
4573 if (conn->conn_transport->transport_type == ISCSI_TCP)
4574 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004575
4576 atomic_set(&conn->conn_logout_remove, 0);
4577 complete(&conn->conn_logout_comp);
4578
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004579 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004580 iscsit_dec_conn_usage_count(conn);
4581}
4582
4583static void iscsit_logout_post_handler_diffcid(
4584 struct iscsi_conn *conn,
4585 u16 cid)
4586{
4587 struct iscsi_conn *l_conn;
4588 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004589 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004590
4591 if (!sess)
4592 return;
4593
4594 spin_lock_bh(&sess->conn_lock);
4595 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4596 if (l_conn->cid == cid) {
4597 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004598 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004599 break;
4600 }
4601 }
4602 spin_unlock_bh(&sess->conn_lock);
4603
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004604 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004605 return;
4606
4607 if (l_conn->sock)
4608 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4609
4610 spin_lock_bh(&l_conn->state_lock);
4611 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4612 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4613 spin_unlock_bh(&l_conn->state_lock);
4614
4615 iscsit_cause_connection_reinstatement(l_conn, 1);
4616 iscsit_dec_conn_usage_count(l_conn);
4617}
4618
4619/*
4620 * Return of 0 causes the TX thread to restart.
4621 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004622int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004623 struct iscsi_cmd *cmd,
4624 struct iscsi_conn *conn)
4625{
4626 int ret = 0;
4627
4628 switch (cmd->logout_reason) {
4629 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4630 switch (cmd->logout_response) {
4631 case ISCSI_LOGOUT_SUCCESS:
4632 case ISCSI_LOGOUT_CLEANUP_FAILED:
4633 default:
4634 iscsit_logout_post_handler_closesession(conn);
4635 break;
4636 }
4637 ret = 0;
4638 break;
4639 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4640 if (conn->cid == cmd->logout_cid) {
4641 switch (cmd->logout_response) {
4642 case ISCSI_LOGOUT_SUCCESS:
4643 case ISCSI_LOGOUT_CLEANUP_FAILED:
4644 default:
4645 iscsit_logout_post_handler_samecid(conn);
4646 break;
4647 }
4648 ret = 0;
4649 } else {
4650 switch (cmd->logout_response) {
4651 case ISCSI_LOGOUT_SUCCESS:
4652 iscsit_logout_post_handler_diffcid(conn,
4653 cmd->logout_cid);
4654 break;
4655 case ISCSI_LOGOUT_CID_NOT_FOUND:
4656 case ISCSI_LOGOUT_CLEANUP_FAILED:
4657 default:
4658 break;
4659 }
4660 ret = 1;
4661 }
4662 break;
4663 case ISCSI_LOGOUT_REASON_RECOVERY:
4664 switch (cmd->logout_response) {
4665 case ISCSI_LOGOUT_SUCCESS:
4666 case ISCSI_LOGOUT_CID_NOT_FOUND:
4667 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4668 case ISCSI_LOGOUT_CLEANUP_FAILED:
4669 default:
4670 break;
4671 }
4672 ret = 1;
4673 break;
4674 default:
4675 break;
4676
4677 }
4678 return ret;
4679}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004680EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004681
4682void iscsit_fail_session(struct iscsi_session *sess)
4683{
4684 struct iscsi_conn *conn;
4685
4686 spin_lock_bh(&sess->conn_lock);
4687 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4688 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4689 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4690 }
4691 spin_unlock_bh(&sess->conn_lock);
4692
4693 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4694 sess->session_state = TARG_SESS_STATE_FAILED;
4695}
4696
4697int iscsit_free_session(struct iscsi_session *sess)
4698{
4699 u16 conn_count = atomic_read(&sess->nconn);
4700 struct iscsi_conn *conn, *conn_tmp = NULL;
4701 int is_last;
4702
4703 spin_lock_bh(&sess->conn_lock);
4704 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4705
4706 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4707 conn_list) {
4708 if (conn_count == 0)
4709 break;
4710
4711 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4712 is_last = 1;
4713 } else {
4714 iscsit_inc_conn_usage_count(conn_tmp);
4715 is_last = 0;
4716 }
4717 iscsit_inc_conn_usage_count(conn);
4718
4719 spin_unlock_bh(&sess->conn_lock);
4720 iscsit_cause_connection_reinstatement(conn, 1);
4721 spin_lock_bh(&sess->conn_lock);
4722
4723 iscsit_dec_conn_usage_count(conn);
4724 if (is_last == 0)
4725 iscsit_dec_conn_usage_count(conn_tmp);
4726
4727 conn_count--;
4728 }
4729
4730 if (atomic_read(&sess->nconn)) {
4731 spin_unlock_bh(&sess->conn_lock);
4732 wait_for_completion(&sess->session_wait_comp);
4733 } else
4734 spin_unlock_bh(&sess->conn_lock);
4735
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004736 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004737 return 0;
4738}
4739
4740void iscsit_stop_session(
4741 struct iscsi_session *sess,
4742 int session_sleep,
4743 int connection_sleep)
4744{
4745 u16 conn_count = atomic_read(&sess->nconn);
4746 struct iscsi_conn *conn, *conn_tmp = NULL;
4747 int is_last;
4748
4749 spin_lock_bh(&sess->conn_lock);
4750 if (session_sleep)
4751 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4752
4753 if (connection_sleep) {
4754 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4755 conn_list) {
4756 if (conn_count == 0)
4757 break;
4758
4759 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4760 is_last = 1;
4761 } else {
4762 iscsit_inc_conn_usage_count(conn_tmp);
4763 is_last = 0;
4764 }
4765 iscsit_inc_conn_usage_count(conn);
4766
4767 spin_unlock_bh(&sess->conn_lock);
4768 iscsit_cause_connection_reinstatement(conn, 1);
4769 spin_lock_bh(&sess->conn_lock);
4770
4771 iscsit_dec_conn_usage_count(conn);
4772 if (is_last == 0)
4773 iscsit_dec_conn_usage_count(conn_tmp);
4774 conn_count--;
4775 }
4776 } else {
4777 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4778 iscsit_cause_connection_reinstatement(conn, 0);
4779 }
4780
4781 if (session_sleep && atomic_read(&sess->nconn)) {
4782 spin_unlock_bh(&sess->conn_lock);
4783 wait_for_completion(&sess->session_wait_comp);
4784 } else
4785 spin_unlock_bh(&sess->conn_lock);
4786}
4787
4788int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4789{
4790 struct iscsi_session *sess;
4791 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4792 struct se_session *se_sess, *se_sess_tmp;
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004793 LIST_HEAD(free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004794 int session_count = 0;
4795
4796 spin_lock_bh(&se_tpg->session_lock);
4797 if (tpg->nsessions && !force) {
4798 spin_unlock_bh(&se_tpg->session_lock);
4799 return -1;
4800 }
4801
4802 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4803 sess_list) {
4804 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4805
4806 spin_lock(&sess->conn_lock);
4807 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4808 atomic_read(&sess->session_logout) ||
4809 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4810 spin_unlock(&sess->conn_lock);
4811 continue;
4812 }
4813 atomic_set(&sess->session_reinstatement, 1);
4814 spin_unlock(&sess->conn_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004815
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004816 list_move_tail(&se_sess->sess_list, &free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004817 }
4818 spin_unlock_bh(&se_tpg->session_lock);
4819
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004820 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4821 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4822
4823 iscsit_free_session(sess);
4824 session_count++;
4825 }
4826
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004827 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4828 " Group: %hu\n", session_count, tpg->tpgt);
4829 return 0;
4830}
4831
4832MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4833MODULE_VERSION("4.1.x");
4834MODULE_AUTHOR("nab@Linux-iSCSI.org");
4835MODULE_LICENSE("GPL");
4836
4837module_init(iscsi_target_init_module);
4838module_exit(iscsi_target_cleanup_module);