blob: 8203bf3de8483859484910f53ddcf071491b7bc5 [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
66struct kmem_cache *lio_cmd_cache;
67struct kmem_cache *lio_qr_cache;
68struct kmem_cache *lio_dr_cache;
69struct kmem_cache *lio_ooo_cache;
70struct kmem_cache *lio_r2t_cache;
71
72static int iscsit_handle_immediate_data(struct iscsi_cmd *,
73 unsigned char *buf, u32);
74static int iscsit_logout_post_handler(struct iscsi_cmd *, struct iscsi_conn *);
75
76struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
77{
78 struct iscsi_tiqn *tiqn = NULL;
79
80 spin_lock(&tiqn_lock);
81 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
82 if (!strcmp(tiqn->tiqn, buf)) {
83
84 spin_lock(&tiqn->tiqn_state_lock);
85 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
86 tiqn->tiqn_access_count++;
87 spin_unlock(&tiqn->tiqn_state_lock);
88 spin_unlock(&tiqn_lock);
89 return tiqn;
90 }
91 spin_unlock(&tiqn->tiqn_state_lock);
92 }
93 }
94 spin_unlock(&tiqn_lock);
95
96 return NULL;
97}
98
99static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
100{
101 spin_lock(&tiqn->tiqn_state_lock);
102 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
103 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
104 spin_unlock(&tiqn->tiqn_state_lock);
105 return 0;
106 }
107 spin_unlock(&tiqn->tiqn_state_lock);
108
109 return -1;
110}
111
112void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
113{
114 spin_lock(&tiqn->tiqn_state_lock);
115 tiqn->tiqn_access_count--;
116 spin_unlock(&tiqn->tiqn_state_lock);
117}
118
119/*
120 * Note that IQN formatting is expected to be done in userspace, and
121 * no explict IQN format checks are done here.
122 */
123struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
124{
125 struct iscsi_tiqn *tiqn = NULL;
126 int ret;
127
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300128 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000129 pr_err("Target IQN exceeds %d bytes\n",
130 ISCSI_IQN_LEN);
131 return ERR_PTR(-EINVAL);
132 }
133
134 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
135 if (!tiqn) {
136 pr_err("Unable to allocate struct iscsi_tiqn\n");
137 return ERR_PTR(-ENOMEM);
138 }
139
140 sprintf(tiqn->tiqn, "%s", buf);
141 INIT_LIST_HEAD(&tiqn->tiqn_list);
142 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
143 spin_lock_init(&tiqn->tiqn_state_lock);
144 spin_lock_init(&tiqn->tiqn_tpg_lock);
145 spin_lock_init(&tiqn->sess_err_stats.lock);
146 spin_lock_init(&tiqn->login_stats.lock);
147 spin_lock_init(&tiqn->logout_stats.lock);
148
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000149 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
150
Tejun Heoc9365bd2013-02-27 17:04:43 -0800151 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000152 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800153
154 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000155 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800156 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000157 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800158 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000159 kfree(tiqn);
160 return ERR_PTR(ret);
161 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800162 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000163 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800164
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000165 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800166 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000167
168 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
169
170 return tiqn;
171
172}
173
174static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
175{
176 /*
177 * Wait for accesses to said struct iscsi_tiqn to end.
178 */
179 spin_lock(&tiqn->tiqn_state_lock);
180 while (tiqn->tiqn_access_count != 0) {
181 spin_unlock(&tiqn->tiqn_state_lock);
182 msleep(10);
183 spin_lock(&tiqn->tiqn_state_lock);
184 }
185 spin_unlock(&tiqn->tiqn_state_lock);
186}
187
188void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
189{
190 /*
191 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
192 * while holding tiqn->tiqn_state_lock. This means that all subsequent
193 * attempts to access this struct iscsi_tiqn will fail from both transport
194 * fabric and control code paths.
195 */
196 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
197 pr_err("iscsit_set_tiqn_shutdown() failed\n");
198 return;
199 }
200
201 iscsit_wait_for_tiqn(tiqn);
202
203 spin_lock(&tiqn_lock);
204 list_del(&tiqn->tiqn_list);
205 idr_remove(&tiqn_idr, tiqn->tiqn_index);
206 spin_unlock(&tiqn_lock);
207
208 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
209 tiqn->tiqn);
210 kfree(tiqn);
211}
212
213int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
214{
215 int ret;
216 /*
217 * Determine if the network portal is accepting storage traffic.
218 */
219 spin_lock_bh(&np->np_thread_lock);
220 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
221 spin_unlock_bh(&np->np_thread_lock);
222 return -1;
223 }
224 if (np->np_login_tpg) {
225 pr_err("np->np_login_tpg() is not NULL!\n");
226 spin_unlock_bh(&np->np_thread_lock);
227 return -1;
228 }
229 spin_unlock_bh(&np->np_thread_lock);
230 /*
231 * Determine if the portal group is accepting storage traffic.
232 */
233 spin_lock_bh(&tpg->tpg_state_lock);
234 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
235 spin_unlock_bh(&tpg->tpg_state_lock);
236 return -1;
237 }
238 spin_unlock_bh(&tpg->tpg_state_lock);
239
240 /*
241 * Here we serialize access across the TIQN+TPG Tuple.
242 */
243 ret = mutex_lock_interruptible(&tpg->np_login_lock);
244 if ((ret != 0) || signal_pending(current))
245 return -1;
246
247 spin_lock_bh(&np->np_thread_lock);
248 np->np_login_tpg = tpg;
249 spin_unlock_bh(&np->np_thread_lock);
250
251 return 0;
252}
253
254int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
255{
256 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
257
258 spin_lock_bh(&np->np_thread_lock);
259 np->np_login_tpg = NULL;
260 spin_unlock_bh(&np->np_thread_lock);
261
262 mutex_unlock(&tpg->np_login_lock);
263
264 if (tiqn)
265 iscsit_put_tiqn_for_login(tiqn);
266
267 return 0;
268}
269
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800270bool iscsit_check_np_match(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000271 struct __kernel_sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800272 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000273 int network_transport)
274{
275 struct sockaddr_in *sock_in, *sock_in_e;
276 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800277 bool ip_match = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000278 u16 port;
279
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800280 if (sockaddr->ss_family == AF_INET6) {
281 sock_in6 = (struct sockaddr_in6 *)sockaddr;
282 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
283
284 if (!memcmp(&sock_in6->sin6_addr.in6_u,
285 &sock_in6_e->sin6_addr.in6_u,
286 sizeof(struct in6_addr)))
287 ip_match = true;
288
289 port = ntohs(sock_in6->sin6_port);
290 } else {
291 sock_in = (struct sockaddr_in *)sockaddr;
292 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
293
294 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
295 ip_match = true;
296
297 port = ntohs(sock_in->sin_port);
298 }
299
300 if ((ip_match == true) && (np->np_port == port) &&
301 (np->np_network_transport == network_transport))
302 return true;
303
304 return false;
305}
306
307static struct iscsi_np *iscsit_get_np(
308 struct __kernel_sockaddr_storage *sockaddr,
309 int network_transport)
310{
311 struct iscsi_np *np;
312 bool match;
313
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000314 spin_lock_bh(&np_lock);
315 list_for_each_entry(np, &g_np_list, np_list) {
316 spin_lock(&np->np_thread_lock);
317 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
318 spin_unlock(&np->np_thread_lock);
319 continue;
320 }
321
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800322 match = iscsit_check_np_match(sockaddr, np, network_transport);
323 if (match == true) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000324 /*
325 * Increment the np_exports reference count now to
326 * prevent iscsit_del_np() below from being called
327 * while iscsi_tpg_add_network_portal() is called.
328 */
329 np->np_exports++;
330 spin_unlock(&np->np_thread_lock);
331 spin_unlock_bh(&np_lock);
332 return np;
333 }
334 spin_unlock(&np->np_thread_lock);
335 }
336 spin_unlock_bh(&np_lock);
337
338 return NULL;
339}
340
341struct iscsi_np *iscsit_add_np(
342 struct __kernel_sockaddr_storage *sockaddr,
343 char *ip_str,
344 int network_transport)
345{
346 struct sockaddr_in *sock_in;
347 struct sockaddr_in6 *sock_in6;
348 struct iscsi_np *np;
349 int ret;
350 /*
351 * Locate the existing struct iscsi_np if already active..
352 */
353 np = iscsit_get_np(sockaddr, network_transport);
354 if (np)
355 return np;
356
357 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
358 if (!np) {
359 pr_err("Unable to allocate memory for struct iscsi_np\n");
360 return ERR_PTR(-ENOMEM);
361 }
362
363 np->np_flags |= NPF_IP_NETWORK;
364 if (sockaddr->ss_family == AF_INET6) {
365 sock_in6 = (struct sockaddr_in6 *)sockaddr;
366 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
367 np->np_port = ntohs(sock_in6->sin6_port);
368 } else {
369 sock_in = (struct sockaddr_in *)sockaddr;
370 sprintf(np->np_ip, "%s", ip_str);
371 np->np_port = ntohs(sock_in->sin_port);
372 }
373
374 np->np_network_transport = network_transport;
375 spin_lock_init(&np->np_thread_lock);
376 init_completion(&np->np_restart_comp);
377 INIT_LIST_HEAD(&np->np_list);
378
379 ret = iscsi_target_setup_login_socket(np, sockaddr);
380 if (ret != 0) {
381 kfree(np);
382 return ERR_PTR(ret);
383 }
384
385 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
386 if (IS_ERR(np->np_thread)) {
387 pr_err("Unable to create kthread: iscsi_np\n");
388 ret = PTR_ERR(np->np_thread);
389 kfree(np);
390 return ERR_PTR(ret);
391 }
392 /*
393 * Increment the np_exports reference count now to prevent
394 * iscsit_del_np() below from being run while a new call to
395 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
396 * active. We don't need to hold np->np_thread_lock at this
397 * point because iscsi_np has not been added to g_np_list yet.
398 */
399 np->np_exports = 1;
400
401 spin_lock_bh(&np_lock);
402 list_add_tail(&np->np_list, &g_np_list);
403 spin_unlock_bh(&np_lock);
404
405 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800406 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000407
408 return np;
409}
410
411int iscsit_reset_np_thread(
412 struct iscsi_np *np,
413 struct iscsi_tpg_np *tpg_np,
414 struct iscsi_portal_group *tpg)
415{
416 spin_lock_bh(&np->np_thread_lock);
417 if (tpg && tpg_np) {
418 /*
419 * The reset operation need only be performed when the
420 * passed struct iscsi_portal_group has a login in progress
421 * to one of the network portals.
422 */
423 if (tpg_np->tpg_np->np_login_tpg != tpg) {
424 spin_unlock_bh(&np->np_thread_lock);
425 return 0;
426 }
427 }
428 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
429 spin_unlock_bh(&np->np_thread_lock);
430 return 0;
431 }
432 np->np_thread_state = ISCSI_NP_THREAD_RESET;
433
434 if (np->np_thread) {
435 spin_unlock_bh(&np->np_thread_lock);
436 send_sig(SIGINT, np->np_thread, 1);
437 wait_for_completion(&np->np_restart_comp);
438 spin_lock_bh(&np->np_thread_lock);
439 }
440 spin_unlock_bh(&np->np_thread_lock);
441
442 return 0;
443}
444
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800445static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000446{
Al Virobf6932f2012-07-21 08:55:18 +0100447 if (np->np_socket)
448 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000449}
450
451int iscsit_del_np(struct iscsi_np *np)
452{
453 spin_lock_bh(&np->np_thread_lock);
454 np->np_exports--;
455 if (np->np_exports) {
456 spin_unlock_bh(&np->np_thread_lock);
457 return 0;
458 }
459 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
460 spin_unlock_bh(&np->np_thread_lock);
461
462 if (np->np_thread) {
463 /*
464 * We need to send the signal to wakeup Linux/Net
465 * which may be sleeping in sock_accept()..
466 */
467 send_sig(SIGINT, np->np_thread, 1);
468 kthread_stop(np->np_thread);
469 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800470
471 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000472
473 spin_lock_bh(&np_lock);
474 list_del(&np->np_list);
475 spin_unlock_bh(&np_lock);
476
477 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800478 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000479
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800480 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000481 kfree(np);
482 return 0;
483}
484
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800485static struct iscsit_transport iscsi_target_transport = {
486 .name = "iSCSI/TCP",
487 .transport_type = ISCSI_TCP,
488 .owner = NULL,
489 .iscsit_setup_np = iscsit_setup_np,
490 .iscsit_accept_np = iscsit_accept_np,
491 .iscsit_free_np = iscsit_free_np,
492 .iscsit_get_login_rx = iscsit_get_login_rx,
493 .iscsit_put_login_tx = iscsit_put_login_tx,
494};
495
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000496static int __init iscsi_target_init_module(void)
497{
498 int ret = 0;
499
500 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
501
502 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
503 if (!iscsit_global) {
504 pr_err("Unable to allocate memory for iscsit_global\n");
505 return -1;
506 }
507 mutex_init(&auth_id_lock);
508 spin_lock_init(&sess_idr_lock);
509 idr_init(&tiqn_idr);
510 idr_init(&sess_idr);
511
512 ret = iscsi_target_register_configfs();
513 if (ret < 0)
514 goto out;
515
516 ret = iscsi_thread_set_init();
517 if (ret < 0)
518 goto configfs_out;
519
520 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
521 TARGET_THREAD_SET_COUNT) {
522 pr_err("iscsi_allocate_thread_sets() returned"
523 " unexpected value!\n");
524 goto ts_out1;
525 }
526
527 lio_cmd_cache = kmem_cache_create("lio_cmd_cache",
528 sizeof(struct iscsi_cmd), __alignof__(struct iscsi_cmd),
529 0, NULL);
530 if (!lio_cmd_cache) {
531 pr_err("Unable to kmem_cache_create() for"
532 " lio_cmd_cache\n");
533 goto ts_out2;
534 }
535
536 lio_qr_cache = kmem_cache_create("lio_qr_cache",
537 sizeof(struct iscsi_queue_req),
538 __alignof__(struct iscsi_queue_req), 0, NULL);
539 if (!lio_qr_cache) {
540 pr_err("nable to kmem_cache_create() for"
541 " lio_qr_cache\n");
542 goto cmd_out;
543 }
544
545 lio_dr_cache = kmem_cache_create("lio_dr_cache",
546 sizeof(struct iscsi_datain_req),
547 __alignof__(struct iscsi_datain_req), 0, NULL);
548 if (!lio_dr_cache) {
549 pr_err("Unable to kmem_cache_create() for"
550 " lio_dr_cache\n");
551 goto qr_out;
552 }
553
554 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
555 sizeof(struct iscsi_ooo_cmdsn),
556 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
557 if (!lio_ooo_cache) {
558 pr_err("Unable to kmem_cache_create() for"
559 " lio_ooo_cache\n");
560 goto dr_out;
561 }
562
563 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
564 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
565 0, NULL);
566 if (!lio_r2t_cache) {
567 pr_err("Unable to kmem_cache_create() for"
568 " lio_r2t_cache\n");
569 goto ooo_out;
570 }
571
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800572 iscsit_register_transport(&iscsi_target_transport);
573
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000574 if (iscsit_load_discovery_tpg() < 0)
575 goto r2t_out;
576
577 return ret;
578r2t_out:
579 kmem_cache_destroy(lio_r2t_cache);
580ooo_out:
581 kmem_cache_destroy(lio_ooo_cache);
582dr_out:
583 kmem_cache_destroy(lio_dr_cache);
584qr_out:
585 kmem_cache_destroy(lio_qr_cache);
586cmd_out:
587 kmem_cache_destroy(lio_cmd_cache);
588ts_out2:
589 iscsi_deallocate_thread_sets();
590ts_out1:
591 iscsi_thread_set_free();
592configfs_out:
593 iscsi_target_deregister_configfs();
594out:
595 kfree(iscsit_global);
596 return -ENOMEM;
597}
598
599static void __exit iscsi_target_cleanup_module(void)
600{
601 iscsi_deallocate_thread_sets();
602 iscsi_thread_set_free();
603 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800604 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000605 kmem_cache_destroy(lio_cmd_cache);
606 kmem_cache_destroy(lio_qr_cache);
607 kmem_cache_destroy(lio_dr_cache);
608 kmem_cache_destroy(lio_ooo_cache);
609 kmem_cache_destroy(lio_r2t_cache);
610
611 iscsi_target_deregister_configfs();
612
613 kfree(iscsit_global);
614}
615
Andy Grover8b1e1242012-04-03 15:51:12 -0700616static int iscsit_add_reject(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000617 u8 reason,
618 int fail_conn,
619 unsigned char *buf,
620 struct iscsi_conn *conn)
621{
622 struct iscsi_cmd *cmd;
623 struct iscsi_reject *hdr;
624 int ret;
625
626 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
627 if (!cmd)
628 return -1;
629
630 cmd->iscsi_opcode = ISCSI_OP_REJECT;
631 if (fail_conn)
632 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
633
634 hdr = (struct iscsi_reject *) cmd->pdu;
635 hdr->reason = reason;
636
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100637 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000638 if (!cmd->buf_ptr) {
639 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
640 iscsit_release_cmd(cmd);
641 return -1;
642 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000643
644 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700645 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000646 spin_unlock_bh(&conn->cmd_lock);
647
648 cmd->i_state = ISTATE_SEND_REJECT;
649 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
650
651 ret = wait_for_completion_interruptible(&cmd->reject_comp);
652 if (ret != 0)
653 return -1;
654
655 return (!fail_conn) ? 0 : -1;
656}
657
658int iscsit_add_reject_from_cmd(
659 u8 reason,
660 int fail_conn,
661 int add_to_conn,
662 unsigned char *buf,
663 struct iscsi_cmd *cmd)
664{
665 struct iscsi_conn *conn;
666 struct iscsi_reject *hdr;
667 int ret;
668
669 if (!cmd->conn) {
670 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
671 cmd->init_task_tag);
672 return -1;
673 }
674 conn = cmd->conn;
675
676 cmd->iscsi_opcode = ISCSI_OP_REJECT;
677 if (fail_conn)
678 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
679
680 hdr = (struct iscsi_reject *) cmd->pdu;
681 hdr->reason = reason;
682
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100683 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000684 if (!cmd->buf_ptr) {
685 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
686 iscsit_release_cmd(cmd);
687 return -1;
688 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000689
690 if (add_to_conn) {
691 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700692 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000693 spin_unlock_bh(&conn->cmd_lock);
694 }
695
696 cmd->i_state = ISTATE_SEND_REJECT;
697 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
698
699 ret = wait_for_completion_interruptible(&cmd->reject_comp);
700 if (ret != 0)
701 return -1;
702
703 return (!fail_conn) ? 0 : -1;
704}
705
706/*
707 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700708 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000709 */
710static int iscsit_map_iovec(
711 struct iscsi_cmd *cmd,
712 struct kvec *iov,
713 u32 data_offset,
714 u32 data_length)
715{
716 u32 i = 0;
717 struct scatterlist *sg;
718 unsigned int page_off;
719
720 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700721 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000722 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700723 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000724 page_off = (data_offset % PAGE_SIZE);
725
726 cmd->first_data_sg = sg;
727 cmd->first_data_sg_off = page_off;
728
729 while (data_length) {
730 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
731
732 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
733 iov[i].iov_len = cur_len;
734
735 data_length -= cur_len;
736 page_off = 0;
737 sg = sg_next(sg);
738 i++;
739 }
740
741 cmd->kmapped_nents = i;
742
743 return i;
744}
745
746static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
747{
748 u32 i;
749 struct scatterlist *sg;
750
751 sg = cmd->first_data_sg;
752
753 for (i = 0; i < cmd->kmapped_nents; i++)
754 kunmap(sg_page(&sg[i]));
755}
756
757static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
758{
759 struct iscsi_cmd *cmd;
760
761 conn->exp_statsn = exp_statsn;
762
763 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700764 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000765 spin_lock(&cmd->istate_lock);
766 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800767 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000768 cmd->i_state = ISTATE_REMOVE;
769 spin_unlock(&cmd->istate_lock);
770 iscsit_add_cmd_to_immediate_queue(cmd, conn,
771 cmd->i_state);
772 continue;
773 }
774 spin_unlock(&cmd->istate_lock);
775 }
776 spin_unlock_bh(&conn->cmd_lock);
777}
778
779static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
780{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700781 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000782
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400783 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000784
785 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
786 if (!cmd->iov_data) {
787 pr_err("Unable to allocate cmd->iov_data\n");
788 return -ENOMEM;
789 }
790
791 cmd->orig_iov_data_count = iov_count;
792 return 0;
793}
794
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000795static int iscsit_handle_scsi_cmd(
796 struct iscsi_conn *conn,
797 unsigned char *buf)
798{
Christoph Hellwigde103c92012-11-06 12:24:09 -0800799 int data_direction, payload_length, cmdsn_ret = 0, immed_ret;
800 struct iscsi_cmd *cmd = NULL;
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");
823 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
824 buf, conn);
825 }
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]);
844 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
845 buf, conn);
846 }
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");
854 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
855 buf, conn);
856 }
857
858 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
859 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
860 pr_err("Bidirectional operations not supported!\n");
861 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
862 buf, conn);
863 }
864
865 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
866 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
867 " Scsi Command PDU.\n");
868 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
869 buf, conn);
870 }
871
872 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
873 pr_err("ImmediateData=No but DataSegmentLength=%u,"
874 " protocol error.\n", payload_length);
875 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
876 buf, conn);
877 }
878
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400879 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");
884 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
885 buf, conn);
886 }
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);
892 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
893 buf, conn);
894 }
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 Bellingere48354c2011-07-23 06:43:04 +0000900 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
901 buf, conn);
902 }
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);
908 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
909 buf, conn);
910 }
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 = iscsit_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000917 if (!cmd)
918 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
Andy Groverd28b11692012-04-03 15:51:22 -0700919 buf, conn);
920
921 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700922 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
923 /*
924 * Figure out the SAM Task Attribute for the incoming SCSI CDB
925 */
926 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
927 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
928 sam_task_attr = MSG_SIMPLE_TAG;
929 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
930 sam_task_attr = MSG_ORDERED_TAG;
931 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
932 sam_task_attr = MSG_HEAD_TAG;
933 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
934 sam_task_attr = MSG_ACA_TAG;
935 else {
936 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
937 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
938 sam_task_attr = MSG_SIMPLE_TAG;
939 }
940
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000941 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
942 cmd->i_state = ISTATE_NEW_CMD;
943 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
944 cmd->immediate_data = (payload_length) ? 1 : 0;
945 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
946 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
947 if (cmd->unsolicited_data)
948 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
949
950 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
951 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
952 spin_lock_bh(&conn->sess->ttt_lock);
953 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
954 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
955 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
956 spin_unlock_bh(&conn->sess->ttt_lock);
957 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
958 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400959 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
960 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000961 cmd->first_burst_len = payload_length;
962
963 if (cmd->data_direction == DMA_FROM_DEVICE) {
964 struct iscsi_datain_req *dr;
965
966 dr = iscsit_allocate_datain_req();
967 if (!dr)
968 return iscsit_add_reject_from_cmd(
969 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
970 1, 1, buf, cmd);
971
972 iscsit_attach_datain_req(cmd, dr);
973 }
974
975 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700976 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
977 */
978 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400979 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
980 cmd->data_direction, sam_task_attr,
981 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700982
983 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
984 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
985 hdr->cmdsn, hdr->data_length, payload_length, conn->cid);
986
Christoph Hellwigde103c92012-11-06 12:24:09 -0800987 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
988 scsilun_to_int(&hdr->lun));
989 if (cmd->sense_reason)
990 goto attach_cmd;
991
992 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
993 if (cmd->sense_reason) {
994 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
995 return iscsit_add_reject_from_cmd(
996 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
997 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000998 }
Christoph Hellwigde103c92012-11-06 12:24:09 -0800999
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001000 goto attach_cmd;
1001 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001002
Christoph Hellwigde103c92012-11-06 12:24:09 -08001003 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001004 return iscsit_add_reject_from_cmd(
Christoph Hellwigde103c92012-11-06 12:24:09 -08001005 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1006 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001007 }
1008
1009attach_cmd:
1010 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001011 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001012 spin_unlock_bh(&conn->cmd_lock);
1013 /*
1014 * Check if we need to delay processing because of ALUA
1015 * Active/NonOptimized primary access state..
1016 */
1017 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001018
Christoph Hellwigde103c92012-11-06 12:24:09 -08001019 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001020 return iscsit_add_reject_from_cmd(
1021 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
Nicholas Bellingercd931ee2012-01-16 17:11:54 -08001022 1, 0, buf, cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001023 }
1024
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001025 /*
1026 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1027 * the Immediate Bit is not set, and no Immediate
1028 * Data is attached.
1029 *
1030 * A PDU/CmdSN carrying Immediate Data can only
1031 * be processed after the DataCRC has passed.
1032 * If the DataCRC fails, the CmdSN MUST NOT
1033 * be acknowledged. (See below)
1034 */
1035 if (!cmd->immediate_data) {
1036 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001037 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1038 return 0;
1039 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001040 return iscsit_add_reject_from_cmd(
1041 ISCSI_REASON_PROTOCOL_ERROR,
1042 1, 0, buf, cmd);
1043 }
1044
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001045 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001046
1047 /*
1048 * If no Immediate Data is attached, it's OK to return now.
1049 */
1050 if (!cmd->immediate_data) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08001051 if (!cmd->sense_reason && cmd->unsolicited_data) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001052 iscsit_set_dataout_sequence_values(cmd);
1053
1054 spin_lock_bh(&cmd->dataout_timeout_lock);
1055 iscsit_start_dataout_timer(cmd, cmd->conn);
1056 spin_unlock_bh(&cmd->dataout_timeout_lock);
1057 }
1058
1059 return 0;
1060 }
1061
1062 /*
1063 * Early CHECK_CONDITIONs never make it to the transport processing
1064 * thread. They are processed in CmdSN order by
1065 * iscsit_check_received_cmdsn() below.
1066 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001067 if (cmd->sense_reason) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001068 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001069 goto after_immediate_data;
1070 }
1071 /*
1072 * Call directly into transport_generic_new_cmd() to perform
1073 * the backend memory allocation.
1074 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001075 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1076 if (cmd->sense_reason) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001077 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001078 goto after_immediate_data;
1079 }
1080
1081 immed_ret = iscsit_handle_immediate_data(cmd, buf, payload_length);
1082after_immediate_data:
1083 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1084 /*
1085 * A PDU/CmdSN carrying Immediate Data passed
1086 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1087 * Immediate Bit is not set.
1088 */
1089 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1090 /*
1091 * Special case for Unsupported SAM WRITE Opcodes
1092 * and ImmediateData=Yes.
1093 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001094 if (cmd->sense_reason) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001095 if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
1096 return -1;
1097 } else if (cmd->unsolicited_data) {
1098 iscsit_set_dataout_sequence_values(cmd);
1099
1100 spin_lock_bh(&cmd->dataout_timeout_lock);
1101 iscsit_start_dataout_timer(cmd, cmd->conn);
1102 spin_unlock_bh(&cmd->dataout_timeout_lock);
1103 }
1104
1105 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1106 return iscsit_add_reject_from_cmd(
1107 ISCSI_REASON_PROTOCOL_ERROR,
1108 1, 0, buf, cmd);
1109
1110 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1111 /*
1112 * Immediate Data failed DataCRC and ERL>=1,
1113 * silently drop this PDU and let the initiator
1114 * plug the CmdSN gap.
1115 *
1116 * FIXME: Send Unsolicited NOPIN with reserved
1117 * TTT here to help the initiator figure out
1118 * the missing CmdSN, although they should be
1119 * intelligent enough to determine the missing
1120 * CmdSN and issue a retry to plug the sequence.
1121 */
1122 cmd->i_state = ISTATE_REMOVE;
1123 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1124 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1125 return -1;
1126
1127 return 0;
1128}
1129
1130static u32 iscsit_do_crypto_hash_sg(
1131 struct hash_desc *hash,
1132 struct iscsi_cmd *cmd,
1133 u32 data_offset,
1134 u32 data_length,
1135 u32 padding,
1136 u8 *pad_bytes)
1137{
1138 u32 data_crc;
1139 u32 i;
1140 struct scatterlist *sg;
1141 unsigned int page_off;
1142
1143 crypto_hash_init(hash);
1144
1145 sg = cmd->first_data_sg;
1146 page_off = cmd->first_data_sg_off;
1147
1148 i = 0;
1149 while (data_length) {
1150 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1151
1152 crypto_hash_update(hash, &sg[i], cur_len);
1153
1154 data_length -= cur_len;
1155 page_off = 0;
1156 i++;
1157 }
1158
1159 if (padding) {
1160 struct scatterlist pad_sg;
1161
1162 sg_init_one(&pad_sg, pad_bytes, padding);
1163 crypto_hash_update(hash, &pad_sg, padding);
1164 }
1165 crypto_hash_final(hash, (u8 *) &data_crc);
1166
1167 return data_crc;
1168}
1169
1170static void iscsit_do_crypto_hash_buf(
1171 struct hash_desc *hash,
1172 unsigned char *buf,
1173 u32 payload_length,
1174 u32 padding,
1175 u8 *pad_bytes,
1176 u8 *data_crc)
1177{
1178 struct scatterlist sg;
1179
1180 crypto_hash_init(hash);
1181
Jörn Engel8359cf42011-11-24 02:05:51 +01001182 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001183 crypto_hash_update(hash, &sg, payload_length);
1184
1185 if (padding) {
1186 sg_init_one(&sg, pad_bytes, padding);
1187 crypto_hash_update(hash, &sg, padding);
1188 }
1189 crypto_hash_final(hash, data_crc);
1190}
1191
1192static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1193{
1194 int iov_ret, ooo_cmdsn = 0, ret;
1195 u8 data_crc_failed = 0;
1196 u32 checksum, iov_count = 0, padding = 0, rx_got = 0;
1197 u32 rx_size = 0, payload_length;
1198 struct iscsi_cmd *cmd = NULL;
1199 struct se_cmd *se_cmd;
1200 struct iscsi_data *hdr;
1201 struct kvec *iov;
1202 unsigned long flags;
1203
1204 hdr = (struct iscsi_data *) buf;
1205 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001206
1207 if (!payload_length) {
1208 pr_err("DataOUT payload is ZERO, protocol error.\n");
1209 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1210 buf, conn);
1211 }
1212
1213 /* iSCSI write */
1214 spin_lock_bh(&conn->sess->session_stats_lock);
1215 conn->sess->rx_data_octets += payload_length;
1216 if (conn->sess->se_sess->se_node_acl) {
1217 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1218 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1219 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1220 }
1221 spin_unlock_bh(&conn->sess->session_stats_lock);
1222
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001223 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001224 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001225 " MaxXmitDataSegmentLength: %u\n", payload_length,
1226 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001227 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1228 buf, conn);
1229 }
1230
1231 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1232 payload_length);
1233 if (!cmd)
1234 return 0;
1235
1236 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1237 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1238 hdr->itt, hdr->ttt, hdr->datasn, hdr->offset,
1239 payload_length, conn->cid);
1240
1241 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1242 pr_err("Command ITT: 0x%08x received DataOUT after"
1243 " last DataOUT received, dumping payload\n",
1244 cmd->init_task_tag);
1245 return iscsit_dump_data_payload(conn, payload_length, 1);
1246 }
1247
1248 if (cmd->data_direction != DMA_TO_DEVICE) {
1249 pr_err("Command ITT: 0x%08x received DataOUT for a"
1250 " NON-WRITE command.\n", cmd->init_task_tag);
1251 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1252 1, 0, buf, cmd);
1253 }
1254 se_cmd = &cmd->se_cmd;
1255 iscsit_mod_dataout_timer(cmd);
1256
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001257 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001258 pr_err("DataOut Offset: %u, Length %u greater than"
1259 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001260 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001261 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
1262 1, 0, buf, cmd);
1263 }
1264
1265 if (cmd->unsolicited_data) {
1266 int dump_unsolicited_data = 0;
1267
1268 if (conn->sess->sess_ops->InitialR2T) {
1269 pr_err("Received unexpected unsolicited data"
1270 " while InitialR2T=Yes, protocol error.\n");
1271 transport_send_check_condition_and_sense(&cmd->se_cmd,
1272 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1273 return -1;
1274 }
1275 /*
1276 * Special case for dealing with Unsolicited DataOUT
1277 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1278 * failures;
1279 */
1280
1281 /* Something's amiss if we're not in WRITE_PENDING state... */
1282 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1283 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1284 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1285
1286 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001287 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001288 dump_unsolicited_data = 1;
1289 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1290
1291 if (dump_unsolicited_data) {
1292 /*
1293 * Check if a delayed TASK_ABORTED status needs to
1294 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1295 * received with the unsolicitied data out.
1296 */
1297 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1298 iscsit_stop_dataout_timer(cmd);
1299
1300 transport_check_aborted_status(se_cmd,
1301 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1302 return iscsit_dump_data_payload(conn, payload_length, 1);
1303 }
1304 } else {
1305 /*
1306 * For the normal solicited data path:
1307 *
1308 * Check for a delayed TASK_ABORTED status and dump any
1309 * incoming data out payload if one exists. Also, when the
1310 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1311 * data out sequence, we decrement outstanding_r2ts. Once
1312 * outstanding_r2ts reaches zero, go ahead and send the delayed
1313 * TASK_ABORTED status.
1314 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001315 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001316 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1317 if (--cmd->outstanding_r2ts < 1) {
1318 iscsit_stop_dataout_timer(cmd);
1319 transport_check_aborted_status(
1320 se_cmd, 1);
1321 }
1322
1323 return iscsit_dump_data_payload(conn, payload_length, 1);
1324 }
1325 }
1326 /*
1327 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1328 * within-command recovery checks before receiving the payload.
1329 */
1330 ret = iscsit_check_pre_dataout(cmd, buf);
1331 if (ret == DATAOUT_WITHIN_COMMAND_RECOVERY)
1332 return 0;
1333 else if (ret == DATAOUT_CANNOT_RECOVER)
1334 return -1;
1335
1336 rx_size += payload_length;
1337 iov = &cmd->iov_data[0];
1338
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001339 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1340 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001341 if (iov_ret < 0)
1342 return -1;
1343
1344 iov_count += iov_ret;
1345
1346 padding = ((-payload_length) & 3);
1347 if (padding != 0) {
1348 iov[iov_count].iov_base = cmd->pad_bytes;
1349 iov[iov_count++].iov_len = padding;
1350 rx_size += padding;
1351 pr_debug("Receiving %u padding bytes.\n", padding);
1352 }
1353
1354 if (conn->conn_ops->DataDigest) {
1355 iov[iov_count].iov_base = &checksum;
1356 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1357 rx_size += ISCSI_CRC_LEN;
1358 }
1359
1360 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1361
1362 iscsit_unmap_iovec(cmd);
1363
1364 if (rx_got != rx_size)
1365 return -1;
1366
1367 if (conn->conn_ops->DataDigest) {
1368 u32 data_crc;
1369
1370 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001371 be32_to_cpu(hdr->offset),
1372 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001373 cmd->pad_bytes);
1374
1375 if (checksum != data_crc) {
1376 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1377 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1378 " does not match computed 0x%08x\n",
1379 hdr->itt, hdr->offset, payload_length,
1380 hdr->datasn, checksum, data_crc);
1381 data_crc_failed = 1;
1382 } else {
1383 pr_debug("Got CRC32C DataDigest 0x%08x for"
1384 " %u bytes of Data Out\n", checksum,
1385 payload_length);
1386 }
1387 }
1388 /*
1389 * Increment post receive data and CRC values or perform
1390 * within-command recovery.
1391 */
1392 ret = iscsit_check_post_dataout(cmd, buf, data_crc_failed);
1393 if ((ret == DATAOUT_NORMAL) || (ret == DATAOUT_WITHIN_COMMAND_RECOVERY))
1394 return 0;
1395 else if (ret == DATAOUT_SEND_R2T) {
1396 iscsit_set_dataout_sequence_values(cmd);
Andy Grover8b1e1242012-04-03 15:51:12 -07001397 iscsit_build_r2ts_for_cmd(cmd, conn, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001398 } else if (ret == DATAOUT_SEND_TO_TRANSPORT) {
1399 /*
1400 * Handle extra special case for out of order
1401 * Unsolicited Data Out.
1402 */
1403 spin_lock_bh(&cmd->istate_lock);
1404 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1405 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1406 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1407 spin_unlock_bh(&cmd->istate_lock);
1408
1409 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001410 if (ooo_cmdsn)
1411 return 0;
1412 target_execute_cmd(&cmd->se_cmd);
1413 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001414 } else /* DATAOUT_CANNOT_RECOVER */
1415 return -1;
1416
1417 return 0;
1418}
1419
1420static int iscsit_handle_nop_out(
1421 struct iscsi_conn *conn,
1422 unsigned char *buf)
1423{
1424 unsigned char *ping_data = NULL;
1425 int cmdsn_ret, niov = 0, ret = 0, rx_got, rx_size;
1426 u32 checksum, data_crc, padding = 0, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001427 struct iscsi_cmd *cmd = NULL;
1428 struct kvec *iov = NULL;
1429 struct iscsi_nopout *hdr;
1430
1431 hdr = (struct iscsi_nopout *) buf;
1432 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001433
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001434 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001435 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1436 " not set, protocol error.\n");
1437 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1438 buf, conn);
1439 }
1440
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001441 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001442 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001443 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001444 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001445 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001446 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1447 buf, conn);
1448 }
1449
1450 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%09x,"
1451 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001452 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001453 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1454 payload_length);
1455 /*
1456 * This is not a response to a Unsolicited NopIN, which means
1457 * it can either be a NOPOUT ping request (with a valid ITT),
1458 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1459 * Either way, make sure we allocate an struct iscsi_cmd, as both
1460 * can contain ping data.
1461 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001462 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001463 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1464 if (!cmd)
1465 return iscsit_add_reject(
1466 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1467 1, buf, conn);
1468
1469 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1470 cmd->i_state = ISTATE_SEND_NOPIN;
1471 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1472 1 : 0);
1473 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1474 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001475 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1476 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001477 cmd->data_direction = DMA_NONE;
1478 }
1479
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001480 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001481 rx_size = payload_length;
1482 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1483 if (!ping_data) {
1484 pr_err("Unable to allocate memory for"
1485 " NOPOUT ping data.\n");
1486 ret = -1;
1487 goto out;
1488 }
1489
1490 iov = &cmd->iov_misc[0];
1491 iov[niov].iov_base = ping_data;
1492 iov[niov++].iov_len = payload_length;
1493
1494 padding = ((-payload_length) & 3);
1495 if (padding != 0) {
1496 pr_debug("Receiving %u additional bytes"
1497 " for padding.\n", padding);
1498 iov[niov].iov_base = &cmd->pad_bytes;
1499 iov[niov++].iov_len = padding;
1500 rx_size += padding;
1501 }
1502 if (conn->conn_ops->DataDigest) {
1503 iov[niov].iov_base = &checksum;
1504 iov[niov++].iov_len = ISCSI_CRC_LEN;
1505 rx_size += ISCSI_CRC_LEN;
1506 }
1507
1508 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1509 if (rx_got != rx_size) {
1510 ret = -1;
1511 goto out;
1512 }
1513
1514 if (conn->conn_ops->DataDigest) {
1515 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1516 ping_data, payload_length,
1517 padding, cmd->pad_bytes,
1518 (u8 *)&data_crc);
1519
1520 if (checksum != data_crc) {
1521 pr_err("Ping data CRC32C DataDigest"
1522 " 0x%08x does not match computed 0x%08x\n",
1523 checksum, data_crc);
1524 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1525 pr_err("Unable to recover from"
1526 " NOPOUT Ping DataCRC failure while in"
1527 " ERL=0.\n");
1528 ret = -1;
1529 goto out;
1530 } else {
1531 /*
1532 * Silently drop this PDU and let the
1533 * initiator plug the CmdSN gap.
1534 */
1535 pr_debug("Dropping NOPOUT"
1536 " Command CmdSN: 0x%08x due to"
1537 " DataCRC error.\n", hdr->cmdsn);
1538 ret = 0;
1539 goto out;
1540 }
1541 } else {
1542 pr_debug("Got CRC32C DataDigest"
1543 " 0x%08x for %u bytes of ping data.\n",
1544 checksum, payload_length);
1545 }
1546 }
1547
1548 ping_data[payload_length] = '\0';
1549 /*
1550 * Attach ping data to struct iscsi_cmd->buf_ptr.
1551 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001552 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001553 cmd->buf_ptr_size = payload_length;
1554
1555 pr_debug("Got %u bytes of NOPOUT ping"
1556 " data.\n", payload_length);
1557 pr_debug("Ping Data: \"%s\"\n", ping_data);
1558 }
1559
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001560 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001561 if (!cmd) {
1562 pr_err("Checking CmdSN for NOPOUT,"
1563 " but cmd is NULL!\n");
1564 return -1;
1565 }
1566 /*
1567 * Initiator is expecting a NopIN ping reply,
1568 */
1569 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001570 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001571 spin_unlock_bh(&conn->cmd_lock);
1572
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001573 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001574
1575 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1576 iscsit_add_cmd_to_response_queue(cmd, conn,
1577 cmd->i_state);
1578 return 0;
1579 }
1580
1581 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1582 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1583 ret = 0;
1584 goto ping_out;
1585 }
1586 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1587 return iscsit_add_reject_from_cmd(
1588 ISCSI_REASON_PROTOCOL_ERROR,
1589 1, 0, buf, cmd);
1590
1591 return 0;
1592 }
1593
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001594 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001595 /*
1596 * This was a response to a unsolicited NOPIN ping.
1597 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001598 cmd = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001599 if (!cmd)
1600 return -1;
1601
1602 iscsit_stop_nopin_response_timer(conn);
1603
1604 cmd->i_state = ISTATE_REMOVE;
1605 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1606 iscsit_start_nopin_timer(conn);
1607 } else {
1608 /*
1609 * Initiator is not expecting a NOPIN is response.
1610 * Just ignore for now.
1611 *
1612 * iSCSI v19-91 10.18
1613 * "A NOP-OUT may also be used to confirm a changed
1614 * ExpStatSN if another PDU will not be available
1615 * for a long time."
1616 */
1617 ret = 0;
1618 goto out;
1619 }
1620
1621 return 0;
1622out:
1623 if (cmd)
1624 iscsit_release_cmd(cmd);
1625ping_out:
1626 kfree(ping_data);
1627 return ret;
1628}
1629
1630static int iscsit_handle_task_mgt_cmd(
1631 struct iscsi_conn *conn,
1632 unsigned char *buf)
1633{
1634 struct iscsi_cmd *cmd;
1635 struct se_tmr_req *se_tmr;
1636 struct iscsi_tmr_req *tmr_req;
1637 struct iscsi_tm *hdr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001638 int out_of_order_cmdsn = 0;
1639 int ret;
1640 u8 function;
1641
1642 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001643 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1644 function = hdr->flags;
1645
1646 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1647 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1648 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1649 hdr->rtt, hdr->refcmdsn, conn->cid);
1650
1651 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1652 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001653 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001654 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001655 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001656 }
1657
1658 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1659 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1660 pr_err("Task Management Request TASK_REASSIGN not"
1661 " issued as immediate command, bad iSCSI Initiator"
1662 "implementation\n");
1663 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1664 buf, conn);
1665 }
1666 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001667 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1668 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001669
Andy Groverd28b11692012-04-03 15:51:22 -07001670 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001671 if (!cmd)
1672 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
Andy Groverd28b11692012-04-03 15:51:22 -07001673 1, buf, conn);
1674
1675 cmd->data_direction = DMA_NONE;
1676
1677 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1678 if (!cmd->tmr_req) {
1679 pr_err("Unable to allocate memory for"
1680 " Task Management command!\n");
1681 return iscsit_add_reject_from_cmd(
1682 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1683 1, 1, buf, cmd);
1684 }
1685
1686 /*
1687 * TASK_REASSIGN for ERL=2 / connection stays inside of
1688 * LIO-Target $FABRIC_MOD
1689 */
1690 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1691
1692 u8 tcm_function;
1693 int ret;
1694
1695 transport_init_se_cmd(&cmd->se_cmd,
1696 &lio_target_fabric_configfs->tf_ops,
1697 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001698 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001699
1700 switch (function) {
1701 case ISCSI_TM_FUNC_ABORT_TASK:
1702 tcm_function = TMR_ABORT_TASK;
1703 break;
1704 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1705 tcm_function = TMR_ABORT_TASK_SET;
1706 break;
1707 case ISCSI_TM_FUNC_CLEAR_ACA:
1708 tcm_function = TMR_CLEAR_ACA;
1709 break;
1710 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1711 tcm_function = TMR_CLEAR_TASK_SET;
1712 break;
1713 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1714 tcm_function = TMR_LUN_RESET;
1715 break;
1716 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1717 tcm_function = TMR_TARGET_WARM_RESET;
1718 break;
1719 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1720 tcm_function = TMR_TARGET_COLD_RESET;
1721 break;
1722 default:
1723 pr_err("Unknown iSCSI TMR Function:"
1724 " 0x%02x\n", function);
1725 return iscsit_add_reject_from_cmd(
1726 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1727 1, 1, buf, cmd);
1728 }
1729
1730 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1731 tcm_function, GFP_KERNEL);
1732 if (ret < 0)
1733 return iscsit_add_reject_from_cmd(
1734 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1735 1, 1, buf, cmd);
1736
1737 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1738 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001739
1740 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1741 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1742 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1743 cmd->init_task_tag = hdr->itt;
1744 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001745 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1746 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001747 se_tmr = cmd->se_cmd.se_tmr_req;
1748 tmr_req = cmd->tmr_req;
1749 /*
1750 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1751 */
1752 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001753 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1754 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001756 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1757 goto attach;
1758 }
1759 }
1760
1761 switch (function) {
1762 case ISCSI_TM_FUNC_ABORT_TASK:
1763 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001764 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001765 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001766 break;
1767 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1768 case ISCSI_TM_FUNC_CLEAR_ACA:
1769 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1770 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1771 break;
1772 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1773 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001774 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1775 goto attach;
1776 }
1777 break;
1778 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1779 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001780 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1781 goto attach;
1782 }
1783 break;
1784 case ISCSI_TM_FUNC_TASK_REASSIGN:
1785 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1786 /*
1787 * Perform sanity checks on the ExpDataSN only if the
1788 * TASK_REASSIGN was successful.
1789 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001790 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001791 break;
1792
1793 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
1794 return iscsit_add_reject_from_cmd(
1795 ISCSI_REASON_BOOKMARK_INVALID, 1, 1,
1796 buf, cmd);
1797 break;
1798 default:
1799 pr_err("Unknown TMR function: 0x%02x, protocol"
1800 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001801 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1802 goto attach;
1803 }
1804
1805 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1806 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1807 se_tmr->call_transport = 1;
1808attach:
1809 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001810 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001811 spin_unlock_bh(&conn->cmd_lock);
1812
1813 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1814 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1815 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1816 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001817 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001818 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001819 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001820 return iscsit_add_reject_from_cmd(
1821 ISCSI_REASON_PROTOCOL_ERROR,
1822 1, 0, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001823 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001824 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001825
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001826 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001827 return 0;
1828 /*
1829 * Found the referenced task, send to transport for processing.
1830 */
1831 if (se_tmr->call_transport)
1832 return transport_generic_handle_tmr(&cmd->se_cmd);
1833
1834 /*
1835 * Could not find the referenced LUN, task, or Task Management
1836 * command not authorized or supported. Change state and
1837 * let the tx_thread send the response.
1838 *
1839 * For connection recovery, this is also the default action for
1840 * TMR TASK_REASSIGN.
1841 */
1842 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1843 return 0;
1844}
1845
1846/* #warning FIXME: Support Text Command parameters besides SendTargets */
1847static int iscsit_handle_text_cmd(
1848 struct iscsi_conn *conn,
1849 unsigned char *buf)
1850{
1851 char *text_ptr, *text_in;
1852 int cmdsn_ret, niov = 0, rx_got, rx_size;
1853 u32 checksum = 0, data_crc = 0, payload_length;
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001854 u32 padding = 0, pad_bytes = 0, text_length = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001855 struct iscsi_cmd *cmd;
1856 struct kvec iov[3];
1857 struct iscsi_text *hdr;
1858
1859 hdr = (struct iscsi_text *) buf;
1860 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001861
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001862 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001863 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001864 "greater than MaxXmitDataSegmentLength %u.\n",
1865 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001866 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1867 buf, conn);
1868 }
1869
1870 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1871 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1872 hdr->exp_statsn, payload_length);
1873
1874 rx_size = text_length = payload_length;
1875 if (text_length) {
1876 text_in = kzalloc(text_length, GFP_KERNEL);
1877 if (!text_in) {
1878 pr_err("Unable to allocate memory for"
1879 " incoming text parameters\n");
1880 return -1;
1881 }
1882
1883 memset(iov, 0, 3 * sizeof(struct kvec));
1884 iov[niov].iov_base = text_in;
1885 iov[niov++].iov_len = text_length;
1886
1887 padding = ((-payload_length) & 3);
1888 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001889 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001890 iov[niov++].iov_len = padding;
1891 rx_size += padding;
1892 pr_debug("Receiving %u additional bytes"
1893 " for padding.\n", padding);
1894 }
1895 if (conn->conn_ops->DataDigest) {
1896 iov[niov].iov_base = &checksum;
1897 iov[niov++].iov_len = ISCSI_CRC_LEN;
1898 rx_size += ISCSI_CRC_LEN;
1899 }
1900
1901 rx_got = rx_data(conn, &iov[0], niov, rx_size);
1902 if (rx_got != rx_size) {
1903 kfree(text_in);
1904 return -1;
1905 }
1906
1907 if (conn->conn_ops->DataDigest) {
1908 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1909 text_in, text_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001910 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001911 (u8 *)&data_crc);
1912
1913 if (checksum != data_crc) {
1914 pr_err("Text data CRC32C DataDigest"
1915 " 0x%08x does not match computed"
1916 " 0x%08x\n", checksum, data_crc);
1917 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1918 pr_err("Unable to recover from"
1919 " Text Data digest failure while in"
1920 " ERL=0.\n");
1921 kfree(text_in);
1922 return -1;
1923 } else {
1924 /*
1925 * Silently drop this PDU and let the
1926 * initiator plug the CmdSN gap.
1927 */
1928 pr_debug("Dropping Text"
1929 " Command CmdSN: 0x%08x due to"
1930 " DataCRC error.\n", hdr->cmdsn);
1931 kfree(text_in);
1932 return 0;
1933 }
1934 } else {
1935 pr_debug("Got CRC32C DataDigest"
1936 " 0x%08x for %u bytes of text data.\n",
1937 checksum, text_length);
1938 }
1939 }
1940 text_in[text_length - 1] = '\0';
1941 pr_debug("Successfully read %d bytes of text"
1942 " data.\n", text_length);
1943
1944 if (strncmp("SendTargets", text_in, 11) != 0) {
1945 pr_err("Received Text Data that is not"
1946 " SendTargets, cannot continue.\n");
1947 kfree(text_in);
1948 return -1;
1949 }
1950 text_ptr = strchr(text_in, '=');
1951 if (!text_ptr) {
1952 pr_err("No \"=\" separator found in Text Data,"
1953 " cannot continue.\n");
1954 kfree(text_in);
1955 return -1;
1956 }
1957 if (strncmp("=All", text_ptr, 4) != 0) {
1958 pr_err("Unable to locate All value for"
1959 " SendTargets key, cannot continue.\n");
1960 kfree(text_in);
1961 return -1;
1962 }
1963/*#warning Support SendTargets=(iSCSI Target Name/Nothing) values. */
1964 kfree(text_in);
1965 }
1966
1967 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1968 if (!cmd)
1969 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1970 1, buf, conn);
1971
1972 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1973 cmd->i_state = ISTATE_SEND_TEXTRSP;
1974 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1975 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1976 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001977 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1978 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001979 cmd->data_direction = DMA_NONE;
1980
1981 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001982 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001983 spin_unlock_bh(&conn->cmd_lock);
1984
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001985 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001986
1987 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1988 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1989 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1990 return iscsit_add_reject_from_cmd(
1991 ISCSI_REASON_PROTOCOL_ERROR,
1992 1, 0, buf, cmd);
1993
1994 return 0;
1995 }
1996
1997 return iscsit_execute_cmd(cmd, 0);
1998}
1999
2000int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2001{
2002 struct iscsi_conn *conn_p;
2003 struct iscsi_session *sess = conn->sess;
2004
2005 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2006 " for SID: %u.\n", conn->cid, conn->sess->sid);
2007
2008 atomic_set(&sess->session_logout, 1);
2009 atomic_set(&conn->conn_logout_remove, 1);
2010 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2011
2012 iscsit_inc_conn_usage_count(conn);
2013 iscsit_inc_session_usage_count(sess);
2014
2015 spin_lock_bh(&sess->conn_lock);
2016 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2017 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2018 continue;
2019
2020 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2021 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2022 }
2023 spin_unlock_bh(&sess->conn_lock);
2024
2025 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2026
2027 return 0;
2028}
2029
2030int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2031{
2032 struct iscsi_conn *l_conn;
2033 struct iscsi_session *sess = conn->sess;
2034
2035 pr_debug("Received logout request CLOSECONNECTION for CID:"
2036 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2037
2038 /*
2039 * A Logout Request with a CLOSECONNECTION reason code for a CID
2040 * can arrive on a connection with a differing CID.
2041 */
2042 if (conn->cid == cmd->logout_cid) {
2043 spin_lock_bh(&conn->state_lock);
2044 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2045 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2046
2047 atomic_set(&conn->conn_logout_remove, 1);
2048 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2049 iscsit_inc_conn_usage_count(conn);
2050
2051 spin_unlock_bh(&conn->state_lock);
2052 } else {
2053 /*
2054 * Handle all different cid CLOSECONNECTION requests in
2055 * iscsit_logout_post_handler_diffcid() as to give enough
2056 * time for any non immediate command's CmdSN to be
2057 * acknowledged on the connection in question.
2058 *
2059 * Here we simply make sure the CID is still around.
2060 */
2061 l_conn = iscsit_get_conn_from_cid(sess,
2062 cmd->logout_cid);
2063 if (!l_conn) {
2064 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2065 iscsit_add_cmd_to_response_queue(cmd, conn,
2066 cmd->i_state);
2067 return 0;
2068 }
2069
2070 iscsit_dec_conn_usage_count(l_conn);
2071 }
2072
2073 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2074
2075 return 0;
2076}
2077
2078int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2079{
2080 struct iscsi_session *sess = conn->sess;
2081
2082 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2083 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2084
2085 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2086 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2087 " while ERL!=2.\n");
2088 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2089 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2090 return 0;
2091 }
2092
2093 if (conn->cid == cmd->logout_cid) {
2094 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2095 " with CID: %hu on CID: %hu, implementation error.\n",
2096 cmd->logout_cid, conn->cid);
2097 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2098 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2099 return 0;
2100 }
2101
2102 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2103
2104 return 0;
2105}
2106
2107static int iscsit_handle_logout_cmd(
2108 struct iscsi_conn *conn,
2109 unsigned char *buf)
2110{
2111 int cmdsn_ret, logout_remove = 0;
2112 u8 reason_code = 0;
2113 struct iscsi_cmd *cmd;
2114 struct iscsi_logout *hdr;
2115 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2116
2117 hdr = (struct iscsi_logout *) buf;
2118 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002119
2120 if (tiqn) {
2121 spin_lock(&tiqn->logout_stats.lock);
2122 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2123 tiqn->logout_stats.normal_logouts++;
2124 else
2125 tiqn->logout_stats.abnormal_logouts++;
2126 spin_unlock(&tiqn->logout_stats.lock);
2127 }
2128
2129 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2130 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2131 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2132 hdr->cid, conn->cid);
2133
2134 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2135 pr_err("Received logout request on connection that"
2136 " is not in logged in state, ignoring request.\n");
2137 return 0;
2138 }
2139
2140 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
2141 if (!cmd)
2142 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
2143 buf, conn);
2144
2145 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2146 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2147 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2148 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2149 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002150 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2151 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2152 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002153 cmd->logout_reason = reason_code;
2154 cmd->data_direction = DMA_NONE;
2155
2156 /*
2157 * We need to sleep in these cases (by returning 1) until the Logout
2158 * Response gets sent in the tx thread.
2159 */
2160 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2161 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002162 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002163 logout_remove = 1;
2164
2165 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002166 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002167 spin_unlock_bh(&conn->cmd_lock);
2168
2169 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002170 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002171
2172 /*
2173 * Immediate commands are executed, well, immediately.
2174 * Non-Immediate Logout Commands are executed in CmdSN order.
2175 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002176 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002177 int ret = iscsit_execute_cmd(cmd, 0);
2178
2179 if (ret < 0)
2180 return ret;
2181 } else {
2182 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2183 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2184 logout_remove = 0;
2185 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2186 return iscsit_add_reject_from_cmd(
2187 ISCSI_REASON_PROTOCOL_ERROR,
2188 1, 0, buf, cmd);
2189 }
2190 }
2191
2192 return logout_remove;
2193}
2194
2195static int iscsit_handle_snack(
2196 struct iscsi_conn *conn,
2197 unsigned char *buf)
2198{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002199 struct iscsi_snack *hdr;
2200
2201 hdr = (struct iscsi_snack *) buf;
2202 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002203
2204 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2205 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2206 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2207 hdr->begrun, hdr->runlength, conn->cid);
2208
2209 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2210 pr_err("Initiator sent SNACK request while in"
2211 " ErrorRecoveryLevel=0.\n");
2212 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2213 buf, conn);
2214 }
2215 /*
2216 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2217 * call from inside iscsi_send_recovery_datain_or_r2t().
2218 */
2219 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2220 case 0:
2221 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002222 hdr->itt,
2223 be32_to_cpu(hdr->ttt),
2224 be32_to_cpu(hdr->begrun),
2225 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002226 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002227 return iscsit_handle_status_snack(conn, hdr->itt,
2228 be32_to_cpu(hdr->ttt),
2229 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002230 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002231 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2232 be32_to_cpu(hdr->begrun),
2233 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002234 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2235 /* FIXME: Support R-Data SNACK */
2236 pr_err("R-Data SNACK Not Supported.\n");
2237 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2238 buf, conn);
2239 default:
2240 pr_err("Unknown SNACK type 0x%02x, protocol"
2241 " error.\n", hdr->flags & 0x0f);
2242 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2243 buf, conn);
2244 }
2245
2246 return 0;
2247}
2248
2249static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2250{
2251 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2252 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2253 wait_for_completion_interruptible_timeout(
2254 &conn->rx_half_close_comp,
2255 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2256 }
2257}
2258
2259static int iscsit_handle_immediate_data(
2260 struct iscsi_cmd *cmd,
2261 unsigned char *buf,
2262 u32 length)
2263{
2264 int iov_ret, rx_got = 0, rx_size = 0;
2265 u32 checksum, iov_count = 0, padding = 0;
2266 struct iscsi_conn *conn = cmd->conn;
2267 struct kvec *iov;
2268
2269 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2270 if (iov_ret < 0)
2271 return IMMEDIATE_DATA_CANNOT_RECOVER;
2272
2273 rx_size = length;
2274 iov_count = iov_ret;
2275 iov = &cmd->iov_data[0];
2276
2277 padding = ((-length) & 3);
2278 if (padding != 0) {
2279 iov[iov_count].iov_base = cmd->pad_bytes;
2280 iov[iov_count++].iov_len = padding;
2281 rx_size += padding;
2282 }
2283
2284 if (conn->conn_ops->DataDigest) {
2285 iov[iov_count].iov_base = &checksum;
2286 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2287 rx_size += ISCSI_CRC_LEN;
2288 }
2289
2290 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2291
2292 iscsit_unmap_iovec(cmd);
2293
2294 if (rx_got != rx_size) {
2295 iscsit_rx_thread_wait_for_tcp(conn);
2296 return IMMEDIATE_DATA_CANNOT_RECOVER;
2297 }
2298
2299 if (conn->conn_ops->DataDigest) {
2300 u32 data_crc;
2301
2302 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2303 cmd->write_data_done, length, padding,
2304 cmd->pad_bytes);
2305
2306 if (checksum != data_crc) {
2307 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2308 " does not match computed 0x%08x\n", checksum,
2309 data_crc);
2310
2311 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2312 pr_err("Unable to recover from"
2313 " Immediate Data digest failure while"
2314 " in ERL=0.\n");
2315 iscsit_add_reject_from_cmd(
2316 ISCSI_REASON_DATA_DIGEST_ERROR,
2317 1, 0, buf, cmd);
2318 return IMMEDIATE_DATA_CANNOT_RECOVER;
2319 } else {
2320 iscsit_add_reject_from_cmd(
2321 ISCSI_REASON_DATA_DIGEST_ERROR,
2322 0, 0, buf, cmd);
2323 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2324 }
2325 } else {
2326 pr_debug("Got CRC32C DataDigest 0x%08x for"
2327 " %u bytes of Immediate Data\n", checksum,
2328 length);
2329 }
2330 }
2331
2332 cmd->write_data_done += length;
2333
Andy Groverebf1d952012-04-03 15:51:24 -07002334 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002335 spin_lock_bh(&cmd->istate_lock);
2336 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2337 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2338 spin_unlock_bh(&cmd->istate_lock);
2339 }
2340
2341 return IMMEDIATE_DATA_NORMAL_OPERATION;
2342}
2343
2344/*
2345 * Called with sess->conn_lock held.
2346 */
2347/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2348 with active network interface */
2349static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2350{
2351 struct iscsi_cmd *cmd;
2352 struct iscsi_conn *conn_p;
2353
2354 /*
2355 * Only send a Asynchronous Message on connections whos network
2356 * interface is still functional.
2357 */
2358 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2359 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2360 iscsit_inc_conn_usage_count(conn_p);
2361 break;
2362 }
2363 }
2364
2365 if (!conn_p)
2366 return;
2367
Wei Yongjun3c989d72012-11-23 12:07:39 +08002368 cmd = iscsit_allocate_cmd(conn_p, GFP_ATOMIC);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002369 if (!cmd) {
2370 iscsit_dec_conn_usage_count(conn_p);
2371 return;
2372 }
2373
2374 cmd->logout_cid = conn->cid;
2375 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2376 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2377
2378 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002379 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002380 spin_unlock_bh(&conn_p->cmd_lock);
2381
2382 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2383 iscsit_dec_conn_usage_count(conn_p);
2384}
2385
2386static int iscsit_send_conn_drop_async_message(
2387 struct iscsi_cmd *cmd,
2388 struct iscsi_conn *conn)
2389{
2390 struct iscsi_async *hdr;
2391
2392 cmd->tx_size = ISCSI_HDR_LEN;
2393 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2394
2395 hdr = (struct iscsi_async *) cmd->pdu;
2396 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2397 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002398 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002399 cmd->targ_xfer_tag = 0xFFFFFFFF;
2400 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2401 cmd->stat_sn = conn->stat_sn++;
2402 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2403 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2404 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2405 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2406 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2407 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2408 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2409
2410 if (conn->conn_ops->HeaderDigest) {
2411 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2412
2413 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2414 (unsigned char *)hdr, ISCSI_HDR_LEN,
2415 0, NULL, (u8 *)header_digest);
2416
2417 cmd->tx_size += ISCSI_CRC_LEN;
2418 pr_debug("Attaching CRC32C HeaderDigest to"
2419 " Async Message 0x%08x\n", *header_digest);
2420 }
2421
2422 cmd->iov_misc[0].iov_base = cmd->pdu;
2423 cmd->iov_misc[0].iov_len = cmd->tx_size;
2424 cmd->iov_misc_count = 1;
2425
2426 pr_debug("Sending Connection Dropped Async Message StatSN:"
2427 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2428 cmd->logout_cid, conn->cid);
2429 return 0;
2430}
2431
Andy Grover6f3c0e62012-04-03 15:51:09 -07002432static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2433{
2434 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2435 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2436 wait_for_completion_interruptible_timeout(
2437 &conn->tx_half_close_comp,
2438 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2439 }
2440}
2441
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002442static int iscsit_send_data_in(
2443 struct iscsi_cmd *cmd,
Andy Grover6f3c0e62012-04-03 15:51:09 -07002444 struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002445{
2446 int iov_ret = 0, set_statsn = 0;
2447 u32 iov_count = 0, tx_size = 0;
2448 struct iscsi_datain datain;
2449 struct iscsi_datain_req *dr;
2450 struct iscsi_data_rsp *hdr;
2451 struct kvec *iov;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002452 int eodr = 0;
2453 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002454
2455 memset(&datain, 0, sizeof(struct iscsi_datain));
2456 dr = iscsit_get_datain_values(cmd, &datain);
2457 if (!dr) {
2458 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2459 cmd->init_task_tag);
2460 return -1;
2461 }
2462
2463 /*
2464 * Be paranoid and double check the logic for now.
2465 */
Andy Groverebf1d952012-04-03 15:51:24 -07002466 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002467 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2468 " datain.length: %u exceeds cmd->data_length: %u\n",
2469 cmd->init_task_tag, datain.offset, datain.length,
Andy Groverebf1d952012-04-03 15:51:24 -07002470 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002471 return -1;
2472 }
2473
2474 spin_lock_bh(&conn->sess->session_stats_lock);
2475 conn->sess->tx_data_octets += datain.length;
2476 if (conn->sess->se_sess->se_node_acl) {
2477 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2478 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2479 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2480 }
2481 spin_unlock_bh(&conn->sess->session_stats_lock);
2482 /*
2483 * Special case for successfully execution w/ both DATAIN
2484 * and Sense Data.
2485 */
2486 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2487 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2488 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2489 else {
2490 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2491 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2492 iscsit_increment_maxcmdsn(cmd, conn->sess);
2493 cmd->stat_sn = conn->stat_sn++;
2494 set_statsn = 1;
2495 } else if (dr->dr_complete ==
2496 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2497 set_statsn = 1;
2498 }
2499
2500 hdr = (struct iscsi_data_rsp *) cmd->pdu;
2501 memset(hdr, 0, ISCSI_HDR_LEN);
2502 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2503 hdr->flags = datain.flags;
2504 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2505 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2506 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002507 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002508 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2509 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002510 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002511 }
2512 }
2513 hton24(hdr->dlength, datain.length);
2514 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2515 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2516 (struct scsi_lun *)&hdr->lun);
2517 else
2518 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2519
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002520 hdr->itt = cmd->init_task_tag;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002521
2522 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2523 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2524 else
2525 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2526 if (set_statsn)
2527 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2528 else
2529 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2530
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002531 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2532 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2533 hdr->datasn = cpu_to_be32(datain.data_sn);
2534 hdr->offset = cpu_to_be32(datain.offset);
2535
2536 iov = &cmd->iov_data[0];
2537 iov[iov_count].iov_base = cmd->pdu;
2538 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2539 tx_size += ISCSI_HDR_LEN;
2540
2541 if (conn->conn_ops->HeaderDigest) {
2542 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2543
2544 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2545 (unsigned char *)hdr, ISCSI_HDR_LEN,
2546 0, NULL, (u8 *)header_digest);
2547
2548 iov[0].iov_len += ISCSI_CRC_LEN;
2549 tx_size += ISCSI_CRC_LEN;
2550
2551 pr_debug("Attaching CRC32 HeaderDigest"
2552 " for DataIN PDU 0x%08x\n", *header_digest);
2553 }
2554
2555 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], datain.offset, datain.length);
2556 if (iov_ret < 0)
2557 return -1;
2558
2559 iov_count += iov_ret;
2560 tx_size += datain.length;
2561
2562 cmd->padding = ((-datain.length) & 3);
2563 if (cmd->padding) {
2564 iov[iov_count].iov_base = cmd->pad_bytes;
2565 iov[iov_count++].iov_len = cmd->padding;
2566 tx_size += cmd->padding;
2567
2568 pr_debug("Attaching %u padding bytes\n",
2569 cmd->padding);
2570 }
2571 if (conn->conn_ops->DataDigest) {
2572 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2573 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2574
2575 iov[iov_count].iov_base = &cmd->data_crc;
2576 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2577 tx_size += ISCSI_CRC_LEN;
2578
2579 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2580 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2581 }
2582
2583 cmd->iov_data_count = iov_count;
2584 cmd->tx_size = tx_size;
2585
2586 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2587 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2588 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2589 ntohl(hdr->offset), datain.length, conn->cid);
2590
Andy Grover6f3c0e62012-04-03 15:51:09 -07002591 /* sendpage is preferred but can't insert markers */
2592 if (!conn->conn_ops->IFMarker)
2593 ret = iscsit_fe_sendpage_sg(cmd, conn);
2594 else
2595 ret = iscsit_send_tx_data(cmd, conn, 0);
2596
2597 iscsit_unmap_iovec(cmd);
2598
2599 if (ret < 0) {
2600 iscsit_tx_thread_wait_for_tcp(conn);
2601 return ret;
2602 }
2603
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002604 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002605 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002606 2 : 1;
2607 iscsit_free_datain_req(cmd, dr);
2608 }
2609
Andy Grover6f3c0e62012-04-03 15:51:09 -07002610 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002611}
2612
2613static int iscsit_send_logout_response(
2614 struct iscsi_cmd *cmd,
2615 struct iscsi_conn *conn)
2616{
2617 int niov = 0, tx_size;
2618 struct iscsi_conn *logout_conn = NULL;
2619 struct iscsi_conn_recovery *cr = NULL;
2620 struct iscsi_session *sess = conn->sess;
2621 struct kvec *iov;
2622 struct iscsi_logout_rsp *hdr;
2623 /*
2624 * The actual shutting down of Sessions and/or Connections
2625 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2626 * is done in scsi_logout_post_handler().
2627 */
2628 switch (cmd->logout_reason) {
2629 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2630 pr_debug("iSCSI session logout successful, setting"
2631 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2632 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2633 break;
2634 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2635 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2636 break;
2637 /*
2638 * For CLOSECONNECTION logout requests carrying
2639 * a matching logout CID -> local CID, the reference
2640 * for the local CID will have been incremented in
2641 * iscsi_logout_closeconnection().
2642 *
2643 * For CLOSECONNECTION logout requests carrying
2644 * a different CID than the connection it arrived
2645 * on, the connection responding to cmd->logout_cid
2646 * is stopped in iscsit_logout_post_handler_diffcid().
2647 */
2648
2649 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2650 " successful.\n", cmd->logout_cid, conn->cid);
2651 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2652 break;
2653 case ISCSI_LOGOUT_REASON_RECOVERY:
2654 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2655 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2656 break;
2657 /*
2658 * If the connection is still active from our point of view
2659 * force connection recovery to occur.
2660 */
2661 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2662 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002663 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002664 iscsit_connection_reinstatement_rcfr(logout_conn);
2665 iscsit_dec_conn_usage_count(logout_conn);
2666 }
2667
2668 cr = iscsit_get_inactive_connection_recovery_entry(
2669 conn->sess, cmd->logout_cid);
2670 if (!cr) {
2671 pr_err("Unable to locate CID: %hu for"
2672 " REMOVECONNFORRECOVERY Logout Request.\n",
2673 cmd->logout_cid);
2674 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2675 break;
2676 }
2677
2678 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2679
2680 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2681 " for recovery for CID: %hu on CID: %hu successful.\n",
2682 cmd->logout_cid, conn->cid);
2683 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2684 break;
2685 default:
2686 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2687 cmd->logout_reason);
2688 return -1;
2689 }
2690
2691 tx_size = ISCSI_HDR_LEN;
2692 hdr = (struct iscsi_logout_rsp *)cmd->pdu;
2693 memset(hdr, 0, ISCSI_HDR_LEN);
2694 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2695 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2696 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002697 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002698 cmd->stat_sn = conn->stat_sn++;
2699 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2700
2701 iscsit_increment_maxcmdsn(cmd, conn->sess);
2702 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2703 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2704
2705 iov = &cmd->iov_misc[0];
2706 iov[niov].iov_base = cmd->pdu;
2707 iov[niov++].iov_len = ISCSI_HDR_LEN;
2708
2709 if (conn->conn_ops->HeaderDigest) {
2710 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2711
2712 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2713 (unsigned char *)hdr, ISCSI_HDR_LEN,
2714 0, NULL, (u8 *)header_digest);
2715
2716 iov[0].iov_len += ISCSI_CRC_LEN;
2717 tx_size += ISCSI_CRC_LEN;
2718 pr_debug("Attaching CRC32C HeaderDigest to"
2719 " Logout Response 0x%08x\n", *header_digest);
2720 }
2721 cmd->iov_misc_count = niov;
2722 cmd->tx_size = tx_size;
2723
2724 pr_debug("Sending Logout Response ITT: 0x%08x StatSN:"
2725 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2726 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2727 cmd->logout_cid, conn->cid);
2728
2729 return 0;
2730}
2731
2732/*
2733 * Unsolicited NOPIN, either requesting a response or not.
2734 */
2735static int iscsit_send_unsolicited_nopin(
2736 struct iscsi_cmd *cmd,
2737 struct iscsi_conn *conn,
2738 int want_response)
2739{
2740 int tx_size = ISCSI_HDR_LEN;
2741 struct iscsi_nopin *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002742 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002743
2744 hdr = (struct iscsi_nopin *) cmd->pdu;
2745 memset(hdr, 0, ISCSI_HDR_LEN);
2746 hdr->opcode = ISCSI_OP_NOOP_IN;
2747 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002748 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002749 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2750 cmd->stat_sn = conn->stat_sn;
2751 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2752 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2753 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2754
2755 if (conn->conn_ops->HeaderDigest) {
2756 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2757
2758 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2759 (unsigned char *)hdr, ISCSI_HDR_LEN,
2760 0, NULL, (u8 *)header_digest);
2761
2762 tx_size += ISCSI_CRC_LEN;
2763 pr_debug("Attaching CRC32C HeaderDigest to"
2764 " NopIN 0x%08x\n", *header_digest);
2765 }
2766
2767 cmd->iov_misc[0].iov_base = cmd->pdu;
2768 cmd->iov_misc[0].iov_len = tx_size;
2769 cmd->iov_misc_count = 1;
2770 cmd->tx_size = tx_size;
2771
2772 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2773 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2774
Andy Grover6f3c0e62012-04-03 15:51:09 -07002775 ret = iscsit_send_tx_data(cmd, conn, 1);
2776 if (ret < 0) {
2777 iscsit_tx_thread_wait_for_tcp(conn);
2778 return ret;
2779 }
2780
2781 spin_lock_bh(&cmd->istate_lock);
2782 cmd->i_state = want_response ?
2783 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2784 spin_unlock_bh(&cmd->istate_lock);
2785
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002786 return 0;
2787}
2788
2789static int iscsit_send_nopin_response(
2790 struct iscsi_cmd *cmd,
2791 struct iscsi_conn *conn)
2792{
2793 int niov = 0, tx_size;
2794 u32 padding = 0;
2795 struct kvec *iov;
2796 struct iscsi_nopin *hdr;
2797
2798 tx_size = ISCSI_HDR_LEN;
2799 hdr = (struct iscsi_nopin *) cmd->pdu;
2800 memset(hdr, 0, ISCSI_HDR_LEN);
2801 hdr->opcode = ISCSI_OP_NOOP_IN;
2802 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2803 hton24(hdr->dlength, cmd->buf_ptr_size);
2804 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002805 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002806 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2807 cmd->stat_sn = conn->stat_sn++;
2808 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2809
2810 iscsit_increment_maxcmdsn(cmd, conn->sess);
2811 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2812 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2813
2814 iov = &cmd->iov_misc[0];
2815 iov[niov].iov_base = cmd->pdu;
2816 iov[niov++].iov_len = ISCSI_HDR_LEN;
2817
2818 if (conn->conn_ops->HeaderDigest) {
2819 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2820
2821 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2822 (unsigned char *)hdr, ISCSI_HDR_LEN,
2823 0, NULL, (u8 *)header_digest);
2824
2825 iov[0].iov_len += ISCSI_CRC_LEN;
2826 tx_size += ISCSI_CRC_LEN;
2827 pr_debug("Attaching CRC32C HeaderDigest"
2828 " to NopIn 0x%08x\n", *header_digest);
2829 }
2830
2831 /*
2832 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2833 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2834 */
2835 if (cmd->buf_ptr_size) {
2836 iov[niov].iov_base = cmd->buf_ptr;
2837 iov[niov++].iov_len = cmd->buf_ptr_size;
2838 tx_size += cmd->buf_ptr_size;
2839
2840 pr_debug("Echoing back %u bytes of ping"
2841 " data.\n", cmd->buf_ptr_size);
2842
2843 padding = ((-cmd->buf_ptr_size) & 3);
2844 if (padding != 0) {
2845 iov[niov].iov_base = &cmd->pad_bytes;
2846 iov[niov++].iov_len = padding;
2847 tx_size += padding;
2848 pr_debug("Attaching %u additional"
2849 " padding bytes.\n", padding);
2850 }
2851 if (conn->conn_ops->DataDigest) {
2852 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2853 cmd->buf_ptr, cmd->buf_ptr_size,
2854 padding, (u8 *)&cmd->pad_bytes,
2855 (u8 *)&cmd->data_crc);
2856
2857 iov[niov].iov_base = &cmd->data_crc;
2858 iov[niov++].iov_len = ISCSI_CRC_LEN;
2859 tx_size += ISCSI_CRC_LEN;
2860 pr_debug("Attached DataDigest for %u"
2861 " bytes of ping data, CRC 0x%08x\n",
2862 cmd->buf_ptr_size, cmd->data_crc);
2863 }
2864 }
2865
2866 cmd->iov_misc_count = niov;
2867 cmd->tx_size = tx_size;
2868
2869 pr_debug("Sending NOPIN Response ITT: 0x%08x, TTT:"
2870 " 0x%08x, StatSN: 0x%08x, Length %u\n", cmd->init_task_tag,
2871 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2872
2873 return 0;
2874}
2875
Andy Grover6f3c0e62012-04-03 15:51:09 -07002876static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002877 struct iscsi_cmd *cmd,
2878 struct iscsi_conn *conn)
2879{
2880 int tx_size = 0;
2881 struct iscsi_r2t *r2t;
2882 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002883 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002884
2885 r2t = iscsit_get_r2t_from_list(cmd);
2886 if (!r2t)
2887 return -1;
2888
2889 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
2890 memset(hdr, 0, ISCSI_HDR_LEN);
2891 hdr->opcode = ISCSI_OP_R2T;
2892 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2893 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2894 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002895 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002896 spin_lock_bh(&conn->sess->ttt_lock);
2897 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2898 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
2899 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2900 spin_unlock_bh(&conn->sess->ttt_lock);
2901 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
2902 hdr->statsn = cpu_to_be32(conn->stat_sn);
2903 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2904 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2905 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
2906 hdr->data_offset = cpu_to_be32(r2t->offset);
2907 hdr->data_length = cpu_to_be32(r2t->xfer_len);
2908
2909 cmd->iov_misc[0].iov_base = cmd->pdu;
2910 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
2911 tx_size += ISCSI_HDR_LEN;
2912
2913 if (conn->conn_ops->HeaderDigest) {
2914 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2915
2916 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2917 (unsigned char *)hdr, ISCSI_HDR_LEN,
2918 0, NULL, (u8 *)header_digest);
2919
2920 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
2921 tx_size += ISCSI_CRC_LEN;
2922 pr_debug("Attaching CRC32 HeaderDigest for R2T"
2923 " PDU 0x%08x\n", *header_digest);
2924 }
2925
2926 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
2927 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
2928 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
2929 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
2930 r2t->offset, r2t->xfer_len, conn->cid);
2931
2932 cmd->iov_misc_count = 1;
2933 cmd->tx_size = tx_size;
2934
2935 spin_lock_bh(&cmd->r2t_lock);
2936 r2t->sent_r2t = 1;
2937 spin_unlock_bh(&cmd->r2t_lock);
2938
Andy Grover6f3c0e62012-04-03 15:51:09 -07002939 ret = iscsit_send_tx_data(cmd, conn, 1);
2940 if (ret < 0) {
2941 iscsit_tx_thread_wait_for_tcp(conn);
2942 return ret;
2943 }
2944
2945 spin_lock_bh(&cmd->dataout_timeout_lock);
2946 iscsit_start_dataout_timer(cmd, conn);
2947 spin_unlock_bh(&cmd->dataout_timeout_lock);
2948
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002949 return 0;
2950}
2951
2952/*
Andy Grover8b1e1242012-04-03 15:51:12 -07002953 * @recovery: If called from iscsi_task_reassign_complete_write() for
2954 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002955 */
2956int iscsit_build_r2ts_for_cmd(
2957 struct iscsi_cmd *cmd,
2958 struct iscsi_conn *conn,
Andy Grover8b1e1242012-04-03 15:51:12 -07002959 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002960{
2961 int first_r2t = 1;
2962 u32 offset = 0, xfer_len = 0;
2963
2964 spin_lock_bh(&cmd->r2t_lock);
2965 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
2966 spin_unlock_bh(&cmd->r2t_lock);
2967 return 0;
2968 }
2969
Andy Grover8b1e1242012-04-03 15:51:12 -07002970 if (conn->sess->sess_ops->DataSequenceInOrder &&
2971 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07002972 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002973
2974 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
2975 if (conn->sess->sess_ops->DataSequenceInOrder) {
2976 offset = cmd->r2t_offset;
2977
Andy Grover8b1e1242012-04-03 15:51:12 -07002978 if (first_r2t && recovery) {
2979 int new_data_end = offset +
2980 conn->sess->sess_ops->MaxBurstLength -
2981 cmd->next_burst_len;
2982
Andy Groverebf1d952012-04-03 15:51:24 -07002983 if (new_data_end > cmd->se_cmd.data_length)
2984 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07002985 else
2986 xfer_len =
2987 conn->sess->sess_ops->MaxBurstLength -
2988 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002989 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07002990 int new_data_end = offset +
2991 conn->sess->sess_ops->MaxBurstLength;
2992
Andy Groverebf1d952012-04-03 15:51:24 -07002993 if (new_data_end > cmd->se_cmd.data_length)
2994 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07002995 else
2996 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002997 }
2998 cmd->r2t_offset += xfer_len;
2999
Andy Groverebf1d952012-04-03 15:51:24 -07003000 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003001 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3002 } else {
3003 struct iscsi_seq *seq;
3004
3005 seq = iscsit_get_seq_holder_for_r2t(cmd);
3006 if (!seq) {
3007 spin_unlock_bh(&cmd->r2t_lock);
3008 return -1;
3009 }
3010
3011 offset = seq->offset;
3012 xfer_len = seq->xfer_len;
3013
3014 if (cmd->seq_send_order == cmd->seq_count)
3015 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3016 }
3017 cmd->outstanding_r2ts++;
3018 first_r2t = 0;
3019
3020 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3021 spin_unlock_bh(&cmd->r2t_lock);
3022 return -1;
3023 }
3024
3025 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3026 break;
3027 }
3028 spin_unlock_bh(&cmd->r2t_lock);
3029
3030 return 0;
3031}
3032
3033static int iscsit_send_status(
3034 struct iscsi_cmd *cmd,
3035 struct iscsi_conn *conn)
3036{
3037 u8 iov_count = 0, recovery;
3038 u32 padding = 0, tx_size = 0;
3039 struct iscsi_scsi_rsp *hdr;
3040 struct kvec *iov;
3041
3042 recovery = (cmd->i_state != ISTATE_SEND_STATUS);
3043 if (!recovery)
3044 cmd->stat_sn = conn->stat_sn++;
3045
3046 spin_lock_bh(&conn->sess->session_stats_lock);
3047 conn->sess->rsp_pdus++;
3048 spin_unlock_bh(&conn->sess->session_stats_lock);
3049
3050 hdr = (struct iscsi_scsi_rsp *) cmd->pdu;
3051 memset(hdr, 0, ISCSI_HDR_LEN);
3052 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3053 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3054 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3055 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003056 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003057 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3058 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003059 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003060 }
3061 hdr->response = cmd->iscsi_response;
3062 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003063 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003064 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3065
3066 iscsit_increment_maxcmdsn(cmd, conn->sess);
3067 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3068 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3069
3070 iov = &cmd->iov_misc[0];
3071 iov[iov_count].iov_base = cmd->pdu;
3072 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3073 tx_size += ISCSI_HDR_LEN;
3074
3075 /*
3076 * Attach SENSE DATA payload to iSCSI Response PDU
3077 */
3078 if (cmd->se_cmd.sense_buffer &&
3079 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3080 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003081 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3082 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3083
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003084 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003085 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003086 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003087 iov[iov_count++].iov_len =
3088 (cmd->se_cmd.scsi_sense_length + padding);
3089 tx_size += cmd->se_cmd.scsi_sense_length;
3090
3091 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003092 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003093 cmd->se_cmd.scsi_sense_length, 0, padding);
3094 tx_size += padding;
3095 pr_debug("Adding %u bytes of padding to"
3096 " SENSE.\n", padding);
3097 }
3098
3099 if (conn->conn_ops->DataDigest) {
3100 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003101 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003102 (cmd->se_cmd.scsi_sense_length + padding),
3103 0, NULL, (u8 *)&cmd->data_crc);
3104
3105 iov[iov_count].iov_base = &cmd->data_crc;
3106 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3107 tx_size += ISCSI_CRC_LEN;
3108
3109 pr_debug("Attaching CRC32 DataDigest for"
3110 " SENSE, %u bytes CRC 0x%08x\n",
3111 (cmd->se_cmd.scsi_sense_length + padding),
3112 cmd->data_crc);
3113 }
3114
3115 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3116 " Response PDU\n",
3117 cmd->se_cmd.scsi_sense_length);
3118 }
3119
3120 if (conn->conn_ops->HeaderDigest) {
3121 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3122
3123 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3124 (unsigned char *)hdr, ISCSI_HDR_LEN,
3125 0, NULL, (u8 *)header_digest);
3126
3127 iov[0].iov_len += ISCSI_CRC_LEN;
3128 tx_size += ISCSI_CRC_LEN;
3129 pr_debug("Attaching CRC32 HeaderDigest for Response"
3130 " PDU 0x%08x\n", *header_digest);
3131 }
3132
3133 cmd->iov_misc_count = iov_count;
3134 cmd->tx_size = tx_size;
3135
3136 pr_debug("Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3137 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3138 (!recovery) ? "" : "Recovery ", cmd->init_task_tag,
3139 cmd->stat_sn, 0x00, cmd->se_cmd.scsi_status, conn->cid);
3140
3141 return 0;
3142}
3143
3144static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3145{
3146 switch (se_tmr->response) {
3147 case TMR_FUNCTION_COMPLETE:
3148 return ISCSI_TMF_RSP_COMPLETE;
3149 case TMR_TASK_DOES_NOT_EXIST:
3150 return ISCSI_TMF_RSP_NO_TASK;
3151 case TMR_LUN_DOES_NOT_EXIST:
3152 return ISCSI_TMF_RSP_NO_LUN;
3153 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3154 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3155 case TMR_FUNCTION_AUTHORIZATION_FAILED:
3156 return ISCSI_TMF_RSP_AUTH_FAILED;
3157 case TMR_FUNCTION_REJECTED:
3158 default:
3159 return ISCSI_TMF_RSP_REJECTED;
3160 }
3161}
3162
3163static int iscsit_send_task_mgt_rsp(
3164 struct iscsi_cmd *cmd,
3165 struct iscsi_conn *conn)
3166{
3167 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3168 struct iscsi_tm_rsp *hdr;
3169 u32 tx_size = 0;
3170
3171 hdr = (struct iscsi_tm_rsp *) cmd->pdu;
3172 memset(hdr, 0, ISCSI_HDR_LEN);
3173 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003174 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003175 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003176 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003177 cmd->stat_sn = conn->stat_sn++;
3178 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3179
3180 iscsit_increment_maxcmdsn(cmd, conn->sess);
3181 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3182 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3183
3184 cmd->iov_misc[0].iov_base = cmd->pdu;
3185 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3186 tx_size += ISCSI_HDR_LEN;
3187
3188 if (conn->conn_ops->HeaderDigest) {
3189 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3190
3191 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3192 (unsigned char *)hdr, ISCSI_HDR_LEN,
3193 0, NULL, (u8 *)header_digest);
3194
3195 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3196 tx_size += ISCSI_CRC_LEN;
3197 pr_debug("Attaching CRC32 HeaderDigest for Task"
3198 " Mgmt Response PDU 0x%08x\n", *header_digest);
3199 }
3200
3201 cmd->iov_misc_count = 1;
3202 cmd->tx_size = tx_size;
3203
3204 pr_debug("Built Task Management Response ITT: 0x%08x,"
3205 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3206 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3207
3208 return 0;
3209}
3210
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003211static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3212{
3213 bool ret = false;
3214
3215 if (np->np_sockaddr.ss_family == AF_INET6) {
3216 const struct sockaddr_in6 sin6 = {
3217 .sin6_addr = IN6ADDR_ANY_INIT };
3218 struct sockaddr_in6 *sock_in6 =
3219 (struct sockaddr_in6 *)&np->np_sockaddr;
3220
3221 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3222 sin6.sin6_addr.s6_addr, 16))
3223 ret = true;
3224 } else {
3225 struct sockaddr_in * sock_in =
3226 (struct sockaddr_in *)&np->np_sockaddr;
3227
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003228 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003229 ret = true;
3230 }
3231
3232 return ret;
3233}
3234
Andy Grover8b1e1242012-04-03 15:51:12 -07003235#define SENDTARGETS_BUF_LIMIT 32768U
3236
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003237static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3238{
3239 char *payload = NULL;
3240 struct iscsi_conn *conn = cmd->conn;
3241 struct iscsi_portal_group *tpg;
3242 struct iscsi_tiqn *tiqn;
3243 struct iscsi_tpg_np *tpg_np;
3244 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003245 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003246
Andy Grover8b1e1242012-04-03 15:51:12 -07003247 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3248 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003249
3250 payload = kzalloc(buffer_len, GFP_KERNEL);
3251 if (!payload) {
3252 pr_err("Unable to allocate memory for sendtargets"
3253 " response.\n");
3254 return -ENOMEM;
3255 }
3256
3257 spin_lock(&tiqn_lock);
3258 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3259 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3260 len += 1;
3261
3262 if ((len + payload_len) > buffer_len) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003263 end_of_buf = 1;
3264 goto eob;
3265 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003266 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003267 payload_len += len;
3268
3269 spin_lock(&tiqn->tiqn_tpg_lock);
3270 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3271
3272 spin_lock(&tpg->tpg_state_lock);
3273 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3274 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3275 spin_unlock(&tpg->tpg_state_lock);
3276 continue;
3277 }
3278 spin_unlock(&tpg->tpg_state_lock);
3279
3280 spin_lock(&tpg->tpg_np_lock);
3281 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3282 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003283 struct iscsi_np *np = tpg_np->tpg_np;
3284 bool inaddr_any = iscsit_check_inaddr_any(np);
3285
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003286 len = sprintf(buf, "TargetAddress="
3287 "%s%s%s:%hu,%hu",
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003288 (np->np_sockaddr.ss_family == AF_INET6) ?
3289 "[" : "", (inaddr_any == false) ?
3290 np->np_ip : conn->local_ip,
3291 (np->np_sockaddr.ss_family == AF_INET6) ?
3292 "]" : "", (inaddr_any == false) ?
3293 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003294 tpg->tpgt);
3295 len += 1;
3296
3297 if ((len + payload_len) > buffer_len) {
3298 spin_unlock(&tpg->tpg_np_lock);
3299 spin_unlock(&tiqn->tiqn_tpg_lock);
3300 end_of_buf = 1;
3301 goto eob;
3302 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003303 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003304 payload_len += len;
3305 }
3306 spin_unlock(&tpg->tpg_np_lock);
3307 }
3308 spin_unlock(&tiqn->tiqn_tpg_lock);
3309eob:
3310 if (end_of_buf)
3311 break;
3312 }
3313 spin_unlock(&tiqn_lock);
3314
3315 cmd->buf_ptr = payload;
3316
3317 return payload_len;
3318}
3319
3320/*
3321 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3322 * MaxRecvDataSegmentLength.
3323 */
3324static int iscsit_send_text_rsp(
3325 struct iscsi_cmd *cmd,
3326 struct iscsi_conn *conn)
3327{
3328 struct iscsi_text_rsp *hdr;
3329 struct kvec *iov;
3330 u32 padding = 0, tx_size = 0;
3331 int text_length, iov_count = 0;
3332
3333 text_length = iscsit_build_sendtargets_response(cmd);
3334 if (text_length < 0)
3335 return text_length;
3336
3337 padding = ((-text_length) & 3);
3338 if (padding != 0) {
3339 memset(cmd->buf_ptr + text_length, 0, padding);
3340 pr_debug("Attaching %u additional bytes for"
3341 " padding.\n", padding);
3342 }
3343
3344 hdr = (struct iscsi_text_rsp *) cmd->pdu;
3345 memset(hdr, 0, ISCSI_HDR_LEN);
3346 hdr->opcode = ISCSI_OP_TEXT_RSP;
3347 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3348 hton24(hdr->dlength, text_length);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003349 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003350 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3351 cmd->stat_sn = conn->stat_sn++;
3352 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3353
3354 iscsit_increment_maxcmdsn(cmd, conn->sess);
3355 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3356 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3357
3358 iov = &cmd->iov_misc[0];
3359
3360 iov[iov_count].iov_base = cmd->pdu;
3361 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3362 iov[iov_count].iov_base = cmd->buf_ptr;
3363 iov[iov_count++].iov_len = text_length + padding;
3364
3365 tx_size += (ISCSI_HDR_LEN + text_length + padding);
3366
3367 if (conn->conn_ops->HeaderDigest) {
3368 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3369
3370 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3371 (unsigned char *)hdr, ISCSI_HDR_LEN,
3372 0, NULL, (u8 *)header_digest);
3373
3374 iov[0].iov_len += ISCSI_CRC_LEN;
3375 tx_size += ISCSI_CRC_LEN;
3376 pr_debug("Attaching CRC32 HeaderDigest for"
3377 " Text Response PDU 0x%08x\n", *header_digest);
3378 }
3379
3380 if (conn->conn_ops->DataDigest) {
3381 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3382 cmd->buf_ptr, (text_length + padding),
3383 0, NULL, (u8 *)&cmd->data_crc);
3384
3385 iov[iov_count].iov_base = &cmd->data_crc;
3386 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3387 tx_size += ISCSI_CRC_LEN;
3388
3389 pr_debug("Attaching DataDigest for %u bytes of text"
3390 " data, CRC 0x%08x\n", (text_length + padding),
3391 cmd->data_crc);
3392 }
3393
3394 cmd->iov_misc_count = iov_count;
3395 cmd->tx_size = tx_size;
3396
3397 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3398 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3399 text_length, conn->cid);
3400 return 0;
3401}
3402
3403static int iscsit_send_reject(
3404 struct iscsi_cmd *cmd,
3405 struct iscsi_conn *conn)
3406{
3407 u32 iov_count = 0, tx_size = 0;
3408 struct iscsi_reject *hdr;
3409 struct kvec *iov;
3410
3411 hdr = (struct iscsi_reject *) cmd->pdu;
3412 hdr->opcode = ISCSI_OP_REJECT;
3413 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3414 hton24(hdr->dlength, ISCSI_HDR_LEN);
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003415 hdr->ffffffff = cpu_to_be32(0xffffffff);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003416 cmd->stat_sn = conn->stat_sn++;
3417 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3418 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3419 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3420
3421 iov = &cmd->iov_misc[0];
3422
3423 iov[iov_count].iov_base = cmd->pdu;
3424 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3425 iov[iov_count].iov_base = cmd->buf_ptr;
3426 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3427
3428 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3429
3430 if (conn->conn_ops->HeaderDigest) {
3431 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3432
3433 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3434 (unsigned char *)hdr, ISCSI_HDR_LEN,
3435 0, NULL, (u8 *)header_digest);
3436
3437 iov[0].iov_len += ISCSI_CRC_LEN;
3438 tx_size += ISCSI_CRC_LEN;
3439 pr_debug("Attaching CRC32 HeaderDigest for"
3440 " REJECT PDU 0x%08x\n", *header_digest);
3441 }
3442
3443 if (conn->conn_ops->DataDigest) {
3444 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3445 (unsigned char *)cmd->buf_ptr, ISCSI_HDR_LEN,
3446 0, NULL, (u8 *)&cmd->data_crc);
3447
3448 iov[iov_count].iov_base = &cmd->data_crc;
3449 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3450 tx_size += ISCSI_CRC_LEN;
3451 pr_debug("Attaching CRC32 DataDigest for REJECT"
3452 " PDU 0x%08x\n", cmd->data_crc);
3453 }
3454
3455 cmd->iov_misc_count = iov_count;
3456 cmd->tx_size = tx_size;
3457
3458 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3459 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3460
3461 return 0;
3462}
3463
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003464void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3465{
3466 struct iscsi_thread_set *ts = conn->thread_set;
3467 int ord, cpu;
3468 /*
3469 * thread_id is assigned from iscsit_global->ts_bitmap from
3470 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3471 *
3472 * Here we use thread_id to determine which CPU that this
3473 * iSCSI connection's iscsi_thread_set will be scheduled to
3474 * execute upon.
3475 */
3476 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003477 for_each_online_cpu(cpu) {
3478 if (ord-- == 0) {
3479 cpumask_set_cpu(cpu, conn->conn_cpumask);
3480 return;
3481 }
3482 }
3483 /*
3484 * This should never be reached..
3485 */
3486 dump_stack();
3487 cpumask_setall(conn->conn_cpumask);
3488}
3489
3490static inline void iscsit_thread_check_cpumask(
3491 struct iscsi_conn *conn,
3492 struct task_struct *p,
3493 int mode)
3494{
3495 char buf[128];
3496 /*
3497 * mode == 1 signals iscsi_target_tx_thread() usage.
3498 * mode == 0 signals iscsi_target_rx_thread() usage.
3499 */
3500 if (mode == 1) {
3501 if (!conn->conn_tx_reset_cpumask)
3502 return;
3503 conn->conn_tx_reset_cpumask = 0;
3504 } else {
3505 if (!conn->conn_rx_reset_cpumask)
3506 return;
3507 conn->conn_rx_reset_cpumask = 0;
3508 }
3509 /*
3510 * Update the CPU mask for this single kthread so that
3511 * both TX and RX kthreads are scheduled to run on the
3512 * same CPU.
3513 */
3514 memset(buf, 0, 128);
3515 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003516 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3517}
3518
Andy Grover6f3c0e62012-04-03 15:51:09 -07003519static int handle_immediate_queue(struct iscsi_conn *conn)
3520{
3521 struct iscsi_queue_req *qr;
3522 struct iscsi_cmd *cmd;
3523 u8 state;
3524 int ret;
3525
3526 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3527 atomic_set(&conn->check_immediate_queue, 0);
3528 cmd = qr->cmd;
3529 state = qr->state;
3530 kmem_cache_free(lio_qr_cache, qr);
3531
3532 switch (state) {
3533 case ISTATE_SEND_R2T:
3534 ret = iscsit_send_r2t(cmd, conn);
3535 if (ret < 0)
3536 goto err;
3537 break;
3538 case ISTATE_REMOVE:
3539 if (cmd->data_direction == DMA_TO_DEVICE)
3540 iscsit_stop_dataout_timer(cmd);
3541
3542 spin_lock_bh(&conn->cmd_lock);
3543 list_del(&cmd->i_conn_node);
3544 spin_unlock_bh(&conn->cmd_lock);
3545
3546 iscsit_free_cmd(cmd);
3547 continue;
3548 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3549 iscsit_mod_nopin_response_timer(conn);
3550 ret = iscsit_send_unsolicited_nopin(cmd,
3551 conn, 1);
3552 if (ret < 0)
3553 goto err;
3554 break;
3555 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3556 ret = iscsit_send_unsolicited_nopin(cmd,
3557 conn, 0);
3558 if (ret < 0)
3559 goto err;
3560 break;
3561 default:
3562 pr_err("Unknown Opcode: 0x%02x ITT:"
3563 " 0x%08x, i_state: %d on CID: %hu\n",
3564 cmd->iscsi_opcode, cmd->init_task_tag, state,
3565 conn->cid);
3566 goto err;
3567 }
3568 }
3569
3570 return 0;
3571
3572err:
3573 return -1;
3574}
3575
3576static int handle_response_queue(struct iscsi_conn *conn)
3577{
3578 struct iscsi_queue_req *qr;
3579 struct iscsi_cmd *cmd;
3580 u8 state;
3581 int ret;
3582
3583 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3584 cmd = qr->cmd;
3585 state = qr->state;
3586 kmem_cache_free(lio_qr_cache, qr);
3587
3588check_rsp_state:
3589 switch (state) {
3590 case ISTATE_SEND_DATAIN:
3591 ret = iscsit_send_data_in(cmd, conn);
3592 if (ret < 0)
3593 goto err;
3594 else if (!ret)
3595 /* more drs */
3596 goto check_rsp_state;
3597 else if (ret == 1) {
3598 /* all done */
3599 spin_lock_bh(&cmd->istate_lock);
3600 cmd->i_state = ISTATE_SENT_STATUS;
3601 spin_unlock_bh(&cmd->istate_lock);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003602
3603 if (atomic_read(&conn->check_immediate_queue))
3604 return 1;
3605
Andy Grover6f3c0e62012-04-03 15:51:09 -07003606 continue;
3607 } else if (ret == 2) {
3608 /* Still must send status,
3609 SCF_TRANSPORT_TASK_SENSE was set */
3610 spin_lock_bh(&cmd->istate_lock);
3611 cmd->i_state = ISTATE_SEND_STATUS;
3612 spin_unlock_bh(&cmd->istate_lock);
3613 state = ISTATE_SEND_STATUS;
3614 goto check_rsp_state;
3615 }
3616
3617 break;
3618 case ISTATE_SEND_STATUS:
3619 case ISTATE_SEND_STATUS_RECOVERY:
3620 ret = iscsit_send_status(cmd, conn);
3621 break;
3622 case ISTATE_SEND_LOGOUTRSP:
3623 ret = iscsit_send_logout_response(cmd, conn);
3624 break;
3625 case ISTATE_SEND_ASYNCMSG:
3626 ret = iscsit_send_conn_drop_async_message(
3627 cmd, conn);
3628 break;
3629 case ISTATE_SEND_NOPIN:
3630 ret = iscsit_send_nopin_response(cmd, conn);
3631 break;
3632 case ISTATE_SEND_REJECT:
3633 ret = iscsit_send_reject(cmd, conn);
3634 break;
3635 case ISTATE_SEND_TASKMGTRSP:
3636 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3637 if (ret != 0)
3638 break;
3639 ret = iscsit_tmr_post_handler(cmd, conn);
3640 if (ret != 0)
3641 iscsit_fall_back_to_erl0(conn->sess);
3642 break;
3643 case ISTATE_SEND_TEXTRSP:
3644 ret = iscsit_send_text_rsp(cmd, conn);
3645 break;
3646 default:
3647 pr_err("Unknown Opcode: 0x%02x ITT:"
3648 " 0x%08x, i_state: %d on CID: %hu\n",
3649 cmd->iscsi_opcode, cmd->init_task_tag,
3650 state, conn->cid);
3651 goto err;
3652 }
3653 if (ret < 0)
3654 goto err;
3655
3656 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3657 iscsit_tx_thread_wait_for_tcp(conn);
3658 iscsit_unmap_iovec(cmd);
3659 goto err;
3660 }
3661 iscsit_unmap_iovec(cmd);
3662
3663 switch (state) {
3664 case ISTATE_SEND_LOGOUTRSP:
3665 if (!iscsit_logout_post_handler(cmd, conn))
3666 goto restart;
3667 /* fall through */
3668 case ISTATE_SEND_STATUS:
3669 case ISTATE_SEND_ASYNCMSG:
3670 case ISTATE_SEND_NOPIN:
3671 case ISTATE_SEND_STATUS_RECOVERY:
3672 case ISTATE_SEND_TEXTRSP:
3673 case ISTATE_SEND_TASKMGTRSP:
3674 spin_lock_bh(&cmd->istate_lock);
3675 cmd->i_state = ISTATE_SENT_STATUS;
3676 spin_unlock_bh(&cmd->istate_lock);
3677 break;
3678 case ISTATE_SEND_REJECT:
3679 if (cmd->cmd_flags & ICF_REJECT_FAIL_CONN) {
3680 cmd->cmd_flags &= ~ICF_REJECT_FAIL_CONN;
3681 complete(&cmd->reject_comp);
3682 goto err;
3683 }
3684 complete(&cmd->reject_comp);
3685 break;
3686 default:
3687 pr_err("Unknown Opcode: 0x%02x ITT:"
3688 " 0x%08x, i_state: %d on CID: %hu\n",
3689 cmd->iscsi_opcode, cmd->init_task_tag,
3690 cmd->i_state, conn->cid);
3691 goto err;
3692 }
3693
3694 if (atomic_read(&conn->check_immediate_queue))
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003695 return 1;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003696 }
3697
3698 return 0;
3699
3700err:
3701 return -1;
3702restart:
3703 return -EAGAIN;
3704}
3705
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003706int iscsi_target_tx_thread(void *arg)
3707{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003708 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003709 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003710 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003711 /*
3712 * Allow ourselves to be interrupted by SIGINT so that a
3713 * connection recovery / failure event can be triggered externally.
3714 */
3715 allow_signal(SIGINT);
3716
3717restart:
3718 conn = iscsi_tx_thread_pre_handler(ts);
3719 if (!conn)
3720 goto out;
3721
Andy Grover6f3c0e62012-04-03 15:51:09 -07003722 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003723
3724 while (!kthread_should_stop()) {
3725 /*
3726 * Ensure that both TX and RX per connection kthreads
3727 * are scheduled to run on the same CPU.
3728 */
3729 iscsit_thread_check_cpumask(conn, current, 1);
3730
Roland Dreierd5627ac2012-10-31 09:16:46 -07003731 wait_event_interruptible(conn->queues_wq,
3732 !iscsit_conn_all_queues_empty(conn) ||
3733 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003734
3735 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3736 signal_pending(current))
3737 goto transport_err;
3738
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003739get_immediate:
Andy Grover6f3c0e62012-04-03 15:51:09 -07003740 ret = handle_immediate_queue(conn);
3741 if (ret < 0)
3742 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003743
Andy Grover6f3c0e62012-04-03 15:51:09 -07003744 ret = handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003745 if (ret == 1)
3746 goto get_immediate;
3747 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003748 goto restart;
3749 else if (ret < 0)
3750 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003751 }
3752
3753transport_err:
3754 iscsit_take_action_for_connection_exit(conn);
3755 goto restart;
3756out:
3757 return 0;
3758}
3759
3760int iscsi_target_rx_thread(void *arg)
3761{
3762 int ret;
3763 u8 buffer[ISCSI_HDR_LEN], opcode;
3764 u32 checksum = 0, digest = 0;
3765 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01003766 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003767 struct kvec iov;
3768 /*
3769 * Allow ourselves to be interrupted by SIGINT so that a
3770 * connection recovery / failure event can be triggered externally.
3771 */
3772 allow_signal(SIGINT);
3773
3774restart:
3775 conn = iscsi_rx_thread_pre_handler(ts);
3776 if (!conn)
3777 goto out;
3778
3779 while (!kthread_should_stop()) {
3780 /*
3781 * Ensure that both TX and RX per connection kthreads
3782 * are scheduled to run on the same CPU.
3783 */
3784 iscsit_thread_check_cpumask(conn, current, 0);
3785
3786 memset(buffer, 0, ISCSI_HDR_LEN);
3787 memset(&iov, 0, sizeof(struct kvec));
3788
3789 iov.iov_base = buffer;
3790 iov.iov_len = ISCSI_HDR_LEN;
3791
3792 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3793 if (ret != ISCSI_HDR_LEN) {
3794 iscsit_rx_thread_wait_for_tcp(conn);
3795 goto transport_err;
3796 }
3797
3798 /*
3799 * Set conn->bad_hdr for use with REJECT PDUs.
3800 */
3801 memcpy(&conn->bad_hdr, &buffer, ISCSI_HDR_LEN);
3802
3803 if (conn->conn_ops->HeaderDigest) {
3804 iov.iov_base = &digest;
3805 iov.iov_len = ISCSI_CRC_LEN;
3806
3807 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3808 if (ret != ISCSI_CRC_LEN) {
3809 iscsit_rx_thread_wait_for_tcp(conn);
3810 goto transport_err;
3811 }
3812
3813 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
3814 buffer, ISCSI_HDR_LEN,
3815 0, NULL, (u8 *)&checksum);
3816
3817 if (digest != checksum) {
3818 pr_err("HeaderDigest CRC32C failed,"
3819 " received 0x%08x, computed 0x%08x\n",
3820 digest, checksum);
3821 /*
3822 * Set the PDU to 0xff so it will intentionally
3823 * hit default in the switch below.
3824 */
3825 memset(buffer, 0xff, ISCSI_HDR_LEN);
3826 spin_lock_bh(&conn->sess->session_stats_lock);
3827 conn->sess->conn_digest_errors++;
3828 spin_unlock_bh(&conn->sess->session_stats_lock);
3829 } else {
3830 pr_debug("Got HeaderDigest CRC32C"
3831 " 0x%08x\n", checksum);
3832 }
3833 }
3834
3835 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
3836 goto transport_err;
3837
3838 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3839
3840 if (conn->sess->sess_ops->SessionType &&
3841 ((!(opcode & ISCSI_OP_TEXT)) ||
3842 (!(opcode & ISCSI_OP_LOGOUT)))) {
3843 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3844 " while in Discovery Session, rejecting.\n", opcode);
3845 iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
3846 buffer, conn);
3847 goto transport_err;
3848 }
3849
3850 switch (opcode) {
3851 case ISCSI_OP_SCSI_CMD:
3852 if (iscsit_handle_scsi_cmd(conn, buffer) < 0)
3853 goto transport_err;
3854 break;
3855 case ISCSI_OP_SCSI_DATA_OUT:
3856 if (iscsit_handle_data_out(conn, buffer) < 0)
3857 goto transport_err;
3858 break;
3859 case ISCSI_OP_NOOP_OUT:
3860 if (iscsit_handle_nop_out(conn, buffer) < 0)
3861 goto transport_err;
3862 break;
3863 case ISCSI_OP_SCSI_TMFUNC:
3864 if (iscsit_handle_task_mgt_cmd(conn, buffer) < 0)
3865 goto transport_err;
3866 break;
3867 case ISCSI_OP_TEXT:
3868 if (iscsit_handle_text_cmd(conn, buffer) < 0)
3869 goto transport_err;
3870 break;
3871 case ISCSI_OP_LOGOUT:
3872 ret = iscsit_handle_logout_cmd(conn, buffer);
3873 if (ret > 0) {
3874 wait_for_completion_timeout(&conn->conn_logout_comp,
3875 SECONDS_FOR_LOGOUT_COMP * HZ);
3876 goto transport_err;
3877 } else if (ret < 0)
3878 goto transport_err;
3879 break;
3880 case ISCSI_OP_SNACK:
3881 if (iscsit_handle_snack(conn, buffer) < 0)
3882 goto transport_err;
3883 break;
3884 default:
3885 pr_err("Got unknown iSCSI OpCode: 0x%02x\n",
3886 opcode);
3887 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3888 pr_err("Cannot recover from unknown"
3889 " opcode while ERL=0, closing iSCSI connection"
3890 ".\n");
3891 goto transport_err;
3892 }
3893 if (!conn->conn_ops->OFMarker) {
3894 pr_err("Unable to recover from unknown"
3895 " opcode while OFMarker=No, closing iSCSI"
3896 " connection.\n");
3897 goto transport_err;
3898 }
3899 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
3900 pr_err("Unable to recover from unknown"
3901 " opcode, closing iSCSI connection.\n");
3902 goto transport_err;
3903 }
3904 break;
3905 }
3906 }
3907
3908transport_err:
3909 if (!signal_pending(current))
3910 atomic_set(&conn->transport_failed, 1);
3911 iscsit_take_action_for_connection_exit(conn);
3912 goto restart;
3913out:
3914 return 0;
3915}
3916
3917static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
3918{
3919 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
3920 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003921 /*
3922 * We expect this function to only ever be called from either RX or TX
3923 * thread context via iscsit_close_connection() once the other context
3924 * has been reset -> returned sleeping pre-handler state.
3925 */
3926 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003927 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003928
Andy Grover2fbb4712012-04-03 15:51:01 -07003929 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003930 spin_unlock_bh(&conn->cmd_lock);
3931
3932 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003933
Nicholas Bellingerd2701902011-10-09 01:48:14 -07003934 iscsit_free_cmd(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003935
3936 spin_lock_bh(&conn->cmd_lock);
3937 }
3938 spin_unlock_bh(&conn->cmd_lock);
3939}
3940
3941static void iscsit_stop_timers_for_cmds(
3942 struct iscsi_conn *conn)
3943{
3944 struct iscsi_cmd *cmd;
3945
3946 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003947 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003948 if (cmd->data_direction == DMA_TO_DEVICE)
3949 iscsit_stop_dataout_timer(cmd);
3950 }
3951 spin_unlock_bh(&conn->cmd_lock);
3952}
3953
3954int iscsit_close_connection(
3955 struct iscsi_conn *conn)
3956{
3957 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
3958 struct iscsi_session *sess = conn->sess;
3959
3960 pr_debug("Closing iSCSI connection CID %hu on SID:"
3961 " %u\n", conn->cid, sess->sid);
3962 /*
3963 * Always up conn_logout_comp just in case the RX Thread is sleeping
3964 * and the logout response never got sent because the connection
3965 * failed.
3966 */
3967 complete(&conn->conn_logout_comp);
3968
3969 iscsi_release_thread_set(conn);
3970
3971 iscsit_stop_timers_for_cmds(conn);
3972 iscsit_stop_nopin_response_timer(conn);
3973 iscsit_stop_nopin_timer(conn);
3974 iscsit_free_queue_reqs_for_conn(conn);
3975
3976 /*
3977 * During Connection recovery drop unacknowledged out of order
3978 * commands for this connection, and prepare the other commands
3979 * for realligence.
3980 *
3981 * During normal operation clear the out of order commands (but
3982 * do not free the struct iscsi_ooo_cmdsn's) and release all
3983 * struct iscsi_cmds.
3984 */
3985 if (atomic_read(&conn->connection_recovery)) {
3986 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
3987 iscsit_prepare_cmds_for_realligance(conn);
3988 } else {
3989 iscsit_clear_ooo_cmdsns_for_conn(conn);
3990 iscsit_release_commands_from_conn(conn);
3991 }
3992
3993 /*
3994 * Handle decrementing session or connection usage count if
3995 * a logout response was not able to be sent because the
3996 * connection failed. Fall back to Session Recovery here.
3997 */
3998 if (atomic_read(&conn->conn_logout_remove)) {
3999 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4000 iscsit_dec_conn_usage_count(conn);
4001 iscsit_dec_session_usage_count(sess);
4002 }
4003 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4004 iscsit_dec_conn_usage_count(conn);
4005
4006 atomic_set(&conn->conn_logout_remove, 0);
4007 atomic_set(&sess->session_reinstatement, 0);
4008 atomic_set(&sess->session_fall_back_to_erl0, 1);
4009 }
4010
4011 spin_lock_bh(&sess->conn_lock);
4012 list_del(&conn->conn_list);
4013
4014 /*
4015 * Attempt to let the Initiator know this connection failed by
4016 * sending an Connection Dropped Async Message on another
4017 * active connection.
4018 */
4019 if (atomic_read(&conn->connection_recovery))
4020 iscsit_build_conn_drop_async_message(conn);
4021
4022 spin_unlock_bh(&sess->conn_lock);
4023
4024 /*
4025 * If connection reinstatement is being performed on this connection,
4026 * up the connection reinstatement semaphore that is being blocked on
4027 * in iscsit_cause_connection_reinstatement().
4028 */
4029 spin_lock_bh(&conn->state_lock);
4030 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4031 spin_unlock_bh(&conn->state_lock);
4032 complete(&conn->conn_wait_comp);
4033 wait_for_completion(&conn->conn_post_wait_comp);
4034 spin_lock_bh(&conn->state_lock);
4035 }
4036
4037 /*
4038 * If connection reinstatement is being performed on this connection
4039 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4040 * connection wait rcfr semaphore that is being blocked on
4041 * an iscsit_connection_reinstatement_rcfr().
4042 */
4043 if (atomic_read(&conn->connection_wait_rcfr)) {
4044 spin_unlock_bh(&conn->state_lock);
4045 complete(&conn->conn_wait_rcfr_comp);
4046 wait_for_completion(&conn->conn_post_wait_comp);
4047 spin_lock_bh(&conn->state_lock);
4048 }
4049 atomic_set(&conn->connection_reinstatement, 1);
4050 spin_unlock_bh(&conn->state_lock);
4051
4052 /*
4053 * If any other processes are accessing this connection pointer we
4054 * must wait until they have completed.
4055 */
4056 iscsit_check_conn_usage_count(conn);
4057
4058 if (conn->conn_rx_hash.tfm)
4059 crypto_free_hash(conn->conn_rx_hash.tfm);
4060 if (conn->conn_tx_hash.tfm)
4061 crypto_free_hash(conn->conn_tx_hash.tfm);
4062
4063 if (conn->conn_cpumask)
4064 free_cpumask_var(conn->conn_cpumask);
4065
4066 kfree(conn->conn_ops);
4067 conn->conn_ops = NULL;
4068
Al Virobf6932f2012-07-21 08:55:18 +01004069 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004070 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004071
4072 if (conn->conn_transport->iscsit_free_conn)
4073 conn->conn_transport->iscsit_free_conn(conn);
4074
4075 iscsit_put_transport(conn->conn_transport);
4076
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004077 conn->thread_set = NULL;
4078
4079 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4080 conn->conn_state = TARG_CONN_STATE_FREE;
4081 kfree(conn);
4082
4083 spin_lock_bh(&sess->conn_lock);
4084 atomic_dec(&sess->nconn);
4085 pr_debug("Decremented iSCSI connection count to %hu from node:"
4086 " %s\n", atomic_read(&sess->nconn),
4087 sess->sess_ops->InitiatorName);
4088 /*
4089 * Make sure that if one connection fails in an non ERL=2 iSCSI
4090 * Session that they all fail.
4091 */
4092 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4093 !atomic_read(&sess->session_logout))
4094 atomic_set(&sess->session_fall_back_to_erl0, 1);
4095
4096 /*
4097 * If this was not the last connection in the session, and we are
4098 * performing session reinstatement or falling back to ERL=0, call
4099 * iscsit_stop_session() without sleeping to shutdown the other
4100 * active connections.
4101 */
4102 if (atomic_read(&sess->nconn)) {
4103 if (!atomic_read(&sess->session_reinstatement) &&
4104 !atomic_read(&sess->session_fall_back_to_erl0)) {
4105 spin_unlock_bh(&sess->conn_lock);
4106 return 0;
4107 }
4108 if (!atomic_read(&sess->session_stop_active)) {
4109 atomic_set(&sess->session_stop_active, 1);
4110 spin_unlock_bh(&sess->conn_lock);
4111 iscsit_stop_session(sess, 0, 0);
4112 return 0;
4113 }
4114 spin_unlock_bh(&sess->conn_lock);
4115 return 0;
4116 }
4117
4118 /*
4119 * If this was the last connection in the session and one of the
4120 * following is occurring:
4121 *
4122 * Session Reinstatement is not being performed, and are falling back
4123 * to ERL=0 call iscsit_close_session().
4124 *
4125 * Session Logout was requested. iscsit_close_session() will be called
4126 * elsewhere.
4127 *
4128 * Session Continuation is not being performed, start the Time2Retain
4129 * handler and check if sleep_on_sess_wait_sem is active.
4130 */
4131 if (!atomic_read(&sess->session_reinstatement) &&
4132 atomic_read(&sess->session_fall_back_to_erl0)) {
4133 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004134 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004135
4136 return 0;
4137 } else if (atomic_read(&sess->session_logout)) {
4138 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4139 sess->session_state = TARG_SESS_STATE_FREE;
4140 spin_unlock_bh(&sess->conn_lock);
4141
4142 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4143 complete(&sess->session_wait_comp);
4144
4145 return 0;
4146 } else {
4147 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4148 sess->session_state = TARG_SESS_STATE_FAILED;
4149
4150 if (!atomic_read(&sess->session_continuation)) {
4151 spin_unlock_bh(&sess->conn_lock);
4152 iscsit_start_time2retain_handler(sess);
4153 } else
4154 spin_unlock_bh(&sess->conn_lock);
4155
4156 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4157 complete(&sess->session_wait_comp);
4158
4159 return 0;
4160 }
4161 spin_unlock_bh(&sess->conn_lock);
4162
4163 return 0;
4164}
4165
4166int iscsit_close_session(struct iscsi_session *sess)
4167{
4168 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4169 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4170
4171 if (atomic_read(&sess->nconn)) {
4172 pr_err("%d connection(s) still exist for iSCSI session"
4173 " to %s\n", atomic_read(&sess->nconn),
4174 sess->sess_ops->InitiatorName);
4175 BUG();
4176 }
4177
4178 spin_lock_bh(&se_tpg->session_lock);
4179 atomic_set(&sess->session_logout, 1);
4180 atomic_set(&sess->session_reinstatement, 1);
4181 iscsit_stop_time2retain_timer(sess);
4182 spin_unlock_bh(&se_tpg->session_lock);
4183
4184 /*
4185 * transport_deregister_session_configfs() will clear the
4186 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4187 * can be setting it again with __transport_register_session() in
4188 * iscsi_post_login_handler() again after the iscsit_stop_session()
4189 * completes in iscsi_np context.
4190 */
4191 transport_deregister_session_configfs(sess->se_sess);
4192
4193 /*
4194 * If any other processes are accessing this session pointer we must
4195 * wait until they have completed. If we are in an interrupt (the
4196 * time2retain handler) and contain and active session usage count we
4197 * restart the timer and exit.
4198 */
4199 if (!in_interrupt()) {
4200 if (iscsit_check_session_usage_count(sess) == 1)
4201 iscsit_stop_session(sess, 1, 1);
4202 } else {
4203 if (iscsit_check_session_usage_count(sess) == 2) {
4204 atomic_set(&sess->session_logout, 0);
4205 iscsit_start_time2retain_handler(sess);
4206 return 0;
4207 }
4208 }
4209
4210 transport_deregister_session(sess->se_sess);
4211
4212 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4213 iscsit_free_connection_recovery_entires(sess);
4214
4215 iscsit_free_all_ooo_cmdsns(sess);
4216
4217 spin_lock_bh(&se_tpg->session_lock);
4218 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4219 sess->session_state = TARG_SESS_STATE_FREE;
4220 pr_debug("Released iSCSI session from node: %s\n",
4221 sess->sess_ops->InitiatorName);
4222 tpg->nsessions--;
4223 if (tpg->tpg_tiqn)
4224 tpg->tpg_tiqn->tiqn_nsessions--;
4225
4226 pr_debug("Decremented number of active iSCSI Sessions on"
4227 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4228
4229 spin_lock(&sess_idr_lock);
4230 idr_remove(&sess_idr, sess->session_index);
4231 spin_unlock(&sess_idr_lock);
4232
4233 kfree(sess->sess_ops);
4234 sess->sess_ops = NULL;
4235 spin_unlock_bh(&se_tpg->session_lock);
4236
4237 kfree(sess);
4238 return 0;
4239}
4240
4241static void iscsit_logout_post_handler_closesession(
4242 struct iscsi_conn *conn)
4243{
4244 struct iscsi_session *sess = conn->sess;
4245
4246 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4247 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4248
4249 atomic_set(&conn->conn_logout_remove, 0);
4250 complete(&conn->conn_logout_comp);
4251
4252 iscsit_dec_conn_usage_count(conn);
4253 iscsit_stop_session(sess, 1, 1);
4254 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004255 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004256}
4257
4258static void iscsit_logout_post_handler_samecid(
4259 struct iscsi_conn *conn)
4260{
4261 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4262 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4263
4264 atomic_set(&conn->conn_logout_remove, 0);
4265 complete(&conn->conn_logout_comp);
4266
4267 iscsit_cause_connection_reinstatement(conn, 1);
4268 iscsit_dec_conn_usage_count(conn);
4269}
4270
4271static void iscsit_logout_post_handler_diffcid(
4272 struct iscsi_conn *conn,
4273 u16 cid)
4274{
4275 struct iscsi_conn *l_conn;
4276 struct iscsi_session *sess = conn->sess;
4277
4278 if (!sess)
4279 return;
4280
4281 spin_lock_bh(&sess->conn_lock);
4282 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4283 if (l_conn->cid == cid) {
4284 iscsit_inc_conn_usage_count(l_conn);
4285 break;
4286 }
4287 }
4288 spin_unlock_bh(&sess->conn_lock);
4289
4290 if (!l_conn)
4291 return;
4292
4293 if (l_conn->sock)
4294 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4295
4296 spin_lock_bh(&l_conn->state_lock);
4297 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4298 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4299 spin_unlock_bh(&l_conn->state_lock);
4300
4301 iscsit_cause_connection_reinstatement(l_conn, 1);
4302 iscsit_dec_conn_usage_count(l_conn);
4303}
4304
4305/*
4306 * Return of 0 causes the TX thread to restart.
4307 */
4308static int iscsit_logout_post_handler(
4309 struct iscsi_cmd *cmd,
4310 struct iscsi_conn *conn)
4311{
4312 int ret = 0;
4313
4314 switch (cmd->logout_reason) {
4315 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4316 switch (cmd->logout_response) {
4317 case ISCSI_LOGOUT_SUCCESS:
4318 case ISCSI_LOGOUT_CLEANUP_FAILED:
4319 default:
4320 iscsit_logout_post_handler_closesession(conn);
4321 break;
4322 }
4323 ret = 0;
4324 break;
4325 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4326 if (conn->cid == cmd->logout_cid) {
4327 switch (cmd->logout_response) {
4328 case ISCSI_LOGOUT_SUCCESS:
4329 case ISCSI_LOGOUT_CLEANUP_FAILED:
4330 default:
4331 iscsit_logout_post_handler_samecid(conn);
4332 break;
4333 }
4334 ret = 0;
4335 } else {
4336 switch (cmd->logout_response) {
4337 case ISCSI_LOGOUT_SUCCESS:
4338 iscsit_logout_post_handler_diffcid(conn,
4339 cmd->logout_cid);
4340 break;
4341 case ISCSI_LOGOUT_CID_NOT_FOUND:
4342 case ISCSI_LOGOUT_CLEANUP_FAILED:
4343 default:
4344 break;
4345 }
4346 ret = 1;
4347 }
4348 break;
4349 case ISCSI_LOGOUT_REASON_RECOVERY:
4350 switch (cmd->logout_response) {
4351 case ISCSI_LOGOUT_SUCCESS:
4352 case ISCSI_LOGOUT_CID_NOT_FOUND:
4353 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4354 case ISCSI_LOGOUT_CLEANUP_FAILED:
4355 default:
4356 break;
4357 }
4358 ret = 1;
4359 break;
4360 default:
4361 break;
4362
4363 }
4364 return ret;
4365}
4366
4367void iscsit_fail_session(struct iscsi_session *sess)
4368{
4369 struct iscsi_conn *conn;
4370
4371 spin_lock_bh(&sess->conn_lock);
4372 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4373 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4374 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4375 }
4376 spin_unlock_bh(&sess->conn_lock);
4377
4378 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4379 sess->session_state = TARG_SESS_STATE_FAILED;
4380}
4381
4382int iscsit_free_session(struct iscsi_session *sess)
4383{
4384 u16 conn_count = atomic_read(&sess->nconn);
4385 struct iscsi_conn *conn, *conn_tmp = NULL;
4386 int is_last;
4387
4388 spin_lock_bh(&sess->conn_lock);
4389 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4390
4391 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4392 conn_list) {
4393 if (conn_count == 0)
4394 break;
4395
4396 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4397 is_last = 1;
4398 } else {
4399 iscsit_inc_conn_usage_count(conn_tmp);
4400 is_last = 0;
4401 }
4402 iscsit_inc_conn_usage_count(conn);
4403
4404 spin_unlock_bh(&sess->conn_lock);
4405 iscsit_cause_connection_reinstatement(conn, 1);
4406 spin_lock_bh(&sess->conn_lock);
4407
4408 iscsit_dec_conn_usage_count(conn);
4409 if (is_last == 0)
4410 iscsit_dec_conn_usage_count(conn_tmp);
4411
4412 conn_count--;
4413 }
4414
4415 if (atomic_read(&sess->nconn)) {
4416 spin_unlock_bh(&sess->conn_lock);
4417 wait_for_completion(&sess->session_wait_comp);
4418 } else
4419 spin_unlock_bh(&sess->conn_lock);
4420
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004421 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004422 return 0;
4423}
4424
4425void iscsit_stop_session(
4426 struct iscsi_session *sess,
4427 int session_sleep,
4428 int connection_sleep)
4429{
4430 u16 conn_count = atomic_read(&sess->nconn);
4431 struct iscsi_conn *conn, *conn_tmp = NULL;
4432 int is_last;
4433
4434 spin_lock_bh(&sess->conn_lock);
4435 if (session_sleep)
4436 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4437
4438 if (connection_sleep) {
4439 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4440 conn_list) {
4441 if (conn_count == 0)
4442 break;
4443
4444 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4445 is_last = 1;
4446 } else {
4447 iscsit_inc_conn_usage_count(conn_tmp);
4448 is_last = 0;
4449 }
4450 iscsit_inc_conn_usage_count(conn);
4451
4452 spin_unlock_bh(&sess->conn_lock);
4453 iscsit_cause_connection_reinstatement(conn, 1);
4454 spin_lock_bh(&sess->conn_lock);
4455
4456 iscsit_dec_conn_usage_count(conn);
4457 if (is_last == 0)
4458 iscsit_dec_conn_usage_count(conn_tmp);
4459 conn_count--;
4460 }
4461 } else {
4462 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4463 iscsit_cause_connection_reinstatement(conn, 0);
4464 }
4465
4466 if (session_sleep && atomic_read(&sess->nconn)) {
4467 spin_unlock_bh(&sess->conn_lock);
4468 wait_for_completion(&sess->session_wait_comp);
4469 } else
4470 spin_unlock_bh(&sess->conn_lock);
4471}
4472
4473int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4474{
4475 struct iscsi_session *sess;
4476 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4477 struct se_session *se_sess, *se_sess_tmp;
4478 int session_count = 0;
4479
4480 spin_lock_bh(&se_tpg->session_lock);
4481 if (tpg->nsessions && !force) {
4482 spin_unlock_bh(&se_tpg->session_lock);
4483 return -1;
4484 }
4485
4486 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4487 sess_list) {
4488 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4489
4490 spin_lock(&sess->conn_lock);
4491 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4492 atomic_read(&sess->session_logout) ||
4493 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4494 spin_unlock(&sess->conn_lock);
4495 continue;
4496 }
4497 atomic_set(&sess->session_reinstatement, 1);
4498 spin_unlock(&sess->conn_lock);
4499 spin_unlock_bh(&se_tpg->session_lock);
4500
4501 iscsit_free_session(sess);
4502 spin_lock_bh(&se_tpg->session_lock);
4503
4504 session_count++;
4505 }
4506 spin_unlock_bh(&se_tpg->session_lock);
4507
4508 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4509 " Group: %hu\n", session_count, tpg->tpgt);
4510 return 0;
4511}
4512
4513MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4514MODULE_VERSION("4.1.x");
4515MODULE_AUTHOR("nab@Linux-iSCSI.org");
4516MODULE_LICENSE("GPL");
4517
4518module_init(iscsi_target_init_module);
4519module_exit(iscsi_target_cleanup_module);