blob: 21620c7510715de3346a1369beaa44b1e732bec8 [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>
Al Viro40401532012-02-13 03:58:52 +000024#include <linux/idr.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000025#include <asm/unaligned.h>
26#include <scsi/scsi_device.h>
27#include <scsi/iscsi_proto.h>
Andy Groverd28b11692012-04-03 15:51:22 -070028#include <scsi/scsi_tcq.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000029#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050030#include <target/target_core_fabric.h>
Andy Groverd28b11692012-04-03 15:51:22 -070031#include <target/target_core_configfs.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000032
Sagi Grimberg67f091f2015-01-07 14:57:31 +020033#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000034#include "iscsi_target_parameters.h"
35#include "iscsi_target_seq_pdu_list.h"
Nicholas Bellingere48354c2011-07-23 06:43:04 +000036#include "iscsi_target_datain_values.h"
37#include "iscsi_target_erl0.h"
38#include "iscsi_target_erl1.h"
39#include "iscsi_target_erl2.h"
40#include "iscsi_target_login.h"
41#include "iscsi_target_tmr.h"
42#include "iscsi_target_tpg.h"
43#include "iscsi_target_util.h"
44#include "iscsi_target.h"
45#include "iscsi_target_device.h"
Sagi Grimberg67f091f2015-01-07 14:57:31 +020046#include <target/iscsi/iscsi_target_stat.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000047
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080048#include <target/iscsi/iscsi_transport.h>
49
Nicholas Bellingere48354c2011-07-23 06:43:04 +000050static LIST_HEAD(g_tiqn_list);
51static LIST_HEAD(g_np_list);
52static DEFINE_SPINLOCK(tiqn_lock);
Andy Groveree291e62014-01-24 16:18:54 -080053static DEFINE_MUTEX(np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000054
55static struct idr tiqn_idr;
56struct idr sess_idr;
57struct mutex auth_id_lock;
58spinlock_t sess_idr_lock;
59
60struct iscsit_global *iscsit_global;
61
Nicholas Bellingere48354c2011-07-23 06:43:04 +000062struct kmem_cache *lio_qr_cache;
63struct kmem_cache *lio_dr_cache;
64struct kmem_cache *lio_ooo_cache;
65struct kmem_cache *lio_r2t_cache;
66
67static int iscsit_handle_immediate_data(struct iscsi_cmd *,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -070068 struct iscsi_scsi_req *, u32);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000069
70struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
71{
72 struct iscsi_tiqn *tiqn = NULL;
73
74 spin_lock(&tiqn_lock);
75 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
76 if (!strcmp(tiqn->tiqn, buf)) {
77
78 spin_lock(&tiqn->tiqn_state_lock);
79 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
80 tiqn->tiqn_access_count++;
81 spin_unlock(&tiqn->tiqn_state_lock);
82 spin_unlock(&tiqn_lock);
83 return tiqn;
84 }
85 spin_unlock(&tiqn->tiqn_state_lock);
86 }
87 }
88 spin_unlock(&tiqn_lock);
89
90 return NULL;
91}
92
93static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
94{
95 spin_lock(&tiqn->tiqn_state_lock);
96 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
97 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
98 spin_unlock(&tiqn->tiqn_state_lock);
99 return 0;
100 }
101 spin_unlock(&tiqn->tiqn_state_lock);
102
103 return -1;
104}
105
106void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
107{
108 spin_lock(&tiqn->tiqn_state_lock);
109 tiqn->tiqn_access_count--;
110 spin_unlock(&tiqn->tiqn_state_lock);
111}
112
113/*
114 * Note that IQN formatting is expected to be done in userspace, and
115 * no explict IQN format checks are done here.
116 */
117struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
118{
119 struct iscsi_tiqn *tiqn = NULL;
120 int ret;
121
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300122 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000123 pr_err("Target IQN exceeds %d bytes\n",
124 ISCSI_IQN_LEN);
125 return ERR_PTR(-EINVAL);
126 }
127
128 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
129 if (!tiqn) {
130 pr_err("Unable to allocate struct iscsi_tiqn\n");
131 return ERR_PTR(-ENOMEM);
132 }
133
134 sprintf(tiqn->tiqn, "%s", buf);
135 INIT_LIST_HEAD(&tiqn->tiqn_list);
136 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
137 spin_lock_init(&tiqn->tiqn_state_lock);
138 spin_lock_init(&tiqn->tiqn_tpg_lock);
139 spin_lock_init(&tiqn->sess_err_stats.lock);
140 spin_lock_init(&tiqn->login_stats.lock);
141 spin_lock_init(&tiqn->logout_stats.lock);
142
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000143 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
144
Tejun Heoc9365bd2013-02-27 17:04:43 -0800145 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000146 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800147
148 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000149 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800150 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000151 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800152 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000153 kfree(tiqn);
154 return ERR_PTR(ret);
155 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800156 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000157 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800158
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000159 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800160 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000161
162 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
163
164 return tiqn;
165
166}
167
168static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
169{
170 /*
171 * Wait for accesses to said struct iscsi_tiqn to end.
172 */
173 spin_lock(&tiqn->tiqn_state_lock);
174 while (tiqn->tiqn_access_count != 0) {
175 spin_unlock(&tiqn->tiqn_state_lock);
176 msleep(10);
177 spin_lock(&tiqn->tiqn_state_lock);
178 }
179 spin_unlock(&tiqn->tiqn_state_lock);
180}
181
182void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
183{
184 /*
185 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
186 * while holding tiqn->tiqn_state_lock. This means that all subsequent
187 * attempts to access this struct iscsi_tiqn will fail from both transport
188 * fabric and control code paths.
189 */
190 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
191 pr_err("iscsit_set_tiqn_shutdown() failed\n");
192 return;
193 }
194
195 iscsit_wait_for_tiqn(tiqn);
196
197 spin_lock(&tiqn_lock);
198 list_del(&tiqn->tiqn_list);
199 idr_remove(&tiqn_idr, tiqn->tiqn_index);
200 spin_unlock(&tiqn_lock);
201
202 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
203 tiqn->tiqn);
204 kfree(tiqn);
205}
206
207int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
208{
209 int ret;
210 /*
211 * Determine if the network portal is accepting storage traffic.
212 */
213 spin_lock_bh(&np->np_thread_lock);
214 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
215 spin_unlock_bh(&np->np_thread_lock);
216 return -1;
217 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000218 spin_unlock_bh(&np->np_thread_lock);
219 /*
220 * Determine if the portal group is accepting storage traffic.
221 */
222 spin_lock_bh(&tpg->tpg_state_lock);
223 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
224 spin_unlock_bh(&tpg->tpg_state_lock);
225 return -1;
226 }
227 spin_unlock_bh(&tpg->tpg_state_lock);
228
229 /*
230 * Here we serialize access across the TIQN+TPG Tuple.
231 */
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700232 ret = down_interruptible(&tpg->np_login_sem);
Nicholas Bellingeree7619f2015-05-19 15:10:44 -0700233 if (ret != 0)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000234 return -1;
235
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700236 spin_lock_bh(&tpg->tpg_state_lock);
237 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
238 spin_unlock_bh(&tpg->tpg_state_lock);
239 up(&tpg->np_login_sem);
240 return -1;
241 }
242 spin_unlock_bh(&tpg->tpg_state_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000243
244 return 0;
245}
246
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700247void iscsit_login_kref_put(struct kref *kref)
248{
249 struct iscsi_tpg_np *tpg_np = container_of(kref,
250 struct iscsi_tpg_np, tpg_np_kref);
251
252 complete(&tpg_np->tpg_np_comp);
253}
254
255int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
256 struct iscsi_tpg_np *tpg_np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000257{
258 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
259
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700260 up(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000261
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700262 if (tpg_np)
263 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000264
265 if (tiqn)
266 iscsit_put_tiqn_for_login(tiqn);
267
268 return 0;
269}
270
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800271bool iscsit_check_np_match(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000272 struct __kernel_sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800273 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000274 int network_transport)
275{
276 struct sockaddr_in *sock_in, *sock_in_e;
277 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800278 bool ip_match = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000279 u16 port;
280
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800281 if (sockaddr->ss_family == AF_INET6) {
282 sock_in6 = (struct sockaddr_in6 *)sockaddr;
283 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
284
285 if (!memcmp(&sock_in6->sin6_addr.in6_u,
286 &sock_in6_e->sin6_addr.in6_u,
287 sizeof(struct in6_addr)))
288 ip_match = true;
289
290 port = ntohs(sock_in6->sin6_port);
291 } else {
292 sock_in = (struct sockaddr_in *)sockaddr;
293 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
294
295 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
296 ip_match = true;
297
298 port = ntohs(sock_in->sin_port);
299 }
300
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200301 if (ip_match && (np->np_port == port) &&
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800302 (np->np_network_transport == network_transport))
303 return true;
304
305 return false;
306}
307
Andy Groveree291e62014-01-24 16:18:54 -0800308/*
309 * Called with mutex np_lock held
310 */
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800311static struct iscsi_np *iscsit_get_np(
312 struct __kernel_sockaddr_storage *sockaddr,
313 int network_transport)
314{
315 struct iscsi_np *np;
316 bool match;
317
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000318 list_for_each_entry(np, &g_np_list, np_list) {
Andy Groveree291e62014-01-24 16:18:54 -0800319 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000320 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
Andy Groveree291e62014-01-24 16:18:54 -0800321 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000322 continue;
323 }
324
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800325 match = iscsit_check_np_match(sockaddr, np, network_transport);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200326 if (match) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000327 /*
328 * Increment the np_exports reference count now to
329 * prevent iscsit_del_np() below from being called
330 * while iscsi_tpg_add_network_portal() is called.
331 */
332 np->np_exports++;
Andy Groveree291e62014-01-24 16:18:54 -0800333 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000334 return np;
335 }
Andy Groveree291e62014-01-24 16:18:54 -0800336 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000337 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000338
339 return NULL;
340}
341
342struct iscsi_np *iscsit_add_np(
343 struct __kernel_sockaddr_storage *sockaddr,
344 char *ip_str,
345 int network_transport)
346{
347 struct sockaddr_in *sock_in;
348 struct sockaddr_in6 *sock_in6;
349 struct iscsi_np *np;
350 int ret;
Andy Groveree291e62014-01-24 16:18:54 -0800351
352 mutex_lock(&np_lock);
353
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000354 /*
355 * Locate the existing struct iscsi_np if already active..
356 */
357 np = iscsit_get_np(sockaddr, network_transport);
Andy Groveree291e62014-01-24 16:18:54 -0800358 if (np) {
359 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000360 return np;
Andy Groveree291e62014-01-24 16:18:54 -0800361 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000362
363 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
364 if (!np) {
365 pr_err("Unable to allocate memory for struct iscsi_np\n");
Andy Groveree291e62014-01-24 16:18:54 -0800366 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000367 return ERR_PTR(-ENOMEM);
368 }
369
370 np->np_flags |= NPF_IP_NETWORK;
371 if (sockaddr->ss_family == AF_INET6) {
372 sock_in6 = (struct sockaddr_in6 *)sockaddr;
373 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
374 np->np_port = ntohs(sock_in6->sin6_port);
375 } else {
376 sock_in = (struct sockaddr_in *)sockaddr;
377 sprintf(np->np_ip, "%s", ip_str);
378 np->np_port = ntohs(sock_in->sin_port);
379 }
380
381 np->np_network_transport = network_transport;
382 spin_lock_init(&np->np_thread_lock);
383 init_completion(&np->np_restart_comp);
384 INIT_LIST_HEAD(&np->np_list);
385
386 ret = iscsi_target_setup_login_socket(np, sockaddr);
387 if (ret != 0) {
388 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800389 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000390 return ERR_PTR(ret);
391 }
392
393 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
394 if (IS_ERR(np->np_thread)) {
395 pr_err("Unable to create kthread: iscsi_np\n");
396 ret = PTR_ERR(np->np_thread);
397 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800398 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000399 return ERR_PTR(ret);
400 }
401 /*
402 * Increment the np_exports reference count now to prevent
403 * iscsit_del_np() below from being run while a new call to
404 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
405 * active. We don't need to hold np->np_thread_lock at this
406 * point because iscsi_np has not been added to g_np_list yet.
407 */
408 np->np_exports = 1;
Andy Groveree291e62014-01-24 16:18:54 -0800409 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000410
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000411 list_add_tail(&np->np_list, &g_np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800412 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000413
414 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800415 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000416
417 return np;
418}
419
420int iscsit_reset_np_thread(
421 struct iscsi_np *np,
422 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700423 struct iscsi_portal_group *tpg,
424 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000425{
426 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000427 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
428 spin_unlock_bh(&np->np_thread_lock);
429 return 0;
430 }
431 np->np_thread_state = ISCSI_NP_THREAD_RESET;
432
433 if (np->np_thread) {
434 spin_unlock_bh(&np->np_thread_lock);
435 send_sig(SIGINT, np->np_thread, 1);
436 wait_for_completion(&np->np_restart_comp);
437 spin_lock_bh(&np->np_thread_lock);
438 }
439 spin_unlock_bh(&np->np_thread_lock);
440
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700441 if (tpg_np && shutdown) {
442 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
443
444 wait_for_completion(&tpg_np->tpg_np_comp);
445 }
446
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000447 return 0;
448}
449
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800450static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000451{
Al Virobf6932f2012-07-21 08:55:18 +0100452 if (np->np_socket)
453 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000454}
455
456int iscsit_del_np(struct iscsi_np *np)
457{
458 spin_lock_bh(&np->np_thread_lock);
459 np->np_exports--;
460 if (np->np_exports) {
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700461 np->enabled = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000462 spin_unlock_bh(&np->np_thread_lock);
463 return 0;
464 }
465 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
466 spin_unlock_bh(&np->np_thread_lock);
467
468 if (np->np_thread) {
469 /*
470 * We need to send the signal to wakeup Linux/Net
471 * which may be sleeping in sock_accept()..
472 */
473 send_sig(SIGINT, np->np_thread, 1);
474 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800475 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000476 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800477
478 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000479
Andy Groveree291e62014-01-24 16:18:54 -0800480 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000481 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800482 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000483
484 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800485 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000486
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800487 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000488 kfree(np);
489 return 0;
490}
491
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700492static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
493static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
494
495static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
496{
497 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
498 return 0;
499}
500
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700501static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
502{
503 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
504
505 spin_lock_bh(&conn->cmd_lock);
506 if (!list_empty(&cmd->i_conn_node))
507 list_del_init(&cmd->i_conn_node);
508 spin_unlock_bh(&conn->cmd_lock);
509
510 __iscsit_free_cmd(cmd, scsi_cmd, true);
511}
512
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700513static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
514{
515 return TARGET_PROT_NORMAL;
516}
517
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800518static struct iscsit_transport iscsi_target_transport = {
519 .name = "iSCSI/TCP",
520 .transport_type = ISCSI_TCP,
521 .owner = NULL,
522 .iscsit_setup_np = iscsit_setup_np,
523 .iscsit_accept_np = iscsit_accept_np,
524 .iscsit_free_np = iscsit_free_np,
525 .iscsit_get_login_rx = iscsit_get_login_rx,
526 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800527 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700528 .iscsit_immediate_queue = iscsit_immediate_queue,
529 .iscsit_response_queue = iscsit_response_queue,
530 .iscsit_queue_data_in = iscsit_queue_rsp,
531 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700532 .iscsit_aborted_task = iscsit_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700533 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800534};
535
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000536static int __init iscsi_target_init_module(void)
537{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800538 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000539
540 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
541
542 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
543 if (!iscsit_global) {
544 pr_err("Unable to allocate memory for iscsit_global\n");
545 return -1;
546 }
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800547 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000548 mutex_init(&auth_id_lock);
549 spin_lock_init(&sess_idr_lock);
550 idr_init(&tiqn_idr);
551 idr_init(&sess_idr);
552
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200553 ret = target_register_template(&iscsi_ops);
554 if (ret)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000555 goto out;
556
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800557 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
558 iscsit_global->ts_bitmap = vzalloc(size);
559 if (!iscsit_global->ts_bitmap) {
560 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000561 goto configfs_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000562 }
563
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000564 lio_qr_cache = kmem_cache_create("lio_qr_cache",
565 sizeof(struct iscsi_queue_req),
566 __alignof__(struct iscsi_queue_req), 0, NULL);
567 if (!lio_qr_cache) {
568 pr_err("nable to kmem_cache_create() for"
569 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800570 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000571 }
572
573 lio_dr_cache = kmem_cache_create("lio_dr_cache",
574 sizeof(struct iscsi_datain_req),
575 __alignof__(struct iscsi_datain_req), 0, NULL);
576 if (!lio_dr_cache) {
577 pr_err("Unable to kmem_cache_create() for"
578 " lio_dr_cache\n");
579 goto qr_out;
580 }
581
582 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
583 sizeof(struct iscsi_ooo_cmdsn),
584 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
585 if (!lio_ooo_cache) {
586 pr_err("Unable to kmem_cache_create() for"
587 " lio_ooo_cache\n");
588 goto dr_out;
589 }
590
591 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
592 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
593 0, NULL);
594 if (!lio_r2t_cache) {
595 pr_err("Unable to kmem_cache_create() for"
596 " lio_r2t_cache\n");
597 goto ooo_out;
598 }
599
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800600 iscsit_register_transport(&iscsi_target_transport);
601
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000602 if (iscsit_load_discovery_tpg() < 0)
603 goto r2t_out;
604
605 return ret;
606r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100607 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000608 kmem_cache_destroy(lio_r2t_cache);
609ooo_out:
610 kmem_cache_destroy(lio_ooo_cache);
611dr_out:
612 kmem_cache_destroy(lio_dr_cache);
613qr_out:
614 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800615bitmap_out:
616 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000617configfs_out:
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200618 /* XXX: this probably wants it to be it's own unwind step.. */
619 if (iscsit_global->discovery_tpg)
620 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
621 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000622out:
623 kfree(iscsit_global);
624 return -ENOMEM;
625}
626
627static void __exit iscsi_target_cleanup_module(void)
628{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000629 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800630 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000631 kmem_cache_destroy(lio_qr_cache);
632 kmem_cache_destroy(lio_dr_cache);
633 kmem_cache_destroy(lio_ooo_cache);
634 kmem_cache_destroy(lio_r2t_cache);
635
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200636 /*
637 * Shutdown discovery sessions and disable discovery TPG
638 */
639 if (iscsit_global->discovery_tpg)
640 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000641
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200642 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000643
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800644 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000645 kfree(iscsit_global);
646}
647
Andy Grover8b1e1242012-04-03 15:51:12 -0700648static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700649 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000650 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700651 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000652{
653 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000654
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000655 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000656 if (!cmd)
657 return -1;
658
659 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700660 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000661
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100662 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000663 if (!cmd->buf_ptr) {
664 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700665 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000666 return -1;
667 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000668
669 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700670 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000671 spin_unlock_bh(&conn->cmd_lock);
672
673 cmd->i_state = ISTATE_SEND_REJECT;
674 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
675
Nicholas Bellingerba159912013-07-03 03:48:24 -0700676 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000677}
678
Nicholas Bellingerba159912013-07-03 03:48:24 -0700679static int iscsit_add_reject_from_cmd(
680 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000681 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700682 bool add_to_conn,
683 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000684{
685 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000686
687 if (!cmd->conn) {
688 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
689 cmd->init_task_tag);
690 return -1;
691 }
692 conn = cmd->conn;
693
694 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700695 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000696
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100697 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000698 if (!cmd->buf_ptr) {
699 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700700 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000701 return -1;
702 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000703
704 if (add_to_conn) {
705 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700706 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000707 spin_unlock_bh(&conn->cmd_lock);
708 }
709
710 cmd->i_state = ISTATE_SEND_REJECT;
711 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800712 /*
713 * Perform the kref_put now if se_cmd has already been setup by
714 * scsit_setup_scsi_cmd()
715 */
716 if (cmd->se_cmd.se_tfo != NULL) {
717 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
718 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
719 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700720 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000721}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700722
723static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
724 unsigned char *buf)
725{
726 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
727}
728
729int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
730{
731 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
732}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000733
734/*
735 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700736 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000737 */
738static int iscsit_map_iovec(
739 struct iscsi_cmd *cmd,
740 struct kvec *iov,
741 u32 data_offset,
742 u32 data_length)
743{
744 u32 i = 0;
745 struct scatterlist *sg;
746 unsigned int page_off;
747
748 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700749 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000750 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700751 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000752 page_off = (data_offset % PAGE_SIZE);
753
754 cmd->first_data_sg = sg;
755 cmd->first_data_sg_off = page_off;
756
757 while (data_length) {
758 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
759
760 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
761 iov[i].iov_len = cur_len;
762
763 data_length -= cur_len;
764 page_off = 0;
765 sg = sg_next(sg);
766 i++;
767 }
768
769 cmd->kmapped_nents = i;
770
771 return i;
772}
773
774static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
775{
776 u32 i;
777 struct scatterlist *sg;
778
779 sg = cmd->first_data_sg;
780
781 for (i = 0; i < cmd->kmapped_nents; i++)
782 kunmap(sg_page(&sg[i]));
783}
784
785static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
786{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700787 LIST_HEAD(ack_list);
788 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000789
790 conn->exp_statsn = exp_statsn;
791
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800792 if (conn->sess->sess_ops->RDMAExtensions)
793 return;
794
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000795 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700796 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000797 spin_lock(&cmd->istate_lock);
798 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800799 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000800 cmd->i_state = ISTATE_REMOVE;
801 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700802 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000803 continue;
804 }
805 spin_unlock(&cmd->istate_lock);
806 }
807 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700808
809 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800810 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700811 iscsit_free_cmd(cmd, false);
812 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000813}
814
815static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
816{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700817 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000818
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400819 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000820
821 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
822 if (!cmd->iov_data) {
823 pr_err("Unable to allocate cmd->iov_data\n");
824 return -ENOMEM;
825 }
826
827 cmd->orig_iov_data_count = iov_count;
828 return 0;
829}
830
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800831int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
832 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000833{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800834 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000835 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700836 int iscsi_task_attr;
837 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000838
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800839 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000840
841 hdr = (struct iscsi_scsi_req *) buf;
842 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000843
844 /* FIXME; Add checks for AdditionalHeaderSegment */
845
846 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
847 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
848 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
849 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700850 return iscsit_add_reject_cmd(cmd,
851 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000852 }
853
854 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
855 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
856 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800857 * From RFC-3720 Section 10.3.1:
858 *
859 * "Either or both of R and W MAY be 1 when either the
860 * Expected Data Transfer Length and/or Bidirectional Read
861 * Expected Data Transfer Length are 0"
862 *
863 * For this case, go ahead and clear the unnecssary bits
864 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000865 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800866 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
867 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000868
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800869 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000870 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800871 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000872 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000873
874 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
875 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
876 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
877 " MUST be set if Expected Data Transfer Length is not 0."
878 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700879 return iscsit_add_reject_cmd(cmd,
880 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000881 }
882
883 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
884 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
885 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700886 return iscsit_add_reject_cmd(cmd,
887 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000888 }
889
890 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
891 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
892 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700893 return iscsit_add_reject_cmd(cmd,
894 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000895 }
896
897 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
898 pr_err("ImmediateData=No but DataSegmentLength=%u,"
899 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700900 return iscsit_add_reject_cmd(cmd,
901 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000902 }
903
Nicholas Bellingerba159912013-07-03 03:48:24 -0700904 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000905 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
906 pr_err("Expected Data Transfer Length and Length of"
907 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
908 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700909 return iscsit_add_reject_cmd(cmd,
910 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000911 }
912
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400913 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000914 pr_err("DataSegmentLength: %u is greater than"
915 " EDTL: %u, protocol error.\n", payload_length,
916 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700917 return iscsit_add_reject_cmd(cmd,
918 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000919 }
920
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700921 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000922 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700923 " MaxXmitDataSegmentLength: %u, protocol error.\n",
924 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700925 return iscsit_add_reject_cmd(cmd,
926 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000927 }
928
929 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
930 pr_err("DataSegmentLength: %u is greater than"
931 " FirstBurstLength: %u, protocol error.\n",
932 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700933 return iscsit_add_reject_cmd(cmd,
934 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000935 }
936
937 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
938 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
939 DMA_NONE;
940
Andy Groverd28b11692012-04-03 15:51:22 -0700941 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700942 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
943 /*
944 * Figure out the SAM Task Attribute for the incoming SCSI CDB
945 */
946 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
947 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800948 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700949 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800950 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700951 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800952 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700953 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800954 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700955 else {
956 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800957 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
958 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700959 }
960
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000961 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
962 cmd->i_state = ISTATE_NEW_CMD;
963 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
964 cmd->immediate_data = (payload_length) ? 1 : 0;
965 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
966 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
967 if (cmd->unsolicited_data)
968 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
969
970 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
971 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
Sagi Grimbergc1e34b62015-01-26 12:49:05 +0200972 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000973 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
974 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400975 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
976 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000977 cmd->first_burst_len = payload_length;
978
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800979 if (!conn->sess->sess_ops->RDMAExtensions &&
980 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000981 struct iscsi_datain_req *dr;
982
983 dr = iscsit_allocate_datain_req();
984 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700985 return iscsit_add_reject_cmd(cmd,
986 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000987
988 iscsit_attach_datain_req(cmd, dr);
989 }
990
991 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700992 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
993 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200994 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400995 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
996 cmd->data_direction, sam_task_attr,
997 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700998
999 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1000 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001001 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1002 conn->cid);
1003
1004 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001005
Christoph Hellwigde103c92012-11-06 12:24:09 -08001006 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1007 scsilun_to_int(&hdr->lun));
1008 if (cmd->sense_reason)
1009 goto attach_cmd;
1010
1011 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1012 if (cmd->sense_reason) {
1013 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001014 return iscsit_add_reject_cmd(cmd,
1015 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001016 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001017
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001018 goto attach_cmd;
1019 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001020
Christoph Hellwigde103c92012-11-06 12:24:09 -08001021 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001022 return iscsit_add_reject_cmd(cmd,
1023 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001024 }
1025
1026attach_cmd:
1027 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001028 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001029 spin_unlock_bh(&conn->cmd_lock);
1030 /*
1031 * Check if we need to delay processing because of ALUA
1032 * Active/NonOptimized primary access state..
1033 */
1034 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001035
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001036 return 0;
1037}
1038EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001039
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001040void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1041{
1042 iscsit_set_dataout_sequence_values(cmd);
1043
1044 spin_lock_bh(&cmd->dataout_timeout_lock);
1045 iscsit_start_dataout_timer(cmd, cmd->conn);
1046 spin_unlock_bh(&cmd->dataout_timeout_lock);
1047}
1048EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1049
1050int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1051 struct iscsi_scsi_req *hdr)
1052{
1053 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001054 /*
1055 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1056 * the Immediate Bit is not set, and no Immediate
1057 * Data is attached.
1058 *
1059 * A PDU/CmdSN carrying Immediate Data can only
1060 * be processed after the DataCRC has passed.
1061 * If the DataCRC fails, the CmdSN MUST NOT
1062 * be acknowledged. (See below)
1063 */
1064 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001065 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1066 (unsigned char *)hdr, hdr->cmdsn);
1067 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1068 return -1;
1069 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001070 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001071 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001072 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001073 }
1074
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001075 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001076
1077 /*
1078 * If no Immediate Data is attached, it's OK to return now.
1079 */
1080 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001081 if (!cmd->sense_reason && cmd->unsolicited_data)
1082 iscsit_set_unsoliticed_dataout(cmd);
1083 if (!cmd->sense_reason)
1084 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001085
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001086 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001087 return 0;
1088 }
1089
1090 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001091 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1092 * execution. These exceptions are processed in CmdSN order using
1093 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001094 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001095 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001096 if (cmd->reject_reason)
1097 return 0;
1098
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001099 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001100 }
1101 /*
1102 * Call directly into transport_generic_new_cmd() to perform
1103 * the backend memory allocation.
1104 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001105 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001106 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001107 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001108
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001109 return 0;
1110}
1111EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1112
1113static int
1114iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1115 bool dump_payload)
1116{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001117 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001118 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1119 /*
1120 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1121 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001122 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001123 goto after_immediate_data;
1124
1125 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1126 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001127after_immediate_data:
1128 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1129 /*
1130 * A PDU/CmdSN carrying Immediate Data passed
1131 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1132 * Immediate Bit is not set.
1133 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001134 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1135 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001136 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001137 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001138
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001139 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001140 int rc;
1141
1142 rc = iscsit_dump_data_payload(cmd->conn,
1143 cmd->first_burst_len, 1);
1144 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1145 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001146 } else if (cmd->unsolicited_data)
1147 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001148
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001149 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1150 /*
1151 * Immediate Data failed DataCRC and ERL>=1,
1152 * silently drop this PDU and let the initiator
1153 * plug the CmdSN gap.
1154 *
1155 * FIXME: Send Unsolicited NOPIN with reserved
1156 * TTT here to help the initiator figure out
1157 * the missing CmdSN, although they should be
1158 * intelligent enough to determine the missing
1159 * CmdSN and issue a retry to plug the sequence.
1160 */
1161 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001162 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001163 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1164 return -1;
1165
1166 return 0;
1167}
1168
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001169static int
1170iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1171 unsigned char *buf)
1172{
1173 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1174 int rc, immed_data;
1175 bool dump_payload = false;
1176
1177 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1178 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001179 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001180 /*
1181 * Allocation iovecs needed for struct socket operations for
1182 * traditional iSCSI block I/O.
1183 */
1184 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001185 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001186 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001187 }
1188 immed_data = cmd->immediate_data;
1189
1190 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1191 if (rc < 0)
1192 return rc;
1193 else if (rc > 0)
1194 dump_payload = true;
1195
1196 if (!immed_data)
1197 return 0;
1198
1199 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1200}
1201
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001202static u32 iscsit_do_crypto_hash_sg(
1203 struct hash_desc *hash,
1204 struct iscsi_cmd *cmd,
1205 u32 data_offset,
1206 u32 data_length,
1207 u32 padding,
1208 u8 *pad_bytes)
1209{
1210 u32 data_crc;
1211 u32 i;
1212 struct scatterlist *sg;
1213 unsigned int page_off;
1214
1215 crypto_hash_init(hash);
1216
1217 sg = cmd->first_data_sg;
1218 page_off = cmd->first_data_sg_off;
1219
1220 i = 0;
1221 while (data_length) {
1222 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1223
1224 crypto_hash_update(hash, &sg[i], cur_len);
1225
1226 data_length -= cur_len;
1227 page_off = 0;
1228 i++;
1229 }
1230
1231 if (padding) {
1232 struct scatterlist pad_sg;
1233
1234 sg_init_one(&pad_sg, pad_bytes, padding);
1235 crypto_hash_update(hash, &pad_sg, padding);
1236 }
1237 crypto_hash_final(hash, (u8 *) &data_crc);
1238
1239 return data_crc;
1240}
1241
1242static void iscsit_do_crypto_hash_buf(
1243 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001244 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001245 u32 payload_length,
1246 u32 padding,
1247 u8 *pad_bytes,
1248 u8 *data_crc)
1249{
1250 struct scatterlist sg;
1251
1252 crypto_hash_init(hash);
1253
Jörn Engel8359cf42011-11-24 02:05:51 +01001254 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001255 crypto_hash_update(hash, &sg, payload_length);
1256
1257 if (padding) {
1258 sg_init_one(&sg, pad_bytes, padding);
1259 crypto_hash_update(hash, &sg, padding);
1260 }
1261 crypto_hash_final(hash, data_crc);
1262}
1263
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001264int
1265iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1266 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001267{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001268 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001269 struct iscsi_cmd *cmd = NULL;
1270 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001271 u32 payload_length = ntoh24(hdr->dlength);
1272 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001273
1274 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001275 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1276 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001277 }
1278
1279 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001280 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001281
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001282 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001283 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001284 " MaxXmitDataSegmentLength: %u\n", payload_length,
1285 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001286 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1287 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001288 }
1289
1290 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1291 payload_length);
1292 if (!cmd)
1293 return 0;
1294
1295 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1296 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001297 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001298 payload_length, conn->cid);
1299
1300 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1301 pr_err("Command ITT: 0x%08x received DataOUT after"
1302 " last DataOUT received, dumping payload\n",
1303 cmd->init_task_tag);
1304 return iscsit_dump_data_payload(conn, payload_length, 1);
1305 }
1306
1307 if (cmd->data_direction != DMA_TO_DEVICE) {
1308 pr_err("Command ITT: 0x%08x received DataOUT for a"
1309 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001310 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001311 }
1312 se_cmd = &cmd->se_cmd;
1313 iscsit_mod_dataout_timer(cmd);
1314
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001315 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001316 pr_err("DataOut Offset: %u, Length %u greater than"
1317 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001318 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001319 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001320 }
1321
1322 if (cmd->unsolicited_data) {
1323 int dump_unsolicited_data = 0;
1324
1325 if (conn->sess->sess_ops->InitialR2T) {
1326 pr_err("Received unexpected unsolicited data"
1327 " while InitialR2T=Yes, protocol error.\n");
1328 transport_send_check_condition_and_sense(&cmd->se_cmd,
1329 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1330 return -1;
1331 }
1332 /*
1333 * Special case for dealing with Unsolicited DataOUT
1334 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1335 * failures;
1336 */
1337
1338 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001339 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001340 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001341 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001342
1343 if (dump_unsolicited_data) {
1344 /*
1345 * Check if a delayed TASK_ABORTED status needs to
1346 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1347 * received with the unsolicitied data out.
1348 */
1349 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1350 iscsit_stop_dataout_timer(cmd);
1351
1352 transport_check_aborted_status(se_cmd,
1353 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1354 return iscsit_dump_data_payload(conn, payload_length, 1);
1355 }
1356 } else {
1357 /*
1358 * For the normal solicited data path:
1359 *
1360 * Check for a delayed TASK_ABORTED status and dump any
1361 * incoming data out payload if one exists. Also, when the
1362 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1363 * data out sequence, we decrement outstanding_r2ts. Once
1364 * outstanding_r2ts reaches zero, go ahead and send the delayed
1365 * TASK_ABORTED status.
1366 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001367 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001368 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1369 if (--cmd->outstanding_r2ts < 1) {
1370 iscsit_stop_dataout_timer(cmd);
1371 transport_check_aborted_status(
1372 se_cmd, 1);
1373 }
1374
1375 return iscsit_dump_data_payload(conn, payload_length, 1);
1376 }
1377 }
1378 /*
1379 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1380 * within-command recovery checks before receiving the payload.
1381 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001382 rc = iscsit_check_pre_dataout(cmd, buf);
1383 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001384 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001385 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001386 return -1;
1387
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001388 *out_cmd = cmd;
1389 return 0;
1390}
1391EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1392
1393static int
1394iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1395 struct iscsi_data *hdr)
1396{
1397 struct kvec *iov;
1398 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1399 u32 payload_length = ntoh24(hdr->dlength);
1400 int iov_ret, data_crc_failed = 0;
1401
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001402 rx_size += payload_length;
1403 iov = &cmd->iov_data[0];
1404
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001405 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1406 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001407 if (iov_ret < 0)
1408 return -1;
1409
1410 iov_count += iov_ret;
1411
1412 padding = ((-payload_length) & 3);
1413 if (padding != 0) {
1414 iov[iov_count].iov_base = cmd->pad_bytes;
1415 iov[iov_count++].iov_len = padding;
1416 rx_size += padding;
1417 pr_debug("Receiving %u padding bytes.\n", padding);
1418 }
1419
1420 if (conn->conn_ops->DataDigest) {
1421 iov[iov_count].iov_base = &checksum;
1422 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1423 rx_size += ISCSI_CRC_LEN;
1424 }
1425
1426 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1427
1428 iscsit_unmap_iovec(cmd);
1429
1430 if (rx_got != rx_size)
1431 return -1;
1432
1433 if (conn->conn_ops->DataDigest) {
1434 u32 data_crc;
1435
1436 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001437 be32_to_cpu(hdr->offset),
1438 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001439 cmd->pad_bytes);
1440
1441 if (checksum != data_crc) {
1442 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1443 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1444 " does not match computed 0x%08x\n",
1445 hdr->itt, hdr->offset, payload_length,
1446 hdr->datasn, checksum, data_crc);
1447 data_crc_failed = 1;
1448 } else {
1449 pr_debug("Got CRC32C DataDigest 0x%08x for"
1450 " %u bytes of Data Out\n", checksum,
1451 payload_length);
1452 }
1453 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001454
1455 return data_crc_failed;
1456}
1457
1458int
1459iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1460 bool data_crc_failed)
1461{
1462 struct iscsi_conn *conn = cmd->conn;
1463 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001464 /*
1465 * Increment post receive data and CRC values or perform
1466 * within-command recovery.
1467 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001468 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1469 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001470 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001471 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001472 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001473 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1474 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001475 /*
1476 * Handle extra special case for out of order
1477 * Unsolicited Data Out.
1478 */
1479 spin_lock_bh(&cmd->istate_lock);
1480 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1481 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1482 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1483 spin_unlock_bh(&cmd->istate_lock);
1484
1485 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001486 if (ooo_cmdsn)
1487 return 0;
1488 target_execute_cmd(&cmd->se_cmd);
1489 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001490 } else /* DATAOUT_CANNOT_RECOVER */
1491 return -1;
1492
1493 return 0;
1494}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001495EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001496
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001497static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1498{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001499 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001500 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1501 int rc;
1502 bool data_crc_failed = false;
1503
1504 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1505 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001506 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001507 else if (!cmd)
1508 return 0;
1509
1510 rc = iscsit_get_dataout(conn, cmd, hdr);
1511 if (rc < 0)
1512 return rc;
1513 else if (rc > 0)
1514 data_crc_failed = true;
1515
1516 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1517}
1518
Nicholas Bellinger778de362013-06-14 16:07:47 -07001519int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1520 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001521{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001522 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001523
Arshad Hussaina3662602014-03-14 15:28:59 -07001524 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1525 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1526 if (!cmd)
1527 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1528 (unsigned char *)hdr);
1529
1530 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1531 (unsigned char *)hdr);
1532 }
1533
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001534 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001535 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1536 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001537 if (!cmd)
1538 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1539 (unsigned char *)hdr);
1540
Nicholas Bellingerba159912013-07-03 03:48:24 -07001541 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1542 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001543 }
1544
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001545 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001546 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001547 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001548 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001549 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001550 if (!cmd)
1551 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1552 (unsigned char *)hdr);
1553
Nicholas Bellingerba159912013-07-03 03:48:24 -07001554 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1555 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001556 }
1557
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001558 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001559 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001560 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001561 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1562 payload_length);
1563 /*
1564 * This is not a response to a Unsolicited NopIN, which means
1565 * it can either be a NOPOUT ping request (with a valid ITT),
1566 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1567 * Either way, make sure we allocate an struct iscsi_cmd, as both
1568 * can contain ping data.
1569 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001570 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001571 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1572 cmd->i_state = ISTATE_SEND_NOPIN;
1573 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1574 1 : 0);
1575 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1576 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001577 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1578 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001579 cmd->data_direction = DMA_NONE;
1580 }
1581
Nicholas Bellinger778de362013-06-14 16:07:47 -07001582 return 0;
1583}
1584EXPORT_SYMBOL(iscsit_setup_nop_out);
1585
1586int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1587 struct iscsi_nopout *hdr)
1588{
1589 struct iscsi_cmd *cmd_p = NULL;
1590 int cmdsn_ret = 0;
1591 /*
1592 * Initiator is expecting a NopIN ping reply..
1593 */
1594 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001595 if (!cmd)
1596 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1597 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001598
1599 spin_lock_bh(&conn->cmd_lock);
1600 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1601 spin_unlock_bh(&conn->cmd_lock);
1602
1603 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1604
1605 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1606 iscsit_add_cmd_to_response_queue(cmd, conn,
1607 cmd->i_state);
1608 return 0;
1609 }
1610
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001611 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1612 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001613 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1614 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001615 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001616 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001617
1618 return 0;
1619 }
1620 /*
1621 * This was a response to a unsolicited NOPIN ping.
1622 */
1623 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1624 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1625 if (!cmd_p)
1626 return -EINVAL;
1627
1628 iscsit_stop_nopin_response_timer(conn);
1629
1630 cmd_p->i_state = ISTATE_REMOVE;
1631 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1632
1633 iscsit_start_nopin_timer(conn);
1634 return 0;
1635 }
1636 /*
1637 * Otherwise, initiator is not expecting a NOPIN is response.
1638 * Just ignore for now.
1639 */
1640 return 0;
1641}
1642EXPORT_SYMBOL(iscsit_process_nop_out);
1643
1644static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1645 unsigned char *buf)
1646{
1647 unsigned char *ping_data = NULL;
1648 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1649 struct kvec *iov = NULL;
1650 u32 payload_length = ntoh24(hdr->dlength);
1651 int ret;
1652
1653 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1654 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001655 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001656 /*
1657 * Handle NOP-OUT payload for traditional iSCSI sockets
1658 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001659 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001660 u32 checksum, data_crc, padding = 0;
1661 int niov = 0, rx_got, rx_size = payload_length;
1662
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001663 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1664 if (!ping_data) {
1665 pr_err("Unable to allocate memory for"
1666 " NOPOUT ping data.\n");
1667 ret = -1;
1668 goto out;
1669 }
1670
1671 iov = &cmd->iov_misc[0];
1672 iov[niov].iov_base = ping_data;
1673 iov[niov++].iov_len = payload_length;
1674
1675 padding = ((-payload_length) & 3);
1676 if (padding != 0) {
1677 pr_debug("Receiving %u additional bytes"
1678 " for padding.\n", padding);
1679 iov[niov].iov_base = &cmd->pad_bytes;
1680 iov[niov++].iov_len = padding;
1681 rx_size += padding;
1682 }
1683 if (conn->conn_ops->DataDigest) {
1684 iov[niov].iov_base = &checksum;
1685 iov[niov++].iov_len = ISCSI_CRC_LEN;
1686 rx_size += ISCSI_CRC_LEN;
1687 }
1688
1689 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1690 if (rx_got != rx_size) {
1691 ret = -1;
1692 goto out;
1693 }
1694
1695 if (conn->conn_ops->DataDigest) {
1696 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1697 ping_data, payload_length,
1698 padding, cmd->pad_bytes,
1699 (u8 *)&data_crc);
1700
1701 if (checksum != data_crc) {
1702 pr_err("Ping data CRC32C DataDigest"
1703 " 0x%08x does not match computed 0x%08x\n",
1704 checksum, data_crc);
1705 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1706 pr_err("Unable to recover from"
1707 " NOPOUT Ping DataCRC failure while in"
1708 " ERL=0.\n");
1709 ret = -1;
1710 goto out;
1711 } else {
1712 /*
1713 * Silently drop this PDU and let the
1714 * initiator plug the CmdSN gap.
1715 */
1716 pr_debug("Dropping NOPOUT"
1717 " Command CmdSN: 0x%08x due to"
1718 " DataCRC error.\n", hdr->cmdsn);
1719 ret = 0;
1720 goto out;
1721 }
1722 } else {
1723 pr_debug("Got CRC32C DataDigest"
1724 " 0x%08x for %u bytes of ping data.\n",
1725 checksum, payload_length);
1726 }
1727 }
1728
1729 ping_data[payload_length] = '\0';
1730 /*
1731 * Attach ping data to struct iscsi_cmd->buf_ptr.
1732 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001733 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001734 cmd->buf_ptr_size = payload_length;
1735
1736 pr_debug("Got %u bytes of NOPOUT ping"
1737 " data.\n", payload_length);
1738 pr_debug("Ping Data: \"%s\"\n", ping_data);
1739 }
1740
Nicholas Bellinger778de362013-06-14 16:07:47 -07001741 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001742out:
1743 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001744 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001745
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001746 kfree(ping_data);
1747 return ret;
1748}
1749
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001750int
1751iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1752 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001753{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001754 struct se_tmr_req *se_tmr;
1755 struct iscsi_tmr_req *tmr_req;
1756 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001757 int out_of_order_cmdsn = 0, ret;
1758 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001759 u8 function;
1760
1761 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001762 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1763 function = hdr->flags;
1764
1765 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1766 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1767 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1768 hdr->rtt, hdr->refcmdsn, conn->cid);
1769
1770 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1771 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001772 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001773 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001774 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001775 }
1776
1777 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1778 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1779 pr_err("Task Management Request TASK_REASSIGN not"
1780 " issued as immediate command, bad iSCSI Initiator"
1781 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001782 return iscsit_add_reject_cmd(cmd,
1783 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001784 }
1785 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001786 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1787 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001788
Andy Groverd28b11692012-04-03 15:51:22 -07001789 cmd->data_direction = DMA_NONE;
1790
1791 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1792 if (!cmd->tmr_req) {
1793 pr_err("Unable to allocate memory for"
1794 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001795 return iscsit_add_reject_cmd(cmd,
1796 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1797 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001798 }
1799
1800 /*
1801 * TASK_REASSIGN for ERL=2 / connection stays inside of
1802 * LIO-Target $FABRIC_MOD
1803 */
1804 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1805
1806 u8 tcm_function;
1807 int ret;
1808
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001809 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Andy Groverd28b11692012-04-03 15:51:22 -07001810 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001811 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001812
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001813 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1814 sess_ref = true;
1815
Andy Groverd28b11692012-04-03 15:51:22 -07001816 switch (function) {
1817 case ISCSI_TM_FUNC_ABORT_TASK:
1818 tcm_function = TMR_ABORT_TASK;
1819 break;
1820 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1821 tcm_function = TMR_ABORT_TASK_SET;
1822 break;
1823 case ISCSI_TM_FUNC_CLEAR_ACA:
1824 tcm_function = TMR_CLEAR_ACA;
1825 break;
1826 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1827 tcm_function = TMR_CLEAR_TASK_SET;
1828 break;
1829 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1830 tcm_function = TMR_LUN_RESET;
1831 break;
1832 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1833 tcm_function = TMR_TARGET_WARM_RESET;
1834 break;
1835 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1836 tcm_function = TMR_TARGET_COLD_RESET;
1837 break;
1838 default:
1839 pr_err("Unknown iSCSI TMR Function:"
1840 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001841 return iscsit_add_reject_cmd(cmd,
1842 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001843 }
1844
1845 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1846 tcm_function, GFP_KERNEL);
1847 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001848 return iscsit_add_reject_cmd(cmd,
1849 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001850
1851 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1852 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001853
1854 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1855 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1856 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1857 cmd->init_task_tag = hdr->itt;
1858 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001859 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1860 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001861 se_tmr = cmd->se_cmd.se_tmr_req;
1862 tmr_req = cmd->tmr_req;
1863 /*
1864 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1865 */
1866 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001867 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1868 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001869 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001870 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1871 goto attach;
1872 }
1873 }
1874
1875 switch (function) {
1876 case ISCSI_TM_FUNC_ABORT_TASK:
1877 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001878 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001879 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001880 break;
1881 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1882 case ISCSI_TM_FUNC_CLEAR_ACA:
1883 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1884 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1885 break;
1886 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1887 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001888 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1889 goto attach;
1890 }
1891 break;
1892 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1893 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001894 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1895 goto attach;
1896 }
1897 break;
1898 case ISCSI_TM_FUNC_TASK_REASSIGN:
1899 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1900 /*
1901 * Perform sanity checks on the ExpDataSN only if the
1902 * TASK_REASSIGN was successful.
1903 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001904 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001905 break;
1906
1907 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001908 return iscsit_add_reject_cmd(cmd,
1909 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001910 break;
1911 default:
1912 pr_err("Unknown TMR function: 0x%02x, protocol"
1913 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001914 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1915 goto attach;
1916 }
1917
1918 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1919 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1920 se_tmr->call_transport = 1;
1921attach:
1922 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001923 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001924 spin_unlock_bh(&conn->cmd_lock);
1925
1926 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001927 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001928 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1929 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001930 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001931 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001932 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001933 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001934 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001935 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001936
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001937 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001938 return 0;
1939 /*
1940 * Found the referenced task, send to transport for processing.
1941 */
1942 if (se_tmr->call_transport)
1943 return transport_generic_handle_tmr(&cmd->se_cmd);
1944
1945 /*
1946 * Could not find the referenced LUN, task, or Task Management
1947 * command not authorized or supported. Change state and
1948 * let the tx_thread send the response.
1949 *
1950 * For connection recovery, this is also the default action for
1951 * TMR TASK_REASSIGN.
1952 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001953 if (sess_ref) {
1954 pr_debug("Handle TMR, using sess_ref=true check\n");
1955 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1956 }
1957
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001958 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1959 return 0;
1960}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001961EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001962
1963/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001964int
1965iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1966 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001967{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001968 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001969
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001970 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001971 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001972 "greater than MaxXmitDataSegmentLength %u.\n",
1973 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001974 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1975 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001976 }
1977
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08001978 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1979 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1980 pr_err("Multi sequence text commands currently not supported\n");
1981 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1982 (unsigned char *)hdr);
1983 }
1984
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001985 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1986 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1987 hdr->exp_statsn, payload_length);
1988
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001989 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1990 cmd->i_state = ISTATE_SEND_TEXTRSP;
1991 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1992 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1993 cmd->targ_xfer_tag = 0xFFFFFFFF;
1994 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1995 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1996 cmd->data_direction = DMA_NONE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001997 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001998
1999 return 0;
2000}
2001EXPORT_SYMBOL(iscsit_setup_text_cmd);
2002
2003int
2004iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2005 struct iscsi_text *hdr)
2006{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002007 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002008 int cmdsn_ret;
2009
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002010 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002011 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2012 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2013 pr_err("Unable to locate text_in buffer for sendtargets"
2014 " discovery\n");
2015 goto reject;
2016 }
2017 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002018 }
2019 if (strncmp("SendTargets", text_in, 11) != 0) {
2020 pr_err("Received Text Data that is not"
2021 " SendTargets, cannot continue.\n");
2022 goto reject;
2023 }
2024 text_ptr = strchr(text_in, '=');
2025 if (!text_ptr) {
2026 pr_err("No \"=\" separator found in Text Data,"
2027 " cannot continue.\n");
2028 goto reject;
2029 }
2030 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002031 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002032 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2033 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002034 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002035 } else {
2036 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2037 goto reject;
2038 }
2039
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002040 spin_lock_bh(&conn->cmd_lock);
2041 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2042 spin_unlock_bh(&conn->cmd_lock);
2043
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002044empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002045 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2046
2047 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002048 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2049 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002050 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002051 return -1;
2052
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002053 return 0;
2054 }
2055
2056 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002057
2058reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002059 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2060 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002061}
2062EXPORT_SYMBOL(iscsit_process_text_cmd);
2063
2064static int
2065iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2066 unsigned char *buf)
2067{
2068 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2069 char *text_in = NULL;
2070 u32 payload_length = ntoh24(hdr->dlength);
2071 int rx_size, rc;
2072
2073 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2074 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002075 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002076
2077 rx_size = payload_length;
2078 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002079 u32 checksum = 0, data_crc = 0;
2080 u32 padding = 0, pad_bytes = 0;
2081 int niov = 0, rx_got;
2082 struct kvec iov[3];
2083
2084 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002085 if (!text_in) {
2086 pr_err("Unable to allocate memory for"
2087 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002088 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002089 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002090 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002091
2092 memset(iov, 0, 3 * sizeof(struct kvec));
2093 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002094 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002095
2096 padding = ((-payload_length) & 3);
2097 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002098 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002099 iov[niov++].iov_len = padding;
2100 rx_size += padding;
2101 pr_debug("Receiving %u additional bytes"
2102 " for padding.\n", padding);
2103 }
2104 if (conn->conn_ops->DataDigest) {
2105 iov[niov].iov_base = &checksum;
2106 iov[niov++].iov_len = ISCSI_CRC_LEN;
2107 rx_size += ISCSI_CRC_LEN;
2108 }
2109
2110 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002111 if (rx_got != rx_size)
2112 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002113
2114 if (conn->conn_ops->DataDigest) {
2115 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002116 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002117 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002118 (u8 *)&data_crc);
2119
2120 if (checksum != data_crc) {
2121 pr_err("Text data CRC32C DataDigest"
2122 " 0x%08x does not match computed"
2123 " 0x%08x\n", checksum, data_crc);
2124 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2125 pr_err("Unable to recover from"
2126 " Text Data digest failure while in"
2127 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002128 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002129 } else {
2130 /*
2131 * Silently drop this PDU and let the
2132 * initiator plug the CmdSN gap.
2133 */
2134 pr_debug("Dropping Text"
2135 " Command CmdSN: 0x%08x due to"
2136 " DataCRC error.\n", hdr->cmdsn);
2137 kfree(text_in);
2138 return 0;
2139 }
2140 } else {
2141 pr_debug("Got CRC32C DataDigest"
2142 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002143 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002144 }
2145 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002146 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002147 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002148 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002149 }
2150
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002151 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002152
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002153reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002154 kfree(cmd->text_in_ptr);
2155 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002156 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002157}
2158
2159int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2160{
2161 struct iscsi_conn *conn_p;
2162 struct iscsi_session *sess = conn->sess;
2163
2164 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2165 " for SID: %u.\n", conn->cid, conn->sess->sid);
2166
2167 atomic_set(&sess->session_logout, 1);
2168 atomic_set(&conn->conn_logout_remove, 1);
2169 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2170
2171 iscsit_inc_conn_usage_count(conn);
2172 iscsit_inc_session_usage_count(sess);
2173
2174 spin_lock_bh(&sess->conn_lock);
2175 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2176 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2177 continue;
2178
2179 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2180 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2181 }
2182 spin_unlock_bh(&sess->conn_lock);
2183
2184 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2185
2186 return 0;
2187}
2188
2189int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2190{
2191 struct iscsi_conn *l_conn;
2192 struct iscsi_session *sess = conn->sess;
2193
2194 pr_debug("Received logout request CLOSECONNECTION for CID:"
2195 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2196
2197 /*
2198 * A Logout Request with a CLOSECONNECTION reason code for a CID
2199 * can arrive on a connection with a differing CID.
2200 */
2201 if (conn->cid == cmd->logout_cid) {
2202 spin_lock_bh(&conn->state_lock);
2203 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2204 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2205
2206 atomic_set(&conn->conn_logout_remove, 1);
2207 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2208 iscsit_inc_conn_usage_count(conn);
2209
2210 spin_unlock_bh(&conn->state_lock);
2211 } else {
2212 /*
2213 * Handle all different cid CLOSECONNECTION requests in
2214 * iscsit_logout_post_handler_diffcid() as to give enough
2215 * time for any non immediate command's CmdSN to be
2216 * acknowledged on the connection in question.
2217 *
2218 * Here we simply make sure the CID is still around.
2219 */
2220 l_conn = iscsit_get_conn_from_cid(sess,
2221 cmd->logout_cid);
2222 if (!l_conn) {
2223 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2224 iscsit_add_cmd_to_response_queue(cmd, conn,
2225 cmd->i_state);
2226 return 0;
2227 }
2228
2229 iscsit_dec_conn_usage_count(l_conn);
2230 }
2231
2232 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2233
2234 return 0;
2235}
2236
2237int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2238{
2239 struct iscsi_session *sess = conn->sess;
2240
2241 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2242 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2243
2244 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2245 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2246 " while ERL!=2.\n");
2247 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2248 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2249 return 0;
2250 }
2251
2252 if (conn->cid == cmd->logout_cid) {
2253 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2254 " with CID: %hu on CID: %hu, implementation error.\n",
2255 cmd->logout_cid, conn->cid);
2256 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2257 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2258 return 0;
2259 }
2260
2261 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2262
2263 return 0;
2264}
2265
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002266int
2267iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2268 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002269{
2270 int cmdsn_ret, logout_remove = 0;
2271 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002272 struct iscsi_logout *hdr;
2273 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2274
2275 hdr = (struct iscsi_logout *) buf;
2276 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002277
2278 if (tiqn) {
2279 spin_lock(&tiqn->logout_stats.lock);
2280 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2281 tiqn->logout_stats.normal_logouts++;
2282 else
2283 tiqn->logout_stats.abnormal_logouts++;
2284 spin_unlock(&tiqn->logout_stats.lock);
2285 }
2286
2287 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2288 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2289 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2290 hdr->cid, conn->cid);
2291
2292 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2293 pr_err("Received logout request on connection that"
2294 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002295 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002296 return 0;
2297 }
2298
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002299 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2300 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2301 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2302 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2303 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002304 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2305 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2306 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002307 cmd->logout_reason = reason_code;
2308 cmd->data_direction = DMA_NONE;
2309
2310 /*
2311 * We need to sleep in these cases (by returning 1) until the Logout
2312 * Response gets sent in the tx thread.
2313 */
2314 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2315 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002316 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002317 logout_remove = 1;
2318
2319 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002320 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002321 spin_unlock_bh(&conn->cmd_lock);
2322
2323 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002324 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002325
2326 /*
2327 * Immediate commands are executed, well, immediately.
2328 * Non-Immediate Logout Commands are executed in CmdSN order.
2329 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002330 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002331 int ret = iscsit_execute_cmd(cmd, 0);
2332
2333 if (ret < 0)
2334 return ret;
2335 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002336 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002337 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002338 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002339 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2340 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002341 }
2342
2343 return logout_remove;
2344}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002345EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002346
2347static int iscsit_handle_snack(
2348 struct iscsi_conn *conn,
2349 unsigned char *buf)
2350{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002351 struct iscsi_snack *hdr;
2352
2353 hdr = (struct iscsi_snack *) buf;
2354 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002355
2356 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2357 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2358 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2359 hdr->begrun, hdr->runlength, conn->cid);
2360
2361 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2362 pr_err("Initiator sent SNACK request while in"
2363 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002364 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2365 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002366 }
2367 /*
2368 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2369 * call from inside iscsi_send_recovery_datain_or_r2t().
2370 */
2371 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2372 case 0:
2373 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002374 hdr->itt,
2375 be32_to_cpu(hdr->ttt),
2376 be32_to_cpu(hdr->begrun),
2377 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002378 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002379 return iscsit_handle_status_snack(conn, hdr->itt,
2380 be32_to_cpu(hdr->ttt),
2381 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002382 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002383 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2384 be32_to_cpu(hdr->begrun),
2385 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002386 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2387 /* FIXME: Support R-Data SNACK */
2388 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002389 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2390 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002391 default:
2392 pr_err("Unknown SNACK type 0x%02x, protocol"
2393 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002394 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2395 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002396 }
2397
2398 return 0;
2399}
2400
2401static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2402{
2403 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2404 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2405 wait_for_completion_interruptible_timeout(
2406 &conn->rx_half_close_comp,
2407 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2408 }
2409}
2410
2411static int iscsit_handle_immediate_data(
2412 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002413 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002414 u32 length)
2415{
2416 int iov_ret, rx_got = 0, rx_size = 0;
2417 u32 checksum, iov_count = 0, padding = 0;
2418 struct iscsi_conn *conn = cmd->conn;
2419 struct kvec *iov;
2420
2421 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2422 if (iov_ret < 0)
2423 return IMMEDIATE_DATA_CANNOT_RECOVER;
2424
2425 rx_size = length;
2426 iov_count = iov_ret;
2427 iov = &cmd->iov_data[0];
2428
2429 padding = ((-length) & 3);
2430 if (padding != 0) {
2431 iov[iov_count].iov_base = cmd->pad_bytes;
2432 iov[iov_count++].iov_len = padding;
2433 rx_size += padding;
2434 }
2435
2436 if (conn->conn_ops->DataDigest) {
2437 iov[iov_count].iov_base = &checksum;
2438 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2439 rx_size += ISCSI_CRC_LEN;
2440 }
2441
2442 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2443
2444 iscsit_unmap_iovec(cmd);
2445
2446 if (rx_got != rx_size) {
2447 iscsit_rx_thread_wait_for_tcp(conn);
2448 return IMMEDIATE_DATA_CANNOT_RECOVER;
2449 }
2450
2451 if (conn->conn_ops->DataDigest) {
2452 u32 data_crc;
2453
2454 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2455 cmd->write_data_done, length, padding,
2456 cmd->pad_bytes);
2457
2458 if (checksum != data_crc) {
2459 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2460 " does not match computed 0x%08x\n", checksum,
2461 data_crc);
2462
2463 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2464 pr_err("Unable to recover from"
2465 " Immediate Data digest failure while"
2466 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002467 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002468 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002469 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002470 return IMMEDIATE_DATA_CANNOT_RECOVER;
2471 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002472 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002473 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002474 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002475 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2476 }
2477 } else {
2478 pr_debug("Got CRC32C DataDigest 0x%08x for"
2479 " %u bytes of Immediate Data\n", checksum,
2480 length);
2481 }
2482 }
2483
2484 cmd->write_data_done += length;
2485
Andy Groverebf1d952012-04-03 15:51:24 -07002486 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002487 spin_lock_bh(&cmd->istate_lock);
2488 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2489 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2490 spin_unlock_bh(&cmd->istate_lock);
2491 }
2492
2493 return IMMEDIATE_DATA_NORMAL_OPERATION;
2494}
2495
2496/*
2497 * Called with sess->conn_lock held.
2498 */
2499/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2500 with active network interface */
2501static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2502{
2503 struct iscsi_cmd *cmd;
2504 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002505 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002506
2507 /*
2508 * Only send a Asynchronous Message on connections whos network
2509 * interface is still functional.
2510 */
2511 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2512 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2513 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002514 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002515 break;
2516 }
2517 }
2518
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002519 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002520 return;
2521
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002522 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002523 if (!cmd) {
2524 iscsit_dec_conn_usage_count(conn_p);
2525 return;
2526 }
2527
2528 cmd->logout_cid = conn->cid;
2529 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2530 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2531
2532 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002533 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002534 spin_unlock_bh(&conn_p->cmd_lock);
2535
2536 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2537 iscsit_dec_conn_usage_count(conn_p);
2538}
2539
2540static int iscsit_send_conn_drop_async_message(
2541 struct iscsi_cmd *cmd,
2542 struct iscsi_conn *conn)
2543{
2544 struct iscsi_async *hdr;
2545
2546 cmd->tx_size = ISCSI_HDR_LEN;
2547 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2548
2549 hdr = (struct iscsi_async *) cmd->pdu;
2550 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2551 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002552 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002553 cmd->targ_xfer_tag = 0xFFFFFFFF;
2554 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2555 cmd->stat_sn = conn->stat_sn++;
2556 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2557 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2558 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2559 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2560 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2561 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2562 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2563
2564 if (conn->conn_ops->HeaderDigest) {
2565 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2566
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002567 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2568 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002569
2570 cmd->tx_size += ISCSI_CRC_LEN;
2571 pr_debug("Attaching CRC32C HeaderDigest to"
2572 " Async Message 0x%08x\n", *header_digest);
2573 }
2574
2575 cmd->iov_misc[0].iov_base = cmd->pdu;
2576 cmd->iov_misc[0].iov_len = cmd->tx_size;
2577 cmd->iov_misc_count = 1;
2578
2579 pr_debug("Sending Connection Dropped Async Message StatSN:"
2580 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2581 cmd->logout_cid, conn->cid);
2582 return 0;
2583}
2584
Andy Grover6f3c0e62012-04-03 15:51:09 -07002585static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2586{
2587 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2588 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2589 wait_for_completion_interruptible_timeout(
2590 &conn->tx_half_close_comp,
2591 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2592 }
2593}
2594
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002595static void
2596iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2597 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2598 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002599{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002600 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2601 hdr->flags = datain->flags;
2602 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2603 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2604 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2605 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2606 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2607 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2608 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2609 }
2610 }
2611 hton24(hdr->dlength, datain->length);
2612 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2613 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2614 (struct scsi_lun *)&hdr->lun);
2615 else
2616 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2617
2618 hdr->itt = cmd->init_task_tag;
2619
2620 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2621 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2622 else
2623 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2624 if (set_statsn)
2625 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2626 else
2627 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2628
2629 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2630 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2631 hdr->datasn = cpu_to_be32(datain->data_sn);
2632 hdr->offset = cpu_to_be32(datain->offset);
2633
2634 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2635 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2636 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2637 ntohl(hdr->offset), datain->length, conn->cid);
2638}
2639
2640static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2641{
2642 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002643 struct iscsi_datain datain;
2644 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002645 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002646 u32 iov_count = 0, tx_size = 0;
2647 int eodr = 0, ret, iov_ret;
2648 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002649
2650 memset(&datain, 0, sizeof(struct iscsi_datain));
2651 dr = iscsit_get_datain_values(cmd, &datain);
2652 if (!dr) {
2653 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2654 cmd->init_task_tag);
2655 return -1;
2656 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002657 /*
2658 * Be paranoid and double check the logic for now.
2659 */
Andy Groverebf1d952012-04-03 15:51:24 -07002660 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002661 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2662 " datain.length: %u exceeds cmd->data_length: %u\n",
2663 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002664 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002665 return -1;
2666 }
2667
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002668 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002669 /*
2670 * Special case for successfully execution w/ both DATAIN
2671 * and Sense Data.
2672 */
2673 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2674 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2675 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2676 else {
2677 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2678 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2679 iscsit_increment_maxcmdsn(cmd, conn->sess);
2680 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002681 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002682 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002683 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2684 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002685 }
2686
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002687 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002688
2689 iov = &cmd->iov_data[0];
2690 iov[iov_count].iov_base = cmd->pdu;
2691 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2692 tx_size += ISCSI_HDR_LEN;
2693
2694 if (conn->conn_ops->HeaderDigest) {
2695 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2696
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002697 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2698 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002699
2700 iov[0].iov_len += ISCSI_CRC_LEN;
2701 tx_size += ISCSI_CRC_LEN;
2702
2703 pr_debug("Attaching CRC32 HeaderDigest"
2704 " for DataIN PDU 0x%08x\n", *header_digest);
2705 }
2706
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002707 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2708 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002709 if (iov_ret < 0)
2710 return -1;
2711
2712 iov_count += iov_ret;
2713 tx_size += datain.length;
2714
2715 cmd->padding = ((-datain.length) & 3);
2716 if (cmd->padding) {
2717 iov[iov_count].iov_base = cmd->pad_bytes;
2718 iov[iov_count++].iov_len = cmd->padding;
2719 tx_size += cmd->padding;
2720
2721 pr_debug("Attaching %u padding bytes\n",
2722 cmd->padding);
2723 }
2724 if (conn->conn_ops->DataDigest) {
2725 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2726 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2727
2728 iov[iov_count].iov_base = &cmd->data_crc;
2729 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2730 tx_size += ISCSI_CRC_LEN;
2731
2732 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2733 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2734 }
2735
2736 cmd->iov_data_count = iov_count;
2737 cmd->tx_size = tx_size;
2738
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02002739 ret = iscsit_fe_sendpage_sg(cmd, conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07002740
2741 iscsit_unmap_iovec(cmd);
2742
2743 if (ret < 0) {
2744 iscsit_tx_thread_wait_for_tcp(conn);
2745 return ret;
2746 }
2747
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002748 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002749 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002750 2 : 1;
2751 iscsit_free_datain_req(cmd, dr);
2752 }
2753
Andy Grover6f3c0e62012-04-03 15:51:09 -07002754 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002755}
2756
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002757int
2758iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2759 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002760{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002761 struct iscsi_conn *logout_conn = NULL;
2762 struct iscsi_conn_recovery *cr = NULL;
2763 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002764 /*
2765 * The actual shutting down of Sessions and/or Connections
2766 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2767 * is done in scsi_logout_post_handler().
2768 */
2769 switch (cmd->logout_reason) {
2770 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2771 pr_debug("iSCSI session logout successful, setting"
2772 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2773 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2774 break;
2775 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2776 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2777 break;
2778 /*
2779 * For CLOSECONNECTION logout requests carrying
2780 * a matching logout CID -> local CID, the reference
2781 * for the local CID will have been incremented in
2782 * iscsi_logout_closeconnection().
2783 *
2784 * For CLOSECONNECTION logout requests carrying
2785 * a different CID than the connection it arrived
2786 * on, the connection responding to cmd->logout_cid
2787 * is stopped in iscsit_logout_post_handler_diffcid().
2788 */
2789
2790 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2791 " successful.\n", cmd->logout_cid, conn->cid);
2792 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2793 break;
2794 case ISCSI_LOGOUT_REASON_RECOVERY:
2795 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2796 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2797 break;
2798 /*
2799 * If the connection is still active from our point of view
2800 * force connection recovery to occur.
2801 */
2802 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2803 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002804 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002805 iscsit_connection_reinstatement_rcfr(logout_conn);
2806 iscsit_dec_conn_usage_count(logout_conn);
2807 }
2808
2809 cr = iscsit_get_inactive_connection_recovery_entry(
2810 conn->sess, cmd->logout_cid);
2811 if (!cr) {
2812 pr_err("Unable to locate CID: %hu for"
2813 " REMOVECONNFORRECOVERY Logout Request.\n",
2814 cmd->logout_cid);
2815 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2816 break;
2817 }
2818
2819 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2820
2821 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2822 " for recovery for CID: %hu on CID: %hu successful.\n",
2823 cmd->logout_cid, conn->cid);
2824 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2825 break;
2826 default:
2827 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2828 cmd->logout_reason);
2829 return -1;
2830 }
2831
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002832 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2833 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2834 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002835 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002836 cmd->stat_sn = conn->stat_sn++;
2837 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2838
2839 iscsit_increment_maxcmdsn(cmd, conn->sess);
2840 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2841 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2842
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002843 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2844 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2845 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2846 cmd->logout_cid, conn->cid);
2847
2848 return 0;
2849}
2850EXPORT_SYMBOL(iscsit_build_logout_rsp);
2851
2852static int
2853iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2854{
2855 struct kvec *iov;
2856 int niov = 0, tx_size, rc;
2857
2858 rc = iscsit_build_logout_rsp(cmd, conn,
2859 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2860 if (rc < 0)
2861 return rc;
2862
2863 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002864 iov = &cmd->iov_misc[0];
2865 iov[niov].iov_base = cmd->pdu;
2866 iov[niov++].iov_len = ISCSI_HDR_LEN;
2867
2868 if (conn->conn_ops->HeaderDigest) {
2869 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2870
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002871 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2872 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002873
2874 iov[0].iov_len += ISCSI_CRC_LEN;
2875 tx_size += ISCSI_CRC_LEN;
2876 pr_debug("Attaching CRC32C HeaderDigest to"
2877 " Logout Response 0x%08x\n", *header_digest);
2878 }
2879 cmd->iov_misc_count = niov;
2880 cmd->tx_size = tx_size;
2881
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002882 return 0;
2883}
2884
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002885void
2886iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2887 struct iscsi_nopin *hdr, bool nopout_response)
2888{
2889 hdr->opcode = ISCSI_OP_NOOP_IN;
2890 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2891 hton24(hdr->dlength, cmd->buf_ptr_size);
2892 if (nopout_response)
2893 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2894 hdr->itt = cmd->init_task_tag;
2895 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2896 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2897 conn->stat_sn;
2898 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2899
2900 if (nopout_response)
2901 iscsit_increment_maxcmdsn(cmd, conn->sess);
2902
2903 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2904 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2905
2906 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2907 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2908 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2909 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2910}
2911EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2912
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002913/*
2914 * Unsolicited NOPIN, either requesting a response or not.
2915 */
2916static int iscsit_send_unsolicited_nopin(
2917 struct iscsi_cmd *cmd,
2918 struct iscsi_conn *conn,
2919 int want_response)
2920{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002921 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2922 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002923
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002924 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002925
2926 if (conn->conn_ops->HeaderDigest) {
2927 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2928
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002929 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2930 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002931
2932 tx_size += ISCSI_CRC_LEN;
2933 pr_debug("Attaching CRC32C HeaderDigest to"
2934 " NopIN 0x%08x\n", *header_digest);
2935 }
2936
2937 cmd->iov_misc[0].iov_base = cmd->pdu;
2938 cmd->iov_misc[0].iov_len = tx_size;
2939 cmd->iov_misc_count = 1;
2940 cmd->tx_size = tx_size;
2941
2942 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2943 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2944
Andy Grover6f3c0e62012-04-03 15:51:09 -07002945 ret = iscsit_send_tx_data(cmd, conn, 1);
2946 if (ret < 0) {
2947 iscsit_tx_thread_wait_for_tcp(conn);
2948 return ret;
2949 }
2950
2951 spin_lock_bh(&cmd->istate_lock);
2952 cmd->i_state = want_response ?
2953 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2954 spin_unlock_bh(&cmd->istate_lock);
2955
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002956 return 0;
2957}
2958
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002959static int
2960iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002961{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002962 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002963 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002964 u32 padding = 0;
2965 int niov = 0, tx_size;
2966
2967 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002968
2969 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002970 iov = &cmd->iov_misc[0];
2971 iov[niov].iov_base = cmd->pdu;
2972 iov[niov++].iov_len = ISCSI_HDR_LEN;
2973
2974 if (conn->conn_ops->HeaderDigest) {
2975 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2976
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002977 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2978 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002979
2980 iov[0].iov_len += ISCSI_CRC_LEN;
2981 tx_size += ISCSI_CRC_LEN;
2982 pr_debug("Attaching CRC32C HeaderDigest"
2983 " to NopIn 0x%08x\n", *header_digest);
2984 }
2985
2986 /*
2987 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2988 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2989 */
2990 if (cmd->buf_ptr_size) {
2991 iov[niov].iov_base = cmd->buf_ptr;
2992 iov[niov++].iov_len = cmd->buf_ptr_size;
2993 tx_size += cmd->buf_ptr_size;
2994
2995 pr_debug("Echoing back %u bytes of ping"
2996 " data.\n", cmd->buf_ptr_size);
2997
2998 padding = ((-cmd->buf_ptr_size) & 3);
2999 if (padding != 0) {
3000 iov[niov].iov_base = &cmd->pad_bytes;
3001 iov[niov++].iov_len = padding;
3002 tx_size += padding;
3003 pr_debug("Attaching %u additional"
3004 " padding bytes.\n", padding);
3005 }
3006 if (conn->conn_ops->DataDigest) {
3007 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3008 cmd->buf_ptr, cmd->buf_ptr_size,
3009 padding, (u8 *)&cmd->pad_bytes,
3010 (u8 *)&cmd->data_crc);
3011
3012 iov[niov].iov_base = &cmd->data_crc;
3013 iov[niov++].iov_len = ISCSI_CRC_LEN;
3014 tx_size += ISCSI_CRC_LEN;
3015 pr_debug("Attached DataDigest for %u"
3016 " bytes of ping data, CRC 0x%08x\n",
3017 cmd->buf_ptr_size, cmd->data_crc);
3018 }
3019 }
3020
3021 cmd->iov_misc_count = niov;
3022 cmd->tx_size = tx_size;
3023
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003024 return 0;
3025}
3026
Andy Grover6f3c0e62012-04-03 15:51:09 -07003027static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028 struct iscsi_cmd *cmd,
3029 struct iscsi_conn *conn)
3030{
3031 int tx_size = 0;
3032 struct iscsi_r2t *r2t;
3033 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003034 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003035
3036 r2t = iscsit_get_r2t_from_list(cmd);
3037 if (!r2t)
3038 return -1;
3039
3040 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3041 memset(hdr, 0, ISCSI_HDR_LEN);
3042 hdr->opcode = ISCSI_OP_R2T;
3043 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3044 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3045 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003046 hdr->itt = cmd->init_task_tag;
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02003047 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003048 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3049 hdr->statsn = cpu_to_be32(conn->stat_sn);
3050 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3051 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3052 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3053 hdr->data_offset = cpu_to_be32(r2t->offset);
3054 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3055
3056 cmd->iov_misc[0].iov_base = cmd->pdu;
3057 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3058 tx_size += ISCSI_HDR_LEN;
3059
3060 if (conn->conn_ops->HeaderDigest) {
3061 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3062
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003063 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3064 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003065
3066 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3067 tx_size += ISCSI_CRC_LEN;
3068 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3069 " PDU 0x%08x\n", *header_digest);
3070 }
3071
3072 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3073 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3074 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3075 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3076 r2t->offset, r2t->xfer_len, conn->cid);
3077
3078 cmd->iov_misc_count = 1;
3079 cmd->tx_size = tx_size;
3080
3081 spin_lock_bh(&cmd->r2t_lock);
3082 r2t->sent_r2t = 1;
3083 spin_unlock_bh(&cmd->r2t_lock);
3084
Andy Grover6f3c0e62012-04-03 15:51:09 -07003085 ret = iscsit_send_tx_data(cmd, conn, 1);
3086 if (ret < 0) {
3087 iscsit_tx_thread_wait_for_tcp(conn);
3088 return ret;
3089 }
3090
3091 spin_lock_bh(&cmd->dataout_timeout_lock);
3092 iscsit_start_dataout_timer(cmd, conn);
3093 spin_unlock_bh(&cmd->dataout_timeout_lock);
3094
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003095 return 0;
3096}
3097
3098/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003099 * @recovery: If called from iscsi_task_reassign_complete_write() for
3100 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003101 */
3102int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003103 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003104 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003105 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003106{
3107 int first_r2t = 1;
3108 u32 offset = 0, xfer_len = 0;
3109
3110 spin_lock_bh(&cmd->r2t_lock);
3111 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3112 spin_unlock_bh(&cmd->r2t_lock);
3113 return 0;
3114 }
3115
Andy Grover8b1e1242012-04-03 15:51:12 -07003116 if (conn->sess->sess_ops->DataSequenceInOrder &&
3117 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003118 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003119
3120 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3121 if (conn->sess->sess_ops->DataSequenceInOrder) {
3122 offset = cmd->r2t_offset;
3123
Andy Grover8b1e1242012-04-03 15:51:12 -07003124 if (first_r2t && recovery) {
3125 int new_data_end = offset +
3126 conn->sess->sess_ops->MaxBurstLength -
3127 cmd->next_burst_len;
3128
Andy Groverebf1d952012-04-03 15:51:24 -07003129 if (new_data_end > cmd->se_cmd.data_length)
3130 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003131 else
3132 xfer_len =
3133 conn->sess->sess_ops->MaxBurstLength -
3134 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003135 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003136 int new_data_end = offset +
3137 conn->sess->sess_ops->MaxBurstLength;
3138
Andy Groverebf1d952012-04-03 15:51:24 -07003139 if (new_data_end > cmd->se_cmd.data_length)
3140 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003141 else
3142 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003143 }
3144 cmd->r2t_offset += xfer_len;
3145
Andy Groverebf1d952012-04-03 15:51:24 -07003146 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003147 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3148 } else {
3149 struct iscsi_seq *seq;
3150
3151 seq = iscsit_get_seq_holder_for_r2t(cmd);
3152 if (!seq) {
3153 spin_unlock_bh(&cmd->r2t_lock);
3154 return -1;
3155 }
3156
3157 offset = seq->offset;
3158 xfer_len = seq->xfer_len;
3159
3160 if (cmd->seq_send_order == cmd->seq_count)
3161 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3162 }
3163 cmd->outstanding_r2ts++;
3164 first_r2t = 0;
3165
3166 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3167 spin_unlock_bh(&cmd->r2t_lock);
3168 return -1;
3169 }
3170
3171 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3172 break;
3173 }
3174 spin_unlock_bh(&cmd->r2t_lock);
3175
3176 return 0;
3177}
3178
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003179void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3180 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003181{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003182 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003183 cmd->stat_sn = conn->stat_sn++;
3184
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003185 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003186
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003187 memset(hdr, 0, ISCSI_HDR_LEN);
3188 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3189 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3190 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3191 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003192 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003193 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3194 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003195 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003196 }
3197 hdr->response = cmd->iscsi_response;
3198 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003199 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003200 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3201
3202 iscsit_increment_maxcmdsn(cmd, conn->sess);
3203 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3204 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3205
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003206 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3207 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3208 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3209 cmd->se_cmd.scsi_status, conn->cid);
3210}
3211EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3212
3213static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3214{
3215 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3216 struct kvec *iov;
3217 u32 padding = 0, tx_size = 0;
3218 int iov_count = 0;
3219 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3220
3221 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3222
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003223 iov = &cmd->iov_misc[0];
3224 iov[iov_count].iov_base = cmd->pdu;
3225 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3226 tx_size += ISCSI_HDR_LEN;
3227
3228 /*
3229 * Attach SENSE DATA payload to iSCSI Response PDU
3230 */
3231 if (cmd->se_cmd.sense_buffer &&
3232 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3233 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003234 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3235 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3236
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003237 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003238 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003239 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003240 iov[iov_count++].iov_len =
3241 (cmd->se_cmd.scsi_sense_length + padding);
3242 tx_size += cmd->se_cmd.scsi_sense_length;
3243
3244 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003245 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003246 cmd->se_cmd.scsi_sense_length, 0, padding);
3247 tx_size += padding;
3248 pr_debug("Adding %u bytes of padding to"
3249 " SENSE.\n", padding);
3250 }
3251
3252 if (conn->conn_ops->DataDigest) {
3253 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003254 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003255 (cmd->se_cmd.scsi_sense_length + padding),
3256 0, NULL, (u8 *)&cmd->data_crc);
3257
3258 iov[iov_count].iov_base = &cmd->data_crc;
3259 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3260 tx_size += ISCSI_CRC_LEN;
3261
3262 pr_debug("Attaching CRC32 DataDigest for"
3263 " SENSE, %u bytes CRC 0x%08x\n",
3264 (cmd->se_cmd.scsi_sense_length + padding),
3265 cmd->data_crc);
3266 }
3267
3268 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3269 " Response PDU\n",
3270 cmd->se_cmd.scsi_sense_length);
3271 }
3272
3273 if (conn->conn_ops->HeaderDigest) {
3274 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3275
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003276 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3277 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003278
3279 iov[0].iov_len += ISCSI_CRC_LEN;
3280 tx_size += ISCSI_CRC_LEN;
3281 pr_debug("Attaching CRC32 HeaderDigest for Response"
3282 " PDU 0x%08x\n", *header_digest);
3283 }
3284
3285 cmd->iov_misc_count = iov_count;
3286 cmd->tx_size = tx_size;
3287
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003288 return 0;
3289}
3290
3291static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3292{
3293 switch (se_tmr->response) {
3294 case TMR_FUNCTION_COMPLETE:
3295 return ISCSI_TMF_RSP_COMPLETE;
3296 case TMR_TASK_DOES_NOT_EXIST:
3297 return ISCSI_TMF_RSP_NO_TASK;
3298 case TMR_LUN_DOES_NOT_EXIST:
3299 return ISCSI_TMF_RSP_NO_LUN;
3300 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3301 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003302 case TMR_FUNCTION_REJECTED:
3303 default:
3304 return ISCSI_TMF_RSP_REJECTED;
3305 }
3306}
3307
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003308void
3309iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3310 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003311{
3312 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003313
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003314 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003315 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003316 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003317 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003318 cmd->stat_sn = conn->stat_sn++;
3319 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3320
3321 iscsit_increment_maxcmdsn(cmd, conn->sess);
3322 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3323 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3324
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003325 pr_debug("Built Task Management Response ITT: 0x%08x,"
3326 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3327 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3328}
3329EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3330
3331static int
3332iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3333{
3334 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3335 u32 tx_size = 0;
3336
3337 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3338
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003339 cmd->iov_misc[0].iov_base = cmd->pdu;
3340 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3341 tx_size += ISCSI_HDR_LEN;
3342
3343 if (conn->conn_ops->HeaderDigest) {
3344 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3345
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003346 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3347 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003348
3349 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3350 tx_size += ISCSI_CRC_LEN;
3351 pr_debug("Attaching CRC32 HeaderDigest for Task"
3352 " Mgmt Response PDU 0x%08x\n", *header_digest);
3353 }
3354
3355 cmd->iov_misc_count = 1;
3356 cmd->tx_size = tx_size;
3357
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003358 return 0;
3359}
3360
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003361static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3362{
3363 bool ret = false;
3364
3365 if (np->np_sockaddr.ss_family == AF_INET6) {
3366 const struct sockaddr_in6 sin6 = {
3367 .sin6_addr = IN6ADDR_ANY_INIT };
3368 struct sockaddr_in6 *sock_in6 =
3369 (struct sockaddr_in6 *)&np->np_sockaddr;
3370
3371 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3372 sin6.sin6_addr.s6_addr, 16))
3373 ret = true;
3374 } else {
3375 struct sockaddr_in * sock_in =
3376 (struct sockaddr_in *)&np->np_sockaddr;
3377
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003378 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003379 ret = true;
3380 }
3381
3382 return ret;
3383}
3384
Andy Grover8b1e1242012-04-03 15:51:12 -07003385#define SENDTARGETS_BUF_LIMIT 32768U
3386
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003387static int
3388iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003389 enum iscsit_transport_type network_transport,
3390 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003391{
3392 char *payload = NULL;
3393 struct iscsi_conn *conn = cmd->conn;
3394 struct iscsi_portal_group *tpg;
3395 struct iscsi_tiqn *tiqn;
3396 struct iscsi_tpg_np *tpg_np;
3397 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003398 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003399 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003400 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003401
Sagi Grimbergbe7dcfb62015-01-26 12:49:06 +02003402 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003403 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003404
3405 payload = kzalloc(buffer_len, GFP_KERNEL);
3406 if (!payload) {
3407 pr_err("Unable to allocate memory for sendtargets"
3408 " response.\n");
3409 return -ENOMEM;
3410 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003411 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003412 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003413 * explicit case..
3414 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003415 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003416 text_ptr = strchr(text_in, '=');
3417 if (!text_ptr) {
3418 pr_err("Unable to locate '=' string in text_in:"
3419 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003420 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003421 return -EINVAL;
3422 }
3423 /*
3424 * Skip over '=' character..
3425 */
3426 text_ptr += 1;
3427 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003428
3429 spin_lock(&tiqn_lock);
3430 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003431 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003432 strcmp(tiqn->tiqn, text_ptr)) {
3433 continue;
3434 }
3435
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003436 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003437
3438 spin_lock(&tiqn->tiqn_tpg_lock);
3439 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3440
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003441 /* If demo_mode_discovery=0 and generate_node_acls=0
3442 * (demo mode dislabed) do not return
3443 * TargetName+TargetAddress unless a NodeACL exists.
3444 */
3445
3446 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3447 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3448 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3449 cmd->conn->sess->sess_ops->InitiatorName))) {
3450 continue;
3451 }
3452
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003453 spin_lock(&tpg->tpg_state_lock);
3454 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3455 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3456 spin_unlock(&tpg->tpg_state_lock);
3457 continue;
3458 }
3459 spin_unlock(&tpg->tpg_state_lock);
3460
3461 spin_lock(&tpg->tpg_np_lock);
3462 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3463 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003464 struct iscsi_np *np = tpg_np->tpg_np;
3465 bool inaddr_any = iscsit_check_inaddr_any(np);
Andy Grover1997e622015-03-31 10:43:18 -07003466 char *fmt_str;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003467
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003468 if (np->np_network_transport != network_transport)
3469 continue;
3470
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003471 if (!target_name_printed) {
3472 len = sprintf(buf, "TargetName=%s",
3473 tiqn->tiqn);
3474 len += 1;
3475
3476 if ((len + payload_len) > buffer_len) {
3477 spin_unlock(&tpg->tpg_np_lock);
3478 spin_unlock(&tiqn->tiqn_tpg_lock);
3479 end_of_buf = 1;
3480 goto eob;
3481 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003482
3483 if (skip_bytes && len <= skip_bytes) {
3484 skip_bytes -= len;
3485 } else {
3486 memcpy(payload + payload_len, buf, len);
3487 payload_len += len;
3488 target_name_printed = 1;
3489 if (len > skip_bytes)
3490 skip_bytes = 0;
3491 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003492 }
3493
Andy Grover1997e622015-03-31 10:43:18 -07003494 if (np->np_sockaddr.ss_family == AF_INET6)
3495 fmt_str = "TargetAddress=[%s]:%hu,%hu";
3496 else
3497 fmt_str = "TargetAddress=%s:%hu,%hu";
3498
3499 len = sprintf(buf, fmt_str,
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02003500 inaddr_any ? conn->local_ip : np->np_ip,
Steven Allenf2774f42014-10-15 10:59:21 -07003501 np->np_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003502 tpg->tpgt);
3503 len += 1;
3504
3505 if ((len + payload_len) > buffer_len) {
3506 spin_unlock(&tpg->tpg_np_lock);
3507 spin_unlock(&tiqn->tiqn_tpg_lock);
3508 end_of_buf = 1;
3509 goto eob;
3510 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003511
3512 if (skip_bytes && len <= skip_bytes) {
3513 skip_bytes -= len;
3514 } else {
3515 memcpy(payload + payload_len, buf, len);
3516 payload_len += len;
3517 if (len > skip_bytes)
3518 skip_bytes = 0;
3519 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003520 }
3521 spin_unlock(&tpg->tpg_np_lock);
3522 }
3523 spin_unlock(&tiqn->tiqn_tpg_lock);
3524eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003525 if (end_of_buf) {
3526 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003527 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003528 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003529
Andy Grover8060b8d2015-01-09 15:13:08 -08003530 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003531 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003532 }
3533 spin_unlock(&tiqn_lock);
3534
3535 cmd->buf_ptr = payload;
3536
3537 return payload_len;
3538}
3539
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003540int
3541iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003542 struct iscsi_text_rsp *hdr,
3543 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003544{
3545 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003546 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003547
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003548 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3549 cmd->read_data_done,
3550 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003551 if (text_length < 0)
3552 return text_length;
3553
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003554 if (completed) {
3555 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3556 } else {
3557 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3558 cmd->read_data_done += text_length;
3559 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3560 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3561 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003562 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003563 padding = ((-text_length) & 3);
3564 hton24(hdr->dlength, text_length);
3565 hdr->itt = cmd->init_task_tag;
3566 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3567 cmd->stat_sn = conn->stat_sn++;
3568 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3569
3570 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003571 /*
3572 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3573 * correctly increment MaxCmdSN for each response answering a
3574 * non immediate text request with a valid CmdSN.
3575 */
3576 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003577 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3578 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3579
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003580 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3581 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3582 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3583 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3584 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003585
3586 return text_length + padding;
3587}
3588EXPORT_SYMBOL(iscsit_build_text_rsp);
3589
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003590static int iscsit_send_text_rsp(
3591 struct iscsi_cmd *cmd,
3592 struct iscsi_conn *conn)
3593{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003594 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003595 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003596 u32 tx_size = 0;
3597 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003598
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003599 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003600 if (rc < 0)
3601 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003602
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003603 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003604 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003605 iov[iov_count].iov_base = cmd->pdu;
3606 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3607 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003608 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003609
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003610 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003611
3612 if (conn->conn_ops->HeaderDigest) {
3613 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3614
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003615 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3616 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003617
3618 iov[0].iov_len += ISCSI_CRC_LEN;
3619 tx_size += ISCSI_CRC_LEN;
3620 pr_debug("Attaching CRC32 HeaderDigest for"
3621 " Text Response PDU 0x%08x\n", *header_digest);
3622 }
3623
3624 if (conn->conn_ops->DataDigest) {
3625 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003626 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003627 0, NULL, (u8 *)&cmd->data_crc);
3628
3629 iov[iov_count].iov_base = &cmd->data_crc;
3630 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3631 tx_size += ISCSI_CRC_LEN;
3632
3633 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003634 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003635 cmd->data_crc);
3636 }
3637
3638 cmd->iov_misc_count = iov_count;
3639 cmd->tx_size = tx_size;
3640
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003641 return 0;
3642}
3643
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003644void
3645iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3646 struct iscsi_reject *hdr)
3647{
3648 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003649 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003650 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3651 hton24(hdr->dlength, ISCSI_HDR_LEN);
3652 hdr->ffffffff = cpu_to_be32(0xffffffff);
3653 cmd->stat_sn = conn->stat_sn++;
3654 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3655 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3656 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3657
3658}
3659EXPORT_SYMBOL(iscsit_build_reject);
3660
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003661static int iscsit_send_reject(
3662 struct iscsi_cmd *cmd,
3663 struct iscsi_conn *conn)
3664{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003665 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003666 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003667 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003668
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003669 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003670
3671 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003672 iov[iov_count].iov_base = cmd->pdu;
3673 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3674 iov[iov_count].iov_base = cmd->buf_ptr;
3675 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3676
3677 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3678
3679 if (conn->conn_ops->HeaderDigest) {
3680 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3681
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003682 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3683 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003684
3685 iov[0].iov_len += ISCSI_CRC_LEN;
3686 tx_size += ISCSI_CRC_LEN;
3687 pr_debug("Attaching CRC32 HeaderDigest for"
3688 " REJECT PDU 0x%08x\n", *header_digest);
3689 }
3690
3691 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003692 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3693 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003694
3695 iov[iov_count].iov_base = &cmd->data_crc;
3696 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3697 tx_size += ISCSI_CRC_LEN;
3698 pr_debug("Attaching CRC32 DataDigest for REJECT"
3699 " PDU 0x%08x\n", cmd->data_crc);
3700 }
3701
3702 cmd->iov_misc_count = iov_count;
3703 cmd->tx_size = tx_size;
3704
3705 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3706 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3707
3708 return 0;
3709}
3710
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003711void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3712{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003713 int ord, cpu;
3714 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003715 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3716 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003717 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003718 * Here we use bitmap_id to determine which CPU that this
3719 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003720 * execute upon.
3721 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003722 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003723 for_each_online_cpu(cpu) {
3724 if (ord-- == 0) {
3725 cpumask_set_cpu(cpu, conn->conn_cpumask);
3726 return;
3727 }
3728 }
3729 /*
3730 * This should never be reached..
3731 */
3732 dump_stack();
3733 cpumask_setall(conn->conn_cpumask);
3734}
3735
3736static inline void iscsit_thread_check_cpumask(
3737 struct iscsi_conn *conn,
3738 struct task_struct *p,
3739 int mode)
3740{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003741 /*
3742 * mode == 1 signals iscsi_target_tx_thread() usage.
3743 * mode == 0 signals iscsi_target_rx_thread() usage.
3744 */
3745 if (mode == 1) {
3746 if (!conn->conn_tx_reset_cpumask)
3747 return;
3748 conn->conn_tx_reset_cpumask = 0;
3749 } else {
3750 if (!conn->conn_rx_reset_cpumask)
3751 return;
3752 conn->conn_rx_reset_cpumask = 0;
3753 }
3754 /*
3755 * Update the CPU mask for this single kthread so that
3756 * both TX and RX kthreads are scheduled to run on the
3757 * same CPU.
3758 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003759 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3760}
3761
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003762static int
3763iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003764{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003765 int ret;
3766
3767 switch (state) {
3768 case ISTATE_SEND_R2T:
3769 ret = iscsit_send_r2t(cmd, conn);
3770 if (ret < 0)
3771 goto err;
3772 break;
3773 case ISTATE_REMOVE:
3774 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003775 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003776 spin_unlock_bh(&conn->cmd_lock);
3777
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003778 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003779 break;
3780 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3781 iscsit_mod_nopin_response_timer(conn);
3782 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3783 if (ret < 0)
3784 goto err;
3785 break;
3786 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3787 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3788 if (ret < 0)
3789 goto err;
3790 break;
3791 default:
3792 pr_err("Unknown Opcode: 0x%02x ITT:"
3793 " 0x%08x, i_state: %d on CID: %hu\n",
3794 cmd->iscsi_opcode, cmd->init_task_tag, state,
3795 conn->cid);
3796 goto err;
3797 }
3798
3799 return 0;
3800
3801err:
3802 return -1;
3803}
3804
3805static int
3806iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3807{
3808 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003809 struct iscsi_queue_req *qr;
3810 struct iscsi_cmd *cmd;
3811 u8 state;
3812 int ret;
3813
3814 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3815 atomic_set(&conn->check_immediate_queue, 0);
3816 cmd = qr->cmd;
3817 state = qr->state;
3818 kmem_cache_free(lio_qr_cache, qr);
3819
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003820 ret = t->iscsit_immediate_queue(conn, cmd, state);
3821 if (ret < 0)
3822 return ret;
3823 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003824
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003825 return 0;
3826}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003827
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003828static int
3829iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3830{
3831 int ret;
3832
3833check_rsp_state:
3834 switch (state) {
3835 case ISTATE_SEND_DATAIN:
3836 ret = iscsit_send_datain(cmd, conn);
3837 if (ret < 0)
3838 goto err;
3839 else if (!ret)
3840 /* more drs */
3841 goto check_rsp_state;
3842 else if (ret == 1) {
3843 /* all done */
3844 spin_lock_bh(&cmd->istate_lock);
3845 cmd->i_state = ISTATE_SENT_STATUS;
3846 spin_unlock_bh(&cmd->istate_lock);
3847
3848 if (atomic_read(&conn->check_immediate_queue))
3849 return 1;
3850
3851 return 0;
3852 } else if (ret == 2) {
3853 /* Still must send status,
3854 SCF_TRANSPORT_TASK_SENSE was set */
3855 spin_lock_bh(&cmd->istate_lock);
3856 cmd->i_state = ISTATE_SEND_STATUS;
3857 spin_unlock_bh(&cmd->istate_lock);
3858 state = ISTATE_SEND_STATUS;
3859 goto check_rsp_state;
3860 }
3861
3862 break;
3863 case ISTATE_SEND_STATUS:
3864 case ISTATE_SEND_STATUS_RECOVERY:
3865 ret = iscsit_send_response(cmd, conn);
3866 break;
3867 case ISTATE_SEND_LOGOUTRSP:
3868 ret = iscsit_send_logout(cmd, conn);
3869 break;
3870 case ISTATE_SEND_ASYNCMSG:
3871 ret = iscsit_send_conn_drop_async_message(
3872 cmd, conn);
3873 break;
3874 case ISTATE_SEND_NOPIN:
3875 ret = iscsit_send_nopin(cmd, conn);
3876 break;
3877 case ISTATE_SEND_REJECT:
3878 ret = iscsit_send_reject(cmd, conn);
3879 break;
3880 case ISTATE_SEND_TASKMGTRSP:
3881 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3882 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003883 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003884 ret = iscsit_tmr_post_handler(cmd, conn);
3885 if (ret != 0)
3886 iscsit_fall_back_to_erl0(conn->sess);
3887 break;
3888 case ISTATE_SEND_TEXTRSP:
3889 ret = iscsit_send_text_rsp(cmd, conn);
3890 break;
3891 default:
3892 pr_err("Unknown Opcode: 0x%02x ITT:"
3893 " 0x%08x, i_state: %d on CID: %hu\n",
3894 cmd->iscsi_opcode, cmd->init_task_tag,
3895 state, conn->cid);
3896 goto err;
3897 }
3898 if (ret < 0)
3899 goto err;
3900
3901 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3902 iscsit_tx_thread_wait_for_tcp(conn);
3903 iscsit_unmap_iovec(cmd);
3904 goto err;
3905 }
3906 iscsit_unmap_iovec(cmd);
3907
3908 switch (state) {
3909 case ISTATE_SEND_LOGOUTRSP:
3910 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003911 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003912 /* fall through */
3913 case ISTATE_SEND_STATUS:
3914 case ISTATE_SEND_ASYNCMSG:
3915 case ISTATE_SEND_NOPIN:
3916 case ISTATE_SEND_STATUS_RECOVERY:
3917 case ISTATE_SEND_TEXTRSP:
3918 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003919 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003920 spin_lock_bh(&cmd->istate_lock);
3921 cmd->i_state = ISTATE_SENT_STATUS;
3922 spin_unlock_bh(&cmd->istate_lock);
3923 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003924 default:
3925 pr_err("Unknown Opcode: 0x%02x ITT:"
3926 " 0x%08x, i_state: %d on CID: %hu\n",
3927 cmd->iscsi_opcode, cmd->init_task_tag,
3928 cmd->i_state, conn->cid);
3929 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003930 }
3931
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003932 if (atomic_read(&conn->check_immediate_queue))
3933 return 1;
3934
Andy Grover6f3c0e62012-04-03 15:51:09 -07003935 return 0;
3936
3937err:
3938 return -1;
3939}
3940
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003941static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003942{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003943 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003944 struct iscsi_queue_req *qr;
3945 struct iscsi_cmd *cmd;
3946 u8 state;
3947 int ret;
3948
3949 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3950 cmd = qr->cmd;
3951 state = qr->state;
3952 kmem_cache_free(lio_qr_cache, qr);
3953
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003954 ret = t->iscsit_response_queue(conn, cmd, state);
3955 if (ret == 1 || ret < 0)
3956 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003957 }
3958
3959 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003960}
3961
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003962int iscsi_target_tx_thread(void *arg)
3963{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003964 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003965 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003966 /*
3967 * Allow ourselves to be interrupted by SIGINT so that a
3968 * connection recovery / failure event can be triggered externally.
3969 */
3970 allow_signal(SIGINT);
3971
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003972 while (!kthread_should_stop()) {
3973 /*
3974 * Ensure that both TX and RX per connection kthreads
3975 * are scheduled to run on the same CPU.
3976 */
3977 iscsit_thread_check_cpumask(conn, current, 1);
3978
Roland Dreierd5627ac2012-10-31 09:16:46 -07003979 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003980 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003981
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003982 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003983 goto transport_err;
3984
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003985get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003986 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003987 if (ret < 0)
3988 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003989
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003990 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003991 if (ret == 1)
3992 goto get_immediate;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003993 else if (ret == -ECONNRESET)
3994 goto out;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003995 else if (ret < 0)
3996 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003997 }
3998
3999transport_err:
4000 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004001out:
4002 return 0;
4003}
4004
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004005static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
4006{
4007 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
4008 struct iscsi_cmd *cmd;
4009 int ret = 0;
4010
4011 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
4012 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004013 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004014 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004015 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004016
4017 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
4018 break;
4019 case ISCSI_OP_SCSI_DATA_OUT:
4020 ret = iscsit_handle_data_out(conn, buf);
4021 break;
4022 case ISCSI_OP_NOOP_OUT:
4023 cmd = NULL;
4024 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004025 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004026 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004027 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004028 }
4029 ret = iscsit_handle_nop_out(conn, cmd, buf);
4030 break;
4031 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004032 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004033 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004034 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004035
4036 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
4037 break;
4038 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02004039 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
4040 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
4041 if (!cmd)
4042 goto reject;
4043 } else {
4044 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4045 if (!cmd)
4046 goto reject;
4047 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004048
4049 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004050 break;
4051 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004052 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004053 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004054 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004055
4056 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4057 if (ret > 0)
4058 wait_for_completion_timeout(&conn->conn_logout_comp,
4059 SECONDS_FOR_LOGOUT_COMP * HZ);
4060 break;
4061 case ISCSI_OP_SNACK:
4062 ret = iscsit_handle_snack(conn, buf);
4063 break;
4064 default:
4065 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4066 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4067 pr_err("Cannot recover from unknown"
4068 " opcode while ERL=0, closing iSCSI connection.\n");
4069 return -1;
4070 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02004071 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
4072 " closing iSCSI connection.\n");
4073 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004074 break;
4075 }
4076
4077 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004078reject:
4079 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004080}
4081
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004082int iscsi_target_rx_thread(void *arg)
4083{
4084 int ret;
4085 u8 buffer[ISCSI_HDR_LEN], opcode;
4086 u32 checksum = 0, digest = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004087 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004088 struct kvec iov;
4089 /*
4090 * Allow ourselves to be interrupted by SIGINT so that a
4091 * connection recovery / failure event can be triggered externally.
4092 */
4093 allow_signal(SIGINT);
4094
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004095 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4096 struct completion comp;
4097 int rc;
4098
4099 init_completion(&comp);
4100 rc = wait_for_completion_interruptible(&comp);
4101 if (rc < 0)
4102 goto transport_err;
4103
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004104 goto transport_err;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004105 }
4106
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004107 while (!kthread_should_stop()) {
4108 /*
4109 * Ensure that both TX and RX per connection kthreads
4110 * are scheduled to run on the same CPU.
4111 */
4112 iscsit_thread_check_cpumask(conn, current, 0);
4113
4114 memset(buffer, 0, ISCSI_HDR_LEN);
4115 memset(&iov, 0, sizeof(struct kvec));
4116
4117 iov.iov_base = buffer;
4118 iov.iov_len = ISCSI_HDR_LEN;
4119
4120 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4121 if (ret != ISCSI_HDR_LEN) {
4122 iscsit_rx_thread_wait_for_tcp(conn);
4123 goto transport_err;
4124 }
4125
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004126 if (conn->conn_ops->HeaderDigest) {
4127 iov.iov_base = &digest;
4128 iov.iov_len = ISCSI_CRC_LEN;
4129
4130 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4131 if (ret != ISCSI_CRC_LEN) {
4132 iscsit_rx_thread_wait_for_tcp(conn);
4133 goto transport_err;
4134 }
4135
4136 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4137 buffer, ISCSI_HDR_LEN,
4138 0, NULL, (u8 *)&checksum);
4139
4140 if (digest != checksum) {
4141 pr_err("HeaderDigest CRC32C failed,"
4142 " received 0x%08x, computed 0x%08x\n",
4143 digest, checksum);
4144 /*
4145 * Set the PDU to 0xff so it will intentionally
4146 * hit default in the switch below.
4147 */
4148 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004149 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004150 } else {
4151 pr_debug("Got HeaderDigest CRC32C"
4152 " 0x%08x\n", checksum);
4153 }
4154 }
4155
4156 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4157 goto transport_err;
4158
4159 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4160
4161 if (conn->sess->sess_ops->SessionType &&
4162 ((!(opcode & ISCSI_OP_TEXT)) ||
4163 (!(opcode & ISCSI_OP_LOGOUT)))) {
4164 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4165 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004166 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4167 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004168 goto transport_err;
4169 }
4170
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004171 ret = iscsi_target_rx_opcode(conn, buffer);
4172 if (ret < 0)
4173 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004174 }
4175
4176transport_err:
4177 if (!signal_pending(current))
4178 atomic_set(&conn->transport_failed, 1);
4179 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004180 return 0;
4181}
4182
4183static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4184{
4185 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4186 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004187 /*
4188 * We expect this function to only ever be called from either RX or TX
4189 * thread context via iscsit_close_connection() once the other context
4190 * has been reset -> returned sleeping pre-handler state.
4191 */
4192 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004193 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004194
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004195 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004196 spin_unlock_bh(&conn->cmd_lock);
4197
4198 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004199
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004200 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004201
4202 spin_lock_bh(&conn->cmd_lock);
4203 }
4204 spin_unlock_bh(&conn->cmd_lock);
4205}
4206
4207static void iscsit_stop_timers_for_cmds(
4208 struct iscsi_conn *conn)
4209{
4210 struct iscsi_cmd *cmd;
4211
4212 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004213 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004214 if (cmd->data_direction == DMA_TO_DEVICE)
4215 iscsit_stop_dataout_timer(cmd);
4216 }
4217 spin_unlock_bh(&conn->cmd_lock);
4218}
4219
4220int iscsit_close_connection(
4221 struct iscsi_conn *conn)
4222{
4223 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4224 struct iscsi_session *sess = conn->sess;
4225
4226 pr_debug("Closing iSCSI connection CID %hu on SID:"
4227 " %u\n", conn->cid, sess->sid);
4228 /*
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004229 * Always up conn_logout_comp for the traditional TCP case just in case
4230 * the RX Thread in iscsi_target_rx_opcode() is sleeping and the logout
4231 * response never got sent because the connection failed.
4232 *
4233 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4234 * to signal logout response TX interrupt completion. Go ahead and skip
4235 * this for iser since isert_rx_opcode() does not wait on logout failure,
4236 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004237 */
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004238 if (conn->conn_transport->transport_type == ISCSI_TCP)
4239 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004240
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004241 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4242 if (conn->tx_thread &&
4243 cmpxchg(&conn->tx_thread_active, true, false)) {
4244 send_sig(SIGINT, conn->tx_thread, 1);
4245 kthread_stop(conn->tx_thread);
4246 }
4247 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4248 if (conn->rx_thread &&
4249 cmpxchg(&conn->rx_thread_active, true, false)) {
4250 send_sig(SIGINT, conn->rx_thread, 1);
4251 kthread_stop(conn->rx_thread);
4252 }
4253 }
4254
4255 spin_lock(&iscsit_global->ts_bitmap_lock);
4256 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4257 get_order(1));
4258 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004259
4260 iscsit_stop_timers_for_cmds(conn);
4261 iscsit_stop_nopin_response_timer(conn);
4262 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004263
4264 if (conn->conn_transport->iscsit_wait_conn)
4265 conn->conn_transport->iscsit_wait_conn(conn);
4266
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004267 /*
4268 * During Connection recovery drop unacknowledged out of order
4269 * commands for this connection, and prepare the other commands
4270 * for realligence.
4271 *
4272 * During normal operation clear the out of order commands (but
4273 * do not free the struct iscsi_ooo_cmdsn's) and release all
4274 * struct iscsi_cmds.
4275 */
4276 if (atomic_read(&conn->connection_recovery)) {
4277 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4278 iscsit_prepare_cmds_for_realligance(conn);
4279 } else {
4280 iscsit_clear_ooo_cmdsns_for_conn(conn);
4281 iscsit_release_commands_from_conn(conn);
4282 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004283 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004284
4285 /*
4286 * Handle decrementing session or connection usage count if
4287 * a logout response was not able to be sent because the
4288 * connection failed. Fall back to Session Recovery here.
4289 */
4290 if (atomic_read(&conn->conn_logout_remove)) {
4291 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4292 iscsit_dec_conn_usage_count(conn);
4293 iscsit_dec_session_usage_count(sess);
4294 }
4295 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4296 iscsit_dec_conn_usage_count(conn);
4297
4298 atomic_set(&conn->conn_logout_remove, 0);
4299 atomic_set(&sess->session_reinstatement, 0);
4300 atomic_set(&sess->session_fall_back_to_erl0, 1);
4301 }
4302
4303 spin_lock_bh(&sess->conn_lock);
4304 list_del(&conn->conn_list);
4305
4306 /*
4307 * Attempt to let the Initiator know this connection failed by
4308 * sending an Connection Dropped Async Message on another
4309 * active connection.
4310 */
4311 if (atomic_read(&conn->connection_recovery))
4312 iscsit_build_conn_drop_async_message(conn);
4313
4314 spin_unlock_bh(&sess->conn_lock);
4315
4316 /*
4317 * If connection reinstatement is being performed on this connection,
4318 * up the connection reinstatement semaphore that is being blocked on
4319 * in iscsit_cause_connection_reinstatement().
4320 */
4321 spin_lock_bh(&conn->state_lock);
4322 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4323 spin_unlock_bh(&conn->state_lock);
4324 complete(&conn->conn_wait_comp);
4325 wait_for_completion(&conn->conn_post_wait_comp);
4326 spin_lock_bh(&conn->state_lock);
4327 }
4328
4329 /*
4330 * If connection reinstatement is being performed on this connection
4331 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4332 * connection wait rcfr semaphore that is being blocked on
4333 * an iscsit_connection_reinstatement_rcfr().
4334 */
4335 if (atomic_read(&conn->connection_wait_rcfr)) {
4336 spin_unlock_bh(&conn->state_lock);
4337 complete(&conn->conn_wait_rcfr_comp);
4338 wait_for_completion(&conn->conn_post_wait_comp);
4339 spin_lock_bh(&conn->state_lock);
4340 }
4341 atomic_set(&conn->connection_reinstatement, 1);
4342 spin_unlock_bh(&conn->state_lock);
4343
4344 /*
4345 * If any other processes are accessing this connection pointer we
4346 * must wait until they have completed.
4347 */
4348 iscsit_check_conn_usage_count(conn);
4349
4350 if (conn->conn_rx_hash.tfm)
4351 crypto_free_hash(conn->conn_rx_hash.tfm);
4352 if (conn->conn_tx_hash.tfm)
4353 crypto_free_hash(conn->conn_tx_hash.tfm);
4354
Joern Engelfbecb652014-09-02 17:49:47 -04004355 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004356
4357 kfree(conn->conn_ops);
4358 conn->conn_ops = NULL;
4359
Al Virobf6932f2012-07-21 08:55:18 +01004360 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004361 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004362
4363 if (conn->conn_transport->iscsit_free_conn)
4364 conn->conn_transport->iscsit_free_conn(conn);
4365
4366 iscsit_put_transport(conn->conn_transport);
4367
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004368 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4369 conn->conn_state = TARG_CONN_STATE_FREE;
4370 kfree(conn);
4371
4372 spin_lock_bh(&sess->conn_lock);
4373 atomic_dec(&sess->nconn);
4374 pr_debug("Decremented iSCSI connection count to %hu from node:"
4375 " %s\n", atomic_read(&sess->nconn),
4376 sess->sess_ops->InitiatorName);
4377 /*
4378 * Make sure that if one connection fails in an non ERL=2 iSCSI
4379 * Session that they all fail.
4380 */
4381 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4382 !atomic_read(&sess->session_logout))
4383 atomic_set(&sess->session_fall_back_to_erl0, 1);
4384
4385 /*
4386 * If this was not the last connection in the session, and we are
4387 * performing session reinstatement or falling back to ERL=0, call
4388 * iscsit_stop_session() without sleeping to shutdown the other
4389 * active connections.
4390 */
4391 if (atomic_read(&sess->nconn)) {
4392 if (!atomic_read(&sess->session_reinstatement) &&
4393 !atomic_read(&sess->session_fall_back_to_erl0)) {
4394 spin_unlock_bh(&sess->conn_lock);
4395 return 0;
4396 }
4397 if (!atomic_read(&sess->session_stop_active)) {
4398 atomic_set(&sess->session_stop_active, 1);
4399 spin_unlock_bh(&sess->conn_lock);
4400 iscsit_stop_session(sess, 0, 0);
4401 return 0;
4402 }
4403 spin_unlock_bh(&sess->conn_lock);
4404 return 0;
4405 }
4406
4407 /*
4408 * If this was the last connection in the session and one of the
4409 * following is occurring:
4410 *
4411 * Session Reinstatement is not being performed, and are falling back
4412 * to ERL=0 call iscsit_close_session().
4413 *
4414 * Session Logout was requested. iscsit_close_session() will be called
4415 * elsewhere.
4416 *
4417 * Session Continuation is not being performed, start the Time2Retain
4418 * handler and check if sleep_on_sess_wait_sem is active.
4419 */
4420 if (!atomic_read(&sess->session_reinstatement) &&
4421 atomic_read(&sess->session_fall_back_to_erl0)) {
4422 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004423 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004424
4425 return 0;
4426 } else if (atomic_read(&sess->session_logout)) {
4427 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4428 sess->session_state = TARG_SESS_STATE_FREE;
4429 spin_unlock_bh(&sess->conn_lock);
4430
4431 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4432 complete(&sess->session_wait_comp);
4433
4434 return 0;
4435 } else {
4436 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4437 sess->session_state = TARG_SESS_STATE_FAILED;
4438
4439 if (!atomic_read(&sess->session_continuation)) {
4440 spin_unlock_bh(&sess->conn_lock);
4441 iscsit_start_time2retain_handler(sess);
4442 } else
4443 spin_unlock_bh(&sess->conn_lock);
4444
4445 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4446 complete(&sess->session_wait_comp);
4447
4448 return 0;
4449 }
4450 spin_unlock_bh(&sess->conn_lock);
4451
4452 return 0;
4453}
4454
4455int iscsit_close_session(struct iscsi_session *sess)
4456{
Andy Grover60bfcf82013-10-09 11:05:58 -07004457 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004458 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4459
4460 if (atomic_read(&sess->nconn)) {
4461 pr_err("%d connection(s) still exist for iSCSI session"
4462 " to %s\n", atomic_read(&sess->nconn),
4463 sess->sess_ops->InitiatorName);
4464 BUG();
4465 }
4466
4467 spin_lock_bh(&se_tpg->session_lock);
4468 atomic_set(&sess->session_logout, 1);
4469 atomic_set(&sess->session_reinstatement, 1);
4470 iscsit_stop_time2retain_timer(sess);
4471 spin_unlock_bh(&se_tpg->session_lock);
4472
4473 /*
4474 * transport_deregister_session_configfs() will clear the
4475 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4476 * can be setting it again with __transport_register_session() in
4477 * iscsi_post_login_handler() again after the iscsit_stop_session()
4478 * completes in iscsi_np context.
4479 */
4480 transport_deregister_session_configfs(sess->se_sess);
4481
4482 /*
4483 * If any other processes are accessing this session pointer we must
4484 * wait until they have completed. If we are in an interrupt (the
4485 * time2retain handler) and contain and active session usage count we
4486 * restart the timer and exit.
4487 */
4488 if (!in_interrupt()) {
4489 if (iscsit_check_session_usage_count(sess) == 1)
4490 iscsit_stop_session(sess, 1, 1);
4491 } else {
4492 if (iscsit_check_session_usage_count(sess) == 2) {
4493 atomic_set(&sess->session_logout, 0);
4494 iscsit_start_time2retain_handler(sess);
4495 return 0;
4496 }
4497 }
4498
4499 transport_deregister_session(sess->se_sess);
4500
4501 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4502 iscsit_free_connection_recovery_entires(sess);
4503
4504 iscsit_free_all_ooo_cmdsns(sess);
4505
4506 spin_lock_bh(&se_tpg->session_lock);
4507 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4508 sess->session_state = TARG_SESS_STATE_FREE;
4509 pr_debug("Released iSCSI session from node: %s\n",
4510 sess->sess_ops->InitiatorName);
4511 tpg->nsessions--;
4512 if (tpg->tpg_tiqn)
4513 tpg->tpg_tiqn->tiqn_nsessions--;
4514
4515 pr_debug("Decremented number of active iSCSI Sessions on"
4516 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4517
4518 spin_lock(&sess_idr_lock);
4519 idr_remove(&sess_idr, sess->session_index);
4520 spin_unlock(&sess_idr_lock);
4521
4522 kfree(sess->sess_ops);
4523 sess->sess_ops = NULL;
4524 spin_unlock_bh(&se_tpg->session_lock);
4525
4526 kfree(sess);
4527 return 0;
4528}
4529
4530static void iscsit_logout_post_handler_closesession(
4531 struct iscsi_conn *conn)
4532{
4533 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004534 int sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004535
4536 atomic_set(&conn->conn_logout_remove, 0);
4537 complete(&conn->conn_logout_comp);
4538
4539 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004540 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004541 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004542 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004543}
4544
4545static void iscsit_logout_post_handler_samecid(
4546 struct iscsi_conn *conn)
4547{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004548 int sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004549
4550 atomic_set(&conn->conn_logout_remove, 0);
4551 complete(&conn->conn_logout_comp);
4552
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004553 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004554 iscsit_dec_conn_usage_count(conn);
4555}
4556
4557static void iscsit_logout_post_handler_diffcid(
4558 struct iscsi_conn *conn,
4559 u16 cid)
4560{
4561 struct iscsi_conn *l_conn;
4562 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004563 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004564
4565 if (!sess)
4566 return;
4567
4568 spin_lock_bh(&sess->conn_lock);
4569 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4570 if (l_conn->cid == cid) {
4571 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004572 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004573 break;
4574 }
4575 }
4576 spin_unlock_bh(&sess->conn_lock);
4577
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004578 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004579 return;
4580
4581 if (l_conn->sock)
4582 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4583
4584 spin_lock_bh(&l_conn->state_lock);
4585 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4586 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4587 spin_unlock_bh(&l_conn->state_lock);
4588
4589 iscsit_cause_connection_reinstatement(l_conn, 1);
4590 iscsit_dec_conn_usage_count(l_conn);
4591}
4592
4593/*
4594 * Return of 0 causes the TX thread to restart.
4595 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004596int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004597 struct iscsi_cmd *cmd,
4598 struct iscsi_conn *conn)
4599{
4600 int ret = 0;
4601
4602 switch (cmd->logout_reason) {
4603 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4604 switch (cmd->logout_response) {
4605 case ISCSI_LOGOUT_SUCCESS:
4606 case ISCSI_LOGOUT_CLEANUP_FAILED:
4607 default:
4608 iscsit_logout_post_handler_closesession(conn);
4609 break;
4610 }
4611 ret = 0;
4612 break;
4613 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4614 if (conn->cid == cmd->logout_cid) {
4615 switch (cmd->logout_response) {
4616 case ISCSI_LOGOUT_SUCCESS:
4617 case ISCSI_LOGOUT_CLEANUP_FAILED:
4618 default:
4619 iscsit_logout_post_handler_samecid(conn);
4620 break;
4621 }
4622 ret = 0;
4623 } else {
4624 switch (cmd->logout_response) {
4625 case ISCSI_LOGOUT_SUCCESS:
4626 iscsit_logout_post_handler_diffcid(conn,
4627 cmd->logout_cid);
4628 break;
4629 case ISCSI_LOGOUT_CID_NOT_FOUND:
4630 case ISCSI_LOGOUT_CLEANUP_FAILED:
4631 default:
4632 break;
4633 }
4634 ret = 1;
4635 }
4636 break;
4637 case ISCSI_LOGOUT_REASON_RECOVERY:
4638 switch (cmd->logout_response) {
4639 case ISCSI_LOGOUT_SUCCESS:
4640 case ISCSI_LOGOUT_CID_NOT_FOUND:
4641 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4642 case ISCSI_LOGOUT_CLEANUP_FAILED:
4643 default:
4644 break;
4645 }
4646 ret = 1;
4647 break;
4648 default:
4649 break;
4650
4651 }
4652 return ret;
4653}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004654EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004655
4656void iscsit_fail_session(struct iscsi_session *sess)
4657{
4658 struct iscsi_conn *conn;
4659
4660 spin_lock_bh(&sess->conn_lock);
4661 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4662 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4663 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4664 }
4665 spin_unlock_bh(&sess->conn_lock);
4666
4667 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4668 sess->session_state = TARG_SESS_STATE_FAILED;
4669}
4670
4671int iscsit_free_session(struct iscsi_session *sess)
4672{
4673 u16 conn_count = atomic_read(&sess->nconn);
4674 struct iscsi_conn *conn, *conn_tmp = NULL;
4675 int is_last;
4676
4677 spin_lock_bh(&sess->conn_lock);
4678 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4679
4680 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4681 conn_list) {
4682 if (conn_count == 0)
4683 break;
4684
4685 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4686 is_last = 1;
4687 } else {
4688 iscsit_inc_conn_usage_count(conn_tmp);
4689 is_last = 0;
4690 }
4691 iscsit_inc_conn_usage_count(conn);
4692
4693 spin_unlock_bh(&sess->conn_lock);
4694 iscsit_cause_connection_reinstatement(conn, 1);
4695 spin_lock_bh(&sess->conn_lock);
4696
4697 iscsit_dec_conn_usage_count(conn);
4698 if (is_last == 0)
4699 iscsit_dec_conn_usage_count(conn_tmp);
4700
4701 conn_count--;
4702 }
4703
4704 if (atomic_read(&sess->nconn)) {
4705 spin_unlock_bh(&sess->conn_lock);
4706 wait_for_completion(&sess->session_wait_comp);
4707 } else
4708 spin_unlock_bh(&sess->conn_lock);
4709
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004710 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004711 return 0;
4712}
4713
4714void iscsit_stop_session(
4715 struct iscsi_session *sess,
4716 int session_sleep,
4717 int connection_sleep)
4718{
4719 u16 conn_count = atomic_read(&sess->nconn);
4720 struct iscsi_conn *conn, *conn_tmp = NULL;
4721 int is_last;
4722
4723 spin_lock_bh(&sess->conn_lock);
4724 if (session_sleep)
4725 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4726
4727 if (connection_sleep) {
4728 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4729 conn_list) {
4730 if (conn_count == 0)
4731 break;
4732
4733 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4734 is_last = 1;
4735 } else {
4736 iscsit_inc_conn_usage_count(conn_tmp);
4737 is_last = 0;
4738 }
4739 iscsit_inc_conn_usage_count(conn);
4740
4741 spin_unlock_bh(&sess->conn_lock);
4742 iscsit_cause_connection_reinstatement(conn, 1);
4743 spin_lock_bh(&sess->conn_lock);
4744
4745 iscsit_dec_conn_usage_count(conn);
4746 if (is_last == 0)
4747 iscsit_dec_conn_usage_count(conn_tmp);
4748 conn_count--;
4749 }
4750 } else {
4751 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4752 iscsit_cause_connection_reinstatement(conn, 0);
4753 }
4754
4755 if (session_sleep && atomic_read(&sess->nconn)) {
4756 spin_unlock_bh(&sess->conn_lock);
4757 wait_for_completion(&sess->session_wait_comp);
4758 } else
4759 spin_unlock_bh(&sess->conn_lock);
4760}
4761
4762int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4763{
4764 struct iscsi_session *sess;
4765 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4766 struct se_session *se_sess, *se_sess_tmp;
4767 int session_count = 0;
4768
4769 spin_lock_bh(&se_tpg->session_lock);
4770 if (tpg->nsessions && !force) {
4771 spin_unlock_bh(&se_tpg->session_lock);
4772 return -1;
4773 }
4774
4775 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4776 sess_list) {
4777 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4778
4779 spin_lock(&sess->conn_lock);
4780 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4781 atomic_read(&sess->session_logout) ||
4782 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4783 spin_unlock(&sess->conn_lock);
4784 continue;
4785 }
4786 atomic_set(&sess->session_reinstatement, 1);
4787 spin_unlock(&sess->conn_lock);
4788 spin_unlock_bh(&se_tpg->session_lock);
4789
4790 iscsit_free_session(sess);
4791 spin_lock_bh(&se_tpg->session_lock);
4792
4793 session_count++;
4794 }
4795 spin_unlock_bh(&se_tpg->session_lock);
4796
4797 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4798 " Group: %hu\n", session_count, tpg->tpgt);
4799 return 0;
4800}
4801
4802MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4803MODULE_VERSION("4.1.x");
4804MODULE_AUTHOR("nab@Linux-iSCSI.org");
4805MODULE_LICENSE("GPL");
4806
4807module_init(iscsi_target_init_module);
4808module_exit(iscsi_target_cleanup_module);