blob: da6bbfe4f26d80e38df766cbeebdc9258d59c508 [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 *
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5 *
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7 *
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 ******************************************************************************/
20
21#include <linux/string.h>
22#include <linux/kthread.h>
23#include <linux/crypto.h>
24#include <linux/completion.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040025#include <linux/module.h>
Al Viro40401532012-02-13 03:58:52 +000026#include <linux/idr.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000027#include <asm/unaligned.h>
28#include <scsi/scsi_device.h>
29#include <scsi/iscsi_proto.h>
Andy Groverd28b11692012-04-03 15:51:22 -070030#include <scsi/scsi_tcq.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000031#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050032#include <target/target_core_fabric.h>
Andy Groverd28b11692012-04-03 15:51:22 -070033#include <target/target_core_configfs.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000034
35#include "iscsi_target_core.h"
36#include "iscsi_target_parameters.h"
37#include "iscsi_target_seq_pdu_list.h"
38#include "iscsi_target_tq.h"
39#include "iscsi_target_configfs.h"
40#include "iscsi_target_datain_values.h"
41#include "iscsi_target_erl0.h"
42#include "iscsi_target_erl1.h"
43#include "iscsi_target_erl2.h"
44#include "iscsi_target_login.h"
45#include "iscsi_target_tmr.h"
46#include "iscsi_target_tpg.h"
47#include "iscsi_target_util.h"
48#include "iscsi_target.h"
49#include "iscsi_target_device.h"
50#include "iscsi_target_stat.h"
51
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080052#include <target/iscsi/iscsi_transport.h>
53
Nicholas Bellingere48354c2011-07-23 06:43:04 +000054static LIST_HEAD(g_tiqn_list);
55static LIST_HEAD(g_np_list);
56static DEFINE_SPINLOCK(tiqn_lock);
57static DEFINE_SPINLOCK(np_lock);
58
59static struct idr tiqn_idr;
60struct idr sess_idr;
61struct mutex auth_id_lock;
62spinlock_t sess_idr_lock;
63
64struct iscsit_global *iscsit_global;
65
Nicholas Bellingere48354c2011-07-23 06:43:04 +000066struct kmem_cache *lio_qr_cache;
67struct kmem_cache *lio_dr_cache;
68struct kmem_cache *lio_ooo_cache;
69struct kmem_cache *lio_r2t_cache;
70
71static int iscsit_handle_immediate_data(struct iscsi_cmd *,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -070072 struct iscsi_scsi_req *, u32);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000073
74struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
75{
76 struct iscsi_tiqn *tiqn = NULL;
77
78 spin_lock(&tiqn_lock);
79 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
80 if (!strcmp(tiqn->tiqn, buf)) {
81
82 spin_lock(&tiqn->tiqn_state_lock);
83 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
84 tiqn->tiqn_access_count++;
85 spin_unlock(&tiqn->tiqn_state_lock);
86 spin_unlock(&tiqn_lock);
87 return tiqn;
88 }
89 spin_unlock(&tiqn->tiqn_state_lock);
90 }
91 }
92 spin_unlock(&tiqn_lock);
93
94 return NULL;
95}
96
97static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
98{
99 spin_lock(&tiqn->tiqn_state_lock);
100 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
101 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
102 spin_unlock(&tiqn->tiqn_state_lock);
103 return 0;
104 }
105 spin_unlock(&tiqn->tiqn_state_lock);
106
107 return -1;
108}
109
110void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
111{
112 spin_lock(&tiqn->tiqn_state_lock);
113 tiqn->tiqn_access_count--;
114 spin_unlock(&tiqn->tiqn_state_lock);
115}
116
117/*
118 * Note that IQN formatting is expected to be done in userspace, and
119 * no explict IQN format checks are done here.
120 */
121struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
122{
123 struct iscsi_tiqn *tiqn = NULL;
124 int ret;
125
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300126 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000127 pr_err("Target IQN exceeds %d bytes\n",
128 ISCSI_IQN_LEN);
129 return ERR_PTR(-EINVAL);
130 }
131
132 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
133 if (!tiqn) {
134 pr_err("Unable to allocate struct iscsi_tiqn\n");
135 return ERR_PTR(-ENOMEM);
136 }
137
138 sprintf(tiqn->tiqn, "%s", buf);
139 INIT_LIST_HEAD(&tiqn->tiqn_list);
140 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
141 spin_lock_init(&tiqn->tiqn_state_lock);
142 spin_lock_init(&tiqn->tiqn_tpg_lock);
143 spin_lock_init(&tiqn->sess_err_stats.lock);
144 spin_lock_init(&tiqn->login_stats.lock);
145 spin_lock_init(&tiqn->logout_stats.lock);
146
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000147 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
148
Tejun Heoc9365bd2013-02-27 17:04:43 -0800149 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000150 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800151
152 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000153 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800154 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000155 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800156 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000157 kfree(tiqn);
158 return ERR_PTR(ret);
159 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800160 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000161 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800162
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000163 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800164 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000165
166 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
167
168 return tiqn;
169
170}
171
172static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
173{
174 /*
175 * Wait for accesses to said struct iscsi_tiqn to end.
176 */
177 spin_lock(&tiqn->tiqn_state_lock);
178 while (tiqn->tiqn_access_count != 0) {
179 spin_unlock(&tiqn->tiqn_state_lock);
180 msleep(10);
181 spin_lock(&tiqn->tiqn_state_lock);
182 }
183 spin_unlock(&tiqn->tiqn_state_lock);
184}
185
186void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
187{
188 /*
189 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
190 * while holding tiqn->tiqn_state_lock. This means that all subsequent
191 * attempts to access this struct iscsi_tiqn will fail from both transport
192 * fabric and control code paths.
193 */
194 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
195 pr_err("iscsit_set_tiqn_shutdown() failed\n");
196 return;
197 }
198
199 iscsit_wait_for_tiqn(tiqn);
200
201 spin_lock(&tiqn_lock);
202 list_del(&tiqn->tiqn_list);
203 idr_remove(&tiqn_idr, tiqn->tiqn_index);
204 spin_unlock(&tiqn_lock);
205
206 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
207 tiqn->tiqn);
208 kfree(tiqn);
209}
210
211int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
212{
213 int ret;
214 /*
215 * Determine if the network portal is accepting storage traffic.
216 */
217 spin_lock_bh(&np->np_thread_lock);
218 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
219 spin_unlock_bh(&np->np_thread_lock);
220 return -1;
221 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000222 spin_unlock_bh(&np->np_thread_lock);
223 /*
224 * Determine if the portal group is accepting storage traffic.
225 */
226 spin_lock_bh(&tpg->tpg_state_lock);
227 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
228 spin_unlock_bh(&tpg->tpg_state_lock);
229 return -1;
230 }
231 spin_unlock_bh(&tpg->tpg_state_lock);
232
233 /*
234 * Here we serialize access across the TIQN+TPG Tuple.
235 */
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700236 ret = down_interruptible(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000237 if ((ret != 0) || signal_pending(current))
238 return -1;
239
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700240 spin_lock_bh(&tpg->tpg_state_lock);
241 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
242 spin_unlock_bh(&tpg->tpg_state_lock);
243 up(&tpg->np_login_sem);
244 return -1;
245 }
246 spin_unlock_bh(&tpg->tpg_state_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000247
248 return 0;
249}
250
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700251void iscsit_login_kref_put(struct kref *kref)
252{
253 struct iscsi_tpg_np *tpg_np = container_of(kref,
254 struct iscsi_tpg_np, tpg_np_kref);
255
256 complete(&tpg_np->tpg_np_comp);
257}
258
259int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
260 struct iscsi_tpg_np *tpg_np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000261{
262 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
263
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700264 up(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000265
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700266 if (tpg_np)
267 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000268
269 if (tiqn)
270 iscsit_put_tiqn_for_login(tiqn);
271
272 return 0;
273}
274
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800275bool iscsit_check_np_match(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000276 struct __kernel_sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800277 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000278 int network_transport)
279{
280 struct sockaddr_in *sock_in, *sock_in_e;
281 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800282 bool ip_match = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000283 u16 port;
284
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800285 if (sockaddr->ss_family == AF_INET6) {
286 sock_in6 = (struct sockaddr_in6 *)sockaddr;
287 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
288
289 if (!memcmp(&sock_in6->sin6_addr.in6_u,
290 &sock_in6_e->sin6_addr.in6_u,
291 sizeof(struct in6_addr)))
292 ip_match = true;
293
294 port = ntohs(sock_in6->sin6_port);
295 } else {
296 sock_in = (struct sockaddr_in *)sockaddr;
297 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
298
299 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
300 ip_match = true;
301
302 port = ntohs(sock_in->sin_port);
303 }
304
305 if ((ip_match == true) && (np->np_port == port) &&
306 (np->np_network_transport == network_transport))
307 return true;
308
309 return false;
310}
311
312static struct iscsi_np *iscsit_get_np(
313 struct __kernel_sockaddr_storage *sockaddr,
314 int network_transport)
315{
316 struct iscsi_np *np;
317 bool match;
318
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000319 spin_lock_bh(&np_lock);
320 list_for_each_entry(np, &g_np_list, np_list) {
321 spin_lock(&np->np_thread_lock);
322 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
323 spin_unlock(&np->np_thread_lock);
324 continue;
325 }
326
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800327 match = iscsit_check_np_match(sockaddr, np, network_transport);
328 if (match == true) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000329 /*
330 * Increment the np_exports reference count now to
331 * prevent iscsit_del_np() below from being called
332 * while iscsi_tpg_add_network_portal() is called.
333 */
334 np->np_exports++;
335 spin_unlock(&np->np_thread_lock);
336 spin_unlock_bh(&np_lock);
337 return np;
338 }
339 spin_unlock(&np->np_thread_lock);
340 }
341 spin_unlock_bh(&np_lock);
342
343 return NULL;
344}
345
346struct iscsi_np *iscsit_add_np(
347 struct __kernel_sockaddr_storage *sockaddr,
348 char *ip_str,
349 int network_transport)
350{
351 struct sockaddr_in *sock_in;
352 struct sockaddr_in6 *sock_in6;
353 struct iscsi_np *np;
354 int ret;
355 /*
356 * Locate the existing struct iscsi_np if already active..
357 */
358 np = iscsit_get_np(sockaddr, network_transport);
359 if (np)
360 return np;
361
362 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
363 if (!np) {
364 pr_err("Unable to allocate memory for struct iscsi_np\n");
365 return ERR_PTR(-ENOMEM);
366 }
367
368 np->np_flags |= NPF_IP_NETWORK;
369 if (sockaddr->ss_family == AF_INET6) {
370 sock_in6 = (struct sockaddr_in6 *)sockaddr;
371 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
372 np->np_port = ntohs(sock_in6->sin6_port);
373 } else {
374 sock_in = (struct sockaddr_in *)sockaddr;
375 sprintf(np->np_ip, "%s", ip_str);
376 np->np_port = ntohs(sock_in->sin_port);
377 }
378
379 np->np_network_transport = network_transport;
380 spin_lock_init(&np->np_thread_lock);
381 init_completion(&np->np_restart_comp);
382 INIT_LIST_HEAD(&np->np_list);
383
384 ret = iscsi_target_setup_login_socket(np, sockaddr);
385 if (ret != 0) {
386 kfree(np);
387 return ERR_PTR(ret);
388 }
389
390 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
391 if (IS_ERR(np->np_thread)) {
392 pr_err("Unable to create kthread: iscsi_np\n");
393 ret = PTR_ERR(np->np_thread);
394 kfree(np);
395 return ERR_PTR(ret);
396 }
397 /*
398 * Increment the np_exports reference count now to prevent
399 * iscsit_del_np() below from being run while a new call to
400 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
401 * active. We don't need to hold np->np_thread_lock at this
402 * point because iscsi_np has not been added to g_np_list yet.
403 */
404 np->np_exports = 1;
405
406 spin_lock_bh(&np_lock);
407 list_add_tail(&np->np_list, &g_np_list);
408 spin_unlock_bh(&np_lock);
409
410 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800411 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000412
413 return np;
414}
415
416int iscsit_reset_np_thread(
417 struct iscsi_np *np,
418 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700419 struct iscsi_portal_group *tpg,
420 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000421{
422 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000423 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
424 spin_unlock_bh(&np->np_thread_lock);
425 return 0;
426 }
427 np->np_thread_state = ISCSI_NP_THREAD_RESET;
428
429 if (np->np_thread) {
430 spin_unlock_bh(&np->np_thread_lock);
431 send_sig(SIGINT, np->np_thread, 1);
432 wait_for_completion(&np->np_restart_comp);
433 spin_lock_bh(&np->np_thread_lock);
434 }
435 spin_unlock_bh(&np->np_thread_lock);
436
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700437 if (tpg_np && shutdown) {
438 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
439
440 wait_for_completion(&tpg_np->tpg_np_comp);
441 }
442
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000443 return 0;
444}
445
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800446static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000447{
Al Virobf6932f2012-07-21 08:55:18 +0100448 if (np->np_socket)
449 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000450}
451
452int iscsit_del_np(struct iscsi_np *np)
453{
454 spin_lock_bh(&np->np_thread_lock);
455 np->np_exports--;
456 if (np->np_exports) {
457 spin_unlock_bh(&np->np_thread_lock);
458 return 0;
459 }
460 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
461 spin_unlock_bh(&np->np_thread_lock);
462
463 if (np->np_thread) {
464 /*
465 * We need to send the signal to wakeup Linux/Net
466 * which may be sleeping in sock_accept()..
467 */
468 send_sig(SIGINT, np->np_thread, 1);
469 kthread_stop(np->np_thread);
470 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800471
472 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000473
474 spin_lock_bh(&np_lock);
475 list_del(&np->np_list);
476 spin_unlock_bh(&np_lock);
477
478 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800479 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000480
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800481 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000482 kfree(np);
483 return 0;
484}
485
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700486static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
487static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
488
489static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
490{
491 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
492 return 0;
493}
494
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800495static struct iscsit_transport iscsi_target_transport = {
496 .name = "iSCSI/TCP",
497 .transport_type = ISCSI_TCP,
498 .owner = NULL,
499 .iscsit_setup_np = iscsit_setup_np,
500 .iscsit_accept_np = iscsit_accept_np,
501 .iscsit_free_np = iscsit_free_np,
502 .iscsit_get_login_rx = iscsit_get_login_rx,
503 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800504 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700505 .iscsit_immediate_queue = iscsit_immediate_queue,
506 .iscsit_response_queue = iscsit_response_queue,
507 .iscsit_queue_data_in = iscsit_queue_rsp,
508 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800509};
510
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000511static int __init iscsi_target_init_module(void)
512{
513 int ret = 0;
514
515 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
516
517 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
518 if (!iscsit_global) {
519 pr_err("Unable to allocate memory for iscsit_global\n");
520 return -1;
521 }
522 mutex_init(&auth_id_lock);
523 spin_lock_init(&sess_idr_lock);
524 idr_init(&tiqn_idr);
525 idr_init(&sess_idr);
526
527 ret = iscsi_target_register_configfs();
528 if (ret < 0)
529 goto out;
530
531 ret = iscsi_thread_set_init();
532 if (ret < 0)
533 goto configfs_out;
534
535 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
536 TARGET_THREAD_SET_COUNT) {
537 pr_err("iscsi_allocate_thread_sets() returned"
538 " unexpected value!\n");
539 goto ts_out1;
540 }
541
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000542 lio_qr_cache = kmem_cache_create("lio_qr_cache",
543 sizeof(struct iscsi_queue_req),
544 __alignof__(struct iscsi_queue_req), 0, NULL);
545 if (!lio_qr_cache) {
546 pr_err("nable to kmem_cache_create() for"
547 " lio_qr_cache\n");
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700548 goto ts_out2;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000549 }
550
551 lio_dr_cache = kmem_cache_create("lio_dr_cache",
552 sizeof(struct iscsi_datain_req),
553 __alignof__(struct iscsi_datain_req), 0, NULL);
554 if (!lio_dr_cache) {
555 pr_err("Unable to kmem_cache_create() for"
556 " lio_dr_cache\n");
557 goto qr_out;
558 }
559
560 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
561 sizeof(struct iscsi_ooo_cmdsn),
562 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
563 if (!lio_ooo_cache) {
564 pr_err("Unable to kmem_cache_create() for"
565 " lio_ooo_cache\n");
566 goto dr_out;
567 }
568
569 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
570 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
571 0, NULL);
572 if (!lio_r2t_cache) {
573 pr_err("Unable to kmem_cache_create() for"
574 " lio_r2t_cache\n");
575 goto ooo_out;
576 }
577
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800578 iscsit_register_transport(&iscsi_target_transport);
579
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000580 if (iscsit_load_discovery_tpg() < 0)
581 goto r2t_out;
582
583 return ret;
584r2t_out:
585 kmem_cache_destroy(lio_r2t_cache);
586ooo_out:
587 kmem_cache_destroy(lio_ooo_cache);
588dr_out:
589 kmem_cache_destroy(lio_dr_cache);
590qr_out:
591 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000592ts_out2:
593 iscsi_deallocate_thread_sets();
594ts_out1:
595 iscsi_thread_set_free();
596configfs_out:
597 iscsi_target_deregister_configfs();
598out:
599 kfree(iscsit_global);
600 return -ENOMEM;
601}
602
603static void __exit iscsi_target_cleanup_module(void)
604{
605 iscsi_deallocate_thread_sets();
606 iscsi_thread_set_free();
607 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800608 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000609 kmem_cache_destroy(lio_qr_cache);
610 kmem_cache_destroy(lio_dr_cache);
611 kmem_cache_destroy(lio_ooo_cache);
612 kmem_cache_destroy(lio_r2t_cache);
613
614 iscsi_target_deregister_configfs();
615
616 kfree(iscsit_global);
617}
618
Andy Grover8b1e1242012-04-03 15:51:12 -0700619static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700620 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000621 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700622 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000623{
624 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000625
626 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
627 if (!cmd)
628 return -1;
629
630 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700631 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000632
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100633 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000634 if (!cmd->buf_ptr) {
635 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700636 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000637 return -1;
638 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000639
640 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700641 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000642 spin_unlock_bh(&conn->cmd_lock);
643
644 cmd->i_state = ISTATE_SEND_REJECT;
645 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
646
Nicholas Bellingerba159912013-07-03 03:48:24 -0700647 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000648}
649
Nicholas Bellingerba159912013-07-03 03:48:24 -0700650static int iscsit_add_reject_from_cmd(
651 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000652 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700653 bool add_to_conn,
654 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000655{
656 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000657
658 if (!cmd->conn) {
659 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
660 cmd->init_task_tag);
661 return -1;
662 }
663 conn = cmd->conn;
664
665 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700666 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000667
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100668 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000669 if (!cmd->buf_ptr) {
670 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700671 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000672 return -1;
673 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000674
675 if (add_to_conn) {
676 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700677 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000678 spin_unlock_bh(&conn->cmd_lock);
679 }
680
681 cmd->i_state = ISTATE_SEND_REJECT;
682 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800683 /*
684 * Perform the kref_put now if se_cmd has already been setup by
685 * scsit_setup_scsi_cmd()
686 */
687 if (cmd->se_cmd.se_tfo != NULL) {
688 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
689 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
690 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700691 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000692}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700693
694static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
695 unsigned char *buf)
696{
697 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
698}
699
700int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
701{
702 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
703}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000704
705/*
706 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700707 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000708 */
709static int iscsit_map_iovec(
710 struct iscsi_cmd *cmd,
711 struct kvec *iov,
712 u32 data_offset,
713 u32 data_length)
714{
715 u32 i = 0;
716 struct scatterlist *sg;
717 unsigned int page_off;
718
719 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700720 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000721 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700722 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000723 page_off = (data_offset % PAGE_SIZE);
724
725 cmd->first_data_sg = sg;
726 cmd->first_data_sg_off = page_off;
727
728 while (data_length) {
729 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
730
731 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
732 iov[i].iov_len = cur_len;
733
734 data_length -= cur_len;
735 page_off = 0;
736 sg = sg_next(sg);
737 i++;
738 }
739
740 cmd->kmapped_nents = i;
741
742 return i;
743}
744
745static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
746{
747 u32 i;
748 struct scatterlist *sg;
749
750 sg = cmd->first_data_sg;
751
752 for (i = 0; i < cmd->kmapped_nents; i++)
753 kunmap(sg_page(&sg[i]));
754}
755
756static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
757{
758 struct iscsi_cmd *cmd;
759
760 conn->exp_statsn = exp_statsn;
761
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800762 if (conn->sess->sess_ops->RDMAExtensions)
763 return;
764
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000765 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700766 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000767 spin_lock(&cmd->istate_lock);
768 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800769 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000770 cmd->i_state = ISTATE_REMOVE;
771 spin_unlock(&cmd->istate_lock);
772 iscsit_add_cmd_to_immediate_queue(cmd, conn,
773 cmd->i_state);
774 continue;
775 }
776 spin_unlock(&cmd->istate_lock);
777 }
778 spin_unlock_bh(&conn->cmd_lock);
779}
780
781static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
782{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700783 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000784
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400785 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000786
787 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
788 if (!cmd->iov_data) {
789 pr_err("Unable to allocate cmd->iov_data\n");
790 return -ENOMEM;
791 }
792
793 cmd->orig_iov_data_count = iov_count;
794 return 0;
795}
796
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800797int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
798 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000799{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800800 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000801 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700802 int iscsi_task_attr;
803 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000804
805 spin_lock_bh(&conn->sess->session_stats_lock);
806 conn->sess->cmd_pdus++;
807 if (conn->sess->se_sess->se_node_acl) {
808 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
809 conn->sess->se_sess->se_node_acl->num_cmds++;
810 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
811 }
812 spin_unlock_bh(&conn->sess->session_stats_lock);
813
814 hdr = (struct iscsi_scsi_req *) buf;
815 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000816
817 /* FIXME; Add checks for AdditionalHeaderSegment */
818
819 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
820 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
821 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
822 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700823 return iscsit_add_reject_cmd(cmd,
824 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000825 }
826
827 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
828 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
829 /*
830 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
831 * that adds support for RESERVE/RELEASE. There is a bug
832 * add with this new functionality that sets R/W bits when
833 * neither CDB carries any READ or WRITE datapayloads.
834 */
835 if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) {
836 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
837 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
838 goto done;
839 }
840
841 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
842 " set when Expected Data Transfer Length is 0 for"
843 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700844 return iscsit_add_reject_cmd(cmd,
845 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000846 }
847done:
848
849 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
850 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
851 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
852 " MUST be set if Expected Data Transfer Length is not 0."
853 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700854 return iscsit_add_reject_cmd(cmd,
855 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000856 }
857
858 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
859 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
860 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700861 return iscsit_add_reject_cmd(cmd,
862 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000863 }
864
865 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
866 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
867 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700868 return iscsit_add_reject_cmd(cmd,
869 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000870 }
871
872 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
873 pr_err("ImmediateData=No but DataSegmentLength=%u,"
874 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700875 return iscsit_add_reject_cmd(cmd,
876 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000877 }
878
Nicholas Bellingerba159912013-07-03 03:48:24 -0700879 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000880 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
881 pr_err("Expected Data Transfer Length and Length of"
882 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
883 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700884 return iscsit_add_reject_cmd(cmd,
885 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000886 }
887
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400888 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000889 pr_err("DataSegmentLength: %u is greater than"
890 " EDTL: %u, protocol error.\n", payload_length,
891 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700892 return iscsit_add_reject_cmd(cmd,
893 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000894 }
895
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700896 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000897 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700898 " MaxXmitDataSegmentLength: %u, protocol error.\n",
899 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
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
904 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
905 pr_err("DataSegmentLength: %u is greater than"
906 " FirstBurstLength: %u, protocol error.\n",
907 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700908 return iscsit_add_reject_cmd(cmd,
909 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000910 }
911
912 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
913 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
914 DMA_NONE;
915
Andy Groverd28b11692012-04-03 15:51:22 -0700916 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700917 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
918 /*
919 * Figure out the SAM Task Attribute for the incoming SCSI CDB
920 */
921 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
922 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
923 sam_task_attr = MSG_SIMPLE_TAG;
924 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
925 sam_task_attr = MSG_ORDERED_TAG;
926 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
927 sam_task_attr = MSG_HEAD_TAG;
928 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
929 sam_task_attr = MSG_ACA_TAG;
930 else {
931 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
932 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
933 sam_task_attr = MSG_SIMPLE_TAG;
934 }
935
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000936 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
937 cmd->i_state = ISTATE_NEW_CMD;
938 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
939 cmd->immediate_data = (payload_length) ? 1 : 0;
940 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
941 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
942 if (cmd->unsolicited_data)
943 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
944
945 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
946 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
947 spin_lock_bh(&conn->sess->ttt_lock);
948 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
949 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
950 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
951 spin_unlock_bh(&conn->sess->ttt_lock);
952 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
953 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400954 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
955 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000956 cmd->first_burst_len = payload_length;
957
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800958 if (!conn->sess->sess_ops->RDMAExtensions &&
959 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000960 struct iscsi_datain_req *dr;
961
962 dr = iscsit_allocate_datain_req();
963 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700964 return iscsit_add_reject_cmd(cmd,
965 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000966
967 iscsit_attach_datain_req(cmd, dr);
968 }
969
970 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700971 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
972 */
973 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400974 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
975 cmd->data_direction, sam_task_attr,
976 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700977
978 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
979 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800980 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
981 conn->cid);
982
983 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -0700984
Christoph Hellwigde103c92012-11-06 12:24:09 -0800985 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
986 scsilun_to_int(&hdr->lun));
987 if (cmd->sense_reason)
988 goto attach_cmd;
989
990 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
991 if (cmd->sense_reason) {
992 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -0700993 return iscsit_add_reject_cmd(cmd,
994 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000995 }
Christoph Hellwigde103c92012-11-06 12:24:09 -0800996
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000997 goto attach_cmd;
998 }
Andy Grovera12f41f2012-04-03 15:51:20 -0700999
Christoph Hellwigde103c92012-11-06 12:24:09 -08001000 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001001 return iscsit_add_reject_cmd(cmd,
1002 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001003 }
1004
1005attach_cmd:
1006 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001007 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001008 spin_unlock_bh(&conn->cmd_lock);
1009 /*
1010 * Check if we need to delay processing because of ALUA
1011 * Active/NonOptimized primary access state..
1012 */
1013 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001014
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001015 return 0;
1016}
1017EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001018
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001019void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1020{
1021 iscsit_set_dataout_sequence_values(cmd);
1022
1023 spin_lock_bh(&cmd->dataout_timeout_lock);
1024 iscsit_start_dataout_timer(cmd, cmd->conn);
1025 spin_unlock_bh(&cmd->dataout_timeout_lock);
1026}
1027EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1028
1029int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1030 struct iscsi_scsi_req *hdr)
1031{
1032 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001033 /*
1034 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1035 * the Immediate Bit is not set, and no Immediate
1036 * Data is attached.
1037 *
1038 * A PDU/CmdSN carrying Immediate Data can only
1039 * be processed after the DataCRC has passed.
1040 * If the DataCRC fails, the CmdSN MUST NOT
1041 * be acknowledged. (See below)
1042 */
1043 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001044 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1045 (unsigned char *)hdr, hdr->cmdsn);
1046 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1047 return -1;
1048 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001049 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001050 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001051 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001052 }
1053
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001054 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001055
1056 /*
1057 * If no Immediate Data is attached, it's OK to return now.
1058 */
1059 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001060 if (!cmd->sense_reason && cmd->unsolicited_data)
1061 iscsit_set_unsoliticed_dataout(cmd);
1062 if (!cmd->sense_reason)
1063 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001064
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001065 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001066 return 0;
1067 }
1068
1069 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001070 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1071 * execution. These exceptions are processed in CmdSN order using
1072 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001073 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001074 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001075 if (cmd->reject_reason)
1076 return 0;
1077
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001078 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1079 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001080 }
1081 /*
1082 * Call directly into transport_generic_new_cmd() to perform
1083 * the backend memory allocation.
1084 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001085 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001086 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001087 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001088
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001089 return 0;
1090}
1091EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1092
1093static int
1094iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1095 bool dump_payload)
1096{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001097 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001098 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1099 /*
1100 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1101 */
1102 if (dump_payload == true)
1103 goto after_immediate_data;
1104
1105 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1106 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001107after_immediate_data:
1108 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1109 /*
1110 * A PDU/CmdSN carrying Immediate Data passed
1111 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1112 * Immediate Bit is not set.
1113 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001114 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1115 (unsigned char *)hdr, hdr->cmdsn);
1116 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
1117 return -1;
1118 } else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1119 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1120 return 0;
1121 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001122
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001123 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001124 int rc;
1125
1126 rc = iscsit_dump_data_payload(cmd->conn,
1127 cmd->first_burst_len, 1);
1128 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1129 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001130 } else if (cmd->unsolicited_data)
1131 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001132
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001133 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1134 /*
1135 * Immediate Data failed DataCRC and ERL>=1,
1136 * silently drop this PDU and let the initiator
1137 * plug the CmdSN gap.
1138 *
1139 * FIXME: Send Unsolicited NOPIN with reserved
1140 * TTT here to help the initiator figure out
1141 * the missing CmdSN, although they should be
1142 * intelligent enough to determine the missing
1143 * CmdSN and issue a retry to plug the sequence.
1144 */
1145 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001146 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001147 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1148 return -1;
1149
1150 return 0;
1151}
1152
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001153static int
1154iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1155 unsigned char *buf)
1156{
1157 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1158 int rc, immed_data;
1159 bool dump_payload = false;
1160
1161 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1162 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001163 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001164 /*
1165 * Allocation iovecs needed for struct socket operations for
1166 * traditional iSCSI block I/O.
1167 */
1168 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001169 return iscsit_add_reject_cmd(cmd,
1170 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001171 }
1172 immed_data = cmd->immediate_data;
1173
1174 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1175 if (rc < 0)
1176 return rc;
1177 else if (rc > 0)
1178 dump_payload = true;
1179
1180 if (!immed_data)
1181 return 0;
1182
1183 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1184}
1185
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001186static u32 iscsit_do_crypto_hash_sg(
1187 struct hash_desc *hash,
1188 struct iscsi_cmd *cmd,
1189 u32 data_offset,
1190 u32 data_length,
1191 u32 padding,
1192 u8 *pad_bytes)
1193{
1194 u32 data_crc;
1195 u32 i;
1196 struct scatterlist *sg;
1197 unsigned int page_off;
1198
1199 crypto_hash_init(hash);
1200
1201 sg = cmd->first_data_sg;
1202 page_off = cmd->first_data_sg_off;
1203
1204 i = 0;
1205 while (data_length) {
1206 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1207
1208 crypto_hash_update(hash, &sg[i], cur_len);
1209
1210 data_length -= cur_len;
1211 page_off = 0;
1212 i++;
1213 }
1214
1215 if (padding) {
1216 struct scatterlist pad_sg;
1217
1218 sg_init_one(&pad_sg, pad_bytes, padding);
1219 crypto_hash_update(hash, &pad_sg, padding);
1220 }
1221 crypto_hash_final(hash, (u8 *) &data_crc);
1222
1223 return data_crc;
1224}
1225
1226static void iscsit_do_crypto_hash_buf(
1227 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001228 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001229 u32 payload_length,
1230 u32 padding,
1231 u8 *pad_bytes,
1232 u8 *data_crc)
1233{
1234 struct scatterlist sg;
1235
1236 crypto_hash_init(hash);
1237
Jörn Engel8359cf42011-11-24 02:05:51 +01001238 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001239 crypto_hash_update(hash, &sg, payload_length);
1240
1241 if (padding) {
1242 sg_init_one(&sg, pad_bytes, padding);
1243 crypto_hash_update(hash, &sg, padding);
1244 }
1245 crypto_hash_final(hash, data_crc);
1246}
1247
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001248int
1249iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1250 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001251{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001252 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001253 struct iscsi_cmd *cmd = NULL;
1254 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001255 u32 payload_length = ntoh24(hdr->dlength);
1256 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001257
1258 if (!payload_length) {
1259 pr_err("DataOUT payload is ZERO, protocol error.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001260 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1261 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001262 }
1263
1264 /* iSCSI write */
1265 spin_lock_bh(&conn->sess->session_stats_lock);
1266 conn->sess->rx_data_octets += payload_length;
1267 if (conn->sess->se_sess->se_node_acl) {
1268 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1269 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1270 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1271 }
1272 spin_unlock_bh(&conn->sess->session_stats_lock);
1273
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001274 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001275 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001276 " MaxXmitDataSegmentLength: %u\n", payload_length,
1277 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001278 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1279 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001280 }
1281
1282 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1283 payload_length);
1284 if (!cmd)
1285 return 0;
1286
1287 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1288 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001289 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001290 payload_length, conn->cid);
1291
1292 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1293 pr_err("Command ITT: 0x%08x received DataOUT after"
1294 " last DataOUT received, dumping payload\n",
1295 cmd->init_task_tag);
1296 return iscsit_dump_data_payload(conn, payload_length, 1);
1297 }
1298
1299 if (cmd->data_direction != DMA_TO_DEVICE) {
1300 pr_err("Command ITT: 0x%08x received DataOUT for a"
1301 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001302 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001303 }
1304 se_cmd = &cmd->se_cmd;
1305 iscsit_mod_dataout_timer(cmd);
1306
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001307 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001308 pr_err("DataOut Offset: %u, Length %u greater than"
1309 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001310 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001311 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001312 }
1313
1314 if (cmd->unsolicited_data) {
1315 int dump_unsolicited_data = 0;
1316
1317 if (conn->sess->sess_ops->InitialR2T) {
1318 pr_err("Received unexpected unsolicited data"
1319 " while InitialR2T=Yes, protocol error.\n");
1320 transport_send_check_condition_and_sense(&cmd->se_cmd,
1321 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1322 return -1;
1323 }
1324 /*
1325 * Special case for dealing with Unsolicited DataOUT
1326 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1327 * failures;
1328 */
1329
1330 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001331 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001332 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001333 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001334
1335 if (dump_unsolicited_data) {
1336 /*
1337 * Check if a delayed TASK_ABORTED status needs to
1338 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1339 * received with the unsolicitied data out.
1340 */
1341 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1342 iscsit_stop_dataout_timer(cmd);
1343
1344 transport_check_aborted_status(se_cmd,
1345 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1346 return iscsit_dump_data_payload(conn, payload_length, 1);
1347 }
1348 } else {
1349 /*
1350 * For the normal solicited data path:
1351 *
1352 * Check for a delayed TASK_ABORTED status and dump any
1353 * incoming data out payload if one exists. Also, when the
1354 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1355 * data out sequence, we decrement outstanding_r2ts. Once
1356 * outstanding_r2ts reaches zero, go ahead and send the delayed
1357 * TASK_ABORTED status.
1358 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001359 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001360 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1361 if (--cmd->outstanding_r2ts < 1) {
1362 iscsit_stop_dataout_timer(cmd);
1363 transport_check_aborted_status(
1364 se_cmd, 1);
1365 }
1366
1367 return iscsit_dump_data_payload(conn, payload_length, 1);
1368 }
1369 }
1370 /*
1371 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1372 * within-command recovery checks before receiving the payload.
1373 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001374 rc = iscsit_check_pre_dataout(cmd, buf);
1375 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001376 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001377 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001378 return -1;
1379
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001380 *out_cmd = cmd;
1381 return 0;
1382}
1383EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1384
1385static int
1386iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1387 struct iscsi_data *hdr)
1388{
1389 struct kvec *iov;
1390 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1391 u32 payload_length = ntoh24(hdr->dlength);
1392 int iov_ret, data_crc_failed = 0;
1393
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001394 rx_size += payload_length;
1395 iov = &cmd->iov_data[0];
1396
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001397 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1398 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001399 if (iov_ret < 0)
1400 return -1;
1401
1402 iov_count += iov_ret;
1403
1404 padding = ((-payload_length) & 3);
1405 if (padding != 0) {
1406 iov[iov_count].iov_base = cmd->pad_bytes;
1407 iov[iov_count++].iov_len = padding;
1408 rx_size += padding;
1409 pr_debug("Receiving %u padding bytes.\n", padding);
1410 }
1411
1412 if (conn->conn_ops->DataDigest) {
1413 iov[iov_count].iov_base = &checksum;
1414 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1415 rx_size += ISCSI_CRC_LEN;
1416 }
1417
1418 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1419
1420 iscsit_unmap_iovec(cmd);
1421
1422 if (rx_got != rx_size)
1423 return -1;
1424
1425 if (conn->conn_ops->DataDigest) {
1426 u32 data_crc;
1427
1428 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001429 be32_to_cpu(hdr->offset),
1430 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001431 cmd->pad_bytes);
1432
1433 if (checksum != data_crc) {
1434 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1435 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1436 " does not match computed 0x%08x\n",
1437 hdr->itt, hdr->offset, payload_length,
1438 hdr->datasn, checksum, data_crc);
1439 data_crc_failed = 1;
1440 } else {
1441 pr_debug("Got CRC32C DataDigest 0x%08x for"
1442 " %u bytes of Data Out\n", checksum,
1443 payload_length);
1444 }
1445 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001446
1447 return data_crc_failed;
1448}
1449
1450int
1451iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1452 bool data_crc_failed)
1453{
1454 struct iscsi_conn *conn = cmd->conn;
1455 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001456 /*
1457 * Increment post receive data and CRC values or perform
1458 * within-command recovery.
1459 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001460 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1461 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001462 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001463 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001464 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001465 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1466 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001467 /*
1468 * Handle extra special case for out of order
1469 * Unsolicited Data Out.
1470 */
1471 spin_lock_bh(&cmd->istate_lock);
1472 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1473 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1474 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1475 spin_unlock_bh(&cmd->istate_lock);
1476
1477 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001478 if (ooo_cmdsn)
1479 return 0;
1480 target_execute_cmd(&cmd->se_cmd);
1481 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001482 } else /* DATAOUT_CANNOT_RECOVER */
1483 return -1;
1484
1485 return 0;
1486}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001487EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001488
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001489static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1490{
1491 struct iscsi_cmd *cmd;
1492 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1493 int rc;
1494 bool data_crc_failed = false;
1495
1496 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1497 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001498 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001499 else if (!cmd)
1500 return 0;
1501
1502 rc = iscsit_get_dataout(conn, cmd, hdr);
1503 if (rc < 0)
1504 return rc;
1505 else if (rc > 0)
1506 data_crc_failed = true;
1507
1508 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1509}
1510
Nicholas Bellinger778de362013-06-14 16:07:47 -07001511int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1512 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001513{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001514 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001515
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001516 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001517 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1518 " not set, protocol error.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001519 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1520 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001521 }
1522
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001523 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001524 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001525 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001526 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001527 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001528 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1529 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001530 }
1531
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001532 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001533 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001534 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001535 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1536 payload_length);
1537 /*
1538 * This is not a response to a Unsolicited NopIN, which means
1539 * it can either be a NOPOUT ping request (with a valid ITT),
1540 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1541 * Either way, make sure we allocate an struct iscsi_cmd, as both
1542 * can contain ping data.
1543 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001544 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001545 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1546 cmd->i_state = ISTATE_SEND_NOPIN;
1547 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1548 1 : 0);
1549 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1550 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001551 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1552 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001553 cmd->data_direction = DMA_NONE;
1554 }
1555
Nicholas Bellinger778de362013-06-14 16:07:47 -07001556 return 0;
1557}
1558EXPORT_SYMBOL(iscsit_setup_nop_out);
1559
1560int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1561 struct iscsi_nopout *hdr)
1562{
1563 struct iscsi_cmd *cmd_p = NULL;
1564 int cmdsn_ret = 0;
1565 /*
1566 * Initiator is expecting a NopIN ping reply..
1567 */
1568 if (hdr->itt != RESERVED_ITT) {
1569 BUG_ON(!cmd);
1570
1571 spin_lock_bh(&conn->cmd_lock);
1572 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1573 spin_unlock_bh(&conn->cmd_lock);
1574
1575 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1576
1577 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1578 iscsit_add_cmd_to_response_queue(cmd, conn,
1579 cmd->i_state);
1580 return 0;
1581 }
1582
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001583 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1584 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001585 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1586 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001587 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001588 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001589
1590 return 0;
1591 }
1592 /*
1593 * This was a response to a unsolicited NOPIN ping.
1594 */
1595 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1596 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1597 if (!cmd_p)
1598 return -EINVAL;
1599
1600 iscsit_stop_nopin_response_timer(conn);
1601
1602 cmd_p->i_state = ISTATE_REMOVE;
1603 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1604
1605 iscsit_start_nopin_timer(conn);
1606 return 0;
1607 }
1608 /*
1609 * Otherwise, initiator is not expecting a NOPIN is response.
1610 * Just ignore for now.
1611 */
1612 return 0;
1613}
1614EXPORT_SYMBOL(iscsit_process_nop_out);
1615
1616static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1617 unsigned char *buf)
1618{
1619 unsigned char *ping_data = NULL;
1620 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1621 struct kvec *iov = NULL;
1622 u32 payload_length = ntoh24(hdr->dlength);
1623 int ret;
1624
1625 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1626 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001627 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001628 /*
1629 * Handle NOP-OUT payload for traditional iSCSI sockets
1630 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001631 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001632 u32 checksum, data_crc, padding = 0;
1633 int niov = 0, rx_got, rx_size = payload_length;
1634
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001635 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1636 if (!ping_data) {
1637 pr_err("Unable to allocate memory for"
1638 " NOPOUT ping data.\n");
1639 ret = -1;
1640 goto out;
1641 }
1642
1643 iov = &cmd->iov_misc[0];
1644 iov[niov].iov_base = ping_data;
1645 iov[niov++].iov_len = payload_length;
1646
1647 padding = ((-payload_length) & 3);
1648 if (padding != 0) {
1649 pr_debug("Receiving %u additional bytes"
1650 " for padding.\n", padding);
1651 iov[niov].iov_base = &cmd->pad_bytes;
1652 iov[niov++].iov_len = padding;
1653 rx_size += padding;
1654 }
1655 if (conn->conn_ops->DataDigest) {
1656 iov[niov].iov_base = &checksum;
1657 iov[niov++].iov_len = ISCSI_CRC_LEN;
1658 rx_size += ISCSI_CRC_LEN;
1659 }
1660
1661 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1662 if (rx_got != rx_size) {
1663 ret = -1;
1664 goto out;
1665 }
1666
1667 if (conn->conn_ops->DataDigest) {
1668 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1669 ping_data, payload_length,
1670 padding, cmd->pad_bytes,
1671 (u8 *)&data_crc);
1672
1673 if (checksum != data_crc) {
1674 pr_err("Ping data CRC32C DataDigest"
1675 " 0x%08x does not match computed 0x%08x\n",
1676 checksum, data_crc);
1677 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1678 pr_err("Unable to recover from"
1679 " NOPOUT Ping DataCRC failure while in"
1680 " ERL=0.\n");
1681 ret = -1;
1682 goto out;
1683 } else {
1684 /*
1685 * Silently drop this PDU and let the
1686 * initiator plug the CmdSN gap.
1687 */
1688 pr_debug("Dropping NOPOUT"
1689 " Command CmdSN: 0x%08x due to"
1690 " DataCRC error.\n", hdr->cmdsn);
1691 ret = 0;
1692 goto out;
1693 }
1694 } else {
1695 pr_debug("Got CRC32C DataDigest"
1696 " 0x%08x for %u bytes of ping data.\n",
1697 checksum, payload_length);
1698 }
1699 }
1700
1701 ping_data[payload_length] = '\0';
1702 /*
1703 * Attach ping data to struct iscsi_cmd->buf_ptr.
1704 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001705 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001706 cmd->buf_ptr_size = payload_length;
1707
1708 pr_debug("Got %u bytes of NOPOUT ping"
1709 " data.\n", payload_length);
1710 pr_debug("Ping Data: \"%s\"\n", ping_data);
1711 }
1712
Nicholas Bellinger778de362013-06-14 16:07:47 -07001713 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001714out:
1715 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001716 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001717
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001718 kfree(ping_data);
1719 return ret;
1720}
1721
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001722int
1723iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1724 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001725{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001726 struct se_tmr_req *se_tmr;
1727 struct iscsi_tmr_req *tmr_req;
1728 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001729 int out_of_order_cmdsn = 0, ret;
1730 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001731 u8 function;
1732
1733 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001734 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1735 function = hdr->flags;
1736
1737 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1738 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1739 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1740 hdr->rtt, hdr->refcmdsn, conn->cid);
1741
1742 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1743 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001744 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001745 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001746 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001747 }
1748
1749 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1750 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1751 pr_err("Task Management Request TASK_REASSIGN not"
1752 " issued as immediate command, bad iSCSI Initiator"
1753 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001754 return iscsit_add_reject_cmd(cmd,
1755 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001756 }
1757 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001758 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1759 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001760
Andy Groverd28b11692012-04-03 15:51:22 -07001761 cmd->data_direction = DMA_NONE;
1762
1763 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1764 if (!cmd->tmr_req) {
1765 pr_err("Unable to allocate memory for"
1766 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001767 return iscsit_add_reject_cmd(cmd,
1768 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1769 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001770 }
1771
1772 /*
1773 * TASK_REASSIGN for ERL=2 / connection stays inside of
1774 * LIO-Target $FABRIC_MOD
1775 */
1776 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1777
1778 u8 tcm_function;
1779 int ret;
1780
1781 transport_init_se_cmd(&cmd->se_cmd,
1782 &lio_target_fabric_configfs->tf_ops,
1783 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001784 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001785
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001786 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1787 sess_ref = true;
1788
Andy Groverd28b11692012-04-03 15:51:22 -07001789 switch (function) {
1790 case ISCSI_TM_FUNC_ABORT_TASK:
1791 tcm_function = TMR_ABORT_TASK;
1792 break;
1793 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1794 tcm_function = TMR_ABORT_TASK_SET;
1795 break;
1796 case ISCSI_TM_FUNC_CLEAR_ACA:
1797 tcm_function = TMR_CLEAR_ACA;
1798 break;
1799 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1800 tcm_function = TMR_CLEAR_TASK_SET;
1801 break;
1802 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1803 tcm_function = TMR_LUN_RESET;
1804 break;
1805 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1806 tcm_function = TMR_TARGET_WARM_RESET;
1807 break;
1808 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1809 tcm_function = TMR_TARGET_COLD_RESET;
1810 break;
1811 default:
1812 pr_err("Unknown iSCSI TMR Function:"
1813 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001814 return iscsit_add_reject_cmd(cmd,
1815 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001816 }
1817
1818 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1819 tcm_function, GFP_KERNEL);
1820 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001821 return iscsit_add_reject_cmd(cmd,
1822 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001823
1824 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1825 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001826
1827 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1828 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1829 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1830 cmd->init_task_tag = hdr->itt;
1831 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001832 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1833 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001834 se_tmr = cmd->se_cmd.se_tmr_req;
1835 tmr_req = cmd->tmr_req;
1836 /*
1837 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1838 */
1839 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001840 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1841 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001842 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001843 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1844 goto attach;
1845 }
1846 }
1847
1848 switch (function) {
1849 case ISCSI_TM_FUNC_ABORT_TASK:
1850 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001851 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001852 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001853 break;
1854 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1855 case ISCSI_TM_FUNC_CLEAR_ACA:
1856 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1857 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1858 break;
1859 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1860 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001861 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1862 goto attach;
1863 }
1864 break;
1865 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1866 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001867 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1868 goto attach;
1869 }
1870 break;
1871 case ISCSI_TM_FUNC_TASK_REASSIGN:
1872 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1873 /*
1874 * Perform sanity checks on the ExpDataSN only if the
1875 * TASK_REASSIGN was successful.
1876 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001877 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001878 break;
1879
1880 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001881 return iscsit_add_reject_cmd(cmd,
1882 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001883 break;
1884 default:
1885 pr_err("Unknown TMR function: 0x%02x, protocol"
1886 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001887 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1888 goto attach;
1889 }
1890
1891 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1892 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1893 se_tmr->call_transport = 1;
1894attach:
1895 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001896 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001897 spin_unlock_bh(&conn->cmd_lock);
1898
1899 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001900 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001901 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1902 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001903 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001904 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001905 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001906 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001907 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001908 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001909
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001910 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001911 return 0;
1912 /*
1913 * Found the referenced task, send to transport for processing.
1914 */
1915 if (se_tmr->call_transport)
1916 return transport_generic_handle_tmr(&cmd->se_cmd);
1917
1918 /*
1919 * Could not find the referenced LUN, task, or Task Management
1920 * command not authorized or supported. Change state and
1921 * let the tx_thread send the response.
1922 *
1923 * For connection recovery, this is also the default action for
1924 * TMR TASK_REASSIGN.
1925 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001926 if (sess_ref) {
1927 pr_debug("Handle TMR, using sess_ref=true check\n");
1928 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1929 }
1930
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001931 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1932 return 0;
1933}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001934EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001935
1936/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001937int
1938iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1939 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001940{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001941 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001942
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001943 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001944 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001945 "greater than MaxXmitDataSegmentLength %u.\n",
1946 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001947 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1948 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001949 }
1950
1951 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1952 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1953 hdr->exp_statsn, payload_length);
1954
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001955 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1956 cmd->i_state = ISTATE_SEND_TEXTRSP;
1957 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1958 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1959 cmd->targ_xfer_tag = 0xFFFFFFFF;
1960 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1961 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1962 cmd->data_direction = DMA_NONE;
1963
1964 return 0;
1965}
1966EXPORT_SYMBOL(iscsit_setup_text_cmd);
1967
1968int
1969iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1970 struct iscsi_text *hdr)
1971{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001972 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001973 int cmdsn_ret;
1974
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001975 if (!text_in) {
1976 pr_err("Unable to locate text_in buffer for sendtargets"
1977 " discovery\n");
1978 goto reject;
1979 }
1980 if (strncmp("SendTargets", text_in, 11) != 0) {
1981 pr_err("Received Text Data that is not"
1982 " SendTargets, cannot continue.\n");
1983 goto reject;
1984 }
1985 text_ptr = strchr(text_in, '=');
1986 if (!text_ptr) {
1987 pr_err("No \"=\" separator found in Text Data,"
1988 " cannot continue.\n");
1989 goto reject;
1990 }
1991 if (!strncmp("=All", text_ptr, 4)) {
1992 cmd->cmd_flags |= IFC_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07001993 } else if (!strncmp("=iqn.", text_ptr, 5) ||
1994 !strncmp("=eui.", text_ptr, 5)) {
1995 cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001996 } else {
1997 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
1998 goto reject;
1999 }
2000
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002001 spin_lock_bh(&conn->cmd_lock);
2002 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2003 spin_unlock_bh(&conn->cmd_lock);
2004
2005 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2006
2007 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002008 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2009 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002010 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002011 return -1;
2012
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002013 return 0;
2014 }
2015
2016 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002017
2018reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002019 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2020 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002021}
2022EXPORT_SYMBOL(iscsit_process_text_cmd);
2023
2024static int
2025iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2026 unsigned char *buf)
2027{
2028 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2029 char *text_in = NULL;
2030 u32 payload_length = ntoh24(hdr->dlength);
2031 int rx_size, rc;
2032
2033 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2034 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002035 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002036
2037 rx_size = payload_length;
2038 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002039 u32 checksum = 0, data_crc = 0;
2040 u32 padding = 0, pad_bytes = 0;
2041 int niov = 0, rx_got;
2042 struct kvec iov[3];
2043
2044 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002045 if (!text_in) {
2046 pr_err("Unable to allocate memory for"
2047 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002048 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002049 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002050 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002051
2052 memset(iov, 0, 3 * sizeof(struct kvec));
2053 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002054 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002055
2056 padding = ((-payload_length) & 3);
2057 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002058 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002059 iov[niov++].iov_len = padding;
2060 rx_size += padding;
2061 pr_debug("Receiving %u additional bytes"
2062 " for padding.\n", padding);
2063 }
2064 if (conn->conn_ops->DataDigest) {
2065 iov[niov].iov_base = &checksum;
2066 iov[niov++].iov_len = ISCSI_CRC_LEN;
2067 rx_size += ISCSI_CRC_LEN;
2068 }
2069
2070 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002071 if (rx_got != rx_size)
2072 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002073
2074 if (conn->conn_ops->DataDigest) {
2075 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002076 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002077 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002078 (u8 *)&data_crc);
2079
2080 if (checksum != data_crc) {
2081 pr_err("Text data CRC32C DataDigest"
2082 " 0x%08x does not match computed"
2083 " 0x%08x\n", checksum, data_crc);
2084 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2085 pr_err("Unable to recover from"
2086 " Text Data digest failure while in"
2087 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002088 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002089 } else {
2090 /*
2091 * Silently drop this PDU and let the
2092 * initiator plug the CmdSN gap.
2093 */
2094 pr_debug("Dropping Text"
2095 " Command CmdSN: 0x%08x due to"
2096 " DataCRC error.\n", hdr->cmdsn);
2097 kfree(text_in);
2098 return 0;
2099 }
2100 } else {
2101 pr_debug("Got CRC32C DataDigest"
2102 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002103 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002104 }
2105 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002106 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002107 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002108 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002109 }
2110
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002111 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002112
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002113reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002114 kfree(cmd->text_in_ptr);
2115 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002116 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002117}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002118EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002119
2120int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2121{
2122 struct iscsi_conn *conn_p;
2123 struct iscsi_session *sess = conn->sess;
2124
2125 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2126 " for SID: %u.\n", conn->cid, conn->sess->sid);
2127
2128 atomic_set(&sess->session_logout, 1);
2129 atomic_set(&conn->conn_logout_remove, 1);
2130 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2131
2132 iscsit_inc_conn_usage_count(conn);
2133 iscsit_inc_session_usage_count(sess);
2134
2135 spin_lock_bh(&sess->conn_lock);
2136 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2137 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2138 continue;
2139
2140 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2141 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2142 }
2143 spin_unlock_bh(&sess->conn_lock);
2144
2145 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2146
2147 return 0;
2148}
2149
2150int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2151{
2152 struct iscsi_conn *l_conn;
2153 struct iscsi_session *sess = conn->sess;
2154
2155 pr_debug("Received logout request CLOSECONNECTION for CID:"
2156 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2157
2158 /*
2159 * A Logout Request with a CLOSECONNECTION reason code for a CID
2160 * can arrive on a connection with a differing CID.
2161 */
2162 if (conn->cid == cmd->logout_cid) {
2163 spin_lock_bh(&conn->state_lock);
2164 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2165 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2166
2167 atomic_set(&conn->conn_logout_remove, 1);
2168 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2169 iscsit_inc_conn_usage_count(conn);
2170
2171 spin_unlock_bh(&conn->state_lock);
2172 } else {
2173 /*
2174 * Handle all different cid CLOSECONNECTION requests in
2175 * iscsit_logout_post_handler_diffcid() as to give enough
2176 * time for any non immediate command's CmdSN to be
2177 * acknowledged on the connection in question.
2178 *
2179 * Here we simply make sure the CID is still around.
2180 */
2181 l_conn = iscsit_get_conn_from_cid(sess,
2182 cmd->logout_cid);
2183 if (!l_conn) {
2184 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2185 iscsit_add_cmd_to_response_queue(cmd, conn,
2186 cmd->i_state);
2187 return 0;
2188 }
2189
2190 iscsit_dec_conn_usage_count(l_conn);
2191 }
2192
2193 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2194
2195 return 0;
2196}
2197
2198int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2199{
2200 struct iscsi_session *sess = conn->sess;
2201
2202 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2203 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2204
2205 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2206 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2207 " while ERL!=2.\n");
2208 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2209 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2210 return 0;
2211 }
2212
2213 if (conn->cid == cmd->logout_cid) {
2214 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2215 " with CID: %hu on CID: %hu, implementation error.\n",
2216 cmd->logout_cid, conn->cid);
2217 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2218 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2219 return 0;
2220 }
2221
2222 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2223
2224 return 0;
2225}
2226
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002227int
2228iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2229 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002230{
2231 int cmdsn_ret, logout_remove = 0;
2232 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002233 struct iscsi_logout *hdr;
2234 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2235
2236 hdr = (struct iscsi_logout *) buf;
2237 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002238
2239 if (tiqn) {
2240 spin_lock(&tiqn->logout_stats.lock);
2241 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2242 tiqn->logout_stats.normal_logouts++;
2243 else
2244 tiqn->logout_stats.abnormal_logouts++;
2245 spin_unlock(&tiqn->logout_stats.lock);
2246 }
2247
2248 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2249 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2250 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2251 hdr->cid, conn->cid);
2252
2253 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2254 pr_err("Received logout request on connection that"
2255 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002256 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002257 return 0;
2258 }
2259
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002260 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2261 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2262 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2263 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2264 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002265 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2266 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2267 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002268 cmd->logout_reason = reason_code;
2269 cmd->data_direction = DMA_NONE;
2270
2271 /*
2272 * We need to sleep in these cases (by returning 1) until the Logout
2273 * Response gets sent in the tx thread.
2274 */
2275 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2276 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002277 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002278 logout_remove = 1;
2279
2280 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002281 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002282 spin_unlock_bh(&conn->cmd_lock);
2283
2284 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002285 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002286
2287 /*
2288 * Immediate commands are executed, well, immediately.
2289 * Non-Immediate Logout Commands are executed in CmdSN order.
2290 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002291 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002292 int ret = iscsit_execute_cmd(cmd, 0);
2293
2294 if (ret < 0)
2295 return ret;
2296 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002297 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002298 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002299 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002300 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2301 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002302 }
2303
2304 return logout_remove;
2305}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002306EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002307
2308static int iscsit_handle_snack(
2309 struct iscsi_conn *conn,
2310 unsigned char *buf)
2311{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002312 struct iscsi_snack *hdr;
2313
2314 hdr = (struct iscsi_snack *) buf;
2315 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002316
2317 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2318 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2319 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2320 hdr->begrun, hdr->runlength, conn->cid);
2321
2322 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2323 pr_err("Initiator sent SNACK request while in"
2324 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002325 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2326 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002327 }
2328 /*
2329 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2330 * call from inside iscsi_send_recovery_datain_or_r2t().
2331 */
2332 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2333 case 0:
2334 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002335 hdr->itt,
2336 be32_to_cpu(hdr->ttt),
2337 be32_to_cpu(hdr->begrun),
2338 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002339 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002340 return iscsit_handle_status_snack(conn, hdr->itt,
2341 be32_to_cpu(hdr->ttt),
2342 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002343 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002344 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2345 be32_to_cpu(hdr->begrun),
2346 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002347 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2348 /* FIXME: Support R-Data SNACK */
2349 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002350 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2351 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002352 default:
2353 pr_err("Unknown SNACK type 0x%02x, protocol"
2354 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002355 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2356 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002357 }
2358
2359 return 0;
2360}
2361
2362static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2363{
2364 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2365 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2366 wait_for_completion_interruptible_timeout(
2367 &conn->rx_half_close_comp,
2368 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2369 }
2370}
2371
2372static int iscsit_handle_immediate_data(
2373 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002374 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002375 u32 length)
2376{
2377 int iov_ret, rx_got = 0, rx_size = 0;
2378 u32 checksum, iov_count = 0, padding = 0;
2379 struct iscsi_conn *conn = cmd->conn;
2380 struct kvec *iov;
2381
2382 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2383 if (iov_ret < 0)
2384 return IMMEDIATE_DATA_CANNOT_RECOVER;
2385
2386 rx_size = length;
2387 iov_count = iov_ret;
2388 iov = &cmd->iov_data[0];
2389
2390 padding = ((-length) & 3);
2391 if (padding != 0) {
2392 iov[iov_count].iov_base = cmd->pad_bytes;
2393 iov[iov_count++].iov_len = padding;
2394 rx_size += padding;
2395 }
2396
2397 if (conn->conn_ops->DataDigest) {
2398 iov[iov_count].iov_base = &checksum;
2399 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2400 rx_size += ISCSI_CRC_LEN;
2401 }
2402
2403 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2404
2405 iscsit_unmap_iovec(cmd);
2406
2407 if (rx_got != rx_size) {
2408 iscsit_rx_thread_wait_for_tcp(conn);
2409 return IMMEDIATE_DATA_CANNOT_RECOVER;
2410 }
2411
2412 if (conn->conn_ops->DataDigest) {
2413 u32 data_crc;
2414
2415 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2416 cmd->write_data_done, length, padding,
2417 cmd->pad_bytes);
2418
2419 if (checksum != data_crc) {
2420 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2421 " does not match computed 0x%08x\n", checksum,
2422 data_crc);
2423
2424 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2425 pr_err("Unable to recover from"
2426 " Immediate Data digest failure while"
2427 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002428 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002429 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002430 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002431 return IMMEDIATE_DATA_CANNOT_RECOVER;
2432 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002433 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002434 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002435 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002436 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2437 }
2438 } else {
2439 pr_debug("Got CRC32C DataDigest 0x%08x for"
2440 " %u bytes of Immediate Data\n", checksum,
2441 length);
2442 }
2443 }
2444
2445 cmd->write_data_done += length;
2446
Andy Groverebf1d952012-04-03 15:51:24 -07002447 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002448 spin_lock_bh(&cmd->istate_lock);
2449 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2450 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2451 spin_unlock_bh(&cmd->istate_lock);
2452 }
2453
2454 return IMMEDIATE_DATA_NORMAL_OPERATION;
2455}
2456
2457/*
2458 * Called with sess->conn_lock held.
2459 */
2460/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2461 with active network interface */
2462static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2463{
2464 struct iscsi_cmd *cmd;
2465 struct iscsi_conn *conn_p;
2466
2467 /*
2468 * Only send a Asynchronous Message on connections whos network
2469 * interface is still functional.
2470 */
2471 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2472 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2473 iscsit_inc_conn_usage_count(conn_p);
2474 break;
2475 }
2476 }
2477
2478 if (!conn_p)
2479 return;
2480
Wei Yongjun3c989d72012-11-23 12:07:39 +08002481 cmd = iscsit_allocate_cmd(conn_p, GFP_ATOMIC);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002482 if (!cmd) {
2483 iscsit_dec_conn_usage_count(conn_p);
2484 return;
2485 }
2486
2487 cmd->logout_cid = conn->cid;
2488 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2489 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2490
2491 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002492 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002493 spin_unlock_bh(&conn_p->cmd_lock);
2494
2495 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2496 iscsit_dec_conn_usage_count(conn_p);
2497}
2498
2499static int iscsit_send_conn_drop_async_message(
2500 struct iscsi_cmd *cmd,
2501 struct iscsi_conn *conn)
2502{
2503 struct iscsi_async *hdr;
2504
2505 cmd->tx_size = ISCSI_HDR_LEN;
2506 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2507
2508 hdr = (struct iscsi_async *) cmd->pdu;
2509 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2510 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002511 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002512 cmd->targ_xfer_tag = 0xFFFFFFFF;
2513 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2514 cmd->stat_sn = conn->stat_sn++;
2515 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2516 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2517 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2518 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2519 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2520 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2521 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2522
2523 if (conn->conn_ops->HeaderDigest) {
2524 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2525
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002526 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2527 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002528
2529 cmd->tx_size += ISCSI_CRC_LEN;
2530 pr_debug("Attaching CRC32C HeaderDigest to"
2531 " Async Message 0x%08x\n", *header_digest);
2532 }
2533
2534 cmd->iov_misc[0].iov_base = cmd->pdu;
2535 cmd->iov_misc[0].iov_len = cmd->tx_size;
2536 cmd->iov_misc_count = 1;
2537
2538 pr_debug("Sending Connection Dropped Async Message StatSN:"
2539 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2540 cmd->logout_cid, conn->cid);
2541 return 0;
2542}
2543
Andy Grover6f3c0e62012-04-03 15:51:09 -07002544static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2545{
2546 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2547 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2548 wait_for_completion_interruptible_timeout(
2549 &conn->tx_half_close_comp,
2550 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2551 }
2552}
2553
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002554static void
2555iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2556 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2557 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002558{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002559 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2560 hdr->flags = datain->flags;
2561 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2562 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2563 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2564 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2565 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2566 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2567 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2568 }
2569 }
2570 hton24(hdr->dlength, datain->length);
2571 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2572 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2573 (struct scsi_lun *)&hdr->lun);
2574 else
2575 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2576
2577 hdr->itt = cmd->init_task_tag;
2578
2579 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2580 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2581 else
2582 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2583 if (set_statsn)
2584 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2585 else
2586 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2587
2588 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2589 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2590 hdr->datasn = cpu_to_be32(datain->data_sn);
2591 hdr->offset = cpu_to_be32(datain->offset);
2592
2593 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2594 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2595 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2596 ntohl(hdr->offset), datain->length, conn->cid);
2597}
2598
2599static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2600{
2601 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002602 struct iscsi_datain datain;
2603 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002604 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002605 u32 iov_count = 0, tx_size = 0;
2606 int eodr = 0, ret, iov_ret;
2607 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002608
2609 memset(&datain, 0, sizeof(struct iscsi_datain));
2610 dr = iscsit_get_datain_values(cmd, &datain);
2611 if (!dr) {
2612 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2613 cmd->init_task_tag);
2614 return -1;
2615 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002616 /*
2617 * Be paranoid and double check the logic for now.
2618 */
Andy Groverebf1d952012-04-03 15:51:24 -07002619 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002620 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2621 " datain.length: %u exceeds cmd->data_length: %u\n",
2622 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002623 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002624 return -1;
2625 }
2626
2627 spin_lock_bh(&conn->sess->session_stats_lock);
2628 conn->sess->tx_data_octets += datain.length;
2629 if (conn->sess->se_sess->se_node_acl) {
2630 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2631 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2632 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2633 }
2634 spin_unlock_bh(&conn->sess->session_stats_lock);
2635 /*
2636 * Special case for successfully execution w/ both DATAIN
2637 * and Sense Data.
2638 */
2639 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2640 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2641 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2642 else {
2643 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2644 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2645 iscsit_increment_maxcmdsn(cmd, conn->sess);
2646 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002647 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002648 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002649 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2650 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002651 }
2652
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002653 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002654
2655 iov = &cmd->iov_data[0];
2656 iov[iov_count].iov_base = cmd->pdu;
2657 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2658 tx_size += ISCSI_HDR_LEN;
2659
2660 if (conn->conn_ops->HeaderDigest) {
2661 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2662
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002663 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2664 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002665
2666 iov[0].iov_len += ISCSI_CRC_LEN;
2667 tx_size += ISCSI_CRC_LEN;
2668
2669 pr_debug("Attaching CRC32 HeaderDigest"
2670 " for DataIN PDU 0x%08x\n", *header_digest);
2671 }
2672
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002673 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2674 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002675 if (iov_ret < 0)
2676 return -1;
2677
2678 iov_count += iov_ret;
2679 tx_size += datain.length;
2680
2681 cmd->padding = ((-datain.length) & 3);
2682 if (cmd->padding) {
2683 iov[iov_count].iov_base = cmd->pad_bytes;
2684 iov[iov_count++].iov_len = cmd->padding;
2685 tx_size += cmd->padding;
2686
2687 pr_debug("Attaching %u padding bytes\n",
2688 cmd->padding);
2689 }
2690 if (conn->conn_ops->DataDigest) {
2691 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2692 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2693
2694 iov[iov_count].iov_base = &cmd->data_crc;
2695 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2696 tx_size += ISCSI_CRC_LEN;
2697
2698 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2699 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2700 }
2701
2702 cmd->iov_data_count = iov_count;
2703 cmd->tx_size = tx_size;
2704
Andy Grover6f3c0e62012-04-03 15:51:09 -07002705 /* sendpage is preferred but can't insert markers */
2706 if (!conn->conn_ops->IFMarker)
2707 ret = iscsit_fe_sendpage_sg(cmd, conn);
2708 else
2709 ret = iscsit_send_tx_data(cmd, conn, 0);
2710
2711 iscsit_unmap_iovec(cmd);
2712
2713 if (ret < 0) {
2714 iscsit_tx_thread_wait_for_tcp(conn);
2715 return ret;
2716 }
2717
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002718 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002719 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002720 2 : 1;
2721 iscsit_free_datain_req(cmd, dr);
2722 }
2723
Andy Grover6f3c0e62012-04-03 15:51:09 -07002724 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002725}
2726
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002727int
2728iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2729 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002730{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002731 struct iscsi_conn *logout_conn = NULL;
2732 struct iscsi_conn_recovery *cr = NULL;
2733 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002734 /*
2735 * The actual shutting down of Sessions and/or Connections
2736 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2737 * is done in scsi_logout_post_handler().
2738 */
2739 switch (cmd->logout_reason) {
2740 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2741 pr_debug("iSCSI session logout successful, setting"
2742 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2743 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2744 break;
2745 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2746 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2747 break;
2748 /*
2749 * For CLOSECONNECTION logout requests carrying
2750 * a matching logout CID -> local CID, the reference
2751 * for the local CID will have been incremented in
2752 * iscsi_logout_closeconnection().
2753 *
2754 * For CLOSECONNECTION logout requests carrying
2755 * a different CID than the connection it arrived
2756 * on, the connection responding to cmd->logout_cid
2757 * is stopped in iscsit_logout_post_handler_diffcid().
2758 */
2759
2760 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2761 " successful.\n", cmd->logout_cid, conn->cid);
2762 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2763 break;
2764 case ISCSI_LOGOUT_REASON_RECOVERY:
2765 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2766 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2767 break;
2768 /*
2769 * If the connection is still active from our point of view
2770 * force connection recovery to occur.
2771 */
2772 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2773 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002774 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002775 iscsit_connection_reinstatement_rcfr(logout_conn);
2776 iscsit_dec_conn_usage_count(logout_conn);
2777 }
2778
2779 cr = iscsit_get_inactive_connection_recovery_entry(
2780 conn->sess, cmd->logout_cid);
2781 if (!cr) {
2782 pr_err("Unable to locate CID: %hu for"
2783 " REMOVECONNFORRECOVERY Logout Request.\n",
2784 cmd->logout_cid);
2785 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2786 break;
2787 }
2788
2789 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2790
2791 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2792 " for recovery for CID: %hu on CID: %hu successful.\n",
2793 cmd->logout_cid, conn->cid);
2794 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2795 break;
2796 default:
2797 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2798 cmd->logout_reason);
2799 return -1;
2800 }
2801
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002802 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2803 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2804 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002805 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002806 cmd->stat_sn = conn->stat_sn++;
2807 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2808
2809 iscsit_increment_maxcmdsn(cmd, conn->sess);
2810 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2811 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2812
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002813 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2814 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2815 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2816 cmd->logout_cid, conn->cid);
2817
2818 return 0;
2819}
2820EXPORT_SYMBOL(iscsit_build_logout_rsp);
2821
2822static int
2823iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2824{
2825 struct kvec *iov;
2826 int niov = 0, tx_size, rc;
2827
2828 rc = iscsit_build_logout_rsp(cmd, conn,
2829 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2830 if (rc < 0)
2831 return rc;
2832
2833 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002834 iov = &cmd->iov_misc[0];
2835 iov[niov].iov_base = cmd->pdu;
2836 iov[niov++].iov_len = ISCSI_HDR_LEN;
2837
2838 if (conn->conn_ops->HeaderDigest) {
2839 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2840
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002841 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2842 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002843
2844 iov[0].iov_len += ISCSI_CRC_LEN;
2845 tx_size += ISCSI_CRC_LEN;
2846 pr_debug("Attaching CRC32C HeaderDigest to"
2847 " Logout Response 0x%08x\n", *header_digest);
2848 }
2849 cmd->iov_misc_count = niov;
2850 cmd->tx_size = tx_size;
2851
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002852 return 0;
2853}
2854
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002855void
2856iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2857 struct iscsi_nopin *hdr, bool nopout_response)
2858{
2859 hdr->opcode = ISCSI_OP_NOOP_IN;
2860 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2861 hton24(hdr->dlength, cmd->buf_ptr_size);
2862 if (nopout_response)
2863 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2864 hdr->itt = cmd->init_task_tag;
2865 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2866 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2867 conn->stat_sn;
2868 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2869
2870 if (nopout_response)
2871 iscsit_increment_maxcmdsn(cmd, conn->sess);
2872
2873 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2874 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2875
2876 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2877 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2878 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2879 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2880}
2881EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2882
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002883/*
2884 * Unsolicited NOPIN, either requesting a response or not.
2885 */
2886static int iscsit_send_unsolicited_nopin(
2887 struct iscsi_cmd *cmd,
2888 struct iscsi_conn *conn,
2889 int want_response)
2890{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002891 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2892 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002893
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002894 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002895
2896 if (conn->conn_ops->HeaderDigest) {
2897 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2898
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002899 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2900 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002901
2902 tx_size += ISCSI_CRC_LEN;
2903 pr_debug("Attaching CRC32C HeaderDigest to"
2904 " NopIN 0x%08x\n", *header_digest);
2905 }
2906
2907 cmd->iov_misc[0].iov_base = cmd->pdu;
2908 cmd->iov_misc[0].iov_len = tx_size;
2909 cmd->iov_misc_count = 1;
2910 cmd->tx_size = tx_size;
2911
2912 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2913 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2914
Andy Grover6f3c0e62012-04-03 15:51:09 -07002915 ret = iscsit_send_tx_data(cmd, conn, 1);
2916 if (ret < 0) {
2917 iscsit_tx_thread_wait_for_tcp(conn);
2918 return ret;
2919 }
2920
2921 spin_lock_bh(&cmd->istate_lock);
2922 cmd->i_state = want_response ?
2923 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2924 spin_unlock_bh(&cmd->istate_lock);
2925
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002926 return 0;
2927}
2928
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002929static int
2930iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002931{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002932 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002933 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002934 u32 padding = 0;
2935 int niov = 0, tx_size;
2936
2937 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002938
2939 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002940 iov = &cmd->iov_misc[0];
2941 iov[niov].iov_base = cmd->pdu;
2942 iov[niov++].iov_len = ISCSI_HDR_LEN;
2943
2944 if (conn->conn_ops->HeaderDigest) {
2945 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2946
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002947 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2948 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002949
2950 iov[0].iov_len += ISCSI_CRC_LEN;
2951 tx_size += ISCSI_CRC_LEN;
2952 pr_debug("Attaching CRC32C HeaderDigest"
2953 " to NopIn 0x%08x\n", *header_digest);
2954 }
2955
2956 /*
2957 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2958 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2959 */
2960 if (cmd->buf_ptr_size) {
2961 iov[niov].iov_base = cmd->buf_ptr;
2962 iov[niov++].iov_len = cmd->buf_ptr_size;
2963 tx_size += cmd->buf_ptr_size;
2964
2965 pr_debug("Echoing back %u bytes of ping"
2966 " data.\n", cmd->buf_ptr_size);
2967
2968 padding = ((-cmd->buf_ptr_size) & 3);
2969 if (padding != 0) {
2970 iov[niov].iov_base = &cmd->pad_bytes;
2971 iov[niov++].iov_len = padding;
2972 tx_size += padding;
2973 pr_debug("Attaching %u additional"
2974 " padding bytes.\n", padding);
2975 }
2976 if (conn->conn_ops->DataDigest) {
2977 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2978 cmd->buf_ptr, cmd->buf_ptr_size,
2979 padding, (u8 *)&cmd->pad_bytes,
2980 (u8 *)&cmd->data_crc);
2981
2982 iov[niov].iov_base = &cmd->data_crc;
2983 iov[niov++].iov_len = ISCSI_CRC_LEN;
2984 tx_size += ISCSI_CRC_LEN;
2985 pr_debug("Attached DataDigest for %u"
2986 " bytes of ping data, CRC 0x%08x\n",
2987 cmd->buf_ptr_size, cmd->data_crc);
2988 }
2989 }
2990
2991 cmd->iov_misc_count = niov;
2992 cmd->tx_size = tx_size;
2993
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002994 return 0;
2995}
2996
Andy Grover6f3c0e62012-04-03 15:51:09 -07002997static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002998 struct iscsi_cmd *cmd,
2999 struct iscsi_conn *conn)
3000{
3001 int tx_size = 0;
3002 struct iscsi_r2t *r2t;
3003 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003004 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003005
3006 r2t = iscsit_get_r2t_from_list(cmd);
3007 if (!r2t)
3008 return -1;
3009
3010 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3011 memset(hdr, 0, ISCSI_HDR_LEN);
3012 hdr->opcode = ISCSI_OP_R2T;
3013 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3014 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3015 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003016 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003017 spin_lock_bh(&conn->sess->ttt_lock);
3018 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3019 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3020 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3021 spin_unlock_bh(&conn->sess->ttt_lock);
3022 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3023 hdr->statsn = cpu_to_be32(conn->stat_sn);
3024 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3025 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3026 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3027 hdr->data_offset = cpu_to_be32(r2t->offset);
3028 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3029
3030 cmd->iov_misc[0].iov_base = cmd->pdu;
3031 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3032 tx_size += ISCSI_HDR_LEN;
3033
3034 if (conn->conn_ops->HeaderDigest) {
3035 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3036
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003037 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3038 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003039
3040 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3041 tx_size += ISCSI_CRC_LEN;
3042 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3043 " PDU 0x%08x\n", *header_digest);
3044 }
3045
3046 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3047 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3048 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3049 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3050 r2t->offset, r2t->xfer_len, conn->cid);
3051
3052 cmd->iov_misc_count = 1;
3053 cmd->tx_size = tx_size;
3054
3055 spin_lock_bh(&cmd->r2t_lock);
3056 r2t->sent_r2t = 1;
3057 spin_unlock_bh(&cmd->r2t_lock);
3058
Andy Grover6f3c0e62012-04-03 15:51:09 -07003059 ret = iscsit_send_tx_data(cmd, conn, 1);
3060 if (ret < 0) {
3061 iscsit_tx_thread_wait_for_tcp(conn);
3062 return ret;
3063 }
3064
3065 spin_lock_bh(&cmd->dataout_timeout_lock);
3066 iscsit_start_dataout_timer(cmd, conn);
3067 spin_unlock_bh(&cmd->dataout_timeout_lock);
3068
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003069 return 0;
3070}
3071
3072/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003073 * @recovery: If called from iscsi_task_reassign_complete_write() for
3074 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003075 */
3076int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003077 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003078 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003079 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003080{
3081 int first_r2t = 1;
3082 u32 offset = 0, xfer_len = 0;
3083
3084 spin_lock_bh(&cmd->r2t_lock);
3085 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3086 spin_unlock_bh(&cmd->r2t_lock);
3087 return 0;
3088 }
3089
Andy Grover8b1e1242012-04-03 15:51:12 -07003090 if (conn->sess->sess_ops->DataSequenceInOrder &&
3091 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003092 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003093
3094 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3095 if (conn->sess->sess_ops->DataSequenceInOrder) {
3096 offset = cmd->r2t_offset;
3097
Andy Grover8b1e1242012-04-03 15:51:12 -07003098 if (first_r2t && recovery) {
3099 int new_data_end = offset +
3100 conn->sess->sess_ops->MaxBurstLength -
3101 cmd->next_burst_len;
3102
Andy Groverebf1d952012-04-03 15:51:24 -07003103 if (new_data_end > cmd->se_cmd.data_length)
3104 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003105 else
3106 xfer_len =
3107 conn->sess->sess_ops->MaxBurstLength -
3108 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003109 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003110 int new_data_end = offset +
3111 conn->sess->sess_ops->MaxBurstLength;
3112
Andy Groverebf1d952012-04-03 15:51:24 -07003113 if (new_data_end > cmd->se_cmd.data_length)
3114 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003115 else
3116 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003117 }
3118 cmd->r2t_offset += xfer_len;
3119
Andy Groverebf1d952012-04-03 15:51:24 -07003120 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003121 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3122 } else {
3123 struct iscsi_seq *seq;
3124
3125 seq = iscsit_get_seq_holder_for_r2t(cmd);
3126 if (!seq) {
3127 spin_unlock_bh(&cmd->r2t_lock);
3128 return -1;
3129 }
3130
3131 offset = seq->offset;
3132 xfer_len = seq->xfer_len;
3133
3134 if (cmd->seq_send_order == cmd->seq_count)
3135 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3136 }
3137 cmd->outstanding_r2ts++;
3138 first_r2t = 0;
3139
3140 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3141 spin_unlock_bh(&cmd->r2t_lock);
3142 return -1;
3143 }
3144
3145 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3146 break;
3147 }
3148 spin_unlock_bh(&cmd->r2t_lock);
3149
3150 return 0;
3151}
3152
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003153void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3154 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003155{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003156 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003157 cmd->stat_sn = conn->stat_sn++;
3158
3159 spin_lock_bh(&conn->sess->session_stats_lock);
3160 conn->sess->rsp_pdus++;
3161 spin_unlock_bh(&conn->sess->session_stats_lock);
3162
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003163 memset(hdr, 0, ISCSI_HDR_LEN);
3164 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3165 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3166 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3167 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003168 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003169 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3170 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003171 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003172 }
3173 hdr->response = cmd->iscsi_response;
3174 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003175 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003176 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3177
3178 iscsit_increment_maxcmdsn(cmd, conn->sess);
3179 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3180 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3181
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003182 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3183 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3184 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3185 cmd->se_cmd.scsi_status, conn->cid);
3186}
3187EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3188
3189static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3190{
3191 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3192 struct kvec *iov;
3193 u32 padding = 0, tx_size = 0;
3194 int iov_count = 0;
3195 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3196
3197 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3198
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003199 iov = &cmd->iov_misc[0];
3200 iov[iov_count].iov_base = cmd->pdu;
3201 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3202 tx_size += ISCSI_HDR_LEN;
3203
3204 /*
3205 * Attach SENSE DATA payload to iSCSI Response PDU
3206 */
3207 if (cmd->se_cmd.sense_buffer &&
3208 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3209 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003210 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3211 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3212
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003213 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003214 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003215 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003216 iov[iov_count++].iov_len =
3217 (cmd->se_cmd.scsi_sense_length + padding);
3218 tx_size += cmd->se_cmd.scsi_sense_length;
3219
3220 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003221 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003222 cmd->se_cmd.scsi_sense_length, 0, padding);
3223 tx_size += padding;
3224 pr_debug("Adding %u bytes of padding to"
3225 " SENSE.\n", padding);
3226 }
3227
3228 if (conn->conn_ops->DataDigest) {
3229 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003230 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003231 (cmd->se_cmd.scsi_sense_length + padding),
3232 0, NULL, (u8 *)&cmd->data_crc);
3233
3234 iov[iov_count].iov_base = &cmd->data_crc;
3235 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3236 tx_size += ISCSI_CRC_LEN;
3237
3238 pr_debug("Attaching CRC32 DataDigest for"
3239 " SENSE, %u bytes CRC 0x%08x\n",
3240 (cmd->se_cmd.scsi_sense_length + padding),
3241 cmd->data_crc);
3242 }
3243
3244 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3245 " Response PDU\n",
3246 cmd->se_cmd.scsi_sense_length);
3247 }
3248
3249 if (conn->conn_ops->HeaderDigest) {
3250 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3251
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003252 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3253 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003254
3255 iov[0].iov_len += ISCSI_CRC_LEN;
3256 tx_size += ISCSI_CRC_LEN;
3257 pr_debug("Attaching CRC32 HeaderDigest for Response"
3258 " PDU 0x%08x\n", *header_digest);
3259 }
3260
3261 cmd->iov_misc_count = iov_count;
3262 cmd->tx_size = tx_size;
3263
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003264 return 0;
3265}
3266
3267static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3268{
3269 switch (se_tmr->response) {
3270 case TMR_FUNCTION_COMPLETE:
3271 return ISCSI_TMF_RSP_COMPLETE;
3272 case TMR_TASK_DOES_NOT_EXIST:
3273 return ISCSI_TMF_RSP_NO_TASK;
3274 case TMR_LUN_DOES_NOT_EXIST:
3275 return ISCSI_TMF_RSP_NO_LUN;
3276 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3277 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003278 case TMR_FUNCTION_REJECTED:
3279 default:
3280 return ISCSI_TMF_RSP_REJECTED;
3281 }
3282}
3283
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003284void
3285iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3286 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003287{
3288 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003289
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003290 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003291 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003292 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003293 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003294 cmd->stat_sn = conn->stat_sn++;
3295 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3296
3297 iscsit_increment_maxcmdsn(cmd, conn->sess);
3298 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3299 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3300
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003301 pr_debug("Built Task Management Response ITT: 0x%08x,"
3302 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3303 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3304}
3305EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3306
3307static int
3308iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3309{
3310 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3311 u32 tx_size = 0;
3312
3313 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3314
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003315 cmd->iov_misc[0].iov_base = cmd->pdu;
3316 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3317 tx_size += ISCSI_HDR_LEN;
3318
3319 if (conn->conn_ops->HeaderDigest) {
3320 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3321
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003322 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3323 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003324
3325 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3326 tx_size += ISCSI_CRC_LEN;
3327 pr_debug("Attaching CRC32 HeaderDigest for Task"
3328 " Mgmt Response PDU 0x%08x\n", *header_digest);
3329 }
3330
3331 cmd->iov_misc_count = 1;
3332 cmd->tx_size = tx_size;
3333
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003334 return 0;
3335}
3336
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003337static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3338{
3339 bool ret = false;
3340
3341 if (np->np_sockaddr.ss_family == AF_INET6) {
3342 const struct sockaddr_in6 sin6 = {
3343 .sin6_addr = IN6ADDR_ANY_INIT };
3344 struct sockaddr_in6 *sock_in6 =
3345 (struct sockaddr_in6 *)&np->np_sockaddr;
3346
3347 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3348 sin6.sin6_addr.s6_addr, 16))
3349 ret = true;
3350 } else {
3351 struct sockaddr_in * sock_in =
3352 (struct sockaddr_in *)&np->np_sockaddr;
3353
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003354 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003355 ret = true;
3356 }
3357
3358 return ret;
3359}
3360
Andy Grover8b1e1242012-04-03 15:51:12 -07003361#define SENDTARGETS_BUF_LIMIT 32768U
3362
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003363static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3364{
3365 char *payload = NULL;
3366 struct iscsi_conn *conn = cmd->conn;
3367 struct iscsi_portal_group *tpg;
3368 struct iscsi_tiqn *tiqn;
3369 struct iscsi_tpg_np *tpg_np;
3370 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003371 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003372 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003373
Andy Grover8b1e1242012-04-03 15:51:12 -07003374 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3375 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003376
3377 payload = kzalloc(buffer_len, GFP_KERNEL);
3378 if (!payload) {
3379 pr_err("Unable to allocate memory for sendtargets"
3380 " response.\n");
3381 return -ENOMEM;
3382 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003383 /*
3384 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3385 * explicit case..
3386 */
3387 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) {
3388 text_ptr = strchr(text_in, '=');
3389 if (!text_ptr) {
3390 pr_err("Unable to locate '=' string in text_in:"
3391 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003392 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003393 return -EINVAL;
3394 }
3395 /*
3396 * Skip over '=' character..
3397 */
3398 text_ptr += 1;
3399 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003400
3401 spin_lock(&tiqn_lock);
3402 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003403 if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) &&
3404 strcmp(tiqn->tiqn, text_ptr)) {
3405 continue;
3406 }
3407
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003408 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3409 len += 1;
3410
3411 if ((len + payload_len) > buffer_len) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003412 end_of_buf = 1;
3413 goto eob;
3414 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003415 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003416 payload_len += len;
3417
3418 spin_lock(&tiqn->tiqn_tpg_lock);
3419 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3420
3421 spin_lock(&tpg->tpg_state_lock);
3422 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3423 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3424 spin_unlock(&tpg->tpg_state_lock);
3425 continue;
3426 }
3427 spin_unlock(&tpg->tpg_state_lock);
3428
3429 spin_lock(&tpg->tpg_np_lock);
3430 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3431 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003432 struct iscsi_np *np = tpg_np->tpg_np;
3433 bool inaddr_any = iscsit_check_inaddr_any(np);
3434
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003435 len = sprintf(buf, "TargetAddress="
Chris Leechdfecf612013-08-12 11:26:28 -07003436 "%s:%hu,%hu",
3437 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003438 np->np_ip : conn->local_ip,
Chris Leechdfecf612013-08-12 11:26:28 -07003439 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003440 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003441 tpg->tpgt);
3442 len += 1;
3443
3444 if ((len + payload_len) > buffer_len) {
3445 spin_unlock(&tpg->tpg_np_lock);
3446 spin_unlock(&tiqn->tiqn_tpg_lock);
3447 end_of_buf = 1;
3448 goto eob;
3449 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003450 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003451 payload_len += len;
3452 }
3453 spin_unlock(&tpg->tpg_np_lock);
3454 }
3455 spin_unlock(&tiqn->tiqn_tpg_lock);
3456eob:
3457 if (end_of_buf)
3458 break;
Nicholas Bellinger66658892013-06-19 22:45:42 -07003459
3460 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE)
3461 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003462 }
3463 spin_unlock(&tiqn_lock);
3464
3465 cmd->buf_ptr = payload;
3466
3467 return payload_len;
3468}
3469
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003470int
3471iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3472 struct iscsi_text_rsp *hdr)
3473{
3474 int text_length, padding;
3475
3476 text_length = iscsit_build_sendtargets_response(cmd);
3477 if (text_length < 0)
3478 return text_length;
3479
3480 hdr->opcode = ISCSI_OP_TEXT_RSP;
3481 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3482 padding = ((-text_length) & 3);
3483 hton24(hdr->dlength, text_length);
3484 hdr->itt = cmd->init_task_tag;
3485 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3486 cmd->stat_sn = conn->stat_sn++;
3487 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3488
3489 iscsit_increment_maxcmdsn(cmd, conn->sess);
3490 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3491 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3492
3493 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3494 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3495 text_length, conn->cid);
3496
3497 return text_length + padding;
3498}
3499EXPORT_SYMBOL(iscsit_build_text_rsp);
3500
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003501/*
3502 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3503 * MaxRecvDataSegmentLength.
3504 */
3505static int iscsit_send_text_rsp(
3506 struct iscsi_cmd *cmd,
3507 struct iscsi_conn *conn)
3508{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003509 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003510 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003511 u32 tx_size = 0;
3512 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003513
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003514 rc = iscsit_build_text_rsp(cmd, conn, hdr);
3515 if (rc < 0)
3516 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003517
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003518 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003519 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003520 iov[iov_count].iov_base = cmd->pdu;
3521 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3522 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003523 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003524
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003525 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003526
3527 if (conn->conn_ops->HeaderDigest) {
3528 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3529
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003530 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3531 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003532
3533 iov[0].iov_len += ISCSI_CRC_LEN;
3534 tx_size += ISCSI_CRC_LEN;
3535 pr_debug("Attaching CRC32 HeaderDigest for"
3536 " Text Response PDU 0x%08x\n", *header_digest);
3537 }
3538
3539 if (conn->conn_ops->DataDigest) {
3540 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003541 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003542 0, NULL, (u8 *)&cmd->data_crc);
3543
3544 iov[iov_count].iov_base = &cmd->data_crc;
3545 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3546 tx_size += ISCSI_CRC_LEN;
3547
3548 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003549 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003550 cmd->data_crc);
3551 }
3552
3553 cmd->iov_misc_count = iov_count;
3554 cmd->tx_size = tx_size;
3555
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003556 return 0;
3557}
3558
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003559void
3560iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3561 struct iscsi_reject *hdr)
3562{
3563 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003564 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003565 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3566 hton24(hdr->dlength, ISCSI_HDR_LEN);
3567 hdr->ffffffff = cpu_to_be32(0xffffffff);
3568 cmd->stat_sn = conn->stat_sn++;
3569 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3570 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3571 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3572
3573}
3574EXPORT_SYMBOL(iscsit_build_reject);
3575
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003576static int iscsit_send_reject(
3577 struct iscsi_cmd *cmd,
3578 struct iscsi_conn *conn)
3579{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003580 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003581 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003582 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003583
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003584 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003585
3586 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003587 iov[iov_count].iov_base = cmd->pdu;
3588 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3589 iov[iov_count].iov_base = cmd->buf_ptr;
3590 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3591
3592 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3593
3594 if (conn->conn_ops->HeaderDigest) {
3595 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3596
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003597 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3598 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003599
3600 iov[0].iov_len += ISCSI_CRC_LEN;
3601 tx_size += ISCSI_CRC_LEN;
3602 pr_debug("Attaching CRC32 HeaderDigest for"
3603 " REJECT PDU 0x%08x\n", *header_digest);
3604 }
3605
3606 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003607 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3608 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003609
3610 iov[iov_count].iov_base = &cmd->data_crc;
3611 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3612 tx_size += ISCSI_CRC_LEN;
3613 pr_debug("Attaching CRC32 DataDigest for REJECT"
3614 " PDU 0x%08x\n", cmd->data_crc);
3615 }
3616
3617 cmd->iov_misc_count = iov_count;
3618 cmd->tx_size = tx_size;
3619
3620 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3621 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3622
3623 return 0;
3624}
3625
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003626void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3627{
3628 struct iscsi_thread_set *ts = conn->thread_set;
3629 int ord, cpu;
3630 /*
3631 * thread_id is assigned from iscsit_global->ts_bitmap from
3632 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3633 *
3634 * Here we use thread_id to determine which CPU that this
3635 * iSCSI connection's iscsi_thread_set will be scheduled to
3636 * execute upon.
3637 */
3638 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003639 for_each_online_cpu(cpu) {
3640 if (ord-- == 0) {
3641 cpumask_set_cpu(cpu, conn->conn_cpumask);
3642 return;
3643 }
3644 }
3645 /*
3646 * This should never be reached..
3647 */
3648 dump_stack();
3649 cpumask_setall(conn->conn_cpumask);
3650}
3651
3652static inline void iscsit_thread_check_cpumask(
3653 struct iscsi_conn *conn,
3654 struct task_struct *p,
3655 int mode)
3656{
3657 char buf[128];
3658 /*
3659 * mode == 1 signals iscsi_target_tx_thread() usage.
3660 * mode == 0 signals iscsi_target_rx_thread() usage.
3661 */
3662 if (mode == 1) {
3663 if (!conn->conn_tx_reset_cpumask)
3664 return;
3665 conn->conn_tx_reset_cpumask = 0;
3666 } else {
3667 if (!conn->conn_rx_reset_cpumask)
3668 return;
3669 conn->conn_rx_reset_cpumask = 0;
3670 }
3671 /*
3672 * Update the CPU mask for this single kthread so that
3673 * both TX and RX kthreads are scheduled to run on the
3674 * same CPU.
3675 */
3676 memset(buf, 0, 128);
3677 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003678 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3679}
3680
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003681static int
3682iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003683{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003684 int ret;
3685
3686 switch (state) {
3687 case ISTATE_SEND_R2T:
3688 ret = iscsit_send_r2t(cmd, conn);
3689 if (ret < 0)
3690 goto err;
3691 break;
3692 case ISTATE_REMOVE:
3693 spin_lock_bh(&conn->cmd_lock);
3694 list_del(&cmd->i_conn_node);
3695 spin_unlock_bh(&conn->cmd_lock);
3696
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003697 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003698 break;
3699 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3700 iscsit_mod_nopin_response_timer(conn);
3701 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3702 if (ret < 0)
3703 goto err;
3704 break;
3705 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3706 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3707 if (ret < 0)
3708 goto err;
3709 break;
3710 default:
3711 pr_err("Unknown Opcode: 0x%02x ITT:"
3712 " 0x%08x, i_state: %d on CID: %hu\n",
3713 cmd->iscsi_opcode, cmd->init_task_tag, state,
3714 conn->cid);
3715 goto err;
3716 }
3717
3718 return 0;
3719
3720err:
3721 return -1;
3722}
3723
3724static int
3725iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3726{
3727 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003728 struct iscsi_queue_req *qr;
3729 struct iscsi_cmd *cmd;
3730 u8 state;
3731 int ret;
3732
3733 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3734 atomic_set(&conn->check_immediate_queue, 0);
3735 cmd = qr->cmd;
3736 state = qr->state;
3737 kmem_cache_free(lio_qr_cache, qr);
3738
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003739 ret = t->iscsit_immediate_queue(conn, cmd, state);
3740 if (ret < 0)
3741 return ret;
3742 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003743
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003744 return 0;
3745}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003746
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003747static int
3748iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3749{
3750 int ret;
3751
3752check_rsp_state:
3753 switch (state) {
3754 case ISTATE_SEND_DATAIN:
3755 ret = iscsit_send_datain(cmd, conn);
3756 if (ret < 0)
3757 goto err;
3758 else if (!ret)
3759 /* more drs */
3760 goto check_rsp_state;
3761 else if (ret == 1) {
3762 /* all done */
3763 spin_lock_bh(&cmd->istate_lock);
3764 cmd->i_state = ISTATE_SENT_STATUS;
3765 spin_unlock_bh(&cmd->istate_lock);
3766
3767 if (atomic_read(&conn->check_immediate_queue))
3768 return 1;
3769
3770 return 0;
3771 } else if (ret == 2) {
3772 /* Still must send status,
3773 SCF_TRANSPORT_TASK_SENSE was set */
3774 spin_lock_bh(&cmd->istate_lock);
3775 cmd->i_state = ISTATE_SEND_STATUS;
3776 spin_unlock_bh(&cmd->istate_lock);
3777 state = ISTATE_SEND_STATUS;
3778 goto check_rsp_state;
3779 }
3780
3781 break;
3782 case ISTATE_SEND_STATUS:
3783 case ISTATE_SEND_STATUS_RECOVERY:
3784 ret = iscsit_send_response(cmd, conn);
3785 break;
3786 case ISTATE_SEND_LOGOUTRSP:
3787 ret = iscsit_send_logout(cmd, conn);
3788 break;
3789 case ISTATE_SEND_ASYNCMSG:
3790 ret = iscsit_send_conn_drop_async_message(
3791 cmd, conn);
3792 break;
3793 case ISTATE_SEND_NOPIN:
3794 ret = iscsit_send_nopin(cmd, conn);
3795 break;
3796 case ISTATE_SEND_REJECT:
3797 ret = iscsit_send_reject(cmd, conn);
3798 break;
3799 case ISTATE_SEND_TASKMGTRSP:
3800 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3801 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003802 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003803 ret = iscsit_tmr_post_handler(cmd, conn);
3804 if (ret != 0)
3805 iscsit_fall_back_to_erl0(conn->sess);
3806 break;
3807 case ISTATE_SEND_TEXTRSP:
3808 ret = iscsit_send_text_rsp(cmd, conn);
3809 break;
3810 default:
3811 pr_err("Unknown Opcode: 0x%02x ITT:"
3812 " 0x%08x, i_state: %d on CID: %hu\n",
3813 cmd->iscsi_opcode, cmd->init_task_tag,
3814 state, conn->cid);
3815 goto err;
3816 }
3817 if (ret < 0)
3818 goto err;
3819
3820 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3821 iscsit_tx_thread_wait_for_tcp(conn);
3822 iscsit_unmap_iovec(cmd);
3823 goto err;
3824 }
3825 iscsit_unmap_iovec(cmd);
3826
3827 switch (state) {
3828 case ISTATE_SEND_LOGOUTRSP:
3829 if (!iscsit_logout_post_handler(cmd, conn))
3830 goto restart;
3831 /* fall through */
3832 case ISTATE_SEND_STATUS:
3833 case ISTATE_SEND_ASYNCMSG:
3834 case ISTATE_SEND_NOPIN:
3835 case ISTATE_SEND_STATUS_RECOVERY:
3836 case ISTATE_SEND_TEXTRSP:
3837 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003838 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003839 spin_lock_bh(&cmd->istate_lock);
3840 cmd->i_state = ISTATE_SENT_STATUS;
3841 spin_unlock_bh(&cmd->istate_lock);
3842 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003843 default:
3844 pr_err("Unknown Opcode: 0x%02x ITT:"
3845 " 0x%08x, i_state: %d on CID: %hu\n",
3846 cmd->iscsi_opcode, cmd->init_task_tag,
3847 cmd->i_state, conn->cid);
3848 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003849 }
3850
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003851 if (atomic_read(&conn->check_immediate_queue))
3852 return 1;
3853
Andy Grover6f3c0e62012-04-03 15:51:09 -07003854 return 0;
3855
3856err:
3857 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003858restart:
3859 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003860}
3861
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003862static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003863{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003864 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003865 struct iscsi_queue_req *qr;
3866 struct iscsi_cmd *cmd;
3867 u8 state;
3868 int ret;
3869
3870 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3871 cmd = qr->cmd;
3872 state = qr->state;
3873 kmem_cache_free(lio_qr_cache, qr);
3874
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003875 ret = t->iscsit_response_queue(conn, cmd, state);
3876 if (ret == 1 || ret < 0)
3877 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003878 }
3879
3880 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003881}
3882
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003883int iscsi_target_tx_thread(void *arg)
3884{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003885 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003886 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003887 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003888 /*
3889 * Allow ourselves to be interrupted by SIGINT so that a
3890 * connection recovery / failure event can be triggered externally.
3891 */
3892 allow_signal(SIGINT);
3893
3894restart:
3895 conn = iscsi_tx_thread_pre_handler(ts);
3896 if (!conn)
3897 goto out;
3898
Andy Grover6f3c0e62012-04-03 15:51:09 -07003899 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003900
3901 while (!kthread_should_stop()) {
3902 /*
3903 * Ensure that both TX and RX per connection kthreads
3904 * are scheduled to run on the same CPU.
3905 */
3906 iscsit_thread_check_cpumask(conn, current, 1);
3907
Roland Dreierd5627ac2012-10-31 09:16:46 -07003908 wait_event_interruptible(conn->queues_wq,
3909 !iscsit_conn_all_queues_empty(conn) ||
3910 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003911
3912 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3913 signal_pending(current))
3914 goto transport_err;
3915
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003916get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003917 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003918 if (ret < 0)
3919 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003920
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003921 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003922 if (ret == 1)
3923 goto get_immediate;
3924 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003925 goto restart;
3926 else if (ret < 0)
3927 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003928 }
3929
3930transport_err:
3931 iscsit_take_action_for_connection_exit(conn);
3932 goto restart;
3933out:
3934 return 0;
3935}
3936
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003937static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3938{
3939 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3940 struct iscsi_cmd *cmd;
3941 int ret = 0;
3942
3943 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3944 case ISCSI_OP_SCSI_CMD:
3945 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3946 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003947 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003948
3949 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3950 break;
3951 case ISCSI_OP_SCSI_DATA_OUT:
3952 ret = iscsit_handle_data_out(conn, buf);
3953 break;
3954 case ISCSI_OP_NOOP_OUT:
3955 cmd = NULL;
3956 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3957 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3958 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003959 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003960 }
3961 ret = iscsit_handle_nop_out(conn, cmd, buf);
3962 break;
3963 case ISCSI_OP_SCSI_TMFUNC:
3964 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3965 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003966 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003967
3968 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3969 break;
3970 case ISCSI_OP_TEXT:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003971 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3972 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003973 goto reject;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003974
3975 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003976 break;
3977 case ISCSI_OP_LOGOUT:
3978 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3979 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003980 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003981
3982 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3983 if (ret > 0)
3984 wait_for_completion_timeout(&conn->conn_logout_comp,
3985 SECONDS_FOR_LOGOUT_COMP * HZ);
3986 break;
3987 case ISCSI_OP_SNACK:
3988 ret = iscsit_handle_snack(conn, buf);
3989 break;
3990 default:
3991 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3992 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3993 pr_err("Cannot recover from unknown"
3994 " opcode while ERL=0, closing iSCSI connection.\n");
3995 return -1;
3996 }
3997 if (!conn->conn_ops->OFMarker) {
3998 pr_err("Unable to recover from unknown"
3999 " opcode while OFMarker=No, closing iSCSI"
4000 " connection.\n");
4001 return -1;
4002 }
4003 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4004 pr_err("Unable to recover from unknown"
4005 " opcode, closing iSCSI connection.\n");
4006 return -1;
4007 }
4008 break;
4009 }
4010
4011 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004012reject:
4013 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004014}
4015
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004016int iscsi_target_rx_thread(void *arg)
4017{
4018 int ret;
4019 u8 buffer[ISCSI_HDR_LEN], opcode;
4020 u32 checksum = 0, digest = 0;
4021 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004022 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004023 struct kvec iov;
4024 /*
4025 * Allow ourselves to be interrupted by SIGINT so that a
4026 * connection recovery / failure event can be triggered externally.
4027 */
4028 allow_signal(SIGINT);
4029
4030restart:
4031 conn = iscsi_rx_thread_pre_handler(ts);
4032 if (!conn)
4033 goto out;
4034
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004035 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4036 struct completion comp;
4037 int rc;
4038
4039 init_completion(&comp);
4040 rc = wait_for_completion_interruptible(&comp);
4041 if (rc < 0)
4042 goto transport_err;
4043
4044 goto out;
4045 }
4046
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004047 while (!kthread_should_stop()) {
4048 /*
4049 * Ensure that both TX and RX per connection kthreads
4050 * are scheduled to run on the same CPU.
4051 */
4052 iscsit_thread_check_cpumask(conn, current, 0);
4053
4054 memset(buffer, 0, ISCSI_HDR_LEN);
4055 memset(&iov, 0, sizeof(struct kvec));
4056
4057 iov.iov_base = buffer;
4058 iov.iov_len = ISCSI_HDR_LEN;
4059
4060 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4061 if (ret != ISCSI_HDR_LEN) {
4062 iscsit_rx_thread_wait_for_tcp(conn);
4063 goto transport_err;
4064 }
4065
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004066 if (conn->conn_ops->HeaderDigest) {
4067 iov.iov_base = &digest;
4068 iov.iov_len = ISCSI_CRC_LEN;
4069
4070 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4071 if (ret != ISCSI_CRC_LEN) {
4072 iscsit_rx_thread_wait_for_tcp(conn);
4073 goto transport_err;
4074 }
4075
4076 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4077 buffer, ISCSI_HDR_LEN,
4078 0, NULL, (u8 *)&checksum);
4079
4080 if (digest != checksum) {
4081 pr_err("HeaderDigest CRC32C failed,"
4082 " received 0x%08x, computed 0x%08x\n",
4083 digest, checksum);
4084 /*
4085 * Set the PDU to 0xff so it will intentionally
4086 * hit default in the switch below.
4087 */
4088 memset(buffer, 0xff, ISCSI_HDR_LEN);
4089 spin_lock_bh(&conn->sess->session_stats_lock);
4090 conn->sess->conn_digest_errors++;
4091 spin_unlock_bh(&conn->sess->session_stats_lock);
4092 } else {
4093 pr_debug("Got HeaderDigest CRC32C"
4094 " 0x%08x\n", checksum);
4095 }
4096 }
4097
4098 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4099 goto transport_err;
4100
4101 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4102
4103 if (conn->sess->sess_ops->SessionType &&
4104 ((!(opcode & ISCSI_OP_TEXT)) ||
4105 (!(opcode & ISCSI_OP_LOGOUT)))) {
4106 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4107 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004108 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4109 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004110 goto transport_err;
4111 }
4112
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004113 ret = iscsi_target_rx_opcode(conn, buffer);
4114 if (ret < 0)
4115 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004116 }
4117
4118transport_err:
4119 if (!signal_pending(current))
4120 atomic_set(&conn->transport_failed, 1);
4121 iscsit_take_action_for_connection_exit(conn);
4122 goto restart;
4123out:
4124 return 0;
4125}
4126
4127static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4128{
4129 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4130 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004131 /*
4132 * We expect this function to only ever be called from either RX or TX
4133 * thread context via iscsit_close_connection() once the other context
4134 * has been reset -> returned sleeping pre-handler state.
4135 */
4136 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004137 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004138
Andy Grover2fbb4712012-04-03 15:51:01 -07004139 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004140 spin_unlock_bh(&conn->cmd_lock);
4141
4142 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004143
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004144 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004145
4146 spin_lock_bh(&conn->cmd_lock);
4147 }
4148 spin_unlock_bh(&conn->cmd_lock);
4149}
4150
4151static void iscsit_stop_timers_for_cmds(
4152 struct iscsi_conn *conn)
4153{
4154 struct iscsi_cmd *cmd;
4155
4156 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004157 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004158 if (cmd->data_direction == DMA_TO_DEVICE)
4159 iscsit_stop_dataout_timer(cmd);
4160 }
4161 spin_unlock_bh(&conn->cmd_lock);
4162}
4163
4164int iscsit_close_connection(
4165 struct iscsi_conn *conn)
4166{
4167 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4168 struct iscsi_session *sess = conn->sess;
4169
4170 pr_debug("Closing iSCSI connection CID %hu on SID:"
4171 " %u\n", conn->cid, sess->sid);
4172 /*
4173 * Always up conn_logout_comp just in case the RX Thread is sleeping
4174 * and the logout response never got sent because the connection
4175 * failed.
4176 */
4177 complete(&conn->conn_logout_comp);
4178
4179 iscsi_release_thread_set(conn);
4180
4181 iscsit_stop_timers_for_cmds(conn);
4182 iscsit_stop_nopin_response_timer(conn);
4183 iscsit_stop_nopin_timer(conn);
4184 iscsit_free_queue_reqs_for_conn(conn);
4185
4186 /*
4187 * During Connection recovery drop unacknowledged out of order
4188 * commands for this connection, and prepare the other commands
4189 * for realligence.
4190 *
4191 * During normal operation clear the out of order commands (but
4192 * do not free the struct iscsi_ooo_cmdsn's) and release all
4193 * struct iscsi_cmds.
4194 */
4195 if (atomic_read(&conn->connection_recovery)) {
4196 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4197 iscsit_prepare_cmds_for_realligance(conn);
4198 } else {
4199 iscsit_clear_ooo_cmdsns_for_conn(conn);
4200 iscsit_release_commands_from_conn(conn);
4201 }
4202
4203 /*
4204 * Handle decrementing session or connection usage count if
4205 * a logout response was not able to be sent because the
4206 * connection failed. Fall back to Session Recovery here.
4207 */
4208 if (atomic_read(&conn->conn_logout_remove)) {
4209 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4210 iscsit_dec_conn_usage_count(conn);
4211 iscsit_dec_session_usage_count(sess);
4212 }
4213 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4214 iscsit_dec_conn_usage_count(conn);
4215
4216 atomic_set(&conn->conn_logout_remove, 0);
4217 atomic_set(&sess->session_reinstatement, 0);
4218 atomic_set(&sess->session_fall_back_to_erl0, 1);
4219 }
4220
4221 spin_lock_bh(&sess->conn_lock);
4222 list_del(&conn->conn_list);
4223
4224 /*
4225 * Attempt to let the Initiator know this connection failed by
4226 * sending an Connection Dropped Async Message on another
4227 * active connection.
4228 */
4229 if (atomic_read(&conn->connection_recovery))
4230 iscsit_build_conn_drop_async_message(conn);
4231
4232 spin_unlock_bh(&sess->conn_lock);
4233
4234 /*
4235 * If connection reinstatement is being performed on this connection,
4236 * up the connection reinstatement semaphore that is being blocked on
4237 * in iscsit_cause_connection_reinstatement().
4238 */
4239 spin_lock_bh(&conn->state_lock);
4240 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4241 spin_unlock_bh(&conn->state_lock);
4242 complete(&conn->conn_wait_comp);
4243 wait_for_completion(&conn->conn_post_wait_comp);
4244 spin_lock_bh(&conn->state_lock);
4245 }
4246
4247 /*
4248 * If connection reinstatement is being performed on this connection
4249 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4250 * connection wait rcfr semaphore that is being blocked on
4251 * an iscsit_connection_reinstatement_rcfr().
4252 */
4253 if (atomic_read(&conn->connection_wait_rcfr)) {
4254 spin_unlock_bh(&conn->state_lock);
4255 complete(&conn->conn_wait_rcfr_comp);
4256 wait_for_completion(&conn->conn_post_wait_comp);
4257 spin_lock_bh(&conn->state_lock);
4258 }
4259 atomic_set(&conn->connection_reinstatement, 1);
4260 spin_unlock_bh(&conn->state_lock);
4261
4262 /*
4263 * If any other processes are accessing this connection pointer we
4264 * must wait until they have completed.
4265 */
4266 iscsit_check_conn_usage_count(conn);
4267
4268 if (conn->conn_rx_hash.tfm)
4269 crypto_free_hash(conn->conn_rx_hash.tfm);
4270 if (conn->conn_tx_hash.tfm)
4271 crypto_free_hash(conn->conn_tx_hash.tfm);
4272
4273 if (conn->conn_cpumask)
4274 free_cpumask_var(conn->conn_cpumask);
4275
4276 kfree(conn->conn_ops);
4277 conn->conn_ops = NULL;
4278
Al Virobf6932f2012-07-21 08:55:18 +01004279 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004280 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004281
4282 if (conn->conn_transport->iscsit_free_conn)
4283 conn->conn_transport->iscsit_free_conn(conn);
4284
4285 iscsit_put_transport(conn->conn_transport);
4286
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004287 conn->thread_set = NULL;
4288
4289 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4290 conn->conn_state = TARG_CONN_STATE_FREE;
4291 kfree(conn);
4292
4293 spin_lock_bh(&sess->conn_lock);
4294 atomic_dec(&sess->nconn);
4295 pr_debug("Decremented iSCSI connection count to %hu from node:"
4296 " %s\n", atomic_read(&sess->nconn),
4297 sess->sess_ops->InitiatorName);
4298 /*
4299 * Make sure that if one connection fails in an non ERL=2 iSCSI
4300 * Session that they all fail.
4301 */
4302 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4303 !atomic_read(&sess->session_logout))
4304 atomic_set(&sess->session_fall_back_to_erl0, 1);
4305
4306 /*
4307 * If this was not the last connection in the session, and we are
4308 * performing session reinstatement or falling back to ERL=0, call
4309 * iscsit_stop_session() without sleeping to shutdown the other
4310 * active connections.
4311 */
4312 if (atomic_read(&sess->nconn)) {
4313 if (!atomic_read(&sess->session_reinstatement) &&
4314 !atomic_read(&sess->session_fall_back_to_erl0)) {
4315 spin_unlock_bh(&sess->conn_lock);
4316 return 0;
4317 }
4318 if (!atomic_read(&sess->session_stop_active)) {
4319 atomic_set(&sess->session_stop_active, 1);
4320 spin_unlock_bh(&sess->conn_lock);
4321 iscsit_stop_session(sess, 0, 0);
4322 return 0;
4323 }
4324 spin_unlock_bh(&sess->conn_lock);
4325 return 0;
4326 }
4327
4328 /*
4329 * If this was the last connection in the session and one of the
4330 * following is occurring:
4331 *
4332 * Session Reinstatement is not being performed, and are falling back
4333 * to ERL=0 call iscsit_close_session().
4334 *
4335 * Session Logout was requested. iscsit_close_session() will be called
4336 * elsewhere.
4337 *
4338 * Session Continuation is not being performed, start the Time2Retain
4339 * handler and check if sleep_on_sess_wait_sem is active.
4340 */
4341 if (!atomic_read(&sess->session_reinstatement) &&
4342 atomic_read(&sess->session_fall_back_to_erl0)) {
4343 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004344 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004345
4346 return 0;
4347 } else if (atomic_read(&sess->session_logout)) {
4348 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4349 sess->session_state = TARG_SESS_STATE_FREE;
4350 spin_unlock_bh(&sess->conn_lock);
4351
4352 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4353 complete(&sess->session_wait_comp);
4354
4355 return 0;
4356 } else {
4357 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4358 sess->session_state = TARG_SESS_STATE_FAILED;
4359
4360 if (!atomic_read(&sess->session_continuation)) {
4361 spin_unlock_bh(&sess->conn_lock);
4362 iscsit_start_time2retain_handler(sess);
4363 } else
4364 spin_unlock_bh(&sess->conn_lock);
4365
4366 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4367 complete(&sess->session_wait_comp);
4368
4369 return 0;
4370 }
4371 spin_unlock_bh(&sess->conn_lock);
4372
4373 return 0;
4374}
4375
4376int iscsit_close_session(struct iscsi_session *sess)
4377{
4378 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4379 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4380
4381 if (atomic_read(&sess->nconn)) {
4382 pr_err("%d connection(s) still exist for iSCSI session"
4383 " to %s\n", atomic_read(&sess->nconn),
4384 sess->sess_ops->InitiatorName);
4385 BUG();
4386 }
4387
4388 spin_lock_bh(&se_tpg->session_lock);
4389 atomic_set(&sess->session_logout, 1);
4390 atomic_set(&sess->session_reinstatement, 1);
4391 iscsit_stop_time2retain_timer(sess);
4392 spin_unlock_bh(&se_tpg->session_lock);
4393
4394 /*
4395 * transport_deregister_session_configfs() will clear the
4396 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4397 * can be setting it again with __transport_register_session() in
4398 * iscsi_post_login_handler() again after the iscsit_stop_session()
4399 * completes in iscsi_np context.
4400 */
4401 transport_deregister_session_configfs(sess->se_sess);
4402
4403 /*
4404 * If any other processes are accessing this session pointer we must
4405 * wait until they have completed. If we are in an interrupt (the
4406 * time2retain handler) and contain and active session usage count we
4407 * restart the timer and exit.
4408 */
4409 if (!in_interrupt()) {
4410 if (iscsit_check_session_usage_count(sess) == 1)
4411 iscsit_stop_session(sess, 1, 1);
4412 } else {
4413 if (iscsit_check_session_usage_count(sess) == 2) {
4414 atomic_set(&sess->session_logout, 0);
4415 iscsit_start_time2retain_handler(sess);
4416 return 0;
4417 }
4418 }
4419
4420 transport_deregister_session(sess->se_sess);
4421
4422 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4423 iscsit_free_connection_recovery_entires(sess);
4424
4425 iscsit_free_all_ooo_cmdsns(sess);
4426
4427 spin_lock_bh(&se_tpg->session_lock);
4428 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4429 sess->session_state = TARG_SESS_STATE_FREE;
4430 pr_debug("Released iSCSI session from node: %s\n",
4431 sess->sess_ops->InitiatorName);
4432 tpg->nsessions--;
4433 if (tpg->tpg_tiqn)
4434 tpg->tpg_tiqn->tiqn_nsessions--;
4435
4436 pr_debug("Decremented number of active iSCSI Sessions on"
4437 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4438
4439 spin_lock(&sess_idr_lock);
4440 idr_remove(&sess_idr, sess->session_index);
4441 spin_unlock(&sess_idr_lock);
4442
4443 kfree(sess->sess_ops);
4444 sess->sess_ops = NULL;
4445 spin_unlock_bh(&se_tpg->session_lock);
4446
4447 kfree(sess);
4448 return 0;
4449}
4450
4451static void iscsit_logout_post_handler_closesession(
4452 struct iscsi_conn *conn)
4453{
4454 struct iscsi_session *sess = conn->sess;
4455
4456 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4457 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4458
4459 atomic_set(&conn->conn_logout_remove, 0);
4460 complete(&conn->conn_logout_comp);
4461
4462 iscsit_dec_conn_usage_count(conn);
4463 iscsit_stop_session(sess, 1, 1);
4464 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004465 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004466}
4467
4468static void iscsit_logout_post_handler_samecid(
4469 struct iscsi_conn *conn)
4470{
4471 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4472 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4473
4474 atomic_set(&conn->conn_logout_remove, 0);
4475 complete(&conn->conn_logout_comp);
4476
4477 iscsit_cause_connection_reinstatement(conn, 1);
4478 iscsit_dec_conn_usage_count(conn);
4479}
4480
4481static void iscsit_logout_post_handler_diffcid(
4482 struct iscsi_conn *conn,
4483 u16 cid)
4484{
4485 struct iscsi_conn *l_conn;
4486 struct iscsi_session *sess = conn->sess;
4487
4488 if (!sess)
4489 return;
4490
4491 spin_lock_bh(&sess->conn_lock);
4492 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4493 if (l_conn->cid == cid) {
4494 iscsit_inc_conn_usage_count(l_conn);
4495 break;
4496 }
4497 }
4498 spin_unlock_bh(&sess->conn_lock);
4499
4500 if (!l_conn)
4501 return;
4502
4503 if (l_conn->sock)
4504 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4505
4506 spin_lock_bh(&l_conn->state_lock);
4507 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4508 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4509 spin_unlock_bh(&l_conn->state_lock);
4510
4511 iscsit_cause_connection_reinstatement(l_conn, 1);
4512 iscsit_dec_conn_usage_count(l_conn);
4513}
4514
4515/*
4516 * Return of 0 causes the TX thread to restart.
4517 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004518int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004519 struct iscsi_cmd *cmd,
4520 struct iscsi_conn *conn)
4521{
4522 int ret = 0;
4523
4524 switch (cmd->logout_reason) {
4525 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4526 switch (cmd->logout_response) {
4527 case ISCSI_LOGOUT_SUCCESS:
4528 case ISCSI_LOGOUT_CLEANUP_FAILED:
4529 default:
4530 iscsit_logout_post_handler_closesession(conn);
4531 break;
4532 }
4533 ret = 0;
4534 break;
4535 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4536 if (conn->cid == cmd->logout_cid) {
4537 switch (cmd->logout_response) {
4538 case ISCSI_LOGOUT_SUCCESS:
4539 case ISCSI_LOGOUT_CLEANUP_FAILED:
4540 default:
4541 iscsit_logout_post_handler_samecid(conn);
4542 break;
4543 }
4544 ret = 0;
4545 } else {
4546 switch (cmd->logout_response) {
4547 case ISCSI_LOGOUT_SUCCESS:
4548 iscsit_logout_post_handler_diffcid(conn,
4549 cmd->logout_cid);
4550 break;
4551 case ISCSI_LOGOUT_CID_NOT_FOUND:
4552 case ISCSI_LOGOUT_CLEANUP_FAILED:
4553 default:
4554 break;
4555 }
4556 ret = 1;
4557 }
4558 break;
4559 case ISCSI_LOGOUT_REASON_RECOVERY:
4560 switch (cmd->logout_response) {
4561 case ISCSI_LOGOUT_SUCCESS:
4562 case ISCSI_LOGOUT_CID_NOT_FOUND:
4563 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4564 case ISCSI_LOGOUT_CLEANUP_FAILED:
4565 default:
4566 break;
4567 }
4568 ret = 1;
4569 break;
4570 default:
4571 break;
4572
4573 }
4574 return ret;
4575}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004576EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004577
4578void iscsit_fail_session(struct iscsi_session *sess)
4579{
4580 struct iscsi_conn *conn;
4581
4582 spin_lock_bh(&sess->conn_lock);
4583 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4584 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4585 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4586 }
4587 spin_unlock_bh(&sess->conn_lock);
4588
4589 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4590 sess->session_state = TARG_SESS_STATE_FAILED;
4591}
4592
4593int iscsit_free_session(struct iscsi_session *sess)
4594{
4595 u16 conn_count = atomic_read(&sess->nconn);
4596 struct iscsi_conn *conn, *conn_tmp = NULL;
4597 int is_last;
4598
4599 spin_lock_bh(&sess->conn_lock);
4600 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4601
4602 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4603 conn_list) {
4604 if (conn_count == 0)
4605 break;
4606
4607 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4608 is_last = 1;
4609 } else {
4610 iscsit_inc_conn_usage_count(conn_tmp);
4611 is_last = 0;
4612 }
4613 iscsit_inc_conn_usage_count(conn);
4614
4615 spin_unlock_bh(&sess->conn_lock);
4616 iscsit_cause_connection_reinstatement(conn, 1);
4617 spin_lock_bh(&sess->conn_lock);
4618
4619 iscsit_dec_conn_usage_count(conn);
4620 if (is_last == 0)
4621 iscsit_dec_conn_usage_count(conn_tmp);
4622
4623 conn_count--;
4624 }
4625
4626 if (atomic_read(&sess->nconn)) {
4627 spin_unlock_bh(&sess->conn_lock);
4628 wait_for_completion(&sess->session_wait_comp);
4629 } else
4630 spin_unlock_bh(&sess->conn_lock);
4631
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004632 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004633 return 0;
4634}
4635
4636void iscsit_stop_session(
4637 struct iscsi_session *sess,
4638 int session_sleep,
4639 int connection_sleep)
4640{
4641 u16 conn_count = atomic_read(&sess->nconn);
4642 struct iscsi_conn *conn, *conn_tmp = NULL;
4643 int is_last;
4644
4645 spin_lock_bh(&sess->conn_lock);
4646 if (session_sleep)
4647 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4648
4649 if (connection_sleep) {
4650 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4651 conn_list) {
4652 if (conn_count == 0)
4653 break;
4654
4655 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4656 is_last = 1;
4657 } else {
4658 iscsit_inc_conn_usage_count(conn_tmp);
4659 is_last = 0;
4660 }
4661 iscsit_inc_conn_usage_count(conn);
4662
4663 spin_unlock_bh(&sess->conn_lock);
4664 iscsit_cause_connection_reinstatement(conn, 1);
4665 spin_lock_bh(&sess->conn_lock);
4666
4667 iscsit_dec_conn_usage_count(conn);
4668 if (is_last == 0)
4669 iscsit_dec_conn_usage_count(conn_tmp);
4670 conn_count--;
4671 }
4672 } else {
4673 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4674 iscsit_cause_connection_reinstatement(conn, 0);
4675 }
4676
4677 if (session_sleep && atomic_read(&sess->nconn)) {
4678 spin_unlock_bh(&sess->conn_lock);
4679 wait_for_completion(&sess->session_wait_comp);
4680 } else
4681 spin_unlock_bh(&sess->conn_lock);
4682}
4683
4684int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4685{
4686 struct iscsi_session *sess;
4687 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4688 struct se_session *se_sess, *se_sess_tmp;
4689 int session_count = 0;
4690
4691 spin_lock_bh(&se_tpg->session_lock);
4692 if (tpg->nsessions && !force) {
4693 spin_unlock_bh(&se_tpg->session_lock);
4694 return -1;
4695 }
4696
4697 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4698 sess_list) {
4699 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4700
4701 spin_lock(&sess->conn_lock);
4702 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4703 atomic_read(&sess->session_logout) ||
4704 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4705 spin_unlock(&sess->conn_lock);
4706 continue;
4707 }
4708 atomic_set(&sess->session_reinstatement, 1);
4709 spin_unlock(&sess->conn_lock);
4710 spin_unlock_bh(&se_tpg->session_lock);
4711
4712 iscsit_free_session(sess);
4713 spin_lock_bh(&se_tpg->session_lock);
4714
4715 session_count++;
4716 }
4717 spin_unlock_bh(&se_tpg->session_lock);
4718
4719 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4720 " Group: %hu\n", session_count, tpg->tpgt);
4721 return 0;
4722}
4723
4724MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4725MODULE_VERSION("4.1.x");
4726MODULE_AUTHOR("nab@Linux-iSCSI.org");
4727MODULE_LICENSE("GPL");
4728
4729module_init(iscsi_target_init_module);
4730module_exit(iscsi_target_cleanup_module);