blob: 093fb6010272d565869a19bf8ae88719081e67ad [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
147 if (!idr_pre_get(&tiqn_idr, GFP_KERNEL)) {
148 pr_err("idr_pre_get() for tiqn_idr failed\n");
149 kfree(tiqn);
150 return ERR_PTR(-ENOMEM);
151 }
152 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
153
154 spin_lock(&tiqn_lock);
155 ret = idr_get_new(&tiqn_idr, NULL, &tiqn->tiqn_index);
156 if (ret < 0) {
157 pr_err("idr_get_new() failed for tiqn->tiqn_index\n");
158 spin_unlock(&tiqn_lock);
159 kfree(tiqn);
160 return ERR_PTR(ret);
161 }
162 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
163 spin_unlock(&tiqn_lock);
164
165 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
166
167 return tiqn;
168
169}
170
171static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
172{
173 /*
174 * Wait for accesses to said struct iscsi_tiqn to end.
175 */
176 spin_lock(&tiqn->tiqn_state_lock);
177 while (tiqn->tiqn_access_count != 0) {
178 spin_unlock(&tiqn->tiqn_state_lock);
179 msleep(10);
180 spin_lock(&tiqn->tiqn_state_lock);
181 }
182 spin_unlock(&tiqn->tiqn_state_lock);
183}
184
185void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
186{
187 /*
188 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
189 * while holding tiqn->tiqn_state_lock. This means that all subsequent
190 * attempts to access this struct iscsi_tiqn will fail from both transport
191 * fabric and control code paths.
192 */
193 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
194 pr_err("iscsit_set_tiqn_shutdown() failed\n");
195 return;
196 }
197
198 iscsit_wait_for_tiqn(tiqn);
199
200 spin_lock(&tiqn_lock);
201 list_del(&tiqn->tiqn_list);
202 idr_remove(&tiqn_idr, tiqn->tiqn_index);
203 spin_unlock(&tiqn_lock);
204
205 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
206 tiqn->tiqn);
207 kfree(tiqn);
208}
209
210int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
211{
212 int ret;
213 /*
214 * Determine if the network portal is accepting storage traffic.
215 */
216 spin_lock_bh(&np->np_thread_lock);
217 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
218 spin_unlock_bh(&np->np_thread_lock);
219 return -1;
220 }
221 if (np->np_login_tpg) {
222 pr_err("np->np_login_tpg() is not NULL!\n");
223 spin_unlock_bh(&np->np_thread_lock);
224 return -1;
225 }
226 spin_unlock_bh(&np->np_thread_lock);
227 /*
228 * Determine if the portal group is accepting storage traffic.
229 */
230 spin_lock_bh(&tpg->tpg_state_lock);
231 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
232 spin_unlock_bh(&tpg->tpg_state_lock);
233 return -1;
234 }
235 spin_unlock_bh(&tpg->tpg_state_lock);
236
237 /*
238 * Here we serialize access across the TIQN+TPG Tuple.
239 */
240 ret = mutex_lock_interruptible(&tpg->np_login_lock);
241 if ((ret != 0) || signal_pending(current))
242 return -1;
243
244 spin_lock_bh(&np->np_thread_lock);
245 np->np_login_tpg = tpg;
246 spin_unlock_bh(&np->np_thread_lock);
247
248 return 0;
249}
250
251int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
252{
253 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
254
255 spin_lock_bh(&np->np_thread_lock);
256 np->np_login_tpg = NULL;
257 spin_unlock_bh(&np->np_thread_lock);
258
259 mutex_unlock(&tpg->np_login_lock);
260
261 if (tiqn)
262 iscsit_put_tiqn_for_login(tiqn);
263
264 return 0;
265}
266
267static struct iscsi_np *iscsit_get_np(
268 struct __kernel_sockaddr_storage *sockaddr,
269 int network_transport)
270{
271 struct sockaddr_in *sock_in, *sock_in_e;
272 struct sockaddr_in6 *sock_in6, *sock_in6_e;
273 struct iscsi_np *np;
274 int ip_match = 0;
275 u16 port;
276
277 spin_lock_bh(&np_lock);
278 list_for_each_entry(np, &g_np_list, np_list) {
279 spin_lock(&np->np_thread_lock);
280 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
281 spin_unlock(&np->np_thread_lock);
282 continue;
283 }
284
285 if (sockaddr->ss_family == AF_INET6) {
286 sock_in6 = (struct sockaddr_in6 *)sockaddr;
287 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
288
Jörn Engel8359cf42011-11-24 02:05:51 +0100289 if (!memcmp(&sock_in6->sin6_addr.in6_u,
290 &sock_in6_e->sin6_addr.in6_u,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000291 sizeof(struct in6_addr)))
292 ip_match = 1;
293
294 port = ntohs(sock_in6->sin6_port);
295 } else {
296 sock_in = (struct sockaddr_in *)sockaddr;
297 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
298
299 if (sock_in->sin_addr.s_addr ==
300 sock_in_e->sin_addr.s_addr)
301 ip_match = 1;
302
303 port = ntohs(sock_in->sin_port);
304 }
305
306 if ((ip_match == 1) && (np->np_port == port) &&
307 (np->np_network_transport == network_transport)) {
308 /*
309 * Increment the np_exports reference count now to
310 * prevent iscsit_del_np() below from being called
311 * while iscsi_tpg_add_network_portal() is called.
312 */
313 np->np_exports++;
314 spin_unlock(&np->np_thread_lock);
315 spin_unlock_bh(&np_lock);
316 return np;
317 }
318 spin_unlock(&np->np_thread_lock);
319 }
320 spin_unlock_bh(&np_lock);
321
322 return NULL;
323}
324
325struct iscsi_np *iscsit_add_np(
326 struct __kernel_sockaddr_storage *sockaddr,
327 char *ip_str,
328 int network_transport)
329{
330 struct sockaddr_in *sock_in;
331 struct sockaddr_in6 *sock_in6;
332 struct iscsi_np *np;
333 int ret;
334 /*
335 * Locate the existing struct iscsi_np if already active..
336 */
337 np = iscsit_get_np(sockaddr, network_transport);
338 if (np)
339 return np;
340
341 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
342 if (!np) {
343 pr_err("Unable to allocate memory for struct iscsi_np\n");
344 return ERR_PTR(-ENOMEM);
345 }
346
347 np->np_flags |= NPF_IP_NETWORK;
348 if (sockaddr->ss_family == AF_INET6) {
349 sock_in6 = (struct sockaddr_in6 *)sockaddr;
350 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
351 np->np_port = ntohs(sock_in6->sin6_port);
352 } else {
353 sock_in = (struct sockaddr_in *)sockaddr;
354 sprintf(np->np_ip, "%s", ip_str);
355 np->np_port = ntohs(sock_in->sin_port);
356 }
357
358 np->np_network_transport = network_transport;
359 spin_lock_init(&np->np_thread_lock);
360 init_completion(&np->np_restart_comp);
361 INIT_LIST_HEAD(&np->np_list);
362
363 ret = iscsi_target_setup_login_socket(np, sockaddr);
364 if (ret != 0) {
365 kfree(np);
366 return ERR_PTR(ret);
367 }
368
369 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
370 if (IS_ERR(np->np_thread)) {
371 pr_err("Unable to create kthread: iscsi_np\n");
372 ret = PTR_ERR(np->np_thread);
373 kfree(np);
374 return ERR_PTR(ret);
375 }
376 /*
377 * Increment the np_exports reference count now to prevent
378 * iscsit_del_np() below from being run while a new call to
379 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
380 * active. We don't need to hold np->np_thread_lock at this
381 * point because iscsi_np has not been added to g_np_list yet.
382 */
383 np->np_exports = 1;
384
385 spin_lock_bh(&np_lock);
386 list_add_tail(&np->np_list, &g_np_list);
387 spin_unlock_bh(&np_lock);
388
389 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
390 np->np_ip, np->np_port, (np->np_network_transport == ISCSI_TCP) ?
391 "TCP" : "SCTP");
392
393 return np;
394}
395
396int iscsit_reset_np_thread(
397 struct iscsi_np *np,
398 struct iscsi_tpg_np *tpg_np,
399 struct iscsi_portal_group *tpg)
400{
401 spin_lock_bh(&np->np_thread_lock);
402 if (tpg && tpg_np) {
403 /*
404 * The reset operation need only be performed when the
405 * passed struct iscsi_portal_group has a login in progress
406 * to one of the network portals.
407 */
408 if (tpg_np->tpg_np->np_login_tpg != tpg) {
409 spin_unlock_bh(&np->np_thread_lock);
410 return 0;
411 }
412 }
413 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
414 spin_unlock_bh(&np->np_thread_lock);
415 return 0;
416 }
417 np->np_thread_state = ISCSI_NP_THREAD_RESET;
418
419 if (np->np_thread) {
420 spin_unlock_bh(&np->np_thread_lock);
421 send_sig(SIGINT, np->np_thread, 1);
422 wait_for_completion(&np->np_restart_comp);
423 spin_lock_bh(&np->np_thread_lock);
424 }
425 spin_unlock_bh(&np->np_thread_lock);
426
427 return 0;
428}
429
Christoph Hellwigfceb5bc2012-09-26 08:00:36 -0400430static int iscsit_del_np_comm(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000431{
Al Virobf6932f2012-07-21 08:55:18 +0100432 if (np->np_socket)
433 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000434 return 0;
435}
436
437int iscsit_del_np(struct iscsi_np *np)
438{
439 spin_lock_bh(&np->np_thread_lock);
440 np->np_exports--;
441 if (np->np_exports) {
442 spin_unlock_bh(&np->np_thread_lock);
443 return 0;
444 }
445 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
446 spin_unlock_bh(&np->np_thread_lock);
447
448 if (np->np_thread) {
449 /*
450 * We need to send the signal to wakeup Linux/Net
451 * which may be sleeping in sock_accept()..
452 */
453 send_sig(SIGINT, np->np_thread, 1);
454 kthread_stop(np->np_thread);
455 }
456 iscsit_del_np_comm(np);
457
458 spin_lock_bh(&np_lock);
459 list_del(&np->np_list);
460 spin_unlock_bh(&np_lock);
461
462 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
463 np->np_ip, np->np_port, (np->np_network_transport == ISCSI_TCP) ?
464 "TCP" : "SCTP");
465
466 kfree(np);
467 return 0;
468}
469
470static int __init iscsi_target_init_module(void)
471{
472 int ret = 0;
473
474 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
475
476 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
477 if (!iscsit_global) {
478 pr_err("Unable to allocate memory for iscsit_global\n");
479 return -1;
480 }
481 mutex_init(&auth_id_lock);
482 spin_lock_init(&sess_idr_lock);
483 idr_init(&tiqn_idr);
484 idr_init(&sess_idr);
485
486 ret = iscsi_target_register_configfs();
487 if (ret < 0)
488 goto out;
489
490 ret = iscsi_thread_set_init();
491 if (ret < 0)
492 goto configfs_out;
493
494 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
495 TARGET_THREAD_SET_COUNT) {
496 pr_err("iscsi_allocate_thread_sets() returned"
497 " unexpected value!\n");
498 goto ts_out1;
499 }
500
501 lio_cmd_cache = kmem_cache_create("lio_cmd_cache",
502 sizeof(struct iscsi_cmd), __alignof__(struct iscsi_cmd),
503 0, NULL);
504 if (!lio_cmd_cache) {
505 pr_err("Unable to kmem_cache_create() for"
506 " lio_cmd_cache\n");
507 goto ts_out2;
508 }
509
510 lio_qr_cache = kmem_cache_create("lio_qr_cache",
511 sizeof(struct iscsi_queue_req),
512 __alignof__(struct iscsi_queue_req), 0, NULL);
513 if (!lio_qr_cache) {
514 pr_err("nable to kmem_cache_create() for"
515 " lio_qr_cache\n");
516 goto cmd_out;
517 }
518
519 lio_dr_cache = kmem_cache_create("lio_dr_cache",
520 sizeof(struct iscsi_datain_req),
521 __alignof__(struct iscsi_datain_req), 0, NULL);
522 if (!lio_dr_cache) {
523 pr_err("Unable to kmem_cache_create() for"
524 " lio_dr_cache\n");
525 goto qr_out;
526 }
527
528 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
529 sizeof(struct iscsi_ooo_cmdsn),
530 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
531 if (!lio_ooo_cache) {
532 pr_err("Unable to kmem_cache_create() for"
533 " lio_ooo_cache\n");
534 goto dr_out;
535 }
536
537 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
538 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
539 0, NULL);
540 if (!lio_r2t_cache) {
541 pr_err("Unable to kmem_cache_create() for"
542 " lio_r2t_cache\n");
543 goto ooo_out;
544 }
545
546 if (iscsit_load_discovery_tpg() < 0)
547 goto r2t_out;
548
549 return ret;
550r2t_out:
551 kmem_cache_destroy(lio_r2t_cache);
552ooo_out:
553 kmem_cache_destroy(lio_ooo_cache);
554dr_out:
555 kmem_cache_destroy(lio_dr_cache);
556qr_out:
557 kmem_cache_destroy(lio_qr_cache);
558cmd_out:
559 kmem_cache_destroy(lio_cmd_cache);
560ts_out2:
561 iscsi_deallocate_thread_sets();
562ts_out1:
563 iscsi_thread_set_free();
564configfs_out:
565 iscsi_target_deregister_configfs();
566out:
567 kfree(iscsit_global);
568 return -ENOMEM;
569}
570
571static void __exit iscsi_target_cleanup_module(void)
572{
573 iscsi_deallocate_thread_sets();
574 iscsi_thread_set_free();
575 iscsit_release_discovery_tpg();
576 kmem_cache_destroy(lio_cmd_cache);
577 kmem_cache_destroy(lio_qr_cache);
578 kmem_cache_destroy(lio_dr_cache);
579 kmem_cache_destroy(lio_ooo_cache);
580 kmem_cache_destroy(lio_r2t_cache);
581
582 iscsi_target_deregister_configfs();
583
584 kfree(iscsit_global);
585}
586
Andy Grover8b1e1242012-04-03 15:51:12 -0700587static int iscsit_add_reject(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000588 u8 reason,
589 int fail_conn,
590 unsigned char *buf,
591 struct iscsi_conn *conn)
592{
593 struct iscsi_cmd *cmd;
594 struct iscsi_reject *hdr;
595 int ret;
596
597 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
598 if (!cmd)
599 return -1;
600
601 cmd->iscsi_opcode = ISCSI_OP_REJECT;
602 if (fail_conn)
603 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
604
605 hdr = (struct iscsi_reject *) cmd->pdu;
606 hdr->reason = reason;
607
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100608 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000609 if (!cmd->buf_ptr) {
610 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
611 iscsit_release_cmd(cmd);
612 return -1;
613 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000614
615 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700616 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000617 spin_unlock_bh(&conn->cmd_lock);
618
619 cmd->i_state = ISTATE_SEND_REJECT;
620 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
621
622 ret = wait_for_completion_interruptible(&cmd->reject_comp);
623 if (ret != 0)
624 return -1;
625
626 return (!fail_conn) ? 0 : -1;
627}
628
629int iscsit_add_reject_from_cmd(
630 u8 reason,
631 int fail_conn,
632 int add_to_conn,
633 unsigned char *buf,
634 struct iscsi_cmd *cmd)
635{
636 struct iscsi_conn *conn;
637 struct iscsi_reject *hdr;
638 int ret;
639
640 if (!cmd->conn) {
641 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
642 cmd->init_task_tag);
643 return -1;
644 }
645 conn = cmd->conn;
646
647 cmd->iscsi_opcode = ISCSI_OP_REJECT;
648 if (fail_conn)
649 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
650
651 hdr = (struct iscsi_reject *) cmd->pdu;
652 hdr->reason = reason;
653
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100654 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000655 if (!cmd->buf_ptr) {
656 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
657 iscsit_release_cmd(cmd);
658 return -1;
659 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000660
661 if (add_to_conn) {
662 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700663 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000664 spin_unlock_bh(&conn->cmd_lock);
665 }
666
667 cmd->i_state = ISTATE_SEND_REJECT;
668 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
669
670 ret = wait_for_completion_interruptible(&cmd->reject_comp);
671 if (ret != 0)
672 return -1;
673
674 return (!fail_conn) ? 0 : -1;
675}
676
677/*
678 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700679 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000680 */
681static int iscsit_map_iovec(
682 struct iscsi_cmd *cmd,
683 struct kvec *iov,
684 u32 data_offset,
685 u32 data_length)
686{
687 u32 i = 0;
688 struct scatterlist *sg;
689 unsigned int page_off;
690
691 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700692 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000693 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700694 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000695 page_off = (data_offset % PAGE_SIZE);
696
697 cmd->first_data_sg = sg;
698 cmd->first_data_sg_off = page_off;
699
700 while (data_length) {
701 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
702
703 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
704 iov[i].iov_len = cur_len;
705
706 data_length -= cur_len;
707 page_off = 0;
708 sg = sg_next(sg);
709 i++;
710 }
711
712 cmd->kmapped_nents = i;
713
714 return i;
715}
716
717static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
718{
719 u32 i;
720 struct scatterlist *sg;
721
722 sg = cmd->first_data_sg;
723
724 for (i = 0; i < cmd->kmapped_nents; i++)
725 kunmap(sg_page(&sg[i]));
726}
727
728static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
729{
730 struct iscsi_cmd *cmd;
731
732 conn->exp_statsn = exp_statsn;
733
734 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700735 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000736 spin_lock(&cmd->istate_lock);
737 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
738 (cmd->stat_sn < exp_statsn)) {
739 cmd->i_state = ISTATE_REMOVE;
740 spin_unlock(&cmd->istate_lock);
741 iscsit_add_cmd_to_immediate_queue(cmd, conn,
742 cmd->i_state);
743 continue;
744 }
745 spin_unlock(&cmd->istate_lock);
746 }
747 spin_unlock_bh(&conn->cmd_lock);
748}
749
750static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
751{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700752 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000753
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400754 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000755
756 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
757 if (!cmd->iov_data) {
758 pr_err("Unable to allocate cmd->iov_data\n");
759 return -ENOMEM;
760 }
761
762 cmd->orig_iov_data_count = iov_count;
763 return 0;
764}
765
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000766static int iscsit_handle_scsi_cmd(
767 struct iscsi_conn *conn,
768 unsigned char *buf)
769{
Christoph Hellwigde103c92012-11-06 12:24:09 -0800770 int data_direction, payload_length, cmdsn_ret = 0, immed_ret;
771 struct iscsi_cmd *cmd = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000772 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700773 int iscsi_task_attr;
774 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000775
776 spin_lock_bh(&conn->sess->session_stats_lock);
777 conn->sess->cmd_pdus++;
778 if (conn->sess->se_sess->se_node_acl) {
779 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
780 conn->sess->se_sess->se_node_acl->num_cmds++;
781 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
782 }
783 spin_unlock_bh(&conn->sess->session_stats_lock);
784
785 hdr = (struct iscsi_scsi_req *) buf;
786 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000787
788 /* FIXME; Add checks for AdditionalHeaderSegment */
789
790 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
791 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
792 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
793 " not set. Bad iSCSI Initiator.\n");
794 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
795 buf, conn);
796 }
797
798 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
799 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
800 /*
801 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
802 * that adds support for RESERVE/RELEASE. There is a bug
803 * add with this new functionality that sets R/W bits when
804 * neither CDB carries any READ or WRITE datapayloads.
805 */
806 if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) {
807 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
808 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
809 goto done;
810 }
811
812 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
813 " set when Expected Data Transfer Length is 0 for"
814 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]);
815 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
816 buf, conn);
817 }
818done:
819
820 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
821 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
822 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
823 " MUST be set if Expected Data Transfer Length is not 0."
824 " Bad iSCSI Initiator\n");
825 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
826 buf, conn);
827 }
828
829 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
830 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
831 pr_err("Bidirectional operations not supported!\n");
832 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
833 buf, conn);
834 }
835
836 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
837 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
838 " Scsi Command PDU.\n");
839 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
840 buf, conn);
841 }
842
843 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
844 pr_err("ImmediateData=No but DataSegmentLength=%u,"
845 " protocol error.\n", payload_length);
846 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
847 buf, conn);
848 }
849
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400850 if ((be32_to_cpu(hdr->data_length )== payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000851 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
852 pr_err("Expected Data Transfer Length and Length of"
853 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
854 " bit is not set protocol error\n");
855 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
856 buf, conn);
857 }
858
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400859 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000860 pr_err("DataSegmentLength: %u is greater than"
861 " EDTL: %u, protocol error.\n", payload_length,
862 hdr->data_length);
863 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
864 buf, conn);
865 }
866
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700867 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000868 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700869 " MaxXmitDataSegmentLength: %u, protocol error.\n",
870 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000871 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
872 buf, conn);
873 }
874
875 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
876 pr_err("DataSegmentLength: %u is greater than"
877 " FirstBurstLength: %u, protocol error.\n",
878 payload_length, conn->sess->sess_ops->FirstBurstLength);
879 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
880 buf, conn);
881 }
882
883 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
884 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
885 DMA_NONE;
886
Andy Groverd28b11692012-04-03 15:51:22 -0700887 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000888 if (!cmd)
889 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
Andy Groverd28b11692012-04-03 15:51:22 -0700890 buf, conn);
891
892 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700893 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
894 /*
895 * Figure out the SAM Task Attribute for the incoming SCSI CDB
896 */
897 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
898 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
899 sam_task_attr = MSG_SIMPLE_TAG;
900 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
901 sam_task_attr = MSG_ORDERED_TAG;
902 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
903 sam_task_attr = MSG_HEAD_TAG;
904 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
905 sam_task_attr = MSG_ACA_TAG;
906 else {
907 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
908 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
909 sam_task_attr = MSG_SIMPLE_TAG;
910 }
911
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000912 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
913 cmd->i_state = ISTATE_NEW_CMD;
914 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
915 cmd->immediate_data = (payload_length) ? 1 : 0;
916 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
917 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
918 if (cmd->unsolicited_data)
919 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
920
921 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
922 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
923 spin_lock_bh(&conn->sess->ttt_lock);
924 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
925 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
926 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
927 spin_unlock_bh(&conn->sess->ttt_lock);
928 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
929 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400930 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
931 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000932 cmd->first_burst_len = payload_length;
933
934 if (cmd->data_direction == DMA_FROM_DEVICE) {
935 struct iscsi_datain_req *dr;
936
937 dr = iscsit_allocate_datain_req();
938 if (!dr)
939 return iscsit_add_reject_from_cmd(
940 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
941 1, 1, buf, cmd);
942
943 iscsit_attach_datain_req(cmd, dr);
944 }
945
946 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700947 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
948 */
949 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400950 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
951 cmd->data_direction, sam_task_attr,
952 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700953
954 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
955 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
956 hdr->cmdsn, hdr->data_length, payload_length, conn->cid);
957
Christoph Hellwigde103c92012-11-06 12:24:09 -0800958 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
959 scsilun_to_int(&hdr->lun));
960 if (cmd->sense_reason)
961 goto attach_cmd;
962
963 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
964 if (cmd->sense_reason) {
965 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
966 return iscsit_add_reject_from_cmd(
967 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
968 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000969 }
Christoph Hellwigde103c92012-11-06 12:24:09 -0800970
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000971 goto attach_cmd;
972 }
Andy Grovera12f41f2012-04-03 15:51:20 -0700973
Christoph Hellwigde103c92012-11-06 12:24:09 -0800974 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000975 return iscsit_add_reject_from_cmd(
Christoph Hellwigde103c92012-11-06 12:24:09 -0800976 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
977 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000978 }
979
980attach_cmd:
981 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700982 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000983 spin_unlock_bh(&conn->cmd_lock);
984 /*
985 * Check if we need to delay processing because of ALUA
986 * Active/NonOptimized primary access state..
987 */
988 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -0700989
Christoph Hellwigde103c92012-11-06 12:24:09 -0800990 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000991 return iscsit_add_reject_from_cmd(
992 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
Nicholas Bellingercd931ee2012-01-16 17:11:54 -0800993 1, 0, buf, cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800994 }
995
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000996 /*
997 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
998 * the Immediate Bit is not set, and no Immediate
999 * Data is attached.
1000 *
1001 * A PDU/CmdSN carrying Immediate Data can only
1002 * be processed after the DataCRC has passed.
1003 * If the DataCRC fails, the CmdSN MUST NOT
1004 * be acknowledged. (See below)
1005 */
1006 if (!cmd->immediate_data) {
1007 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001008 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1009 return 0;
1010 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001011 return iscsit_add_reject_from_cmd(
1012 ISCSI_REASON_PROTOCOL_ERROR,
1013 1, 0, buf, cmd);
1014 }
1015
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001016 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001017
1018 /*
1019 * If no Immediate Data is attached, it's OK to return now.
1020 */
1021 if (!cmd->immediate_data) {
Christoph Hellwigde103c92012-11-06 12:24:09 -08001022 if (!cmd->sense_reason && cmd->unsolicited_data) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001023 iscsit_set_dataout_sequence_values(cmd);
1024
1025 spin_lock_bh(&cmd->dataout_timeout_lock);
1026 iscsit_start_dataout_timer(cmd, cmd->conn);
1027 spin_unlock_bh(&cmd->dataout_timeout_lock);
1028 }
1029
1030 return 0;
1031 }
1032
1033 /*
1034 * Early CHECK_CONDITIONs never make it to the transport processing
1035 * thread. They are processed in CmdSN order by
1036 * iscsit_check_received_cmdsn() below.
1037 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001038 if (cmd->sense_reason) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001039 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001040 goto after_immediate_data;
1041 }
1042 /*
1043 * Call directly into transport_generic_new_cmd() to perform
1044 * the backend memory allocation.
1045 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001046 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1047 if (cmd->sense_reason) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001048 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001049 goto after_immediate_data;
1050 }
1051
1052 immed_ret = iscsit_handle_immediate_data(cmd, buf, payload_length);
1053after_immediate_data:
1054 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1055 /*
1056 * A PDU/CmdSN carrying Immediate Data passed
1057 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1058 * Immediate Bit is not set.
1059 */
1060 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1061 /*
1062 * Special case for Unsupported SAM WRITE Opcodes
1063 * and ImmediateData=Yes.
1064 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001065 if (cmd->sense_reason) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001066 if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
1067 return -1;
1068 } else if (cmd->unsolicited_data) {
1069 iscsit_set_dataout_sequence_values(cmd);
1070
1071 spin_lock_bh(&cmd->dataout_timeout_lock);
1072 iscsit_start_dataout_timer(cmd, cmd->conn);
1073 spin_unlock_bh(&cmd->dataout_timeout_lock);
1074 }
1075
1076 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1077 return iscsit_add_reject_from_cmd(
1078 ISCSI_REASON_PROTOCOL_ERROR,
1079 1, 0, buf, cmd);
1080
1081 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1082 /*
1083 * Immediate Data failed DataCRC and ERL>=1,
1084 * silently drop this PDU and let the initiator
1085 * plug the CmdSN gap.
1086 *
1087 * FIXME: Send Unsolicited NOPIN with reserved
1088 * TTT here to help the initiator figure out
1089 * the missing CmdSN, although they should be
1090 * intelligent enough to determine the missing
1091 * CmdSN and issue a retry to plug the sequence.
1092 */
1093 cmd->i_state = ISTATE_REMOVE;
1094 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1095 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1096 return -1;
1097
1098 return 0;
1099}
1100
1101static u32 iscsit_do_crypto_hash_sg(
1102 struct hash_desc *hash,
1103 struct iscsi_cmd *cmd,
1104 u32 data_offset,
1105 u32 data_length,
1106 u32 padding,
1107 u8 *pad_bytes)
1108{
1109 u32 data_crc;
1110 u32 i;
1111 struct scatterlist *sg;
1112 unsigned int page_off;
1113
1114 crypto_hash_init(hash);
1115
1116 sg = cmd->first_data_sg;
1117 page_off = cmd->first_data_sg_off;
1118
1119 i = 0;
1120 while (data_length) {
1121 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1122
1123 crypto_hash_update(hash, &sg[i], cur_len);
1124
1125 data_length -= cur_len;
1126 page_off = 0;
1127 i++;
1128 }
1129
1130 if (padding) {
1131 struct scatterlist pad_sg;
1132
1133 sg_init_one(&pad_sg, pad_bytes, padding);
1134 crypto_hash_update(hash, &pad_sg, padding);
1135 }
1136 crypto_hash_final(hash, (u8 *) &data_crc);
1137
1138 return data_crc;
1139}
1140
1141static void iscsit_do_crypto_hash_buf(
1142 struct hash_desc *hash,
1143 unsigned char *buf,
1144 u32 payload_length,
1145 u32 padding,
1146 u8 *pad_bytes,
1147 u8 *data_crc)
1148{
1149 struct scatterlist sg;
1150
1151 crypto_hash_init(hash);
1152
Jörn Engel8359cf42011-11-24 02:05:51 +01001153 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001154 crypto_hash_update(hash, &sg, payload_length);
1155
1156 if (padding) {
1157 sg_init_one(&sg, pad_bytes, padding);
1158 crypto_hash_update(hash, &sg, padding);
1159 }
1160 crypto_hash_final(hash, data_crc);
1161}
1162
1163static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1164{
1165 int iov_ret, ooo_cmdsn = 0, ret;
1166 u8 data_crc_failed = 0;
1167 u32 checksum, iov_count = 0, padding = 0, rx_got = 0;
1168 u32 rx_size = 0, payload_length;
1169 struct iscsi_cmd *cmd = NULL;
1170 struct se_cmd *se_cmd;
1171 struct iscsi_data *hdr;
1172 struct kvec *iov;
1173 unsigned long flags;
1174
1175 hdr = (struct iscsi_data *) buf;
1176 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001177
1178 if (!payload_length) {
1179 pr_err("DataOUT payload is ZERO, protocol error.\n");
1180 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1181 buf, conn);
1182 }
1183
1184 /* iSCSI write */
1185 spin_lock_bh(&conn->sess->session_stats_lock);
1186 conn->sess->rx_data_octets += payload_length;
1187 if (conn->sess->se_sess->se_node_acl) {
1188 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1189 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1190 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1191 }
1192 spin_unlock_bh(&conn->sess->session_stats_lock);
1193
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001194 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001195 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001196 " MaxXmitDataSegmentLength: %u\n", payload_length,
1197 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001198 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1199 buf, conn);
1200 }
1201
1202 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1203 payload_length);
1204 if (!cmd)
1205 return 0;
1206
1207 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1208 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1209 hdr->itt, hdr->ttt, hdr->datasn, hdr->offset,
1210 payload_length, conn->cid);
1211
1212 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1213 pr_err("Command ITT: 0x%08x received DataOUT after"
1214 " last DataOUT received, dumping payload\n",
1215 cmd->init_task_tag);
1216 return iscsit_dump_data_payload(conn, payload_length, 1);
1217 }
1218
1219 if (cmd->data_direction != DMA_TO_DEVICE) {
1220 pr_err("Command ITT: 0x%08x received DataOUT for a"
1221 " NON-WRITE command.\n", cmd->init_task_tag);
1222 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1223 1, 0, buf, cmd);
1224 }
1225 se_cmd = &cmd->se_cmd;
1226 iscsit_mod_dataout_timer(cmd);
1227
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001228 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001229 pr_err("DataOut Offset: %u, Length %u greater than"
1230 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001231 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001232 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
1233 1, 0, buf, cmd);
1234 }
1235
1236 if (cmd->unsolicited_data) {
1237 int dump_unsolicited_data = 0;
1238
1239 if (conn->sess->sess_ops->InitialR2T) {
1240 pr_err("Received unexpected unsolicited data"
1241 " while InitialR2T=Yes, protocol error.\n");
1242 transport_send_check_condition_and_sense(&cmd->se_cmd,
1243 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1244 return -1;
1245 }
1246 /*
1247 * Special case for dealing with Unsolicited DataOUT
1248 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1249 * failures;
1250 */
1251
1252 /* Something's amiss if we're not in WRITE_PENDING state... */
1253 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1254 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1255 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1256
1257 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001258 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001259 dump_unsolicited_data = 1;
1260 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1261
1262 if (dump_unsolicited_data) {
1263 /*
1264 * Check if a delayed TASK_ABORTED status needs to
1265 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1266 * received with the unsolicitied data out.
1267 */
1268 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1269 iscsit_stop_dataout_timer(cmd);
1270
1271 transport_check_aborted_status(se_cmd,
1272 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1273 return iscsit_dump_data_payload(conn, payload_length, 1);
1274 }
1275 } else {
1276 /*
1277 * For the normal solicited data path:
1278 *
1279 * Check for a delayed TASK_ABORTED status and dump any
1280 * incoming data out payload if one exists. Also, when the
1281 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1282 * data out sequence, we decrement outstanding_r2ts. Once
1283 * outstanding_r2ts reaches zero, go ahead and send the delayed
1284 * TASK_ABORTED status.
1285 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001286 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001287 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1288 if (--cmd->outstanding_r2ts < 1) {
1289 iscsit_stop_dataout_timer(cmd);
1290 transport_check_aborted_status(
1291 se_cmd, 1);
1292 }
1293
1294 return iscsit_dump_data_payload(conn, payload_length, 1);
1295 }
1296 }
1297 /*
1298 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1299 * within-command recovery checks before receiving the payload.
1300 */
1301 ret = iscsit_check_pre_dataout(cmd, buf);
1302 if (ret == DATAOUT_WITHIN_COMMAND_RECOVERY)
1303 return 0;
1304 else if (ret == DATAOUT_CANNOT_RECOVER)
1305 return -1;
1306
1307 rx_size += payload_length;
1308 iov = &cmd->iov_data[0];
1309
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001310 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1311 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001312 if (iov_ret < 0)
1313 return -1;
1314
1315 iov_count += iov_ret;
1316
1317 padding = ((-payload_length) & 3);
1318 if (padding != 0) {
1319 iov[iov_count].iov_base = cmd->pad_bytes;
1320 iov[iov_count++].iov_len = padding;
1321 rx_size += padding;
1322 pr_debug("Receiving %u padding bytes.\n", padding);
1323 }
1324
1325 if (conn->conn_ops->DataDigest) {
1326 iov[iov_count].iov_base = &checksum;
1327 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1328 rx_size += ISCSI_CRC_LEN;
1329 }
1330
1331 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1332
1333 iscsit_unmap_iovec(cmd);
1334
1335 if (rx_got != rx_size)
1336 return -1;
1337
1338 if (conn->conn_ops->DataDigest) {
1339 u32 data_crc;
1340
1341 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001342 be32_to_cpu(hdr->offset),
1343 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001344 cmd->pad_bytes);
1345
1346 if (checksum != data_crc) {
1347 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1348 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1349 " does not match computed 0x%08x\n",
1350 hdr->itt, hdr->offset, payload_length,
1351 hdr->datasn, checksum, data_crc);
1352 data_crc_failed = 1;
1353 } else {
1354 pr_debug("Got CRC32C DataDigest 0x%08x for"
1355 " %u bytes of Data Out\n", checksum,
1356 payload_length);
1357 }
1358 }
1359 /*
1360 * Increment post receive data and CRC values or perform
1361 * within-command recovery.
1362 */
1363 ret = iscsit_check_post_dataout(cmd, buf, data_crc_failed);
1364 if ((ret == DATAOUT_NORMAL) || (ret == DATAOUT_WITHIN_COMMAND_RECOVERY))
1365 return 0;
1366 else if (ret == DATAOUT_SEND_R2T) {
1367 iscsit_set_dataout_sequence_values(cmd);
Andy Grover8b1e1242012-04-03 15:51:12 -07001368 iscsit_build_r2ts_for_cmd(cmd, conn, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001369 } else if (ret == DATAOUT_SEND_TO_TRANSPORT) {
1370 /*
1371 * Handle extra special case for out of order
1372 * Unsolicited Data Out.
1373 */
1374 spin_lock_bh(&cmd->istate_lock);
1375 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1376 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1377 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1378 spin_unlock_bh(&cmd->istate_lock);
1379
1380 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001381 if (ooo_cmdsn)
1382 return 0;
1383 target_execute_cmd(&cmd->se_cmd);
1384 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001385 } else /* DATAOUT_CANNOT_RECOVER */
1386 return -1;
1387
1388 return 0;
1389}
1390
1391static int iscsit_handle_nop_out(
1392 struct iscsi_conn *conn,
1393 unsigned char *buf)
1394{
1395 unsigned char *ping_data = NULL;
1396 int cmdsn_ret, niov = 0, ret = 0, rx_got, rx_size;
1397 u32 checksum, data_crc, padding = 0, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001398 struct iscsi_cmd *cmd = NULL;
1399 struct kvec *iov = NULL;
1400 struct iscsi_nopout *hdr;
1401
1402 hdr = (struct iscsi_nopout *) buf;
1403 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001404
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001405 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001406 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1407 " not set, protocol error.\n");
1408 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1409 buf, conn);
1410 }
1411
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001412 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001413 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001414 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001415 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001416 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001417 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1418 buf, conn);
1419 }
1420
1421 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%09x,"
1422 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001423 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001424 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1425 payload_length);
1426 /*
1427 * This is not a response to a Unsolicited NopIN, which means
1428 * it can either be a NOPOUT ping request (with a valid ITT),
1429 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1430 * Either way, make sure we allocate an struct iscsi_cmd, as both
1431 * can contain ping data.
1432 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001433 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001434 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1435 if (!cmd)
1436 return iscsit_add_reject(
1437 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1438 1, buf, conn);
1439
1440 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1441 cmd->i_state = ISTATE_SEND_NOPIN;
1442 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1443 1 : 0);
1444 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1445 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001446 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1447 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001448 cmd->data_direction = DMA_NONE;
1449 }
1450
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001451 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001452 rx_size = payload_length;
1453 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1454 if (!ping_data) {
1455 pr_err("Unable to allocate memory for"
1456 " NOPOUT ping data.\n");
1457 ret = -1;
1458 goto out;
1459 }
1460
1461 iov = &cmd->iov_misc[0];
1462 iov[niov].iov_base = ping_data;
1463 iov[niov++].iov_len = payload_length;
1464
1465 padding = ((-payload_length) & 3);
1466 if (padding != 0) {
1467 pr_debug("Receiving %u additional bytes"
1468 " for padding.\n", padding);
1469 iov[niov].iov_base = &cmd->pad_bytes;
1470 iov[niov++].iov_len = padding;
1471 rx_size += padding;
1472 }
1473 if (conn->conn_ops->DataDigest) {
1474 iov[niov].iov_base = &checksum;
1475 iov[niov++].iov_len = ISCSI_CRC_LEN;
1476 rx_size += ISCSI_CRC_LEN;
1477 }
1478
1479 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1480 if (rx_got != rx_size) {
1481 ret = -1;
1482 goto out;
1483 }
1484
1485 if (conn->conn_ops->DataDigest) {
1486 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1487 ping_data, payload_length,
1488 padding, cmd->pad_bytes,
1489 (u8 *)&data_crc);
1490
1491 if (checksum != data_crc) {
1492 pr_err("Ping data CRC32C DataDigest"
1493 " 0x%08x does not match computed 0x%08x\n",
1494 checksum, data_crc);
1495 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1496 pr_err("Unable to recover from"
1497 " NOPOUT Ping DataCRC failure while in"
1498 " ERL=0.\n");
1499 ret = -1;
1500 goto out;
1501 } else {
1502 /*
1503 * Silently drop this PDU and let the
1504 * initiator plug the CmdSN gap.
1505 */
1506 pr_debug("Dropping NOPOUT"
1507 " Command CmdSN: 0x%08x due to"
1508 " DataCRC error.\n", hdr->cmdsn);
1509 ret = 0;
1510 goto out;
1511 }
1512 } else {
1513 pr_debug("Got CRC32C DataDigest"
1514 " 0x%08x for %u bytes of ping data.\n",
1515 checksum, payload_length);
1516 }
1517 }
1518
1519 ping_data[payload_length] = '\0';
1520 /*
1521 * Attach ping data to struct iscsi_cmd->buf_ptr.
1522 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001523 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001524 cmd->buf_ptr_size = payload_length;
1525
1526 pr_debug("Got %u bytes of NOPOUT ping"
1527 " data.\n", payload_length);
1528 pr_debug("Ping Data: \"%s\"\n", ping_data);
1529 }
1530
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001531 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001532 if (!cmd) {
1533 pr_err("Checking CmdSN for NOPOUT,"
1534 " but cmd is NULL!\n");
1535 return -1;
1536 }
1537 /*
1538 * Initiator is expecting a NopIN ping reply,
1539 */
1540 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001541 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001542 spin_unlock_bh(&conn->cmd_lock);
1543
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001544 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001545
1546 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1547 iscsit_add_cmd_to_response_queue(cmd, conn,
1548 cmd->i_state);
1549 return 0;
1550 }
1551
1552 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1553 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1554 ret = 0;
1555 goto ping_out;
1556 }
1557 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1558 return iscsit_add_reject_from_cmd(
1559 ISCSI_REASON_PROTOCOL_ERROR,
1560 1, 0, buf, cmd);
1561
1562 return 0;
1563 }
1564
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001565 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001566 /*
1567 * This was a response to a unsolicited NOPIN ping.
1568 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001569 cmd = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001570 if (!cmd)
1571 return -1;
1572
1573 iscsit_stop_nopin_response_timer(conn);
1574
1575 cmd->i_state = ISTATE_REMOVE;
1576 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1577 iscsit_start_nopin_timer(conn);
1578 } else {
1579 /*
1580 * Initiator is not expecting a NOPIN is response.
1581 * Just ignore for now.
1582 *
1583 * iSCSI v19-91 10.18
1584 * "A NOP-OUT may also be used to confirm a changed
1585 * ExpStatSN if another PDU will not be available
1586 * for a long time."
1587 */
1588 ret = 0;
1589 goto out;
1590 }
1591
1592 return 0;
1593out:
1594 if (cmd)
1595 iscsit_release_cmd(cmd);
1596ping_out:
1597 kfree(ping_data);
1598 return ret;
1599}
1600
1601static int iscsit_handle_task_mgt_cmd(
1602 struct iscsi_conn *conn,
1603 unsigned char *buf)
1604{
1605 struct iscsi_cmd *cmd;
1606 struct se_tmr_req *se_tmr;
1607 struct iscsi_tmr_req *tmr_req;
1608 struct iscsi_tm *hdr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001609 int out_of_order_cmdsn = 0;
1610 int ret;
1611 u8 function;
1612
1613 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001614 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1615 function = hdr->flags;
1616
1617 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1618 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1619 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1620 hdr->rtt, hdr->refcmdsn, conn->cid);
1621
1622 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1623 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001624 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001625 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001626 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001627 }
1628
1629 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1630 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1631 pr_err("Task Management Request TASK_REASSIGN not"
1632 " issued as immediate command, bad iSCSI Initiator"
1633 "implementation\n");
1634 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1635 buf, conn);
1636 }
1637 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001638 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1639 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001640
Andy Groverd28b11692012-04-03 15:51:22 -07001641 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001642 if (!cmd)
1643 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
Andy Groverd28b11692012-04-03 15:51:22 -07001644 1, buf, conn);
1645
1646 cmd->data_direction = DMA_NONE;
1647
1648 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1649 if (!cmd->tmr_req) {
1650 pr_err("Unable to allocate memory for"
1651 " Task Management command!\n");
1652 return iscsit_add_reject_from_cmd(
1653 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1654 1, 1, buf, cmd);
1655 }
1656
1657 /*
1658 * TASK_REASSIGN for ERL=2 / connection stays inside of
1659 * LIO-Target $FABRIC_MOD
1660 */
1661 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1662
1663 u8 tcm_function;
1664 int ret;
1665
1666 transport_init_se_cmd(&cmd->se_cmd,
1667 &lio_target_fabric_configfs->tf_ops,
1668 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001669 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001670
1671 switch (function) {
1672 case ISCSI_TM_FUNC_ABORT_TASK:
1673 tcm_function = TMR_ABORT_TASK;
1674 break;
1675 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1676 tcm_function = TMR_ABORT_TASK_SET;
1677 break;
1678 case ISCSI_TM_FUNC_CLEAR_ACA:
1679 tcm_function = TMR_CLEAR_ACA;
1680 break;
1681 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1682 tcm_function = TMR_CLEAR_TASK_SET;
1683 break;
1684 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1685 tcm_function = TMR_LUN_RESET;
1686 break;
1687 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1688 tcm_function = TMR_TARGET_WARM_RESET;
1689 break;
1690 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1691 tcm_function = TMR_TARGET_COLD_RESET;
1692 break;
1693 default:
1694 pr_err("Unknown iSCSI TMR Function:"
1695 " 0x%02x\n", function);
1696 return iscsit_add_reject_from_cmd(
1697 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1698 1, 1, buf, cmd);
1699 }
1700
1701 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1702 tcm_function, GFP_KERNEL);
1703 if (ret < 0)
1704 return iscsit_add_reject_from_cmd(
1705 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1706 1, 1, buf, cmd);
1707
1708 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1709 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001710
1711 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1712 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1713 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1714 cmd->init_task_tag = hdr->itt;
1715 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001716 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1717 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001718 se_tmr = cmd->se_cmd.se_tmr_req;
1719 tmr_req = cmd->tmr_req;
1720 /*
1721 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1722 */
1723 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001724 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1725 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001726 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001727 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1728 goto attach;
1729 }
1730 }
1731
1732 switch (function) {
1733 case ISCSI_TM_FUNC_ABORT_TASK:
1734 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001735 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001736 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001737 break;
1738 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1739 case ISCSI_TM_FUNC_CLEAR_ACA:
1740 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1741 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1742 break;
1743 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1744 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001745 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1746 goto attach;
1747 }
1748 break;
1749 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1750 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001751 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1752 goto attach;
1753 }
1754 break;
1755 case ISCSI_TM_FUNC_TASK_REASSIGN:
1756 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1757 /*
1758 * Perform sanity checks on the ExpDataSN only if the
1759 * TASK_REASSIGN was successful.
1760 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001761 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001762 break;
1763
1764 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
1765 return iscsit_add_reject_from_cmd(
1766 ISCSI_REASON_BOOKMARK_INVALID, 1, 1,
1767 buf, cmd);
1768 break;
1769 default:
1770 pr_err("Unknown TMR function: 0x%02x, protocol"
1771 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001772 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1773 goto attach;
1774 }
1775
1776 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1777 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1778 se_tmr->call_transport = 1;
1779attach:
1780 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001781 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001782 spin_unlock_bh(&conn->cmd_lock);
1783
1784 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1785 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1786 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1787 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001788 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001789 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001790 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001791 return iscsit_add_reject_from_cmd(
1792 ISCSI_REASON_PROTOCOL_ERROR,
1793 1, 0, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001794 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001795 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001796
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001797 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001798 return 0;
1799 /*
1800 * Found the referenced task, send to transport for processing.
1801 */
1802 if (se_tmr->call_transport)
1803 return transport_generic_handle_tmr(&cmd->se_cmd);
1804
1805 /*
1806 * Could not find the referenced LUN, task, or Task Management
1807 * command not authorized or supported. Change state and
1808 * let the tx_thread send the response.
1809 *
1810 * For connection recovery, this is also the default action for
1811 * TMR TASK_REASSIGN.
1812 */
1813 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1814 return 0;
1815}
1816
1817/* #warning FIXME: Support Text Command parameters besides SendTargets */
1818static int iscsit_handle_text_cmd(
1819 struct iscsi_conn *conn,
1820 unsigned char *buf)
1821{
1822 char *text_ptr, *text_in;
1823 int cmdsn_ret, niov = 0, rx_got, rx_size;
1824 u32 checksum = 0, data_crc = 0, payload_length;
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001825 u32 padding = 0, pad_bytes = 0, text_length = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001826 struct iscsi_cmd *cmd;
1827 struct kvec iov[3];
1828 struct iscsi_text *hdr;
1829
1830 hdr = (struct iscsi_text *) buf;
1831 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001832
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001833 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001834 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001835 "greater than MaxXmitDataSegmentLength %u.\n",
1836 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001837 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1838 buf, conn);
1839 }
1840
1841 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1842 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1843 hdr->exp_statsn, payload_length);
1844
1845 rx_size = text_length = payload_length;
1846 if (text_length) {
1847 text_in = kzalloc(text_length, GFP_KERNEL);
1848 if (!text_in) {
1849 pr_err("Unable to allocate memory for"
1850 " incoming text parameters\n");
1851 return -1;
1852 }
1853
1854 memset(iov, 0, 3 * sizeof(struct kvec));
1855 iov[niov].iov_base = text_in;
1856 iov[niov++].iov_len = text_length;
1857
1858 padding = ((-payload_length) & 3);
1859 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001860 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001861 iov[niov++].iov_len = padding;
1862 rx_size += padding;
1863 pr_debug("Receiving %u additional bytes"
1864 " for padding.\n", padding);
1865 }
1866 if (conn->conn_ops->DataDigest) {
1867 iov[niov].iov_base = &checksum;
1868 iov[niov++].iov_len = ISCSI_CRC_LEN;
1869 rx_size += ISCSI_CRC_LEN;
1870 }
1871
1872 rx_got = rx_data(conn, &iov[0], niov, rx_size);
1873 if (rx_got != rx_size) {
1874 kfree(text_in);
1875 return -1;
1876 }
1877
1878 if (conn->conn_ops->DataDigest) {
1879 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1880 text_in, text_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001881 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001882 (u8 *)&data_crc);
1883
1884 if (checksum != data_crc) {
1885 pr_err("Text data CRC32C DataDigest"
1886 " 0x%08x does not match computed"
1887 " 0x%08x\n", checksum, data_crc);
1888 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1889 pr_err("Unable to recover from"
1890 " Text Data digest failure while in"
1891 " ERL=0.\n");
1892 kfree(text_in);
1893 return -1;
1894 } else {
1895 /*
1896 * Silently drop this PDU and let the
1897 * initiator plug the CmdSN gap.
1898 */
1899 pr_debug("Dropping Text"
1900 " Command CmdSN: 0x%08x due to"
1901 " DataCRC error.\n", hdr->cmdsn);
1902 kfree(text_in);
1903 return 0;
1904 }
1905 } else {
1906 pr_debug("Got CRC32C DataDigest"
1907 " 0x%08x for %u bytes of text data.\n",
1908 checksum, text_length);
1909 }
1910 }
1911 text_in[text_length - 1] = '\0';
1912 pr_debug("Successfully read %d bytes of text"
1913 " data.\n", text_length);
1914
1915 if (strncmp("SendTargets", text_in, 11) != 0) {
1916 pr_err("Received Text Data that is not"
1917 " SendTargets, cannot continue.\n");
1918 kfree(text_in);
1919 return -1;
1920 }
1921 text_ptr = strchr(text_in, '=');
1922 if (!text_ptr) {
1923 pr_err("No \"=\" separator found in Text Data,"
1924 " cannot continue.\n");
1925 kfree(text_in);
1926 return -1;
1927 }
1928 if (strncmp("=All", text_ptr, 4) != 0) {
1929 pr_err("Unable to locate All value for"
1930 " SendTargets key, cannot continue.\n");
1931 kfree(text_in);
1932 return -1;
1933 }
1934/*#warning Support SendTargets=(iSCSI Target Name/Nothing) values. */
1935 kfree(text_in);
1936 }
1937
1938 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1939 if (!cmd)
1940 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1941 1, buf, conn);
1942
1943 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1944 cmd->i_state = ISTATE_SEND_TEXTRSP;
1945 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1946 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1947 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001948 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1949 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001950 cmd->data_direction = DMA_NONE;
1951
1952 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001953 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001954 spin_unlock_bh(&conn->cmd_lock);
1955
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001956 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001957
1958 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1959 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1960 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1961 return iscsit_add_reject_from_cmd(
1962 ISCSI_REASON_PROTOCOL_ERROR,
1963 1, 0, buf, cmd);
1964
1965 return 0;
1966 }
1967
1968 return iscsit_execute_cmd(cmd, 0);
1969}
1970
1971int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
1972{
1973 struct iscsi_conn *conn_p;
1974 struct iscsi_session *sess = conn->sess;
1975
1976 pr_debug("Received logout request CLOSESESSION on CID: %hu"
1977 " for SID: %u.\n", conn->cid, conn->sess->sid);
1978
1979 atomic_set(&sess->session_logout, 1);
1980 atomic_set(&conn->conn_logout_remove, 1);
1981 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
1982
1983 iscsit_inc_conn_usage_count(conn);
1984 iscsit_inc_session_usage_count(sess);
1985
1986 spin_lock_bh(&sess->conn_lock);
1987 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
1988 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
1989 continue;
1990
1991 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
1992 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
1993 }
1994 spin_unlock_bh(&sess->conn_lock);
1995
1996 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1997
1998 return 0;
1999}
2000
2001int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2002{
2003 struct iscsi_conn *l_conn;
2004 struct iscsi_session *sess = conn->sess;
2005
2006 pr_debug("Received logout request CLOSECONNECTION for CID:"
2007 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2008
2009 /*
2010 * A Logout Request with a CLOSECONNECTION reason code for a CID
2011 * can arrive on a connection with a differing CID.
2012 */
2013 if (conn->cid == cmd->logout_cid) {
2014 spin_lock_bh(&conn->state_lock);
2015 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2016 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2017
2018 atomic_set(&conn->conn_logout_remove, 1);
2019 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2020 iscsit_inc_conn_usage_count(conn);
2021
2022 spin_unlock_bh(&conn->state_lock);
2023 } else {
2024 /*
2025 * Handle all different cid CLOSECONNECTION requests in
2026 * iscsit_logout_post_handler_diffcid() as to give enough
2027 * time for any non immediate command's CmdSN to be
2028 * acknowledged on the connection in question.
2029 *
2030 * Here we simply make sure the CID is still around.
2031 */
2032 l_conn = iscsit_get_conn_from_cid(sess,
2033 cmd->logout_cid);
2034 if (!l_conn) {
2035 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2036 iscsit_add_cmd_to_response_queue(cmd, conn,
2037 cmd->i_state);
2038 return 0;
2039 }
2040
2041 iscsit_dec_conn_usage_count(l_conn);
2042 }
2043
2044 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2045
2046 return 0;
2047}
2048
2049int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2050{
2051 struct iscsi_session *sess = conn->sess;
2052
2053 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2054 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2055
2056 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2057 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2058 " while ERL!=2.\n");
2059 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2060 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2061 return 0;
2062 }
2063
2064 if (conn->cid == cmd->logout_cid) {
2065 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2066 " with CID: %hu on CID: %hu, implementation error.\n",
2067 cmd->logout_cid, conn->cid);
2068 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2069 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2070 return 0;
2071 }
2072
2073 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2074
2075 return 0;
2076}
2077
2078static int iscsit_handle_logout_cmd(
2079 struct iscsi_conn *conn,
2080 unsigned char *buf)
2081{
2082 int cmdsn_ret, logout_remove = 0;
2083 u8 reason_code = 0;
2084 struct iscsi_cmd *cmd;
2085 struct iscsi_logout *hdr;
2086 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2087
2088 hdr = (struct iscsi_logout *) buf;
2089 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002090
2091 if (tiqn) {
2092 spin_lock(&tiqn->logout_stats.lock);
2093 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2094 tiqn->logout_stats.normal_logouts++;
2095 else
2096 tiqn->logout_stats.abnormal_logouts++;
2097 spin_unlock(&tiqn->logout_stats.lock);
2098 }
2099
2100 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2101 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2102 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2103 hdr->cid, conn->cid);
2104
2105 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2106 pr_err("Received logout request on connection that"
2107 " is not in logged in state, ignoring request.\n");
2108 return 0;
2109 }
2110
2111 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
2112 if (!cmd)
2113 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
2114 buf, conn);
2115
2116 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2117 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2118 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2119 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2120 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002121 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2122 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2123 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002124 cmd->logout_reason = reason_code;
2125 cmd->data_direction = DMA_NONE;
2126
2127 /*
2128 * We need to sleep in these cases (by returning 1) until the Logout
2129 * Response gets sent in the tx thread.
2130 */
2131 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2132 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002133 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002134 logout_remove = 1;
2135
2136 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002137 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002138 spin_unlock_bh(&conn->cmd_lock);
2139
2140 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002141 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002142
2143 /*
2144 * Immediate commands are executed, well, immediately.
2145 * Non-Immediate Logout Commands are executed in CmdSN order.
2146 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002147 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002148 int ret = iscsit_execute_cmd(cmd, 0);
2149
2150 if (ret < 0)
2151 return ret;
2152 } else {
2153 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2154 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2155 logout_remove = 0;
2156 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2157 return iscsit_add_reject_from_cmd(
2158 ISCSI_REASON_PROTOCOL_ERROR,
2159 1, 0, buf, cmd);
2160 }
2161 }
2162
2163 return logout_remove;
2164}
2165
2166static int iscsit_handle_snack(
2167 struct iscsi_conn *conn,
2168 unsigned char *buf)
2169{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002170 struct iscsi_snack *hdr;
2171
2172 hdr = (struct iscsi_snack *) buf;
2173 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002174
2175 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2176 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2177 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2178 hdr->begrun, hdr->runlength, conn->cid);
2179
2180 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2181 pr_err("Initiator sent SNACK request while in"
2182 " ErrorRecoveryLevel=0.\n");
2183 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2184 buf, conn);
2185 }
2186 /*
2187 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2188 * call from inside iscsi_send_recovery_datain_or_r2t().
2189 */
2190 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2191 case 0:
2192 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002193 hdr->itt,
2194 be32_to_cpu(hdr->ttt),
2195 be32_to_cpu(hdr->begrun),
2196 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002197 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002198 return iscsit_handle_status_snack(conn, hdr->itt,
2199 be32_to_cpu(hdr->ttt),
2200 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002201 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002202 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2203 be32_to_cpu(hdr->begrun),
2204 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002205 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2206 /* FIXME: Support R-Data SNACK */
2207 pr_err("R-Data SNACK Not Supported.\n");
2208 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2209 buf, conn);
2210 default:
2211 pr_err("Unknown SNACK type 0x%02x, protocol"
2212 " error.\n", hdr->flags & 0x0f);
2213 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2214 buf, conn);
2215 }
2216
2217 return 0;
2218}
2219
2220static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2221{
2222 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2223 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2224 wait_for_completion_interruptible_timeout(
2225 &conn->rx_half_close_comp,
2226 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2227 }
2228}
2229
2230static int iscsit_handle_immediate_data(
2231 struct iscsi_cmd *cmd,
2232 unsigned char *buf,
2233 u32 length)
2234{
2235 int iov_ret, rx_got = 0, rx_size = 0;
2236 u32 checksum, iov_count = 0, padding = 0;
2237 struct iscsi_conn *conn = cmd->conn;
2238 struct kvec *iov;
2239
2240 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2241 if (iov_ret < 0)
2242 return IMMEDIATE_DATA_CANNOT_RECOVER;
2243
2244 rx_size = length;
2245 iov_count = iov_ret;
2246 iov = &cmd->iov_data[0];
2247
2248 padding = ((-length) & 3);
2249 if (padding != 0) {
2250 iov[iov_count].iov_base = cmd->pad_bytes;
2251 iov[iov_count++].iov_len = padding;
2252 rx_size += padding;
2253 }
2254
2255 if (conn->conn_ops->DataDigest) {
2256 iov[iov_count].iov_base = &checksum;
2257 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2258 rx_size += ISCSI_CRC_LEN;
2259 }
2260
2261 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2262
2263 iscsit_unmap_iovec(cmd);
2264
2265 if (rx_got != rx_size) {
2266 iscsit_rx_thread_wait_for_tcp(conn);
2267 return IMMEDIATE_DATA_CANNOT_RECOVER;
2268 }
2269
2270 if (conn->conn_ops->DataDigest) {
2271 u32 data_crc;
2272
2273 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2274 cmd->write_data_done, length, padding,
2275 cmd->pad_bytes);
2276
2277 if (checksum != data_crc) {
2278 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2279 " does not match computed 0x%08x\n", checksum,
2280 data_crc);
2281
2282 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2283 pr_err("Unable to recover from"
2284 " Immediate Data digest failure while"
2285 " in ERL=0.\n");
2286 iscsit_add_reject_from_cmd(
2287 ISCSI_REASON_DATA_DIGEST_ERROR,
2288 1, 0, buf, cmd);
2289 return IMMEDIATE_DATA_CANNOT_RECOVER;
2290 } else {
2291 iscsit_add_reject_from_cmd(
2292 ISCSI_REASON_DATA_DIGEST_ERROR,
2293 0, 0, buf, cmd);
2294 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2295 }
2296 } else {
2297 pr_debug("Got CRC32C DataDigest 0x%08x for"
2298 " %u bytes of Immediate Data\n", checksum,
2299 length);
2300 }
2301 }
2302
2303 cmd->write_data_done += length;
2304
Andy Groverebf1d952012-04-03 15:51:24 -07002305 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002306 spin_lock_bh(&cmd->istate_lock);
2307 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2308 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2309 spin_unlock_bh(&cmd->istate_lock);
2310 }
2311
2312 return IMMEDIATE_DATA_NORMAL_OPERATION;
2313}
2314
2315/*
2316 * Called with sess->conn_lock held.
2317 */
2318/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2319 with active network interface */
2320static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2321{
2322 struct iscsi_cmd *cmd;
2323 struct iscsi_conn *conn_p;
2324
2325 /*
2326 * Only send a Asynchronous Message on connections whos network
2327 * interface is still functional.
2328 */
2329 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2330 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2331 iscsit_inc_conn_usage_count(conn_p);
2332 break;
2333 }
2334 }
2335
2336 if (!conn_p)
2337 return;
2338
2339 cmd = iscsit_allocate_cmd(conn_p, GFP_KERNEL);
2340 if (!cmd) {
2341 iscsit_dec_conn_usage_count(conn_p);
2342 return;
2343 }
2344
2345 cmd->logout_cid = conn->cid;
2346 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2347 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2348
2349 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002350 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002351 spin_unlock_bh(&conn_p->cmd_lock);
2352
2353 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2354 iscsit_dec_conn_usage_count(conn_p);
2355}
2356
2357static int iscsit_send_conn_drop_async_message(
2358 struct iscsi_cmd *cmd,
2359 struct iscsi_conn *conn)
2360{
2361 struct iscsi_async *hdr;
2362
2363 cmd->tx_size = ISCSI_HDR_LEN;
2364 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2365
2366 hdr = (struct iscsi_async *) cmd->pdu;
2367 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2368 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002369 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002370 cmd->targ_xfer_tag = 0xFFFFFFFF;
2371 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2372 cmd->stat_sn = conn->stat_sn++;
2373 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2374 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2375 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2376 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2377 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2378 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2379 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2380
2381 if (conn->conn_ops->HeaderDigest) {
2382 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2383
2384 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2385 (unsigned char *)hdr, ISCSI_HDR_LEN,
2386 0, NULL, (u8 *)header_digest);
2387
2388 cmd->tx_size += ISCSI_CRC_LEN;
2389 pr_debug("Attaching CRC32C HeaderDigest to"
2390 " Async Message 0x%08x\n", *header_digest);
2391 }
2392
2393 cmd->iov_misc[0].iov_base = cmd->pdu;
2394 cmd->iov_misc[0].iov_len = cmd->tx_size;
2395 cmd->iov_misc_count = 1;
2396
2397 pr_debug("Sending Connection Dropped Async Message StatSN:"
2398 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2399 cmd->logout_cid, conn->cid);
2400 return 0;
2401}
2402
Andy Grover6f3c0e62012-04-03 15:51:09 -07002403static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2404{
2405 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2406 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2407 wait_for_completion_interruptible_timeout(
2408 &conn->tx_half_close_comp,
2409 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2410 }
2411}
2412
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002413static int iscsit_send_data_in(
2414 struct iscsi_cmd *cmd,
Andy Grover6f3c0e62012-04-03 15:51:09 -07002415 struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002416{
2417 int iov_ret = 0, set_statsn = 0;
2418 u32 iov_count = 0, tx_size = 0;
2419 struct iscsi_datain datain;
2420 struct iscsi_datain_req *dr;
2421 struct iscsi_data_rsp *hdr;
2422 struct kvec *iov;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002423 int eodr = 0;
2424 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002425
2426 memset(&datain, 0, sizeof(struct iscsi_datain));
2427 dr = iscsit_get_datain_values(cmd, &datain);
2428 if (!dr) {
2429 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2430 cmd->init_task_tag);
2431 return -1;
2432 }
2433
2434 /*
2435 * Be paranoid and double check the logic for now.
2436 */
Andy Groverebf1d952012-04-03 15:51:24 -07002437 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002438 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2439 " datain.length: %u exceeds cmd->data_length: %u\n",
2440 cmd->init_task_tag, datain.offset, datain.length,
Andy Groverebf1d952012-04-03 15:51:24 -07002441 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002442 return -1;
2443 }
2444
2445 spin_lock_bh(&conn->sess->session_stats_lock);
2446 conn->sess->tx_data_octets += datain.length;
2447 if (conn->sess->se_sess->se_node_acl) {
2448 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2449 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2450 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2451 }
2452 spin_unlock_bh(&conn->sess->session_stats_lock);
2453 /*
2454 * Special case for successfully execution w/ both DATAIN
2455 * and Sense Data.
2456 */
2457 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2458 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2459 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2460 else {
2461 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2462 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2463 iscsit_increment_maxcmdsn(cmd, conn->sess);
2464 cmd->stat_sn = conn->stat_sn++;
2465 set_statsn = 1;
2466 } else if (dr->dr_complete ==
2467 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2468 set_statsn = 1;
2469 }
2470
2471 hdr = (struct iscsi_data_rsp *) cmd->pdu;
2472 memset(hdr, 0, ISCSI_HDR_LEN);
2473 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2474 hdr->flags = datain.flags;
2475 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2476 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2477 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002478 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002479 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2480 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002481 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002482 }
2483 }
2484 hton24(hdr->dlength, datain.length);
2485 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2486 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2487 (struct scsi_lun *)&hdr->lun);
2488 else
2489 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2490
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002491 hdr->itt = cmd->init_task_tag;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002492
2493 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2494 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2495 else
2496 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2497 if (set_statsn)
2498 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2499 else
2500 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2501
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002502 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2503 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2504 hdr->datasn = cpu_to_be32(datain.data_sn);
2505 hdr->offset = cpu_to_be32(datain.offset);
2506
2507 iov = &cmd->iov_data[0];
2508 iov[iov_count].iov_base = cmd->pdu;
2509 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2510 tx_size += ISCSI_HDR_LEN;
2511
2512 if (conn->conn_ops->HeaderDigest) {
2513 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2514
2515 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2516 (unsigned char *)hdr, ISCSI_HDR_LEN,
2517 0, NULL, (u8 *)header_digest);
2518
2519 iov[0].iov_len += ISCSI_CRC_LEN;
2520 tx_size += ISCSI_CRC_LEN;
2521
2522 pr_debug("Attaching CRC32 HeaderDigest"
2523 " for DataIN PDU 0x%08x\n", *header_digest);
2524 }
2525
2526 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], datain.offset, datain.length);
2527 if (iov_ret < 0)
2528 return -1;
2529
2530 iov_count += iov_ret;
2531 tx_size += datain.length;
2532
2533 cmd->padding = ((-datain.length) & 3);
2534 if (cmd->padding) {
2535 iov[iov_count].iov_base = cmd->pad_bytes;
2536 iov[iov_count++].iov_len = cmd->padding;
2537 tx_size += cmd->padding;
2538
2539 pr_debug("Attaching %u padding bytes\n",
2540 cmd->padding);
2541 }
2542 if (conn->conn_ops->DataDigest) {
2543 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2544 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2545
2546 iov[iov_count].iov_base = &cmd->data_crc;
2547 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2548 tx_size += ISCSI_CRC_LEN;
2549
2550 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2551 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2552 }
2553
2554 cmd->iov_data_count = iov_count;
2555 cmd->tx_size = tx_size;
2556
2557 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2558 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2559 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2560 ntohl(hdr->offset), datain.length, conn->cid);
2561
Andy Grover6f3c0e62012-04-03 15:51:09 -07002562 /* sendpage is preferred but can't insert markers */
2563 if (!conn->conn_ops->IFMarker)
2564 ret = iscsit_fe_sendpage_sg(cmd, conn);
2565 else
2566 ret = iscsit_send_tx_data(cmd, conn, 0);
2567
2568 iscsit_unmap_iovec(cmd);
2569
2570 if (ret < 0) {
2571 iscsit_tx_thread_wait_for_tcp(conn);
2572 return ret;
2573 }
2574
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002575 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002576 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002577 2 : 1;
2578 iscsit_free_datain_req(cmd, dr);
2579 }
2580
Andy Grover6f3c0e62012-04-03 15:51:09 -07002581 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002582}
2583
2584static int iscsit_send_logout_response(
2585 struct iscsi_cmd *cmd,
2586 struct iscsi_conn *conn)
2587{
2588 int niov = 0, tx_size;
2589 struct iscsi_conn *logout_conn = NULL;
2590 struct iscsi_conn_recovery *cr = NULL;
2591 struct iscsi_session *sess = conn->sess;
2592 struct kvec *iov;
2593 struct iscsi_logout_rsp *hdr;
2594 /*
2595 * The actual shutting down of Sessions and/or Connections
2596 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2597 * is done in scsi_logout_post_handler().
2598 */
2599 switch (cmd->logout_reason) {
2600 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2601 pr_debug("iSCSI session logout successful, setting"
2602 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2603 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2604 break;
2605 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2606 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2607 break;
2608 /*
2609 * For CLOSECONNECTION logout requests carrying
2610 * a matching logout CID -> local CID, the reference
2611 * for the local CID will have been incremented in
2612 * iscsi_logout_closeconnection().
2613 *
2614 * For CLOSECONNECTION logout requests carrying
2615 * a different CID than the connection it arrived
2616 * on, the connection responding to cmd->logout_cid
2617 * is stopped in iscsit_logout_post_handler_diffcid().
2618 */
2619
2620 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2621 " successful.\n", cmd->logout_cid, conn->cid);
2622 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2623 break;
2624 case ISCSI_LOGOUT_REASON_RECOVERY:
2625 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2626 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2627 break;
2628 /*
2629 * If the connection is still active from our point of view
2630 * force connection recovery to occur.
2631 */
2632 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2633 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002634 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002635 iscsit_connection_reinstatement_rcfr(logout_conn);
2636 iscsit_dec_conn_usage_count(logout_conn);
2637 }
2638
2639 cr = iscsit_get_inactive_connection_recovery_entry(
2640 conn->sess, cmd->logout_cid);
2641 if (!cr) {
2642 pr_err("Unable to locate CID: %hu for"
2643 " REMOVECONNFORRECOVERY Logout Request.\n",
2644 cmd->logout_cid);
2645 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2646 break;
2647 }
2648
2649 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2650
2651 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2652 " for recovery for CID: %hu on CID: %hu successful.\n",
2653 cmd->logout_cid, conn->cid);
2654 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2655 break;
2656 default:
2657 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2658 cmd->logout_reason);
2659 return -1;
2660 }
2661
2662 tx_size = ISCSI_HDR_LEN;
2663 hdr = (struct iscsi_logout_rsp *)cmd->pdu;
2664 memset(hdr, 0, ISCSI_HDR_LEN);
2665 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2666 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2667 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002668 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002669 cmd->stat_sn = conn->stat_sn++;
2670 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2671
2672 iscsit_increment_maxcmdsn(cmd, conn->sess);
2673 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2674 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2675
2676 iov = &cmd->iov_misc[0];
2677 iov[niov].iov_base = cmd->pdu;
2678 iov[niov++].iov_len = ISCSI_HDR_LEN;
2679
2680 if (conn->conn_ops->HeaderDigest) {
2681 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2682
2683 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2684 (unsigned char *)hdr, ISCSI_HDR_LEN,
2685 0, NULL, (u8 *)header_digest);
2686
2687 iov[0].iov_len += ISCSI_CRC_LEN;
2688 tx_size += ISCSI_CRC_LEN;
2689 pr_debug("Attaching CRC32C HeaderDigest to"
2690 " Logout Response 0x%08x\n", *header_digest);
2691 }
2692 cmd->iov_misc_count = niov;
2693 cmd->tx_size = tx_size;
2694
2695 pr_debug("Sending Logout Response ITT: 0x%08x StatSN:"
2696 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2697 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2698 cmd->logout_cid, conn->cid);
2699
2700 return 0;
2701}
2702
2703/*
2704 * Unsolicited NOPIN, either requesting a response or not.
2705 */
2706static int iscsit_send_unsolicited_nopin(
2707 struct iscsi_cmd *cmd,
2708 struct iscsi_conn *conn,
2709 int want_response)
2710{
2711 int tx_size = ISCSI_HDR_LEN;
2712 struct iscsi_nopin *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002713 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002714
2715 hdr = (struct iscsi_nopin *) cmd->pdu;
2716 memset(hdr, 0, ISCSI_HDR_LEN);
2717 hdr->opcode = ISCSI_OP_NOOP_IN;
2718 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002719 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002720 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2721 cmd->stat_sn = conn->stat_sn;
2722 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2723 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2724 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2725
2726 if (conn->conn_ops->HeaderDigest) {
2727 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2728
2729 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2730 (unsigned char *)hdr, ISCSI_HDR_LEN,
2731 0, NULL, (u8 *)header_digest);
2732
2733 tx_size += ISCSI_CRC_LEN;
2734 pr_debug("Attaching CRC32C HeaderDigest to"
2735 " NopIN 0x%08x\n", *header_digest);
2736 }
2737
2738 cmd->iov_misc[0].iov_base = cmd->pdu;
2739 cmd->iov_misc[0].iov_len = tx_size;
2740 cmd->iov_misc_count = 1;
2741 cmd->tx_size = tx_size;
2742
2743 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2744 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2745
Andy Grover6f3c0e62012-04-03 15:51:09 -07002746 ret = iscsit_send_tx_data(cmd, conn, 1);
2747 if (ret < 0) {
2748 iscsit_tx_thread_wait_for_tcp(conn);
2749 return ret;
2750 }
2751
2752 spin_lock_bh(&cmd->istate_lock);
2753 cmd->i_state = want_response ?
2754 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2755 spin_unlock_bh(&cmd->istate_lock);
2756
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002757 return 0;
2758}
2759
2760static int iscsit_send_nopin_response(
2761 struct iscsi_cmd *cmd,
2762 struct iscsi_conn *conn)
2763{
2764 int niov = 0, tx_size;
2765 u32 padding = 0;
2766 struct kvec *iov;
2767 struct iscsi_nopin *hdr;
2768
2769 tx_size = ISCSI_HDR_LEN;
2770 hdr = (struct iscsi_nopin *) cmd->pdu;
2771 memset(hdr, 0, ISCSI_HDR_LEN);
2772 hdr->opcode = ISCSI_OP_NOOP_IN;
2773 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2774 hton24(hdr->dlength, cmd->buf_ptr_size);
2775 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002776 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002777 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2778 cmd->stat_sn = conn->stat_sn++;
2779 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2780
2781 iscsit_increment_maxcmdsn(cmd, conn->sess);
2782 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2783 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2784
2785 iov = &cmd->iov_misc[0];
2786 iov[niov].iov_base = cmd->pdu;
2787 iov[niov++].iov_len = ISCSI_HDR_LEN;
2788
2789 if (conn->conn_ops->HeaderDigest) {
2790 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2791
2792 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2793 (unsigned char *)hdr, ISCSI_HDR_LEN,
2794 0, NULL, (u8 *)header_digest);
2795
2796 iov[0].iov_len += ISCSI_CRC_LEN;
2797 tx_size += ISCSI_CRC_LEN;
2798 pr_debug("Attaching CRC32C HeaderDigest"
2799 " to NopIn 0x%08x\n", *header_digest);
2800 }
2801
2802 /*
2803 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2804 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2805 */
2806 if (cmd->buf_ptr_size) {
2807 iov[niov].iov_base = cmd->buf_ptr;
2808 iov[niov++].iov_len = cmd->buf_ptr_size;
2809 tx_size += cmd->buf_ptr_size;
2810
2811 pr_debug("Echoing back %u bytes of ping"
2812 " data.\n", cmd->buf_ptr_size);
2813
2814 padding = ((-cmd->buf_ptr_size) & 3);
2815 if (padding != 0) {
2816 iov[niov].iov_base = &cmd->pad_bytes;
2817 iov[niov++].iov_len = padding;
2818 tx_size += padding;
2819 pr_debug("Attaching %u additional"
2820 " padding bytes.\n", padding);
2821 }
2822 if (conn->conn_ops->DataDigest) {
2823 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2824 cmd->buf_ptr, cmd->buf_ptr_size,
2825 padding, (u8 *)&cmd->pad_bytes,
2826 (u8 *)&cmd->data_crc);
2827
2828 iov[niov].iov_base = &cmd->data_crc;
2829 iov[niov++].iov_len = ISCSI_CRC_LEN;
2830 tx_size += ISCSI_CRC_LEN;
2831 pr_debug("Attached DataDigest for %u"
2832 " bytes of ping data, CRC 0x%08x\n",
2833 cmd->buf_ptr_size, cmd->data_crc);
2834 }
2835 }
2836
2837 cmd->iov_misc_count = niov;
2838 cmd->tx_size = tx_size;
2839
2840 pr_debug("Sending NOPIN Response ITT: 0x%08x, TTT:"
2841 " 0x%08x, StatSN: 0x%08x, Length %u\n", cmd->init_task_tag,
2842 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2843
2844 return 0;
2845}
2846
Andy Grover6f3c0e62012-04-03 15:51:09 -07002847static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002848 struct iscsi_cmd *cmd,
2849 struct iscsi_conn *conn)
2850{
2851 int tx_size = 0;
2852 struct iscsi_r2t *r2t;
2853 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002854 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002855
2856 r2t = iscsit_get_r2t_from_list(cmd);
2857 if (!r2t)
2858 return -1;
2859
2860 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
2861 memset(hdr, 0, ISCSI_HDR_LEN);
2862 hdr->opcode = ISCSI_OP_R2T;
2863 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2864 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2865 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002866 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002867 spin_lock_bh(&conn->sess->ttt_lock);
2868 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2869 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
2870 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2871 spin_unlock_bh(&conn->sess->ttt_lock);
2872 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
2873 hdr->statsn = cpu_to_be32(conn->stat_sn);
2874 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2875 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2876 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
2877 hdr->data_offset = cpu_to_be32(r2t->offset);
2878 hdr->data_length = cpu_to_be32(r2t->xfer_len);
2879
2880 cmd->iov_misc[0].iov_base = cmd->pdu;
2881 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
2882 tx_size += ISCSI_HDR_LEN;
2883
2884 if (conn->conn_ops->HeaderDigest) {
2885 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2886
2887 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2888 (unsigned char *)hdr, ISCSI_HDR_LEN,
2889 0, NULL, (u8 *)header_digest);
2890
2891 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
2892 tx_size += ISCSI_CRC_LEN;
2893 pr_debug("Attaching CRC32 HeaderDigest for R2T"
2894 " PDU 0x%08x\n", *header_digest);
2895 }
2896
2897 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
2898 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
2899 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
2900 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
2901 r2t->offset, r2t->xfer_len, conn->cid);
2902
2903 cmd->iov_misc_count = 1;
2904 cmd->tx_size = tx_size;
2905
2906 spin_lock_bh(&cmd->r2t_lock);
2907 r2t->sent_r2t = 1;
2908 spin_unlock_bh(&cmd->r2t_lock);
2909
Andy Grover6f3c0e62012-04-03 15:51:09 -07002910 ret = iscsit_send_tx_data(cmd, conn, 1);
2911 if (ret < 0) {
2912 iscsit_tx_thread_wait_for_tcp(conn);
2913 return ret;
2914 }
2915
2916 spin_lock_bh(&cmd->dataout_timeout_lock);
2917 iscsit_start_dataout_timer(cmd, conn);
2918 spin_unlock_bh(&cmd->dataout_timeout_lock);
2919
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002920 return 0;
2921}
2922
2923/*
Andy Grover8b1e1242012-04-03 15:51:12 -07002924 * @recovery: If called from iscsi_task_reassign_complete_write() for
2925 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002926 */
2927int iscsit_build_r2ts_for_cmd(
2928 struct iscsi_cmd *cmd,
2929 struct iscsi_conn *conn,
Andy Grover8b1e1242012-04-03 15:51:12 -07002930 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002931{
2932 int first_r2t = 1;
2933 u32 offset = 0, xfer_len = 0;
2934
2935 spin_lock_bh(&cmd->r2t_lock);
2936 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
2937 spin_unlock_bh(&cmd->r2t_lock);
2938 return 0;
2939 }
2940
Andy Grover8b1e1242012-04-03 15:51:12 -07002941 if (conn->sess->sess_ops->DataSequenceInOrder &&
2942 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07002943 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002944
2945 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
2946 if (conn->sess->sess_ops->DataSequenceInOrder) {
2947 offset = cmd->r2t_offset;
2948
Andy Grover8b1e1242012-04-03 15:51:12 -07002949 if (first_r2t && recovery) {
2950 int new_data_end = offset +
2951 conn->sess->sess_ops->MaxBurstLength -
2952 cmd->next_burst_len;
2953
Andy Groverebf1d952012-04-03 15:51:24 -07002954 if (new_data_end > cmd->se_cmd.data_length)
2955 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07002956 else
2957 xfer_len =
2958 conn->sess->sess_ops->MaxBurstLength -
2959 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002960 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07002961 int new_data_end = offset +
2962 conn->sess->sess_ops->MaxBurstLength;
2963
Andy Groverebf1d952012-04-03 15:51:24 -07002964 if (new_data_end > cmd->se_cmd.data_length)
2965 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07002966 else
2967 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002968 }
2969 cmd->r2t_offset += xfer_len;
2970
Andy Groverebf1d952012-04-03 15:51:24 -07002971 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002972 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
2973 } else {
2974 struct iscsi_seq *seq;
2975
2976 seq = iscsit_get_seq_holder_for_r2t(cmd);
2977 if (!seq) {
2978 spin_unlock_bh(&cmd->r2t_lock);
2979 return -1;
2980 }
2981
2982 offset = seq->offset;
2983 xfer_len = seq->xfer_len;
2984
2985 if (cmd->seq_send_order == cmd->seq_count)
2986 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
2987 }
2988 cmd->outstanding_r2ts++;
2989 first_r2t = 0;
2990
2991 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
2992 spin_unlock_bh(&cmd->r2t_lock);
2993 return -1;
2994 }
2995
2996 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
2997 break;
2998 }
2999 spin_unlock_bh(&cmd->r2t_lock);
3000
3001 return 0;
3002}
3003
3004static int iscsit_send_status(
3005 struct iscsi_cmd *cmd,
3006 struct iscsi_conn *conn)
3007{
3008 u8 iov_count = 0, recovery;
3009 u32 padding = 0, tx_size = 0;
3010 struct iscsi_scsi_rsp *hdr;
3011 struct kvec *iov;
3012
3013 recovery = (cmd->i_state != ISTATE_SEND_STATUS);
3014 if (!recovery)
3015 cmd->stat_sn = conn->stat_sn++;
3016
3017 spin_lock_bh(&conn->sess->session_stats_lock);
3018 conn->sess->rsp_pdus++;
3019 spin_unlock_bh(&conn->sess->session_stats_lock);
3020
3021 hdr = (struct iscsi_scsi_rsp *) cmd->pdu;
3022 memset(hdr, 0, ISCSI_HDR_LEN);
3023 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3024 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3025 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3026 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003027 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3029 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003030 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003031 }
3032 hdr->response = cmd->iscsi_response;
3033 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003034 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003035 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3036
3037 iscsit_increment_maxcmdsn(cmd, conn->sess);
3038 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3039 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3040
3041 iov = &cmd->iov_misc[0];
3042 iov[iov_count].iov_base = cmd->pdu;
3043 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3044 tx_size += ISCSI_HDR_LEN;
3045
3046 /*
3047 * Attach SENSE DATA payload to iSCSI Response PDU
3048 */
3049 if (cmd->se_cmd.sense_buffer &&
3050 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3051 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003052 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3053 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3054
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003055 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003056 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003057 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003058 iov[iov_count++].iov_len =
3059 (cmd->se_cmd.scsi_sense_length + padding);
3060 tx_size += cmd->se_cmd.scsi_sense_length;
3061
3062 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003063 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003064 cmd->se_cmd.scsi_sense_length, 0, padding);
3065 tx_size += padding;
3066 pr_debug("Adding %u bytes of padding to"
3067 " SENSE.\n", padding);
3068 }
3069
3070 if (conn->conn_ops->DataDigest) {
3071 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003072 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003073 (cmd->se_cmd.scsi_sense_length + padding),
3074 0, NULL, (u8 *)&cmd->data_crc);
3075
3076 iov[iov_count].iov_base = &cmd->data_crc;
3077 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3078 tx_size += ISCSI_CRC_LEN;
3079
3080 pr_debug("Attaching CRC32 DataDigest for"
3081 " SENSE, %u bytes CRC 0x%08x\n",
3082 (cmd->se_cmd.scsi_sense_length + padding),
3083 cmd->data_crc);
3084 }
3085
3086 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3087 " Response PDU\n",
3088 cmd->se_cmd.scsi_sense_length);
3089 }
3090
3091 if (conn->conn_ops->HeaderDigest) {
3092 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3093
3094 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3095 (unsigned char *)hdr, ISCSI_HDR_LEN,
3096 0, NULL, (u8 *)header_digest);
3097
3098 iov[0].iov_len += ISCSI_CRC_LEN;
3099 tx_size += ISCSI_CRC_LEN;
3100 pr_debug("Attaching CRC32 HeaderDigest for Response"
3101 " PDU 0x%08x\n", *header_digest);
3102 }
3103
3104 cmd->iov_misc_count = iov_count;
3105 cmd->tx_size = tx_size;
3106
3107 pr_debug("Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3108 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3109 (!recovery) ? "" : "Recovery ", cmd->init_task_tag,
3110 cmd->stat_sn, 0x00, cmd->se_cmd.scsi_status, conn->cid);
3111
3112 return 0;
3113}
3114
3115static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3116{
3117 switch (se_tmr->response) {
3118 case TMR_FUNCTION_COMPLETE:
3119 return ISCSI_TMF_RSP_COMPLETE;
3120 case TMR_TASK_DOES_NOT_EXIST:
3121 return ISCSI_TMF_RSP_NO_TASK;
3122 case TMR_LUN_DOES_NOT_EXIST:
3123 return ISCSI_TMF_RSP_NO_LUN;
3124 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3125 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3126 case TMR_FUNCTION_AUTHORIZATION_FAILED:
3127 return ISCSI_TMF_RSP_AUTH_FAILED;
3128 case TMR_FUNCTION_REJECTED:
3129 default:
3130 return ISCSI_TMF_RSP_REJECTED;
3131 }
3132}
3133
3134static int iscsit_send_task_mgt_rsp(
3135 struct iscsi_cmd *cmd,
3136 struct iscsi_conn *conn)
3137{
3138 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3139 struct iscsi_tm_rsp *hdr;
3140 u32 tx_size = 0;
3141
3142 hdr = (struct iscsi_tm_rsp *) cmd->pdu;
3143 memset(hdr, 0, ISCSI_HDR_LEN);
3144 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003145 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003146 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003147 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003148 cmd->stat_sn = conn->stat_sn++;
3149 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3150
3151 iscsit_increment_maxcmdsn(cmd, conn->sess);
3152 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3153 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3154
3155 cmd->iov_misc[0].iov_base = cmd->pdu;
3156 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3157 tx_size += ISCSI_HDR_LEN;
3158
3159 if (conn->conn_ops->HeaderDigest) {
3160 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3161
3162 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3163 (unsigned char *)hdr, ISCSI_HDR_LEN,
3164 0, NULL, (u8 *)header_digest);
3165
3166 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3167 tx_size += ISCSI_CRC_LEN;
3168 pr_debug("Attaching CRC32 HeaderDigest for Task"
3169 " Mgmt Response PDU 0x%08x\n", *header_digest);
3170 }
3171
3172 cmd->iov_misc_count = 1;
3173 cmd->tx_size = tx_size;
3174
3175 pr_debug("Built Task Management Response ITT: 0x%08x,"
3176 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3177 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3178
3179 return 0;
3180}
3181
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003182static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3183{
3184 bool ret = false;
3185
3186 if (np->np_sockaddr.ss_family == AF_INET6) {
3187 const struct sockaddr_in6 sin6 = {
3188 .sin6_addr = IN6ADDR_ANY_INIT };
3189 struct sockaddr_in6 *sock_in6 =
3190 (struct sockaddr_in6 *)&np->np_sockaddr;
3191
3192 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3193 sin6.sin6_addr.s6_addr, 16))
3194 ret = true;
3195 } else {
3196 struct sockaddr_in * sock_in =
3197 (struct sockaddr_in *)&np->np_sockaddr;
3198
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003199 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003200 ret = true;
3201 }
3202
3203 return ret;
3204}
3205
Andy Grover8b1e1242012-04-03 15:51:12 -07003206#define SENDTARGETS_BUF_LIMIT 32768U
3207
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003208static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3209{
3210 char *payload = NULL;
3211 struct iscsi_conn *conn = cmd->conn;
3212 struct iscsi_portal_group *tpg;
3213 struct iscsi_tiqn *tiqn;
3214 struct iscsi_tpg_np *tpg_np;
3215 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003216 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003217
Andy Grover8b1e1242012-04-03 15:51:12 -07003218 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3219 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003220
3221 payload = kzalloc(buffer_len, GFP_KERNEL);
3222 if (!payload) {
3223 pr_err("Unable to allocate memory for sendtargets"
3224 " response.\n");
3225 return -ENOMEM;
3226 }
3227
3228 spin_lock(&tiqn_lock);
3229 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3230 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3231 len += 1;
3232
3233 if ((len + payload_len) > buffer_len) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003234 end_of_buf = 1;
3235 goto eob;
3236 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003237 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003238 payload_len += len;
3239
3240 spin_lock(&tiqn->tiqn_tpg_lock);
3241 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3242
3243 spin_lock(&tpg->tpg_state_lock);
3244 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3245 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3246 spin_unlock(&tpg->tpg_state_lock);
3247 continue;
3248 }
3249 spin_unlock(&tpg->tpg_state_lock);
3250
3251 spin_lock(&tpg->tpg_np_lock);
3252 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3253 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003254 struct iscsi_np *np = tpg_np->tpg_np;
3255 bool inaddr_any = iscsit_check_inaddr_any(np);
3256
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003257 len = sprintf(buf, "TargetAddress="
3258 "%s%s%s:%hu,%hu",
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003259 (np->np_sockaddr.ss_family == AF_INET6) ?
3260 "[" : "", (inaddr_any == false) ?
3261 np->np_ip : conn->local_ip,
3262 (np->np_sockaddr.ss_family == AF_INET6) ?
3263 "]" : "", (inaddr_any == false) ?
3264 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003265 tpg->tpgt);
3266 len += 1;
3267
3268 if ((len + payload_len) > buffer_len) {
3269 spin_unlock(&tpg->tpg_np_lock);
3270 spin_unlock(&tiqn->tiqn_tpg_lock);
3271 end_of_buf = 1;
3272 goto eob;
3273 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003274 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003275 payload_len += len;
3276 }
3277 spin_unlock(&tpg->tpg_np_lock);
3278 }
3279 spin_unlock(&tiqn->tiqn_tpg_lock);
3280eob:
3281 if (end_of_buf)
3282 break;
3283 }
3284 spin_unlock(&tiqn_lock);
3285
3286 cmd->buf_ptr = payload;
3287
3288 return payload_len;
3289}
3290
3291/*
3292 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3293 * MaxRecvDataSegmentLength.
3294 */
3295static int iscsit_send_text_rsp(
3296 struct iscsi_cmd *cmd,
3297 struct iscsi_conn *conn)
3298{
3299 struct iscsi_text_rsp *hdr;
3300 struct kvec *iov;
3301 u32 padding = 0, tx_size = 0;
3302 int text_length, iov_count = 0;
3303
3304 text_length = iscsit_build_sendtargets_response(cmd);
3305 if (text_length < 0)
3306 return text_length;
3307
3308 padding = ((-text_length) & 3);
3309 if (padding != 0) {
3310 memset(cmd->buf_ptr + text_length, 0, padding);
3311 pr_debug("Attaching %u additional bytes for"
3312 " padding.\n", padding);
3313 }
3314
3315 hdr = (struct iscsi_text_rsp *) cmd->pdu;
3316 memset(hdr, 0, ISCSI_HDR_LEN);
3317 hdr->opcode = ISCSI_OP_TEXT_RSP;
3318 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3319 hton24(hdr->dlength, text_length);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003320 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003321 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3322 cmd->stat_sn = conn->stat_sn++;
3323 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3324
3325 iscsit_increment_maxcmdsn(cmd, conn->sess);
3326 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3327 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3328
3329 iov = &cmd->iov_misc[0];
3330
3331 iov[iov_count].iov_base = cmd->pdu;
3332 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3333 iov[iov_count].iov_base = cmd->buf_ptr;
3334 iov[iov_count++].iov_len = text_length + padding;
3335
3336 tx_size += (ISCSI_HDR_LEN + text_length + padding);
3337
3338 if (conn->conn_ops->HeaderDigest) {
3339 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3340
3341 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3342 (unsigned char *)hdr, ISCSI_HDR_LEN,
3343 0, NULL, (u8 *)header_digest);
3344
3345 iov[0].iov_len += ISCSI_CRC_LEN;
3346 tx_size += ISCSI_CRC_LEN;
3347 pr_debug("Attaching CRC32 HeaderDigest for"
3348 " Text Response PDU 0x%08x\n", *header_digest);
3349 }
3350
3351 if (conn->conn_ops->DataDigest) {
3352 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3353 cmd->buf_ptr, (text_length + padding),
3354 0, NULL, (u8 *)&cmd->data_crc);
3355
3356 iov[iov_count].iov_base = &cmd->data_crc;
3357 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3358 tx_size += ISCSI_CRC_LEN;
3359
3360 pr_debug("Attaching DataDigest for %u bytes of text"
3361 " data, CRC 0x%08x\n", (text_length + padding),
3362 cmd->data_crc);
3363 }
3364
3365 cmd->iov_misc_count = iov_count;
3366 cmd->tx_size = tx_size;
3367
3368 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3369 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3370 text_length, conn->cid);
3371 return 0;
3372}
3373
3374static int iscsit_send_reject(
3375 struct iscsi_cmd *cmd,
3376 struct iscsi_conn *conn)
3377{
3378 u32 iov_count = 0, tx_size = 0;
3379 struct iscsi_reject *hdr;
3380 struct kvec *iov;
3381
3382 hdr = (struct iscsi_reject *) cmd->pdu;
3383 hdr->opcode = ISCSI_OP_REJECT;
3384 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3385 hton24(hdr->dlength, ISCSI_HDR_LEN);
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003386 hdr->ffffffff = cpu_to_be32(0xffffffff);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003387 cmd->stat_sn = conn->stat_sn++;
3388 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3389 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3390 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3391
3392 iov = &cmd->iov_misc[0];
3393
3394 iov[iov_count].iov_base = cmd->pdu;
3395 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3396 iov[iov_count].iov_base = cmd->buf_ptr;
3397 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3398
3399 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3400
3401 if (conn->conn_ops->HeaderDigest) {
3402 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3403
3404 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3405 (unsigned char *)hdr, ISCSI_HDR_LEN,
3406 0, NULL, (u8 *)header_digest);
3407
3408 iov[0].iov_len += ISCSI_CRC_LEN;
3409 tx_size += ISCSI_CRC_LEN;
3410 pr_debug("Attaching CRC32 HeaderDigest for"
3411 " REJECT PDU 0x%08x\n", *header_digest);
3412 }
3413
3414 if (conn->conn_ops->DataDigest) {
3415 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3416 (unsigned char *)cmd->buf_ptr, ISCSI_HDR_LEN,
3417 0, NULL, (u8 *)&cmd->data_crc);
3418
3419 iov[iov_count].iov_base = &cmd->data_crc;
3420 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3421 tx_size += ISCSI_CRC_LEN;
3422 pr_debug("Attaching CRC32 DataDigest for REJECT"
3423 " PDU 0x%08x\n", cmd->data_crc);
3424 }
3425
3426 cmd->iov_misc_count = iov_count;
3427 cmd->tx_size = tx_size;
3428
3429 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3430 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3431
3432 return 0;
3433}
3434
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003435void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3436{
3437 struct iscsi_thread_set *ts = conn->thread_set;
3438 int ord, cpu;
3439 /*
3440 * thread_id is assigned from iscsit_global->ts_bitmap from
3441 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3442 *
3443 * Here we use thread_id to determine which CPU that this
3444 * iSCSI connection's iscsi_thread_set will be scheduled to
3445 * execute upon.
3446 */
3447 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003448 for_each_online_cpu(cpu) {
3449 if (ord-- == 0) {
3450 cpumask_set_cpu(cpu, conn->conn_cpumask);
3451 return;
3452 }
3453 }
3454 /*
3455 * This should never be reached..
3456 */
3457 dump_stack();
3458 cpumask_setall(conn->conn_cpumask);
3459}
3460
3461static inline void iscsit_thread_check_cpumask(
3462 struct iscsi_conn *conn,
3463 struct task_struct *p,
3464 int mode)
3465{
3466 char buf[128];
3467 /*
3468 * mode == 1 signals iscsi_target_tx_thread() usage.
3469 * mode == 0 signals iscsi_target_rx_thread() usage.
3470 */
3471 if (mode == 1) {
3472 if (!conn->conn_tx_reset_cpumask)
3473 return;
3474 conn->conn_tx_reset_cpumask = 0;
3475 } else {
3476 if (!conn->conn_rx_reset_cpumask)
3477 return;
3478 conn->conn_rx_reset_cpumask = 0;
3479 }
3480 /*
3481 * Update the CPU mask for this single kthread so that
3482 * both TX and RX kthreads are scheduled to run on the
3483 * same CPU.
3484 */
3485 memset(buf, 0, 128);
3486 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003487 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3488}
3489
Andy Grover6f3c0e62012-04-03 15:51:09 -07003490static int handle_immediate_queue(struct iscsi_conn *conn)
3491{
3492 struct iscsi_queue_req *qr;
3493 struct iscsi_cmd *cmd;
3494 u8 state;
3495 int ret;
3496
3497 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3498 atomic_set(&conn->check_immediate_queue, 0);
3499 cmd = qr->cmd;
3500 state = qr->state;
3501 kmem_cache_free(lio_qr_cache, qr);
3502
3503 switch (state) {
3504 case ISTATE_SEND_R2T:
3505 ret = iscsit_send_r2t(cmd, conn);
3506 if (ret < 0)
3507 goto err;
3508 break;
3509 case ISTATE_REMOVE:
3510 if (cmd->data_direction == DMA_TO_DEVICE)
3511 iscsit_stop_dataout_timer(cmd);
3512
3513 spin_lock_bh(&conn->cmd_lock);
3514 list_del(&cmd->i_conn_node);
3515 spin_unlock_bh(&conn->cmd_lock);
3516
3517 iscsit_free_cmd(cmd);
3518 continue;
3519 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3520 iscsit_mod_nopin_response_timer(conn);
3521 ret = iscsit_send_unsolicited_nopin(cmd,
3522 conn, 1);
3523 if (ret < 0)
3524 goto err;
3525 break;
3526 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3527 ret = iscsit_send_unsolicited_nopin(cmd,
3528 conn, 0);
3529 if (ret < 0)
3530 goto err;
3531 break;
3532 default:
3533 pr_err("Unknown Opcode: 0x%02x ITT:"
3534 " 0x%08x, i_state: %d on CID: %hu\n",
3535 cmd->iscsi_opcode, cmd->init_task_tag, state,
3536 conn->cid);
3537 goto err;
3538 }
3539 }
3540
3541 return 0;
3542
3543err:
3544 return -1;
3545}
3546
3547static int handle_response_queue(struct iscsi_conn *conn)
3548{
3549 struct iscsi_queue_req *qr;
3550 struct iscsi_cmd *cmd;
3551 u8 state;
3552 int ret;
3553
3554 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3555 cmd = qr->cmd;
3556 state = qr->state;
3557 kmem_cache_free(lio_qr_cache, qr);
3558
3559check_rsp_state:
3560 switch (state) {
3561 case ISTATE_SEND_DATAIN:
3562 ret = iscsit_send_data_in(cmd, conn);
3563 if (ret < 0)
3564 goto err;
3565 else if (!ret)
3566 /* more drs */
3567 goto check_rsp_state;
3568 else if (ret == 1) {
3569 /* all done */
3570 spin_lock_bh(&cmd->istate_lock);
3571 cmd->i_state = ISTATE_SENT_STATUS;
3572 spin_unlock_bh(&cmd->istate_lock);
3573 continue;
3574 } else if (ret == 2) {
3575 /* Still must send status,
3576 SCF_TRANSPORT_TASK_SENSE was set */
3577 spin_lock_bh(&cmd->istate_lock);
3578 cmd->i_state = ISTATE_SEND_STATUS;
3579 spin_unlock_bh(&cmd->istate_lock);
3580 state = ISTATE_SEND_STATUS;
3581 goto check_rsp_state;
3582 }
3583
3584 break;
3585 case ISTATE_SEND_STATUS:
3586 case ISTATE_SEND_STATUS_RECOVERY:
3587 ret = iscsit_send_status(cmd, conn);
3588 break;
3589 case ISTATE_SEND_LOGOUTRSP:
3590 ret = iscsit_send_logout_response(cmd, conn);
3591 break;
3592 case ISTATE_SEND_ASYNCMSG:
3593 ret = iscsit_send_conn_drop_async_message(
3594 cmd, conn);
3595 break;
3596 case ISTATE_SEND_NOPIN:
3597 ret = iscsit_send_nopin_response(cmd, conn);
3598 break;
3599 case ISTATE_SEND_REJECT:
3600 ret = iscsit_send_reject(cmd, conn);
3601 break;
3602 case ISTATE_SEND_TASKMGTRSP:
3603 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3604 if (ret != 0)
3605 break;
3606 ret = iscsit_tmr_post_handler(cmd, conn);
3607 if (ret != 0)
3608 iscsit_fall_back_to_erl0(conn->sess);
3609 break;
3610 case ISTATE_SEND_TEXTRSP:
3611 ret = iscsit_send_text_rsp(cmd, conn);
3612 break;
3613 default:
3614 pr_err("Unknown Opcode: 0x%02x ITT:"
3615 " 0x%08x, i_state: %d on CID: %hu\n",
3616 cmd->iscsi_opcode, cmd->init_task_tag,
3617 state, conn->cid);
3618 goto err;
3619 }
3620 if (ret < 0)
3621 goto err;
3622
3623 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3624 iscsit_tx_thread_wait_for_tcp(conn);
3625 iscsit_unmap_iovec(cmd);
3626 goto err;
3627 }
3628 iscsit_unmap_iovec(cmd);
3629
3630 switch (state) {
3631 case ISTATE_SEND_LOGOUTRSP:
3632 if (!iscsit_logout_post_handler(cmd, conn))
3633 goto restart;
3634 /* fall through */
3635 case ISTATE_SEND_STATUS:
3636 case ISTATE_SEND_ASYNCMSG:
3637 case ISTATE_SEND_NOPIN:
3638 case ISTATE_SEND_STATUS_RECOVERY:
3639 case ISTATE_SEND_TEXTRSP:
3640 case ISTATE_SEND_TASKMGTRSP:
3641 spin_lock_bh(&cmd->istate_lock);
3642 cmd->i_state = ISTATE_SENT_STATUS;
3643 spin_unlock_bh(&cmd->istate_lock);
3644 break;
3645 case ISTATE_SEND_REJECT:
3646 if (cmd->cmd_flags & ICF_REJECT_FAIL_CONN) {
3647 cmd->cmd_flags &= ~ICF_REJECT_FAIL_CONN;
3648 complete(&cmd->reject_comp);
3649 goto err;
3650 }
3651 complete(&cmd->reject_comp);
3652 break;
3653 default:
3654 pr_err("Unknown Opcode: 0x%02x ITT:"
3655 " 0x%08x, i_state: %d on CID: %hu\n",
3656 cmd->iscsi_opcode, cmd->init_task_tag,
3657 cmd->i_state, conn->cid);
3658 goto err;
3659 }
3660
3661 if (atomic_read(&conn->check_immediate_queue))
3662 break;
3663 }
3664
3665 return 0;
3666
3667err:
3668 return -1;
3669restart:
3670 return -EAGAIN;
3671}
3672
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003673int iscsi_target_tx_thread(void *arg)
3674{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003675 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003676 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003677 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003678 /*
3679 * Allow ourselves to be interrupted by SIGINT so that a
3680 * connection recovery / failure event can be triggered externally.
3681 */
3682 allow_signal(SIGINT);
3683
3684restart:
3685 conn = iscsi_tx_thread_pre_handler(ts);
3686 if (!conn)
3687 goto out;
3688
Andy Grover6f3c0e62012-04-03 15:51:09 -07003689 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003690
3691 while (!kthread_should_stop()) {
3692 /*
3693 * Ensure that both TX and RX per connection kthreads
3694 * are scheduled to run on the same CPU.
3695 */
3696 iscsit_thread_check_cpumask(conn, current, 1);
3697
Roland Dreierd5627ac2012-10-31 09:16:46 -07003698 wait_event_interruptible(conn->queues_wq,
3699 !iscsit_conn_all_queues_empty(conn) ||
3700 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003701
3702 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3703 signal_pending(current))
3704 goto transport_err;
3705
Andy Grover6f3c0e62012-04-03 15:51:09 -07003706 ret = handle_immediate_queue(conn);
3707 if (ret < 0)
3708 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003709
Andy Grover6f3c0e62012-04-03 15:51:09 -07003710 ret = handle_response_queue(conn);
3711 if (ret == -EAGAIN)
3712 goto restart;
3713 else if (ret < 0)
3714 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003715 }
3716
3717transport_err:
3718 iscsit_take_action_for_connection_exit(conn);
3719 goto restart;
3720out:
3721 return 0;
3722}
3723
3724int iscsi_target_rx_thread(void *arg)
3725{
3726 int ret;
3727 u8 buffer[ISCSI_HDR_LEN], opcode;
3728 u32 checksum = 0, digest = 0;
3729 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01003730 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003731 struct kvec iov;
3732 /*
3733 * Allow ourselves to be interrupted by SIGINT so that a
3734 * connection recovery / failure event can be triggered externally.
3735 */
3736 allow_signal(SIGINT);
3737
3738restart:
3739 conn = iscsi_rx_thread_pre_handler(ts);
3740 if (!conn)
3741 goto out;
3742
3743 while (!kthread_should_stop()) {
3744 /*
3745 * Ensure that both TX and RX per connection kthreads
3746 * are scheduled to run on the same CPU.
3747 */
3748 iscsit_thread_check_cpumask(conn, current, 0);
3749
3750 memset(buffer, 0, ISCSI_HDR_LEN);
3751 memset(&iov, 0, sizeof(struct kvec));
3752
3753 iov.iov_base = buffer;
3754 iov.iov_len = ISCSI_HDR_LEN;
3755
3756 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3757 if (ret != ISCSI_HDR_LEN) {
3758 iscsit_rx_thread_wait_for_tcp(conn);
3759 goto transport_err;
3760 }
3761
3762 /*
3763 * Set conn->bad_hdr for use with REJECT PDUs.
3764 */
3765 memcpy(&conn->bad_hdr, &buffer, ISCSI_HDR_LEN);
3766
3767 if (conn->conn_ops->HeaderDigest) {
3768 iov.iov_base = &digest;
3769 iov.iov_len = ISCSI_CRC_LEN;
3770
3771 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3772 if (ret != ISCSI_CRC_LEN) {
3773 iscsit_rx_thread_wait_for_tcp(conn);
3774 goto transport_err;
3775 }
3776
3777 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
3778 buffer, ISCSI_HDR_LEN,
3779 0, NULL, (u8 *)&checksum);
3780
3781 if (digest != checksum) {
3782 pr_err("HeaderDigest CRC32C failed,"
3783 " received 0x%08x, computed 0x%08x\n",
3784 digest, checksum);
3785 /*
3786 * Set the PDU to 0xff so it will intentionally
3787 * hit default in the switch below.
3788 */
3789 memset(buffer, 0xff, ISCSI_HDR_LEN);
3790 spin_lock_bh(&conn->sess->session_stats_lock);
3791 conn->sess->conn_digest_errors++;
3792 spin_unlock_bh(&conn->sess->session_stats_lock);
3793 } else {
3794 pr_debug("Got HeaderDigest CRC32C"
3795 " 0x%08x\n", checksum);
3796 }
3797 }
3798
3799 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
3800 goto transport_err;
3801
3802 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3803
3804 if (conn->sess->sess_ops->SessionType &&
3805 ((!(opcode & ISCSI_OP_TEXT)) ||
3806 (!(opcode & ISCSI_OP_LOGOUT)))) {
3807 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3808 " while in Discovery Session, rejecting.\n", opcode);
3809 iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
3810 buffer, conn);
3811 goto transport_err;
3812 }
3813
3814 switch (opcode) {
3815 case ISCSI_OP_SCSI_CMD:
3816 if (iscsit_handle_scsi_cmd(conn, buffer) < 0)
3817 goto transport_err;
3818 break;
3819 case ISCSI_OP_SCSI_DATA_OUT:
3820 if (iscsit_handle_data_out(conn, buffer) < 0)
3821 goto transport_err;
3822 break;
3823 case ISCSI_OP_NOOP_OUT:
3824 if (iscsit_handle_nop_out(conn, buffer) < 0)
3825 goto transport_err;
3826 break;
3827 case ISCSI_OP_SCSI_TMFUNC:
3828 if (iscsit_handle_task_mgt_cmd(conn, buffer) < 0)
3829 goto transport_err;
3830 break;
3831 case ISCSI_OP_TEXT:
3832 if (iscsit_handle_text_cmd(conn, buffer) < 0)
3833 goto transport_err;
3834 break;
3835 case ISCSI_OP_LOGOUT:
3836 ret = iscsit_handle_logout_cmd(conn, buffer);
3837 if (ret > 0) {
3838 wait_for_completion_timeout(&conn->conn_logout_comp,
3839 SECONDS_FOR_LOGOUT_COMP * HZ);
3840 goto transport_err;
3841 } else if (ret < 0)
3842 goto transport_err;
3843 break;
3844 case ISCSI_OP_SNACK:
3845 if (iscsit_handle_snack(conn, buffer) < 0)
3846 goto transport_err;
3847 break;
3848 default:
3849 pr_err("Got unknown iSCSI OpCode: 0x%02x\n",
3850 opcode);
3851 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3852 pr_err("Cannot recover from unknown"
3853 " opcode while ERL=0, closing iSCSI connection"
3854 ".\n");
3855 goto transport_err;
3856 }
3857 if (!conn->conn_ops->OFMarker) {
3858 pr_err("Unable to recover from unknown"
3859 " opcode while OFMarker=No, closing iSCSI"
3860 " connection.\n");
3861 goto transport_err;
3862 }
3863 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
3864 pr_err("Unable to recover from unknown"
3865 " opcode, closing iSCSI connection.\n");
3866 goto transport_err;
3867 }
3868 break;
3869 }
3870 }
3871
3872transport_err:
3873 if (!signal_pending(current))
3874 atomic_set(&conn->transport_failed, 1);
3875 iscsit_take_action_for_connection_exit(conn);
3876 goto restart;
3877out:
3878 return 0;
3879}
3880
3881static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
3882{
3883 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
3884 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003885 /*
3886 * We expect this function to only ever be called from either RX or TX
3887 * thread context via iscsit_close_connection() once the other context
3888 * has been reset -> returned sleeping pre-handler state.
3889 */
3890 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003891 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003892
Andy Grover2fbb4712012-04-03 15:51:01 -07003893 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003894 spin_unlock_bh(&conn->cmd_lock);
3895
3896 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003897
Nicholas Bellingerd2701902011-10-09 01:48:14 -07003898 iscsit_free_cmd(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003899
3900 spin_lock_bh(&conn->cmd_lock);
3901 }
3902 spin_unlock_bh(&conn->cmd_lock);
3903}
3904
3905static void iscsit_stop_timers_for_cmds(
3906 struct iscsi_conn *conn)
3907{
3908 struct iscsi_cmd *cmd;
3909
3910 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003911 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003912 if (cmd->data_direction == DMA_TO_DEVICE)
3913 iscsit_stop_dataout_timer(cmd);
3914 }
3915 spin_unlock_bh(&conn->cmd_lock);
3916}
3917
3918int iscsit_close_connection(
3919 struct iscsi_conn *conn)
3920{
3921 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
3922 struct iscsi_session *sess = conn->sess;
3923
3924 pr_debug("Closing iSCSI connection CID %hu on SID:"
3925 " %u\n", conn->cid, sess->sid);
3926 /*
3927 * Always up conn_logout_comp just in case the RX Thread is sleeping
3928 * and the logout response never got sent because the connection
3929 * failed.
3930 */
3931 complete(&conn->conn_logout_comp);
3932
3933 iscsi_release_thread_set(conn);
3934
3935 iscsit_stop_timers_for_cmds(conn);
3936 iscsit_stop_nopin_response_timer(conn);
3937 iscsit_stop_nopin_timer(conn);
3938 iscsit_free_queue_reqs_for_conn(conn);
3939
3940 /*
3941 * During Connection recovery drop unacknowledged out of order
3942 * commands for this connection, and prepare the other commands
3943 * for realligence.
3944 *
3945 * During normal operation clear the out of order commands (but
3946 * do not free the struct iscsi_ooo_cmdsn's) and release all
3947 * struct iscsi_cmds.
3948 */
3949 if (atomic_read(&conn->connection_recovery)) {
3950 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
3951 iscsit_prepare_cmds_for_realligance(conn);
3952 } else {
3953 iscsit_clear_ooo_cmdsns_for_conn(conn);
3954 iscsit_release_commands_from_conn(conn);
3955 }
3956
3957 /*
3958 * Handle decrementing session or connection usage count if
3959 * a logout response was not able to be sent because the
3960 * connection failed. Fall back to Session Recovery here.
3961 */
3962 if (atomic_read(&conn->conn_logout_remove)) {
3963 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
3964 iscsit_dec_conn_usage_count(conn);
3965 iscsit_dec_session_usage_count(sess);
3966 }
3967 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
3968 iscsit_dec_conn_usage_count(conn);
3969
3970 atomic_set(&conn->conn_logout_remove, 0);
3971 atomic_set(&sess->session_reinstatement, 0);
3972 atomic_set(&sess->session_fall_back_to_erl0, 1);
3973 }
3974
3975 spin_lock_bh(&sess->conn_lock);
3976 list_del(&conn->conn_list);
3977
3978 /*
3979 * Attempt to let the Initiator know this connection failed by
3980 * sending an Connection Dropped Async Message on another
3981 * active connection.
3982 */
3983 if (atomic_read(&conn->connection_recovery))
3984 iscsit_build_conn_drop_async_message(conn);
3985
3986 spin_unlock_bh(&sess->conn_lock);
3987
3988 /*
3989 * If connection reinstatement is being performed on this connection,
3990 * up the connection reinstatement semaphore that is being blocked on
3991 * in iscsit_cause_connection_reinstatement().
3992 */
3993 spin_lock_bh(&conn->state_lock);
3994 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
3995 spin_unlock_bh(&conn->state_lock);
3996 complete(&conn->conn_wait_comp);
3997 wait_for_completion(&conn->conn_post_wait_comp);
3998 spin_lock_bh(&conn->state_lock);
3999 }
4000
4001 /*
4002 * If connection reinstatement is being performed on this connection
4003 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4004 * connection wait rcfr semaphore that is being blocked on
4005 * an iscsit_connection_reinstatement_rcfr().
4006 */
4007 if (atomic_read(&conn->connection_wait_rcfr)) {
4008 spin_unlock_bh(&conn->state_lock);
4009 complete(&conn->conn_wait_rcfr_comp);
4010 wait_for_completion(&conn->conn_post_wait_comp);
4011 spin_lock_bh(&conn->state_lock);
4012 }
4013 atomic_set(&conn->connection_reinstatement, 1);
4014 spin_unlock_bh(&conn->state_lock);
4015
4016 /*
4017 * If any other processes are accessing this connection pointer we
4018 * must wait until they have completed.
4019 */
4020 iscsit_check_conn_usage_count(conn);
4021
4022 if (conn->conn_rx_hash.tfm)
4023 crypto_free_hash(conn->conn_rx_hash.tfm);
4024 if (conn->conn_tx_hash.tfm)
4025 crypto_free_hash(conn->conn_tx_hash.tfm);
4026
4027 if (conn->conn_cpumask)
4028 free_cpumask_var(conn->conn_cpumask);
4029
4030 kfree(conn->conn_ops);
4031 conn->conn_ops = NULL;
4032
Al Virobf6932f2012-07-21 08:55:18 +01004033 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004034 sock_release(conn->sock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004035 conn->thread_set = NULL;
4036
4037 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4038 conn->conn_state = TARG_CONN_STATE_FREE;
4039 kfree(conn);
4040
4041 spin_lock_bh(&sess->conn_lock);
4042 atomic_dec(&sess->nconn);
4043 pr_debug("Decremented iSCSI connection count to %hu from node:"
4044 " %s\n", atomic_read(&sess->nconn),
4045 sess->sess_ops->InitiatorName);
4046 /*
4047 * Make sure that if one connection fails in an non ERL=2 iSCSI
4048 * Session that they all fail.
4049 */
4050 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4051 !atomic_read(&sess->session_logout))
4052 atomic_set(&sess->session_fall_back_to_erl0, 1);
4053
4054 /*
4055 * If this was not the last connection in the session, and we are
4056 * performing session reinstatement or falling back to ERL=0, call
4057 * iscsit_stop_session() without sleeping to shutdown the other
4058 * active connections.
4059 */
4060 if (atomic_read(&sess->nconn)) {
4061 if (!atomic_read(&sess->session_reinstatement) &&
4062 !atomic_read(&sess->session_fall_back_to_erl0)) {
4063 spin_unlock_bh(&sess->conn_lock);
4064 return 0;
4065 }
4066 if (!atomic_read(&sess->session_stop_active)) {
4067 atomic_set(&sess->session_stop_active, 1);
4068 spin_unlock_bh(&sess->conn_lock);
4069 iscsit_stop_session(sess, 0, 0);
4070 return 0;
4071 }
4072 spin_unlock_bh(&sess->conn_lock);
4073 return 0;
4074 }
4075
4076 /*
4077 * If this was the last connection in the session and one of the
4078 * following is occurring:
4079 *
4080 * Session Reinstatement is not being performed, and are falling back
4081 * to ERL=0 call iscsit_close_session().
4082 *
4083 * Session Logout was requested. iscsit_close_session() will be called
4084 * elsewhere.
4085 *
4086 * Session Continuation is not being performed, start the Time2Retain
4087 * handler and check if sleep_on_sess_wait_sem is active.
4088 */
4089 if (!atomic_read(&sess->session_reinstatement) &&
4090 atomic_read(&sess->session_fall_back_to_erl0)) {
4091 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004092 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004093
4094 return 0;
4095 } else if (atomic_read(&sess->session_logout)) {
4096 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4097 sess->session_state = TARG_SESS_STATE_FREE;
4098 spin_unlock_bh(&sess->conn_lock);
4099
4100 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4101 complete(&sess->session_wait_comp);
4102
4103 return 0;
4104 } else {
4105 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4106 sess->session_state = TARG_SESS_STATE_FAILED;
4107
4108 if (!atomic_read(&sess->session_continuation)) {
4109 spin_unlock_bh(&sess->conn_lock);
4110 iscsit_start_time2retain_handler(sess);
4111 } else
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 }
4119 spin_unlock_bh(&sess->conn_lock);
4120
4121 return 0;
4122}
4123
4124int iscsit_close_session(struct iscsi_session *sess)
4125{
4126 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4127 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4128
4129 if (atomic_read(&sess->nconn)) {
4130 pr_err("%d connection(s) still exist for iSCSI session"
4131 " to %s\n", atomic_read(&sess->nconn),
4132 sess->sess_ops->InitiatorName);
4133 BUG();
4134 }
4135
4136 spin_lock_bh(&se_tpg->session_lock);
4137 atomic_set(&sess->session_logout, 1);
4138 atomic_set(&sess->session_reinstatement, 1);
4139 iscsit_stop_time2retain_timer(sess);
4140 spin_unlock_bh(&se_tpg->session_lock);
4141
4142 /*
4143 * transport_deregister_session_configfs() will clear the
4144 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4145 * can be setting it again with __transport_register_session() in
4146 * iscsi_post_login_handler() again after the iscsit_stop_session()
4147 * completes in iscsi_np context.
4148 */
4149 transport_deregister_session_configfs(sess->se_sess);
4150
4151 /*
4152 * If any other processes are accessing this session pointer we must
4153 * wait until they have completed. If we are in an interrupt (the
4154 * time2retain handler) and contain and active session usage count we
4155 * restart the timer and exit.
4156 */
4157 if (!in_interrupt()) {
4158 if (iscsit_check_session_usage_count(sess) == 1)
4159 iscsit_stop_session(sess, 1, 1);
4160 } else {
4161 if (iscsit_check_session_usage_count(sess) == 2) {
4162 atomic_set(&sess->session_logout, 0);
4163 iscsit_start_time2retain_handler(sess);
4164 return 0;
4165 }
4166 }
4167
4168 transport_deregister_session(sess->se_sess);
4169
4170 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4171 iscsit_free_connection_recovery_entires(sess);
4172
4173 iscsit_free_all_ooo_cmdsns(sess);
4174
4175 spin_lock_bh(&se_tpg->session_lock);
4176 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4177 sess->session_state = TARG_SESS_STATE_FREE;
4178 pr_debug("Released iSCSI session from node: %s\n",
4179 sess->sess_ops->InitiatorName);
4180 tpg->nsessions--;
4181 if (tpg->tpg_tiqn)
4182 tpg->tpg_tiqn->tiqn_nsessions--;
4183
4184 pr_debug("Decremented number of active iSCSI Sessions on"
4185 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4186
4187 spin_lock(&sess_idr_lock);
4188 idr_remove(&sess_idr, sess->session_index);
4189 spin_unlock(&sess_idr_lock);
4190
4191 kfree(sess->sess_ops);
4192 sess->sess_ops = NULL;
4193 spin_unlock_bh(&se_tpg->session_lock);
4194
4195 kfree(sess);
4196 return 0;
4197}
4198
4199static void iscsit_logout_post_handler_closesession(
4200 struct iscsi_conn *conn)
4201{
4202 struct iscsi_session *sess = conn->sess;
4203
4204 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4205 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4206
4207 atomic_set(&conn->conn_logout_remove, 0);
4208 complete(&conn->conn_logout_comp);
4209
4210 iscsit_dec_conn_usage_count(conn);
4211 iscsit_stop_session(sess, 1, 1);
4212 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004213 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004214}
4215
4216static void iscsit_logout_post_handler_samecid(
4217 struct iscsi_conn *conn)
4218{
4219 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4220 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4221
4222 atomic_set(&conn->conn_logout_remove, 0);
4223 complete(&conn->conn_logout_comp);
4224
4225 iscsit_cause_connection_reinstatement(conn, 1);
4226 iscsit_dec_conn_usage_count(conn);
4227}
4228
4229static void iscsit_logout_post_handler_diffcid(
4230 struct iscsi_conn *conn,
4231 u16 cid)
4232{
4233 struct iscsi_conn *l_conn;
4234 struct iscsi_session *sess = conn->sess;
4235
4236 if (!sess)
4237 return;
4238
4239 spin_lock_bh(&sess->conn_lock);
4240 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4241 if (l_conn->cid == cid) {
4242 iscsit_inc_conn_usage_count(l_conn);
4243 break;
4244 }
4245 }
4246 spin_unlock_bh(&sess->conn_lock);
4247
4248 if (!l_conn)
4249 return;
4250
4251 if (l_conn->sock)
4252 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4253
4254 spin_lock_bh(&l_conn->state_lock);
4255 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4256 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4257 spin_unlock_bh(&l_conn->state_lock);
4258
4259 iscsit_cause_connection_reinstatement(l_conn, 1);
4260 iscsit_dec_conn_usage_count(l_conn);
4261}
4262
4263/*
4264 * Return of 0 causes the TX thread to restart.
4265 */
4266static int iscsit_logout_post_handler(
4267 struct iscsi_cmd *cmd,
4268 struct iscsi_conn *conn)
4269{
4270 int ret = 0;
4271
4272 switch (cmd->logout_reason) {
4273 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4274 switch (cmd->logout_response) {
4275 case ISCSI_LOGOUT_SUCCESS:
4276 case ISCSI_LOGOUT_CLEANUP_FAILED:
4277 default:
4278 iscsit_logout_post_handler_closesession(conn);
4279 break;
4280 }
4281 ret = 0;
4282 break;
4283 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4284 if (conn->cid == cmd->logout_cid) {
4285 switch (cmd->logout_response) {
4286 case ISCSI_LOGOUT_SUCCESS:
4287 case ISCSI_LOGOUT_CLEANUP_FAILED:
4288 default:
4289 iscsit_logout_post_handler_samecid(conn);
4290 break;
4291 }
4292 ret = 0;
4293 } else {
4294 switch (cmd->logout_response) {
4295 case ISCSI_LOGOUT_SUCCESS:
4296 iscsit_logout_post_handler_diffcid(conn,
4297 cmd->logout_cid);
4298 break;
4299 case ISCSI_LOGOUT_CID_NOT_FOUND:
4300 case ISCSI_LOGOUT_CLEANUP_FAILED:
4301 default:
4302 break;
4303 }
4304 ret = 1;
4305 }
4306 break;
4307 case ISCSI_LOGOUT_REASON_RECOVERY:
4308 switch (cmd->logout_response) {
4309 case ISCSI_LOGOUT_SUCCESS:
4310 case ISCSI_LOGOUT_CID_NOT_FOUND:
4311 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4312 case ISCSI_LOGOUT_CLEANUP_FAILED:
4313 default:
4314 break;
4315 }
4316 ret = 1;
4317 break;
4318 default:
4319 break;
4320
4321 }
4322 return ret;
4323}
4324
4325void iscsit_fail_session(struct iscsi_session *sess)
4326{
4327 struct iscsi_conn *conn;
4328
4329 spin_lock_bh(&sess->conn_lock);
4330 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4331 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4332 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4333 }
4334 spin_unlock_bh(&sess->conn_lock);
4335
4336 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4337 sess->session_state = TARG_SESS_STATE_FAILED;
4338}
4339
4340int iscsit_free_session(struct iscsi_session *sess)
4341{
4342 u16 conn_count = atomic_read(&sess->nconn);
4343 struct iscsi_conn *conn, *conn_tmp = NULL;
4344 int is_last;
4345
4346 spin_lock_bh(&sess->conn_lock);
4347 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4348
4349 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4350 conn_list) {
4351 if (conn_count == 0)
4352 break;
4353
4354 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4355 is_last = 1;
4356 } else {
4357 iscsit_inc_conn_usage_count(conn_tmp);
4358 is_last = 0;
4359 }
4360 iscsit_inc_conn_usage_count(conn);
4361
4362 spin_unlock_bh(&sess->conn_lock);
4363 iscsit_cause_connection_reinstatement(conn, 1);
4364 spin_lock_bh(&sess->conn_lock);
4365
4366 iscsit_dec_conn_usage_count(conn);
4367 if (is_last == 0)
4368 iscsit_dec_conn_usage_count(conn_tmp);
4369
4370 conn_count--;
4371 }
4372
4373 if (atomic_read(&sess->nconn)) {
4374 spin_unlock_bh(&sess->conn_lock);
4375 wait_for_completion(&sess->session_wait_comp);
4376 } else
4377 spin_unlock_bh(&sess->conn_lock);
4378
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004379 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004380 return 0;
4381}
4382
4383void iscsit_stop_session(
4384 struct iscsi_session *sess,
4385 int session_sleep,
4386 int connection_sleep)
4387{
4388 u16 conn_count = atomic_read(&sess->nconn);
4389 struct iscsi_conn *conn, *conn_tmp = NULL;
4390 int is_last;
4391
4392 spin_lock_bh(&sess->conn_lock);
4393 if (session_sleep)
4394 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4395
4396 if (connection_sleep) {
4397 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4398 conn_list) {
4399 if (conn_count == 0)
4400 break;
4401
4402 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4403 is_last = 1;
4404 } else {
4405 iscsit_inc_conn_usage_count(conn_tmp);
4406 is_last = 0;
4407 }
4408 iscsit_inc_conn_usage_count(conn);
4409
4410 spin_unlock_bh(&sess->conn_lock);
4411 iscsit_cause_connection_reinstatement(conn, 1);
4412 spin_lock_bh(&sess->conn_lock);
4413
4414 iscsit_dec_conn_usage_count(conn);
4415 if (is_last == 0)
4416 iscsit_dec_conn_usage_count(conn_tmp);
4417 conn_count--;
4418 }
4419 } else {
4420 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4421 iscsit_cause_connection_reinstatement(conn, 0);
4422 }
4423
4424 if (session_sleep && atomic_read(&sess->nconn)) {
4425 spin_unlock_bh(&sess->conn_lock);
4426 wait_for_completion(&sess->session_wait_comp);
4427 } else
4428 spin_unlock_bh(&sess->conn_lock);
4429}
4430
4431int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4432{
4433 struct iscsi_session *sess;
4434 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4435 struct se_session *se_sess, *se_sess_tmp;
4436 int session_count = 0;
4437
4438 spin_lock_bh(&se_tpg->session_lock);
4439 if (tpg->nsessions && !force) {
4440 spin_unlock_bh(&se_tpg->session_lock);
4441 return -1;
4442 }
4443
4444 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4445 sess_list) {
4446 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4447
4448 spin_lock(&sess->conn_lock);
4449 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4450 atomic_read(&sess->session_logout) ||
4451 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4452 spin_unlock(&sess->conn_lock);
4453 continue;
4454 }
4455 atomic_set(&sess->session_reinstatement, 1);
4456 spin_unlock(&sess->conn_lock);
4457 spin_unlock_bh(&se_tpg->session_lock);
4458
4459 iscsit_free_session(sess);
4460 spin_lock_bh(&se_tpg->session_lock);
4461
4462 session_count++;
4463 }
4464 spin_unlock_bh(&se_tpg->session_lock);
4465
4466 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4467 " Group: %hu\n", session_count, tpg->tpgt);
4468 return 0;
4469}
4470
4471MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4472MODULE_VERSION("4.1.x");
4473MODULE_AUTHOR("nab@Linux-iSCSI.org");
4474MODULE_LICENSE("GPL");
4475
4476module_init(iscsi_target_init_module);
4477module_exit(iscsi_target_cleanup_module);