blob: 9435a3d369a7b8aa7f8e628ea2c32f0845b1f2a2 [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
52static LIST_HEAD(g_tiqn_list);
53static LIST_HEAD(g_np_list);
54static DEFINE_SPINLOCK(tiqn_lock);
55static DEFINE_SPINLOCK(np_lock);
56
57static struct idr tiqn_idr;
58struct idr sess_idr;
59struct mutex auth_id_lock;
60spinlock_t sess_idr_lock;
61
62struct iscsit_global *iscsit_global;
63
64struct kmem_cache *lio_cmd_cache;
65struct kmem_cache *lio_qr_cache;
66struct kmem_cache *lio_dr_cache;
67struct kmem_cache *lio_ooo_cache;
68struct kmem_cache *lio_r2t_cache;
69
70static int iscsit_handle_immediate_data(struct iscsi_cmd *,
71 unsigned char *buf, u32);
72static int iscsit_logout_post_handler(struct iscsi_cmd *, struct iscsi_conn *);
73
74struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
75{
76 struct iscsi_tiqn *tiqn = NULL;
77
78 spin_lock(&tiqn_lock);
79 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
80 if (!strcmp(tiqn->tiqn, buf)) {
81
82 spin_lock(&tiqn->tiqn_state_lock);
83 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
84 tiqn->tiqn_access_count++;
85 spin_unlock(&tiqn->tiqn_state_lock);
86 spin_unlock(&tiqn_lock);
87 return tiqn;
88 }
89 spin_unlock(&tiqn->tiqn_state_lock);
90 }
91 }
92 spin_unlock(&tiqn_lock);
93
94 return NULL;
95}
96
97static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
98{
99 spin_lock(&tiqn->tiqn_state_lock);
100 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
101 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
102 spin_unlock(&tiqn->tiqn_state_lock);
103 return 0;
104 }
105 spin_unlock(&tiqn->tiqn_state_lock);
106
107 return -1;
108}
109
110void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
111{
112 spin_lock(&tiqn->tiqn_state_lock);
113 tiqn->tiqn_access_count--;
114 spin_unlock(&tiqn->tiqn_state_lock);
115}
116
117/*
118 * Note that IQN formatting is expected to be done in userspace, and
119 * no explict IQN format checks are done here.
120 */
121struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
122{
123 struct iscsi_tiqn *tiqn = NULL;
124 int ret;
125
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300126 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000127 pr_err("Target IQN exceeds %d bytes\n",
128 ISCSI_IQN_LEN);
129 return ERR_PTR(-EINVAL);
130 }
131
132 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
133 if (!tiqn) {
134 pr_err("Unable to allocate struct iscsi_tiqn\n");
135 return ERR_PTR(-ENOMEM);
136 }
137
138 sprintf(tiqn->tiqn, "%s", buf);
139 INIT_LIST_HEAD(&tiqn->tiqn_list);
140 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
141 spin_lock_init(&tiqn->tiqn_state_lock);
142 spin_lock_init(&tiqn->tiqn_tpg_lock);
143 spin_lock_init(&tiqn->sess_err_stats.lock);
144 spin_lock_init(&tiqn->login_stats.lock);
145 spin_lock_init(&tiqn->logout_stats.lock);
146
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000147 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
148
Tejun Heoc9365bd2013-02-27 17:04:43 -0800149 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000150 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800151
152 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000153 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800154 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000155 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800156 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000157 kfree(tiqn);
158 return ERR_PTR(ret);
159 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800160 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000161 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800162
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000163 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800164 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000165
166 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
167
168 return tiqn;
169
170}
171
172static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
173{
174 /*
175 * Wait for accesses to said struct iscsi_tiqn to end.
176 */
177 spin_lock(&tiqn->tiqn_state_lock);
178 while (tiqn->tiqn_access_count != 0) {
179 spin_unlock(&tiqn->tiqn_state_lock);
180 msleep(10);
181 spin_lock(&tiqn->tiqn_state_lock);
182 }
183 spin_unlock(&tiqn->tiqn_state_lock);
184}
185
186void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
187{
188 /*
189 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
190 * while holding tiqn->tiqn_state_lock. This means that all subsequent
191 * attempts to access this struct iscsi_tiqn will fail from both transport
192 * fabric and control code paths.
193 */
194 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
195 pr_err("iscsit_set_tiqn_shutdown() failed\n");
196 return;
197 }
198
199 iscsit_wait_for_tiqn(tiqn);
200
201 spin_lock(&tiqn_lock);
202 list_del(&tiqn->tiqn_list);
203 idr_remove(&tiqn_idr, tiqn->tiqn_index);
204 spin_unlock(&tiqn_lock);
205
206 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
207 tiqn->tiqn);
208 kfree(tiqn);
209}
210
211int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
212{
213 int ret;
214 /*
215 * Determine if the network portal is accepting storage traffic.
216 */
217 spin_lock_bh(&np->np_thread_lock);
218 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
219 spin_unlock_bh(&np->np_thread_lock);
220 return -1;
221 }
222 if (np->np_login_tpg) {
223 pr_err("np->np_login_tpg() is not NULL!\n");
224 spin_unlock_bh(&np->np_thread_lock);
225 return -1;
226 }
227 spin_unlock_bh(&np->np_thread_lock);
228 /*
229 * Determine if the portal group is accepting storage traffic.
230 */
231 spin_lock_bh(&tpg->tpg_state_lock);
232 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
233 spin_unlock_bh(&tpg->tpg_state_lock);
234 return -1;
235 }
236 spin_unlock_bh(&tpg->tpg_state_lock);
237
238 /*
239 * Here we serialize access across the TIQN+TPG Tuple.
240 */
241 ret = mutex_lock_interruptible(&tpg->np_login_lock);
242 if ((ret != 0) || signal_pending(current))
243 return -1;
244
245 spin_lock_bh(&np->np_thread_lock);
246 np->np_login_tpg = tpg;
247 spin_unlock_bh(&np->np_thread_lock);
248
249 return 0;
250}
251
252int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
253{
254 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
255
256 spin_lock_bh(&np->np_thread_lock);
257 np->np_login_tpg = NULL;
258 spin_unlock_bh(&np->np_thread_lock);
259
260 mutex_unlock(&tpg->np_login_lock);
261
262 if (tiqn)
263 iscsit_put_tiqn_for_login(tiqn);
264
265 return 0;
266}
267
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800268bool iscsit_check_np_match(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000269 struct __kernel_sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800270 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000271 int network_transport)
272{
273 struct sockaddr_in *sock_in, *sock_in_e;
274 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800275 bool ip_match = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000276 u16 port;
277
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800278 if (sockaddr->ss_family == AF_INET6) {
279 sock_in6 = (struct sockaddr_in6 *)sockaddr;
280 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
281
282 if (!memcmp(&sock_in6->sin6_addr.in6_u,
283 &sock_in6_e->sin6_addr.in6_u,
284 sizeof(struct in6_addr)))
285 ip_match = true;
286
287 port = ntohs(sock_in6->sin6_port);
288 } else {
289 sock_in = (struct sockaddr_in *)sockaddr;
290 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
291
292 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
293 ip_match = true;
294
295 port = ntohs(sock_in->sin_port);
296 }
297
298 if ((ip_match == true) && (np->np_port == port) &&
299 (np->np_network_transport == network_transport))
300 return true;
301
302 return false;
303}
304
305static struct iscsi_np *iscsit_get_np(
306 struct __kernel_sockaddr_storage *sockaddr,
307 int network_transport)
308{
309 struct iscsi_np *np;
310 bool match;
311
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000312 spin_lock_bh(&np_lock);
313 list_for_each_entry(np, &g_np_list, np_list) {
314 spin_lock(&np->np_thread_lock);
315 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
316 spin_unlock(&np->np_thread_lock);
317 continue;
318 }
319
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800320 match = iscsit_check_np_match(sockaddr, np, network_transport);
321 if (match == true) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000322 /*
323 * Increment the np_exports reference count now to
324 * prevent iscsit_del_np() below from being called
325 * while iscsi_tpg_add_network_portal() is called.
326 */
327 np->np_exports++;
328 spin_unlock(&np->np_thread_lock);
329 spin_unlock_bh(&np_lock);
330 return np;
331 }
332 spin_unlock(&np->np_thread_lock);
333 }
334 spin_unlock_bh(&np_lock);
335
336 return NULL;
337}
338
339struct iscsi_np *iscsit_add_np(
340 struct __kernel_sockaddr_storage *sockaddr,
341 char *ip_str,
342 int network_transport)
343{
344 struct sockaddr_in *sock_in;
345 struct sockaddr_in6 *sock_in6;
346 struct iscsi_np *np;
347 int ret;
348 /*
349 * Locate the existing struct iscsi_np if already active..
350 */
351 np = iscsit_get_np(sockaddr, network_transport);
352 if (np)
353 return np;
354
355 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
356 if (!np) {
357 pr_err("Unable to allocate memory for struct iscsi_np\n");
358 return ERR_PTR(-ENOMEM);
359 }
360
361 np->np_flags |= NPF_IP_NETWORK;
362 if (sockaddr->ss_family == AF_INET6) {
363 sock_in6 = (struct sockaddr_in6 *)sockaddr;
364 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
365 np->np_port = ntohs(sock_in6->sin6_port);
366 } else {
367 sock_in = (struct sockaddr_in *)sockaddr;
368 sprintf(np->np_ip, "%s", ip_str);
369 np->np_port = ntohs(sock_in->sin_port);
370 }
371
372 np->np_network_transport = network_transport;
373 spin_lock_init(&np->np_thread_lock);
374 init_completion(&np->np_restart_comp);
375 INIT_LIST_HEAD(&np->np_list);
376
377 ret = iscsi_target_setup_login_socket(np, sockaddr);
378 if (ret != 0) {
379 kfree(np);
380 return ERR_PTR(ret);
381 }
382
383 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
384 if (IS_ERR(np->np_thread)) {
385 pr_err("Unable to create kthread: iscsi_np\n");
386 ret = PTR_ERR(np->np_thread);
387 kfree(np);
388 return ERR_PTR(ret);
389 }
390 /*
391 * Increment the np_exports reference count now to prevent
392 * iscsit_del_np() below from being run while a new call to
393 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
394 * active. We don't need to hold np->np_thread_lock at this
395 * point because iscsi_np has not been added to g_np_list yet.
396 */
397 np->np_exports = 1;
398
399 spin_lock_bh(&np_lock);
400 list_add_tail(&np->np_list, &g_np_list);
401 spin_unlock_bh(&np_lock);
402
403 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
404 np->np_ip, np->np_port, (np->np_network_transport == ISCSI_TCP) ?
405 "TCP" : "SCTP");
406
407 return np;
408}
409
410int iscsit_reset_np_thread(
411 struct iscsi_np *np,
412 struct iscsi_tpg_np *tpg_np,
413 struct iscsi_portal_group *tpg)
414{
415 spin_lock_bh(&np->np_thread_lock);
416 if (tpg && tpg_np) {
417 /*
418 * The reset operation need only be performed when the
419 * passed struct iscsi_portal_group has a login in progress
420 * to one of the network portals.
421 */
422 if (tpg_np->tpg_np->np_login_tpg != tpg) {
423 spin_unlock_bh(&np->np_thread_lock);
424 return 0;
425 }
426 }
427 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
428 spin_unlock_bh(&np->np_thread_lock);
429 return 0;
430 }
431 np->np_thread_state = ISCSI_NP_THREAD_RESET;
432
433 if (np->np_thread) {
434 spin_unlock_bh(&np->np_thread_lock);
435 send_sig(SIGINT, np->np_thread, 1);
436 wait_for_completion(&np->np_restart_comp);
437 spin_lock_bh(&np->np_thread_lock);
438 }
439 spin_unlock_bh(&np->np_thread_lock);
440
441 return 0;
442}
443
Christoph Hellwigfceb5bc2012-09-26 08:00:36 -0400444static int iscsit_del_np_comm(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000445{
Al Virobf6932f2012-07-21 08:55:18 +0100446 if (np->np_socket)
447 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000448 return 0;
449}
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 }
470 iscsit_del_np_comm(np);
471
472 spin_lock_bh(&np_lock);
473 list_del(&np->np_list);
474 spin_unlock_bh(&np_lock);
475
476 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
477 np->np_ip, np->np_port, (np->np_network_transport == ISCSI_TCP) ?
478 "TCP" : "SCTP");
479
480 kfree(np);
481 return 0;
482}
483
484static int __init iscsi_target_init_module(void)
485{
486 int ret = 0;
487
488 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
489
490 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
491 if (!iscsit_global) {
492 pr_err("Unable to allocate memory for iscsit_global\n");
493 return -1;
494 }
495 mutex_init(&auth_id_lock);
496 spin_lock_init(&sess_idr_lock);
497 idr_init(&tiqn_idr);
498 idr_init(&sess_idr);
499
500 ret = iscsi_target_register_configfs();
501 if (ret < 0)
502 goto out;
503
504 ret = iscsi_thread_set_init();
505 if (ret < 0)
506 goto configfs_out;
507
508 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
509 TARGET_THREAD_SET_COUNT) {
510 pr_err("iscsi_allocate_thread_sets() returned"
511 " unexpected value!\n");
512 goto ts_out1;
513 }
514
515 lio_cmd_cache = kmem_cache_create("lio_cmd_cache",
516 sizeof(struct iscsi_cmd), __alignof__(struct iscsi_cmd),
517 0, NULL);
518 if (!lio_cmd_cache) {
519 pr_err("Unable to kmem_cache_create() for"
520 " lio_cmd_cache\n");
521 goto ts_out2;
522 }
523
524 lio_qr_cache = kmem_cache_create("lio_qr_cache",
525 sizeof(struct iscsi_queue_req),
526 __alignof__(struct iscsi_queue_req), 0, NULL);
527 if (!lio_qr_cache) {
528 pr_err("nable to kmem_cache_create() for"
529 " lio_qr_cache\n");
530 goto cmd_out;
531 }
532
533 lio_dr_cache = kmem_cache_create("lio_dr_cache",
534 sizeof(struct iscsi_datain_req),
535 __alignof__(struct iscsi_datain_req), 0, NULL);
536 if (!lio_dr_cache) {
537 pr_err("Unable to kmem_cache_create() for"
538 " lio_dr_cache\n");
539 goto qr_out;
540 }
541
542 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
543 sizeof(struct iscsi_ooo_cmdsn),
544 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
545 if (!lio_ooo_cache) {
546 pr_err("Unable to kmem_cache_create() for"
547 " lio_ooo_cache\n");
548 goto dr_out;
549 }
550
551 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
552 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
553 0, NULL);
554 if (!lio_r2t_cache) {
555 pr_err("Unable to kmem_cache_create() for"
556 " lio_r2t_cache\n");
557 goto ooo_out;
558 }
559
560 if (iscsit_load_discovery_tpg() < 0)
561 goto r2t_out;
562
563 return ret;
564r2t_out:
565 kmem_cache_destroy(lio_r2t_cache);
566ooo_out:
567 kmem_cache_destroy(lio_ooo_cache);
568dr_out:
569 kmem_cache_destroy(lio_dr_cache);
570qr_out:
571 kmem_cache_destroy(lio_qr_cache);
572cmd_out:
573 kmem_cache_destroy(lio_cmd_cache);
574ts_out2:
575 iscsi_deallocate_thread_sets();
576ts_out1:
577 iscsi_thread_set_free();
578configfs_out:
579 iscsi_target_deregister_configfs();
580out:
581 kfree(iscsit_global);
582 return -ENOMEM;
583}
584
585static void __exit iscsi_target_cleanup_module(void)
586{
587 iscsi_deallocate_thread_sets();
588 iscsi_thread_set_free();
589 iscsit_release_discovery_tpg();
590 kmem_cache_destroy(lio_cmd_cache);
591 kmem_cache_destroy(lio_qr_cache);
592 kmem_cache_destroy(lio_dr_cache);
593 kmem_cache_destroy(lio_ooo_cache);
594 kmem_cache_destroy(lio_r2t_cache);
595
596 iscsi_target_deregister_configfs();
597
598 kfree(iscsit_global);
599}
600
Andy Grover8b1e1242012-04-03 15:51:12 -0700601static int iscsit_add_reject(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000602 u8 reason,
603 int fail_conn,
604 unsigned char *buf,
605 struct iscsi_conn *conn)
606{
607 struct iscsi_cmd *cmd;
608 struct iscsi_reject *hdr;
609 int ret;
610
611 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
612 if (!cmd)
613 return -1;
614
615 cmd->iscsi_opcode = ISCSI_OP_REJECT;
616 if (fail_conn)
617 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
618
619 hdr = (struct iscsi_reject *) cmd->pdu;
620 hdr->reason = reason;
621
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100622 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000623 if (!cmd->buf_ptr) {
624 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
625 iscsit_release_cmd(cmd);
626 return -1;
627 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000628
629 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700630 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000631 spin_unlock_bh(&conn->cmd_lock);
632
633 cmd->i_state = ISTATE_SEND_REJECT;
634 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
635
636 ret = wait_for_completion_interruptible(&cmd->reject_comp);
637 if (ret != 0)
638 return -1;
639
640 return (!fail_conn) ? 0 : -1;
641}
642
643int iscsit_add_reject_from_cmd(
644 u8 reason,
645 int fail_conn,
646 int add_to_conn,
647 unsigned char *buf,
648 struct iscsi_cmd *cmd)
649{
650 struct iscsi_conn *conn;
651 struct iscsi_reject *hdr;
652 int ret;
653
654 if (!cmd->conn) {
655 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
656 cmd->init_task_tag);
657 return -1;
658 }
659 conn = cmd->conn;
660
661 cmd->iscsi_opcode = ISCSI_OP_REJECT;
662 if (fail_conn)
663 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
664
665 hdr = (struct iscsi_reject *) cmd->pdu;
666 hdr->reason = reason;
667
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100668 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000669 if (!cmd->buf_ptr) {
670 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
671 iscsit_release_cmd(cmd);
672 return -1;
673 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000674
675 if (add_to_conn) {
676 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700677 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000678 spin_unlock_bh(&conn->cmd_lock);
679 }
680
681 cmd->i_state = ISTATE_SEND_REJECT;
682 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
683
684 ret = wait_for_completion_interruptible(&cmd->reject_comp);
685 if (ret != 0)
686 return -1;
687
688 return (!fail_conn) ? 0 : -1;
689}
690
691/*
692 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700693 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000694 */
695static int iscsit_map_iovec(
696 struct iscsi_cmd *cmd,
697 struct kvec *iov,
698 u32 data_offset,
699 u32 data_length)
700{
701 u32 i = 0;
702 struct scatterlist *sg;
703 unsigned int page_off;
704
705 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700706 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000707 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700708 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000709 page_off = (data_offset % PAGE_SIZE);
710
711 cmd->first_data_sg = sg;
712 cmd->first_data_sg_off = page_off;
713
714 while (data_length) {
715 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
716
717 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
718 iov[i].iov_len = cur_len;
719
720 data_length -= cur_len;
721 page_off = 0;
722 sg = sg_next(sg);
723 i++;
724 }
725
726 cmd->kmapped_nents = i;
727
728 return i;
729}
730
731static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
732{
733 u32 i;
734 struct scatterlist *sg;
735
736 sg = cmd->first_data_sg;
737
738 for (i = 0; i < cmd->kmapped_nents; i++)
739 kunmap(sg_page(&sg[i]));
740}
741
742static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
743{
744 struct iscsi_cmd *cmd;
745
746 conn->exp_statsn = exp_statsn;
747
748 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700749 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000750 spin_lock(&cmd->istate_lock);
751 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800752 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000753 cmd->i_state = ISTATE_REMOVE;
754 spin_unlock(&cmd->istate_lock);
755 iscsit_add_cmd_to_immediate_queue(cmd, conn,
756 cmd->i_state);
757 continue;
758 }
759 spin_unlock(&cmd->istate_lock);
760 }
761 spin_unlock_bh(&conn->cmd_lock);
762}
763
764static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
765{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700766 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000767
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400768 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000769
770 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
771 if (!cmd->iov_data) {
772 pr_err("Unable to allocate cmd->iov_data\n");
773 return -ENOMEM;
774 }
775
776 cmd->orig_iov_data_count = iov_count;
777 return 0;
778}
779
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000780static int iscsit_handle_scsi_cmd(
781 struct iscsi_conn *conn,
782 unsigned char *buf)
783{
Christoph Hellwigde103c92012-11-06 12:24:09 -0800784 int data_direction, payload_length, cmdsn_ret = 0, immed_ret;
785 struct iscsi_cmd *cmd = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000786 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700787 int iscsi_task_attr;
788 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000789
790 spin_lock_bh(&conn->sess->session_stats_lock);
791 conn->sess->cmd_pdus++;
792 if (conn->sess->se_sess->se_node_acl) {
793 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
794 conn->sess->se_sess->se_node_acl->num_cmds++;
795 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
796 }
797 spin_unlock_bh(&conn->sess->session_stats_lock);
798
799 hdr = (struct iscsi_scsi_req *) buf;
800 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000801
802 /* FIXME; Add checks for AdditionalHeaderSegment */
803
804 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
805 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
806 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
807 " not set. Bad iSCSI Initiator.\n");
808 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
809 buf, conn);
810 }
811
812 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
813 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
814 /*
815 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
816 * that adds support for RESERVE/RELEASE. There is a bug
817 * add with this new functionality that sets R/W bits when
818 * neither CDB carries any READ or WRITE datapayloads.
819 */
820 if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) {
821 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
822 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
823 goto done;
824 }
825
826 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
827 " set when Expected Data Transfer Length is 0 for"
828 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]);
829 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
830 buf, conn);
831 }
832done:
833
834 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
835 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
836 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
837 " MUST be set if Expected Data Transfer Length is not 0."
838 " Bad iSCSI Initiator\n");
839 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
840 buf, conn);
841 }
842
843 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
844 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
845 pr_err("Bidirectional operations not supported!\n");
846 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
847 buf, conn);
848 }
849
850 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
851 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
852 " Scsi Command PDU.\n");
853 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
854 buf, conn);
855 }
856
857 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
858 pr_err("ImmediateData=No but DataSegmentLength=%u,"
859 " protocol error.\n", payload_length);
860 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
861 buf, conn);
862 }
863
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400864 if ((be32_to_cpu(hdr->data_length )== payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000865 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
866 pr_err("Expected Data Transfer Length and Length of"
867 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
868 " bit is not set protocol error\n");
869 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
870 buf, conn);
871 }
872
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400873 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000874 pr_err("DataSegmentLength: %u is greater than"
875 " EDTL: %u, protocol error.\n", payload_length,
876 hdr->data_length);
877 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
878 buf, conn);
879 }
880
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700881 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000882 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700883 " MaxXmitDataSegmentLength: %u, protocol error.\n",
884 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000885 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
886 buf, conn);
887 }
888
889 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
890 pr_err("DataSegmentLength: %u is greater than"
891 " FirstBurstLength: %u, protocol error.\n",
892 payload_length, conn->sess->sess_ops->FirstBurstLength);
893 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
894 buf, conn);
895 }
896
897 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
898 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
899 DMA_NONE;
900
Andy Groverd28b11692012-04-03 15:51:22 -0700901 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000902 if (!cmd)
903 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
Andy Groverd28b11692012-04-03 15:51:22 -0700904 buf, conn);
905
906 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700907 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
908 /*
909 * Figure out the SAM Task Attribute for the incoming SCSI CDB
910 */
911 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
912 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
913 sam_task_attr = MSG_SIMPLE_TAG;
914 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
915 sam_task_attr = MSG_ORDERED_TAG;
916 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
917 sam_task_attr = MSG_HEAD_TAG;
918 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
919 sam_task_attr = MSG_ACA_TAG;
920 else {
921 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
922 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
923 sam_task_attr = MSG_SIMPLE_TAG;
924 }
925
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000926 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
927 cmd->i_state = ISTATE_NEW_CMD;
928 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
929 cmd->immediate_data = (payload_length) ? 1 : 0;
930 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
931 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
932 if (cmd->unsolicited_data)
933 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
934
935 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
936 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
937 spin_lock_bh(&conn->sess->ttt_lock);
938 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
939 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
940 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
941 spin_unlock_bh(&conn->sess->ttt_lock);
942 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
943 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400944 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
945 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000946 cmd->first_burst_len = payload_length;
947
948 if (cmd->data_direction == DMA_FROM_DEVICE) {
949 struct iscsi_datain_req *dr;
950
951 dr = iscsit_allocate_datain_req();
952 if (!dr)
953 return iscsit_add_reject_from_cmd(
954 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
955 1, 1, buf, cmd);
956
957 iscsit_attach_datain_req(cmd, dr);
958 }
959
960 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700961 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
962 */
963 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400964 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
965 cmd->data_direction, sam_task_attr,
966 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700967
968 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
969 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
970 hdr->cmdsn, hdr->data_length, payload_length, conn->cid);
971
Christoph Hellwigde103c92012-11-06 12:24:09 -0800972 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
973 scsilun_to_int(&hdr->lun));
974 if (cmd->sense_reason)
975 goto attach_cmd;
976
977 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
978 if (cmd->sense_reason) {
979 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
980 return iscsit_add_reject_from_cmd(
981 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
982 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000983 }
Christoph Hellwigde103c92012-11-06 12:24:09 -0800984
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000985 goto attach_cmd;
986 }
Andy Grovera12f41f2012-04-03 15:51:20 -0700987
Christoph Hellwigde103c92012-11-06 12:24:09 -0800988 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000989 return iscsit_add_reject_from_cmd(
Christoph Hellwigde103c92012-11-06 12:24:09 -0800990 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
991 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000992 }
993
994attach_cmd:
995 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700996 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000997 spin_unlock_bh(&conn->cmd_lock);
998 /*
999 * Check if we need to delay processing because of ALUA
1000 * Active/NonOptimized primary access state..
1001 */
1002 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001003
Christoph Hellwigde103c92012-11-06 12:24:09 -08001004 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001005 return iscsit_add_reject_from_cmd(
1006 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
Nicholas Bellingercd931ee2012-01-16 17:11:54 -08001007 1, 0, buf, cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001008 }
1009
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001010 /*
1011 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1012 * the Immediate Bit is not set, and no Immediate
1013 * Data is attached.
1014 *
1015 * A PDU/CmdSN carrying Immediate Data can only
1016 * be processed after the DataCRC has passed.
1017 * If the DataCRC fails, the CmdSN MUST NOT
1018 * be acknowledged. (See below)
1019 */
1020 if (!cmd->immediate_data) {
1021 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001022 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1023 return 0;
1024 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001025 return iscsit_add_reject_from_cmd(
1026 ISCSI_REASON_PROTOCOL_ERROR,
1027 1, 0, buf, cmd);
1028 }
1029
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001030 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001031
1032 /*
1033 * If no Immediate Data is attached, it's OK to return now.
1034 */
1035 if (!cmd->immediate_data) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08001036 if (!cmd->sense_reason && cmd->unsolicited_data) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001037 iscsit_set_dataout_sequence_values(cmd);
1038
1039 spin_lock_bh(&cmd->dataout_timeout_lock);
1040 iscsit_start_dataout_timer(cmd, cmd->conn);
1041 spin_unlock_bh(&cmd->dataout_timeout_lock);
1042 }
1043
1044 return 0;
1045 }
1046
1047 /*
1048 * Early CHECK_CONDITIONs never make it to the transport processing
1049 * thread. They are processed in CmdSN order by
1050 * iscsit_check_received_cmdsn() below.
1051 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001052 if (cmd->sense_reason) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001053 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001054 goto after_immediate_data;
1055 }
1056 /*
1057 * Call directly into transport_generic_new_cmd() to perform
1058 * the backend memory allocation.
1059 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001060 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1061 if (cmd->sense_reason) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001062 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001063 goto after_immediate_data;
1064 }
1065
1066 immed_ret = iscsit_handle_immediate_data(cmd, buf, payload_length);
1067after_immediate_data:
1068 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1069 /*
1070 * A PDU/CmdSN carrying Immediate Data passed
1071 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1072 * Immediate Bit is not set.
1073 */
1074 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1075 /*
1076 * Special case for Unsupported SAM WRITE Opcodes
1077 * and ImmediateData=Yes.
1078 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001079 if (cmd->sense_reason) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001080 if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
1081 return -1;
1082 } else if (cmd->unsolicited_data) {
1083 iscsit_set_dataout_sequence_values(cmd);
1084
1085 spin_lock_bh(&cmd->dataout_timeout_lock);
1086 iscsit_start_dataout_timer(cmd, cmd->conn);
1087 spin_unlock_bh(&cmd->dataout_timeout_lock);
1088 }
1089
1090 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1091 return iscsit_add_reject_from_cmd(
1092 ISCSI_REASON_PROTOCOL_ERROR,
1093 1, 0, buf, cmd);
1094
1095 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1096 /*
1097 * Immediate Data failed DataCRC and ERL>=1,
1098 * silently drop this PDU and let the initiator
1099 * plug the CmdSN gap.
1100 *
1101 * FIXME: Send Unsolicited NOPIN with reserved
1102 * TTT here to help the initiator figure out
1103 * the missing CmdSN, although they should be
1104 * intelligent enough to determine the missing
1105 * CmdSN and issue a retry to plug the sequence.
1106 */
1107 cmd->i_state = ISTATE_REMOVE;
1108 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1109 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1110 return -1;
1111
1112 return 0;
1113}
1114
1115static u32 iscsit_do_crypto_hash_sg(
1116 struct hash_desc *hash,
1117 struct iscsi_cmd *cmd,
1118 u32 data_offset,
1119 u32 data_length,
1120 u32 padding,
1121 u8 *pad_bytes)
1122{
1123 u32 data_crc;
1124 u32 i;
1125 struct scatterlist *sg;
1126 unsigned int page_off;
1127
1128 crypto_hash_init(hash);
1129
1130 sg = cmd->first_data_sg;
1131 page_off = cmd->first_data_sg_off;
1132
1133 i = 0;
1134 while (data_length) {
1135 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1136
1137 crypto_hash_update(hash, &sg[i], cur_len);
1138
1139 data_length -= cur_len;
1140 page_off = 0;
1141 i++;
1142 }
1143
1144 if (padding) {
1145 struct scatterlist pad_sg;
1146
1147 sg_init_one(&pad_sg, pad_bytes, padding);
1148 crypto_hash_update(hash, &pad_sg, padding);
1149 }
1150 crypto_hash_final(hash, (u8 *) &data_crc);
1151
1152 return data_crc;
1153}
1154
1155static void iscsit_do_crypto_hash_buf(
1156 struct hash_desc *hash,
1157 unsigned char *buf,
1158 u32 payload_length,
1159 u32 padding,
1160 u8 *pad_bytes,
1161 u8 *data_crc)
1162{
1163 struct scatterlist sg;
1164
1165 crypto_hash_init(hash);
1166
Jörn Engel8359cf42011-11-24 02:05:51 +01001167 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001168 crypto_hash_update(hash, &sg, payload_length);
1169
1170 if (padding) {
1171 sg_init_one(&sg, pad_bytes, padding);
1172 crypto_hash_update(hash, &sg, padding);
1173 }
1174 crypto_hash_final(hash, data_crc);
1175}
1176
1177static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1178{
1179 int iov_ret, ooo_cmdsn = 0, ret;
1180 u8 data_crc_failed = 0;
1181 u32 checksum, iov_count = 0, padding = 0, rx_got = 0;
1182 u32 rx_size = 0, payload_length;
1183 struct iscsi_cmd *cmd = NULL;
1184 struct se_cmd *se_cmd;
1185 struct iscsi_data *hdr;
1186 struct kvec *iov;
1187 unsigned long flags;
1188
1189 hdr = (struct iscsi_data *) buf;
1190 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001191
1192 if (!payload_length) {
1193 pr_err("DataOUT payload is ZERO, protocol error.\n");
1194 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1195 buf, conn);
1196 }
1197
1198 /* iSCSI write */
1199 spin_lock_bh(&conn->sess->session_stats_lock);
1200 conn->sess->rx_data_octets += payload_length;
1201 if (conn->sess->se_sess->se_node_acl) {
1202 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1203 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1204 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1205 }
1206 spin_unlock_bh(&conn->sess->session_stats_lock);
1207
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001208 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001209 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001210 " MaxXmitDataSegmentLength: %u\n", payload_length,
1211 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001212 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1213 buf, conn);
1214 }
1215
1216 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1217 payload_length);
1218 if (!cmd)
1219 return 0;
1220
1221 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1222 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1223 hdr->itt, hdr->ttt, hdr->datasn, hdr->offset,
1224 payload_length, conn->cid);
1225
1226 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1227 pr_err("Command ITT: 0x%08x received DataOUT after"
1228 " last DataOUT received, dumping payload\n",
1229 cmd->init_task_tag);
1230 return iscsit_dump_data_payload(conn, payload_length, 1);
1231 }
1232
1233 if (cmd->data_direction != DMA_TO_DEVICE) {
1234 pr_err("Command ITT: 0x%08x received DataOUT for a"
1235 " NON-WRITE command.\n", cmd->init_task_tag);
1236 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1237 1, 0, buf, cmd);
1238 }
1239 se_cmd = &cmd->se_cmd;
1240 iscsit_mod_dataout_timer(cmd);
1241
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001242 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001243 pr_err("DataOut Offset: %u, Length %u greater than"
1244 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001245 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001246 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
1247 1, 0, buf, cmd);
1248 }
1249
1250 if (cmd->unsolicited_data) {
1251 int dump_unsolicited_data = 0;
1252
1253 if (conn->sess->sess_ops->InitialR2T) {
1254 pr_err("Received unexpected unsolicited data"
1255 " while InitialR2T=Yes, protocol error.\n");
1256 transport_send_check_condition_and_sense(&cmd->se_cmd,
1257 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1258 return -1;
1259 }
1260 /*
1261 * Special case for dealing with Unsolicited DataOUT
1262 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1263 * failures;
1264 */
1265
1266 /* Something's amiss if we're not in WRITE_PENDING state... */
1267 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1268 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1269 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1270
1271 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001272 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001273 dump_unsolicited_data = 1;
1274 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1275
1276 if (dump_unsolicited_data) {
1277 /*
1278 * Check if a delayed TASK_ABORTED status needs to
1279 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1280 * received with the unsolicitied data out.
1281 */
1282 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1283 iscsit_stop_dataout_timer(cmd);
1284
1285 transport_check_aborted_status(se_cmd,
1286 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1287 return iscsit_dump_data_payload(conn, payload_length, 1);
1288 }
1289 } else {
1290 /*
1291 * For the normal solicited data path:
1292 *
1293 * Check for a delayed TASK_ABORTED status and dump any
1294 * incoming data out payload if one exists. Also, when the
1295 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1296 * data out sequence, we decrement outstanding_r2ts. Once
1297 * outstanding_r2ts reaches zero, go ahead and send the delayed
1298 * TASK_ABORTED status.
1299 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001300 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001301 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1302 if (--cmd->outstanding_r2ts < 1) {
1303 iscsit_stop_dataout_timer(cmd);
1304 transport_check_aborted_status(
1305 se_cmd, 1);
1306 }
1307
1308 return iscsit_dump_data_payload(conn, payload_length, 1);
1309 }
1310 }
1311 /*
1312 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1313 * within-command recovery checks before receiving the payload.
1314 */
1315 ret = iscsit_check_pre_dataout(cmd, buf);
1316 if (ret == DATAOUT_WITHIN_COMMAND_RECOVERY)
1317 return 0;
1318 else if (ret == DATAOUT_CANNOT_RECOVER)
1319 return -1;
1320
1321 rx_size += payload_length;
1322 iov = &cmd->iov_data[0];
1323
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001324 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1325 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001326 if (iov_ret < 0)
1327 return -1;
1328
1329 iov_count += iov_ret;
1330
1331 padding = ((-payload_length) & 3);
1332 if (padding != 0) {
1333 iov[iov_count].iov_base = cmd->pad_bytes;
1334 iov[iov_count++].iov_len = padding;
1335 rx_size += padding;
1336 pr_debug("Receiving %u padding bytes.\n", padding);
1337 }
1338
1339 if (conn->conn_ops->DataDigest) {
1340 iov[iov_count].iov_base = &checksum;
1341 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1342 rx_size += ISCSI_CRC_LEN;
1343 }
1344
1345 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1346
1347 iscsit_unmap_iovec(cmd);
1348
1349 if (rx_got != rx_size)
1350 return -1;
1351
1352 if (conn->conn_ops->DataDigest) {
1353 u32 data_crc;
1354
1355 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001356 be32_to_cpu(hdr->offset),
1357 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001358 cmd->pad_bytes);
1359
1360 if (checksum != data_crc) {
1361 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1362 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1363 " does not match computed 0x%08x\n",
1364 hdr->itt, hdr->offset, payload_length,
1365 hdr->datasn, checksum, data_crc);
1366 data_crc_failed = 1;
1367 } else {
1368 pr_debug("Got CRC32C DataDigest 0x%08x for"
1369 " %u bytes of Data Out\n", checksum,
1370 payload_length);
1371 }
1372 }
1373 /*
1374 * Increment post receive data and CRC values or perform
1375 * within-command recovery.
1376 */
1377 ret = iscsit_check_post_dataout(cmd, buf, data_crc_failed);
1378 if ((ret == DATAOUT_NORMAL) || (ret == DATAOUT_WITHIN_COMMAND_RECOVERY))
1379 return 0;
1380 else if (ret == DATAOUT_SEND_R2T) {
1381 iscsit_set_dataout_sequence_values(cmd);
Andy Grover8b1e1242012-04-03 15:51:12 -07001382 iscsit_build_r2ts_for_cmd(cmd, conn, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001383 } else if (ret == DATAOUT_SEND_TO_TRANSPORT) {
1384 /*
1385 * Handle extra special case for out of order
1386 * Unsolicited Data Out.
1387 */
1388 spin_lock_bh(&cmd->istate_lock);
1389 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1390 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1391 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1392 spin_unlock_bh(&cmd->istate_lock);
1393
1394 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001395 if (ooo_cmdsn)
1396 return 0;
1397 target_execute_cmd(&cmd->se_cmd);
1398 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001399 } else /* DATAOUT_CANNOT_RECOVER */
1400 return -1;
1401
1402 return 0;
1403}
1404
1405static int iscsit_handle_nop_out(
1406 struct iscsi_conn *conn,
1407 unsigned char *buf)
1408{
1409 unsigned char *ping_data = NULL;
1410 int cmdsn_ret, niov = 0, ret = 0, rx_got, rx_size;
1411 u32 checksum, data_crc, padding = 0, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001412 struct iscsi_cmd *cmd = NULL;
1413 struct kvec *iov = NULL;
1414 struct iscsi_nopout *hdr;
1415
1416 hdr = (struct iscsi_nopout *) buf;
1417 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001418
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001419 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001420 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1421 " not set, protocol error.\n");
1422 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1423 buf, conn);
1424 }
1425
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001426 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001427 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001428 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001429 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001430 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001431 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1432 buf, conn);
1433 }
1434
1435 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%09x,"
1436 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001437 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001438 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1439 payload_length);
1440 /*
1441 * This is not a response to a Unsolicited NopIN, which means
1442 * it can either be a NOPOUT ping request (with a valid ITT),
1443 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1444 * Either way, make sure we allocate an struct iscsi_cmd, as both
1445 * can contain ping data.
1446 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001447 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001448 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1449 if (!cmd)
1450 return iscsit_add_reject(
1451 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1452 1, buf, conn);
1453
1454 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1455 cmd->i_state = ISTATE_SEND_NOPIN;
1456 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1457 1 : 0);
1458 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1459 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001460 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1461 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001462 cmd->data_direction = DMA_NONE;
1463 }
1464
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001465 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001466 rx_size = payload_length;
1467 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1468 if (!ping_data) {
1469 pr_err("Unable to allocate memory for"
1470 " NOPOUT ping data.\n");
1471 ret = -1;
1472 goto out;
1473 }
1474
1475 iov = &cmd->iov_misc[0];
1476 iov[niov].iov_base = ping_data;
1477 iov[niov++].iov_len = payload_length;
1478
1479 padding = ((-payload_length) & 3);
1480 if (padding != 0) {
1481 pr_debug("Receiving %u additional bytes"
1482 " for padding.\n", padding);
1483 iov[niov].iov_base = &cmd->pad_bytes;
1484 iov[niov++].iov_len = padding;
1485 rx_size += padding;
1486 }
1487 if (conn->conn_ops->DataDigest) {
1488 iov[niov].iov_base = &checksum;
1489 iov[niov++].iov_len = ISCSI_CRC_LEN;
1490 rx_size += ISCSI_CRC_LEN;
1491 }
1492
1493 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1494 if (rx_got != rx_size) {
1495 ret = -1;
1496 goto out;
1497 }
1498
1499 if (conn->conn_ops->DataDigest) {
1500 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1501 ping_data, payload_length,
1502 padding, cmd->pad_bytes,
1503 (u8 *)&data_crc);
1504
1505 if (checksum != data_crc) {
1506 pr_err("Ping data CRC32C DataDigest"
1507 " 0x%08x does not match computed 0x%08x\n",
1508 checksum, data_crc);
1509 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1510 pr_err("Unable to recover from"
1511 " NOPOUT Ping DataCRC failure while in"
1512 " ERL=0.\n");
1513 ret = -1;
1514 goto out;
1515 } else {
1516 /*
1517 * Silently drop this PDU and let the
1518 * initiator plug the CmdSN gap.
1519 */
1520 pr_debug("Dropping NOPOUT"
1521 " Command CmdSN: 0x%08x due to"
1522 " DataCRC error.\n", hdr->cmdsn);
1523 ret = 0;
1524 goto out;
1525 }
1526 } else {
1527 pr_debug("Got CRC32C DataDigest"
1528 " 0x%08x for %u bytes of ping data.\n",
1529 checksum, payload_length);
1530 }
1531 }
1532
1533 ping_data[payload_length] = '\0';
1534 /*
1535 * Attach ping data to struct iscsi_cmd->buf_ptr.
1536 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001537 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001538 cmd->buf_ptr_size = payload_length;
1539
1540 pr_debug("Got %u bytes of NOPOUT ping"
1541 " data.\n", payload_length);
1542 pr_debug("Ping Data: \"%s\"\n", ping_data);
1543 }
1544
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001545 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001546 if (!cmd) {
1547 pr_err("Checking CmdSN for NOPOUT,"
1548 " but cmd is NULL!\n");
1549 return -1;
1550 }
1551 /*
1552 * Initiator is expecting a NopIN ping reply,
1553 */
1554 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001555 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001556 spin_unlock_bh(&conn->cmd_lock);
1557
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001558 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001559
1560 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1561 iscsit_add_cmd_to_response_queue(cmd, conn,
1562 cmd->i_state);
1563 return 0;
1564 }
1565
1566 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1567 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1568 ret = 0;
1569 goto ping_out;
1570 }
1571 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1572 return iscsit_add_reject_from_cmd(
1573 ISCSI_REASON_PROTOCOL_ERROR,
1574 1, 0, buf, cmd);
1575
1576 return 0;
1577 }
1578
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001579 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001580 /*
1581 * This was a response to a unsolicited NOPIN ping.
1582 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001583 cmd = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001584 if (!cmd)
1585 return -1;
1586
1587 iscsit_stop_nopin_response_timer(conn);
1588
1589 cmd->i_state = ISTATE_REMOVE;
1590 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1591 iscsit_start_nopin_timer(conn);
1592 } else {
1593 /*
1594 * Initiator is not expecting a NOPIN is response.
1595 * Just ignore for now.
1596 *
1597 * iSCSI v19-91 10.18
1598 * "A NOP-OUT may also be used to confirm a changed
1599 * ExpStatSN if another PDU will not be available
1600 * for a long time."
1601 */
1602 ret = 0;
1603 goto out;
1604 }
1605
1606 return 0;
1607out:
1608 if (cmd)
1609 iscsit_release_cmd(cmd);
1610ping_out:
1611 kfree(ping_data);
1612 return ret;
1613}
1614
1615static int iscsit_handle_task_mgt_cmd(
1616 struct iscsi_conn *conn,
1617 unsigned char *buf)
1618{
1619 struct iscsi_cmd *cmd;
1620 struct se_tmr_req *se_tmr;
1621 struct iscsi_tmr_req *tmr_req;
1622 struct iscsi_tm *hdr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001623 int out_of_order_cmdsn = 0;
1624 int ret;
1625 u8 function;
1626
1627 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001628 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1629 function = hdr->flags;
1630
1631 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1632 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1633 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1634 hdr->rtt, hdr->refcmdsn, conn->cid);
1635
1636 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1637 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001638 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001639 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001640 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001641 }
1642
1643 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1644 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1645 pr_err("Task Management Request TASK_REASSIGN not"
1646 " issued as immediate command, bad iSCSI Initiator"
1647 "implementation\n");
1648 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1649 buf, conn);
1650 }
1651 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001652 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1653 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001654
Andy Groverd28b11692012-04-03 15:51:22 -07001655 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001656 if (!cmd)
1657 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
Andy Groverd28b11692012-04-03 15:51:22 -07001658 1, buf, conn);
1659
1660 cmd->data_direction = DMA_NONE;
1661
1662 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1663 if (!cmd->tmr_req) {
1664 pr_err("Unable to allocate memory for"
1665 " Task Management command!\n");
1666 return iscsit_add_reject_from_cmd(
1667 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1668 1, 1, buf, cmd);
1669 }
1670
1671 /*
1672 * TASK_REASSIGN for ERL=2 / connection stays inside of
1673 * LIO-Target $FABRIC_MOD
1674 */
1675 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1676
1677 u8 tcm_function;
1678 int ret;
1679
1680 transport_init_se_cmd(&cmd->se_cmd,
1681 &lio_target_fabric_configfs->tf_ops,
1682 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001683 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001684
1685 switch (function) {
1686 case ISCSI_TM_FUNC_ABORT_TASK:
1687 tcm_function = TMR_ABORT_TASK;
1688 break;
1689 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1690 tcm_function = TMR_ABORT_TASK_SET;
1691 break;
1692 case ISCSI_TM_FUNC_CLEAR_ACA:
1693 tcm_function = TMR_CLEAR_ACA;
1694 break;
1695 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1696 tcm_function = TMR_CLEAR_TASK_SET;
1697 break;
1698 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1699 tcm_function = TMR_LUN_RESET;
1700 break;
1701 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1702 tcm_function = TMR_TARGET_WARM_RESET;
1703 break;
1704 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1705 tcm_function = TMR_TARGET_COLD_RESET;
1706 break;
1707 default:
1708 pr_err("Unknown iSCSI TMR Function:"
1709 " 0x%02x\n", function);
1710 return iscsit_add_reject_from_cmd(
1711 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1712 1, 1, buf, cmd);
1713 }
1714
1715 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1716 tcm_function, GFP_KERNEL);
1717 if (ret < 0)
1718 return iscsit_add_reject_from_cmd(
1719 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1720 1, 1, buf, cmd);
1721
1722 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1723 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001724
1725 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1726 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1727 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1728 cmd->init_task_tag = hdr->itt;
1729 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001730 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1731 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001732 se_tmr = cmd->se_cmd.se_tmr_req;
1733 tmr_req = cmd->tmr_req;
1734 /*
1735 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1736 */
1737 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001738 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1739 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001740 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001741 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1742 goto attach;
1743 }
1744 }
1745
1746 switch (function) {
1747 case ISCSI_TM_FUNC_ABORT_TASK:
1748 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001749 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001750 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001751 break;
1752 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1753 case ISCSI_TM_FUNC_CLEAR_ACA:
1754 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1755 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1756 break;
1757 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1758 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001759 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1760 goto attach;
1761 }
1762 break;
1763 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1764 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001765 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1766 goto attach;
1767 }
1768 break;
1769 case ISCSI_TM_FUNC_TASK_REASSIGN:
1770 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1771 /*
1772 * Perform sanity checks on the ExpDataSN only if the
1773 * TASK_REASSIGN was successful.
1774 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001775 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001776 break;
1777
1778 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
1779 return iscsit_add_reject_from_cmd(
1780 ISCSI_REASON_BOOKMARK_INVALID, 1, 1,
1781 buf, cmd);
1782 break;
1783 default:
1784 pr_err("Unknown TMR function: 0x%02x, protocol"
1785 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001786 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1787 goto attach;
1788 }
1789
1790 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1791 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1792 se_tmr->call_transport = 1;
1793attach:
1794 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001795 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001796 spin_unlock_bh(&conn->cmd_lock);
1797
1798 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1799 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1800 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1801 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001802 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001803 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001804 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001805 return iscsit_add_reject_from_cmd(
1806 ISCSI_REASON_PROTOCOL_ERROR,
1807 1, 0, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001808 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001809 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001810
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001811 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001812 return 0;
1813 /*
1814 * Found the referenced task, send to transport for processing.
1815 */
1816 if (se_tmr->call_transport)
1817 return transport_generic_handle_tmr(&cmd->se_cmd);
1818
1819 /*
1820 * Could not find the referenced LUN, task, or Task Management
1821 * command not authorized or supported. Change state and
1822 * let the tx_thread send the response.
1823 *
1824 * For connection recovery, this is also the default action for
1825 * TMR TASK_REASSIGN.
1826 */
1827 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1828 return 0;
1829}
1830
1831/* #warning FIXME: Support Text Command parameters besides SendTargets */
1832static int iscsit_handle_text_cmd(
1833 struct iscsi_conn *conn,
1834 unsigned char *buf)
1835{
1836 char *text_ptr, *text_in;
1837 int cmdsn_ret, niov = 0, rx_got, rx_size;
1838 u32 checksum = 0, data_crc = 0, payload_length;
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001839 u32 padding = 0, pad_bytes = 0, text_length = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001840 struct iscsi_cmd *cmd;
1841 struct kvec iov[3];
1842 struct iscsi_text *hdr;
1843
1844 hdr = (struct iscsi_text *) buf;
1845 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001846
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001847 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001848 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001849 "greater than MaxXmitDataSegmentLength %u.\n",
1850 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001851 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1852 buf, conn);
1853 }
1854
1855 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1856 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1857 hdr->exp_statsn, payload_length);
1858
1859 rx_size = text_length = payload_length;
1860 if (text_length) {
1861 text_in = kzalloc(text_length, GFP_KERNEL);
1862 if (!text_in) {
1863 pr_err("Unable to allocate memory for"
1864 " incoming text parameters\n");
1865 return -1;
1866 }
1867
1868 memset(iov, 0, 3 * sizeof(struct kvec));
1869 iov[niov].iov_base = text_in;
1870 iov[niov++].iov_len = text_length;
1871
1872 padding = ((-payload_length) & 3);
1873 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001874 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001875 iov[niov++].iov_len = padding;
1876 rx_size += padding;
1877 pr_debug("Receiving %u additional bytes"
1878 " for padding.\n", padding);
1879 }
1880 if (conn->conn_ops->DataDigest) {
1881 iov[niov].iov_base = &checksum;
1882 iov[niov++].iov_len = ISCSI_CRC_LEN;
1883 rx_size += ISCSI_CRC_LEN;
1884 }
1885
1886 rx_got = rx_data(conn, &iov[0], niov, rx_size);
1887 if (rx_got != rx_size) {
1888 kfree(text_in);
1889 return -1;
1890 }
1891
1892 if (conn->conn_ops->DataDigest) {
1893 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1894 text_in, text_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001895 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001896 (u8 *)&data_crc);
1897
1898 if (checksum != data_crc) {
1899 pr_err("Text data CRC32C DataDigest"
1900 " 0x%08x does not match computed"
1901 " 0x%08x\n", checksum, data_crc);
1902 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1903 pr_err("Unable to recover from"
1904 " Text Data digest failure while in"
1905 " ERL=0.\n");
1906 kfree(text_in);
1907 return -1;
1908 } else {
1909 /*
1910 * Silently drop this PDU and let the
1911 * initiator plug the CmdSN gap.
1912 */
1913 pr_debug("Dropping Text"
1914 " Command CmdSN: 0x%08x due to"
1915 " DataCRC error.\n", hdr->cmdsn);
1916 kfree(text_in);
1917 return 0;
1918 }
1919 } else {
1920 pr_debug("Got CRC32C DataDigest"
1921 " 0x%08x for %u bytes of text data.\n",
1922 checksum, text_length);
1923 }
1924 }
1925 text_in[text_length - 1] = '\0';
1926 pr_debug("Successfully read %d bytes of text"
1927 " data.\n", text_length);
1928
1929 if (strncmp("SendTargets", text_in, 11) != 0) {
1930 pr_err("Received Text Data that is not"
1931 " SendTargets, cannot continue.\n");
1932 kfree(text_in);
1933 return -1;
1934 }
1935 text_ptr = strchr(text_in, '=');
1936 if (!text_ptr) {
1937 pr_err("No \"=\" separator found in Text Data,"
1938 " cannot continue.\n");
1939 kfree(text_in);
1940 return -1;
1941 }
1942 if (strncmp("=All", text_ptr, 4) != 0) {
1943 pr_err("Unable to locate All value for"
1944 " SendTargets key, cannot continue.\n");
1945 kfree(text_in);
1946 return -1;
1947 }
1948/*#warning Support SendTargets=(iSCSI Target Name/Nothing) values. */
1949 kfree(text_in);
1950 }
1951
1952 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1953 if (!cmd)
1954 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1955 1, buf, conn);
1956
1957 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1958 cmd->i_state = ISTATE_SEND_TEXTRSP;
1959 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1960 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1961 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001962 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1963 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001964 cmd->data_direction = DMA_NONE;
1965
1966 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001967 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001968 spin_unlock_bh(&conn->cmd_lock);
1969
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001970 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001971
1972 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1973 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1974 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1975 return iscsit_add_reject_from_cmd(
1976 ISCSI_REASON_PROTOCOL_ERROR,
1977 1, 0, buf, cmd);
1978
1979 return 0;
1980 }
1981
1982 return iscsit_execute_cmd(cmd, 0);
1983}
1984
1985int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1986{
1987 struct iscsi_conn *conn_p;
1988 struct iscsi_session *sess = conn->sess;
1989
1990 pr_debug("Received logout request CLOSESESSION on CID: %hu"
1991 " for SID: %u.\n", conn->cid, conn->sess->sid);
1992
1993 atomic_set(&sess->session_logout, 1);
1994 atomic_set(&conn->conn_logout_remove, 1);
1995 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
1996
1997 iscsit_inc_conn_usage_count(conn);
1998 iscsit_inc_session_usage_count(sess);
1999
2000 spin_lock_bh(&sess->conn_lock);
2001 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2002 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2003 continue;
2004
2005 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2006 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2007 }
2008 spin_unlock_bh(&sess->conn_lock);
2009
2010 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2011
2012 return 0;
2013}
2014
2015int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2016{
2017 struct iscsi_conn *l_conn;
2018 struct iscsi_session *sess = conn->sess;
2019
2020 pr_debug("Received logout request CLOSECONNECTION for CID:"
2021 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2022
2023 /*
2024 * A Logout Request with a CLOSECONNECTION reason code for a CID
2025 * can arrive on a connection with a differing CID.
2026 */
2027 if (conn->cid == cmd->logout_cid) {
2028 spin_lock_bh(&conn->state_lock);
2029 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2030 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2031
2032 atomic_set(&conn->conn_logout_remove, 1);
2033 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2034 iscsit_inc_conn_usage_count(conn);
2035
2036 spin_unlock_bh(&conn->state_lock);
2037 } else {
2038 /*
2039 * Handle all different cid CLOSECONNECTION requests in
2040 * iscsit_logout_post_handler_diffcid() as to give enough
2041 * time for any non immediate command's CmdSN to be
2042 * acknowledged on the connection in question.
2043 *
2044 * Here we simply make sure the CID is still around.
2045 */
2046 l_conn = iscsit_get_conn_from_cid(sess,
2047 cmd->logout_cid);
2048 if (!l_conn) {
2049 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2050 iscsit_add_cmd_to_response_queue(cmd, conn,
2051 cmd->i_state);
2052 return 0;
2053 }
2054
2055 iscsit_dec_conn_usage_count(l_conn);
2056 }
2057
2058 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2059
2060 return 0;
2061}
2062
2063int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2064{
2065 struct iscsi_session *sess = conn->sess;
2066
2067 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2068 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2069
2070 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2071 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2072 " while ERL!=2.\n");
2073 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2074 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2075 return 0;
2076 }
2077
2078 if (conn->cid == cmd->logout_cid) {
2079 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2080 " with CID: %hu on CID: %hu, implementation error.\n",
2081 cmd->logout_cid, conn->cid);
2082 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2083 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2084 return 0;
2085 }
2086
2087 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2088
2089 return 0;
2090}
2091
2092static int iscsit_handle_logout_cmd(
2093 struct iscsi_conn *conn,
2094 unsigned char *buf)
2095{
2096 int cmdsn_ret, logout_remove = 0;
2097 u8 reason_code = 0;
2098 struct iscsi_cmd *cmd;
2099 struct iscsi_logout *hdr;
2100 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2101
2102 hdr = (struct iscsi_logout *) buf;
2103 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002104
2105 if (tiqn) {
2106 spin_lock(&tiqn->logout_stats.lock);
2107 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2108 tiqn->logout_stats.normal_logouts++;
2109 else
2110 tiqn->logout_stats.abnormal_logouts++;
2111 spin_unlock(&tiqn->logout_stats.lock);
2112 }
2113
2114 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2115 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2116 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2117 hdr->cid, conn->cid);
2118
2119 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2120 pr_err("Received logout request on connection that"
2121 " is not in logged in state, ignoring request.\n");
2122 return 0;
2123 }
2124
2125 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
2126 if (!cmd)
2127 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
2128 buf, conn);
2129
2130 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2131 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2132 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2133 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2134 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002135 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2136 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2137 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002138 cmd->logout_reason = reason_code;
2139 cmd->data_direction = DMA_NONE;
2140
2141 /*
2142 * We need to sleep in these cases (by returning 1) until the Logout
2143 * Response gets sent in the tx thread.
2144 */
2145 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2146 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002147 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002148 logout_remove = 1;
2149
2150 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002151 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002152 spin_unlock_bh(&conn->cmd_lock);
2153
2154 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002155 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002156
2157 /*
2158 * Immediate commands are executed, well, immediately.
2159 * Non-Immediate Logout Commands are executed in CmdSN order.
2160 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002161 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002162 int ret = iscsit_execute_cmd(cmd, 0);
2163
2164 if (ret < 0)
2165 return ret;
2166 } else {
2167 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2168 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2169 logout_remove = 0;
2170 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2171 return iscsit_add_reject_from_cmd(
2172 ISCSI_REASON_PROTOCOL_ERROR,
2173 1, 0, buf, cmd);
2174 }
2175 }
2176
2177 return logout_remove;
2178}
2179
2180static int iscsit_handle_snack(
2181 struct iscsi_conn *conn,
2182 unsigned char *buf)
2183{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002184 struct iscsi_snack *hdr;
2185
2186 hdr = (struct iscsi_snack *) buf;
2187 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002188
2189 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2190 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2191 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2192 hdr->begrun, hdr->runlength, conn->cid);
2193
2194 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2195 pr_err("Initiator sent SNACK request while in"
2196 " ErrorRecoveryLevel=0.\n");
2197 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2198 buf, conn);
2199 }
2200 /*
2201 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2202 * call from inside iscsi_send_recovery_datain_or_r2t().
2203 */
2204 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2205 case 0:
2206 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002207 hdr->itt,
2208 be32_to_cpu(hdr->ttt),
2209 be32_to_cpu(hdr->begrun),
2210 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002211 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002212 return iscsit_handle_status_snack(conn, hdr->itt,
2213 be32_to_cpu(hdr->ttt),
2214 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002215 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002216 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2217 be32_to_cpu(hdr->begrun),
2218 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002219 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2220 /* FIXME: Support R-Data SNACK */
2221 pr_err("R-Data SNACK Not Supported.\n");
2222 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2223 buf, conn);
2224 default:
2225 pr_err("Unknown SNACK type 0x%02x, protocol"
2226 " error.\n", hdr->flags & 0x0f);
2227 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2228 buf, conn);
2229 }
2230
2231 return 0;
2232}
2233
2234static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2235{
2236 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2237 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2238 wait_for_completion_interruptible_timeout(
2239 &conn->rx_half_close_comp,
2240 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2241 }
2242}
2243
2244static int iscsit_handle_immediate_data(
2245 struct iscsi_cmd *cmd,
2246 unsigned char *buf,
2247 u32 length)
2248{
2249 int iov_ret, rx_got = 0, rx_size = 0;
2250 u32 checksum, iov_count = 0, padding = 0;
2251 struct iscsi_conn *conn = cmd->conn;
2252 struct kvec *iov;
2253
2254 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2255 if (iov_ret < 0)
2256 return IMMEDIATE_DATA_CANNOT_RECOVER;
2257
2258 rx_size = length;
2259 iov_count = iov_ret;
2260 iov = &cmd->iov_data[0];
2261
2262 padding = ((-length) & 3);
2263 if (padding != 0) {
2264 iov[iov_count].iov_base = cmd->pad_bytes;
2265 iov[iov_count++].iov_len = padding;
2266 rx_size += padding;
2267 }
2268
2269 if (conn->conn_ops->DataDigest) {
2270 iov[iov_count].iov_base = &checksum;
2271 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2272 rx_size += ISCSI_CRC_LEN;
2273 }
2274
2275 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2276
2277 iscsit_unmap_iovec(cmd);
2278
2279 if (rx_got != rx_size) {
2280 iscsit_rx_thread_wait_for_tcp(conn);
2281 return IMMEDIATE_DATA_CANNOT_RECOVER;
2282 }
2283
2284 if (conn->conn_ops->DataDigest) {
2285 u32 data_crc;
2286
2287 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2288 cmd->write_data_done, length, padding,
2289 cmd->pad_bytes);
2290
2291 if (checksum != data_crc) {
2292 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2293 " does not match computed 0x%08x\n", checksum,
2294 data_crc);
2295
2296 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2297 pr_err("Unable to recover from"
2298 " Immediate Data digest failure while"
2299 " in ERL=0.\n");
2300 iscsit_add_reject_from_cmd(
2301 ISCSI_REASON_DATA_DIGEST_ERROR,
2302 1, 0, buf, cmd);
2303 return IMMEDIATE_DATA_CANNOT_RECOVER;
2304 } else {
2305 iscsit_add_reject_from_cmd(
2306 ISCSI_REASON_DATA_DIGEST_ERROR,
2307 0, 0, buf, cmd);
2308 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2309 }
2310 } else {
2311 pr_debug("Got CRC32C DataDigest 0x%08x for"
2312 " %u bytes of Immediate Data\n", checksum,
2313 length);
2314 }
2315 }
2316
2317 cmd->write_data_done += length;
2318
Andy Groverebf1d952012-04-03 15:51:24 -07002319 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002320 spin_lock_bh(&cmd->istate_lock);
2321 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2322 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2323 spin_unlock_bh(&cmd->istate_lock);
2324 }
2325
2326 return IMMEDIATE_DATA_NORMAL_OPERATION;
2327}
2328
2329/*
2330 * Called with sess->conn_lock held.
2331 */
2332/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2333 with active network interface */
2334static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2335{
2336 struct iscsi_cmd *cmd;
2337 struct iscsi_conn *conn_p;
2338
2339 /*
2340 * Only send a Asynchronous Message on connections whos network
2341 * interface is still functional.
2342 */
2343 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2344 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2345 iscsit_inc_conn_usage_count(conn_p);
2346 break;
2347 }
2348 }
2349
2350 if (!conn_p)
2351 return;
2352
Wei Yongjun3c989d72012-11-23 12:07:39 +08002353 cmd = iscsit_allocate_cmd(conn_p, GFP_ATOMIC);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002354 if (!cmd) {
2355 iscsit_dec_conn_usage_count(conn_p);
2356 return;
2357 }
2358
2359 cmd->logout_cid = conn->cid;
2360 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2361 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2362
2363 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002364 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002365 spin_unlock_bh(&conn_p->cmd_lock);
2366
2367 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2368 iscsit_dec_conn_usage_count(conn_p);
2369}
2370
2371static int iscsit_send_conn_drop_async_message(
2372 struct iscsi_cmd *cmd,
2373 struct iscsi_conn *conn)
2374{
2375 struct iscsi_async *hdr;
2376
2377 cmd->tx_size = ISCSI_HDR_LEN;
2378 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2379
2380 hdr = (struct iscsi_async *) cmd->pdu;
2381 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2382 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002383 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002384 cmd->targ_xfer_tag = 0xFFFFFFFF;
2385 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2386 cmd->stat_sn = conn->stat_sn++;
2387 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2388 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2389 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2390 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2391 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2392 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2393 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2394
2395 if (conn->conn_ops->HeaderDigest) {
2396 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2397
2398 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2399 (unsigned char *)hdr, ISCSI_HDR_LEN,
2400 0, NULL, (u8 *)header_digest);
2401
2402 cmd->tx_size += ISCSI_CRC_LEN;
2403 pr_debug("Attaching CRC32C HeaderDigest to"
2404 " Async Message 0x%08x\n", *header_digest);
2405 }
2406
2407 cmd->iov_misc[0].iov_base = cmd->pdu;
2408 cmd->iov_misc[0].iov_len = cmd->tx_size;
2409 cmd->iov_misc_count = 1;
2410
2411 pr_debug("Sending Connection Dropped Async Message StatSN:"
2412 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2413 cmd->logout_cid, conn->cid);
2414 return 0;
2415}
2416
Andy Grover6f3c0e62012-04-03 15:51:09 -07002417static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2418{
2419 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2420 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2421 wait_for_completion_interruptible_timeout(
2422 &conn->tx_half_close_comp,
2423 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2424 }
2425}
2426
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002427static int iscsit_send_data_in(
2428 struct iscsi_cmd *cmd,
Andy Grover6f3c0e62012-04-03 15:51:09 -07002429 struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002430{
2431 int iov_ret = 0, set_statsn = 0;
2432 u32 iov_count = 0, tx_size = 0;
2433 struct iscsi_datain datain;
2434 struct iscsi_datain_req *dr;
2435 struct iscsi_data_rsp *hdr;
2436 struct kvec *iov;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002437 int eodr = 0;
2438 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002439
2440 memset(&datain, 0, sizeof(struct iscsi_datain));
2441 dr = iscsit_get_datain_values(cmd, &datain);
2442 if (!dr) {
2443 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2444 cmd->init_task_tag);
2445 return -1;
2446 }
2447
2448 /*
2449 * Be paranoid and double check the logic for now.
2450 */
Andy Groverebf1d952012-04-03 15:51:24 -07002451 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002452 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2453 " datain.length: %u exceeds cmd->data_length: %u\n",
2454 cmd->init_task_tag, datain.offset, datain.length,
Andy Groverebf1d952012-04-03 15:51:24 -07002455 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002456 return -1;
2457 }
2458
2459 spin_lock_bh(&conn->sess->session_stats_lock);
2460 conn->sess->tx_data_octets += datain.length;
2461 if (conn->sess->se_sess->se_node_acl) {
2462 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2463 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2464 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2465 }
2466 spin_unlock_bh(&conn->sess->session_stats_lock);
2467 /*
2468 * Special case for successfully execution w/ both DATAIN
2469 * and Sense Data.
2470 */
2471 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2472 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2473 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2474 else {
2475 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2476 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2477 iscsit_increment_maxcmdsn(cmd, conn->sess);
2478 cmd->stat_sn = conn->stat_sn++;
2479 set_statsn = 1;
2480 } else if (dr->dr_complete ==
2481 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2482 set_statsn = 1;
2483 }
2484
2485 hdr = (struct iscsi_data_rsp *) cmd->pdu;
2486 memset(hdr, 0, ISCSI_HDR_LEN);
2487 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2488 hdr->flags = datain.flags;
2489 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2490 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2491 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002492 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002493 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2494 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002495 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002496 }
2497 }
2498 hton24(hdr->dlength, datain.length);
2499 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2500 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2501 (struct scsi_lun *)&hdr->lun);
2502 else
2503 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2504
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002505 hdr->itt = cmd->init_task_tag;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002506
2507 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2508 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2509 else
2510 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2511 if (set_statsn)
2512 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2513 else
2514 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2515
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002516 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2517 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2518 hdr->datasn = cpu_to_be32(datain.data_sn);
2519 hdr->offset = cpu_to_be32(datain.offset);
2520
2521 iov = &cmd->iov_data[0];
2522 iov[iov_count].iov_base = cmd->pdu;
2523 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2524 tx_size += ISCSI_HDR_LEN;
2525
2526 if (conn->conn_ops->HeaderDigest) {
2527 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2528
2529 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2530 (unsigned char *)hdr, ISCSI_HDR_LEN,
2531 0, NULL, (u8 *)header_digest);
2532
2533 iov[0].iov_len += ISCSI_CRC_LEN;
2534 tx_size += ISCSI_CRC_LEN;
2535
2536 pr_debug("Attaching CRC32 HeaderDigest"
2537 " for DataIN PDU 0x%08x\n", *header_digest);
2538 }
2539
2540 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], datain.offset, datain.length);
2541 if (iov_ret < 0)
2542 return -1;
2543
2544 iov_count += iov_ret;
2545 tx_size += datain.length;
2546
2547 cmd->padding = ((-datain.length) & 3);
2548 if (cmd->padding) {
2549 iov[iov_count].iov_base = cmd->pad_bytes;
2550 iov[iov_count++].iov_len = cmd->padding;
2551 tx_size += cmd->padding;
2552
2553 pr_debug("Attaching %u padding bytes\n",
2554 cmd->padding);
2555 }
2556 if (conn->conn_ops->DataDigest) {
2557 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2558 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2559
2560 iov[iov_count].iov_base = &cmd->data_crc;
2561 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2562 tx_size += ISCSI_CRC_LEN;
2563
2564 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2565 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2566 }
2567
2568 cmd->iov_data_count = iov_count;
2569 cmd->tx_size = tx_size;
2570
2571 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2572 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2573 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2574 ntohl(hdr->offset), datain.length, conn->cid);
2575
Andy Grover6f3c0e62012-04-03 15:51:09 -07002576 /* sendpage is preferred but can't insert markers */
2577 if (!conn->conn_ops->IFMarker)
2578 ret = iscsit_fe_sendpage_sg(cmd, conn);
2579 else
2580 ret = iscsit_send_tx_data(cmd, conn, 0);
2581
2582 iscsit_unmap_iovec(cmd);
2583
2584 if (ret < 0) {
2585 iscsit_tx_thread_wait_for_tcp(conn);
2586 return ret;
2587 }
2588
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002589 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002590 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002591 2 : 1;
2592 iscsit_free_datain_req(cmd, dr);
2593 }
2594
Andy Grover6f3c0e62012-04-03 15:51:09 -07002595 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002596}
2597
2598static int iscsit_send_logout_response(
2599 struct iscsi_cmd *cmd,
2600 struct iscsi_conn *conn)
2601{
2602 int niov = 0, tx_size;
2603 struct iscsi_conn *logout_conn = NULL;
2604 struct iscsi_conn_recovery *cr = NULL;
2605 struct iscsi_session *sess = conn->sess;
2606 struct kvec *iov;
2607 struct iscsi_logout_rsp *hdr;
2608 /*
2609 * The actual shutting down of Sessions and/or Connections
2610 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2611 * is done in scsi_logout_post_handler().
2612 */
2613 switch (cmd->logout_reason) {
2614 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2615 pr_debug("iSCSI session logout successful, setting"
2616 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2617 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2618 break;
2619 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2620 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2621 break;
2622 /*
2623 * For CLOSECONNECTION logout requests carrying
2624 * a matching logout CID -> local CID, the reference
2625 * for the local CID will have been incremented in
2626 * iscsi_logout_closeconnection().
2627 *
2628 * For CLOSECONNECTION logout requests carrying
2629 * a different CID than the connection it arrived
2630 * on, the connection responding to cmd->logout_cid
2631 * is stopped in iscsit_logout_post_handler_diffcid().
2632 */
2633
2634 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2635 " successful.\n", cmd->logout_cid, conn->cid);
2636 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2637 break;
2638 case ISCSI_LOGOUT_REASON_RECOVERY:
2639 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2640 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2641 break;
2642 /*
2643 * If the connection is still active from our point of view
2644 * force connection recovery to occur.
2645 */
2646 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2647 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002648 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002649 iscsit_connection_reinstatement_rcfr(logout_conn);
2650 iscsit_dec_conn_usage_count(logout_conn);
2651 }
2652
2653 cr = iscsit_get_inactive_connection_recovery_entry(
2654 conn->sess, cmd->logout_cid);
2655 if (!cr) {
2656 pr_err("Unable to locate CID: %hu for"
2657 " REMOVECONNFORRECOVERY Logout Request.\n",
2658 cmd->logout_cid);
2659 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2660 break;
2661 }
2662
2663 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2664
2665 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2666 " for recovery for CID: %hu on CID: %hu successful.\n",
2667 cmd->logout_cid, conn->cid);
2668 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2669 break;
2670 default:
2671 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2672 cmd->logout_reason);
2673 return -1;
2674 }
2675
2676 tx_size = ISCSI_HDR_LEN;
2677 hdr = (struct iscsi_logout_rsp *)cmd->pdu;
2678 memset(hdr, 0, ISCSI_HDR_LEN);
2679 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2680 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2681 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002682 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002683 cmd->stat_sn = conn->stat_sn++;
2684 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2685
2686 iscsit_increment_maxcmdsn(cmd, conn->sess);
2687 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2688 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2689
2690 iov = &cmd->iov_misc[0];
2691 iov[niov].iov_base = cmd->pdu;
2692 iov[niov++].iov_len = ISCSI_HDR_LEN;
2693
2694 if (conn->conn_ops->HeaderDigest) {
2695 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2696
2697 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2698 (unsigned char *)hdr, ISCSI_HDR_LEN,
2699 0, NULL, (u8 *)header_digest);
2700
2701 iov[0].iov_len += ISCSI_CRC_LEN;
2702 tx_size += ISCSI_CRC_LEN;
2703 pr_debug("Attaching CRC32C HeaderDigest to"
2704 " Logout Response 0x%08x\n", *header_digest);
2705 }
2706 cmd->iov_misc_count = niov;
2707 cmd->tx_size = tx_size;
2708
2709 pr_debug("Sending Logout Response ITT: 0x%08x StatSN:"
2710 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2711 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2712 cmd->logout_cid, conn->cid);
2713
2714 return 0;
2715}
2716
2717/*
2718 * Unsolicited NOPIN, either requesting a response or not.
2719 */
2720static int iscsit_send_unsolicited_nopin(
2721 struct iscsi_cmd *cmd,
2722 struct iscsi_conn *conn,
2723 int want_response)
2724{
2725 int tx_size = ISCSI_HDR_LEN;
2726 struct iscsi_nopin *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002727 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002728
2729 hdr = (struct iscsi_nopin *) cmd->pdu;
2730 memset(hdr, 0, ISCSI_HDR_LEN);
2731 hdr->opcode = ISCSI_OP_NOOP_IN;
2732 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002733 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002734 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2735 cmd->stat_sn = conn->stat_sn;
2736 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2737 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2738 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2739
2740 if (conn->conn_ops->HeaderDigest) {
2741 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2742
2743 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2744 (unsigned char *)hdr, ISCSI_HDR_LEN,
2745 0, NULL, (u8 *)header_digest);
2746
2747 tx_size += ISCSI_CRC_LEN;
2748 pr_debug("Attaching CRC32C HeaderDigest to"
2749 " NopIN 0x%08x\n", *header_digest);
2750 }
2751
2752 cmd->iov_misc[0].iov_base = cmd->pdu;
2753 cmd->iov_misc[0].iov_len = tx_size;
2754 cmd->iov_misc_count = 1;
2755 cmd->tx_size = tx_size;
2756
2757 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2758 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2759
Andy Grover6f3c0e62012-04-03 15:51:09 -07002760 ret = iscsit_send_tx_data(cmd, conn, 1);
2761 if (ret < 0) {
2762 iscsit_tx_thread_wait_for_tcp(conn);
2763 return ret;
2764 }
2765
2766 spin_lock_bh(&cmd->istate_lock);
2767 cmd->i_state = want_response ?
2768 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2769 spin_unlock_bh(&cmd->istate_lock);
2770
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002771 return 0;
2772}
2773
2774static int iscsit_send_nopin_response(
2775 struct iscsi_cmd *cmd,
2776 struct iscsi_conn *conn)
2777{
2778 int niov = 0, tx_size;
2779 u32 padding = 0;
2780 struct kvec *iov;
2781 struct iscsi_nopin *hdr;
2782
2783 tx_size = ISCSI_HDR_LEN;
2784 hdr = (struct iscsi_nopin *) cmd->pdu;
2785 memset(hdr, 0, ISCSI_HDR_LEN);
2786 hdr->opcode = ISCSI_OP_NOOP_IN;
2787 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2788 hton24(hdr->dlength, cmd->buf_ptr_size);
2789 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002790 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002791 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2792 cmd->stat_sn = conn->stat_sn++;
2793 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2794
2795 iscsit_increment_maxcmdsn(cmd, conn->sess);
2796 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2797 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2798
2799 iov = &cmd->iov_misc[0];
2800 iov[niov].iov_base = cmd->pdu;
2801 iov[niov++].iov_len = ISCSI_HDR_LEN;
2802
2803 if (conn->conn_ops->HeaderDigest) {
2804 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2805
2806 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2807 (unsigned char *)hdr, ISCSI_HDR_LEN,
2808 0, NULL, (u8 *)header_digest);
2809
2810 iov[0].iov_len += ISCSI_CRC_LEN;
2811 tx_size += ISCSI_CRC_LEN;
2812 pr_debug("Attaching CRC32C HeaderDigest"
2813 " to NopIn 0x%08x\n", *header_digest);
2814 }
2815
2816 /*
2817 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2818 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2819 */
2820 if (cmd->buf_ptr_size) {
2821 iov[niov].iov_base = cmd->buf_ptr;
2822 iov[niov++].iov_len = cmd->buf_ptr_size;
2823 tx_size += cmd->buf_ptr_size;
2824
2825 pr_debug("Echoing back %u bytes of ping"
2826 " data.\n", cmd->buf_ptr_size);
2827
2828 padding = ((-cmd->buf_ptr_size) & 3);
2829 if (padding != 0) {
2830 iov[niov].iov_base = &cmd->pad_bytes;
2831 iov[niov++].iov_len = padding;
2832 tx_size += padding;
2833 pr_debug("Attaching %u additional"
2834 " padding bytes.\n", padding);
2835 }
2836 if (conn->conn_ops->DataDigest) {
2837 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2838 cmd->buf_ptr, cmd->buf_ptr_size,
2839 padding, (u8 *)&cmd->pad_bytes,
2840 (u8 *)&cmd->data_crc);
2841
2842 iov[niov].iov_base = &cmd->data_crc;
2843 iov[niov++].iov_len = ISCSI_CRC_LEN;
2844 tx_size += ISCSI_CRC_LEN;
2845 pr_debug("Attached DataDigest for %u"
2846 " bytes of ping data, CRC 0x%08x\n",
2847 cmd->buf_ptr_size, cmd->data_crc);
2848 }
2849 }
2850
2851 cmd->iov_misc_count = niov;
2852 cmd->tx_size = tx_size;
2853
2854 pr_debug("Sending NOPIN Response ITT: 0x%08x, TTT:"
2855 " 0x%08x, StatSN: 0x%08x, Length %u\n", cmd->init_task_tag,
2856 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2857
2858 return 0;
2859}
2860
Andy Grover6f3c0e62012-04-03 15:51:09 -07002861static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002862 struct iscsi_cmd *cmd,
2863 struct iscsi_conn *conn)
2864{
2865 int tx_size = 0;
2866 struct iscsi_r2t *r2t;
2867 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002868 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002869
2870 r2t = iscsit_get_r2t_from_list(cmd);
2871 if (!r2t)
2872 return -1;
2873
2874 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
2875 memset(hdr, 0, ISCSI_HDR_LEN);
2876 hdr->opcode = ISCSI_OP_R2T;
2877 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2878 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2879 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002880 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002881 spin_lock_bh(&conn->sess->ttt_lock);
2882 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2883 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
2884 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2885 spin_unlock_bh(&conn->sess->ttt_lock);
2886 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
2887 hdr->statsn = cpu_to_be32(conn->stat_sn);
2888 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2889 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2890 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
2891 hdr->data_offset = cpu_to_be32(r2t->offset);
2892 hdr->data_length = cpu_to_be32(r2t->xfer_len);
2893
2894 cmd->iov_misc[0].iov_base = cmd->pdu;
2895 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
2896 tx_size += ISCSI_HDR_LEN;
2897
2898 if (conn->conn_ops->HeaderDigest) {
2899 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2900
2901 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2902 (unsigned char *)hdr, ISCSI_HDR_LEN,
2903 0, NULL, (u8 *)header_digest);
2904
2905 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
2906 tx_size += ISCSI_CRC_LEN;
2907 pr_debug("Attaching CRC32 HeaderDigest for R2T"
2908 " PDU 0x%08x\n", *header_digest);
2909 }
2910
2911 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
2912 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
2913 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
2914 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
2915 r2t->offset, r2t->xfer_len, conn->cid);
2916
2917 cmd->iov_misc_count = 1;
2918 cmd->tx_size = tx_size;
2919
2920 spin_lock_bh(&cmd->r2t_lock);
2921 r2t->sent_r2t = 1;
2922 spin_unlock_bh(&cmd->r2t_lock);
2923
Andy Grover6f3c0e62012-04-03 15:51:09 -07002924 ret = iscsit_send_tx_data(cmd, conn, 1);
2925 if (ret < 0) {
2926 iscsit_tx_thread_wait_for_tcp(conn);
2927 return ret;
2928 }
2929
2930 spin_lock_bh(&cmd->dataout_timeout_lock);
2931 iscsit_start_dataout_timer(cmd, conn);
2932 spin_unlock_bh(&cmd->dataout_timeout_lock);
2933
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002934 return 0;
2935}
2936
2937/*
Andy Grover8b1e1242012-04-03 15:51:12 -07002938 * @recovery: If called from iscsi_task_reassign_complete_write() for
2939 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002940 */
2941int iscsit_build_r2ts_for_cmd(
2942 struct iscsi_cmd *cmd,
2943 struct iscsi_conn *conn,
Andy Grover8b1e1242012-04-03 15:51:12 -07002944 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002945{
2946 int first_r2t = 1;
2947 u32 offset = 0, xfer_len = 0;
2948
2949 spin_lock_bh(&cmd->r2t_lock);
2950 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
2951 spin_unlock_bh(&cmd->r2t_lock);
2952 return 0;
2953 }
2954
Andy Grover8b1e1242012-04-03 15:51:12 -07002955 if (conn->sess->sess_ops->DataSequenceInOrder &&
2956 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07002957 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002958
2959 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
2960 if (conn->sess->sess_ops->DataSequenceInOrder) {
2961 offset = cmd->r2t_offset;
2962
Andy Grover8b1e1242012-04-03 15:51:12 -07002963 if (first_r2t && recovery) {
2964 int new_data_end = offset +
2965 conn->sess->sess_ops->MaxBurstLength -
2966 cmd->next_burst_len;
2967
Andy Groverebf1d952012-04-03 15:51:24 -07002968 if (new_data_end > cmd->se_cmd.data_length)
2969 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07002970 else
2971 xfer_len =
2972 conn->sess->sess_ops->MaxBurstLength -
2973 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002974 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07002975 int new_data_end = offset +
2976 conn->sess->sess_ops->MaxBurstLength;
2977
Andy Groverebf1d952012-04-03 15:51:24 -07002978 if (new_data_end > cmd->se_cmd.data_length)
2979 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07002980 else
2981 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002982 }
2983 cmd->r2t_offset += xfer_len;
2984
Andy Groverebf1d952012-04-03 15:51:24 -07002985 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002986 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
2987 } else {
2988 struct iscsi_seq *seq;
2989
2990 seq = iscsit_get_seq_holder_for_r2t(cmd);
2991 if (!seq) {
2992 spin_unlock_bh(&cmd->r2t_lock);
2993 return -1;
2994 }
2995
2996 offset = seq->offset;
2997 xfer_len = seq->xfer_len;
2998
2999 if (cmd->seq_send_order == cmd->seq_count)
3000 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3001 }
3002 cmd->outstanding_r2ts++;
3003 first_r2t = 0;
3004
3005 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3006 spin_unlock_bh(&cmd->r2t_lock);
3007 return -1;
3008 }
3009
3010 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3011 break;
3012 }
3013 spin_unlock_bh(&cmd->r2t_lock);
3014
3015 return 0;
3016}
3017
3018static int iscsit_send_status(
3019 struct iscsi_cmd *cmd,
3020 struct iscsi_conn *conn)
3021{
3022 u8 iov_count = 0, recovery;
3023 u32 padding = 0, tx_size = 0;
3024 struct iscsi_scsi_rsp *hdr;
3025 struct kvec *iov;
3026
3027 recovery = (cmd->i_state != ISTATE_SEND_STATUS);
3028 if (!recovery)
3029 cmd->stat_sn = conn->stat_sn++;
3030
3031 spin_lock_bh(&conn->sess->session_stats_lock);
3032 conn->sess->rsp_pdus++;
3033 spin_unlock_bh(&conn->sess->session_stats_lock);
3034
3035 hdr = (struct iscsi_scsi_rsp *) cmd->pdu;
3036 memset(hdr, 0, ISCSI_HDR_LEN);
3037 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3038 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3039 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3040 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003041 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003042 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3043 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003044 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003045 }
3046 hdr->response = cmd->iscsi_response;
3047 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003048 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003049 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3050
3051 iscsit_increment_maxcmdsn(cmd, conn->sess);
3052 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3053 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3054
3055 iov = &cmd->iov_misc[0];
3056 iov[iov_count].iov_base = cmd->pdu;
3057 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3058 tx_size += ISCSI_HDR_LEN;
3059
3060 /*
3061 * Attach SENSE DATA payload to iSCSI Response PDU
3062 */
3063 if (cmd->se_cmd.sense_buffer &&
3064 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3065 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003066 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3067 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3068
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003069 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003070 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003071 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003072 iov[iov_count++].iov_len =
3073 (cmd->se_cmd.scsi_sense_length + padding);
3074 tx_size += cmd->se_cmd.scsi_sense_length;
3075
3076 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003077 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003078 cmd->se_cmd.scsi_sense_length, 0, padding);
3079 tx_size += padding;
3080 pr_debug("Adding %u bytes of padding to"
3081 " SENSE.\n", padding);
3082 }
3083
3084 if (conn->conn_ops->DataDigest) {
3085 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003086 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003087 (cmd->se_cmd.scsi_sense_length + padding),
3088 0, NULL, (u8 *)&cmd->data_crc);
3089
3090 iov[iov_count].iov_base = &cmd->data_crc;
3091 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3092 tx_size += ISCSI_CRC_LEN;
3093
3094 pr_debug("Attaching CRC32 DataDigest for"
3095 " SENSE, %u bytes CRC 0x%08x\n",
3096 (cmd->se_cmd.scsi_sense_length + padding),
3097 cmd->data_crc);
3098 }
3099
3100 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3101 " Response PDU\n",
3102 cmd->se_cmd.scsi_sense_length);
3103 }
3104
3105 if (conn->conn_ops->HeaderDigest) {
3106 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3107
3108 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3109 (unsigned char *)hdr, ISCSI_HDR_LEN,
3110 0, NULL, (u8 *)header_digest);
3111
3112 iov[0].iov_len += ISCSI_CRC_LEN;
3113 tx_size += ISCSI_CRC_LEN;
3114 pr_debug("Attaching CRC32 HeaderDigest for Response"
3115 " PDU 0x%08x\n", *header_digest);
3116 }
3117
3118 cmd->iov_misc_count = iov_count;
3119 cmd->tx_size = tx_size;
3120
3121 pr_debug("Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3122 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3123 (!recovery) ? "" : "Recovery ", cmd->init_task_tag,
3124 cmd->stat_sn, 0x00, cmd->se_cmd.scsi_status, conn->cid);
3125
3126 return 0;
3127}
3128
3129static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3130{
3131 switch (se_tmr->response) {
3132 case TMR_FUNCTION_COMPLETE:
3133 return ISCSI_TMF_RSP_COMPLETE;
3134 case TMR_TASK_DOES_NOT_EXIST:
3135 return ISCSI_TMF_RSP_NO_TASK;
3136 case TMR_LUN_DOES_NOT_EXIST:
3137 return ISCSI_TMF_RSP_NO_LUN;
3138 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3139 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3140 case TMR_FUNCTION_AUTHORIZATION_FAILED:
3141 return ISCSI_TMF_RSP_AUTH_FAILED;
3142 case TMR_FUNCTION_REJECTED:
3143 default:
3144 return ISCSI_TMF_RSP_REJECTED;
3145 }
3146}
3147
3148static int iscsit_send_task_mgt_rsp(
3149 struct iscsi_cmd *cmd,
3150 struct iscsi_conn *conn)
3151{
3152 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3153 struct iscsi_tm_rsp *hdr;
3154 u32 tx_size = 0;
3155
3156 hdr = (struct iscsi_tm_rsp *) cmd->pdu;
3157 memset(hdr, 0, ISCSI_HDR_LEN);
3158 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003159 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003160 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003161 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003162 cmd->stat_sn = conn->stat_sn++;
3163 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3164
3165 iscsit_increment_maxcmdsn(cmd, conn->sess);
3166 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3167 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3168
3169 cmd->iov_misc[0].iov_base = cmd->pdu;
3170 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3171 tx_size += ISCSI_HDR_LEN;
3172
3173 if (conn->conn_ops->HeaderDigest) {
3174 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3175
3176 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3177 (unsigned char *)hdr, ISCSI_HDR_LEN,
3178 0, NULL, (u8 *)header_digest);
3179
3180 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3181 tx_size += ISCSI_CRC_LEN;
3182 pr_debug("Attaching CRC32 HeaderDigest for Task"
3183 " Mgmt Response PDU 0x%08x\n", *header_digest);
3184 }
3185
3186 cmd->iov_misc_count = 1;
3187 cmd->tx_size = tx_size;
3188
3189 pr_debug("Built Task Management Response ITT: 0x%08x,"
3190 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3191 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3192
3193 return 0;
3194}
3195
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003196static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3197{
3198 bool ret = false;
3199
3200 if (np->np_sockaddr.ss_family == AF_INET6) {
3201 const struct sockaddr_in6 sin6 = {
3202 .sin6_addr = IN6ADDR_ANY_INIT };
3203 struct sockaddr_in6 *sock_in6 =
3204 (struct sockaddr_in6 *)&np->np_sockaddr;
3205
3206 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3207 sin6.sin6_addr.s6_addr, 16))
3208 ret = true;
3209 } else {
3210 struct sockaddr_in * sock_in =
3211 (struct sockaddr_in *)&np->np_sockaddr;
3212
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003213 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003214 ret = true;
3215 }
3216
3217 return ret;
3218}
3219
Andy Grover8b1e1242012-04-03 15:51:12 -07003220#define SENDTARGETS_BUF_LIMIT 32768U
3221
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003222static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3223{
3224 char *payload = NULL;
3225 struct iscsi_conn *conn = cmd->conn;
3226 struct iscsi_portal_group *tpg;
3227 struct iscsi_tiqn *tiqn;
3228 struct iscsi_tpg_np *tpg_np;
3229 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003230 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003231
Andy Grover8b1e1242012-04-03 15:51:12 -07003232 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3233 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003234
3235 payload = kzalloc(buffer_len, GFP_KERNEL);
3236 if (!payload) {
3237 pr_err("Unable to allocate memory for sendtargets"
3238 " response.\n");
3239 return -ENOMEM;
3240 }
3241
3242 spin_lock(&tiqn_lock);
3243 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3244 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3245 len += 1;
3246
3247 if ((len + payload_len) > buffer_len) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003248 end_of_buf = 1;
3249 goto eob;
3250 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003251 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003252 payload_len += len;
3253
3254 spin_lock(&tiqn->tiqn_tpg_lock);
3255 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3256
3257 spin_lock(&tpg->tpg_state_lock);
3258 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3259 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3260 spin_unlock(&tpg->tpg_state_lock);
3261 continue;
3262 }
3263 spin_unlock(&tpg->tpg_state_lock);
3264
3265 spin_lock(&tpg->tpg_np_lock);
3266 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3267 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003268 struct iscsi_np *np = tpg_np->tpg_np;
3269 bool inaddr_any = iscsit_check_inaddr_any(np);
3270
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003271 len = sprintf(buf, "TargetAddress="
3272 "%s%s%s:%hu,%hu",
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003273 (np->np_sockaddr.ss_family == AF_INET6) ?
3274 "[" : "", (inaddr_any == false) ?
3275 np->np_ip : conn->local_ip,
3276 (np->np_sockaddr.ss_family == AF_INET6) ?
3277 "]" : "", (inaddr_any == false) ?
3278 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003279 tpg->tpgt);
3280 len += 1;
3281
3282 if ((len + payload_len) > buffer_len) {
3283 spin_unlock(&tpg->tpg_np_lock);
3284 spin_unlock(&tiqn->tiqn_tpg_lock);
3285 end_of_buf = 1;
3286 goto eob;
3287 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003288 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003289 payload_len += len;
3290 }
3291 spin_unlock(&tpg->tpg_np_lock);
3292 }
3293 spin_unlock(&tiqn->tiqn_tpg_lock);
3294eob:
3295 if (end_of_buf)
3296 break;
3297 }
3298 spin_unlock(&tiqn_lock);
3299
3300 cmd->buf_ptr = payload;
3301
3302 return payload_len;
3303}
3304
3305/*
3306 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3307 * MaxRecvDataSegmentLength.
3308 */
3309static int iscsit_send_text_rsp(
3310 struct iscsi_cmd *cmd,
3311 struct iscsi_conn *conn)
3312{
3313 struct iscsi_text_rsp *hdr;
3314 struct kvec *iov;
3315 u32 padding = 0, tx_size = 0;
3316 int text_length, iov_count = 0;
3317
3318 text_length = iscsit_build_sendtargets_response(cmd);
3319 if (text_length < 0)
3320 return text_length;
3321
3322 padding = ((-text_length) & 3);
3323 if (padding != 0) {
3324 memset(cmd->buf_ptr + text_length, 0, padding);
3325 pr_debug("Attaching %u additional bytes for"
3326 " padding.\n", padding);
3327 }
3328
3329 hdr = (struct iscsi_text_rsp *) cmd->pdu;
3330 memset(hdr, 0, ISCSI_HDR_LEN);
3331 hdr->opcode = ISCSI_OP_TEXT_RSP;
3332 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3333 hton24(hdr->dlength, text_length);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003334 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003335 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3336 cmd->stat_sn = conn->stat_sn++;
3337 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3338
3339 iscsit_increment_maxcmdsn(cmd, conn->sess);
3340 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3341 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3342
3343 iov = &cmd->iov_misc[0];
3344
3345 iov[iov_count].iov_base = cmd->pdu;
3346 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3347 iov[iov_count].iov_base = cmd->buf_ptr;
3348 iov[iov_count++].iov_len = text_length + padding;
3349
3350 tx_size += (ISCSI_HDR_LEN + text_length + padding);
3351
3352 if (conn->conn_ops->HeaderDigest) {
3353 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3354
3355 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3356 (unsigned char *)hdr, ISCSI_HDR_LEN,
3357 0, NULL, (u8 *)header_digest);
3358
3359 iov[0].iov_len += ISCSI_CRC_LEN;
3360 tx_size += ISCSI_CRC_LEN;
3361 pr_debug("Attaching CRC32 HeaderDigest for"
3362 " Text Response PDU 0x%08x\n", *header_digest);
3363 }
3364
3365 if (conn->conn_ops->DataDigest) {
3366 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3367 cmd->buf_ptr, (text_length + padding),
3368 0, NULL, (u8 *)&cmd->data_crc);
3369
3370 iov[iov_count].iov_base = &cmd->data_crc;
3371 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3372 tx_size += ISCSI_CRC_LEN;
3373
3374 pr_debug("Attaching DataDigest for %u bytes of text"
3375 " data, CRC 0x%08x\n", (text_length + padding),
3376 cmd->data_crc);
3377 }
3378
3379 cmd->iov_misc_count = iov_count;
3380 cmd->tx_size = tx_size;
3381
3382 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3383 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3384 text_length, conn->cid);
3385 return 0;
3386}
3387
3388static int iscsit_send_reject(
3389 struct iscsi_cmd *cmd,
3390 struct iscsi_conn *conn)
3391{
3392 u32 iov_count = 0, tx_size = 0;
3393 struct iscsi_reject *hdr;
3394 struct kvec *iov;
3395
3396 hdr = (struct iscsi_reject *) cmd->pdu;
3397 hdr->opcode = ISCSI_OP_REJECT;
3398 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3399 hton24(hdr->dlength, ISCSI_HDR_LEN);
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003400 hdr->ffffffff = cpu_to_be32(0xffffffff);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003401 cmd->stat_sn = conn->stat_sn++;
3402 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3403 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3404 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3405
3406 iov = &cmd->iov_misc[0];
3407
3408 iov[iov_count].iov_base = cmd->pdu;
3409 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3410 iov[iov_count].iov_base = cmd->buf_ptr;
3411 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3412
3413 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3414
3415 if (conn->conn_ops->HeaderDigest) {
3416 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3417
3418 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3419 (unsigned char *)hdr, ISCSI_HDR_LEN,
3420 0, NULL, (u8 *)header_digest);
3421
3422 iov[0].iov_len += ISCSI_CRC_LEN;
3423 tx_size += ISCSI_CRC_LEN;
3424 pr_debug("Attaching CRC32 HeaderDigest for"
3425 " REJECT PDU 0x%08x\n", *header_digest);
3426 }
3427
3428 if (conn->conn_ops->DataDigest) {
3429 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3430 (unsigned char *)cmd->buf_ptr, ISCSI_HDR_LEN,
3431 0, NULL, (u8 *)&cmd->data_crc);
3432
3433 iov[iov_count].iov_base = &cmd->data_crc;
3434 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3435 tx_size += ISCSI_CRC_LEN;
3436 pr_debug("Attaching CRC32 DataDigest for REJECT"
3437 " PDU 0x%08x\n", cmd->data_crc);
3438 }
3439
3440 cmd->iov_misc_count = iov_count;
3441 cmd->tx_size = tx_size;
3442
3443 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3444 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3445
3446 return 0;
3447}
3448
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003449void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3450{
3451 struct iscsi_thread_set *ts = conn->thread_set;
3452 int ord, cpu;
3453 /*
3454 * thread_id is assigned from iscsit_global->ts_bitmap from
3455 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3456 *
3457 * Here we use thread_id to determine which CPU that this
3458 * iSCSI connection's iscsi_thread_set will be scheduled to
3459 * execute upon.
3460 */
3461 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003462 for_each_online_cpu(cpu) {
3463 if (ord-- == 0) {
3464 cpumask_set_cpu(cpu, conn->conn_cpumask);
3465 return;
3466 }
3467 }
3468 /*
3469 * This should never be reached..
3470 */
3471 dump_stack();
3472 cpumask_setall(conn->conn_cpumask);
3473}
3474
3475static inline void iscsit_thread_check_cpumask(
3476 struct iscsi_conn *conn,
3477 struct task_struct *p,
3478 int mode)
3479{
3480 char buf[128];
3481 /*
3482 * mode == 1 signals iscsi_target_tx_thread() usage.
3483 * mode == 0 signals iscsi_target_rx_thread() usage.
3484 */
3485 if (mode == 1) {
3486 if (!conn->conn_tx_reset_cpumask)
3487 return;
3488 conn->conn_tx_reset_cpumask = 0;
3489 } else {
3490 if (!conn->conn_rx_reset_cpumask)
3491 return;
3492 conn->conn_rx_reset_cpumask = 0;
3493 }
3494 /*
3495 * Update the CPU mask for this single kthread so that
3496 * both TX and RX kthreads are scheduled to run on the
3497 * same CPU.
3498 */
3499 memset(buf, 0, 128);
3500 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003501 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3502}
3503
Andy Grover6f3c0e62012-04-03 15:51:09 -07003504static int handle_immediate_queue(struct iscsi_conn *conn)
3505{
3506 struct iscsi_queue_req *qr;
3507 struct iscsi_cmd *cmd;
3508 u8 state;
3509 int ret;
3510
3511 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3512 atomic_set(&conn->check_immediate_queue, 0);
3513 cmd = qr->cmd;
3514 state = qr->state;
3515 kmem_cache_free(lio_qr_cache, qr);
3516
3517 switch (state) {
3518 case ISTATE_SEND_R2T:
3519 ret = iscsit_send_r2t(cmd, conn);
3520 if (ret < 0)
3521 goto err;
3522 break;
3523 case ISTATE_REMOVE:
3524 if (cmd->data_direction == DMA_TO_DEVICE)
3525 iscsit_stop_dataout_timer(cmd);
3526
3527 spin_lock_bh(&conn->cmd_lock);
3528 list_del(&cmd->i_conn_node);
3529 spin_unlock_bh(&conn->cmd_lock);
3530
3531 iscsit_free_cmd(cmd);
3532 continue;
3533 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3534 iscsit_mod_nopin_response_timer(conn);
3535 ret = iscsit_send_unsolicited_nopin(cmd,
3536 conn, 1);
3537 if (ret < 0)
3538 goto err;
3539 break;
3540 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3541 ret = iscsit_send_unsolicited_nopin(cmd,
3542 conn, 0);
3543 if (ret < 0)
3544 goto err;
3545 break;
3546 default:
3547 pr_err("Unknown Opcode: 0x%02x ITT:"
3548 " 0x%08x, i_state: %d on CID: %hu\n",
3549 cmd->iscsi_opcode, cmd->init_task_tag, state,
3550 conn->cid);
3551 goto err;
3552 }
3553 }
3554
3555 return 0;
3556
3557err:
3558 return -1;
3559}
3560
3561static int handle_response_queue(struct iscsi_conn *conn)
3562{
3563 struct iscsi_queue_req *qr;
3564 struct iscsi_cmd *cmd;
3565 u8 state;
3566 int ret;
3567
3568 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3569 cmd = qr->cmd;
3570 state = qr->state;
3571 kmem_cache_free(lio_qr_cache, qr);
3572
3573check_rsp_state:
3574 switch (state) {
3575 case ISTATE_SEND_DATAIN:
3576 ret = iscsit_send_data_in(cmd, conn);
3577 if (ret < 0)
3578 goto err;
3579 else if (!ret)
3580 /* more drs */
3581 goto check_rsp_state;
3582 else if (ret == 1) {
3583 /* all done */
3584 spin_lock_bh(&cmd->istate_lock);
3585 cmd->i_state = ISTATE_SENT_STATUS;
3586 spin_unlock_bh(&cmd->istate_lock);
3587 continue;
3588 } else if (ret == 2) {
3589 /* Still must send status,
3590 SCF_TRANSPORT_TASK_SENSE was set */
3591 spin_lock_bh(&cmd->istate_lock);
3592 cmd->i_state = ISTATE_SEND_STATUS;
3593 spin_unlock_bh(&cmd->istate_lock);
3594 state = ISTATE_SEND_STATUS;
3595 goto check_rsp_state;
3596 }
3597
3598 break;
3599 case ISTATE_SEND_STATUS:
3600 case ISTATE_SEND_STATUS_RECOVERY:
3601 ret = iscsit_send_status(cmd, conn);
3602 break;
3603 case ISTATE_SEND_LOGOUTRSP:
3604 ret = iscsit_send_logout_response(cmd, conn);
3605 break;
3606 case ISTATE_SEND_ASYNCMSG:
3607 ret = iscsit_send_conn_drop_async_message(
3608 cmd, conn);
3609 break;
3610 case ISTATE_SEND_NOPIN:
3611 ret = iscsit_send_nopin_response(cmd, conn);
3612 break;
3613 case ISTATE_SEND_REJECT:
3614 ret = iscsit_send_reject(cmd, conn);
3615 break;
3616 case ISTATE_SEND_TASKMGTRSP:
3617 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3618 if (ret != 0)
3619 break;
3620 ret = iscsit_tmr_post_handler(cmd, conn);
3621 if (ret != 0)
3622 iscsit_fall_back_to_erl0(conn->sess);
3623 break;
3624 case ISTATE_SEND_TEXTRSP:
3625 ret = iscsit_send_text_rsp(cmd, conn);
3626 break;
3627 default:
3628 pr_err("Unknown Opcode: 0x%02x ITT:"
3629 " 0x%08x, i_state: %d on CID: %hu\n",
3630 cmd->iscsi_opcode, cmd->init_task_tag,
3631 state, conn->cid);
3632 goto err;
3633 }
3634 if (ret < 0)
3635 goto err;
3636
3637 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3638 iscsit_tx_thread_wait_for_tcp(conn);
3639 iscsit_unmap_iovec(cmd);
3640 goto err;
3641 }
3642 iscsit_unmap_iovec(cmd);
3643
3644 switch (state) {
3645 case ISTATE_SEND_LOGOUTRSP:
3646 if (!iscsit_logout_post_handler(cmd, conn))
3647 goto restart;
3648 /* fall through */
3649 case ISTATE_SEND_STATUS:
3650 case ISTATE_SEND_ASYNCMSG:
3651 case ISTATE_SEND_NOPIN:
3652 case ISTATE_SEND_STATUS_RECOVERY:
3653 case ISTATE_SEND_TEXTRSP:
3654 case ISTATE_SEND_TASKMGTRSP:
3655 spin_lock_bh(&cmd->istate_lock);
3656 cmd->i_state = ISTATE_SENT_STATUS;
3657 spin_unlock_bh(&cmd->istate_lock);
3658 break;
3659 case ISTATE_SEND_REJECT:
3660 if (cmd->cmd_flags & ICF_REJECT_FAIL_CONN) {
3661 cmd->cmd_flags &= ~ICF_REJECT_FAIL_CONN;
3662 complete(&cmd->reject_comp);
3663 goto err;
3664 }
3665 complete(&cmd->reject_comp);
3666 break;
3667 default:
3668 pr_err("Unknown Opcode: 0x%02x ITT:"
3669 " 0x%08x, i_state: %d on CID: %hu\n",
3670 cmd->iscsi_opcode, cmd->init_task_tag,
3671 cmd->i_state, conn->cid);
3672 goto err;
3673 }
3674
3675 if (atomic_read(&conn->check_immediate_queue))
3676 break;
3677 }
3678
3679 return 0;
3680
3681err:
3682 return -1;
3683restart:
3684 return -EAGAIN;
3685}
3686
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003687int iscsi_target_tx_thread(void *arg)
3688{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003689 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003690 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003691 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003692 /*
3693 * Allow ourselves to be interrupted by SIGINT so that a
3694 * connection recovery / failure event can be triggered externally.
3695 */
3696 allow_signal(SIGINT);
3697
3698restart:
3699 conn = iscsi_tx_thread_pre_handler(ts);
3700 if (!conn)
3701 goto out;
3702
Andy Grover6f3c0e62012-04-03 15:51:09 -07003703 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003704
3705 while (!kthread_should_stop()) {
3706 /*
3707 * Ensure that both TX and RX per connection kthreads
3708 * are scheduled to run on the same CPU.
3709 */
3710 iscsit_thread_check_cpumask(conn, current, 1);
3711
Roland Dreierd5627ac2012-10-31 09:16:46 -07003712 wait_event_interruptible(conn->queues_wq,
3713 !iscsit_conn_all_queues_empty(conn) ||
3714 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003715
3716 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3717 signal_pending(current))
3718 goto transport_err;
3719
Andy Grover6f3c0e62012-04-03 15:51:09 -07003720 ret = handle_immediate_queue(conn);
3721 if (ret < 0)
3722 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003723
Andy Grover6f3c0e62012-04-03 15:51:09 -07003724 ret = handle_response_queue(conn);
3725 if (ret == -EAGAIN)
3726 goto restart;
3727 else if (ret < 0)
3728 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003729 }
3730
3731transport_err:
3732 iscsit_take_action_for_connection_exit(conn);
3733 goto restart;
3734out:
3735 return 0;
3736}
3737
3738int iscsi_target_rx_thread(void *arg)
3739{
3740 int ret;
3741 u8 buffer[ISCSI_HDR_LEN], opcode;
3742 u32 checksum = 0, digest = 0;
3743 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01003744 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003745 struct kvec iov;
3746 /*
3747 * Allow ourselves to be interrupted by SIGINT so that a
3748 * connection recovery / failure event can be triggered externally.
3749 */
3750 allow_signal(SIGINT);
3751
3752restart:
3753 conn = iscsi_rx_thread_pre_handler(ts);
3754 if (!conn)
3755 goto out;
3756
3757 while (!kthread_should_stop()) {
3758 /*
3759 * Ensure that both TX and RX per connection kthreads
3760 * are scheduled to run on the same CPU.
3761 */
3762 iscsit_thread_check_cpumask(conn, current, 0);
3763
3764 memset(buffer, 0, ISCSI_HDR_LEN);
3765 memset(&iov, 0, sizeof(struct kvec));
3766
3767 iov.iov_base = buffer;
3768 iov.iov_len = ISCSI_HDR_LEN;
3769
3770 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3771 if (ret != ISCSI_HDR_LEN) {
3772 iscsit_rx_thread_wait_for_tcp(conn);
3773 goto transport_err;
3774 }
3775
3776 /*
3777 * Set conn->bad_hdr for use with REJECT PDUs.
3778 */
3779 memcpy(&conn->bad_hdr, &buffer, ISCSI_HDR_LEN);
3780
3781 if (conn->conn_ops->HeaderDigest) {
3782 iov.iov_base = &digest;
3783 iov.iov_len = ISCSI_CRC_LEN;
3784
3785 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3786 if (ret != ISCSI_CRC_LEN) {
3787 iscsit_rx_thread_wait_for_tcp(conn);
3788 goto transport_err;
3789 }
3790
3791 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
3792 buffer, ISCSI_HDR_LEN,
3793 0, NULL, (u8 *)&checksum);
3794
3795 if (digest != checksum) {
3796 pr_err("HeaderDigest CRC32C failed,"
3797 " received 0x%08x, computed 0x%08x\n",
3798 digest, checksum);
3799 /*
3800 * Set the PDU to 0xff so it will intentionally
3801 * hit default in the switch below.
3802 */
3803 memset(buffer, 0xff, ISCSI_HDR_LEN);
3804 spin_lock_bh(&conn->sess->session_stats_lock);
3805 conn->sess->conn_digest_errors++;
3806 spin_unlock_bh(&conn->sess->session_stats_lock);
3807 } else {
3808 pr_debug("Got HeaderDigest CRC32C"
3809 " 0x%08x\n", checksum);
3810 }
3811 }
3812
3813 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
3814 goto transport_err;
3815
3816 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3817
3818 if (conn->sess->sess_ops->SessionType &&
3819 ((!(opcode & ISCSI_OP_TEXT)) ||
3820 (!(opcode & ISCSI_OP_LOGOUT)))) {
3821 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3822 " while in Discovery Session, rejecting.\n", opcode);
3823 iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
3824 buffer, conn);
3825 goto transport_err;
3826 }
3827
3828 switch (opcode) {
3829 case ISCSI_OP_SCSI_CMD:
3830 if (iscsit_handle_scsi_cmd(conn, buffer) < 0)
3831 goto transport_err;
3832 break;
3833 case ISCSI_OP_SCSI_DATA_OUT:
3834 if (iscsit_handle_data_out(conn, buffer) < 0)
3835 goto transport_err;
3836 break;
3837 case ISCSI_OP_NOOP_OUT:
3838 if (iscsit_handle_nop_out(conn, buffer) < 0)
3839 goto transport_err;
3840 break;
3841 case ISCSI_OP_SCSI_TMFUNC:
3842 if (iscsit_handle_task_mgt_cmd(conn, buffer) < 0)
3843 goto transport_err;
3844 break;
3845 case ISCSI_OP_TEXT:
3846 if (iscsit_handle_text_cmd(conn, buffer) < 0)
3847 goto transport_err;
3848 break;
3849 case ISCSI_OP_LOGOUT:
3850 ret = iscsit_handle_logout_cmd(conn, buffer);
3851 if (ret > 0) {
3852 wait_for_completion_timeout(&conn->conn_logout_comp,
3853 SECONDS_FOR_LOGOUT_COMP * HZ);
3854 goto transport_err;
3855 } else if (ret < 0)
3856 goto transport_err;
3857 break;
3858 case ISCSI_OP_SNACK:
3859 if (iscsit_handle_snack(conn, buffer) < 0)
3860 goto transport_err;
3861 break;
3862 default:
3863 pr_err("Got unknown iSCSI OpCode: 0x%02x\n",
3864 opcode);
3865 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3866 pr_err("Cannot recover from unknown"
3867 " opcode while ERL=0, closing iSCSI connection"
3868 ".\n");
3869 goto transport_err;
3870 }
3871 if (!conn->conn_ops->OFMarker) {
3872 pr_err("Unable to recover from unknown"
3873 " opcode while OFMarker=No, closing iSCSI"
3874 " connection.\n");
3875 goto transport_err;
3876 }
3877 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
3878 pr_err("Unable to recover from unknown"
3879 " opcode, closing iSCSI connection.\n");
3880 goto transport_err;
3881 }
3882 break;
3883 }
3884 }
3885
3886transport_err:
3887 if (!signal_pending(current))
3888 atomic_set(&conn->transport_failed, 1);
3889 iscsit_take_action_for_connection_exit(conn);
3890 goto restart;
3891out:
3892 return 0;
3893}
3894
3895static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
3896{
3897 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
3898 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003899 /*
3900 * We expect this function to only ever be called from either RX or TX
3901 * thread context via iscsit_close_connection() once the other context
3902 * has been reset -> returned sleeping pre-handler state.
3903 */
3904 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003905 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003906
Andy Grover2fbb4712012-04-03 15:51:01 -07003907 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003908 spin_unlock_bh(&conn->cmd_lock);
3909
3910 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003911
Nicholas Bellingerd2701902011-10-09 01:48:14 -07003912 iscsit_free_cmd(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003913
3914 spin_lock_bh(&conn->cmd_lock);
3915 }
3916 spin_unlock_bh(&conn->cmd_lock);
3917}
3918
3919static void iscsit_stop_timers_for_cmds(
3920 struct iscsi_conn *conn)
3921{
3922 struct iscsi_cmd *cmd;
3923
3924 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003925 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003926 if (cmd->data_direction == DMA_TO_DEVICE)
3927 iscsit_stop_dataout_timer(cmd);
3928 }
3929 spin_unlock_bh(&conn->cmd_lock);
3930}
3931
3932int iscsit_close_connection(
3933 struct iscsi_conn *conn)
3934{
3935 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
3936 struct iscsi_session *sess = conn->sess;
3937
3938 pr_debug("Closing iSCSI connection CID %hu on SID:"
3939 " %u\n", conn->cid, sess->sid);
3940 /*
3941 * Always up conn_logout_comp just in case the RX Thread is sleeping
3942 * and the logout response never got sent because the connection
3943 * failed.
3944 */
3945 complete(&conn->conn_logout_comp);
3946
3947 iscsi_release_thread_set(conn);
3948
3949 iscsit_stop_timers_for_cmds(conn);
3950 iscsit_stop_nopin_response_timer(conn);
3951 iscsit_stop_nopin_timer(conn);
3952 iscsit_free_queue_reqs_for_conn(conn);
3953
3954 /*
3955 * During Connection recovery drop unacknowledged out of order
3956 * commands for this connection, and prepare the other commands
3957 * for realligence.
3958 *
3959 * During normal operation clear the out of order commands (but
3960 * do not free the struct iscsi_ooo_cmdsn's) and release all
3961 * struct iscsi_cmds.
3962 */
3963 if (atomic_read(&conn->connection_recovery)) {
3964 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
3965 iscsit_prepare_cmds_for_realligance(conn);
3966 } else {
3967 iscsit_clear_ooo_cmdsns_for_conn(conn);
3968 iscsit_release_commands_from_conn(conn);
3969 }
3970
3971 /*
3972 * Handle decrementing session or connection usage count if
3973 * a logout response was not able to be sent because the
3974 * connection failed. Fall back to Session Recovery here.
3975 */
3976 if (atomic_read(&conn->conn_logout_remove)) {
3977 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
3978 iscsit_dec_conn_usage_count(conn);
3979 iscsit_dec_session_usage_count(sess);
3980 }
3981 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
3982 iscsit_dec_conn_usage_count(conn);
3983
3984 atomic_set(&conn->conn_logout_remove, 0);
3985 atomic_set(&sess->session_reinstatement, 0);
3986 atomic_set(&sess->session_fall_back_to_erl0, 1);
3987 }
3988
3989 spin_lock_bh(&sess->conn_lock);
3990 list_del(&conn->conn_list);
3991
3992 /*
3993 * Attempt to let the Initiator know this connection failed by
3994 * sending an Connection Dropped Async Message on another
3995 * active connection.
3996 */
3997 if (atomic_read(&conn->connection_recovery))
3998 iscsit_build_conn_drop_async_message(conn);
3999
4000 spin_unlock_bh(&sess->conn_lock);
4001
4002 /*
4003 * If connection reinstatement is being performed on this connection,
4004 * up the connection reinstatement semaphore that is being blocked on
4005 * in iscsit_cause_connection_reinstatement().
4006 */
4007 spin_lock_bh(&conn->state_lock);
4008 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4009 spin_unlock_bh(&conn->state_lock);
4010 complete(&conn->conn_wait_comp);
4011 wait_for_completion(&conn->conn_post_wait_comp);
4012 spin_lock_bh(&conn->state_lock);
4013 }
4014
4015 /*
4016 * If connection reinstatement is being performed on this connection
4017 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4018 * connection wait rcfr semaphore that is being blocked on
4019 * an iscsit_connection_reinstatement_rcfr().
4020 */
4021 if (atomic_read(&conn->connection_wait_rcfr)) {
4022 spin_unlock_bh(&conn->state_lock);
4023 complete(&conn->conn_wait_rcfr_comp);
4024 wait_for_completion(&conn->conn_post_wait_comp);
4025 spin_lock_bh(&conn->state_lock);
4026 }
4027 atomic_set(&conn->connection_reinstatement, 1);
4028 spin_unlock_bh(&conn->state_lock);
4029
4030 /*
4031 * If any other processes are accessing this connection pointer we
4032 * must wait until they have completed.
4033 */
4034 iscsit_check_conn_usage_count(conn);
4035
4036 if (conn->conn_rx_hash.tfm)
4037 crypto_free_hash(conn->conn_rx_hash.tfm);
4038 if (conn->conn_tx_hash.tfm)
4039 crypto_free_hash(conn->conn_tx_hash.tfm);
4040
4041 if (conn->conn_cpumask)
4042 free_cpumask_var(conn->conn_cpumask);
4043
4044 kfree(conn->conn_ops);
4045 conn->conn_ops = NULL;
4046
Al Virobf6932f2012-07-21 08:55:18 +01004047 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004048 sock_release(conn->sock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004049 conn->thread_set = NULL;
4050
4051 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4052 conn->conn_state = TARG_CONN_STATE_FREE;
4053 kfree(conn);
4054
4055 spin_lock_bh(&sess->conn_lock);
4056 atomic_dec(&sess->nconn);
4057 pr_debug("Decremented iSCSI connection count to %hu from node:"
4058 " %s\n", atomic_read(&sess->nconn),
4059 sess->sess_ops->InitiatorName);
4060 /*
4061 * Make sure that if one connection fails in an non ERL=2 iSCSI
4062 * Session that they all fail.
4063 */
4064 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4065 !atomic_read(&sess->session_logout))
4066 atomic_set(&sess->session_fall_back_to_erl0, 1);
4067
4068 /*
4069 * If this was not the last connection in the session, and we are
4070 * performing session reinstatement or falling back to ERL=0, call
4071 * iscsit_stop_session() without sleeping to shutdown the other
4072 * active connections.
4073 */
4074 if (atomic_read(&sess->nconn)) {
4075 if (!atomic_read(&sess->session_reinstatement) &&
4076 !atomic_read(&sess->session_fall_back_to_erl0)) {
4077 spin_unlock_bh(&sess->conn_lock);
4078 return 0;
4079 }
4080 if (!atomic_read(&sess->session_stop_active)) {
4081 atomic_set(&sess->session_stop_active, 1);
4082 spin_unlock_bh(&sess->conn_lock);
4083 iscsit_stop_session(sess, 0, 0);
4084 return 0;
4085 }
4086 spin_unlock_bh(&sess->conn_lock);
4087 return 0;
4088 }
4089
4090 /*
4091 * If this was the last connection in the session and one of the
4092 * following is occurring:
4093 *
4094 * Session Reinstatement is not being performed, and are falling back
4095 * to ERL=0 call iscsit_close_session().
4096 *
4097 * Session Logout was requested. iscsit_close_session() will be called
4098 * elsewhere.
4099 *
4100 * Session Continuation is not being performed, start the Time2Retain
4101 * handler and check if sleep_on_sess_wait_sem is active.
4102 */
4103 if (!atomic_read(&sess->session_reinstatement) &&
4104 atomic_read(&sess->session_fall_back_to_erl0)) {
4105 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004106 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004107
4108 return 0;
4109 } else if (atomic_read(&sess->session_logout)) {
4110 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4111 sess->session_state = TARG_SESS_STATE_FREE;
4112 spin_unlock_bh(&sess->conn_lock);
4113
4114 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4115 complete(&sess->session_wait_comp);
4116
4117 return 0;
4118 } else {
4119 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4120 sess->session_state = TARG_SESS_STATE_FAILED;
4121
4122 if (!atomic_read(&sess->session_continuation)) {
4123 spin_unlock_bh(&sess->conn_lock);
4124 iscsit_start_time2retain_handler(sess);
4125 } else
4126 spin_unlock_bh(&sess->conn_lock);
4127
4128 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4129 complete(&sess->session_wait_comp);
4130
4131 return 0;
4132 }
4133 spin_unlock_bh(&sess->conn_lock);
4134
4135 return 0;
4136}
4137
4138int iscsit_close_session(struct iscsi_session *sess)
4139{
4140 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4141 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4142
4143 if (atomic_read(&sess->nconn)) {
4144 pr_err("%d connection(s) still exist for iSCSI session"
4145 " to %s\n", atomic_read(&sess->nconn),
4146 sess->sess_ops->InitiatorName);
4147 BUG();
4148 }
4149
4150 spin_lock_bh(&se_tpg->session_lock);
4151 atomic_set(&sess->session_logout, 1);
4152 atomic_set(&sess->session_reinstatement, 1);
4153 iscsit_stop_time2retain_timer(sess);
4154 spin_unlock_bh(&se_tpg->session_lock);
4155
4156 /*
4157 * transport_deregister_session_configfs() will clear the
4158 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4159 * can be setting it again with __transport_register_session() in
4160 * iscsi_post_login_handler() again after the iscsit_stop_session()
4161 * completes in iscsi_np context.
4162 */
4163 transport_deregister_session_configfs(sess->se_sess);
4164
4165 /*
4166 * If any other processes are accessing this session pointer we must
4167 * wait until they have completed. If we are in an interrupt (the
4168 * time2retain handler) and contain and active session usage count we
4169 * restart the timer and exit.
4170 */
4171 if (!in_interrupt()) {
4172 if (iscsit_check_session_usage_count(sess) == 1)
4173 iscsit_stop_session(sess, 1, 1);
4174 } else {
4175 if (iscsit_check_session_usage_count(sess) == 2) {
4176 atomic_set(&sess->session_logout, 0);
4177 iscsit_start_time2retain_handler(sess);
4178 return 0;
4179 }
4180 }
4181
4182 transport_deregister_session(sess->se_sess);
4183
4184 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4185 iscsit_free_connection_recovery_entires(sess);
4186
4187 iscsit_free_all_ooo_cmdsns(sess);
4188
4189 spin_lock_bh(&se_tpg->session_lock);
4190 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4191 sess->session_state = TARG_SESS_STATE_FREE;
4192 pr_debug("Released iSCSI session from node: %s\n",
4193 sess->sess_ops->InitiatorName);
4194 tpg->nsessions--;
4195 if (tpg->tpg_tiqn)
4196 tpg->tpg_tiqn->tiqn_nsessions--;
4197
4198 pr_debug("Decremented number of active iSCSI Sessions on"
4199 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4200
4201 spin_lock(&sess_idr_lock);
4202 idr_remove(&sess_idr, sess->session_index);
4203 spin_unlock(&sess_idr_lock);
4204
4205 kfree(sess->sess_ops);
4206 sess->sess_ops = NULL;
4207 spin_unlock_bh(&se_tpg->session_lock);
4208
4209 kfree(sess);
4210 return 0;
4211}
4212
4213static void iscsit_logout_post_handler_closesession(
4214 struct iscsi_conn *conn)
4215{
4216 struct iscsi_session *sess = conn->sess;
4217
4218 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4219 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4220
4221 atomic_set(&conn->conn_logout_remove, 0);
4222 complete(&conn->conn_logout_comp);
4223
4224 iscsit_dec_conn_usage_count(conn);
4225 iscsit_stop_session(sess, 1, 1);
4226 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004227 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004228}
4229
4230static void iscsit_logout_post_handler_samecid(
4231 struct iscsi_conn *conn)
4232{
4233 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4234 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4235
4236 atomic_set(&conn->conn_logout_remove, 0);
4237 complete(&conn->conn_logout_comp);
4238
4239 iscsit_cause_connection_reinstatement(conn, 1);
4240 iscsit_dec_conn_usage_count(conn);
4241}
4242
4243static void iscsit_logout_post_handler_diffcid(
4244 struct iscsi_conn *conn,
4245 u16 cid)
4246{
4247 struct iscsi_conn *l_conn;
4248 struct iscsi_session *sess = conn->sess;
4249
4250 if (!sess)
4251 return;
4252
4253 spin_lock_bh(&sess->conn_lock);
4254 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4255 if (l_conn->cid == cid) {
4256 iscsit_inc_conn_usage_count(l_conn);
4257 break;
4258 }
4259 }
4260 spin_unlock_bh(&sess->conn_lock);
4261
4262 if (!l_conn)
4263 return;
4264
4265 if (l_conn->sock)
4266 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4267
4268 spin_lock_bh(&l_conn->state_lock);
4269 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4270 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4271 spin_unlock_bh(&l_conn->state_lock);
4272
4273 iscsit_cause_connection_reinstatement(l_conn, 1);
4274 iscsit_dec_conn_usage_count(l_conn);
4275}
4276
4277/*
4278 * Return of 0 causes the TX thread to restart.
4279 */
4280static int iscsit_logout_post_handler(
4281 struct iscsi_cmd *cmd,
4282 struct iscsi_conn *conn)
4283{
4284 int ret = 0;
4285
4286 switch (cmd->logout_reason) {
4287 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4288 switch (cmd->logout_response) {
4289 case ISCSI_LOGOUT_SUCCESS:
4290 case ISCSI_LOGOUT_CLEANUP_FAILED:
4291 default:
4292 iscsit_logout_post_handler_closesession(conn);
4293 break;
4294 }
4295 ret = 0;
4296 break;
4297 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4298 if (conn->cid == cmd->logout_cid) {
4299 switch (cmd->logout_response) {
4300 case ISCSI_LOGOUT_SUCCESS:
4301 case ISCSI_LOGOUT_CLEANUP_FAILED:
4302 default:
4303 iscsit_logout_post_handler_samecid(conn);
4304 break;
4305 }
4306 ret = 0;
4307 } else {
4308 switch (cmd->logout_response) {
4309 case ISCSI_LOGOUT_SUCCESS:
4310 iscsit_logout_post_handler_diffcid(conn,
4311 cmd->logout_cid);
4312 break;
4313 case ISCSI_LOGOUT_CID_NOT_FOUND:
4314 case ISCSI_LOGOUT_CLEANUP_FAILED:
4315 default:
4316 break;
4317 }
4318 ret = 1;
4319 }
4320 break;
4321 case ISCSI_LOGOUT_REASON_RECOVERY:
4322 switch (cmd->logout_response) {
4323 case ISCSI_LOGOUT_SUCCESS:
4324 case ISCSI_LOGOUT_CID_NOT_FOUND:
4325 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4326 case ISCSI_LOGOUT_CLEANUP_FAILED:
4327 default:
4328 break;
4329 }
4330 ret = 1;
4331 break;
4332 default:
4333 break;
4334
4335 }
4336 return ret;
4337}
4338
4339void iscsit_fail_session(struct iscsi_session *sess)
4340{
4341 struct iscsi_conn *conn;
4342
4343 spin_lock_bh(&sess->conn_lock);
4344 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4345 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4346 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4347 }
4348 spin_unlock_bh(&sess->conn_lock);
4349
4350 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4351 sess->session_state = TARG_SESS_STATE_FAILED;
4352}
4353
4354int iscsit_free_session(struct iscsi_session *sess)
4355{
4356 u16 conn_count = atomic_read(&sess->nconn);
4357 struct iscsi_conn *conn, *conn_tmp = NULL;
4358 int is_last;
4359
4360 spin_lock_bh(&sess->conn_lock);
4361 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4362
4363 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4364 conn_list) {
4365 if (conn_count == 0)
4366 break;
4367
4368 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4369 is_last = 1;
4370 } else {
4371 iscsit_inc_conn_usage_count(conn_tmp);
4372 is_last = 0;
4373 }
4374 iscsit_inc_conn_usage_count(conn);
4375
4376 spin_unlock_bh(&sess->conn_lock);
4377 iscsit_cause_connection_reinstatement(conn, 1);
4378 spin_lock_bh(&sess->conn_lock);
4379
4380 iscsit_dec_conn_usage_count(conn);
4381 if (is_last == 0)
4382 iscsit_dec_conn_usage_count(conn_tmp);
4383
4384 conn_count--;
4385 }
4386
4387 if (atomic_read(&sess->nconn)) {
4388 spin_unlock_bh(&sess->conn_lock);
4389 wait_for_completion(&sess->session_wait_comp);
4390 } else
4391 spin_unlock_bh(&sess->conn_lock);
4392
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004393 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004394 return 0;
4395}
4396
4397void iscsit_stop_session(
4398 struct iscsi_session *sess,
4399 int session_sleep,
4400 int connection_sleep)
4401{
4402 u16 conn_count = atomic_read(&sess->nconn);
4403 struct iscsi_conn *conn, *conn_tmp = NULL;
4404 int is_last;
4405
4406 spin_lock_bh(&sess->conn_lock);
4407 if (session_sleep)
4408 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4409
4410 if (connection_sleep) {
4411 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4412 conn_list) {
4413 if (conn_count == 0)
4414 break;
4415
4416 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4417 is_last = 1;
4418 } else {
4419 iscsit_inc_conn_usage_count(conn_tmp);
4420 is_last = 0;
4421 }
4422 iscsit_inc_conn_usage_count(conn);
4423
4424 spin_unlock_bh(&sess->conn_lock);
4425 iscsit_cause_connection_reinstatement(conn, 1);
4426 spin_lock_bh(&sess->conn_lock);
4427
4428 iscsit_dec_conn_usage_count(conn);
4429 if (is_last == 0)
4430 iscsit_dec_conn_usage_count(conn_tmp);
4431 conn_count--;
4432 }
4433 } else {
4434 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4435 iscsit_cause_connection_reinstatement(conn, 0);
4436 }
4437
4438 if (session_sleep && atomic_read(&sess->nconn)) {
4439 spin_unlock_bh(&sess->conn_lock);
4440 wait_for_completion(&sess->session_wait_comp);
4441 } else
4442 spin_unlock_bh(&sess->conn_lock);
4443}
4444
4445int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4446{
4447 struct iscsi_session *sess;
4448 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4449 struct se_session *se_sess, *se_sess_tmp;
4450 int session_count = 0;
4451
4452 spin_lock_bh(&se_tpg->session_lock);
4453 if (tpg->nsessions && !force) {
4454 spin_unlock_bh(&se_tpg->session_lock);
4455 return -1;
4456 }
4457
4458 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4459 sess_list) {
4460 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4461
4462 spin_lock(&sess->conn_lock);
4463 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4464 atomic_read(&sess->session_logout) ||
4465 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4466 spin_unlock(&sess->conn_lock);
4467 continue;
4468 }
4469 atomic_set(&sess->session_reinstatement, 1);
4470 spin_unlock(&sess->conn_lock);
4471 spin_unlock_bh(&se_tpg->session_lock);
4472
4473 iscsit_free_session(sess);
4474 spin_lock_bh(&se_tpg->session_lock);
4475
4476 session_count++;
4477 }
4478 spin_unlock_bh(&se_tpg->session_lock);
4479
4480 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4481 " Group: %hu\n", session_count, tpg->tpgt);
4482 return 0;
4483}
4484
4485MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4486MODULE_VERSION("4.1.x");
4487MODULE_AUTHOR("nab@Linux-iSCSI.org");
4488MODULE_LICENSE("GPL");
4489
4490module_init(iscsi_target_init_module);
4491module_exit(iscsi_target_cleanup_module);