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