blob: 0aa04a0b91deb55fa6a92d6c8b1734d7897546f8 [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
430int iscsit_del_np_comm(struct iscsi_np *np)
431{
432 if (!np->np_socket)
433 return 0;
434
435 /*
436 * Some network transports allocate their own struct sock->file,
437 * see if we need to free any additional allocated resources.
438 */
439 if (np->np_flags & NPF_SCTP_STRUCT_FILE) {
440 kfree(np->np_socket->file);
441 np->np_socket->file = NULL;
442 }
443
444 sock_release(np->np_socket);
445 return 0;
446}
447
448int iscsit_del_np(struct iscsi_np *np)
449{
450 spin_lock_bh(&np->np_thread_lock);
451 np->np_exports--;
452 if (np->np_exports) {
453 spin_unlock_bh(&np->np_thread_lock);
454 return 0;
455 }
456 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
457 spin_unlock_bh(&np->np_thread_lock);
458
459 if (np->np_thread) {
460 /*
461 * We need to send the signal to wakeup Linux/Net
462 * which may be sleeping in sock_accept()..
463 */
464 send_sig(SIGINT, np->np_thread, 1);
465 kthread_stop(np->np_thread);
466 }
467 iscsit_del_np_comm(np);
468
469 spin_lock_bh(&np_lock);
470 list_del(&np->np_list);
471 spin_unlock_bh(&np_lock);
472
473 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
474 np->np_ip, np->np_port, (np->np_network_transport == ISCSI_TCP) ?
475 "TCP" : "SCTP");
476
477 kfree(np);
478 return 0;
479}
480
481static int __init iscsi_target_init_module(void)
482{
483 int ret = 0;
484
485 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
486
487 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
488 if (!iscsit_global) {
489 pr_err("Unable to allocate memory for iscsit_global\n");
490 return -1;
491 }
492 mutex_init(&auth_id_lock);
493 spin_lock_init(&sess_idr_lock);
494 idr_init(&tiqn_idr);
495 idr_init(&sess_idr);
496
497 ret = iscsi_target_register_configfs();
498 if (ret < 0)
499 goto out;
500
501 ret = iscsi_thread_set_init();
502 if (ret < 0)
503 goto configfs_out;
504
505 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
506 TARGET_THREAD_SET_COUNT) {
507 pr_err("iscsi_allocate_thread_sets() returned"
508 " unexpected value!\n");
509 goto ts_out1;
510 }
511
512 lio_cmd_cache = kmem_cache_create("lio_cmd_cache",
513 sizeof(struct iscsi_cmd), __alignof__(struct iscsi_cmd),
514 0, NULL);
515 if (!lio_cmd_cache) {
516 pr_err("Unable to kmem_cache_create() for"
517 " lio_cmd_cache\n");
518 goto ts_out2;
519 }
520
521 lio_qr_cache = kmem_cache_create("lio_qr_cache",
522 sizeof(struct iscsi_queue_req),
523 __alignof__(struct iscsi_queue_req), 0, NULL);
524 if (!lio_qr_cache) {
525 pr_err("nable to kmem_cache_create() for"
526 " lio_qr_cache\n");
527 goto cmd_out;
528 }
529
530 lio_dr_cache = kmem_cache_create("lio_dr_cache",
531 sizeof(struct iscsi_datain_req),
532 __alignof__(struct iscsi_datain_req), 0, NULL);
533 if (!lio_dr_cache) {
534 pr_err("Unable to kmem_cache_create() for"
535 " lio_dr_cache\n");
536 goto qr_out;
537 }
538
539 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
540 sizeof(struct iscsi_ooo_cmdsn),
541 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
542 if (!lio_ooo_cache) {
543 pr_err("Unable to kmem_cache_create() for"
544 " lio_ooo_cache\n");
545 goto dr_out;
546 }
547
548 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
549 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
550 0, NULL);
551 if (!lio_r2t_cache) {
552 pr_err("Unable to kmem_cache_create() for"
553 " lio_r2t_cache\n");
554 goto ooo_out;
555 }
556
557 if (iscsit_load_discovery_tpg() < 0)
558 goto r2t_out;
559
560 return ret;
561r2t_out:
562 kmem_cache_destroy(lio_r2t_cache);
563ooo_out:
564 kmem_cache_destroy(lio_ooo_cache);
565dr_out:
566 kmem_cache_destroy(lio_dr_cache);
567qr_out:
568 kmem_cache_destroy(lio_qr_cache);
569cmd_out:
570 kmem_cache_destroy(lio_cmd_cache);
571ts_out2:
572 iscsi_deallocate_thread_sets();
573ts_out1:
574 iscsi_thread_set_free();
575configfs_out:
576 iscsi_target_deregister_configfs();
577out:
578 kfree(iscsit_global);
579 return -ENOMEM;
580}
581
582static void __exit iscsi_target_cleanup_module(void)
583{
584 iscsi_deallocate_thread_sets();
585 iscsi_thread_set_free();
586 iscsit_release_discovery_tpg();
587 kmem_cache_destroy(lio_cmd_cache);
588 kmem_cache_destroy(lio_qr_cache);
589 kmem_cache_destroy(lio_dr_cache);
590 kmem_cache_destroy(lio_ooo_cache);
591 kmem_cache_destroy(lio_r2t_cache);
592
593 iscsi_target_deregister_configfs();
594
595 kfree(iscsit_global);
596}
597
Andy Grover8b1e1242012-04-03 15:51:12 -0700598static int iscsit_add_reject(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000599 u8 reason,
600 int fail_conn,
601 unsigned char *buf,
602 struct iscsi_conn *conn)
603{
604 struct iscsi_cmd *cmd;
605 struct iscsi_reject *hdr;
606 int ret;
607
608 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
609 if (!cmd)
610 return -1;
611
612 cmd->iscsi_opcode = ISCSI_OP_REJECT;
613 if (fail_conn)
614 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
615
616 hdr = (struct iscsi_reject *) cmd->pdu;
617 hdr->reason = reason;
618
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100619 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000620 if (!cmd->buf_ptr) {
621 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
622 iscsit_release_cmd(cmd);
623 return -1;
624 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000625
626 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700627 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000628 spin_unlock_bh(&conn->cmd_lock);
629
630 cmd->i_state = ISTATE_SEND_REJECT;
631 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
632
633 ret = wait_for_completion_interruptible(&cmd->reject_comp);
634 if (ret != 0)
635 return -1;
636
637 return (!fail_conn) ? 0 : -1;
638}
639
640int iscsit_add_reject_from_cmd(
641 u8 reason,
642 int fail_conn,
643 int add_to_conn,
644 unsigned char *buf,
645 struct iscsi_cmd *cmd)
646{
647 struct iscsi_conn *conn;
648 struct iscsi_reject *hdr;
649 int ret;
650
651 if (!cmd->conn) {
652 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
653 cmd->init_task_tag);
654 return -1;
655 }
656 conn = cmd->conn;
657
658 cmd->iscsi_opcode = ISCSI_OP_REJECT;
659 if (fail_conn)
660 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
661
662 hdr = (struct iscsi_reject *) cmd->pdu;
663 hdr->reason = reason;
664
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100665 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000666 if (!cmd->buf_ptr) {
667 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
668 iscsit_release_cmd(cmd);
669 return -1;
670 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000671
672 if (add_to_conn) {
673 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700674 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000675 spin_unlock_bh(&conn->cmd_lock);
676 }
677
678 cmd->i_state = ISTATE_SEND_REJECT;
679 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
680
681 ret = wait_for_completion_interruptible(&cmd->reject_comp);
682 if (ret != 0)
683 return -1;
684
685 return (!fail_conn) ? 0 : -1;
686}
687
688/*
689 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700690 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000691 */
692static int iscsit_map_iovec(
693 struct iscsi_cmd *cmd,
694 struct kvec *iov,
695 u32 data_offset,
696 u32 data_length)
697{
698 u32 i = 0;
699 struct scatterlist *sg;
700 unsigned int page_off;
701
702 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700703 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000704 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700705 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000706 page_off = (data_offset % PAGE_SIZE);
707
708 cmd->first_data_sg = sg;
709 cmd->first_data_sg_off = page_off;
710
711 while (data_length) {
712 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
713
714 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
715 iov[i].iov_len = cur_len;
716
717 data_length -= cur_len;
718 page_off = 0;
719 sg = sg_next(sg);
720 i++;
721 }
722
723 cmd->kmapped_nents = i;
724
725 return i;
726}
727
728static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
729{
730 u32 i;
731 struct scatterlist *sg;
732
733 sg = cmd->first_data_sg;
734
735 for (i = 0; i < cmd->kmapped_nents; i++)
736 kunmap(sg_page(&sg[i]));
737}
738
739static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
740{
741 struct iscsi_cmd *cmd;
742
743 conn->exp_statsn = exp_statsn;
744
745 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700746 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000747 spin_lock(&cmd->istate_lock);
748 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
749 (cmd->stat_sn < exp_statsn)) {
750 cmd->i_state = ISTATE_REMOVE;
751 spin_unlock(&cmd->istate_lock);
752 iscsit_add_cmd_to_immediate_queue(cmd, conn,
753 cmd->i_state);
754 continue;
755 }
756 spin_unlock(&cmd->istate_lock);
757 }
758 spin_unlock_bh(&conn->cmd_lock);
759}
760
761static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
762{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700763 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000764
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400765 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000766
767 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
768 if (!cmd->iov_data) {
769 pr_err("Unable to allocate cmd->iov_data\n");
770 return -ENOMEM;
771 }
772
773 cmd->orig_iov_data_count = iov_count;
774 return 0;
775}
776
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000777static int iscsit_handle_scsi_cmd(
778 struct iscsi_conn *conn,
779 unsigned char *buf)
780{
781 int data_direction, cmdsn_ret = 0, immed_ret, ret, transport_ret;
782 int dump_immediate_data = 0, send_check_condition = 0, payload_length;
783 struct iscsi_cmd *cmd = NULL;
784 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700785 int iscsi_task_attr;
786 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000787
788 spin_lock_bh(&conn->sess->session_stats_lock);
789 conn->sess->cmd_pdus++;
790 if (conn->sess->se_sess->se_node_acl) {
791 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
792 conn->sess->se_sess->se_node_acl->num_cmds++;
793 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
794 }
795 spin_unlock_bh(&conn->sess->session_stats_lock);
796
797 hdr = (struct iscsi_scsi_req *) buf;
798 payload_length = ntoh24(hdr->dlength);
799 hdr->itt = be32_to_cpu(hdr->itt);
800 hdr->data_length = be32_to_cpu(hdr->data_length);
801 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
802 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
803
804 /* FIXME; Add checks for AdditionalHeaderSegment */
805
806 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
807 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
808 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
809 " not set. Bad iSCSI Initiator.\n");
810 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
811 buf, conn);
812 }
813
814 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
815 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
816 /*
817 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
818 * that adds support for RESERVE/RELEASE. There is a bug
819 * add with this new functionality that sets R/W bits when
820 * neither CDB carries any READ or WRITE datapayloads.
821 */
822 if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) {
823 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
824 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
825 goto done;
826 }
827
828 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
829 " set when Expected Data Transfer Length is 0 for"
830 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]);
831 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
832 buf, conn);
833 }
834done:
835
836 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
837 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
838 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
839 " MUST be set if Expected Data Transfer Length is not 0."
840 " Bad iSCSI Initiator\n");
841 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
842 buf, conn);
843 }
844
845 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
846 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
847 pr_err("Bidirectional operations not supported!\n");
848 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
849 buf, conn);
850 }
851
852 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
853 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
854 " Scsi Command PDU.\n");
855 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
856 buf, conn);
857 }
858
859 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
860 pr_err("ImmediateData=No but DataSegmentLength=%u,"
861 " protocol error.\n", payload_length);
862 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
863 buf, conn);
864 }
865
866 if ((hdr->data_length == payload_length) &&
867 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
868 pr_err("Expected Data Transfer Length and Length of"
869 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
870 " bit is not set protocol error\n");
871 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
872 buf, conn);
873 }
874
875 if (payload_length > hdr->data_length) {
876 pr_err("DataSegmentLength: %u is greater than"
877 " EDTL: %u, protocol error.\n", payload_length,
878 hdr->data_length);
879 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
880 buf, conn);
881 }
882
883 if (payload_length > conn->conn_ops->MaxRecvDataSegmentLength) {
884 pr_err("DataSegmentLength: %u is greater than"
885 " MaxRecvDataSegmentLength: %u, protocol error.\n",
886 payload_length, conn->conn_ops->MaxRecvDataSegmentLength);
887 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
888 buf, conn);
889 }
890
891 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
892 pr_err("DataSegmentLength: %u is greater than"
893 " FirstBurstLength: %u, protocol error.\n",
894 payload_length, conn->sess->sess_ops->FirstBurstLength);
895 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
896 buf, conn);
897 }
898
899 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
900 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
901 DMA_NONE;
902
Andy Groverd28b11692012-04-03 15:51:22 -0700903 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000904 if (!cmd)
905 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
Andy Groverd28b11692012-04-03 15:51:22 -0700906 buf, conn);
907
908 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700909 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
910 /*
911 * Figure out the SAM Task Attribute for the incoming SCSI CDB
912 */
913 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
914 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
915 sam_task_attr = MSG_SIMPLE_TAG;
916 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
917 sam_task_attr = MSG_ORDERED_TAG;
918 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
919 sam_task_attr = MSG_HEAD_TAG;
920 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
921 sam_task_attr = MSG_ACA_TAG;
922 else {
923 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
924 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
925 sam_task_attr = MSG_SIMPLE_TAG;
926 }
927
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000928 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
929 cmd->i_state = ISTATE_NEW_CMD;
930 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
931 cmd->immediate_data = (payload_length) ? 1 : 0;
932 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
933 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
934 if (cmd->unsolicited_data)
935 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
936
937 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
938 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
939 spin_lock_bh(&conn->sess->ttt_lock);
940 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
941 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
942 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
943 spin_unlock_bh(&conn->sess->ttt_lock);
944 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
945 cmd->targ_xfer_tag = 0xFFFFFFFF;
946 cmd->cmd_sn = hdr->cmdsn;
947 cmd->exp_stat_sn = hdr->exp_statsn;
948 cmd->first_burst_len = payload_length;
949
950 if (cmd->data_direction == DMA_FROM_DEVICE) {
951 struct iscsi_datain_req *dr;
952
953 dr = iscsit_allocate_datain_req();
954 if (!dr)
955 return iscsit_add_reject_from_cmd(
956 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
957 1, 1, buf, cmd);
958
959 iscsit_attach_datain_req(cmd, dr);
960 }
961
962 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700963 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
964 */
965 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Andy Groverebf1d952012-04-03 15:51:24 -0700966 conn->sess->se_sess, hdr->data_length, cmd->data_direction,
Andy Grover065ca1e2012-04-03 15:51:23 -0700967 sam_task_attr, &cmd->sense_buffer[0]);
968
969 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
970 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
971 hdr->cmdsn, hdr->data_length, payload_length, conn->cid);
972
973 /*
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000974 * The CDB is going to an se_device_t.
975 */
Andy Grover4f269982012-01-19 13:39:14 -0800976 ret = transport_lookup_cmd_lun(&cmd->se_cmd,
977 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000978 if (ret < 0) {
979 if (cmd->se_cmd.scsi_sense_reason == TCM_NON_EXISTENT_LUN) {
980 pr_debug("Responding to non-acl'ed,"
981 " non-existent or non-exported iSCSI LUN:"
982 " 0x%016Lx\n", get_unaligned_le64(&hdr->lun));
983 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000984 send_check_condition = 1;
985 goto attach_cmd;
986 }
Andy Grovera12f41f2012-04-03 15:51:20 -0700987
988 transport_ret = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000989 if (transport_ret == -ENOMEM) {
990 return iscsit_add_reject_from_cmd(
991 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
992 1, 1, buf, cmd);
Nicholas Bellinger00fdc6b2012-03-13 18:20:11 -0700993 } else if (transport_ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000994 /*
995 * Unsupported SAM Opcode. CHECK_CONDITION will be sent
996 * in iscsit_execute_cmd() during the CmdSN OOO Execution
997 * Mechinism.
998 */
999 send_check_condition = 1;
1000 } else {
Andy Grover4334e492012-04-03 15:51:25 -07001001 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001002 return iscsit_add_reject_from_cmd(
1003 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1004 1, 1, buf, cmd);
1005 }
1006
1007attach_cmd:
1008 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001009 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001010 spin_unlock_bh(&conn->cmd_lock);
1011 /*
1012 * Check if we need to delay processing because of ALUA
1013 * Active/NonOptimized primary access state..
1014 */
1015 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001016
1017 ret = iscsit_allocate_iovecs(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001018 if (ret < 0)
1019 return iscsit_add_reject_from_cmd(
1020 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
Nicholas Bellingercd931ee2012-01-16 17:11:54 -08001021 1, 0, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001022 /*
1023 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1024 * the Immediate Bit is not set, and no Immediate
1025 * Data is attached.
1026 *
1027 * A PDU/CmdSN carrying Immediate Data can only
1028 * be processed after the DataCRC has passed.
1029 * If the DataCRC fails, the CmdSN MUST NOT
1030 * be acknowledged. (See below)
1031 */
1032 if (!cmd->immediate_data) {
1033 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001034 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1035 return 0;
1036 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001037 return iscsit_add_reject_from_cmd(
1038 ISCSI_REASON_PROTOCOL_ERROR,
1039 1, 0, buf, cmd);
1040 }
1041
1042 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
1043
1044 /*
1045 * If no Immediate Data is attached, it's OK to return now.
1046 */
1047 if (!cmd->immediate_data) {
1048 if (send_check_condition)
1049 return 0;
1050
1051 if (cmd->unsolicited_data) {
1052 iscsit_set_dataout_sequence_values(cmd);
1053
1054 spin_lock_bh(&cmd->dataout_timeout_lock);
1055 iscsit_start_dataout_timer(cmd, cmd->conn);
1056 spin_unlock_bh(&cmd->dataout_timeout_lock);
1057 }
1058
1059 return 0;
1060 }
1061
1062 /*
1063 * Early CHECK_CONDITIONs never make it to the transport processing
1064 * thread. They are processed in CmdSN order by
1065 * iscsit_check_received_cmdsn() below.
1066 */
1067 if (send_check_condition) {
1068 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1069 dump_immediate_data = 1;
1070 goto after_immediate_data;
1071 }
1072 /*
1073 * Call directly into transport_generic_new_cmd() to perform
1074 * the backend memory allocation.
1075 */
1076 ret = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001077 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001078 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1079 dump_immediate_data = 1;
1080 goto after_immediate_data;
1081 }
1082
1083 immed_ret = iscsit_handle_immediate_data(cmd, buf, payload_length);
1084after_immediate_data:
1085 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1086 /*
1087 * A PDU/CmdSN carrying Immediate Data passed
1088 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1089 * Immediate Bit is not set.
1090 */
1091 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1092 /*
1093 * Special case for Unsupported SAM WRITE Opcodes
1094 * and ImmediateData=Yes.
1095 */
1096 if (dump_immediate_data) {
1097 if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
1098 return -1;
1099 } else if (cmd->unsolicited_data) {
1100 iscsit_set_dataout_sequence_values(cmd);
1101
1102 spin_lock_bh(&cmd->dataout_timeout_lock);
1103 iscsit_start_dataout_timer(cmd, cmd->conn);
1104 spin_unlock_bh(&cmd->dataout_timeout_lock);
1105 }
1106
1107 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1108 return iscsit_add_reject_from_cmd(
1109 ISCSI_REASON_PROTOCOL_ERROR,
1110 1, 0, buf, cmd);
1111
1112 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1113 /*
1114 * Immediate Data failed DataCRC and ERL>=1,
1115 * silently drop this PDU and let the initiator
1116 * plug the CmdSN gap.
1117 *
1118 * FIXME: Send Unsolicited NOPIN with reserved
1119 * TTT here to help the initiator figure out
1120 * the missing CmdSN, although they should be
1121 * intelligent enough to determine the missing
1122 * CmdSN and issue a retry to plug the sequence.
1123 */
1124 cmd->i_state = ISTATE_REMOVE;
1125 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1126 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1127 return -1;
1128
1129 return 0;
1130}
1131
1132static u32 iscsit_do_crypto_hash_sg(
1133 struct hash_desc *hash,
1134 struct iscsi_cmd *cmd,
1135 u32 data_offset,
1136 u32 data_length,
1137 u32 padding,
1138 u8 *pad_bytes)
1139{
1140 u32 data_crc;
1141 u32 i;
1142 struct scatterlist *sg;
1143 unsigned int page_off;
1144
1145 crypto_hash_init(hash);
1146
1147 sg = cmd->first_data_sg;
1148 page_off = cmd->first_data_sg_off;
1149
1150 i = 0;
1151 while (data_length) {
1152 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1153
1154 crypto_hash_update(hash, &sg[i], cur_len);
1155
1156 data_length -= cur_len;
1157 page_off = 0;
1158 i++;
1159 }
1160
1161 if (padding) {
1162 struct scatterlist pad_sg;
1163
1164 sg_init_one(&pad_sg, pad_bytes, padding);
1165 crypto_hash_update(hash, &pad_sg, padding);
1166 }
1167 crypto_hash_final(hash, (u8 *) &data_crc);
1168
1169 return data_crc;
1170}
1171
1172static void iscsit_do_crypto_hash_buf(
1173 struct hash_desc *hash,
1174 unsigned char *buf,
1175 u32 payload_length,
1176 u32 padding,
1177 u8 *pad_bytes,
1178 u8 *data_crc)
1179{
1180 struct scatterlist sg;
1181
1182 crypto_hash_init(hash);
1183
Jörn Engel8359cf42011-11-24 02:05:51 +01001184 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001185 crypto_hash_update(hash, &sg, payload_length);
1186
1187 if (padding) {
1188 sg_init_one(&sg, pad_bytes, padding);
1189 crypto_hash_update(hash, &sg, padding);
1190 }
1191 crypto_hash_final(hash, data_crc);
1192}
1193
1194static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1195{
1196 int iov_ret, ooo_cmdsn = 0, ret;
1197 u8 data_crc_failed = 0;
1198 u32 checksum, iov_count = 0, padding = 0, rx_got = 0;
1199 u32 rx_size = 0, payload_length;
1200 struct iscsi_cmd *cmd = NULL;
1201 struct se_cmd *se_cmd;
1202 struct iscsi_data *hdr;
1203 struct kvec *iov;
1204 unsigned long flags;
1205
1206 hdr = (struct iscsi_data *) buf;
1207 payload_length = ntoh24(hdr->dlength);
1208 hdr->itt = be32_to_cpu(hdr->itt);
1209 hdr->ttt = be32_to_cpu(hdr->ttt);
1210 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1211 hdr->datasn = be32_to_cpu(hdr->datasn);
1212 hdr->offset = be32_to_cpu(hdr->offset);
1213
1214 if (!payload_length) {
1215 pr_err("DataOUT payload is ZERO, protocol error.\n");
1216 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1217 buf, conn);
1218 }
1219
1220 /* iSCSI write */
1221 spin_lock_bh(&conn->sess->session_stats_lock);
1222 conn->sess->rx_data_octets += payload_length;
1223 if (conn->sess->se_sess->se_node_acl) {
1224 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1225 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1226 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1227 }
1228 spin_unlock_bh(&conn->sess->session_stats_lock);
1229
1230 if (payload_length > conn->conn_ops->MaxRecvDataSegmentLength) {
1231 pr_err("DataSegmentLength: %u is greater than"
1232 " MaxRecvDataSegmentLength: %u\n", payload_length,
1233 conn->conn_ops->MaxRecvDataSegmentLength);
1234 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1235 buf, conn);
1236 }
1237
1238 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1239 payload_length);
1240 if (!cmd)
1241 return 0;
1242
1243 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1244 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1245 hdr->itt, hdr->ttt, hdr->datasn, hdr->offset,
1246 payload_length, conn->cid);
1247
1248 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1249 pr_err("Command ITT: 0x%08x received DataOUT after"
1250 " last DataOUT received, dumping payload\n",
1251 cmd->init_task_tag);
1252 return iscsit_dump_data_payload(conn, payload_length, 1);
1253 }
1254
1255 if (cmd->data_direction != DMA_TO_DEVICE) {
1256 pr_err("Command ITT: 0x%08x received DataOUT for a"
1257 " NON-WRITE command.\n", cmd->init_task_tag);
1258 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1259 1, 0, buf, cmd);
1260 }
1261 se_cmd = &cmd->se_cmd;
1262 iscsit_mod_dataout_timer(cmd);
1263
Andy Groverebf1d952012-04-03 15:51:24 -07001264 if ((hdr->offset + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001265 pr_err("DataOut Offset: %u, Length %u greater than"
1266 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001267 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001268 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
1269 1, 0, buf, cmd);
1270 }
1271
1272 if (cmd->unsolicited_data) {
1273 int dump_unsolicited_data = 0;
1274
1275 if (conn->sess->sess_ops->InitialR2T) {
1276 pr_err("Received unexpected unsolicited data"
1277 " while InitialR2T=Yes, protocol error.\n");
1278 transport_send_check_condition_and_sense(&cmd->se_cmd,
1279 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1280 return -1;
1281 }
1282 /*
1283 * Special case for dealing with Unsolicited DataOUT
1284 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1285 * failures;
1286 */
1287
1288 /* Something's amiss if we're not in WRITE_PENDING state... */
1289 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1290 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1291 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1292
1293 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1294 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) ||
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001295 (se_cmd->se_cmd_flags & SCF_SCSI_CDB_EXCEPTION))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001296 dump_unsolicited_data = 1;
1297 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1298
1299 if (dump_unsolicited_data) {
1300 /*
1301 * Check if a delayed TASK_ABORTED status needs to
1302 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1303 * received with the unsolicitied data out.
1304 */
1305 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1306 iscsit_stop_dataout_timer(cmd);
1307
1308 transport_check_aborted_status(se_cmd,
1309 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1310 return iscsit_dump_data_payload(conn, payload_length, 1);
1311 }
1312 } else {
1313 /*
1314 * For the normal solicited data path:
1315 *
1316 * Check for a delayed TASK_ABORTED status and dump any
1317 * incoming data out payload if one exists. Also, when the
1318 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1319 * data out sequence, we decrement outstanding_r2ts. Once
1320 * outstanding_r2ts reaches zero, go ahead and send the delayed
1321 * TASK_ABORTED status.
1322 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001323 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001324 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1325 if (--cmd->outstanding_r2ts < 1) {
1326 iscsit_stop_dataout_timer(cmd);
1327 transport_check_aborted_status(
1328 se_cmd, 1);
1329 }
1330
1331 return iscsit_dump_data_payload(conn, payload_length, 1);
1332 }
1333 }
1334 /*
1335 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1336 * within-command recovery checks before receiving the payload.
1337 */
1338 ret = iscsit_check_pre_dataout(cmd, buf);
1339 if (ret == DATAOUT_WITHIN_COMMAND_RECOVERY)
1340 return 0;
1341 else if (ret == DATAOUT_CANNOT_RECOVER)
1342 return -1;
1343
1344 rx_size += payload_length;
1345 iov = &cmd->iov_data[0];
1346
1347 iov_ret = iscsit_map_iovec(cmd, iov, hdr->offset, payload_length);
1348 if (iov_ret < 0)
1349 return -1;
1350
1351 iov_count += iov_ret;
1352
1353 padding = ((-payload_length) & 3);
1354 if (padding != 0) {
1355 iov[iov_count].iov_base = cmd->pad_bytes;
1356 iov[iov_count++].iov_len = padding;
1357 rx_size += padding;
1358 pr_debug("Receiving %u padding bytes.\n", padding);
1359 }
1360
1361 if (conn->conn_ops->DataDigest) {
1362 iov[iov_count].iov_base = &checksum;
1363 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1364 rx_size += ISCSI_CRC_LEN;
1365 }
1366
1367 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1368
1369 iscsit_unmap_iovec(cmd);
1370
1371 if (rx_got != rx_size)
1372 return -1;
1373
1374 if (conn->conn_ops->DataDigest) {
1375 u32 data_crc;
1376
1377 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
1378 hdr->offset, payload_length, padding,
1379 cmd->pad_bytes);
1380
1381 if (checksum != data_crc) {
1382 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1383 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1384 " does not match computed 0x%08x\n",
1385 hdr->itt, hdr->offset, payload_length,
1386 hdr->datasn, checksum, data_crc);
1387 data_crc_failed = 1;
1388 } else {
1389 pr_debug("Got CRC32C DataDigest 0x%08x for"
1390 " %u bytes of Data Out\n", checksum,
1391 payload_length);
1392 }
1393 }
1394 /*
1395 * Increment post receive data and CRC values or perform
1396 * within-command recovery.
1397 */
1398 ret = iscsit_check_post_dataout(cmd, buf, data_crc_failed);
1399 if ((ret == DATAOUT_NORMAL) || (ret == DATAOUT_WITHIN_COMMAND_RECOVERY))
1400 return 0;
1401 else if (ret == DATAOUT_SEND_R2T) {
1402 iscsit_set_dataout_sequence_values(cmd);
Andy Grover8b1e1242012-04-03 15:51:12 -07001403 iscsit_build_r2ts_for_cmd(cmd, conn, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001404 } else if (ret == DATAOUT_SEND_TO_TRANSPORT) {
1405 /*
1406 * Handle extra special case for out of order
1407 * Unsolicited Data Out.
1408 */
1409 spin_lock_bh(&cmd->istate_lock);
1410 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1411 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1412 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1413 spin_unlock_bh(&cmd->istate_lock);
1414
1415 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001416 if (ooo_cmdsn)
1417 return 0;
1418 target_execute_cmd(&cmd->se_cmd);
1419 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001420 } else /* DATAOUT_CANNOT_RECOVER */
1421 return -1;
1422
1423 return 0;
1424}
1425
1426static int iscsit_handle_nop_out(
1427 struct iscsi_conn *conn,
1428 unsigned char *buf)
1429{
1430 unsigned char *ping_data = NULL;
1431 int cmdsn_ret, niov = 0, ret = 0, rx_got, rx_size;
1432 u32 checksum, data_crc, padding = 0, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001433 struct iscsi_cmd *cmd = NULL;
1434 struct kvec *iov = NULL;
1435 struct iscsi_nopout *hdr;
1436
1437 hdr = (struct iscsi_nopout *) buf;
1438 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001439 hdr->itt = be32_to_cpu(hdr->itt);
1440 hdr->ttt = be32_to_cpu(hdr->ttt);
1441 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
1442 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1443
1444 if ((hdr->itt == 0xFFFFFFFF) && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1445 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1446 " not set, protocol error.\n");
1447 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1448 buf, conn);
1449 }
1450
1451 if (payload_length > conn->conn_ops->MaxRecvDataSegmentLength) {
1452 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1453 " greater than MaxRecvDataSegmentLength: %u, protocol"
1454 " error.\n", payload_length,
1455 conn->conn_ops->MaxRecvDataSegmentLength);
1456 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1457 buf, conn);
1458 }
1459
1460 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%09x,"
1461 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1462 (hdr->itt == 0xFFFFFFFF) ? "Response" : "Request",
1463 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1464 payload_length);
1465 /*
1466 * This is not a response to a Unsolicited NopIN, which means
1467 * it can either be a NOPOUT ping request (with a valid ITT),
1468 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1469 * Either way, make sure we allocate an struct iscsi_cmd, as both
1470 * can contain ping data.
1471 */
1472 if (hdr->ttt == 0xFFFFFFFF) {
1473 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1474 if (!cmd)
1475 return iscsit_add_reject(
1476 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1477 1, buf, conn);
1478
1479 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1480 cmd->i_state = ISTATE_SEND_NOPIN;
1481 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1482 1 : 0);
1483 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1484 cmd->targ_xfer_tag = 0xFFFFFFFF;
1485 cmd->cmd_sn = hdr->cmdsn;
1486 cmd->exp_stat_sn = hdr->exp_statsn;
1487 cmd->data_direction = DMA_NONE;
1488 }
1489
1490 if (payload_length && (hdr->ttt == 0xFFFFFFFF)) {
1491 rx_size = payload_length;
1492 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1493 if (!ping_data) {
1494 pr_err("Unable to allocate memory for"
1495 " NOPOUT ping data.\n");
1496 ret = -1;
1497 goto out;
1498 }
1499
1500 iov = &cmd->iov_misc[0];
1501 iov[niov].iov_base = ping_data;
1502 iov[niov++].iov_len = payload_length;
1503
1504 padding = ((-payload_length) & 3);
1505 if (padding != 0) {
1506 pr_debug("Receiving %u additional bytes"
1507 " for padding.\n", padding);
1508 iov[niov].iov_base = &cmd->pad_bytes;
1509 iov[niov++].iov_len = padding;
1510 rx_size += padding;
1511 }
1512 if (conn->conn_ops->DataDigest) {
1513 iov[niov].iov_base = &checksum;
1514 iov[niov++].iov_len = ISCSI_CRC_LEN;
1515 rx_size += ISCSI_CRC_LEN;
1516 }
1517
1518 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1519 if (rx_got != rx_size) {
1520 ret = -1;
1521 goto out;
1522 }
1523
1524 if (conn->conn_ops->DataDigest) {
1525 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1526 ping_data, payload_length,
1527 padding, cmd->pad_bytes,
1528 (u8 *)&data_crc);
1529
1530 if (checksum != data_crc) {
1531 pr_err("Ping data CRC32C DataDigest"
1532 " 0x%08x does not match computed 0x%08x\n",
1533 checksum, data_crc);
1534 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1535 pr_err("Unable to recover from"
1536 " NOPOUT Ping DataCRC failure while in"
1537 " ERL=0.\n");
1538 ret = -1;
1539 goto out;
1540 } else {
1541 /*
1542 * Silently drop this PDU and let the
1543 * initiator plug the CmdSN gap.
1544 */
1545 pr_debug("Dropping NOPOUT"
1546 " Command CmdSN: 0x%08x due to"
1547 " DataCRC error.\n", hdr->cmdsn);
1548 ret = 0;
1549 goto out;
1550 }
1551 } else {
1552 pr_debug("Got CRC32C DataDigest"
1553 " 0x%08x for %u bytes of ping data.\n",
1554 checksum, payload_length);
1555 }
1556 }
1557
1558 ping_data[payload_length] = '\0';
1559 /*
1560 * Attach ping data to struct iscsi_cmd->buf_ptr.
1561 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001562 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001563 cmd->buf_ptr_size = payload_length;
1564
1565 pr_debug("Got %u bytes of NOPOUT ping"
1566 " data.\n", payload_length);
1567 pr_debug("Ping Data: \"%s\"\n", ping_data);
1568 }
1569
1570 if (hdr->itt != 0xFFFFFFFF) {
1571 if (!cmd) {
1572 pr_err("Checking CmdSN for NOPOUT,"
1573 " but cmd is NULL!\n");
1574 return -1;
1575 }
1576 /*
1577 * Initiator is expecting a NopIN ping reply,
1578 */
1579 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001580 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001581 spin_unlock_bh(&conn->cmd_lock);
1582
1583 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
1584
1585 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1586 iscsit_add_cmd_to_response_queue(cmd, conn,
1587 cmd->i_state);
1588 return 0;
1589 }
1590
1591 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1592 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1593 ret = 0;
1594 goto ping_out;
1595 }
1596 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1597 return iscsit_add_reject_from_cmd(
1598 ISCSI_REASON_PROTOCOL_ERROR,
1599 1, 0, buf, cmd);
1600
1601 return 0;
1602 }
1603
1604 if (hdr->ttt != 0xFFFFFFFF) {
1605 /*
1606 * This was a response to a unsolicited NOPIN ping.
1607 */
1608 cmd = iscsit_find_cmd_from_ttt(conn, hdr->ttt);
1609 if (!cmd)
1610 return -1;
1611
1612 iscsit_stop_nopin_response_timer(conn);
1613
1614 cmd->i_state = ISTATE_REMOVE;
1615 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1616 iscsit_start_nopin_timer(conn);
1617 } else {
1618 /*
1619 * Initiator is not expecting a NOPIN is response.
1620 * Just ignore for now.
1621 *
1622 * iSCSI v19-91 10.18
1623 * "A NOP-OUT may also be used to confirm a changed
1624 * ExpStatSN if another PDU will not be available
1625 * for a long time."
1626 */
1627 ret = 0;
1628 goto out;
1629 }
1630
1631 return 0;
1632out:
1633 if (cmd)
1634 iscsit_release_cmd(cmd);
1635ping_out:
1636 kfree(ping_data);
1637 return ret;
1638}
1639
1640static int iscsit_handle_task_mgt_cmd(
1641 struct iscsi_conn *conn,
1642 unsigned char *buf)
1643{
1644 struct iscsi_cmd *cmd;
1645 struct se_tmr_req *se_tmr;
1646 struct iscsi_tmr_req *tmr_req;
1647 struct iscsi_tm *hdr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001648 int out_of_order_cmdsn = 0;
1649 int ret;
1650 u8 function;
1651
1652 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001653 hdr->itt = be32_to_cpu(hdr->itt);
1654 hdr->rtt = be32_to_cpu(hdr->rtt);
1655 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
1656 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1657 hdr->refcmdsn = be32_to_cpu(hdr->refcmdsn);
1658 hdr->exp_datasn = be32_to_cpu(hdr->exp_datasn);
1659 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1660 function = hdr->flags;
1661
1662 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1663 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1664 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1665 hdr->rtt, hdr->refcmdsn, conn->cid);
1666
1667 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1668 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1669 (hdr->rtt != ISCSI_RESERVED_TAG))) {
1670 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1671 hdr->rtt = ISCSI_RESERVED_TAG;
1672 }
1673
1674 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1675 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1676 pr_err("Task Management Request TASK_REASSIGN not"
1677 " issued as immediate command, bad iSCSI Initiator"
1678 "implementation\n");
1679 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1680 buf, conn);
1681 }
1682 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1683 (hdr->refcmdsn != ISCSI_RESERVED_TAG))
1684 hdr->refcmdsn = ISCSI_RESERVED_TAG;
1685
Andy Groverd28b11692012-04-03 15:51:22 -07001686 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001687 if (!cmd)
1688 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
Andy Groverd28b11692012-04-03 15:51:22 -07001689 1, buf, conn);
1690
1691 cmd->data_direction = DMA_NONE;
1692
1693 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1694 if (!cmd->tmr_req) {
1695 pr_err("Unable to allocate memory for"
1696 " Task Management command!\n");
1697 return iscsit_add_reject_from_cmd(
1698 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1699 1, 1, buf, cmd);
1700 }
1701
1702 /*
1703 * TASK_REASSIGN for ERL=2 / connection stays inside of
1704 * LIO-Target $FABRIC_MOD
1705 */
1706 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1707
1708 u8 tcm_function;
1709 int ret;
1710
1711 transport_init_se_cmd(&cmd->se_cmd,
1712 &lio_target_fabric_configfs->tf_ops,
1713 conn->sess->se_sess, 0, DMA_NONE,
1714 MSG_SIMPLE_TAG, &cmd->sense_buffer[0]);
1715
1716 switch (function) {
1717 case ISCSI_TM_FUNC_ABORT_TASK:
1718 tcm_function = TMR_ABORT_TASK;
1719 break;
1720 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1721 tcm_function = TMR_ABORT_TASK_SET;
1722 break;
1723 case ISCSI_TM_FUNC_CLEAR_ACA:
1724 tcm_function = TMR_CLEAR_ACA;
1725 break;
1726 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1727 tcm_function = TMR_CLEAR_TASK_SET;
1728 break;
1729 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1730 tcm_function = TMR_LUN_RESET;
1731 break;
1732 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1733 tcm_function = TMR_TARGET_WARM_RESET;
1734 break;
1735 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1736 tcm_function = TMR_TARGET_COLD_RESET;
1737 break;
1738 default:
1739 pr_err("Unknown iSCSI TMR Function:"
1740 " 0x%02x\n", function);
1741 return iscsit_add_reject_from_cmd(
1742 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1743 1, 1, buf, cmd);
1744 }
1745
1746 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1747 tcm_function, GFP_KERNEL);
1748 if (ret < 0)
1749 return iscsit_add_reject_from_cmd(
1750 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1751 1, 1, buf, cmd);
1752
1753 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1754 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755
1756 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1757 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1758 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1759 cmd->init_task_tag = hdr->itt;
1760 cmd->targ_xfer_tag = 0xFFFFFFFF;
1761 cmd->cmd_sn = hdr->cmdsn;
1762 cmd->exp_stat_sn = hdr->exp_statsn;
1763 se_tmr = cmd->se_cmd.se_tmr_req;
1764 tmr_req = cmd->tmr_req;
1765 /*
1766 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1767 */
1768 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001769 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1770 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001771 if (ret < 0) {
1772 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1773 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1774 goto attach;
1775 }
1776 }
1777
1778 switch (function) {
1779 case ISCSI_TM_FUNC_ABORT_TASK:
1780 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
1781 if (se_tmr->response != ISCSI_TMF_RSP_COMPLETE) {
1782 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1783 goto attach;
1784 }
1785 break;
1786 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1787 case ISCSI_TM_FUNC_CLEAR_ACA:
1788 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1789 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1790 break;
1791 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1792 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
1793 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1794 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1795 goto attach;
1796 }
1797 break;
1798 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1799 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
1800 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1801 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1802 goto attach;
1803 }
1804 break;
1805 case ISCSI_TM_FUNC_TASK_REASSIGN:
1806 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1807 /*
1808 * Perform sanity checks on the ExpDataSN only if the
1809 * TASK_REASSIGN was successful.
1810 */
1811 if (se_tmr->response != ISCSI_TMF_RSP_COMPLETE)
1812 break;
1813
1814 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
1815 return iscsit_add_reject_from_cmd(
1816 ISCSI_REASON_BOOKMARK_INVALID, 1, 1,
1817 buf, cmd);
1818 break;
1819 default:
1820 pr_err("Unknown TMR function: 0x%02x, protocol"
1821 " error.\n", function);
1822 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1823 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1824 goto attach;
1825 }
1826
1827 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1828 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1829 se_tmr->call_transport = 1;
1830attach:
1831 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001832 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001833 spin_unlock_bh(&conn->cmd_lock);
1834
1835 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1836 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1837 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1838 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001839 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001840 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001841 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001842 return iscsit_add_reject_from_cmd(
1843 ISCSI_REASON_PROTOCOL_ERROR,
1844 1, 0, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001845 }
1846 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
1847
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001848 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001849 return 0;
1850 /*
1851 * Found the referenced task, send to transport for processing.
1852 */
1853 if (se_tmr->call_transport)
1854 return transport_generic_handle_tmr(&cmd->se_cmd);
1855
1856 /*
1857 * Could not find the referenced LUN, task, or Task Management
1858 * command not authorized or supported. Change state and
1859 * let the tx_thread send the response.
1860 *
1861 * For connection recovery, this is also the default action for
1862 * TMR TASK_REASSIGN.
1863 */
1864 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1865 return 0;
1866}
1867
1868/* #warning FIXME: Support Text Command parameters besides SendTargets */
1869static int iscsit_handle_text_cmd(
1870 struct iscsi_conn *conn,
1871 unsigned char *buf)
1872{
1873 char *text_ptr, *text_in;
1874 int cmdsn_ret, niov = 0, rx_got, rx_size;
1875 u32 checksum = 0, data_crc = 0, payload_length;
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001876 u32 padding = 0, pad_bytes = 0, text_length = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001877 struct iscsi_cmd *cmd;
1878 struct kvec iov[3];
1879 struct iscsi_text *hdr;
1880
1881 hdr = (struct iscsi_text *) buf;
1882 payload_length = ntoh24(hdr->dlength);
1883 hdr->itt = be32_to_cpu(hdr->itt);
1884 hdr->ttt = be32_to_cpu(hdr->ttt);
1885 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
1886 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1887
1888 if (payload_length > conn->conn_ops->MaxRecvDataSegmentLength) {
1889 pr_err("Unable to accept text parameter length: %u"
1890 "greater than MaxRecvDataSegmentLength %u.\n",
1891 payload_length, conn->conn_ops->MaxRecvDataSegmentLength);
1892 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1893 buf, conn);
1894 }
1895
1896 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1897 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1898 hdr->exp_statsn, payload_length);
1899
1900 rx_size = text_length = payload_length;
1901 if (text_length) {
1902 text_in = kzalloc(text_length, GFP_KERNEL);
1903 if (!text_in) {
1904 pr_err("Unable to allocate memory for"
1905 " incoming text parameters\n");
1906 return -1;
1907 }
1908
1909 memset(iov, 0, 3 * sizeof(struct kvec));
1910 iov[niov].iov_base = text_in;
1911 iov[niov++].iov_len = text_length;
1912
1913 padding = ((-payload_length) & 3);
1914 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001915 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001916 iov[niov++].iov_len = padding;
1917 rx_size += padding;
1918 pr_debug("Receiving %u additional bytes"
1919 " for padding.\n", padding);
1920 }
1921 if (conn->conn_ops->DataDigest) {
1922 iov[niov].iov_base = &checksum;
1923 iov[niov++].iov_len = ISCSI_CRC_LEN;
1924 rx_size += ISCSI_CRC_LEN;
1925 }
1926
1927 rx_got = rx_data(conn, &iov[0], niov, rx_size);
1928 if (rx_got != rx_size) {
1929 kfree(text_in);
1930 return -1;
1931 }
1932
1933 if (conn->conn_ops->DataDigest) {
1934 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1935 text_in, text_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001936 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001937 (u8 *)&data_crc);
1938
1939 if (checksum != data_crc) {
1940 pr_err("Text data CRC32C DataDigest"
1941 " 0x%08x does not match computed"
1942 " 0x%08x\n", checksum, data_crc);
1943 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1944 pr_err("Unable to recover from"
1945 " Text Data digest failure while in"
1946 " ERL=0.\n");
1947 kfree(text_in);
1948 return -1;
1949 } else {
1950 /*
1951 * Silently drop this PDU and let the
1952 * initiator plug the CmdSN gap.
1953 */
1954 pr_debug("Dropping Text"
1955 " Command CmdSN: 0x%08x due to"
1956 " DataCRC error.\n", hdr->cmdsn);
1957 kfree(text_in);
1958 return 0;
1959 }
1960 } else {
1961 pr_debug("Got CRC32C DataDigest"
1962 " 0x%08x for %u bytes of text data.\n",
1963 checksum, text_length);
1964 }
1965 }
1966 text_in[text_length - 1] = '\0';
1967 pr_debug("Successfully read %d bytes of text"
1968 " data.\n", text_length);
1969
1970 if (strncmp("SendTargets", text_in, 11) != 0) {
1971 pr_err("Received Text Data that is not"
1972 " SendTargets, cannot continue.\n");
1973 kfree(text_in);
1974 return -1;
1975 }
1976 text_ptr = strchr(text_in, '=');
1977 if (!text_ptr) {
1978 pr_err("No \"=\" separator found in Text Data,"
1979 " cannot continue.\n");
1980 kfree(text_in);
1981 return -1;
1982 }
1983 if (strncmp("=All", text_ptr, 4) != 0) {
1984 pr_err("Unable to locate All value for"
1985 " SendTargets key, cannot continue.\n");
1986 kfree(text_in);
1987 return -1;
1988 }
1989/*#warning Support SendTargets=(iSCSI Target Name/Nothing) values. */
1990 kfree(text_in);
1991 }
1992
1993 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1994 if (!cmd)
1995 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1996 1, buf, conn);
1997
1998 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1999 cmd->i_state = ISTATE_SEND_TEXTRSP;
2000 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2001 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2002 cmd->targ_xfer_tag = 0xFFFFFFFF;
2003 cmd->cmd_sn = hdr->cmdsn;
2004 cmd->exp_stat_sn = hdr->exp_statsn;
2005 cmd->data_direction = DMA_NONE;
2006
2007 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002008 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002009 spin_unlock_bh(&conn->cmd_lock);
2010
2011 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
2012
2013 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2014 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2015 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2016 return iscsit_add_reject_from_cmd(
2017 ISCSI_REASON_PROTOCOL_ERROR,
2018 1, 0, buf, cmd);
2019
2020 return 0;
2021 }
2022
2023 return iscsit_execute_cmd(cmd, 0);
2024}
2025
2026int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2027{
2028 struct iscsi_conn *conn_p;
2029 struct iscsi_session *sess = conn->sess;
2030
2031 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2032 " for SID: %u.\n", conn->cid, conn->sess->sid);
2033
2034 atomic_set(&sess->session_logout, 1);
2035 atomic_set(&conn->conn_logout_remove, 1);
2036 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2037
2038 iscsit_inc_conn_usage_count(conn);
2039 iscsit_inc_session_usage_count(sess);
2040
2041 spin_lock_bh(&sess->conn_lock);
2042 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2043 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2044 continue;
2045
2046 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2047 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2048 }
2049 spin_unlock_bh(&sess->conn_lock);
2050
2051 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2052
2053 return 0;
2054}
2055
2056int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2057{
2058 struct iscsi_conn *l_conn;
2059 struct iscsi_session *sess = conn->sess;
2060
2061 pr_debug("Received logout request CLOSECONNECTION for CID:"
2062 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2063
2064 /*
2065 * A Logout Request with a CLOSECONNECTION reason code for a CID
2066 * can arrive on a connection with a differing CID.
2067 */
2068 if (conn->cid == cmd->logout_cid) {
2069 spin_lock_bh(&conn->state_lock);
2070 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2071 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2072
2073 atomic_set(&conn->conn_logout_remove, 1);
2074 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2075 iscsit_inc_conn_usage_count(conn);
2076
2077 spin_unlock_bh(&conn->state_lock);
2078 } else {
2079 /*
2080 * Handle all different cid CLOSECONNECTION requests in
2081 * iscsit_logout_post_handler_diffcid() as to give enough
2082 * time for any non immediate command's CmdSN to be
2083 * acknowledged on the connection in question.
2084 *
2085 * Here we simply make sure the CID is still around.
2086 */
2087 l_conn = iscsit_get_conn_from_cid(sess,
2088 cmd->logout_cid);
2089 if (!l_conn) {
2090 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2091 iscsit_add_cmd_to_response_queue(cmd, conn,
2092 cmd->i_state);
2093 return 0;
2094 }
2095
2096 iscsit_dec_conn_usage_count(l_conn);
2097 }
2098
2099 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2100
2101 return 0;
2102}
2103
2104int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2105{
2106 struct iscsi_session *sess = conn->sess;
2107
2108 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2109 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2110
2111 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2112 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2113 " while ERL!=2.\n");
2114 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2115 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2116 return 0;
2117 }
2118
2119 if (conn->cid == cmd->logout_cid) {
2120 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2121 " with CID: %hu on CID: %hu, implementation error.\n",
2122 cmd->logout_cid, conn->cid);
2123 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2124 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2125 return 0;
2126 }
2127
2128 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2129
2130 return 0;
2131}
2132
2133static int iscsit_handle_logout_cmd(
2134 struct iscsi_conn *conn,
2135 unsigned char *buf)
2136{
2137 int cmdsn_ret, logout_remove = 0;
2138 u8 reason_code = 0;
2139 struct iscsi_cmd *cmd;
2140 struct iscsi_logout *hdr;
2141 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2142
2143 hdr = (struct iscsi_logout *) buf;
2144 reason_code = (hdr->flags & 0x7f);
2145 hdr->itt = be32_to_cpu(hdr->itt);
2146 hdr->cid = be16_to_cpu(hdr->cid);
2147 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
2148 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
2149
2150 if (tiqn) {
2151 spin_lock(&tiqn->logout_stats.lock);
2152 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2153 tiqn->logout_stats.normal_logouts++;
2154 else
2155 tiqn->logout_stats.abnormal_logouts++;
2156 spin_unlock(&tiqn->logout_stats.lock);
2157 }
2158
2159 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2160 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2161 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2162 hdr->cid, conn->cid);
2163
2164 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2165 pr_err("Received logout request on connection that"
2166 " is not in logged in state, ignoring request.\n");
2167 return 0;
2168 }
2169
2170 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
2171 if (!cmd)
2172 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
2173 buf, conn);
2174
2175 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2176 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2177 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2178 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2179 cmd->targ_xfer_tag = 0xFFFFFFFF;
2180 cmd->cmd_sn = hdr->cmdsn;
2181 cmd->exp_stat_sn = hdr->exp_statsn;
2182 cmd->logout_cid = hdr->cid;
2183 cmd->logout_reason = reason_code;
2184 cmd->data_direction = DMA_NONE;
2185
2186 /*
2187 * We need to sleep in these cases (by returning 1) until the Logout
2188 * Response gets sent in the tx thread.
2189 */
2190 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2191 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
2192 (hdr->cid == conn->cid)))
2193 logout_remove = 1;
2194
2195 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002196 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002197 spin_unlock_bh(&conn->cmd_lock);
2198
2199 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
2200 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
2201
2202 /*
2203 * Immediate commands are executed, well, immediately.
2204 * Non-Immediate Logout Commands are executed in CmdSN order.
2205 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002206 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002207 int ret = iscsit_execute_cmd(cmd, 0);
2208
2209 if (ret < 0)
2210 return ret;
2211 } else {
2212 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2213 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2214 logout_remove = 0;
2215 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2216 return iscsit_add_reject_from_cmd(
2217 ISCSI_REASON_PROTOCOL_ERROR,
2218 1, 0, buf, cmd);
2219 }
2220 }
2221
2222 return logout_remove;
2223}
2224
2225static int iscsit_handle_snack(
2226 struct iscsi_conn *conn,
2227 unsigned char *buf)
2228{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002229 struct iscsi_snack *hdr;
2230
2231 hdr = (struct iscsi_snack *) buf;
2232 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002233 hdr->itt = be32_to_cpu(hdr->itt);
2234 hdr->ttt = be32_to_cpu(hdr->ttt);
2235 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
2236 hdr->begrun = be32_to_cpu(hdr->begrun);
2237 hdr->runlength = be32_to_cpu(hdr->runlength);
2238
2239 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2240 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2241 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2242 hdr->begrun, hdr->runlength, conn->cid);
2243
2244 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2245 pr_err("Initiator sent SNACK request while in"
2246 " ErrorRecoveryLevel=0.\n");
2247 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2248 buf, conn);
2249 }
2250 /*
2251 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2252 * call from inside iscsi_send_recovery_datain_or_r2t().
2253 */
2254 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2255 case 0:
2256 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
2257 hdr->itt, hdr->ttt, hdr->begrun, hdr->runlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002258 case ISCSI_FLAG_SNACK_TYPE_STATUS:
2259 return iscsit_handle_status_snack(conn, hdr->itt, hdr->ttt,
2260 hdr->begrun, hdr->runlength);
2261 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
2262 return iscsit_handle_data_ack(conn, hdr->ttt, hdr->begrun,
2263 hdr->runlength);
2264 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2265 /* FIXME: Support R-Data SNACK */
2266 pr_err("R-Data SNACK Not Supported.\n");
2267 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2268 buf, conn);
2269 default:
2270 pr_err("Unknown SNACK type 0x%02x, protocol"
2271 " error.\n", hdr->flags & 0x0f);
2272 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2273 buf, conn);
2274 }
2275
2276 return 0;
2277}
2278
2279static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2280{
2281 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2282 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2283 wait_for_completion_interruptible_timeout(
2284 &conn->rx_half_close_comp,
2285 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2286 }
2287}
2288
2289static int iscsit_handle_immediate_data(
2290 struct iscsi_cmd *cmd,
2291 unsigned char *buf,
2292 u32 length)
2293{
2294 int iov_ret, rx_got = 0, rx_size = 0;
2295 u32 checksum, iov_count = 0, padding = 0;
2296 struct iscsi_conn *conn = cmd->conn;
2297 struct kvec *iov;
2298
2299 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2300 if (iov_ret < 0)
2301 return IMMEDIATE_DATA_CANNOT_RECOVER;
2302
2303 rx_size = length;
2304 iov_count = iov_ret;
2305 iov = &cmd->iov_data[0];
2306
2307 padding = ((-length) & 3);
2308 if (padding != 0) {
2309 iov[iov_count].iov_base = cmd->pad_bytes;
2310 iov[iov_count++].iov_len = padding;
2311 rx_size += padding;
2312 }
2313
2314 if (conn->conn_ops->DataDigest) {
2315 iov[iov_count].iov_base = &checksum;
2316 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2317 rx_size += ISCSI_CRC_LEN;
2318 }
2319
2320 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2321
2322 iscsit_unmap_iovec(cmd);
2323
2324 if (rx_got != rx_size) {
2325 iscsit_rx_thread_wait_for_tcp(conn);
2326 return IMMEDIATE_DATA_CANNOT_RECOVER;
2327 }
2328
2329 if (conn->conn_ops->DataDigest) {
2330 u32 data_crc;
2331
2332 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2333 cmd->write_data_done, length, padding,
2334 cmd->pad_bytes);
2335
2336 if (checksum != data_crc) {
2337 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2338 " does not match computed 0x%08x\n", checksum,
2339 data_crc);
2340
2341 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2342 pr_err("Unable to recover from"
2343 " Immediate Data digest failure while"
2344 " in ERL=0.\n");
2345 iscsit_add_reject_from_cmd(
2346 ISCSI_REASON_DATA_DIGEST_ERROR,
2347 1, 0, buf, cmd);
2348 return IMMEDIATE_DATA_CANNOT_RECOVER;
2349 } else {
2350 iscsit_add_reject_from_cmd(
2351 ISCSI_REASON_DATA_DIGEST_ERROR,
2352 0, 0, buf, cmd);
2353 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2354 }
2355 } else {
2356 pr_debug("Got CRC32C DataDigest 0x%08x for"
2357 " %u bytes of Immediate Data\n", checksum,
2358 length);
2359 }
2360 }
2361
2362 cmd->write_data_done += length;
2363
Andy Groverebf1d952012-04-03 15:51:24 -07002364 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002365 spin_lock_bh(&cmd->istate_lock);
2366 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2367 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2368 spin_unlock_bh(&cmd->istate_lock);
2369 }
2370
2371 return IMMEDIATE_DATA_NORMAL_OPERATION;
2372}
2373
2374/*
2375 * Called with sess->conn_lock held.
2376 */
2377/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2378 with active network interface */
2379static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2380{
2381 struct iscsi_cmd *cmd;
2382 struct iscsi_conn *conn_p;
2383
2384 /*
2385 * Only send a Asynchronous Message on connections whos network
2386 * interface is still functional.
2387 */
2388 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2389 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2390 iscsit_inc_conn_usage_count(conn_p);
2391 break;
2392 }
2393 }
2394
2395 if (!conn_p)
2396 return;
2397
2398 cmd = iscsit_allocate_cmd(conn_p, GFP_KERNEL);
2399 if (!cmd) {
2400 iscsit_dec_conn_usage_count(conn_p);
2401 return;
2402 }
2403
2404 cmd->logout_cid = conn->cid;
2405 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2406 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2407
2408 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002409 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002410 spin_unlock_bh(&conn_p->cmd_lock);
2411
2412 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2413 iscsit_dec_conn_usage_count(conn_p);
2414}
2415
2416static int iscsit_send_conn_drop_async_message(
2417 struct iscsi_cmd *cmd,
2418 struct iscsi_conn *conn)
2419{
2420 struct iscsi_async *hdr;
2421
2422 cmd->tx_size = ISCSI_HDR_LEN;
2423 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2424
2425 hdr = (struct iscsi_async *) cmd->pdu;
2426 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2427 hdr->flags = ISCSI_FLAG_CMD_FINAL;
2428 cmd->init_task_tag = 0xFFFFFFFF;
2429 cmd->targ_xfer_tag = 0xFFFFFFFF;
2430 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2431 cmd->stat_sn = conn->stat_sn++;
2432 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2433 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2434 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2435 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2436 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2437 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2438 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2439
2440 if (conn->conn_ops->HeaderDigest) {
2441 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2442
2443 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2444 (unsigned char *)hdr, ISCSI_HDR_LEN,
2445 0, NULL, (u8 *)header_digest);
2446
2447 cmd->tx_size += ISCSI_CRC_LEN;
2448 pr_debug("Attaching CRC32C HeaderDigest to"
2449 " Async Message 0x%08x\n", *header_digest);
2450 }
2451
2452 cmd->iov_misc[0].iov_base = cmd->pdu;
2453 cmd->iov_misc[0].iov_len = cmd->tx_size;
2454 cmd->iov_misc_count = 1;
2455
2456 pr_debug("Sending Connection Dropped Async Message StatSN:"
2457 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2458 cmd->logout_cid, conn->cid);
2459 return 0;
2460}
2461
Andy Grover6f3c0e62012-04-03 15:51:09 -07002462static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2463{
2464 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2465 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2466 wait_for_completion_interruptible_timeout(
2467 &conn->tx_half_close_comp,
2468 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2469 }
2470}
2471
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002472static int iscsit_send_data_in(
2473 struct iscsi_cmd *cmd,
Andy Grover6f3c0e62012-04-03 15:51:09 -07002474 struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002475{
2476 int iov_ret = 0, set_statsn = 0;
2477 u32 iov_count = 0, tx_size = 0;
2478 struct iscsi_datain datain;
2479 struct iscsi_datain_req *dr;
2480 struct iscsi_data_rsp *hdr;
2481 struct kvec *iov;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002482 int eodr = 0;
2483 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002484
2485 memset(&datain, 0, sizeof(struct iscsi_datain));
2486 dr = iscsit_get_datain_values(cmd, &datain);
2487 if (!dr) {
2488 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2489 cmd->init_task_tag);
2490 return -1;
2491 }
2492
2493 /*
2494 * Be paranoid and double check the logic for now.
2495 */
Andy Groverebf1d952012-04-03 15:51:24 -07002496 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002497 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2498 " datain.length: %u exceeds cmd->data_length: %u\n",
2499 cmd->init_task_tag, datain.offset, datain.length,
Andy Groverebf1d952012-04-03 15:51:24 -07002500 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002501 return -1;
2502 }
2503
2504 spin_lock_bh(&conn->sess->session_stats_lock);
2505 conn->sess->tx_data_octets += datain.length;
2506 if (conn->sess->se_sess->se_node_acl) {
2507 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2508 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2509 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2510 }
2511 spin_unlock_bh(&conn->sess->session_stats_lock);
2512 /*
2513 * Special case for successfully execution w/ both DATAIN
2514 * and Sense Data.
2515 */
2516 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2517 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2518 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2519 else {
2520 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2521 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2522 iscsit_increment_maxcmdsn(cmd, conn->sess);
2523 cmd->stat_sn = conn->stat_sn++;
2524 set_statsn = 1;
2525 } else if (dr->dr_complete ==
2526 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2527 set_statsn = 1;
2528 }
2529
2530 hdr = (struct iscsi_data_rsp *) cmd->pdu;
2531 memset(hdr, 0, ISCSI_HDR_LEN);
2532 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2533 hdr->flags = datain.flags;
2534 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2535 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2536 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002537 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002538 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2539 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002540 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002541 }
2542 }
2543 hton24(hdr->dlength, datain.length);
2544 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2545 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2546 (struct scsi_lun *)&hdr->lun);
2547 else
2548 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2549
2550 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2551 hdr->ttt = (hdr->flags & ISCSI_FLAG_DATA_ACK) ?
2552 cpu_to_be32(cmd->targ_xfer_tag) :
2553 0xFFFFFFFF;
2554 hdr->statsn = (set_statsn) ? cpu_to_be32(cmd->stat_sn) :
2555 0xFFFFFFFF;
2556 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2557 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2558 hdr->datasn = cpu_to_be32(datain.data_sn);
2559 hdr->offset = cpu_to_be32(datain.offset);
2560
2561 iov = &cmd->iov_data[0];
2562 iov[iov_count].iov_base = cmd->pdu;
2563 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2564 tx_size += ISCSI_HDR_LEN;
2565
2566 if (conn->conn_ops->HeaderDigest) {
2567 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2568
2569 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2570 (unsigned char *)hdr, ISCSI_HDR_LEN,
2571 0, NULL, (u8 *)header_digest);
2572
2573 iov[0].iov_len += ISCSI_CRC_LEN;
2574 tx_size += ISCSI_CRC_LEN;
2575
2576 pr_debug("Attaching CRC32 HeaderDigest"
2577 " for DataIN PDU 0x%08x\n", *header_digest);
2578 }
2579
2580 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], datain.offset, datain.length);
2581 if (iov_ret < 0)
2582 return -1;
2583
2584 iov_count += iov_ret;
2585 tx_size += datain.length;
2586
2587 cmd->padding = ((-datain.length) & 3);
2588 if (cmd->padding) {
2589 iov[iov_count].iov_base = cmd->pad_bytes;
2590 iov[iov_count++].iov_len = cmd->padding;
2591 tx_size += cmd->padding;
2592
2593 pr_debug("Attaching %u padding bytes\n",
2594 cmd->padding);
2595 }
2596 if (conn->conn_ops->DataDigest) {
2597 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2598 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2599
2600 iov[iov_count].iov_base = &cmd->data_crc;
2601 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2602 tx_size += ISCSI_CRC_LEN;
2603
2604 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2605 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2606 }
2607
2608 cmd->iov_data_count = iov_count;
2609 cmd->tx_size = tx_size;
2610
2611 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2612 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2613 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2614 ntohl(hdr->offset), datain.length, conn->cid);
2615
Andy Grover6f3c0e62012-04-03 15:51:09 -07002616 /* sendpage is preferred but can't insert markers */
2617 if (!conn->conn_ops->IFMarker)
2618 ret = iscsit_fe_sendpage_sg(cmd, conn);
2619 else
2620 ret = iscsit_send_tx_data(cmd, conn, 0);
2621
2622 iscsit_unmap_iovec(cmd);
2623
2624 if (ret < 0) {
2625 iscsit_tx_thread_wait_for_tcp(conn);
2626 return ret;
2627 }
2628
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002629 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002630 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002631 2 : 1;
2632 iscsit_free_datain_req(cmd, dr);
2633 }
2634
Andy Grover6f3c0e62012-04-03 15:51:09 -07002635 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002636}
2637
2638static int iscsit_send_logout_response(
2639 struct iscsi_cmd *cmd,
2640 struct iscsi_conn *conn)
2641{
2642 int niov = 0, tx_size;
2643 struct iscsi_conn *logout_conn = NULL;
2644 struct iscsi_conn_recovery *cr = NULL;
2645 struct iscsi_session *sess = conn->sess;
2646 struct kvec *iov;
2647 struct iscsi_logout_rsp *hdr;
2648 /*
2649 * The actual shutting down of Sessions and/or Connections
2650 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2651 * is done in scsi_logout_post_handler().
2652 */
2653 switch (cmd->logout_reason) {
2654 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2655 pr_debug("iSCSI session logout successful, setting"
2656 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2657 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2658 break;
2659 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2660 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2661 break;
2662 /*
2663 * For CLOSECONNECTION logout requests carrying
2664 * a matching logout CID -> local CID, the reference
2665 * for the local CID will have been incremented in
2666 * iscsi_logout_closeconnection().
2667 *
2668 * For CLOSECONNECTION logout requests carrying
2669 * a different CID than the connection it arrived
2670 * on, the connection responding to cmd->logout_cid
2671 * is stopped in iscsit_logout_post_handler_diffcid().
2672 */
2673
2674 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2675 " successful.\n", cmd->logout_cid, conn->cid);
2676 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2677 break;
2678 case ISCSI_LOGOUT_REASON_RECOVERY:
2679 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2680 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2681 break;
2682 /*
2683 * If the connection is still active from our point of view
2684 * force connection recovery to occur.
2685 */
2686 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2687 cmd->logout_cid);
2688 if ((logout_conn)) {
2689 iscsit_connection_reinstatement_rcfr(logout_conn);
2690 iscsit_dec_conn_usage_count(logout_conn);
2691 }
2692
2693 cr = iscsit_get_inactive_connection_recovery_entry(
2694 conn->sess, cmd->logout_cid);
2695 if (!cr) {
2696 pr_err("Unable to locate CID: %hu for"
2697 " REMOVECONNFORRECOVERY Logout Request.\n",
2698 cmd->logout_cid);
2699 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2700 break;
2701 }
2702
2703 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2704
2705 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2706 " for recovery for CID: %hu on CID: %hu successful.\n",
2707 cmd->logout_cid, conn->cid);
2708 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2709 break;
2710 default:
2711 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2712 cmd->logout_reason);
2713 return -1;
2714 }
2715
2716 tx_size = ISCSI_HDR_LEN;
2717 hdr = (struct iscsi_logout_rsp *)cmd->pdu;
2718 memset(hdr, 0, ISCSI_HDR_LEN);
2719 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2720 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2721 hdr->response = cmd->logout_response;
2722 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2723 cmd->stat_sn = conn->stat_sn++;
2724 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2725
2726 iscsit_increment_maxcmdsn(cmd, conn->sess);
2727 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2728 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2729
2730 iov = &cmd->iov_misc[0];
2731 iov[niov].iov_base = cmd->pdu;
2732 iov[niov++].iov_len = ISCSI_HDR_LEN;
2733
2734 if (conn->conn_ops->HeaderDigest) {
2735 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2736
2737 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2738 (unsigned char *)hdr, ISCSI_HDR_LEN,
2739 0, NULL, (u8 *)header_digest);
2740
2741 iov[0].iov_len += ISCSI_CRC_LEN;
2742 tx_size += ISCSI_CRC_LEN;
2743 pr_debug("Attaching CRC32C HeaderDigest to"
2744 " Logout Response 0x%08x\n", *header_digest);
2745 }
2746 cmd->iov_misc_count = niov;
2747 cmd->tx_size = tx_size;
2748
2749 pr_debug("Sending Logout Response ITT: 0x%08x StatSN:"
2750 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2751 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2752 cmd->logout_cid, conn->cid);
2753
2754 return 0;
2755}
2756
2757/*
2758 * Unsolicited NOPIN, either requesting a response or not.
2759 */
2760static int iscsit_send_unsolicited_nopin(
2761 struct iscsi_cmd *cmd,
2762 struct iscsi_conn *conn,
2763 int want_response)
2764{
2765 int tx_size = ISCSI_HDR_LEN;
2766 struct iscsi_nopin *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002767 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002768
2769 hdr = (struct iscsi_nopin *) cmd->pdu;
2770 memset(hdr, 0, ISCSI_HDR_LEN);
2771 hdr->opcode = ISCSI_OP_NOOP_IN;
2772 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2773 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2774 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2775 cmd->stat_sn = conn->stat_sn;
2776 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2777 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2778 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2779
2780 if (conn->conn_ops->HeaderDigest) {
2781 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2782
2783 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2784 (unsigned char *)hdr, ISCSI_HDR_LEN,
2785 0, NULL, (u8 *)header_digest);
2786
2787 tx_size += ISCSI_CRC_LEN;
2788 pr_debug("Attaching CRC32C HeaderDigest to"
2789 " NopIN 0x%08x\n", *header_digest);
2790 }
2791
2792 cmd->iov_misc[0].iov_base = cmd->pdu;
2793 cmd->iov_misc[0].iov_len = tx_size;
2794 cmd->iov_misc_count = 1;
2795 cmd->tx_size = tx_size;
2796
2797 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2798 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2799
Andy Grover6f3c0e62012-04-03 15:51:09 -07002800 ret = iscsit_send_tx_data(cmd, conn, 1);
2801 if (ret < 0) {
2802 iscsit_tx_thread_wait_for_tcp(conn);
2803 return ret;
2804 }
2805
2806 spin_lock_bh(&cmd->istate_lock);
2807 cmd->i_state = want_response ?
2808 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2809 spin_unlock_bh(&cmd->istate_lock);
2810
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002811 return 0;
2812}
2813
2814static int iscsit_send_nopin_response(
2815 struct iscsi_cmd *cmd,
2816 struct iscsi_conn *conn)
2817{
2818 int niov = 0, tx_size;
2819 u32 padding = 0;
2820 struct kvec *iov;
2821 struct iscsi_nopin *hdr;
2822
2823 tx_size = ISCSI_HDR_LEN;
2824 hdr = (struct iscsi_nopin *) cmd->pdu;
2825 memset(hdr, 0, ISCSI_HDR_LEN);
2826 hdr->opcode = ISCSI_OP_NOOP_IN;
2827 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2828 hton24(hdr->dlength, cmd->buf_ptr_size);
2829 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2830 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2831 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2832 cmd->stat_sn = conn->stat_sn++;
2833 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2834
2835 iscsit_increment_maxcmdsn(cmd, conn->sess);
2836 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2837 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2838
2839 iov = &cmd->iov_misc[0];
2840 iov[niov].iov_base = cmd->pdu;
2841 iov[niov++].iov_len = ISCSI_HDR_LEN;
2842
2843 if (conn->conn_ops->HeaderDigest) {
2844 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2845
2846 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2847 (unsigned char *)hdr, ISCSI_HDR_LEN,
2848 0, NULL, (u8 *)header_digest);
2849
2850 iov[0].iov_len += ISCSI_CRC_LEN;
2851 tx_size += ISCSI_CRC_LEN;
2852 pr_debug("Attaching CRC32C HeaderDigest"
2853 " to NopIn 0x%08x\n", *header_digest);
2854 }
2855
2856 /*
2857 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2858 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2859 */
2860 if (cmd->buf_ptr_size) {
2861 iov[niov].iov_base = cmd->buf_ptr;
2862 iov[niov++].iov_len = cmd->buf_ptr_size;
2863 tx_size += cmd->buf_ptr_size;
2864
2865 pr_debug("Echoing back %u bytes of ping"
2866 " data.\n", cmd->buf_ptr_size);
2867
2868 padding = ((-cmd->buf_ptr_size) & 3);
2869 if (padding != 0) {
2870 iov[niov].iov_base = &cmd->pad_bytes;
2871 iov[niov++].iov_len = padding;
2872 tx_size += padding;
2873 pr_debug("Attaching %u additional"
2874 " padding bytes.\n", padding);
2875 }
2876 if (conn->conn_ops->DataDigest) {
2877 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2878 cmd->buf_ptr, cmd->buf_ptr_size,
2879 padding, (u8 *)&cmd->pad_bytes,
2880 (u8 *)&cmd->data_crc);
2881
2882 iov[niov].iov_base = &cmd->data_crc;
2883 iov[niov++].iov_len = ISCSI_CRC_LEN;
2884 tx_size += ISCSI_CRC_LEN;
2885 pr_debug("Attached DataDigest for %u"
2886 " bytes of ping data, CRC 0x%08x\n",
2887 cmd->buf_ptr_size, cmd->data_crc);
2888 }
2889 }
2890
2891 cmd->iov_misc_count = niov;
2892 cmd->tx_size = tx_size;
2893
2894 pr_debug("Sending NOPIN Response ITT: 0x%08x, TTT:"
2895 " 0x%08x, StatSN: 0x%08x, Length %u\n", cmd->init_task_tag,
2896 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2897
2898 return 0;
2899}
2900
Andy Grover6f3c0e62012-04-03 15:51:09 -07002901static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002902 struct iscsi_cmd *cmd,
2903 struct iscsi_conn *conn)
2904{
2905 int tx_size = 0;
2906 struct iscsi_r2t *r2t;
2907 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002908 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002909
2910 r2t = iscsit_get_r2t_from_list(cmd);
2911 if (!r2t)
2912 return -1;
2913
2914 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
2915 memset(hdr, 0, ISCSI_HDR_LEN);
2916 hdr->opcode = ISCSI_OP_R2T;
2917 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2918 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2919 (struct scsi_lun *)&hdr->lun);
2920 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2921 spin_lock_bh(&conn->sess->ttt_lock);
2922 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2923 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
2924 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2925 spin_unlock_bh(&conn->sess->ttt_lock);
2926 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
2927 hdr->statsn = cpu_to_be32(conn->stat_sn);
2928 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2929 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2930 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
2931 hdr->data_offset = cpu_to_be32(r2t->offset);
2932 hdr->data_length = cpu_to_be32(r2t->xfer_len);
2933
2934 cmd->iov_misc[0].iov_base = cmd->pdu;
2935 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
2936 tx_size += ISCSI_HDR_LEN;
2937
2938 if (conn->conn_ops->HeaderDigest) {
2939 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2940
2941 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2942 (unsigned char *)hdr, ISCSI_HDR_LEN,
2943 0, NULL, (u8 *)header_digest);
2944
2945 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
2946 tx_size += ISCSI_CRC_LEN;
2947 pr_debug("Attaching CRC32 HeaderDigest for R2T"
2948 " PDU 0x%08x\n", *header_digest);
2949 }
2950
2951 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
2952 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
2953 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
2954 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
2955 r2t->offset, r2t->xfer_len, conn->cid);
2956
2957 cmd->iov_misc_count = 1;
2958 cmd->tx_size = tx_size;
2959
2960 spin_lock_bh(&cmd->r2t_lock);
2961 r2t->sent_r2t = 1;
2962 spin_unlock_bh(&cmd->r2t_lock);
2963
Andy Grover6f3c0e62012-04-03 15:51:09 -07002964 ret = iscsit_send_tx_data(cmd, conn, 1);
2965 if (ret < 0) {
2966 iscsit_tx_thread_wait_for_tcp(conn);
2967 return ret;
2968 }
2969
2970 spin_lock_bh(&cmd->dataout_timeout_lock);
2971 iscsit_start_dataout_timer(cmd, conn);
2972 spin_unlock_bh(&cmd->dataout_timeout_lock);
2973
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002974 return 0;
2975}
2976
2977/*
Andy Grover8b1e1242012-04-03 15:51:12 -07002978 * @recovery: If called from iscsi_task_reassign_complete_write() for
2979 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002980 */
2981int iscsit_build_r2ts_for_cmd(
2982 struct iscsi_cmd *cmd,
2983 struct iscsi_conn *conn,
Andy Grover8b1e1242012-04-03 15:51:12 -07002984 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002985{
2986 int first_r2t = 1;
2987 u32 offset = 0, xfer_len = 0;
2988
2989 spin_lock_bh(&cmd->r2t_lock);
2990 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
2991 spin_unlock_bh(&cmd->r2t_lock);
2992 return 0;
2993 }
2994
Andy Grover8b1e1242012-04-03 15:51:12 -07002995 if (conn->sess->sess_ops->DataSequenceInOrder &&
2996 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07002997 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002998
2999 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3000 if (conn->sess->sess_ops->DataSequenceInOrder) {
3001 offset = cmd->r2t_offset;
3002
Andy Grover8b1e1242012-04-03 15:51:12 -07003003 if (first_r2t && recovery) {
3004 int new_data_end = offset +
3005 conn->sess->sess_ops->MaxBurstLength -
3006 cmd->next_burst_len;
3007
Andy Groverebf1d952012-04-03 15:51:24 -07003008 if (new_data_end > cmd->se_cmd.data_length)
3009 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003010 else
3011 xfer_len =
3012 conn->sess->sess_ops->MaxBurstLength -
3013 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003014 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003015 int new_data_end = offset +
3016 conn->sess->sess_ops->MaxBurstLength;
3017
Andy Groverebf1d952012-04-03 15:51:24 -07003018 if (new_data_end > cmd->se_cmd.data_length)
3019 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003020 else
3021 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003022 }
3023 cmd->r2t_offset += xfer_len;
3024
Andy Groverebf1d952012-04-03 15:51:24 -07003025 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003026 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3027 } else {
3028 struct iscsi_seq *seq;
3029
3030 seq = iscsit_get_seq_holder_for_r2t(cmd);
3031 if (!seq) {
3032 spin_unlock_bh(&cmd->r2t_lock);
3033 return -1;
3034 }
3035
3036 offset = seq->offset;
3037 xfer_len = seq->xfer_len;
3038
3039 if (cmd->seq_send_order == cmd->seq_count)
3040 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3041 }
3042 cmd->outstanding_r2ts++;
3043 first_r2t = 0;
3044
3045 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3046 spin_unlock_bh(&cmd->r2t_lock);
3047 return -1;
3048 }
3049
3050 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3051 break;
3052 }
3053 spin_unlock_bh(&cmd->r2t_lock);
3054
3055 return 0;
3056}
3057
3058static int iscsit_send_status(
3059 struct iscsi_cmd *cmd,
3060 struct iscsi_conn *conn)
3061{
3062 u8 iov_count = 0, recovery;
3063 u32 padding = 0, tx_size = 0;
3064 struct iscsi_scsi_rsp *hdr;
3065 struct kvec *iov;
3066
3067 recovery = (cmd->i_state != ISTATE_SEND_STATUS);
3068 if (!recovery)
3069 cmd->stat_sn = conn->stat_sn++;
3070
3071 spin_lock_bh(&conn->sess->session_stats_lock);
3072 conn->sess->rsp_pdus++;
3073 spin_unlock_bh(&conn->sess->session_stats_lock);
3074
3075 hdr = (struct iscsi_scsi_rsp *) cmd->pdu;
3076 memset(hdr, 0, ISCSI_HDR_LEN);
3077 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3078 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3079 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3080 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003081 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003082 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3083 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003084 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003085 }
3086 hdr->response = cmd->iscsi_response;
3087 hdr->cmd_status = cmd->se_cmd.scsi_status;
3088 hdr->itt = cpu_to_be32(cmd->init_task_tag);
3089 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3090
3091 iscsit_increment_maxcmdsn(cmd, conn->sess);
3092 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3093 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3094
3095 iov = &cmd->iov_misc[0];
3096 iov[iov_count].iov_base = cmd->pdu;
3097 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3098 tx_size += ISCSI_HDR_LEN;
3099
3100 /*
3101 * Attach SENSE DATA payload to iSCSI Response PDU
3102 */
3103 if (cmd->se_cmd.sense_buffer &&
3104 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3105 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
3106 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
3107 hton24(hdr->dlength, cmd->se_cmd.scsi_sense_length);
3108 iov[iov_count].iov_base = cmd->se_cmd.sense_buffer;
3109 iov[iov_count++].iov_len =
3110 (cmd->se_cmd.scsi_sense_length + padding);
3111 tx_size += cmd->se_cmd.scsi_sense_length;
3112
3113 if (padding) {
3114 memset(cmd->se_cmd.sense_buffer +
3115 cmd->se_cmd.scsi_sense_length, 0, padding);
3116 tx_size += padding;
3117 pr_debug("Adding %u bytes of padding to"
3118 " SENSE.\n", padding);
3119 }
3120
3121 if (conn->conn_ops->DataDigest) {
3122 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3123 cmd->se_cmd.sense_buffer,
3124 (cmd->se_cmd.scsi_sense_length + padding),
3125 0, NULL, (u8 *)&cmd->data_crc);
3126
3127 iov[iov_count].iov_base = &cmd->data_crc;
3128 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3129 tx_size += ISCSI_CRC_LEN;
3130
3131 pr_debug("Attaching CRC32 DataDigest for"
3132 " SENSE, %u bytes CRC 0x%08x\n",
3133 (cmd->se_cmd.scsi_sense_length + padding),
3134 cmd->data_crc);
3135 }
3136
3137 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3138 " Response PDU\n",
3139 cmd->se_cmd.scsi_sense_length);
3140 }
3141
3142 if (conn->conn_ops->HeaderDigest) {
3143 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3144
3145 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3146 (unsigned char *)hdr, ISCSI_HDR_LEN,
3147 0, NULL, (u8 *)header_digest);
3148
3149 iov[0].iov_len += ISCSI_CRC_LEN;
3150 tx_size += ISCSI_CRC_LEN;
3151 pr_debug("Attaching CRC32 HeaderDigest for Response"
3152 " PDU 0x%08x\n", *header_digest);
3153 }
3154
3155 cmd->iov_misc_count = iov_count;
3156 cmd->tx_size = tx_size;
3157
3158 pr_debug("Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3159 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3160 (!recovery) ? "" : "Recovery ", cmd->init_task_tag,
3161 cmd->stat_sn, 0x00, cmd->se_cmd.scsi_status, conn->cid);
3162
3163 return 0;
3164}
3165
3166static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3167{
3168 switch (se_tmr->response) {
3169 case TMR_FUNCTION_COMPLETE:
3170 return ISCSI_TMF_RSP_COMPLETE;
3171 case TMR_TASK_DOES_NOT_EXIST:
3172 return ISCSI_TMF_RSP_NO_TASK;
3173 case TMR_LUN_DOES_NOT_EXIST:
3174 return ISCSI_TMF_RSP_NO_LUN;
3175 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3176 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3177 case TMR_FUNCTION_AUTHORIZATION_FAILED:
3178 return ISCSI_TMF_RSP_AUTH_FAILED;
3179 case TMR_FUNCTION_REJECTED:
3180 default:
3181 return ISCSI_TMF_RSP_REJECTED;
3182 }
3183}
3184
3185static int iscsit_send_task_mgt_rsp(
3186 struct iscsi_cmd *cmd,
3187 struct iscsi_conn *conn)
3188{
3189 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3190 struct iscsi_tm_rsp *hdr;
3191 u32 tx_size = 0;
3192
3193 hdr = (struct iscsi_tm_rsp *) cmd->pdu;
3194 memset(hdr, 0, ISCSI_HDR_LEN);
3195 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003196 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003197 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
3198 hdr->itt = cpu_to_be32(cmd->init_task_tag);
3199 cmd->stat_sn = conn->stat_sn++;
3200 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3201
3202 iscsit_increment_maxcmdsn(cmd, conn->sess);
3203 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3204 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3205
3206 cmd->iov_misc[0].iov_base = cmd->pdu;
3207 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3208 tx_size += ISCSI_HDR_LEN;
3209
3210 if (conn->conn_ops->HeaderDigest) {
3211 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3212
3213 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3214 (unsigned char *)hdr, ISCSI_HDR_LEN,
3215 0, NULL, (u8 *)header_digest);
3216
3217 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3218 tx_size += ISCSI_CRC_LEN;
3219 pr_debug("Attaching CRC32 HeaderDigest for Task"
3220 " Mgmt Response PDU 0x%08x\n", *header_digest);
3221 }
3222
3223 cmd->iov_misc_count = 1;
3224 cmd->tx_size = tx_size;
3225
3226 pr_debug("Built Task Management Response ITT: 0x%08x,"
3227 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3228 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3229
3230 return 0;
3231}
3232
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003233static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3234{
3235 bool ret = false;
3236
3237 if (np->np_sockaddr.ss_family == AF_INET6) {
3238 const struct sockaddr_in6 sin6 = {
3239 .sin6_addr = IN6ADDR_ANY_INIT };
3240 struct sockaddr_in6 *sock_in6 =
3241 (struct sockaddr_in6 *)&np->np_sockaddr;
3242
3243 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3244 sin6.sin6_addr.s6_addr, 16))
3245 ret = true;
3246 } else {
3247 struct sockaddr_in * sock_in =
3248 (struct sockaddr_in *)&np->np_sockaddr;
3249
3250 if (sock_in->sin_addr.s_addr == INADDR_ANY)
3251 ret = true;
3252 }
3253
3254 return ret;
3255}
3256
Andy Grover8b1e1242012-04-03 15:51:12 -07003257#define SENDTARGETS_BUF_LIMIT 32768U
3258
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003259static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3260{
3261 char *payload = NULL;
3262 struct iscsi_conn *conn = cmd->conn;
3263 struct iscsi_portal_group *tpg;
3264 struct iscsi_tiqn *tiqn;
3265 struct iscsi_tpg_np *tpg_np;
3266 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003267 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003268
Andy Grover8b1e1242012-04-03 15:51:12 -07003269 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3270 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003271
3272 payload = kzalloc(buffer_len, GFP_KERNEL);
3273 if (!payload) {
3274 pr_err("Unable to allocate memory for sendtargets"
3275 " response.\n");
3276 return -ENOMEM;
3277 }
3278
3279 spin_lock(&tiqn_lock);
3280 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3281 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3282 len += 1;
3283
3284 if ((len + payload_len) > buffer_len) {
3285 spin_unlock(&tiqn->tiqn_tpg_lock);
3286 end_of_buf = 1;
3287 goto eob;
3288 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003289 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003290 payload_len += len;
3291
3292 spin_lock(&tiqn->tiqn_tpg_lock);
3293 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3294
3295 spin_lock(&tpg->tpg_state_lock);
3296 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3297 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3298 spin_unlock(&tpg->tpg_state_lock);
3299 continue;
3300 }
3301 spin_unlock(&tpg->tpg_state_lock);
3302
3303 spin_lock(&tpg->tpg_np_lock);
3304 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3305 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003306 struct iscsi_np *np = tpg_np->tpg_np;
3307 bool inaddr_any = iscsit_check_inaddr_any(np);
3308
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003309 len = sprintf(buf, "TargetAddress="
3310 "%s%s%s:%hu,%hu",
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003311 (np->np_sockaddr.ss_family == AF_INET6) ?
3312 "[" : "", (inaddr_any == false) ?
3313 np->np_ip : conn->local_ip,
3314 (np->np_sockaddr.ss_family == AF_INET6) ?
3315 "]" : "", (inaddr_any == false) ?
3316 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003317 tpg->tpgt);
3318 len += 1;
3319
3320 if ((len + payload_len) > buffer_len) {
3321 spin_unlock(&tpg->tpg_np_lock);
3322 spin_unlock(&tiqn->tiqn_tpg_lock);
3323 end_of_buf = 1;
3324 goto eob;
3325 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003326 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003327 payload_len += len;
3328 }
3329 spin_unlock(&tpg->tpg_np_lock);
3330 }
3331 spin_unlock(&tiqn->tiqn_tpg_lock);
3332eob:
3333 if (end_of_buf)
3334 break;
3335 }
3336 spin_unlock(&tiqn_lock);
3337
3338 cmd->buf_ptr = payload;
3339
3340 return payload_len;
3341}
3342
3343/*
3344 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3345 * MaxRecvDataSegmentLength.
3346 */
3347static int iscsit_send_text_rsp(
3348 struct iscsi_cmd *cmd,
3349 struct iscsi_conn *conn)
3350{
3351 struct iscsi_text_rsp *hdr;
3352 struct kvec *iov;
3353 u32 padding = 0, tx_size = 0;
3354 int text_length, iov_count = 0;
3355
3356 text_length = iscsit_build_sendtargets_response(cmd);
3357 if (text_length < 0)
3358 return text_length;
3359
3360 padding = ((-text_length) & 3);
3361 if (padding != 0) {
3362 memset(cmd->buf_ptr + text_length, 0, padding);
3363 pr_debug("Attaching %u additional bytes for"
3364 " padding.\n", padding);
3365 }
3366
3367 hdr = (struct iscsi_text_rsp *) cmd->pdu;
3368 memset(hdr, 0, ISCSI_HDR_LEN);
3369 hdr->opcode = ISCSI_OP_TEXT_RSP;
3370 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3371 hton24(hdr->dlength, text_length);
3372 hdr->itt = cpu_to_be32(cmd->init_task_tag);
3373 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3374 cmd->stat_sn = conn->stat_sn++;
3375 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3376
3377 iscsit_increment_maxcmdsn(cmd, conn->sess);
3378 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3379 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3380
3381 iov = &cmd->iov_misc[0];
3382
3383 iov[iov_count].iov_base = cmd->pdu;
3384 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3385 iov[iov_count].iov_base = cmd->buf_ptr;
3386 iov[iov_count++].iov_len = text_length + padding;
3387
3388 tx_size += (ISCSI_HDR_LEN + text_length + padding);
3389
3390 if (conn->conn_ops->HeaderDigest) {
3391 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3392
3393 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3394 (unsigned char *)hdr, ISCSI_HDR_LEN,
3395 0, NULL, (u8 *)header_digest);
3396
3397 iov[0].iov_len += ISCSI_CRC_LEN;
3398 tx_size += ISCSI_CRC_LEN;
3399 pr_debug("Attaching CRC32 HeaderDigest for"
3400 " Text Response PDU 0x%08x\n", *header_digest);
3401 }
3402
3403 if (conn->conn_ops->DataDigest) {
3404 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3405 cmd->buf_ptr, (text_length + padding),
3406 0, NULL, (u8 *)&cmd->data_crc);
3407
3408 iov[iov_count].iov_base = &cmd->data_crc;
3409 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3410 tx_size += ISCSI_CRC_LEN;
3411
3412 pr_debug("Attaching DataDigest for %u bytes of text"
3413 " data, CRC 0x%08x\n", (text_length + padding),
3414 cmd->data_crc);
3415 }
3416
3417 cmd->iov_misc_count = iov_count;
3418 cmd->tx_size = tx_size;
3419
3420 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3421 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3422 text_length, conn->cid);
3423 return 0;
3424}
3425
3426static int iscsit_send_reject(
3427 struct iscsi_cmd *cmd,
3428 struct iscsi_conn *conn)
3429{
3430 u32 iov_count = 0, tx_size = 0;
3431 struct iscsi_reject *hdr;
3432 struct kvec *iov;
3433
3434 hdr = (struct iscsi_reject *) cmd->pdu;
3435 hdr->opcode = ISCSI_OP_REJECT;
3436 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3437 hton24(hdr->dlength, ISCSI_HDR_LEN);
3438 cmd->stat_sn = conn->stat_sn++;
3439 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3440 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3441 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3442
3443 iov = &cmd->iov_misc[0];
3444
3445 iov[iov_count].iov_base = cmd->pdu;
3446 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3447 iov[iov_count].iov_base = cmd->buf_ptr;
3448 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3449
3450 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3451
3452 if (conn->conn_ops->HeaderDigest) {
3453 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3454
3455 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3456 (unsigned char *)hdr, ISCSI_HDR_LEN,
3457 0, NULL, (u8 *)header_digest);
3458
3459 iov[0].iov_len += ISCSI_CRC_LEN;
3460 tx_size += ISCSI_CRC_LEN;
3461 pr_debug("Attaching CRC32 HeaderDigest for"
3462 " REJECT PDU 0x%08x\n", *header_digest);
3463 }
3464
3465 if (conn->conn_ops->DataDigest) {
3466 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3467 (unsigned char *)cmd->buf_ptr, ISCSI_HDR_LEN,
3468 0, NULL, (u8 *)&cmd->data_crc);
3469
3470 iov[iov_count].iov_base = &cmd->data_crc;
3471 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3472 tx_size += ISCSI_CRC_LEN;
3473 pr_debug("Attaching CRC32 DataDigest for REJECT"
3474 " PDU 0x%08x\n", cmd->data_crc);
3475 }
3476
3477 cmd->iov_misc_count = iov_count;
3478 cmd->tx_size = tx_size;
3479
3480 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3481 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3482
3483 return 0;
3484}
3485
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003486void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3487{
3488 struct iscsi_thread_set *ts = conn->thread_set;
3489 int ord, cpu;
3490 /*
3491 * thread_id is assigned from iscsit_global->ts_bitmap from
3492 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3493 *
3494 * Here we use thread_id to determine which CPU that this
3495 * iSCSI connection's iscsi_thread_set will be scheduled to
3496 * execute upon.
3497 */
3498 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003499 for_each_online_cpu(cpu) {
3500 if (ord-- == 0) {
3501 cpumask_set_cpu(cpu, conn->conn_cpumask);
3502 return;
3503 }
3504 }
3505 /*
3506 * This should never be reached..
3507 */
3508 dump_stack();
3509 cpumask_setall(conn->conn_cpumask);
3510}
3511
3512static inline void iscsit_thread_check_cpumask(
3513 struct iscsi_conn *conn,
3514 struct task_struct *p,
3515 int mode)
3516{
3517 char buf[128];
3518 /*
3519 * mode == 1 signals iscsi_target_tx_thread() usage.
3520 * mode == 0 signals iscsi_target_rx_thread() usage.
3521 */
3522 if (mode == 1) {
3523 if (!conn->conn_tx_reset_cpumask)
3524 return;
3525 conn->conn_tx_reset_cpumask = 0;
3526 } else {
3527 if (!conn->conn_rx_reset_cpumask)
3528 return;
3529 conn->conn_rx_reset_cpumask = 0;
3530 }
3531 /*
3532 * Update the CPU mask for this single kthread so that
3533 * both TX and RX kthreads are scheduled to run on the
3534 * same CPU.
3535 */
3536 memset(buf, 0, 128);
3537 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003538 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3539}
3540
Andy Grover6f3c0e62012-04-03 15:51:09 -07003541static int handle_immediate_queue(struct iscsi_conn *conn)
3542{
3543 struct iscsi_queue_req *qr;
3544 struct iscsi_cmd *cmd;
3545 u8 state;
3546 int ret;
3547
3548 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3549 atomic_set(&conn->check_immediate_queue, 0);
3550 cmd = qr->cmd;
3551 state = qr->state;
3552 kmem_cache_free(lio_qr_cache, qr);
3553
3554 switch (state) {
3555 case ISTATE_SEND_R2T:
3556 ret = iscsit_send_r2t(cmd, conn);
3557 if (ret < 0)
3558 goto err;
3559 break;
3560 case ISTATE_REMOVE:
3561 if (cmd->data_direction == DMA_TO_DEVICE)
3562 iscsit_stop_dataout_timer(cmd);
3563
3564 spin_lock_bh(&conn->cmd_lock);
3565 list_del(&cmd->i_conn_node);
3566 spin_unlock_bh(&conn->cmd_lock);
3567
3568 iscsit_free_cmd(cmd);
3569 continue;
3570 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3571 iscsit_mod_nopin_response_timer(conn);
3572 ret = iscsit_send_unsolicited_nopin(cmd,
3573 conn, 1);
3574 if (ret < 0)
3575 goto err;
3576 break;
3577 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3578 ret = iscsit_send_unsolicited_nopin(cmd,
3579 conn, 0);
3580 if (ret < 0)
3581 goto err;
3582 break;
3583 default:
3584 pr_err("Unknown Opcode: 0x%02x ITT:"
3585 " 0x%08x, i_state: %d on CID: %hu\n",
3586 cmd->iscsi_opcode, cmd->init_task_tag, state,
3587 conn->cid);
3588 goto err;
3589 }
3590 }
3591
3592 return 0;
3593
3594err:
3595 return -1;
3596}
3597
3598static int handle_response_queue(struct iscsi_conn *conn)
3599{
3600 struct iscsi_queue_req *qr;
3601 struct iscsi_cmd *cmd;
3602 u8 state;
3603 int ret;
3604
3605 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3606 cmd = qr->cmd;
3607 state = qr->state;
3608 kmem_cache_free(lio_qr_cache, qr);
3609
3610check_rsp_state:
3611 switch (state) {
3612 case ISTATE_SEND_DATAIN:
3613 ret = iscsit_send_data_in(cmd, conn);
3614 if (ret < 0)
3615 goto err;
3616 else if (!ret)
3617 /* more drs */
3618 goto check_rsp_state;
3619 else if (ret == 1) {
3620 /* all done */
3621 spin_lock_bh(&cmd->istate_lock);
3622 cmd->i_state = ISTATE_SENT_STATUS;
3623 spin_unlock_bh(&cmd->istate_lock);
3624 continue;
3625 } else if (ret == 2) {
3626 /* Still must send status,
3627 SCF_TRANSPORT_TASK_SENSE was set */
3628 spin_lock_bh(&cmd->istate_lock);
3629 cmd->i_state = ISTATE_SEND_STATUS;
3630 spin_unlock_bh(&cmd->istate_lock);
3631 state = ISTATE_SEND_STATUS;
3632 goto check_rsp_state;
3633 }
3634
3635 break;
3636 case ISTATE_SEND_STATUS:
3637 case ISTATE_SEND_STATUS_RECOVERY:
3638 ret = iscsit_send_status(cmd, conn);
3639 break;
3640 case ISTATE_SEND_LOGOUTRSP:
3641 ret = iscsit_send_logout_response(cmd, conn);
3642 break;
3643 case ISTATE_SEND_ASYNCMSG:
3644 ret = iscsit_send_conn_drop_async_message(
3645 cmd, conn);
3646 break;
3647 case ISTATE_SEND_NOPIN:
3648 ret = iscsit_send_nopin_response(cmd, conn);
3649 break;
3650 case ISTATE_SEND_REJECT:
3651 ret = iscsit_send_reject(cmd, conn);
3652 break;
3653 case ISTATE_SEND_TASKMGTRSP:
3654 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3655 if (ret != 0)
3656 break;
3657 ret = iscsit_tmr_post_handler(cmd, conn);
3658 if (ret != 0)
3659 iscsit_fall_back_to_erl0(conn->sess);
3660 break;
3661 case ISTATE_SEND_TEXTRSP:
3662 ret = iscsit_send_text_rsp(cmd, conn);
3663 break;
3664 default:
3665 pr_err("Unknown Opcode: 0x%02x ITT:"
3666 " 0x%08x, i_state: %d on CID: %hu\n",
3667 cmd->iscsi_opcode, cmd->init_task_tag,
3668 state, conn->cid);
3669 goto err;
3670 }
3671 if (ret < 0)
3672 goto err;
3673
3674 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3675 iscsit_tx_thread_wait_for_tcp(conn);
3676 iscsit_unmap_iovec(cmd);
3677 goto err;
3678 }
3679 iscsit_unmap_iovec(cmd);
3680
3681 switch (state) {
3682 case ISTATE_SEND_LOGOUTRSP:
3683 if (!iscsit_logout_post_handler(cmd, conn))
3684 goto restart;
3685 /* fall through */
3686 case ISTATE_SEND_STATUS:
3687 case ISTATE_SEND_ASYNCMSG:
3688 case ISTATE_SEND_NOPIN:
3689 case ISTATE_SEND_STATUS_RECOVERY:
3690 case ISTATE_SEND_TEXTRSP:
3691 case ISTATE_SEND_TASKMGTRSP:
3692 spin_lock_bh(&cmd->istate_lock);
3693 cmd->i_state = ISTATE_SENT_STATUS;
3694 spin_unlock_bh(&cmd->istate_lock);
3695 break;
3696 case ISTATE_SEND_REJECT:
3697 if (cmd->cmd_flags & ICF_REJECT_FAIL_CONN) {
3698 cmd->cmd_flags &= ~ICF_REJECT_FAIL_CONN;
3699 complete(&cmd->reject_comp);
3700 goto err;
3701 }
3702 complete(&cmd->reject_comp);
3703 break;
3704 default:
3705 pr_err("Unknown Opcode: 0x%02x ITT:"
3706 " 0x%08x, i_state: %d on CID: %hu\n",
3707 cmd->iscsi_opcode, cmd->init_task_tag,
3708 cmd->i_state, conn->cid);
3709 goto err;
3710 }
3711
3712 if (atomic_read(&conn->check_immediate_queue))
3713 break;
3714 }
3715
3716 return 0;
3717
3718err:
3719 return -1;
3720restart:
3721 return -EAGAIN;
3722}
3723
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003724int iscsi_target_tx_thread(void *arg)
3725{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003726 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003727 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003728 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003729 /*
3730 * Allow ourselves to be interrupted by SIGINT so that a
3731 * connection recovery / failure event can be triggered externally.
3732 */
3733 allow_signal(SIGINT);
3734
3735restart:
3736 conn = iscsi_tx_thread_pre_handler(ts);
3737 if (!conn)
3738 goto out;
3739
Andy Grover6f3c0e62012-04-03 15:51:09 -07003740 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003741
3742 while (!kthread_should_stop()) {
3743 /*
3744 * Ensure that both TX and RX per connection kthreads
3745 * are scheduled to run on the same CPU.
3746 */
3747 iscsit_thread_check_cpumask(conn, current, 1);
3748
3749 schedule_timeout_interruptible(MAX_SCHEDULE_TIMEOUT);
3750
3751 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3752 signal_pending(current))
3753 goto transport_err;
3754
Andy Grover6f3c0e62012-04-03 15:51:09 -07003755 ret = handle_immediate_queue(conn);
3756 if (ret < 0)
3757 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003758
Andy Grover6f3c0e62012-04-03 15:51:09 -07003759 ret = handle_response_queue(conn);
3760 if (ret == -EAGAIN)
3761 goto restart;
3762 else if (ret < 0)
3763 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003764 }
3765
3766transport_err:
3767 iscsit_take_action_for_connection_exit(conn);
3768 goto restart;
3769out:
3770 return 0;
3771}
3772
3773int iscsi_target_rx_thread(void *arg)
3774{
3775 int ret;
3776 u8 buffer[ISCSI_HDR_LEN], opcode;
3777 u32 checksum = 0, digest = 0;
3778 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01003779 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003780 struct kvec iov;
3781 /*
3782 * Allow ourselves to be interrupted by SIGINT so that a
3783 * connection recovery / failure event can be triggered externally.
3784 */
3785 allow_signal(SIGINT);
3786
3787restart:
3788 conn = iscsi_rx_thread_pre_handler(ts);
3789 if (!conn)
3790 goto out;
3791
3792 while (!kthread_should_stop()) {
3793 /*
3794 * Ensure that both TX and RX per connection kthreads
3795 * are scheduled to run on the same CPU.
3796 */
3797 iscsit_thread_check_cpumask(conn, current, 0);
3798
3799 memset(buffer, 0, ISCSI_HDR_LEN);
3800 memset(&iov, 0, sizeof(struct kvec));
3801
3802 iov.iov_base = buffer;
3803 iov.iov_len = ISCSI_HDR_LEN;
3804
3805 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3806 if (ret != ISCSI_HDR_LEN) {
3807 iscsit_rx_thread_wait_for_tcp(conn);
3808 goto transport_err;
3809 }
3810
3811 /*
3812 * Set conn->bad_hdr for use with REJECT PDUs.
3813 */
3814 memcpy(&conn->bad_hdr, &buffer, ISCSI_HDR_LEN);
3815
3816 if (conn->conn_ops->HeaderDigest) {
3817 iov.iov_base = &digest;
3818 iov.iov_len = ISCSI_CRC_LEN;
3819
3820 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3821 if (ret != ISCSI_CRC_LEN) {
3822 iscsit_rx_thread_wait_for_tcp(conn);
3823 goto transport_err;
3824 }
3825
3826 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
3827 buffer, ISCSI_HDR_LEN,
3828 0, NULL, (u8 *)&checksum);
3829
3830 if (digest != checksum) {
3831 pr_err("HeaderDigest CRC32C failed,"
3832 " received 0x%08x, computed 0x%08x\n",
3833 digest, checksum);
3834 /*
3835 * Set the PDU to 0xff so it will intentionally
3836 * hit default in the switch below.
3837 */
3838 memset(buffer, 0xff, ISCSI_HDR_LEN);
3839 spin_lock_bh(&conn->sess->session_stats_lock);
3840 conn->sess->conn_digest_errors++;
3841 spin_unlock_bh(&conn->sess->session_stats_lock);
3842 } else {
3843 pr_debug("Got HeaderDigest CRC32C"
3844 " 0x%08x\n", checksum);
3845 }
3846 }
3847
3848 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
3849 goto transport_err;
3850
3851 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3852
3853 if (conn->sess->sess_ops->SessionType &&
3854 ((!(opcode & ISCSI_OP_TEXT)) ||
3855 (!(opcode & ISCSI_OP_LOGOUT)))) {
3856 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3857 " while in Discovery Session, rejecting.\n", opcode);
3858 iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
3859 buffer, conn);
3860 goto transport_err;
3861 }
3862
3863 switch (opcode) {
3864 case ISCSI_OP_SCSI_CMD:
3865 if (iscsit_handle_scsi_cmd(conn, buffer) < 0)
3866 goto transport_err;
3867 break;
3868 case ISCSI_OP_SCSI_DATA_OUT:
3869 if (iscsit_handle_data_out(conn, buffer) < 0)
3870 goto transport_err;
3871 break;
3872 case ISCSI_OP_NOOP_OUT:
3873 if (iscsit_handle_nop_out(conn, buffer) < 0)
3874 goto transport_err;
3875 break;
3876 case ISCSI_OP_SCSI_TMFUNC:
3877 if (iscsit_handle_task_mgt_cmd(conn, buffer) < 0)
3878 goto transport_err;
3879 break;
3880 case ISCSI_OP_TEXT:
3881 if (iscsit_handle_text_cmd(conn, buffer) < 0)
3882 goto transport_err;
3883 break;
3884 case ISCSI_OP_LOGOUT:
3885 ret = iscsit_handle_logout_cmd(conn, buffer);
3886 if (ret > 0) {
3887 wait_for_completion_timeout(&conn->conn_logout_comp,
3888 SECONDS_FOR_LOGOUT_COMP * HZ);
3889 goto transport_err;
3890 } else if (ret < 0)
3891 goto transport_err;
3892 break;
3893 case ISCSI_OP_SNACK:
3894 if (iscsit_handle_snack(conn, buffer) < 0)
3895 goto transport_err;
3896 break;
3897 default:
3898 pr_err("Got unknown iSCSI OpCode: 0x%02x\n",
3899 opcode);
3900 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3901 pr_err("Cannot recover from unknown"
3902 " opcode while ERL=0, closing iSCSI connection"
3903 ".\n");
3904 goto transport_err;
3905 }
3906 if (!conn->conn_ops->OFMarker) {
3907 pr_err("Unable to recover from unknown"
3908 " opcode while OFMarker=No, closing iSCSI"
3909 " connection.\n");
3910 goto transport_err;
3911 }
3912 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
3913 pr_err("Unable to recover from unknown"
3914 " opcode, closing iSCSI connection.\n");
3915 goto transport_err;
3916 }
3917 break;
3918 }
3919 }
3920
3921transport_err:
3922 if (!signal_pending(current))
3923 atomic_set(&conn->transport_failed, 1);
3924 iscsit_take_action_for_connection_exit(conn);
3925 goto restart;
3926out:
3927 return 0;
3928}
3929
3930static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
3931{
3932 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
3933 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003934 /*
3935 * We expect this function to only ever be called from either RX or TX
3936 * thread context via iscsit_close_connection() once the other context
3937 * has been reset -> returned sleeping pre-handler state.
3938 */
3939 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003940 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003941
Andy Grover2fbb4712012-04-03 15:51:01 -07003942 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003943 spin_unlock_bh(&conn->cmd_lock);
3944
3945 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003946
Nicholas Bellingerd2701902011-10-09 01:48:14 -07003947 iscsit_free_cmd(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003948
3949 spin_lock_bh(&conn->cmd_lock);
3950 }
3951 spin_unlock_bh(&conn->cmd_lock);
3952}
3953
3954static void iscsit_stop_timers_for_cmds(
3955 struct iscsi_conn *conn)
3956{
3957 struct iscsi_cmd *cmd;
3958
3959 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003960 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003961 if (cmd->data_direction == DMA_TO_DEVICE)
3962 iscsit_stop_dataout_timer(cmd);
3963 }
3964 spin_unlock_bh(&conn->cmd_lock);
3965}
3966
3967int iscsit_close_connection(
3968 struct iscsi_conn *conn)
3969{
3970 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
3971 struct iscsi_session *sess = conn->sess;
3972
3973 pr_debug("Closing iSCSI connection CID %hu on SID:"
3974 " %u\n", conn->cid, sess->sid);
3975 /*
3976 * Always up conn_logout_comp just in case the RX Thread is sleeping
3977 * and the logout response never got sent because the connection
3978 * failed.
3979 */
3980 complete(&conn->conn_logout_comp);
3981
3982 iscsi_release_thread_set(conn);
3983
3984 iscsit_stop_timers_for_cmds(conn);
3985 iscsit_stop_nopin_response_timer(conn);
3986 iscsit_stop_nopin_timer(conn);
3987 iscsit_free_queue_reqs_for_conn(conn);
3988
3989 /*
3990 * During Connection recovery drop unacknowledged out of order
3991 * commands for this connection, and prepare the other commands
3992 * for realligence.
3993 *
3994 * During normal operation clear the out of order commands (but
3995 * do not free the struct iscsi_ooo_cmdsn's) and release all
3996 * struct iscsi_cmds.
3997 */
3998 if (atomic_read(&conn->connection_recovery)) {
3999 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4000 iscsit_prepare_cmds_for_realligance(conn);
4001 } else {
4002 iscsit_clear_ooo_cmdsns_for_conn(conn);
4003 iscsit_release_commands_from_conn(conn);
4004 }
4005
4006 /*
4007 * Handle decrementing session or connection usage count if
4008 * a logout response was not able to be sent because the
4009 * connection failed. Fall back to Session Recovery here.
4010 */
4011 if (atomic_read(&conn->conn_logout_remove)) {
4012 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4013 iscsit_dec_conn_usage_count(conn);
4014 iscsit_dec_session_usage_count(sess);
4015 }
4016 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4017 iscsit_dec_conn_usage_count(conn);
4018
4019 atomic_set(&conn->conn_logout_remove, 0);
4020 atomic_set(&sess->session_reinstatement, 0);
4021 atomic_set(&sess->session_fall_back_to_erl0, 1);
4022 }
4023
4024 spin_lock_bh(&sess->conn_lock);
4025 list_del(&conn->conn_list);
4026
4027 /*
4028 * Attempt to let the Initiator know this connection failed by
4029 * sending an Connection Dropped Async Message on another
4030 * active connection.
4031 */
4032 if (atomic_read(&conn->connection_recovery))
4033 iscsit_build_conn_drop_async_message(conn);
4034
4035 spin_unlock_bh(&sess->conn_lock);
4036
4037 /*
4038 * If connection reinstatement is being performed on this connection,
4039 * up the connection reinstatement semaphore that is being blocked on
4040 * in iscsit_cause_connection_reinstatement().
4041 */
4042 spin_lock_bh(&conn->state_lock);
4043 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4044 spin_unlock_bh(&conn->state_lock);
4045 complete(&conn->conn_wait_comp);
4046 wait_for_completion(&conn->conn_post_wait_comp);
4047 spin_lock_bh(&conn->state_lock);
4048 }
4049
4050 /*
4051 * If connection reinstatement is being performed on this connection
4052 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4053 * connection wait rcfr semaphore that is being blocked on
4054 * an iscsit_connection_reinstatement_rcfr().
4055 */
4056 if (atomic_read(&conn->connection_wait_rcfr)) {
4057 spin_unlock_bh(&conn->state_lock);
4058 complete(&conn->conn_wait_rcfr_comp);
4059 wait_for_completion(&conn->conn_post_wait_comp);
4060 spin_lock_bh(&conn->state_lock);
4061 }
4062 atomic_set(&conn->connection_reinstatement, 1);
4063 spin_unlock_bh(&conn->state_lock);
4064
4065 /*
4066 * If any other processes are accessing this connection pointer we
4067 * must wait until they have completed.
4068 */
4069 iscsit_check_conn_usage_count(conn);
4070
4071 if (conn->conn_rx_hash.tfm)
4072 crypto_free_hash(conn->conn_rx_hash.tfm);
4073 if (conn->conn_tx_hash.tfm)
4074 crypto_free_hash(conn->conn_tx_hash.tfm);
4075
4076 if (conn->conn_cpumask)
4077 free_cpumask_var(conn->conn_cpumask);
4078
4079 kfree(conn->conn_ops);
4080 conn->conn_ops = NULL;
4081
4082 if (conn->sock) {
4083 if (conn->conn_flags & CONNFLAG_SCTP_STRUCT_FILE) {
4084 kfree(conn->sock->file);
4085 conn->sock->file = NULL;
4086 }
4087 sock_release(conn->sock);
4088 }
4089 conn->thread_set = NULL;
4090
4091 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4092 conn->conn_state = TARG_CONN_STATE_FREE;
4093 kfree(conn);
4094
4095 spin_lock_bh(&sess->conn_lock);
4096 atomic_dec(&sess->nconn);
4097 pr_debug("Decremented iSCSI connection count to %hu from node:"
4098 " %s\n", atomic_read(&sess->nconn),
4099 sess->sess_ops->InitiatorName);
4100 /*
4101 * Make sure that if one connection fails in an non ERL=2 iSCSI
4102 * Session that they all fail.
4103 */
4104 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4105 !atomic_read(&sess->session_logout))
4106 atomic_set(&sess->session_fall_back_to_erl0, 1);
4107
4108 /*
4109 * If this was not the last connection in the session, and we are
4110 * performing session reinstatement or falling back to ERL=0, call
4111 * iscsit_stop_session() without sleeping to shutdown the other
4112 * active connections.
4113 */
4114 if (atomic_read(&sess->nconn)) {
4115 if (!atomic_read(&sess->session_reinstatement) &&
4116 !atomic_read(&sess->session_fall_back_to_erl0)) {
4117 spin_unlock_bh(&sess->conn_lock);
4118 return 0;
4119 }
4120 if (!atomic_read(&sess->session_stop_active)) {
4121 atomic_set(&sess->session_stop_active, 1);
4122 spin_unlock_bh(&sess->conn_lock);
4123 iscsit_stop_session(sess, 0, 0);
4124 return 0;
4125 }
4126 spin_unlock_bh(&sess->conn_lock);
4127 return 0;
4128 }
4129
4130 /*
4131 * If this was the last connection in the session and one of the
4132 * following is occurring:
4133 *
4134 * Session Reinstatement is not being performed, and are falling back
4135 * to ERL=0 call iscsit_close_session().
4136 *
4137 * Session Logout was requested. iscsit_close_session() will be called
4138 * elsewhere.
4139 *
4140 * Session Continuation is not being performed, start the Time2Retain
4141 * handler and check if sleep_on_sess_wait_sem is active.
4142 */
4143 if (!atomic_read(&sess->session_reinstatement) &&
4144 atomic_read(&sess->session_fall_back_to_erl0)) {
4145 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004146 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004147
4148 return 0;
4149 } else if (atomic_read(&sess->session_logout)) {
4150 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4151 sess->session_state = TARG_SESS_STATE_FREE;
4152 spin_unlock_bh(&sess->conn_lock);
4153
4154 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4155 complete(&sess->session_wait_comp);
4156
4157 return 0;
4158 } else {
4159 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4160 sess->session_state = TARG_SESS_STATE_FAILED;
4161
4162 if (!atomic_read(&sess->session_continuation)) {
4163 spin_unlock_bh(&sess->conn_lock);
4164 iscsit_start_time2retain_handler(sess);
4165 } else
4166 spin_unlock_bh(&sess->conn_lock);
4167
4168 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4169 complete(&sess->session_wait_comp);
4170
4171 return 0;
4172 }
4173 spin_unlock_bh(&sess->conn_lock);
4174
4175 return 0;
4176}
4177
4178int iscsit_close_session(struct iscsi_session *sess)
4179{
4180 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4181 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4182
4183 if (atomic_read(&sess->nconn)) {
4184 pr_err("%d connection(s) still exist for iSCSI session"
4185 " to %s\n", atomic_read(&sess->nconn),
4186 sess->sess_ops->InitiatorName);
4187 BUG();
4188 }
4189
4190 spin_lock_bh(&se_tpg->session_lock);
4191 atomic_set(&sess->session_logout, 1);
4192 atomic_set(&sess->session_reinstatement, 1);
4193 iscsit_stop_time2retain_timer(sess);
4194 spin_unlock_bh(&se_tpg->session_lock);
4195
4196 /*
4197 * transport_deregister_session_configfs() will clear the
4198 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4199 * can be setting it again with __transport_register_session() in
4200 * iscsi_post_login_handler() again after the iscsit_stop_session()
4201 * completes in iscsi_np context.
4202 */
4203 transport_deregister_session_configfs(sess->se_sess);
4204
4205 /*
4206 * If any other processes are accessing this session pointer we must
4207 * wait until they have completed. If we are in an interrupt (the
4208 * time2retain handler) and contain and active session usage count we
4209 * restart the timer and exit.
4210 */
4211 if (!in_interrupt()) {
4212 if (iscsit_check_session_usage_count(sess) == 1)
4213 iscsit_stop_session(sess, 1, 1);
4214 } else {
4215 if (iscsit_check_session_usage_count(sess) == 2) {
4216 atomic_set(&sess->session_logout, 0);
4217 iscsit_start_time2retain_handler(sess);
4218 return 0;
4219 }
4220 }
4221
4222 transport_deregister_session(sess->se_sess);
4223
4224 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4225 iscsit_free_connection_recovery_entires(sess);
4226
4227 iscsit_free_all_ooo_cmdsns(sess);
4228
4229 spin_lock_bh(&se_tpg->session_lock);
4230 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4231 sess->session_state = TARG_SESS_STATE_FREE;
4232 pr_debug("Released iSCSI session from node: %s\n",
4233 sess->sess_ops->InitiatorName);
4234 tpg->nsessions--;
4235 if (tpg->tpg_tiqn)
4236 tpg->tpg_tiqn->tiqn_nsessions--;
4237
4238 pr_debug("Decremented number of active iSCSI Sessions on"
4239 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4240
4241 spin_lock(&sess_idr_lock);
4242 idr_remove(&sess_idr, sess->session_index);
4243 spin_unlock(&sess_idr_lock);
4244
4245 kfree(sess->sess_ops);
4246 sess->sess_ops = NULL;
4247 spin_unlock_bh(&se_tpg->session_lock);
4248
4249 kfree(sess);
4250 return 0;
4251}
4252
4253static void iscsit_logout_post_handler_closesession(
4254 struct iscsi_conn *conn)
4255{
4256 struct iscsi_session *sess = conn->sess;
4257
4258 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4259 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4260
4261 atomic_set(&conn->conn_logout_remove, 0);
4262 complete(&conn->conn_logout_comp);
4263
4264 iscsit_dec_conn_usage_count(conn);
4265 iscsit_stop_session(sess, 1, 1);
4266 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004267 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004268}
4269
4270static void iscsit_logout_post_handler_samecid(
4271 struct iscsi_conn *conn)
4272{
4273 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4274 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4275
4276 atomic_set(&conn->conn_logout_remove, 0);
4277 complete(&conn->conn_logout_comp);
4278
4279 iscsit_cause_connection_reinstatement(conn, 1);
4280 iscsit_dec_conn_usage_count(conn);
4281}
4282
4283static void iscsit_logout_post_handler_diffcid(
4284 struct iscsi_conn *conn,
4285 u16 cid)
4286{
4287 struct iscsi_conn *l_conn;
4288 struct iscsi_session *sess = conn->sess;
4289
4290 if (!sess)
4291 return;
4292
4293 spin_lock_bh(&sess->conn_lock);
4294 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4295 if (l_conn->cid == cid) {
4296 iscsit_inc_conn_usage_count(l_conn);
4297 break;
4298 }
4299 }
4300 spin_unlock_bh(&sess->conn_lock);
4301
4302 if (!l_conn)
4303 return;
4304
4305 if (l_conn->sock)
4306 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4307
4308 spin_lock_bh(&l_conn->state_lock);
4309 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4310 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4311 spin_unlock_bh(&l_conn->state_lock);
4312
4313 iscsit_cause_connection_reinstatement(l_conn, 1);
4314 iscsit_dec_conn_usage_count(l_conn);
4315}
4316
4317/*
4318 * Return of 0 causes the TX thread to restart.
4319 */
4320static int iscsit_logout_post_handler(
4321 struct iscsi_cmd *cmd,
4322 struct iscsi_conn *conn)
4323{
4324 int ret = 0;
4325
4326 switch (cmd->logout_reason) {
4327 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4328 switch (cmd->logout_response) {
4329 case ISCSI_LOGOUT_SUCCESS:
4330 case ISCSI_LOGOUT_CLEANUP_FAILED:
4331 default:
4332 iscsit_logout_post_handler_closesession(conn);
4333 break;
4334 }
4335 ret = 0;
4336 break;
4337 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4338 if (conn->cid == cmd->logout_cid) {
4339 switch (cmd->logout_response) {
4340 case ISCSI_LOGOUT_SUCCESS:
4341 case ISCSI_LOGOUT_CLEANUP_FAILED:
4342 default:
4343 iscsit_logout_post_handler_samecid(conn);
4344 break;
4345 }
4346 ret = 0;
4347 } else {
4348 switch (cmd->logout_response) {
4349 case ISCSI_LOGOUT_SUCCESS:
4350 iscsit_logout_post_handler_diffcid(conn,
4351 cmd->logout_cid);
4352 break;
4353 case ISCSI_LOGOUT_CID_NOT_FOUND:
4354 case ISCSI_LOGOUT_CLEANUP_FAILED:
4355 default:
4356 break;
4357 }
4358 ret = 1;
4359 }
4360 break;
4361 case ISCSI_LOGOUT_REASON_RECOVERY:
4362 switch (cmd->logout_response) {
4363 case ISCSI_LOGOUT_SUCCESS:
4364 case ISCSI_LOGOUT_CID_NOT_FOUND:
4365 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4366 case ISCSI_LOGOUT_CLEANUP_FAILED:
4367 default:
4368 break;
4369 }
4370 ret = 1;
4371 break;
4372 default:
4373 break;
4374
4375 }
4376 return ret;
4377}
4378
4379void iscsit_fail_session(struct iscsi_session *sess)
4380{
4381 struct iscsi_conn *conn;
4382
4383 spin_lock_bh(&sess->conn_lock);
4384 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4385 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4386 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4387 }
4388 spin_unlock_bh(&sess->conn_lock);
4389
4390 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4391 sess->session_state = TARG_SESS_STATE_FAILED;
4392}
4393
4394int iscsit_free_session(struct iscsi_session *sess)
4395{
4396 u16 conn_count = atomic_read(&sess->nconn);
4397 struct iscsi_conn *conn, *conn_tmp = NULL;
4398 int is_last;
4399
4400 spin_lock_bh(&sess->conn_lock);
4401 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4402
4403 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4404 conn_list) {
4405 if (conn_count == 0)
4406 break;
4407
4408 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4409 is_last = 1;
4410 } else {
4411 iscsit_inc_conn_usage_count(conn_tmp);
4412 is_last = 0;
4413 }
4414 iscsit_inc_conn_usage_count(conn);
4415
4416 spin_unlock_bh(&sess->conn_lock);
4417 iscsit_cause_connection_reinstatement(conn, 1);
4418 spin_lock_bh(&sess->conn_lock);
4419
4420 iscsit_dec_conn_usage_count(conn);
4421 if (is_last == 0)
4422 iscsit_dec_conn_usage_count(conn_tmp);
4423
4424 conn_count--;
4425 }
4426
4427 if (atomic_read(&sess->nconn)) {
4428 spin_unlock_bh(&sess->conn_lock);
4429 wait_for_completion(&sess->session_wait_comp);
4430 } else
4431 spin_unlock_bh(&sess->conn_lock);
4432
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004433 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004434 return 0;
4435}
4436
4437void iscsit_stop_session(
4438 struct iscsi_session *sess,
4439 int session_sleep,
4440 int connection_sleep)
4441{
4442 u16 conn_count = atomic_read(&sess->nconn);
4443 struct iscsi_conn *conn, *conn_tmp = NULL;
4444 int is_last;
4445
4446 spin_lock_bh(&sess->conn_lock);
4447 if (session_sleep)
4448 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4449
4450 if (connection_sleep) {
4451 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4452 conn_list) {
4453 if (conn_count == 0)
4454 break;
4455
4456 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4457 is_last = 1;
4458 } else {
4459 iscsit_inc_conn_usage_count(conn_tmp);
4460 is_last = 0;
4461 }
4462 iscsit_inc_conn_usage_count(conn);
4463
4464 spin_unlock_bh(&sess->conn_lock);
4465 iscsit_cause_connection_reinstatement(conn, 1);
4466 spin_lock_bh(&sess->conn_lock);
4467
4468 iscsit_dec_conn_usage_count(conn);
4469 if (is_last == 0)
4470 iscsit_dec_conn_usage_count(conn_tmp);
4471 conn_count--;
4472 }
4473 } else {
4474 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4475 iscsit_cause_connection_reinstatement(conn, 0);
4476 }
4477
4478 if (session_sleep && atomic_read(&sess->nconn)) {
4479 spin_unlock_bh(&sess->conn_lock);
4480 wait_for_completion(&sess->session_wait_comp);
4481 } else
4482 spin_unlock_bh(&sess->conn_lock);
4483}
4484
4485int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4486{
4487 struct iscsi_session *sess;
4488 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4489 struct se_session *se_sess, *se_sess_tmp;
4490 int session_count = 0;
4491
4492 spin_lock_bh(&se_tpg->session_lock);
4493 if (tpg->nsessions && !force) {
4494 spin_unlock_bh(&se_tpg->session_lock);
4495 return -1;
4496 }
4497
4498 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4499 sess_list) {
4500 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4501
4502 spin_lock(&sess->conn_lock);
4503 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4504 atomic_read(&sess->session_logout) ||
4505 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4506 spin_unlock(&sess->conn_lock);
4507 continue;
4508 }
4509 atomic_set(&sess->session_reinstatement, 1);
4510 spin_unlock(&sess->conn_lock);
4511 spin_unlock_bh(&se_tpg->session_lock);
4512
4513 iscsit_free_session(sess);
4514 spin_lock_bh(&se_tpg->session_lock);
4515
4516 session_count++;
4517 }
4518 spin_unlock_bh(&se_tpg->session_lock);
4519
4520 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4521 " Group: %hu\n", session_count, tpg->tpgt);
4522 return 0;
4523}
4524
4525MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4526MODULE_VERSION("4.1.x");
4527MODULE_AUTHOR("nab@Linux-iSCSI.org");
4528MODULE_LICENSE("GPL");
4529
4530module_init(iscsi_target_init_module);
4531module_exit(iscsi_target_cleanup_module);