blob: 1c843b51f2615e19776b80960bc6503f53bceafa [file] [log] [blame]
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001/*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
3 *
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5 *
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7 *
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 ******************************************************************************/
20
21#include <linux/string.h>
22#include <linux/kthread.h>
23#include <linux/crypto.h>
24#include <linux/completion.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040025#include <linux/module.h>
Al Viro40401532012-02-13 03:58:52 +000026#include <linux/idr.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000027#include <asm/unaligned.h>
28#include <scsi/scsi_device.h>
29#include <scsi/iscsi_proto.h>
Andy Groverd28b11692012-04-03 15:51:22 -070030#include <scsi/scsi_tcq.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000031#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050032#include <target/target_core_fabric.h>
Andy Groverd28b11692012-04-03 15:51:22 -070033#include <target/target_core_configfs.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000034
35#include "iscsi_target_core.h"
36#include "iscsi_target_parameters.h"
37#include "iscsi_target_seq_pdu_list.h"
38#include "iscsi_target_tq.h"
39#include "iscsi_target_configfs.h"
40#include "iscsi_target_datain_values.h"
41#include "iscsi_target_erl0.h"
42#include "iscsi_target_erl1.h"
43#include "iscsi_target_erl2.h"
44#include "iscsi_target_login.h"
45#include "iscsi_target_tmr.h"
46#include "iscsi_target_tpg.h"
47#include "iscsi_target_util.h"
48#include "iscsi_target.h"
49#include "iscsi_target_device.h"
50#include "iscsi_target_stat.h"
51
52static LIST_HEAD(g_tiqn_list);
53static LIST_HEAD(g_np_list);
54static DEFINE_SPINLOCK(tiqn_lock);
55static DEFINE_SPINLOCK(np_lock);
56
57static struct idr tiqn_idr;
58struct idr sess_idr;
59struct mutex auth_id_lock;
60spinlock_t sess_idr_lock;
61
62struct iscsit_global *iscsit_global;
63
64struct kmem_cache *lio_cmd_cache;
65struct kmem_cache *lio_qr_cache;
66struct kmem_cache *lio_dr_cache;
67struct kmem_cache *lio_ooo_cache;
68struct kmem_cache *lio_r2t_cache;
69
70static int iscsit_handle_immediate_data(struct iscsi_cmd *,
71 unsigned char *buf, u32);
72static int iscsit_logout_post_handler(struct iscsi_cmd *, struct iscsi_conn *);
73
74struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
75{
76 struct iscsi_tiqn *tiqn = NULL;
77
78 spin_lock(&tiqn_lock);
79 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
80 if (!strcmp(tiqn->tiqn, buf)) {
81
82 spin_lock(&tiqn->tiqn_state_lock);
83 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
84 tiqn->tiqn_access_count++;
85 spin_unlock(&tiqn->tiqn_state_lock);
86 spin_unlock(&tiqn_lock);
87 return tiqn;
88 }
89 spin_unlock(&tiqn->tiqn_state_lock);
90 }
91 }
92 spin_unlock(&tiqn_lock);
93
94 return NULL;
95}
96
97static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
98{
99 spin_lock(&tiqn->tiqn_state_lock);
100 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
101 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
102 spin_unlock(&tiqn->tiqn_state_lock);
103 return 0;
104 }
105 spin_unlock(&tiqn->tiqn_state_lock);
106
107 return -1;
108}
109
110void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
111{
112 spin_lock(&tiqn->tiqn_state_lock);
113 tiqn->tiqn_access_count--;
114 spin_unlock(&tiqn->tiqn_state_lock);
115}
116
117/*
118 * Note that IQN formatting is expected to be done in userspace, and
119 * no explict IQN format checks are done here.
120 */
121struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
122{
123 struct iscsi_tiqn *tiqn = NULL;
124 int ret;
125
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300126 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000127 pr_err("Target IQN exceeds %d bytes\n",
128 ISCSI_IQN_LEN);
129 return ERR_PTR(-EINVAL);
130 }
131
132 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
133 if (!tiqn) {
134 pr_err("Unable to allocate struct iscsi_tiqn\n");
135 return ERR_PTR(-ENOMEM);
136 }
137
138 sprintf(tiqn->tiqn, "%s", buf);
139 INIT_LIST_HEAD(&tiqn->tiqn_list);
140 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
141 spin_lock_init(&tiqn->tiqn_state_lock);
142 spin_lock_init(&tiqn->tiqn_tpg_lock);
143 spin_lock_init(&tiqn->sess_err_stats.lock);
144 spin_lock_init(&tiqn->login_stats.lock);
145 spin_lock_init(&tiqn->logout_stats.lock);
146
147 if (!idr_pre_get(&tiqn_idr, GFP_KERNEL)) {
148 pr_err("idr_pre_get() for tiqn_idr failed\n");
149 kfree(tiqn);
150 return ERR_PTR(-ENOMEM);
151 }
152 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
153
154 spin_lock(&tiqn_lock);
155 ret = idr_get_new(&tiqn_idr, NULL, &tiqn->tiqn_index);
156 if (ret < 0) {
157 pr_err("idr_get_new() failed for tiqn->tiqn_index\n");
158 spin_unlock(&tiqn_lock);
159 kfree(tiqn);
160 return ERR_PTR(ret);
161 }
162 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
163 spin_unlock(&tiqn_lock);
164
165 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
166
167 return tiqn;
168
169}
170
171static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
172{
173 /*
174 * Wait for accesses to said struct iscsi_tiqn to end.
175 */
176 spin_lock(&tiqn->tiqn_state_lock);
177 while (tiqn->tiqn_access_count != 0) {
178 spin_unlock(&tiqn->tiqn_state_lock);
179 msleep(10);
180 spin_lock(&tiqn->tiqn_state_lock);
181 }
182 spin_unlock(&tiqn->tiqn_state_lock);
183}
184
185void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
186{
187 /*
188 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
189 * while holding tiqn->tiqn_state_lock. This means that all subsequent
190 * attempts to access this struct iscsi_tiqn will fail from both transport
191 * fabric and control code paths.
192 */
193 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
194 pr_err("iscsit_set_tiqn_shutdown() failed\n");
195 return;
196 }
197
198 iscsit_wait_for_tiqn(tiqn);
199
200 spin_lock(&tiqn_lock);
201 list_del(&tiqn->tiqn_list);
202 idr_remove(&tiqn_idr, tiqn->tiqn_index);
203 spin_unlock(&tiqn_lock);
204
205 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
206 tiqn->tiqn);
207 kfree(tiqn);
208}
209
210int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
211{
212 int ret;
213 /*
214 * Determine if the network portal is accepting storage traffic.
215 */
216 spin_lock_bh(&np->np_thread_lock);
217 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
218 spin_unlock_bh(&np->np_thread_lock);
219 return -1;
220 }
221 if (np->np_login_tpg) {
222 pr_err("np->np_login_tpg() is not NULL!\n");
223 spin_unlock_bh(&np->np_thread_lock);
224 return -1;
225 }
226 spin_unlock_bh(&np->np_thread_lock);
227 /*
228 * Determine if the portal group is accepting storage traffic.
229 */
230 spin_lock_bh(&tpg->tpg_state_lock);
231 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
232 spin_unlock_bh(&tpg->tpg_state_lock);
233 return -1;
234 }
235 spin_unlock_bh(&tpg->tpg_state_lock);
236
237 /*
238 * Here we serialize access across the TIQN+TPG Tuple.
239 */
240 ret = mutex_lock_interruptible(&tpg->np_login_lock);
241 if ((ret != 0) || signal_pending(current))
242 return -1;
243
244 spin_lock_bh(&np->np_thread_lock);
245 np->np_login_tpg = tpg;
246 spin_unlock_bh(&np->np_thread_lock);
247
248 return 0;
249}
250
251int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
252{
253 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
254
255 spin_lock_bh(&np->np_thread_lock);
256 np->np_login_tpg = NULL;
257 spin_unlock_bh(&np->np_thread_lock);
258
259 mutex_unlock(&tpg->np_login_lock);
260
261 if (tiqn)
262 iscsit_put_tiqn_for_login(tiqn);
263
264 return 0;
265}
266
267static struct iscsi_np *iscsit_get_np(
268 struct __kernel_sockaddr_storage *sockaddr,
269 int network_transport)
270{
271 struct sockaddr_in *sock_in, *sock_in_e;
272 struct sockaddr_in6 *sock_in6, *sock_in6_e;
273 struct iscsi_np *np;
274 int ip_match = 0;
275 u16 port;
276
277 spin_lock_bh(&np_lock);
278 list_for_each_entry(np, &g_np_list, np_list) {
279 spin_lock(&np->np_thread_lock);
280 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
281 spin_unlock(&np->np_thread_lock);
282 continue;
283 }
284
285 if (sockaddr->ss_family == AF_INET6) {
286 sock_in6 = (struct sockaddr_in6 *)sockaddr;
287 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
288
Jörn Engel8359cf42011-11-24 02:05:51 +0100289 if (!memcmp(&sock_in6->sin6_addr.in6_u,
290 &sock_in6_e->sin6_addr.in6_u,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000291 sizeof(struct in6_addr)))
292 ip_match = 1;
293
294 port = ntohs(sock_in6->sin6_port);
295 } else {
296 sock_in = (struct sockaddr_in *)sockaddr;
297 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
298
299 if (sock_in->sin_addr.s_addr ==
300 sock_in_e->sin_addr.s_addr)
301 ip_match = 1;
302
303 port = ntohs(sock_in->sin_port);
304 }
305
306 if ((ip_match == 1) && (np->np_port == port) &&
307 (np->np_network_transport == network_transport)) {
308 /*
309 * Increment the np_exports reference count now to
310 * prevent iscsit_del_np() below from being called
311 * while iscsi_tpg_add_network_portal() is called.
312 */
313 np->np_exports++;
314 spin_unlock(&np->np_thread_lock);
315 spin_unlock_bh(&np_lock);
316 return np;
317 }
318 spin_unlock(&np->np_thread_lock);
319 }
320 spin_unlock_bh(&np_lock);
321
322 return NULL;
323}
324
325struct iscsi_np *iscsit_add_np(
326 struct __kernel_sockaddr_storage *sockaddr,
327 char *ip_str,
328 int network_transport)
329{
330 struct sockaddr_in *sock_in;
331 struct sockaddr_in6 *sock_in6;
332 struct iscsi_np *np;
333 int ret;
334 /*
335 * Locate the existing struct iscsi_np if already active..
336 */
337 np = iscsit_get_np(sockaddr, network_transport);
338 if (np)
339 return np;
340
341 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
342 if (!np) {
343 pr_err("Unable to allocate memory for struct iscsi_np\n");
344 return ERR_PTR(-ENOMEM);
345 }
346
347 np->np_flags |= NPF_IP_NETWORK;
348 if (sockaddr->ss_family == AF_INET6) {
349 sock_in6 = (struct sockaddr_in6 *)sockaddr;
350 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
351 np->np_port = ntohs(sock_in6->sin6_port);
352 } else {
353 sock_in = (struct sockaddr_in *)sockaddr;
354 sprintf(np->np_ip, "%s", ip_str);
355 np->np_port = ntohs(sock_in->sin_port);
356 }
357
358 np->np_network_transport = network_transport;
359 spin_lock_init(&np->np_thread_lock);
360 init_completion(&np->np_restart_comp);
361 INIT_LIST_HEAD(&np->np_list);
362
363 ret = iscsi_target_setup_login_socket(np, sockaddr);
364 if (ret != 0) {
365 kfree(np);
366 return ERR_PTR(ret);
367 }
368
369 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
370 if (IS_ERR(np->np_thread)) {
371 pr_err("Unable to create kthread: iscsi_np\n");
372 ret = PTR_ERR(np->np_thread);
373 kfree(np);
374 return ERR_PTR(ret);
375 }
376 /*
377 * Increment the np_exports reference count now to prevent
378 * iscsit_del_np() below from being run while a new call to
379 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
380 * active. We don't need to hold np->np_thread_lock at this
381 * point because iscsi_np has not been added to g_np_list yet.
382 */
383 np->np_exports = 1;
384
385 spin_lock_bh(&np_lock);
386 list_add_tail(&np->np_list, &g_np_list);
387 spin_unlock_bh(&np_lock);
388
389 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
390 np->np_ip, np->np_port, (np->np_network_transport == ISCSI_TCP) ?
391 "TCP" : "SCTP");
392
393 return np;
394}
395
396int iscsit_reset_np_thread(
397 struct iscsi_np *np,
398 struct iscsi_tpg_np *tpg_np,
399 struct iscsi_portal_group *tpg)
400{
401 spin_lock_bh(&np->np_thread_lock);
402 if (tpg && tpg_np) {
403 /*
404 * The reset operation need only be performed when the
405 * passed struct iscsi_portal_group has a login in progress
406 * to one of the network portals.
407 */
408 if (tpg_np->tpg_np->np_login_tpg != tpg) {
409 spin_unlock_bh(&np->np_thread_lock);
410 return 0;
411 }
412 }
413 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
414 spin_unlock_bh(&np->np_thread_lock);
415 return 0;
416 }
417 np->np_thread_state = ISCSI_NP_THREAD_RESET;
418
419 if (np->np_thread) {
420 spin_unlock_bh(&np->np_thread_lock);
421 send_sig(SIGINT, np->np_thread, 1);
422 wait_for_completion(&np->np_restart_comp);
423 spin_lock_bh(&np->np_thread_lock);
424 }
425 spin_unlock_bh(&np->np_thread_lock);
426
427 return 0;
428}
429
Christoph Hellwigfceb5bc2012-09-26 08:00:36 -0400430static int iscsit_del_np_comm(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000431{
Al Virobf6932f2012-07-21 08:55:18 +0100432 if (np->np_socket)
433 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000434 return 0;
435}
436
437int iscsit_del_np(struct iscsi_np *np)
438{
439 spin_lock_bh(&np->np_thread_lock);
440 np->np_exports--;
441 if (np->np_exports) {
442 spin_unlock_bh(&np->np_thread_lock);
443 return 0;
444 }
445 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
446 spin_unlock_bh(&np->np_thread_lock);
447
448 if (np->np_thread) {
449 /*
450 * We need to send the signal to wakeup Linux/Net
451 * which may be sleeping in sock_accept()..
452 */
453 send_sig(SIGINT, np->np_thread, 1);
454 kthread_stop(np->np_thread);
455 }
456 iscsit_del_np_comm(np);
457
458 spin_lock_bh(&np_lock);
459 list_del(&np->np_list);
460 spin_unlock_bh(&np_lock);
461
462 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
463 np->np_ip, np->np_port, (np->np_network_transport == ISCSI_TCP) ?
464 "TCP" : "SCTP");
465
466 kfree(np);
467 return 0;
468}
469
470static int __init iscsi_target_init_module(void)
471{
472 int ret = 0;
473
474 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
475
476 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
477 if (!iscsit_global) {
478 pr_err("Unable to allocate memory for iscsit_global\n");
479 return -1;
480 }
481 mutex_init(&auth_id_lock);
482 spin_lock_init(&sess_idr_lock);
483 idr_init(&tiqn_idr);
484 idr_init(&sess_idr);
485
486 ret = iscsi_target_register_configfs();
487 if (ret < 0)
488 goto out;
489
490 ret = iscsi_thread_set_init();
491 if (ret < 0)
492 goto configfs_out;
493
494 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
495 TARGET_THREAD_SET_COUNT) {
496 pr_err("iscsi_allocate_thread_sets() returned"
497 " unexpected value!\n");
498 goto ts_out1;
499 }
500
501 lio_cmd_cache = kmem_cache_create("lio_cmd_cache",
502 sizeof(struct iscsi_cmd), __alignof__(struct iscsi_cmd),
503 0, NULL);
504 if (!lio_cmd_cache) {
505 pr_err("Unable to kmem_cache_create() for"
506 " lio_cmd_cache\n");
507 goto ts_out2;
508 }
509
510 lio_qr_cache = kmem_cache_create("lio_qr_cache",
511 sizeof(struct iscsi_queue_req),
512 __alignof__(struct iscsi_queue_req), 0, NULL);
513 if (!lio_qr_cache) {
514 pr_err("nable to kmem_cache_create() for"
515 " lio_qr_cache\n");
516 goto cmd_out;
517 }
518
519 lio_dr_cache = kmem_cache_create("lio_dr_cache",
520 sizeof(struct iscsi_datain_req),
521 __alignof__(struct iscsi_datain_req), 0, NULL);
522 if (!lio_dr_cache) {
523 pr_err("Unable to kmem_cache_create() for"
524 " lio_dr_cache\n");
525 goto qr_out;
526 }
527
528 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
529 sizeof(struct iscsi_ooo_cmdsn),
530 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
531 if (!lio_ooo_cache) {
532 pr_err("Unable to kmem_cache_create() for"
533 " lio_ooo_cache\n");
534 goto dr_out;
535 }
536
537 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
538 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
539 0, NULL);
540 if (!lio_r2t_cache) {
541 pr_err("Unable to kmem_cache_create() for"
542 " lio_r2t_cache\n");
543 goto ooo_out;
544 }
545
546 if (iscsit_load_discovery_tpg() < 0)
547 goto r2t_out;
548
549 return ret;
550r2t_out:
551 kmem_cache_destroy(lio_r2t_cache);
552ooo_out:
553 kmem_cache_destroy(lio_ooo_cache);
554dr_out:
555 kmem_cache_destroy(lio_dr_cache);
556qr_out:
557 kmem_cache_destroy(lio_qr_cache);
558cmd_out:
559 kmem_cache_destroy(lio_cmd_cache);
560ts_out2:
561 iscsi_deallocate_thread_sets();
562ts_out1:
563 iscsi_thread_set_free();
564configfs_out:
565 iscsi_target_deregister_configfs();
566out:
567 kfree(iscsit_global);
568 return -ENOMEM;
569}
570
571static void __exit iscsi_target_cleanup_module(void)
572{
573 iscsi_deallocate_thread_sets();
574 iscsi_thread_set_free();
575 iscsit_release_discovery_tpg();
576 kmem_cache_destroy(lio_cmd_cache);
577 kmem_cache_destroy(lio_qr_cache);
578 kmem_cache_destroy(lio_dr_cache);
579 kmem_cache_destroy(lio_ooo_cache);
580 kmem_cache_destroy(lio_r2t_cache);
581
582 iscsi_target_deregister_configfs();
583
584 kfree(iscsit_global);
585}
586
Andy Grover8b1e1242012-04-03 15:51:12 -0700587static int iscsit_add_reject(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000588 u8 reason,
589 int fail_conn,
590 unsigned char *buf,
591 struct iscsi_conn *conn)
592{
593 struct iscsi_cmd *cmd;
594 struct iscsi_reject *hdr;
595 int ret;
596
597 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
598 if (!cmd)
599 return -1;
600
601 cmd->iscsi_opcode = ISCSI_OP_REJECT;
602 if (fail_conn)
603 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
604
605 hdr = (struct iscsi_reject *) cmd->pdu;
606 hdr->reason = reason;
607
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100608 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000609 if (!cmd->buf_ptr) {
610 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
611 iscsit_release_cmd(cmd);
612 return -1;
613 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000614
615 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700616 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000617 spin_unlock_bh(&conn->cmd_lock);
618
619 cmd->i_state = ISTATE_SEND_REJECT;
620 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
621
622 ret = wait_for_completion_interruptible(&cmd->reject_comp);
623 if (ret != 0)
624 return -1;
625
626 return (!fail_conn) ? 0 : -1;
627}
628
629int iscsit_add_reject_from_cmd(
630 u8 reason,
631 int fail_conn,
632 int add_to_conn,
633 unsigned char *buf,
634 struct iscsi_cmd *cmd)
635{
636 struct iscsi_conn *conn;
637 struct iscsi_reject *hdr;
638 int ret;
639
640 if (!cmd->conn) {
641 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
642 cmd->init_task_tag);
643 return -1;
644 }
645 conn = cmd->conn;
646
647 cmd->iscsi_opcode = ISCSI_OP_REJECT;
648 if (fail_conn)
649 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
650
651 hdr = (struct iscsi_reject *) cmd->pdu;
652 hdr->reason = reason;
653
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100654 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000655 if (!cmd->buf_ptr) {
656 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
657 iscsit_release_cmd(cmd);
658 return -1;
659 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000660
661 if (add_to_conn) {
662 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700663 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000664 spin_unlock_bh(&conn->cmd_lock);
665 }
666
667 cmd->i_state = ISTATE_SEND_REJECT;
668 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
669
670 ret = wait_for_completion_interruptible(&cmd->reject_comp);
671 if (ret != 0)
672 return -1;
673
674 return (!fail_conn) ? 0 : -1;
675}
676
677/*
678 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700679 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000680 */
681static int iscsit_map_iovec(
682 struct iscsi_cmd *cmd,
683 struct kvec *iov,
684 u32 data_offset,
685 u32 data_length)
686{
687 u32 i = 0;
688 struct scatterlist *sg;
689 unsigned int page_off;
690
691 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700692 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000693 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700694 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000695 page_off = (data_offset % PAGE_SIZE);
696
697 cmd->first_data_sg = sg;
698 cmd->first_data_sg_off = page_off;
699
700 while (data_length) {
701 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
702
703 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
704 iov[i].iov_len = cur_len;
705
706 data_length -= cur_len;
707 page_off = 0;
708 sg = sg_next(sg);
709 i++;
710 }
711
712 cmd->kmapped_nents = i;
713
714 return i;
715}
716
717static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
718{
719 u32 i;
720 struct scatterlist *sg;
721
722 sg = cmd->first_data_sg;
723
724 for (i = 0; i < cmd->kmapped_nents; i++)
725 kunmap(sg_page(&sg[i]));
726}
727
728static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
729{
730 struct iscsi_cmd *cmd;
731
732 conn->exp_statsn = exp_statsn;
733
734 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700735 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000736 spin_lock(&cmd->istate_lock);
737 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
738 (cmd->stat_sn < exp_statsn)) {
739 cmd->i_state = ISTATE_REMOVE;
740 spin_unlock(&cmd->istate_lock);
741 iscsit_add_cmd_to_immediate_queue(cmd, conn,
742 cmd->i_state);
743 continue;
744 }
745 spin_unlock(&cmd->istate_lock);
746 }
747 spin_unlock_bh(&conn->cmd_lock);
748}
749
750static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
751{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700752 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000753
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400754 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000755
756 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
757 if (!cmd->iov_data) {
758 pr_err("Unable to allocate cmd->iov_data\n");
759 return -ENOMEM;
760 }
761
762 cmd->orig_iov_data_count = iov_count;
763 return 0;
764}
765
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000766static int iscsit_handle_scsi_cmd(
767 struct iscsi_conn *conn,
768 unsigned char *buf)
769{
770 int data_direction, cmdsn_ret = 0, immed_ret, ret, transport_ret;
771 int dump_immediate_data = 0, send_check_condition = 0, payload_length;
772 struct iscsi_cmd *cmd = NULL;
773 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700774 int iscsi_task_attr;
775 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000776
777 spin_lock_bh(&conn->sess->session_stats_lock);
778 conn->sess->cmd_pdus++;
779 if (conn->sess->se_sess->se_node_acl) {
780 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
781 conn->sess->se_sess->se_node_acl->num_cmds++;
782 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
783 }
784 spin_unlock_bh(&conn->sess->session_stats_lock);
785
786 hdr = (struct iscsi_scsi_req *) buf;
787 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000788 hdr->data_length = be32_to_cpu(hdr->data_length);
789 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
790 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
791
792 /* FIXME; Add checks for AdditionalHeaderSegment */
793
794 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
795 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
796 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
797 " not set. Bad iSCSI Initiator.\n");
798 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
799 buf, conn);
800 }
801
802 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
803 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
804 /*
805 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
806 * that adds support for RESERVE/RELEASE. There is a bug
807 * add with this new functionality that sets R/W bits when
808 * neither CDB carries any READ or WRITE datapayloads.
809 */
810 if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) {
811 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
812 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
813 goto done;
814 }
815
816 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
817 " set when Expected Data Transfer Length is 0 for"
818 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]);
819 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
820 buf, conn);
821 }
822done:
823
824 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
825 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
826 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
827 " MUST be set if Expected Data Transfer Length is not 0."
828 " Bad iSCSI Initiator\n");
829 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
830 buf, conn);
831 }
832
833 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
834 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
835 pr_err("Bidirectional operations not supported!\n");
836 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
837 buf, conn);
838 }
839
840 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
841 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
842 " Scsi Command PDU.\n");
843 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
844 buf, conn);
845 }
846
847 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
848 pr_err("ImmediateData=No but DataSegmentLength=%u,"
849 " protocol error.\n", payload_length);
850 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
851 buf, conn);
852 }
853
854 if ((hdr->data_length == payload_length) &&
855 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
856 pr_err("Expected Data Transfer Length and Length of"
857 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
858 " bit is not set protocol error\n");
859 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
860 buf, conn);
861 }
862
863 if (payload_length > hdr->data_length) {
864 pr_err("DataSegmentLength: %u is greater than"
865 " EDTL: %u, protocol error.\n", payload_length,
866 hdr->data_length);
867 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
868 buf, conn);
869 }
870
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700871 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000872 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700873 " MaxXmitDataSegmentLength: %u, protocol error.\n",
874 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000875 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
876 buf, conn);
877 }
878
879 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
880 pr_err("DataSegmentLength: %u is greater than"
881 " FirstBurstLength: %u, protocol error.\n",
882 payload_length, conn->sess->sess_ops->FirstBurstLength);
883 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_INVALID, 1,
884 buf, conn);
885 }
886
887 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
888 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
889 DMA_NONE;
890
Andy Groverd28b11692012-04-03 15:51:22 -0700891 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000892 if (!cmd)
893 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
Andy Groverd28b11692012-04-03 15:51:22 -0700894 buf, conn);
895
896 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700897 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
898 /*
899 * Figure out the SAM Task Attribute for the incoming SCSI CDB
900 */
901 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
902 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
903 sam_task_attr = MSG_SIMPLE_TAG;
904 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
905 sam_task_attr = MSG_ORDERED_TAG;
906 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
907 sam_task_attr = MSG_HEAD_TAG;
908 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
909 sam_task_attr = MSG_ACA_TAG;
910 else {
911 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
912 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
913 sam_task_attr = MSG_SIMPLE_TAG;
914 }
915
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000916 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
917 cmd->i_state = ISTATE_NEW_CMD;
918 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
919 cmd->immediate_data = (payload_length) ? 1 : 0;
920 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
921 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
922 if (cmd->unsolicited_data)
923 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
924
925 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
926 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
927 spin_lock_bh(&conn->sess->ttt_lock);
928 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
929 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
930 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
931 spin_unlock_bh(&conn->sess->ttt_lock);
932 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
933 cmd->targ_xfer_tag = 0xFFFFFFFF;
934 cmd->cmd_sn = hdr->cmdsn;
935 cmd->exp_stat_sn = hdr->exp_statsn;
936 cmd->first_burst_len = payload_length;
937
938 if (cmd->data_direction == DMA_FROM_DEVICE) {
939 struct iscsi_datain_req *dr;
940
941 dr = iscsit_allocate_datain_req();
942 if (!dr)
943 return iscsit_add_reject_from_cmd(
944 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
945 1, 1, buf, cmd);
946
947 iscsit_attach_datain_req(cmd, dr);
948 }
949
950 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700951 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
952 */
953 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Andy Groverebf1d952012-04-03 15:51:24 -0700954 conn->sess->se_sess, hdr->data_length, cmd->data_direction,
Roland Dreier9c58b7d2012-08-15 14:35:25 -0700955 sam_task_attr, cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700956
957 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
958 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
959 hdr->cmdsn, hdr->data_length, payload_length, conn->cid);
960
961 /*
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000962 * The CDB is going to an se_device_t.
963 */
Andy Grover4f269982012-01-19 13:39:14 -0800964 ret = transport_lookup_cmd_lun(&cmd->se_cmd,
965 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000966 if (ret < 0) {
967 if (cmd->se_cmd.scsi_sense_reason == TCM_NON_EXISTENT_LUN) {
968 pr_debug("Responding to non-acl'ed,"
969 " non-existent or non-exported iSCSI LUN:"
970 " 0x%016Lx\n", get_unaligned_le64(&hdr->lun));
971 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000972 send_check_condition = 1;
973 goto attach_cmd;
974 }
Andy Grovera12f41f2012-04-03 15:51:20 -0700975
976 transport_ret = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000977 if (transport_ret == -ENOMEM) {
978 return iscsit_add_reject_from_cmd(
979 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
980 1, 1, buf, cmd);
Nicholas Bellinger00fdc6b2012-03-13 18:20:11 -0700981 } else if (transport_ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000982 /*
983 * Unsupported SAM Opcode. CHECK_CONDITION will be sent
984 * in iscsit_execute_cmd() during the CmdSN OOO Execution
985 * Mechinism.
986 */
987 send_check_condition = 1;
988 } else {
Andy Grover4334e492012-04-03 15:51:25 -0700989 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000990 return iscsit_add_reject_from_cmd(
991 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
992 1, 1, buf, cmd);
993 }
994
995attach_cmd:
996 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700997 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000998 spin_unlock_bh(&conn->cmd_lock);
999 /*
1000 * Check if we need to delay processing because of ALUA
1001 * Active/NonOptimized primary access state..
1002 */
1003 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001004
1005 ret = iscsit_allocate_iovecs(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001006 if (ret < 0)
1007 return iscsit_add_reject_from_cmd(
1008 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
Nicholas Bellingercd931ee2012-01-16 17:11:54 -08001009 1, 0, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001010 /*
1011 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1012 * the Immediate Bit is not set, and no Immediate
1013 * Data is attached.
1014 *
1015 * A PDU/CmdSN carrying Immediate Data can only
1016 * be processed after the DataCRC has passed.
1017 * If the DataCRC fails, the CmdSN MUST NOT
1018 * be acknowledged. (See below)
1019 */
1020 if (!cmd->immediate_data) {
1021 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001022 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1023 return 0;
1024 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001025 return iscsit_add_reject_from_cmd(
1026 ISCSI_REASON_PROTOCOL_ERROR,
1027 1, 0, buf, cmd);
1028 }
1029
1030 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
1031
1032 /*
1033 * If no Immediate Data is attached, it's OK to return now.
1034 */
1035 if (!cmd->immediate_data) {
1036 if (send_check_condition)
1037 return 0;
1038
1039 if (cmd->unsolicited_data) {
1040 iscsit_set_dataout_sequence_values(cmd);
1041
1042 spin_lock_bh(&cmd->dataout_timeout_lock);
1043 iscsit_start_dataout_timer(cmd, cmd->conn);
1044 spin_unlock_bh(&cmd->dataout_timeout_lock);
1045 }
1046
1047 return 0;
1048 }
1049
1050 /*
1051 * Early CHECK_CONDITIONs never make it to the transport processing
1052 * thread. They are processed in CmdSN order by
1053 * iscsit_check_received_cmdsn() below.
1054 */
1055 if (send_check_condition) {
1056 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1057 dump_immediate_data = 1;
1058 goto after_immediate_data;
1059 }
1060 /*
1061 * Call directly into transport_generic_new_cmd() to perform
1062 * the backend memory allocation.
1063 */
1064 ret = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001065 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001066 immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1067 dump_immediate_data = 1;
1068 goto after_immediate_data;
1069 }
1070
1071 immed_ret = iscsit_handle_immediate_data(cmd, buf, payload_length);
1072after_immediate_data:
1073 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1074 /*
1075 * A PDU/CmdSN carrying Immediate Data passed
1076 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1077 * Immediate Bit is not set.
1078 */
1079 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1080 /*
1081 * Special case for Unsupported SAM WRITE Opcodes
1082 * and ImmediateData=Yes.
1083 */
1084 if (dump_immediate_data) {
1085 if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
1086 return -1;
1087 } else if (cmd->unsolicited_data) {
1088 iscsit_set_dataout_sequence_values(cmd);
1089
1090 spin_lock_bh(&cmd->dataout_timeout_lock);
1091 iscsit_start_dataout_timer(cmd, cmd->conn);
1092 spin_unlock_bh(&cmd->dataout_timeout_lock);
1093 }
1094
1095 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1096 return iscsit_add_reject_from_cmd(
1097 ISCSI_REASON_PROTOCOL_ERROR,
1098 1, 0, buf, cmd);
1099
1100 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1101 /*
1102 * Immediate Data failed DataCRC and ERL>=1,
1103 * silently drop this PDU and let the initiator
1104 * plug the CmdSN gap.
1105 *
1106 * FIXME: Send Unsolicited NOPIN with reserved
1107 * TTT here to help the initiator figure out
1108 * the missing CmdSN, although they should be
1109 * intelligent enough to determine the missing
1110 * CmdSN and issue a retry to plug the sequence.
1111 */
1112 cmd->i_state = ISTATE_REMOVE;
1113 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1114 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1115 return -1;
1116
1117 return 0;
1118}
1119
1120static u32 iscsit_do_crypto_hash_sg(
1121 struct hash_desc *hash,
1122 struct iscsi_cmd *cmd,
1123 u32 data_offset,
1124 u32 data_length,
1125 u32 padding,
1126 u8 *pad_bytes)
1127{
1128 u32 data_crc;
1129 u32 i;
1130 struct scatterlist *sg;
1131 unsigned int page_off;
1132
1133 crypto_hash_init(hash);
1134
1135 sg = cmd->first_data_sg;
1136 page_off = cmd->first_data_sg_off;
1137
1138 i = 0;
1139 while (data_length) {
1140 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1141
1142 crypto_hash_update(hash, &sg[i], cur_len);
1143
1144 data_length -= cur_len;
1145 page_off = 0;
1146 i++;
1147 }
1148
1149 if (padding) {
1150 struct scatterlist pad_sg;
1151
1152 sg_init_one(&pad_sg, pad_bytes, padding);
1153 crypto_hash_update(hash, &pad_sg, padding);
1154 }
1155 crypto_hash_final(hash, (u8 *) &data_crc);
1156
1157 return data_crc;
1158}
1159
1160static void iscsit_do_crypto_hash_buf(
1161 struct hash_desc *hash,
1162 unsigned char *buf,
1163 u32 payload_length,
1164 u32 padding,
1165 u8 *pad_bytes,
1166 u8 *data_crc)
1167{
1168 struct scatterlist sg;
1169
1170 crypto_hash_init(hash);
1171
Jörn Engel8359cf42011-11-24 02:05:51 +01001172 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001173 crypto_hash_update(hash, &sg, payload_length);
1174
1175 if (padding) {
1176 sg_init_one(&sg, pad_bytes, padding);
1177 crypto_hash_update(hash, &sg, padding);
1178 }
1179 crypto_hash_final(hash, data_crc);
1180}
1181
1182static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1183{
1184 int iov_ret, ooo_cmdsn = 0, ret;
1185 u8 data_crc_failed = 0;
1186 u32 checksum, iov_count = 0, padding = 0, rx_got = 0;
1187 u32 rx_size = 0, payload_length;
1188 struct iscsi_cmd *cmd = NULL;
1189 struct se_cmd *se_cmd;
1190 struct iscsi_data *hdr;
1191 struct kvec *iov;
1192 unsigned long flags;
1193
1194 hdr = (struct iscsi_data *) buf;
1195 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001196 hdr->ttt = be32_to_cpu(hdr->ttt);
1197 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1198 hdr->datasn = be32_to_cpu(hdr->datasn);
1199 hdr->offset = be32_to_cpu(hdr->offset);
1200
1201 if (!payload_length) {
1202 pr_err("DataOUT payload is ZERO, protocol error.\n");
1203 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1204 buf, conn);
1205 }
1206
1207 /* iSCSI write */
1208 spin_lock_bh(&conn->sess->session_stats_lock);
1209 conn->sess->rx_data_octets += payload_length;
1210 if (conn->sess->se_sess->se_node_acl) {
1211 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1212 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1213 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1214 }
1215 spin_unlock_bh(&conn->sess->session_stats_lock);
1216
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001217 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001218 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001219 " MaxXmitDataSegmentLength: %u\n", payload_length,
1220 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001221 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1222 buf, conn);
1223 }
1224
1225 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1226 payload_length);
1227 if (!cmd)
1228 return 0;
1229
1230 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1231 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1232 hdr->itt, hdr->ttt, hdr->datasn, hdr->offset,
1233 payload_length, conn->cid);
1234
1235 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1236 pr_err("Command ITT: 0x%08x received DataOUT after"
1237 " last DataOUT received, dumping payload\n",
1238 cmd->init_task_tag);
1239 return iscsit_dump_data_payload(conn, payload_length, 1);
1240 }
1241
1242 if (cmd->data_direction != DMA_TO_DEVICE) {
1243 pr_err("Command ITT: 0x%08x received DataOUT for a"
1244 " NON-WRITE command.\n", cmd->init_task_tag);
1245 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1246 1, 0, buf, cmd);
1247 }
1248 se_cmd = &cmd->se_cmd;
1249 iscsit_mod_dataout_timer(cmd);
1250
Andy Groverebf1d952012-04-03 15:51:24 -07001251 if ((hdr->offset + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001252 pr_err("DataOut Offset: %u, Length %u greater than"
1253 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001254 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001255 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
1256 1, 0, buf, cmd);
1257 }
1258
1259 if (cmd->unsolicited_data) {
1260 int dump_unsolicited_data = 0;
1261
1262 if (conn->sess->sess_ops->InitialR2T) {
1263 pr_err("Received unexpected unsolicited data"
1264 " while InitialR2T=Yes, protocol error.\n");
1265 transport_send_check_condition_and_sense(&cmd->se_cmd,
1266 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1267 return -1;
1268 }
1269 /*
1270 * Special case for dealing with Unsolicited DataOUT
1271 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1272 * failures;
1273 */
1274
1275 /* Something's amiss if we're not in WRITE_PENDING state... */
1276 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1277 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1278 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1279
1280 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1281 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) ||
Nicholas Bellinger03e98c92011-11-04 02:36:16 -07001282 (se_cmd->se_cmd_flags & SCF_SCSI_CDB_EXCEPTION))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001283 dump_unsolicited_data = 1;
1284 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1285
1286 if (dump_unsolicited_data) {
1287 /*
1288 * Check if a delayed TASK_ABORTED status needs to
1289 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1290 * received with the unsolicitied data out.
1291 */
1292 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1293 iscsit_stop_dataout_timer(cmd);
1294
1295 transport_check_aborted_status(se_cmd,
1296 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1297 return iscsit_dump_data_payload(conn, payload_length, 1);
1298 }
1299 } else {
1300 /*
1301 * For the normal solicited data path:
1302 *
1303 * Check for a delayed TASK_ABORTED status and dump any
1304 * incoming data out payload if one exists. Also, when the
1305 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1306 * data out sequence, we decrement outstanding_r2ts. Once
1307 * outstanding_r2ts reaches zero, go ahead and send the delayed
1308 * TASK_ABORTED status.
1309 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001310 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001311 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1312 if (--cmd->outstanding_r2ts < 1) {
1313 iscsit_stop_dataout_timer(cmd);
1314 transport_check_aborted_status(
1315 se_cmd, 1);
1316 }
1317
1318 return iscsit_dump_data_payload(conn, payload_length, 1);
1319 }
1320 }
1321 /*
1322 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1323 * within-command recovery checks before receiving the payload.
1324 */
1325 ret = iscsit_check_pre_dataout(cmd, buf);
1326 if (ret == DATAOUT_WITHIN_COMMAND_RECOVERY)
1327 return 0;
1328 else if (ret == DATAOUT_CANNOT_RECOVER)
1329 return -1;
1330
1331 rx_size += payload_length;
1332 iov = &cmd->iov_data[0];
1333
1334 iov_ret = iscsit_map_iovec(cmd, iov, hdr->offset, payload_length);
1335 if (iov_ret < 0)
1336 return -1;
1337
1338 iov_count += iov_ret;
1339
1340 padding = ((-payload_length) & 3);
1341 if (padding != 0) {
1342 iov[iov_count].iov_base = cmd->pad_bytes;
1343 iov[iov_count++].iov_len = padding;
1344 rx_size += padding;
1345 pr_debug("Receiving %u padding bytes.\n", padding);
1346 }
1347
1348 if (conn->conn_ops->DataDigest) {
1349 iov[iov_count].iov_base = &checksum;
1350 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1351 rx_size += ISCSI_CRC_LEN;
1352 }
1353
1354 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1355
1356 iscsit_unmap_iovec(cmd);
1357
1358 if (rx_got != rx_size)
1359 return -1;
1360
1361 if (conn->conn_ops->DataDigest) {
1362 u32 data_crc;
1363
1364 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
1365 hdr->offset, payload_length, padding,
1366 cmd->pad_bytes);
1367
1368 if (checksum != data_crc) {
1369 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1370 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1371 " does not match computed 0x%08x\n",
1372 hdr->itt, hdr->offset, payload_length,
1373 hdr->datasn, checksum, data_crc);
1374 data_crc_failed = 1;
1375 } else {
1376 pr_debug("Got CRC32C DataDigest 0x%08x for"
1377 " %u bytes of Data Out\n", checksum,
1378 payload_length);
1379 }
1380 }
1381 /*
1382 * Increment post receive data and CRC values or perform
1383 * within-command recovery.
1384 */
1385 ret = iscsit_check_post_dataout(cmd, buf, data_crc_failed);
1386 if ((ret == DATAOUT_NORMAL) || (ret == DATAOUT_WITHIN_COMMAND_RECOVERY))
1387 return 0;
1388 else if (ret == DATAOUT_SEND_R2T) {
1389 iscsit_set_dataout_sequence_values(cmd);
Andy Grover8b1e1242012-04-03 15:51:12 -07001390 iscsit_build_r2ts_for_cmd(cmd, conn, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001391 } else if (ret == DATAOUT_SEND_TO_TRANSPORT) {
1392 /*
1393 * Handle extra special case for out of order
1394 * Unsolicited Data Out.
1395 */
1396 spin_lock_bh(&cmd->istate_lock);
1397 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1398 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1399 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1400 spin_unlock_bh(&cmd->istate_lock);
1401
1402 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001403 if (ooo_cmdsn)
1404 return 0;
1405 target_execute_cmd(&cmd->se_cmd);
1406 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001407 } else /* DATAOUT_CANNOT_RECOVER */
1408 return -1;
1409
1410 return 0;
1411}
1412
1413static int iscsit_handle_nop_out(
1414 struct iscsi_conn *conn,
1415 unsigned char *buf)
1416{
1417 unsigned char *ping_data = NULL;
1418 int cmdsn_ret, niov = 0, ret = 0, rx_got, rx_size;
1419 u32 checksum, data_crc, padding = 0, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001420 struct iscsi_cmd *cmd = NULL;
1421 struct kvec *iov = NULL;
1422 struct iscsi_nopout *hdr;
1423
1424 hdr = (struct iscsi_nopout *) buf;
1425 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001426 hdr->ttt = be32_to_cpu(hdr->ttt);
1427 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
1428 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1429
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001430 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001431 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1432 " not set, protocol error.\n");
1433 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1434 buf, conn);
1435 }
1436
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001437 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001438 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001439 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001440 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001441 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001442 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1443 buf, conn);
1444 }
1445
1446 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%09x,"
1447 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001448 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001449 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1450 payload_length);
1451 /*
1452 * This is not a response to a Unsolicited NopIN, which means
1453 * it can either be a NOPOUT ping request (with a valid ITT),
1454 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1455 * Either way, make sure we allocate an struct iscsi_cmd, as both
1456 * can contain ping data.
1457 */
1458 if (hdr->ttt == 0xFFFFFFFF) {
1459 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1460 if (!cmd)
1461 return iscsit_add_reject(
1462 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1463 1, buf, conn);
1464
1465 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1466 cmd->i_state = ISTATE_SEND_NOPIN;
1467 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1468 1 : 0);
1469 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1470 cmd->targ_xfer_tag = 0xFFFFFFFF;
1471 cmd->cmd_sn = hdr->cmdsn;
1472 cmd->exp_stat_sn = hdr->exp_statsn;
1473 cmd->data_direction = DMA_NONE;
1474 }
1475
1476 if (payload_length && (hdr->ttt == 0xFFFFFFFF)) {
1477 rx_size = payload_length;
1478 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1479 if (!ping_data) {
1480 pr_err("Unable to allocate memory for"
1481 " NOPOUT ping data.\n");
1482 ret = -1;
1483 goto out;
1484 }
1485
1486 iov = &cmd->iov_misc[0];
1487 iov[niov].iov_base = ping_data;
1488 iov[niov++].iov_len = payload_length;
1489
1490 padding = ((-payload_length) & 3);
1491 if (padding != 0) {
1492 pr_debug("Receiving %u additional bytes"
1493 " for padding.\n", padding);
1494 iov[niov].iov_base = &cmd->pad_bytes;
1495 iov[niov++].iov_len = padding;
1496 rx_size += padding;
1497 }
1498 if (conn->conn_ops->DataDigest) {
1499 iov[niov].iov_base = &checksum;
1500 iov[niov++].iov_len = ISCSI_CRC_LEN;
1501 rx_size += ISCSI_CRC_LEN;
1502 }
1503
1504 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1505 if (rx_got != rx_size) {
1506 ret = -1;
1507 goto out;
1508 }
1509
1510 if (conn->conn_ops->DataDigest) {
1511 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1512 ping_data, payload_length,
1513 padding, cmd->pad_bytes,
1514 (u8 *)&data_crc);
1515
1516 if (checksum != data_crc) {
1517 pr_err("Ping data CRC32C DataDigest"
1518 " 0x%08x does not match computed 0x%08x\n",
1519 checksum, data_crc);
1520 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1521 pr_err("Unable to recover from"
1522 " NOPOUT Ping DataCRC failure while in"
1523 " ERL=0.\n");
1524 ret = -1;
1525 goto out;
1526 } else {
1527 /*
1528 * Silently drop this PDU and let the
1529 * initiator plug the CmdSN gap.
1530 */
1531 pr_debug("Dropping NOPOUT"
1532 " Command CmdSN: 0x%08x due to"
1533 " DataCRC error.\n", hdr->cmdsn);
1534 ret = 0;
1535 goto out;
1536 }
1537 } else {
1538 pr_debug("Got CRC32C DataDigest"
1539 " 0x%08x for %u bytes of ping data.\n",
1540 checksum, payload_length);
1541 }
1542 }
1543
1544 ping_data[payload_length] = '\0';
1545 /*
1546 * Attach ping data to struct iscsi_cmd->buf_ptr.
1547 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001548 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001549 cmd->buf_ptr_size = payload_length;
1550
1551 pr_debug("Got %u bytes of NOPOUT ping"
1552 " data.\n", payload_length);
1553 pr_debug("Ping Data: \"%s\"\n", ping_data);
1554 }
1555
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001556 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001557 if (!cmd) {
1558 pr_err("Checking CmdSN for NOPOUT,"
1559 " but cmd is NULL!\n");
1560 return -1;
1561 }
1562 /*
1563 * Initiator is expecting a NopIN ping reply,
1564 */
1565 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001566 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001567 spin_unlock_bh(&conn->cmd_lock);
1568
1569 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
1570
1571 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1572 iscsit_add_cmd_to_response_queue(cmd, conn,
1573 cmd->i_state);
1574 return 0;
1575 }
1576
1577 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1578 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1579 ret = 0;
1580 goto ping_out;
1581 }
1582 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1583 return iscsit_add_reject_from_cmd(
1584 ISCSI_REASON_PROTOCOL_ERROR,
1585 1, 0, buf, cmd);
1586
1587 return 0;
1588 }
1589
1590 if (hdr->ttt != 0xFFFFFFFF) {
1591 /*
1592 * This was a response to a unsolicited NOPIN ping.
1593 */
1594 cmd = iscsit_find_cmd_from_ttt(conn, hdr->ttt);
1595 if (!cmd)
1596 return -1;
1597
1598 iscsit_stop_nopin_response_timer(conn);
1599
1600 cmd->i_state = ISTATE_REMOVE;
1601 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
1602 iscsit_start_nopin_timer(conn);
1603 } else {
1604 /*
1605 * Initiator is not expecting a NOPIN is response.
1606 * Just ignore for now.
1607 *
1608 * iSCSI v19-91 10.18
1609 * "A NOP-OUT may also be used to confirm a changed
1610 * ExpStatSN if another PDU will not be available
1611 * for a long time."
1612 */
1613 ret = 0;
1614 goto out;
1615 }
1616
1617 return 0;
1618out:
1619 if (cmd)
1620 iscsit_release_cmd(cmd);
1621ping_out:
1622 kfree(ping_data);
1623 return ret;
1624}
1625
1626static int iscsit_handle_task_mgt_cmd(
1627 struct iscsi_conn *conn,
1628 unsigned char *buf)
1629{
1630 struct iscsi_cmd *cmd;
1631 struct se_tmr_req *se_tmr;
1632 struct iscsi_tmr_req *tmr_req;
1633 struct iscsi_tm *hdr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001634 int out_of_order_cmdsn = 0;
1635 int ret;
1636 u8 function;
1637
1638 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001639 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
1640 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1641 hdr->refcmdsn = be32_to_cpu(hdr->refcmdsn);
1642 hdr->exp_datasn = be32_to_cpu(hdr->exp_datasn);
1643 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1644 function = hdr->flags;
1645
1646 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1647 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1648 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1649 hdr->rtt, hdr->refcmdsn, conn->cid);
1650
1651 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1652 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001653 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001654 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001655 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001656 }
1657
1658 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1659 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1660 pr_err("Task Management Request TASK_REASSIGN not"
1661 " issued as immediate command, bad iSCSI Initiator"
1662 "implementation\n");
1663 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1664 buf, conn);
1665 }
1666 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1667 (hdr->refcmdsn != ISCSI_RESERVED_TAG))
1668 hdr->refcmdsn = ISCSI_RESERVED_TAG;
1669
Andy Groverd28b11692012-04-03 15:51:22 -07001670 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001671 if (!cmd)
1672 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
Andy Groverd28b11692012-04-03 15:51:22 -07001673 1, buf, conn);
1674
1675 cmd->data_direction = DMA_NONE;
1676
1677 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1678 if (!cmd->tmr_req) {
1679 pr_err("Unable to allocate memory for"
1680 " Task Management command!\n");
1681 return iscsit_add_reject_from_cmd(
1682 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1683 1, 1, buf, cmd);
1684 }
1685
1686 /*
1687 * TASK_REASSIGN for ERL=2 / connection stays inside of
1688 * LIO-Target $FABRIC_MOD
1689 */
1690 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1691
1692 u8 tcm_function;
1693 int ret;
1694
1695 transport_init_se_cmd(&cmd->se_cmd,
1696 &lio_target_fabric_configfs->tf_ops,
1697 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001698 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001699
1700 switch (function) {
1701 case ISCSI_TM_FUNC_ABORT_TASK:
1702 tcm_function = TMR_ABORT_TASK;
1703 break;
1704 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1705 tcm_function = TMR_ABORT_TASK_SET;
1706 break;
1707 case ISCSI_TM_FUNC_CLEAR_ACA:
1708 tcm_function = TMR_CLEAR_ACA;
1709 break;
1710 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1711 tcm_function = TMR_CLEAR_TASK_SET;
1712 break;
1713 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1714 tcm_function = TMR_LUN_RESET;
1715 break;
1716 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1717 tcm_function = TMR_TARGET_WARM_RESET;
1718 break;
1719 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1720 tcm_function = TMR_TARGET_COLD_RESET;
1721 break;
1722 default:
1723 pr_err("Unknown iSCSI TMR Function:"
1724 " 0x%02x\n", function);
1725 return iscsit_add_reject_from_cmd(
1726 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1727 1, 1, buf, cmd);
1728 }
1729
1730 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1731 tcm_function, GFP_KERNEL);
1732 if (ret < 0)
1733 return iscsit_add_reject_from_cmd(
1734 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1735 1, 1, buf, cmd);
1736
1737 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1738 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001739
1740 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1741 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1742 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1743 cmd->init_task_tag = hdr->itt;
1744 cmd->targ_xfer_tag = 0xFFFFFFFF;
1745 cmd->cmd_sn = hdr->cmdsn;
1746 cmd->exp_stat_sn = hdr->exp_statsn;
1747 se_tmr = cmd->se_cmd.se_tmr_req;
1748 tmr_req = cmd->tmr_req;
1749 /*
1750 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1751 */
1752 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001753 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1754 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 if (ret < 0) {
1756 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1757 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1758 goto attach;
1759 }
1760 }
1761
1762 switch (function) {
1763 case ISCSI_TM_FUNC_ABORT_TASK:
1764 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
1765 if (se_tmr->response != ISCSI_TMF_RSP_COMPLETE) {
1766 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1767 goto attach;
1768 }
1769 break;
1770 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1771 case ISCSI_TM_FUNC_CLEAR_ACA:
1772 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1773 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1774 break;
1775 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1776 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
1777 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1778 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1779 goto attach;
1780 }
1781 break;
1782 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1783 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
1784 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1785 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1786 goto attach;
1787 }
1788 break;
1789 case ISCSI_TM_FUNC_TASK_REASSIGN:
1790 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1791 /*
1792 * Perform sanity checks on the ExpDataSN only if the
1793 * TASK_REASSIGN was successful.
1794 */
1795 if (se_tmr->response != ISCSI_TMF_RSP_COMPLETE)
1796 break;
1797
1798 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
1799 return iscsit_add_reject_from_cmd(
1800 ISCSI_REASON_BOOKMARK_INVALID, 1, 1,
1801 buf, cmd);
1802 break;
1803 default:
1804 pr_err("Unknown TMR function: 0x%02x, protocol"
1805 " error.\n", function);
1806 cmd->se_cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1807 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1808 goto attach;
1809 }
1810
1811 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1812 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1813 se_tmr->call_transport = 1;
1814attach:
1815 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001816 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001817 spin_unlock_bh(&conn->cmd_lock);
1818
1819 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1820 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1821 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1822 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001823 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001824 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001825 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001826 return iscsit_add_reject_from_cmd(
1827 ISCSI_REASON_PROTOCOL_ERROR,
1828 1, 0, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001829 }
1830 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
1831
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001832 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001833 return 0;
1834 /*
1835 * Found the referenced task, send to transport for processing.
1836 */
1837 if (se_tmr->call_transport)
1838 return transport_generic_handle_tmr(&cmd->se_cmd);
1839
1840 /*
1841 * Could not find the referenced LUN, task, or Task Management
1842 * command not authorized or supported. Change state and
1843 * let the tx_thread send the response.
1844 *
1845 * For connection recovery, this is also the default action for
1846 * TMR TASK_REASSIGN.
1847 */
1848 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1849 return 0;
1850}
1851
1852/* #warning FIXME: Support Text Command parameters besides SendTargets */
1853static int iscsit_handle_text_cmd(
1854 struct iscsi_conn *conn,
1855 unsigned char *buf)
1856{
1857 char *text_ptr, *text_in;
1858 int cmdsn_ret, niov = 0, rx_got, rx_size;
1859 u32 checksum = 0, data_crc = 0, payload_length;
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001860 u32 padding = 0, pad_bytes = 0, text_length = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001861 struct iscsi_cmd *cmd;
1862 struct kvec iov[3];
1863 struct iscsi_text *hdr;
1864
1865 hdr = (struct iscsi_text *) buf;
1866 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001867 hdr->ttt = be32_to_cpu(hdr->ttt);
1868 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
1869 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
1870
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001871 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001872 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001873 "greater than MaxXmitDataSegmentLength %u.\n",
1874 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001875 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1876 buf, conn);
1877 }
1878
1879 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1880 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1881 hdr->exp_statsn, payload_length);
1882
1883 rx_size = text_length = payload_length;
1884 if (text_length) {
1885 text_in = kzalloc(text_length, GFP_KERNEL);
1886 if (!text_in) {
1887 pr_err("Unable to allocate memory for"
1888 " incoming text parameters\n");
1889 return -1;
1890 }
1891
1892 memset(iov, 0, 3 * sizeof(struct kvec));
1893 iov[niov].iov_base = text_in;
1894 iov[niov++].iov_len = text_length;
1895
1896 padding = ((-payload_length) & 3);
1897 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001898 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001899 iov[niov++].iov_len = padding;
1900 rx_size += padding;
1901 pr_debug("Receiving %u additional bytes"
1902 " for padding.\n", padding);
1903 }
1904 if (conn->conn_ops->DataDigest) {
1905 iov[niov].iov_base = &checksum;
1906 iov[niov++].iov_len = ISCSI_CRC_LEN;
1907 rx_size += ISCSI_CRC_LEN;
1908 }
1909
1910 rx_got = rx_data(conn, &iov[0], niov, rx_size);
1911 if (rx_got != rx_size) {
1912 kfree(text_in);
1913 return -1;
1914 }
1915
1916 if (conn->conn_ops->DataDigest) {
1917 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1918 text_in, text_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07001919 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001920 (u8 *)&data_crc);
1921
1922 if (checksum != data_crc) {
1923 pr_err("Text data CRC32C DataDigest"
1924 " 0x%08x does not match computed"
1925 " 0x%08x\n", checksum, data_crc);
1926 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1927 pr_err("Unable to recover from"
1928 " Text Data digest failure while in"
1929 " ERL=0.\n");
1930 kfree(text_in);
1931 return -1;
1932 } else {
1933 /*
1934 * Silently drop this PDU and let the
1935 * initiator plug the CmdSN gap.
1936 */
1937 pr_debug("Dropping Text"
1938 " Command CmdSN: 0x%08x due to"
1939 " DataCRC error.\n", hdr->cmdsn);
1940 kfree(text_in);
1941 return 0;
1942 }
1943 } else {
1944 pr_debug("Got CRC32C DataDigest"
1945 " 0x%08x for %u bytes of text data.\n",
1946 checksum, text_length);
1947 }
1948 }
1949 text_in[text_length - 1] = '\0';
1950 pr_debug("Successfully read %d bytes of text"
1951 " data.\n", text_length);
1952
1953 if (strncmp("SendTargets", text_in, 11) != 0) {
1954 pr_err("Received Text Data that is not"
1955 " SendTargets, cannot continue.\n");
1956 kfree(text_in);
1957 return -1;
1958 }
1959 text_ptr = strchr(text_in, '=');
1960 if (!text_ptr) {
1961 pr_err("No \"=\" separator found in Text Data,"
1962 " cannot continue.\n");
1963 kfree(text_in);
1964 return -1;
1965 }
1966 if (strncmp("=All", text_ptr, 4) != 0) {
1967 pr_err("Unable to locate All value for"
1968 " SendTargets key, cannot continue.\n");
1969 kfree(text_in);
1970 return -1;
1971 }
1972/*#warning Support SendTargets=(iSCSI Target Name/Nothing) values. */
1973 kfree(text_in);
1974 }
1975
1976 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
1977 if (!cmd)
1978 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1979 1, buf, conn);
1980
1981 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1982 cmd->i_state = ISTATE_SEND_TEXTRSP;
1983 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1984 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1985 cmd->targ_xfer_tag = 0xFFFFFFFF;
1986 cmd->cmd_sn = hdr->cmdsn;
1987 cmd->exp_stat_sn = hdr->exp_statsn;
1988 cmd->data_direction = DMA_NONE;
1989
1990 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001991 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001992 spin_unlock_bh(&conn->cmd_lock);
1993
1994 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
1995
1996 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1997 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1998 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1999 return iscsit_add_reject_from_cmd(
2000 ISCSI_REASON_PROTOCOL_ERROR,
2001 1, 0, buf, cmd);
2002
2003 return 0;
2004 }
2005
2006 return iscsit_execute_cmd(cmd, 0);
2007}
2008
2009int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2010{
2011 struct iscsi_conn *conn_p;
2012 struct iscsi_session *sess = conn->sess;
2013
2014 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2015 " for SID: %u.\n", conn->cid, conn->sess->sid);
2016
2017 atomic_set(&sess->session_logout, 1);
2018 atomic_set(&conn->conn_logout_remove, 1);
2019 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2020
2021 iscsit_inc_conn_usage_count(conn);
2022 iscsit_inc_session_usage_count(sess);
2023
2024 spin_lock_bh(&sess->conn_lock);
2025 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2026 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2027 continue;
2028
2029 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2030 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2031 }
2032 spin_unlock_bh(&sess->conn_lock);
2033
2034 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2035
2036 return 0;
2037}
2038
2039int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2040{
2041 struct iscsi_conn *l_conn;
2042 struct iscsi_session *sess = conn->sess;
2043
2044 pr_debug("Received logout request CLOSECONNECTION for CID:"
2045 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2046
2047 /*
2048 * A Logout Request with a CLOSECONNECTION reason code for a CID
2049 * can arrive on a connection with a differing CID.
2050 */
2051 if (conn->cid == cmd->logout_cid) {
2052 spin_lock_bh(&conn->state_lock);
2053 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2054 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2055
2056 atomic_set(&conn->conn_logout_remove, 1);
2057 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2058 iscsit_inc_conn_usage_count(conn);
2059
2060 spin_unlock_bh(&conn->state_lock);
2061 } else {
2062 /*
2063 * Handle all different cid CLOSECONNECTION requests in
2064 * iscsit_logout_post_handler_diffcid() as to give enough
2065 * time for any non immediate command's CmdSN to be
2066 * acknowledged on the connection in question.
2067 *
2068 * Here we simply make sure the CID is still around.
2069 */
2070 l_conn = iscsit_get_conn_from_cid(sess,
2071 cmd->logout_cid);
2072 if (!l_conn) {
2073 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2074 iscsit_add_cmd_to_response_queue(cmd, conn,
2075 cmd->i_state);
2076 return 0;
2077 }
2078
2079 iscsit_dec_conn_usage_count(l_conn);
2080 }
2081
2082 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2083
2084 return 0;
2085}
2086
2087int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2088{
2089 struct iscsi_session *sess = conn->sess;
2090
2091 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2092 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2093
2094 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2095 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2096 " while ERL!=2.\n");
2097 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2098 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2099 return 0;
2100 }
2101
2102 if (conn->cid == cmd->logout_cid) {
2103 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2104 " with CID: %hu on CID: %hu, implementation error.\n",
2105 cmd->logout_cid, conn->cid);
2106 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2107 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2108 return 0;
2109 }
2110
2111 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2112
2113 return 0;
2114}
2115
2116static int iscsit_handle_logout_cmd(
2117 struct iscsi_conn *conn,
2118 unsigned char *buf)
2119{
2120 int cmdsn_ret, logout_remove = 0;
2121 u8 reason_code = 0;
2122 struct iscsi_cmd *cmd;
2123 struct iscsi_logout *hdr;
2124 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2125
2126 hdr = (struct iscsi_logout *) buf;
2127 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002128 hdr->cid = be16_to_cpu(hdr->cid);
2129 hdr->cmdsn = be32_to_cpu(hdr->cmdsn);
2130 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
2131
2132 if (tiqn) {
2133 spin_lock(&tiqn->logout_stats.lock);
2134 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2135 tiqn->logout_stats.normal_logouts++;
2136 else
2137 tiqn->logout_stats.abnormal_logouts++;
2138 spin_unlock(&tiqn->logout_stats.lock);
2139 }
2140
2141 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2142 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2143 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2144 hdr->cid, conn->cid);
2145
2146 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2147 pr_err("Received logout request on connection that"
2148 " is not in logged in state, ignoring request.\n");
2149 return 0;
2150 }
2151
2152 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
2153 if (!cmd)
2154 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES, 1,
2155 buf, conn);
2156
2157 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2158 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2159 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2160 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2161 cmd->targ_xfer_tag = 0xFFFFFFFF;
2162 cmd->cmd_sn = hdr->cmdsn;
2163 cmd->exp_stat_sn = hdr->exp_statsn;
2164 cmd->logout_cid = hdr->cid;
2165 cmd->logout_reason = reason_code;
2166 cmd->data_direction = DMA_NONE;
2167
2168 /*
2169 * We need to sleep in these cases (by returning 1) until the Logout
2170 * Response gets sent in the tx thread.
2171 */
2172 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2173 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
2174 (hdr->cid == conn->cid)))
2175 logout_remove = 1;
2176
2177 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002178 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002179 spin_unlock_bh(&conn->cmd_lock);
2180
2181 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
2182 iscsit_ack_from_expstatsn(conn, hdr->exp_statsn);
2183
2184 /*
2185 * Immediate commands are executed, well, immediately.
2186 * Non-Immediate Logout Commands are executed in CmdSN order.
2187 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002188 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002189 int ret = iscsit_execute_cmd(cmd, 0);
2190
2191 if (ret < 0)
2192 return ret;
2193 } else {
2194 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2195 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2196 logout_remove = 0;
2197 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2198 return iscsit_add_reject_from_cmd(
2199 ISCSI_REASON_PROTOCOL_ERROR,
2200 1, 0, buf, cmd);
2201 }
2202 }
2203
2204 return logout_remove;
2205}
2206
2207static int iscsit_handle_snack(
2208 struct iscsi_conn *conn,
2209 unsigned char *buf)
2210{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002211 struct iscsi_snack *hdr;
2212
2213 hdr = (struct iscsi_snack *) buf;
2214 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002215 hdr->ttt = be32_to_cpu(hdr->ttt);
2216 hdr->exp_statsn = be32_to_cpu(hdr->exp_statsn);
2217 hdr->begrun = be32_to_cpu(hdr->begrun);
2218 hdr->runlength = be32_to_cpu(hdr->runlength);
2219
2220 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2221 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2222 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2223 hdr->begrun, hdr->runlength, conn->cid);
2224
2225 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2226 pr_err("Initiator sent SNACK request while in"
2227 " ErrorRecoveryLevel=0.\n");
2228 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2229 buf, conn);
2230 }
2231 /*
2232 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2233 * call from inside iscsi_send_recovery_datain_or_r2t().
2234 */
2235 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2236 case 0:
2237 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
2238 hdr->itt, hdr->ttt, hdr->begrun, hdr->runlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002239 case ISCSI_FLAG_SNACK_TYPE_STATUS:
2240 return iscsit_handle_status_snack(conn, hdr->itt, hdr->ttt,
2241 hdr->begrun, hdr->runlength);
2242 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
2243 return iscsit_handle_data_ack(conn, hdr->ttt, hdr->begrun,
2244 hdr->runlength);
2245 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2246 /* FIXME: Support R-Data SNACK */
2247 pr_err("R-Data SNACK Not Supported.\n");
2248 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2249 buf, conn);
2250 default:
2251 pr_err("Unknown SNACK type 0x%02x, protocol"
2252 " error.\n", hdr->flags & 0x0f);
2253 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2254 buf, conn);
2255 }
2256
2257 return 0;
2258}
2259
2260static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2261{
2262 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2263 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2264 wait_for_completion_interruptible_timeout(
2265 &conn->rx_half_close_comp,
2266 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2267 }
2268}
2269
2270static int iscsit_handle_immediate_data(
2271 struct iscsi_cmd *cmd,
2272 unsigned char *buf,
2273 u32 length)
2274{
2275 int iov_ret, rx_got = 0, rx_size = 0;
2276 u32 checksum, iov_count = 0, padding = 0;
2277 struct iscsi_conn *conn = cmd->conn;
2278 struct kvec *iov;
2279
2280 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2281 if (iov_ret < 0)
2282 return IMMEDIATE_DATA_CANNOT_RECOVER;
2283
2284 rx_size = length;
2285 iov_count = iov_ret;
2286 iov = &cmd->iov_data[0];
2287
2288 padding = ((-length) & 3);
2289 if (padding != 0) {
2290 iov[iov_count].iov_base = cmd->pad_bytes;
2291 iov[iov_count++].iov_len = padding;
2292 rx_size += padding;
2293 }
2294
2295 if (conn->conn_ops->DataDigest) {
2296 iov[iov_count].iov_base = &checksum;
2297 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2298 rx_size += ISCSI_CRC_LEN;
2299 }
2300
2301 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2302
2303 iscsit_unmap_iovec(cmd);
2304
2305 if (rx_got != rx_size) {
2306 iscsit_rx_thread_wait_for_tcp(conn);
2307 return IMMEDIATE_DATA_CANNOT_RECOVER;
2308 }
2309
2310 if (conn->conn_ops->DataDigest) {
2311 u32 data_crc;
2312
2313 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2314 cmd->write_data_done, length, padding,
2315 cmd->pad_bytes);
2316
2317 if (checksum != data_crc) {
2318 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2319 " does not match computed 0x%08x\n", checksum,
2320 data_crc);
2321
2322 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2323 pr_err("Unable to recover from"
2324 " Immediate Data digest failure while"
2325 " in ERL=0.\n");
2326 iscsit_add_reject_from_cmd(
2327 ISCSI_REASON_DATA_DIGEST_ERROR,
2328 1, 0, buf, cmd);
2329 return IMMEDIATE_DATA_CANNOT_RECOVER;
2330 } else {
2331 iscsit_add_reject_from_cmd(
2332 ISCSI_REASON_DATA_DIGEST_ERROR,
2333 0, 0, buf, cmd);
2334 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2335 }
2336 } else {
2337 pr_debug("Got CRC32C DataDigest 0x%08x for"
2338 " %u bytes of Immediate Data\n", checksum,
2339 length);
2340 }
2341 }
2342
2343 cmd->write_data_done += length;
2344
Andy Groverebf1d952012-04-03 15:51:24 -07002345 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002346 spin_lock_bh(&cmd->istate_lock);
2347 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2348 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2349 spin_unlock_bh(&cmd->istate_lock);
2350 }
2351
2352 return IMMEDIATE_DATA_NORMAL_OPERATION;
2353}
2354
2355/*
2356 * Called with sess->conn_lock held.
2357 */
2358/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2359 with active network interface */
2360static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2361{
2362 struct iscsi_cmd *cmd;
2363 struct iscsi_conn *conn_p;
2364
2365 /*
2366 * Only send a Asynchronous Message on connections whos network
2367 * interface is still functional.
2368 */
2369 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2370 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2371 iscsit_inc_conn_usage_count(conn_p);
2372 break;
2373 }
2374 }
2375
2376 if (!conn_p)
2377 return;
2378
2379 cmd = iscsit_allocate_cmd(conn_p, GFP_KERNEL);
2380 if (!cmd) {
2381 iscsit_dec_conn_usage_count(conn_p);
2382 return;
2383 }
2384
2385 cmd->logout_cid = conn->cid;
2386 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2387 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2388
2389 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002390 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002391 spin_unlock_bh(&conn_p->cmd_lock);
2392
2393 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2394 iscsit_dec_conn_usage_count(conn_p);
2395}
2396
2397static int iscsit_send_conn_drop_async_message(
2398 struct iscsi_cmd *cmd,
2399 struct iscsi_conn *conn)
2400{
2401 struct iscsi_async *hdr;
2402
2403 cmd->tx_size = ISCSI_HDR_LEN;
2404 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2405
2406 hdr = (struct iscsi_async *) cmd->pdu;
2407 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2408 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002409 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002410 cmd->targ_xfer_tag = 0xFFFFFFFF;
2411 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2412 cmd->stat_sn = conn->stat_sn++;
2413 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2414 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2415 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2416 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2417 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2418 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2419 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2420
2421 if (conn->conn_ops->HeaderDigest) {
2422 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2423
2424 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2425 (unsigned char *)hdr, ISCSI_HDR_LEN,
2426 0, NULL, (u8 *)header_digest);
2427
2428 cmd->tx_size += ISCSI_CRC_LEN;
2429 pr_debug("Attaching CRC32C HeaderDigest to"
2430 " Async Message 0x%08x\n", *header_digest);
2431 }
2432
2433 cmd->iov_misc[0].iov_base = cmd->pdu;
2434 cmd->iov_misc[0].iov_len = cmd->tx_size;
2435 cmd->iov_misc_count = 1;
2436
2437 pr_debug("Sending Connection Dropped Async Message StatSN:"
2438 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2439 cmd->logout_cid, conn->cid);
2440 return 0;
2441}
2442
Andy Grover6f3c0e62012-04-03 15:51:09 -07002443static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2444{
2445 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2446 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2447 wait_for_completion_interruptible_timeout(
2448 &conn->tx_half_close_comp,
2449 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2450 }
2451}
2452
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002453static int iscsit_send_data_in(
2454 struct iscsi_cmd *cmd,
Andy Grover6f3c0e62012-04-03 15:51:09 -07002455 struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002456{
2457 int iov_ret = 0, set_statsn = 0;
2458 u32 iov_count = 0, tx_size = 0;
2459 struct iscsi_datain datain;
2460 struct iscsi_datain_req *dr;
2461 struct iscsi_data_rsp *hdr;
2462 struct kvec *iov;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002463 int eodr = 0;
2464 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002465
2466 memset(&datain, 0, sizeof(struct iscsi_datain));
2467 dr = iscsit_get_datain_values(cmd, &datain);
2468 if (!dr) {
2469 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2470 cmd->init_task_tag);
2471 return -1;
2472 }
2473
2474 /*
2475 * Be paranoid and double check the logic for now.
2476 */
Andy Groverebf1d952012-04-03 15:51:24 -07002477 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002478 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2479 " datain.length: %u exceeds cmd->data_length: %u\n",
2480 cmd->init_task_tag, datain.offset, datain.length,
Andy Groverebf1d952012-04-03 15:51:24 -07002481 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002482 return -1;
2483 }
2484
2485 spin_lock_bh(&conn->sess->session_stats_lock);
2486 conn->sess->tx_data_octets += datain.length;
2487 if (conn->sess->se_sess->se_node_acl) {
2488 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2489 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2490 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2491 }
2492 spin_unlock_bh(&conn->sess->session_stats_lock);
2493 /*
2494 * Special case for successfully execution w/ both DATAIN
2495 * and Sense Data.
2496 */
2497 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2498 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2499 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2500 else {
2501 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2502 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2503 iscsit_increment_maxcmdsn(cmd, conn->sess);
2504 cmd->stat_sn = conn->stat_sn++;
2505 set_statsn = 1;
2506 } else if (dr->dr_complete ==
2507 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2508 set_statsn = 1;
2509 }
2510
2511 hdr = (struct iscsi_data_rsp *) cmd->pdu;
2512 memset(hdr, 0, ISCSI_HDR_LEN);
2513 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2514 hdr->flags = datain.flags;
2515 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2516 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2517 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002518 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002519 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2520 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002521 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002522 }
2523 }
2524 hton24(hdr->dlength, datain.length);
2525 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2526 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2527 (struct scsi_lun *)&hdr->lun);
2528 else
2529 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2530
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002531 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002532 hdr->ttt = (hdr->flags & ISCSI_FLAG_DATA_ACK) ?
2533 cpu_to_be32(cmd->targ_xfer_tag) :
2534 0xFFFFFFFF;
2535 hdr->statsn = (set_statsn) ? cpu_to_be32(cmd->stat_sn) :
2536 0xFFFFFFFF;
2537 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2538 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2539 hdr->datasn = cpu_to_be32(datain.data_sn);
2540 hdr->offset = cpu_to_be32(datain.offset);
2541
2542 iov = &cmd->iov_data[0];
2543 iov[iov_count].iov_base = cmd->pdu;
2544 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2545 tx_size += ISCSI_HDR_LEN;
2546
2547 if (conn->conn_ops->HeaderDigest) {
2548 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2549
2550 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2551 (unsigned char *)hdr, ISCSI_HDR_LEN,
2552 0, NULL, (u8 *)header_digest);
2553
2554 iov[0].iov_len += ISCSI_CRC_LEN;
2555 tx_size += ISCSI_CRC_LEN;
2556
2557 pr_debug("Attaching CRC32 HeaderDigest"
2558 " for DataIN PDU 0x%08x\n", *header_digest);
2559 }
2560
2561 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], datain.offset, datain.length);
2562 if (iov_ret < 0)
2563 return -1;
2564
2565 iov_count += iov_ret;
2566 tx_size += datain.length;
2567
2568 cmd->padding = ((-datain.length) & 3);
2569 if (cmd->padding) {
2570 iov[iov_count].iov_base = cmd->pad_bytes;
2571 iov[iov_count++].iov_len = cmd->padding;
2572 tx_size += cmd->padding;
2573
2574 pr_debug("Attaching %u padding bytes\n",
2575 cmd->padding);
2576 }
2577 if (conn->conn_ops->DataDigest) {
2578 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2579 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2580
2581 iov[iov_count].iov_base = &cmd->data_crc;
2582 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2583 tx_size += ISCSI_CRC_LEN;
2584
2585 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2586 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2587 }
2588
2589 cmd->iov_data_count = iov_count;
2590 cmd->tx_size = tx_size;
2591
2592 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2593 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2594 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2595 ntohl(hdr->offset), datain.length, conn->cid);
2596
Andy Grover6f3c0e62012-04-03 15:51:09 -07002597 /* sendpage is preferred but can't insert markers */
2598 if (!conn->conn_ops->IFMarker)
2599 ret = iscsit_fe_sendpage_sg(cmd, conn);
2600 else
2601 ret = iscsit_send_tx_data(cmd, conn, 0);
2602
2603 iscsit_unmap_iovec(cmd);
2604
2605 if (ret < 0) {
2606 iscsit_tx_thread_wait_for_tcp(conn);
2607 return ret;
2608 }
2609
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002610 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002611 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002612 2 : 1;
2613 iscsit_free_datain_req(cmd, dr);
2614 }
2615
Andy Grover6f3c0e62012-04-03 15:51:09 -07002616 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002617}
2618
2619static int iscsit_send_logout_response(
2620 struct iscsi_cmd *cmd,
2621 struct iscsi_conn *conn)
2622{
2623 int niov = 0, tx_size;
2624 struct iscsi_conn *logout_conn = NULL;
2625 struct iscsi_conn_recovery *cr = NULL;
2626 struct iscsi_session *sess = conn->sess;
2627 struct kvec *iov;
2628 struct iscsi_logout_rsp *hdr;
2629 /*
2630 * The actual shutting down of Sessions and/or Connections
2631 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2632 * is done in scsi_logout_post_handler().
2633 */
2634 switch (cmd->logout_reason) {
2635 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2636 pr_debug("iSCSI session logout successful, setting"
2637 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2638 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2639 break;
2640 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2641 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2642 break;
2643 /*
2644 * For CLOSECONNECTION logout requests carrying
2645 * a matching logout CID -> local CID, the reference
2646 * for the local CID will have been incremented in
2647 * iscsi_logout_closeconnection().
2648 *
2649 * For CLOSECONNECTION logout requests carrying
2650 * a different CID than the connection it arrived
2651 * on, the connection responding to cmd->logout_cid
2652 * is stopped in iscsit_logout_post_handler_diffcid().
2653 */
2654
2655 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2656 " successful.\n", cmd->logout_cid, conn->cid);
2657 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2658 break;
2659 case ISCSI_LOGOUT_REASON_RECOVERY:
2660 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2661 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2662 break;
2663 /*
2664 * If the connection is still active from our point of view
2665 * force connection recovery to occur.
2666 */
2667 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2668 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002669 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002670 iscsit_connection_reinstatement_rcfr(logout_conn);
2671 iscsit_dec_conn_usage_count(logout_conn);
2672 }
2673
2674 cr = iscsit_get_inactive_connection_recovery_entry(
2675 conn->sess, cmd->logout_cid);
2676 if (!cr) {
2677 pr_err("Unable to locate CID: %hu for"
2678 " REMOVECONNFORRECOVERY Logout Request.\n",
2679 cmd->logout_cid);
2680 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2681 break;
2682 }
2683
2684 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2685
2686 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2687 " for recovery for CID: %hu on CID: %hu successful.\n",
2688 cmd->logout_cid, conn->cid);
2689 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2690 break;
2691 default:
2692 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2693 cmd->logout_reason);
2694 return -1;
2695 }
2696
2697 tx_size = ISCSI_HDR_LEN;
2698 hdr = (struct iscsi_logout_rsp *)cmd->pdu;
2699 memset(hdr, 0, ISCSI_HDR_LEN);
2700 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2701 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2702 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002703 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002704 cmd->stat_sn = conn->stat_sn++;
2705 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2706
2707 iscsit_increment_maxcmdsn(cmd, conn->sess);
2708 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2709 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2710
2711 iov = &cmd->iov_misc[0];
2712 iov[niov].iov_base = cmd->pdu;
2713 iov[niov++].iov_len = ISCSI_HDR_LEN;
2714
2715 if (conn->conn_ops->HeaderDigest) {
2716 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2717
2718 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2719 (unsigned char *)hdr, ISCSI_HDR_LEN,
2720 0, NULL, (u8 *)header_digest);
2721
2722 iov[0].iov_len += ISCSI_CRC_LEN;
2723 tx_size += ISCSI_CRC_LEN;
2724 pr_debug("Attaching CRC32C HeaderDigest to"
2725 " Logout Response 0x%08x\n", *header_digest);
2726 }
2727 cmd->iov_misc_count = niov;
2728 cmd->tx_size = tx_size;
2729
2730 pr_debug("Sending Logout Response ITT: 0x%08x StatSN:"
2731 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2732 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2733 cmd->logout_cid, conn->cid);
2734
2735 return 0;
2736}
2737
2738/*
2739 * Unsolicited NOPIN, either requesting a response or not.
2740 */
2741static int iscsit_send_unsolicited_nopin(
2742 struct iscsi_cmd *cmd,
2743 struct iscsi_conn *conn,
2744 int want_response)
2745{
2746 int tx_size = ISCSI_HDR_LEN;
2747 struct iscsi_nopin *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002748 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002749
2750 hdr = (struct iscsi_nopin *) cmd->pdu;
2751 memset(hdr, 0, ISCSI_HDR_LEN);
2752 hdr->opcode = ISCSI_OP_NOOP_IN;
2753 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002754 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002755 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2756 cmd->stat_sn = conn->stat_sn;
2757 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2758 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2759 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2760
2761 if (conn->conn_ops->HeaderDigest) {
2762 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2763
2764 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2765 (unsigned char *)hdr, ISCSI_HDR_LEN,
2766 0, NULL, (u8 *)header_digest);
2767
2768 tx_size += ISCSI_CRC_LEN;
2769 pr_debug("Attaching CRC32C HeaderDigest to"
2770 " NopIN 0x%08x\n", *header_digest);
2771 }
2772
2773 cmd->iov_misc[0].iov_base = cmd->pdu;
2774 cmd->iov_misc[0].iov_len = tx_size;
2775 cmd->iov_misc_count = 1;
2776 cmd->tx_size = tx_size;
2777
2778 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2779 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2780
Andy Grover6f3c0e62012-04-03 15:51:09 -07002781 ret = iscsit_send_tx_data(cmd, conn, 1);
2782 if (ret < 0) {
2783 iscsit_tx_thread_wait_for_tcp(conn);
2784 return ret;
2785 }
2786
2787 spin_lock_bh(&cmd->istate_lock);
2788 cmd->i_state = want_response ?
2789 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2790 spin_unlock_bh(&cmd->istate_lock);
2791
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002792 return 0;
2793}
2794
2795static int iscsit_send_nopin_response(
2796 struct iscsi_cmd *cmd,
2797 struct iscsi_conn *conn)
2798{
2799 int niov = 0, tx_size;
2800 u32 padding = 0;
2801 struct kvec *iov;
2802 struct iscsi_nopin *hdr;
2803
2804 tx_size = ISCSI_HDR_LEN;
2805 hdr = (struct iscsi_nopin *) cmd->pdu;
2806 memset(hdr, 0, ISCSI_HDR_LEN);
2807 hdr->opcode = ISCSI_OP_NOOP_IN;
2808 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2809 hton24(hdr->dlength, cmd->buf_ptr_size);
2810 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002811 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002812 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2813 cmd->stat_sn = conn->stat_sn++;
2814 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2815
2816 iscsit_increment_maxcmdsn(cmd, conn->sess);
2817 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2818 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2819
2820 iov = &cmd->iov_misc[0];
2821 iov[niov].iov_base = cmd->pdu;
2822 iov[niov++].iov_len = ISCSI_HDR_LEN;
2823
2824 if (conn->conn_ops->HeaderDigest) {
2825 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2826
2827 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2828 (unsigned char *)hdr, ISCSI_HDR_LEN,
2829 0, NULL, (u8 *)header_digest);
2830
2831 iov[0].iov_len += ISCSI_CRC_LEN;
2832 tx_size += ISCSI_CRC_LEN;
2833 pr_debug("Attaching CRC32C HeaderDigest"
2834 " to NopIn 0x%08x\n", *header_digest);
2835 }
2836
2837 /*
2838 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2839 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2840 */
2841 if (cmd->buf_ptr_size) {
2842 iov[niov].iov_base = cmd->buf_ptr;
2843 iov[niov++].iov_len = cmd->buf_ptr_size;
2844 tx_size += cmd->buf_ptr_size;
2845
2846 pr_debug("Echoing back %u bytes of ping"
2847 " data.\n", cmd->buf_ptr_size);
2848
2849 padding = ((-cmd->buf_ptr_size) & 3);
2850 if (padding != 0) {
2851 iov[niov].iov_base = &cmd->pad_bytes;
2852 iov[niov++].iov_len = padding;
2853 tx_size += padding;
2854 pr_debug("Attaching %u additional"
2855 " padding bytes.\n", padding);
2856 }
2857 if (conn->conn_ops->DataDigest) {
2858 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2859 cmd->buf_ptr, cmd->buf_ptr_size,
2860 padding, (u8 *)&cmd->pad_bytes,
2861 (u8 *)&cmd->data_crc);
2862
2863 iov[niov].iov_base = &cmd->data_crc;
2864 iov[niov++].iov_len = ISCSI_CRC_LEN;
2865 tx_size += ISCSI_CRC_LEN;
2866 pr_debug("Attached DataDigest for %u"
2867 " bytes of ping data, CRC 0x%08x\n",
2868 cmd->buf_ptr_size, cmd->data_crc);
2869 }
2870 }
2871
2872 cmd->iov_misc_count = niov;
2873 cmd->tx_size = tx_size;
2874
2875 pr_debug("Sending NOPIN Response ITT: 0x%08x, TTT:"
2876 " 0x%08x, StatSN: 0x%08x, Length %u\n", cmd->init_task_tag,
2877 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2878
2879 return 0;
2880}
2881
Andy Grover6f3c0e62012-04-03 15:51:09 -07002882static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002883 struct iscsi_cmd *cmd,
2884 struct iscsi_conn *conn)
2885{
2886 int tx_size = 0;
2887 struct iscsi_r2t *r2t;
2888 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002889 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002890
2891 r2t = iscsit_get_r2t_from_list(cmd);
2892 if (!r2t)
2893 return -1;
2894
2895 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
2896 memset(hdr, 0, ISCSI_HDR_LEN);
2897 hdr->opcode = ISCSI_OP_R2T;
2898 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2899 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2900 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002901 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002902 spin_lock_bh(&conn->sess->ttt_lock);
2903 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2904 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
2905 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2906 spin_unlock_bh(&conn->sess->ttt_lock);
2907 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
2908 hdr->statsn = cpu_to_be32(conn->stat_sn);
2909 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2910 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2911 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
2912 hdr->data_offset = cpu_to_be32(r2t->offset);
2913 hdr->data_length = cpu_to_be32(r2t->xfer_len);
2914
2915 cmd->iov_misc[0].iov_base = cmd->pdu;
2916 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
2917 tx_size += ISCSI_HDR_LEN;
2918
2919 if (conn->conn_ops->HeaderDigest) {
2920 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2921
2922 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2923 (unsigned char *)hdr, ISCSI_HDR_LEN,
2924 0, NULL, (u8 *)header_digest);
2925
2926 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
2927 tx_size += ISCSI_CRC_LEN;
2928 pr_debug("Attaching CRC32 HeaderDigest for R2T"
2929 " PDU 0x%08x\n", *header_digest);
2930 }
2931
2932 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
2933 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
2934 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
2935 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
2936 r2t->offset, r2t->xfer_len, conn->cid);
2937
2938 cmd->iov_misc_count = 1;
2939 cmd->tx_size = tx_size;
2940
2941 spin_lock_bh(&cmd->r2t_lock);
2942 r2t->sent_r2t = 1;
2943 spin_unlock_bh(&cmd->r2t_lock);
2944
Andy Grover6f3c0e62012-04-03 15:51:09 -07002945 ret = iscsit_send_tx_data(cmd, conn, 1);
2946 if (ret < 0) {
2947 iscsit_tx_thread_wait_for_tcp(conn);
2948 return ret;
2949 }
2950
2951 spin_lock_bh(&cmd->dataout_timeout_lock);
2952 iscsit_start_dataout_timer(cmd, conn);
2953 spin_unlock_bh(&cmd->dataout_timeout_lock);
2954
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002955 return 0;
2956}
2957
2958/*
Andy Grover8b1e1242012-04-03 15:51:12 -07002959 * @recovery: If called from iscsi_task_reassign_complete_write() for
2960 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002961 */
2962int iscsit_build_r2ts_for_cmd(
2963 struct iscsi_cmd *cmd,
2964 struct iscsi_conn *conn,
Andy Grover8b1e1242012-04-03 15:51:12 -07002965 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002966{
2967 int first_r2t = 1;
2968 u32 offset = 0, xfer_len = 0;
2969
2970 spin_lock_bh(&cmd->r2t_lock);
2971 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
2972 spin_unlock_bh(&cmd->r2t_lock);
2973 return 0;
2974 }
2975
Andy Grover8b1e1242012-04-03 15:51:12 -07002976 if (conn->sess->sess_ops->DataSequenceInOrder &&
2977 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07002978 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002979
2980 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
2981 if (conn->sess->sess_ops->DataSequenceInOrder) {
2982 offset = cmd->r2t_offset;
2983
Andy Grover8b1e1242012-04-03 15:51:12 -07002984 if (first_r2t && recovery) {
2985 int new_data_end = offset +
2986 conn->sess->sess_ops->MaxBurstLength -
2987 cmd->next_burst_len;
2988
Andy Groverebf1d952012-04-03 15:51:24 -07002989 if (new_data_end > cmd->se_cmd.data_length)
2990 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07002991 else
2992 xfer_len =
2993 conn->sess->sess_ops->MaxBurstLength -
2994 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002995 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07002996 int new_data_end = offset +
2997 conn->sess->sess_ops->MaxBurstLength;
2998
Andy Groverebf1d952012-04-03 15:51:24 -07002999 if (new_data_end > cmd->se_cmd.data_length)
3000 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003001 else
3002 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003003 }
3004 cmd->r2t_offset += xfer_len;
3005
Andy Groverebf1d952012-04-03 15:51:24 -07003006 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003007 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3008 } else {
3009 struct iscsi_seq *seq;
3010
3011 seq = iscsit_get_seq_holder_for_r2t(cmd);
3012 if (!seq) {
3013 spin_unlock_bh(&cmd->r2t_lock);
3014 return -1;
3015 }
3016
3017 offset = seq->offset;
3018 xfer_len = seq->xfer_len;
3019
3020 if (cmd->seq_send_order == cmd->seq_count)
3021 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3022 }
3023 cmd->outstanding_r2ts++;
3024 first_r2t = 0;
3025
3026 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3027 spin_unlock_bh(&cmd->r2t_lock);
3028 return -1;
3029 }
3030
3031 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3032 break;
3033 }
3034 spin_unlock_bh(&cmd->r2t_lock);
3035
3036 return 0;
3037}
3038
3039static int iscsit_send_status(
3040 struct iscsi_cmd *cmd,
3041 struct iscsi_conn *conn)
3042{
3043 u8 iov_count = 0, recovery;
3044 u32 padding = 0, tx_size = 0;
3045 struct iscsi_scsi_rsp *hdr;
3046 struct kvec *iov;
3047
3048 recovery = (cmd->i_state != ISTATE_SEND_STATUS);
3049 if (!recovery)
3050 cmd->stat_sn = conn->stat_sn++;
3051
3052 spin_lock_bh(&conn->sess->session_stats_lock);
3053 conn->sess->rsp_pdus++;
3054 spin_unlock_bh(&conn->sess->session_stats_lock);
3055
3056 hdr = (struct iscsi_scsi_rsp *) cmd->pdu;
3057 memset(hdr, 0, ISCSI_HDR_LEN);
3058 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3059 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3060 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3061 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003062 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003063 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3064 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003065 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003066 }
3067 hdr->response = cmd->iscsi_response;
3068 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003069 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003070 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3071
3072 iscsit_increment_maxcmdsn(cmd, conn->sess);
3073 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3074 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3075
3076 iov = &cmd->iov_misc[0];
3077 iov[iov_count].iov_base = cmd->pdu;
3078 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3079 tx_size += ISCSI_HDR_LEN;
3080
3081 /*
3082 * Attach SENSE DATA payload to iSCSI Response PDU
3083 */
3084 if (cmd->se_cmd.sense_buffer &&
3085 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3086 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003087 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3088 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3089
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003090 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
3091 hton24(hdr->dlength, cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003092 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003093 iov[iov_count++].iov_len =
3094 (cmd->se_cmd.scsi_sense_length + padding);
3095 tx_size += cmd->se_cmd.scsi_sense_length;
3096
3097 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003098 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003099 cmd->se_cmd.scsi_sense_length, 0, padding);
3100 tx_size += padding;
3101 pr_debug("Adding %u bytes of padding to"
3102 " SENSE.\n", padding);
3103 }
3104
3105 if (conn->conn_ops->DataDigest) {
3106 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003107 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003108 (cmd->se_cmd.scsi_sense_length + padding),
3109 0, NULL, (u8 *)&cmd->data_crc);
3110
3111 iov[iov_count].iov_base = &cmd->data_crc;
3112 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3113 tx_size += ISCSI_CRC_LEN;
3114
3115 pr_debug("Attaching CRC32 DataDigest for"
3116 " SENSE, %u bytes CRC 0x%08x\n",
3117 (cmd->se_cmd.scsi_sense_length + padding),
3118 cmd->data_crc);
3119 }
3120
3121 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3122 " Response PDU\n",
3123 cmd->se_cmd.scsi_sense_length);
3124 }
3125
3126 if (conn->conn_ops->HeaderDigest) {
3127 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3128
3129 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3130 (unsigned char *)hdr, ISCSI_HDR_LEN,
3131 0, NULL, (u8 *)header_digest);
3132
3133 iov[0].iov_len += ISCSI_CRC_LEN;
3134 tx_size += ISCSI_CRC_LEN;
3135 pr_debug("Attaching CRC32 HeaderDigest for Response"
3136 " PDU 0x%08x\n", *header_digest);
3137 }
3138
3139 cmd->iov_misc_count = iov_count;
3140 cmd->tx_size = tx_size;
3141
3142 pr_debug("Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3143 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3144 (!recovery) ? "" : "Recovery ", cmd->init_task_tag,
3145 cmd->stat_sn, 0x00, cmd->se_cmd.scsi_status, conn->cid);
3146
3147 return 0;
3148}
3149
3150static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3151{
3152 switch (se_tmr->response) {
3153 case TMR_FUNCTION_COMPLETE:
3154 return ISCSI_TMF_RSP_COMPLETE;
3155 case TMR_TASK_DOES_NOT_EXIST:
3156 return ISCSI_TMF_RSP_NO_TASK;
3157 case TMR_LUN_DOES_NOT_EXIST:
3158 return ISCSI_TMF_RSP_NO_LUN;
3159 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3160 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3161 case TMR_FUNCTION_AUTHORIZATION_FAILED:
3162 return ISCSI_TMF_RSP_AUTH_FAILED;
3163 case TMR_FUNCTION_REJECTED:
3164 default:
3165 return ISCSI_TMF_RSP_REJECTED;
3166 }
3167}
3168
3169static int iscsit_send_task_mgt_rsp(
3170 struct iscsi_cmd *cmd,
3171 struct iscsi_conn *conn)
3172{
3173 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3174 struct iscsi_tm_rsp *hdr;
3175 u32 tx_size = 0;
3176
3177 hdr = (struct iscsi_tm_rsp *) cmd->pdu;
3178 memset(hdr, 0, ISCSI_HDR_LEN);
3179 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003180 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003181 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003182 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003183 cmd->stat_sn = conn->stat_sn++;
3184 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3185
3186 iscsit_increment_maxcmdsn(cmd, conn->sess);
3187 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3188 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3189
3190 cmd->iov_misc[0].iov_base = cmd->pdu;
3191 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3192 tx_size += ISCSI_HDR_LEN;
3193
3194 if (conn->conn_ops->HeaderDigest) {
3195 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3196
3197 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3198 (unsigned char *)hdr, ISCSI_HDR_LEN,
3199 0, NULL, (u8 *)header_digest);
3200
3201 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3202 tx_size += ISCSI_CRC_LEN;
3203 pr_debug("Attaching CRC32 HeaderDigest for Task"
3204 " Mgmt Response PDU 0x%08x\n", *header_digest);
3205 }
3206
3207 cmd->iov_misc_count = 1;
3208 cmd->tx_size = tx_size;
3209
3210 pr_debug("Built Task Management Response ITT: 0x%08x,"
3211 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3212 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3213
3214 return 0;
3215}
3216
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003217static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3218{
3219 bool ret = false;
3220
3221 if (np->np_sockaddr.ss_family == AF_INET6) {
3222 const struct sockaddr_in6 sin6 = {
3223 .sin6_addr = IN6ADDR_ANY_INIT };
3224 struct sockaddr_in6 *sock_in6 =
3225 (struct sockaddr_in6 *)&np->np_sockaddr;
3226
3227 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3228 sin6.sin6_addr.s6_addr, 16))
3229 ret = true;
3230 } else {
3231 struct sockaddr_in * sock_in =
3232 (struct sockaddr_in *)&np->np_sockaddr;
3233
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003234 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003235 ret = true;
3236 }
3237
3238 return ret;
3239}
3240
Andy Grover8b1e1242012-04-03 15:51:12 -07003241#define SENDTARGETS_BUF_LIMIT 32768U
3242
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003243static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3244{
3245 char *payload = NULL;
3246 struct iscsi_conn *conn = cmd->conn;
3247 struct iscsi_portal_group *tpg;
3248 struct iscsi_tiqn *tiqn;
3249 struct iscsi_tpg_np *tpg_np;
3250 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003251 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003252
Andy Grover8b1e1242012-04-03 15:51:12 -07003253 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3254 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003255
3256 payload = kzalloc(buffer_len, GFP_KERNEL);
3257 if (!payload) {
3258 pr_err("Unable to allocate memory for sendtargets"
3259 " response.\n");
3260 return -ENOMEM;
3261 }
3262
3263 spin_lock(&tiqn_lock);
3264 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3265 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3266 len += 1;
3267
3268 if ((len + payload_len) > buffer_len) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003269 end_of_buf = 1;
3270 goto eob;
3271 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003272 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003273 payload_len += len;
3274
3275 spin_lock(&tiqn->tiqn_tpg_lock);
3276 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3277
3278 spin_lock(&tpg->tpg_state_lock);
3279 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3280 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3281 spin_unlock(&tpg->tpg_state_lock);
3282 continue;
3283 }
3284 spin_unlock(&tpg->tpg_state_lock);
3285
3286 spin_lock(&tpg->tpg_np_lock);
3287 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3288 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003289 struct iscsi_np *np = tpg_np->tpg_np;
3290 bool inaddr_any = iscsit_check_inaddr_any(np);
3291
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003292 len = sprintf(buf, "TargetAddress="
3293 "%s%s%s:%hu,%hu",
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003294 (np->np_sockaddr.ss_family == AF_INET6) ?
3295 "[" : "", (inaddr_any == false) ?
3296 np->np_ip : conn->local_ip,
3297 (np->np_sockaddr.ss_family == AF_INET6) ?
3298 "]" : "", (inaddr_any == false) ?
3299 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003300 tpg->tpgt);
3301 len += 1;
3302
3303 if ((len + payload_len) > buffer_len) {
3304 spin_unlock(&tpg->tpg_np_lock);
3305 spin_unlock(&tiqn->tiqn_tpg_lock);
3306 end_of_buf = 1;
3307 goto eob;
3308 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003309 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003310 payload_len += len;
3311 }
3312 spin_unlock(&tpg->tpg_np_lock);
3313 }
3314 spin_unlock(&tiqn->tiqn_tpg_lock);
3315eob:
3316 if (end_of_buf)
3317 break;
3318 }
3319 spin_unlock(&tiqn_lock);
3320
3321 cmd->buf_ptr = payload;
3322
3323 return payload_len;
3324}
3325
3326/*
3327 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3328 * MaxRecvDataSegmentLength.
3329 */
3330static int iscsit_send_text_rsp(
3331 struct iscsi_cmd *cmd,
3332 struct iscsi_conn *conn)
3333{
3334 struct iscsi_text_rsp *hdr;
3335 struct kvec *iov;
3336 u32 padding = 0, tx_size = 0;
3337 int text_length, iov_count = 0;
3338
3339 text_length = iscsit_build_sendtargets_response(cmd);
3340 if (text_length < 0)
3341 return text_length;
3342
3343 padding = ((-text_length) & 3);
3344 if (padding != 0) {
3345 memset(cmd->buf_ptr + text_length, 0, padding);
3346 pr_debug("Attaching %u additional bytes for"
3347 " padding.\n", padding);
3348 }
3349
3350 hdr = (struct iscsi_text_rsp *) cmd->pdu;
3351 memset(hdr, 0, ISCSI_HDR_LEN);
3352 hdr->opcode = ISCSI_OP_TEXT_RSP;
3353 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3354 hton24(hdr->dlength, text_length);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003355 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003356 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3357 cmd->stat_sn = conn->stat_sn++;
3358 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3359
3360 iscsit_increment_maxcmdsn(cmd, conn->sess);
3361 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3362 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3363
3364 iov = &cmd->iov_misc[0];
3365
3366 iov[iov_count].iov_base = cmd->pdu;
3367 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3368 iov[iov_count].iov_base = cmd->buf_ptr;
3369 iov[iov_count++].iov_len = text_length + padding;
3370
3371 tx_size += (ISCSI_HDR_LEN + text_length + padding);
3372
3373 if (conn->conn_ops->HeaderDigest) {
3374 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3375
3376 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3377 (unsigned char *)hdr, ISCSI_HDR_LEN,
3378 0, NULL, (u8 *)header_digest);
3379
3380 iov[0].iov_len += ISCSI_CRC_LEN;
3381 tx_size += ISCSI_CRC_LEN;
3382 pr_debug("Attaching CRC32 HeaderDigest for"
3383 " Text Response PDU 0x%08x\n", *header_digest);
3384 }
3385
3386 if (conn->conn_ops->DataDigest) {
3387 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3388 cmd->buf_ptr, (text_length + padding),
3389 0, NULL, (u8 *)&cmd->data_crc);
3390
3391 iov[iov_count].iov_base = &cmd->data_crc;
3392 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3393 tx_size += ISCSI_CRC_LEN;
3394
3395 pr_debug("Attaching DataDigest for %u bytes of text"
3396 " data, CRC 0x%08x\n", (text_length + padding),
3397 cmd->data_crc);
3398 }
3399
3400 cmd->iov_misc_count = iov_count;
3401 cmd->tx_size = tx_size;
3402
3403 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3404 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3405 text_length, conn->cid);
3406 return 0;
3407}
3408
3409static int iscsit_send_reject(
3410 struct iscsi_cmd *cmd,
3411 struct iscsi_conn *conn)
3412{
3413 u32 iov_count = 0, tx_size = 0;
3414 struct iscsi_reject *hdr;
3415 struct kvec *iov;
3416
3417 hdr = (struct iscsi_reject *) cmd->pdu;
3418 hdr->opcode = ISCSI_OP_REJECT;
3419 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3420 hton24(hdr->dlength, ISCSI_HDR_LEN);
Nicholas Bellingerf25590f2012-09-22 17:21:06 -07003421 hdr->ffffffff = 0xffffffff;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003422 cmd->stat_sn = conn->stat_sn++;
3423 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3424 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3425 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3426
3427 iov = &cmd->iov_misc[0];
3428
3429 iov[iov_count].iov_base = cmd->pdu;
3430 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3431 iov[iov_count].iov_base = cmd->buf_ptr;
3432 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3433
3434 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3435
3436 if (conn->conn_ops->HeaderDigest) {
3437 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3438
3439 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3440 (unsigned char *)hdr, ISCSI_HDR_LEN,
3441 0, NULL, (u8 *)header_digest);
3442
3443 iov[0].iov_len += ISCSI_CRC_LEN;
3444 tx_size += ISCSI_CRC_LEN;
3445 pr_debug("Attaching CRC32 HeaderDigest for"
3446 " REJECT PDU 0x%08x\n", *header_digest);
3447 }
3448
3449 if (conn->conn_ops->DataDigest) {
3450 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3451 (unsigned char *)cmd->buf_ptr, ISCSI_HDR_LEN,
3452 0, NULL, (u8 *)&cmd->data_crc);
3453
3454 iov[iov_count].iov_base = &cmd->data_crc;
3455 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3456 tx_size += ISCSI_CRC_LEN;
3457 pr_debug("Attaching CRC32 DataDigest for REJECT"
3458 " PDU 0x%08x\n", cmd->data_crc);
3459 }
3460
3461 cmd->iov_misc_count = iov_count;
3462 cmd->tx_size = tx_size;
3463
3464 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3465 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3466
3467 return 0;
3468}
3469
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003470void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3471{
3472 struct iscsi_thread_set *ts = conn->thread_set;
3473 int ord, cpu;
3474 /*
3475 * thread_id is assigned from iscsit_global->ts_bitmap from
3476 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3477 *
3478 * Here we use thread_id to determine which CPU that this
3479 * iSCSI connection's iscsi_thread_set will be scheduled to
3480 * execute upon.
3481 */
3482 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003483 for_each_online_cpu(cpu) {
3484 if (ord-- == 0) {
3485 cpumask_set_cpu(cpu, conn->conn_cpumask);
3486 return;
3487 }
3488 }
3489 /*
3490 * This should never be reached..
3491 */
3492 dump_stack();
3493 cpumask_setall(conn->conn_cpumask);
3494}
3495
3496static inline void iscsit_thread_check_cpumask(
3497 struct iscsi_conn *conn,
3498 struct task_struct *p,
3499 int mode)
3500{
3501 char buf[128];
3502 /*
3503 * mode == 1 signals iscsi_target_tx_thread() usage.
3504 * mode == 0 signals iscsi_target_rx_thread() usage.
3505 */
3506 if (mode == 1) {
3507 if (!conn->conn_tx_reset_cpumask)
3508 return;
3509 conn->conn_tx_reset_cpumask = 0;
3510 } else {
3511 if (!conn->conn_rx_reset_cpumask)
3512 return;
3513 conn->conn_rx_reset_cpumask = 0;
3514 }
3515 /*
3516 * Update the CPU mask for this single kthread so that
3517 * both TX and RX kthreads are scheduled to run on the
3518 * same CPU.
3519 */
3520 memset(buf, 0, 128);
3521 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003522 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3523}
3524
Andy Grover6f3c0e62012-04-03 15:51:09 -07003525static int handle_immediate_queue(struct iscsi_conn *conn)
3526{
3527 struct iscsi_queue_req *qr;
3528 struct iscsi_cmd *cmd;
3529 u8 state;
3530 int ret;
3531
3532 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3533 atomic_set(&conn->check_immediate_queue, 0);
3534 cmd = qr->cmd;
3535 state = qr->state;
3536 kmem_cache_free(lio_qr_cache, qr);
3537
3538 switch (state) {
3539 case ISTATE_SEND_R2T:
3540 ret = iscsit_send_r2t(cmd, conn);
3541 if (ret < 0)
3542 goto err;
3543 break;
3544 case ISTATE_REMOVE:
3545 if (cmd->data_direction == DMA_TO_DEVICE)
3546 iscsit_stop_dataout_timer(cmd);
3547
3548 spin_lock_bh(&conn->cmd_lock);
3549 list_del(&cmd->i_conn_node);
3550 spin_unlock_bh(&conn->cmd_lock);
3551
3552 iscsit_free_cmd(cmd);
3553 continue;
3554 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3555 iscsit_mod_nopin_response_timer(conn);
3556 ret = iscsit_send_unsolicited_nopin(cmd,
3557 conn, 1);
3558 if (ret < 0)
3559 goto err;
3560 break;
3561 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3562 ret = iscsit_send_unsolicited_nopin(cmd,
3563 conn, 0);
3564 if (ret < 0)
3565 goto err;
3566 break;
3567 default:
3568 pr_err("Unknown Opcode: 0x%02x ITT:"
3569 " 0x%08x, i_state: %d on CID: %hu\n",
3570 cmd->iscsi_opcode, cmd->init_task_tag, state,
3571 conn->cid);
3572 goto err;
3573 }
3574 }
3575
3576 return 0;
3577
3578err:
3579 return -1;
3580}
3581
3582static int handle_response_queue(struct iscsi_conn *conn)
3583{
3584 struct iscsi_queue_req *qr;
3585 struct iscsi_cmd *cmd;
3586 u8 state;
3587 int ret;
3588
3589 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3590 cmd = qr->cmd;
3591 state = qr->state;
3592 kmem_cache_free(lio_qr_cache, qr);
3593
3594check_rsp_state:
3595 switch (state) {
3596 case ISTATE_SEND_DATAIN:
3597 ret = iscsit_send_data_in(cmd, conn);
3598 if (ret < 0)
3599 goto err;
3600 else if (!ret)
3601 /* more drs */
3602 goto check_rsp_state;
3603 else if (ret == 1) {
3604 /* all done */
3605 spin_lock_bh(&cmd->istate_lock);
3606 cmd->i_state = ISTATE_SENT_STATUS;
3607 spin_unlock_bh(&cmd->istate_lock);
3608 continue;
3609 } else if (ret == 2) {
3610 /* Still must send status,
3611 SCF_TRANSPORT_TASK_SENSE was set */
3612 spin_lock_bh(&cmd->istate_lock);
3613 cmd->i_state = ISTATE_SEND_STATUS;
3614 spin_unlock_bh(&cmd->istate_lock);
3615 state = ISTATE_SEND_STATUS;
3616 goto check_rsp_state;
3617 }
3618
3619 break;
3620 case ISTATE_SEND_STATUS:
3621 case ISTATE_SEND_STATUS_RECOVERY:
3622 ret = iscsit_send_status(cmd, conn);
3623 break;
3624 case ISTATE_SEND_LOGOUTRSP:
3625 ret = iscsit_send_logout_response(cmd, conn);
3626 break;
3627 case ISTATE_SEND_ASYNCMSG:
3628 ret = iscsit_send_conn_drop_async_message(
3629 cmd, conn);
3630 break;
3631 case ISTATE_SEND_NOPIN:
3632 ret = iscsit_send_nopin_response(cmd, conn);
3633 break;
3634 case ISTATE_SEND_REJECT:
3635 ret = iscsit_send_reject(cmd, conn);
3636 break;
3637 case ISTATE_SEND_TASKMGTRSP:
3638 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3639 if (ret != 0)
3640 break;
3641 ret = iscsit_tmr_post_handler(cmd, conn);
3642 if (ret != 0)
3643 iscsit_fall_back_to_erl0(conn->sess);
3644 break;
3645 case ISTATE_SEND_TEXTRSP:
3646 ret = iscsit_send_text_rsp(cmd, conn);
3647 break;
3648 default:
3649 pr_err("Unknown Opcode: 0x%02x ITT:"
3650 " 0x%08x, i_state: %d on CID: %hu\n",
3651 cmd->iscsi_opcode, cmd->init_task_tag,
3652 state, conn->cid);
3653 goto err;
3654 }
3655 if (ret < 0)
3656 goto err;
3657
3658 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3659 iscsit_tx_thread_wait_for_tcp(conn);
3660 iscsit_unmap_iovec(cmd);
3661 goto err;
3662 }
3663 iscsit_unmap_iovec(cmd);
3664
3665 switch (state) {
3666 case ISTATE_SEND_LOGOUTRSP:
3667 if (!iscsit_logout_post_handler(cmd, conn))
3668 goto restart;
3669 /* fall through */
3670 case ISTATE_SEND_STATUS:
3671 case ISTATE_SEND_ASYNCMSG:
3672 case ISTATE_SEND_NOPIN:
3673 case ISTATE_SEND_STATUS_RECOVERY:
3674 case ISTATE_SEND_TEXTRSP:
3675 case ISTATE_SEND_TASKMGTRSP:
3676 spin_lock_bh(&cmd->istate_lock);
3677 cmd->i_state = ISTATE_SENT_STATUS;
3678 spin_unlock_bh(&cmd->istate_lock);
3679 break;
3680 case ISTATE_SEND_REJECT:
3681 if (cmd->cmd_flags & ICF_REJECT_FAIL_CONN) {
3682 cmd->cmd_flags &= ~ICF_REJECT_FAIL_CONN;
3683 complete(&cmd->reject_comp);
3684 goto err;
3685 }
3686 complete(&cmd->reject_comp);
3687 break;
3688 default:
3689 pr_err("Unknown Opcode: 0x%02x ITT:"
3690 " 0x%08x, i_state: %d on CID: %hu\n",
3691 cmd->iscsi_opcode, cmd->init_task_tag,
3692 cmd->i_state, conn->cid);
3693 goto err;
3694 }
3695
3696 if (atomic_read(&conn->check_immediate_queue))
3697 break;
3698 }
3699
3700 return 0;
3701
3702err:
3703 return -1;
3704restart:
3705 return -EAGAIN;
3706}
3707
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003708int iscsi_target_tx_thread(void *arg)
3709{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003710 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003711 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003712 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003713 /*
3714 * Allow ourselves to be interrupted by SIGINT so that a
3715 * connection recovery / failure event can be triggered externally.
3716 */
3717 allow_signal(SIGINT);
3718
3719restart:
3720 conn = iscsi_tx_thread_pre_handler(ts);
3721 if (!conn)
3722 goto out;
3723
Andy Grover6f3c0e62012-04-03 15:51:09 -07003724 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003725
3726 while (!kthread_should_stop()) {
3727 /*
3728 * Ensure that both TX and RX per connection kthreads
3729 * are scheduled to run on the same CPU.
3730 */
3731 iscsit_thread_check_cpumask(conn, current, 1);
3732
3733 schedule_timeout_interruptible(MAX_SCHEDULE_TIMEOUT);
3734
3735 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3736 signal_pending(current))
3737 goto transport_err;
3738
Andy Grover6f3c0e62012-04-03 15:51:09 -07003739 ret = handle_immediate_queue(conn);
3740 if (ret < 0)
3741 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003742
Andy Grover6f3c0e62012-04-03 15:51:09 -07003743 ret = handle_response_queue(conn);
3744 if (ret == -EAGAIN)
3745 goto restart;
3746 else if (ret < 0)
3747 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003748 }
3749
3750transport_err:
3751 iscsit_take_action_for_connection_exit(conn);
3752 goto restart;
3753out:
3754 return 0;
3755}
3756
3757int iscsi_target_rx_thread(void *arg)
3758{
3759 int ret;
3760 u8 buffer[ISCSI_HDR_LEN], opcode;
3761 u32 checksum = 0, digest = 0;
3762 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01003763 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003764 struct kvec iov;
3765 /*
3766 * Allow ourselves to be interrupted by SIGINT so that a
3767 * connection recovery / failure event can be triggered externally.
3768 */
3769 allow_signal(SIGINT);
3770
3771restart:
3772 conn = iscsi_rx_thread_pre_handler(ts);
3773 if (!conn)
3774 goto out;
3775
3776 while (!kthread_should_stop()) {
3777 /*
3778 * Ensure that both TX and RX per connection kthreads
3779 * are scheduled to run on the same CPU.
3780 */
3781 iscsit_thread_check_cpumask(conn, current, 0);
3782
3783 memset(buffer, 0, ISCSI_HDR_LEN);
3784 memset(&iov, 0, sizeof(struct kvec));
3785
3786 iov.iov_base = buffer;
3787 iov.iov_len = ISCSI_HDR_LEN;
3788
3789 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3790 if (ret != ISCSI_HDR_LEN) {
3791 iscsit_rx_thread_wait_for_tcp(conn);
3792 goto transport_err;
3793 }
3794
3795 /*
3796 * Set conn->bad_hdr for use with REJECT PDUs.
3797 */
3798 memcpy(&conn->bad_hdr, &buffer, ISCSI_HDR_LEN);
3799
3800 if (conn->conn_ops->HeaderDigest) {
3801 iov.iov_base = &digest;
3802 iov.iov_len = ISCSI_CRC_LEN;
3803
3804 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3805 if (ret != ISCSI_CRC_LEN) {
3806 iscsit_rx_thread_wait_for_tcp(conn);
3807 goto transport_err;
3808 }
3809
3810 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
3811 buffer, ISCSI_HDR_LEN,
3812 0, NULL, (u8 *)&checksum);
3813
3814 if (digest != checksum) {
3815 pr_err("HeaderDigest CRC32C failed,"
3816 " received 0x%08x, computed 0x%08x\n",
3817 digest, checksum);
3818 /*
3819 * Set the PDU to 0xff so it will intentionally
3820 * hit default in the switch below.
3821 */
3822 memset(buffer, 0xff, ISCSI_HDR_LEN);
3823 spin_lock_bh(&conn->sess->session_stats_lock);
3824 conn->sess->conn_digest_errors++;
3825 spin_unlock_bh(&conn->sess->session_stats_lock);
3826 } else {
3827 pr_debug("Got HeaderDigest CRC32C"
3828 " 0x%08x\n", checksum);
3829 }
3830 }
3831
3832 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
3833 goto transport_err;
3834
3835 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3836
3837 if (conn->sess->sess_ops->SessionType &&
3838 ((!(opcode & ISCSI_OP_TEXT)) ||
3839 (!(opcode & ISCSI_OP_LOGOUT)))) {
3840 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3841 " while in Discovery Session, rejecting.\n", opcode);
3842 iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
3843 buffer, conn);
3844 goto transport_err;
3845 }
3846
3847 switch (opcode) {
3848 case ISCSI_OP_SCSI_CMD:
3849 if (iscsit_handle_scsi_cmd(conn, buffer) < 0)
3850 goto transport_err;
3851 break;
3852 case ISCSI_OP_SCSI_DATA_OUT:
3853 if (iscsit_handle_data_out(conn, buffer) < 0)
3854 goto transport_err;
3855 break;
3856 case ISCSI_OP_NOOP_OUT:
3857 if (iscsit_handle_nop_out(conn, buffer) < 0)
3858 goto transport_err;
3859 break;
3860 case ISCSI_OP_SCSI_TMFUNC:
3861 if (iscsit_handle_task_mgt_cmd(conn, buffer) < 0)
3862 goto transport_err;
3863 break;
3864 case ISCSI_OP_TEXT:
3865 if (iscsit_handle_text_cmd(conn, buffer) < 0)
3866 goto transport_err;
3867 break;
3868 case ISCSI_OP_LOGOUT:
3869 ret = iscsit_handle_logout_cmd(conn, buffer);
3870 if (ret > 0) {
3871 wait_for_completion_timeout(&conn->conn_logout_comp,
3872 SECONDS_FOR_LOGOUT_COMP * HZ);
3873 goto transport_err;
3874 } else if (ret < 0)
3875 goto transport_err;
3876 break;
3877 case ISCSI_OP_SNACK:
3878 if (iscsit_handle_snack(conn, buffer) < 0)
3879 goto transport_err;
3880 break;
3881 default:
3882 pr_err("Got unknown iSCSI OpCode: 0x%02x\n",
3883 opcode);
3884 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3885 pr_err("Cannot recover from unknown"
3886 " opcode while ERL=0, closing iSCSI connection"
3887 ".\n");
3888 goto transport_err;
3889 }
3890 if (!conn->conn_ops->OFMarker) {
3891 pr_err("Unable to recover from unknown"
3892 " opcode while OFMarker=No, closing iSCSI"
3893 " connection.\n");
3894 goto transport_err;
3895 }
3896 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
3897 pr_err("Unable to recover from unknown"
3898 " opcode, closing iSCSI connection.\n");
3899 goto transport_err;
3900 }
3901 break;
3902 }
3903 }
3904
3905transport_err:
3906 if (!signal_pending(current))
3907 atomic_set(&conn->transport_failed, 1);
3908 iscsit_take_action_for_connection_exit(conn);
3909 goto restart;
3910out:
3911 return 0;
3912}
3913
3914static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
3915{
3916 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
3917 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003918 /*
3919 * We expect this function to only ever be called from either RX or TX
3920 * thread context via iscsit_close_connection() once the other context
3921 * has been reset -> returned sleeping pre-handler state.
3922 */
3923 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003924 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003925
Andy Grover2fbb4712012-04-03 15:51:01 -07003926 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003927 spin_unlock_bh(&conn->cmd_lock);
3928
3929 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003930
Nicholas Bellingerd2701902011-10-09 01:48:14 -07003931 iscsit_free_cmd(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003932
3933 spin_lock_bh(&conn->cmd_lock);
3934 }
3935 spin_unlock_bh(&conn->cmd_lock);
3936}
3937
3938static void iscsit_stop_timers_for_cmds(
3939 struct iscsi_conn *conn)
3940{
3941 struct iscsi_cmd *cmd;
3942
3943 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003944 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003945 if (cmd->data_direction == DMA_TO_DEVICE)
3946 iscsit_stop_dataout_timer(cmd);
3947 }
3948 spin_unlock_bh(&conn->cmd_lock);
3949}
3950
3951int iscsit_close_connection(
3952 struct iscsi_conn *conn)
3953{
3954 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
3955 struct iscsi_session *sess = conn->sess;
3956
3957 pr_debug("Closing iSCSI connection CID %hu on SID:"
3958 " %u\n", conn->cid, sess->sid);
3959 /*
3960 * Always up conn_logout_comp just in case the RX Thread is sleeping
3961 * and the logout response never got sent because the connection
3962 * failed.
3963 */
3964 complete(&conn->conn_logout_comp);
3965
3966 iscsi_release_thread_set(conn);
3967
3968 iscsit_stop_timers_for_cmds(conn);
3969 iscsit_stop_nopin_response_timer(conn);
3970 iscsit_stop_nopin_timer(conn);
3971 iscsit_free_queue_reqs_for_conn(conn);
3972
3973 /*
3974 * During Connection recovery drop unacknowledged out of order
3975 * commands for this connection, and prepare the other commands
3976 * for realligence.
3977 *
3978 * During normal operation clear the out of order commands (but
3979 * do not free the struct iscsi_ooo_cmdsn's) and release all
3980 * struct iscsi_cmds.
3981 */
3982 if (atomic_read(&conn->connection_recovery)) {
3983 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
3984 iscsit_prepare_cmds_for_realligance(conn);
3985 } else {
3986 iscsit_clear_ooo_cmdsns_for_conn(conn);
3987 iscsit_release_commands_from_conn(conn);
3988 }
3989
3990 /*
3991 * Handle decrementing session or connection usage count if
3992 * a logout response was not able to be sent because the
3993 * connection failed. Fall back to Session Recovery here.
3994 */
3995 if (atomic_read(&conn->conn_logout_remove)) {
3996 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
3997 iscsit_dec_conn_usage_count(conn);
3998 iscsit_dec_session_usage_count(sess);
3999 }
4000 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4001 iscsit_dec_conn_usage_count(conn);
4002
4003 atomic_set(&conn->conn_logout_remove, 0);
4004 atomic_set(&sess->session_reinstatement, 0);
4005 atomic_set(&sess->session_fall_back_to_erl0, 1);
4006 }
4007
4008 spin_lock_bh(&sess->conn_lock);
4009 list_del(&conn->conn_list);
4010
4011 /*
4012 * Attempt to let the Initiator know this connection failed by
4013 * sending an Connection Dropped Async Message on another
4014 * active connection.
4015 */
4016 if (atomic_read(&conn->connection_recovery))
4017 iscsit_build_conn_drop_async_message(conn);
4018
4019 spin_unlock_bh(&sess->conn_lock);
4020
4021 /*
4022 * If connection reinstatement is being performed on this connection,
4023 * up the connection reinstatement semaphore that is being blocked on
4024 * in iscsit_cause_connection_reinstatement().
4025 */
4026 spin_lock_bh(&conn->state_lock);
4027 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4028 spin_unlock_bh(&conn->state_lock);
4029 complete(&conn->conn_wait_comp);
4030 wait_for_completion(&conn->conn_post_wait_comp);
4031 spin_lock_bh(&conn->state_lock);
4032 }
4033
4034 /*
4035 * If connection reinstatement is being performed on this connection
4036 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4037 * connection wait rcfr semaphore that is being blocked on
4038 * an iscsit_connection_reinstatement_rcfr().
4039 */
4040 if (atomic_read(&conn->connection_wait_rcfr)) {
4041 spin_unlock_bh(&conn->state_lock);
4042 complete(&conn->conn_wait_rcfr_comp);
4043 wait_for_completion(&conn->conn_post_wait_comp);
4044 spin_lock_bh(&conn->state_lock);
4045 }
4046 atomic_set(&conn->connection_reinstatement, 1);
4047 spin_unlock_bh(&conn->state_lock);
4048
4049 /*
4050 * If any other processes are accessing this connection pointer we
4051 * must wait until they have completed.
4052 */
4053 iscsit_check_conn_usage_count(conn);
4054
4055 if (conn->conn_rx_hash.tfm)
4056 crypto_free_hash(conn->conn_rx_hash.tfm);
4057 if (conn->conn_tx_hash.tfm)
4058 crypto_free_hash(conn->conn_tx_hash.tfm);
4059
4060 if (conn->conn_cpumask)
4061 free_cpumask_var(conn->conn_cpumask);
4062
4063 kfree(conn->conn_ops);
4064 conn->conn_ops = NULL;
4065
Al Virobf6932f2012-07-21 08:55:18 +01004066 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004067 sock_release(conn->sock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004068 conn->thread_set = NULL;
4069
4070 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4071 conn->conn_state = TARG_CONN_STATE_FREE;
4072 kfree(conn);
4073
4074 spin_lock_bh(&sess->conn_lock);
4075 atomic_dec(&sess->nconn);
4076 pr_debug("Decremented iSCSI connection count to %hu from node:"
4077 " %s\n", atomic_read(&sess->nconn),
4078 sess->sess_ops->InitiatorName);
4079 /*
4080 * Make sure that if one connection fails in an non ERL=2 iSCSI
4081 * Session that they all fail.
4082 */
4083 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4084 !atomic_read(&sess->session_logout))
4085 atomic_set(&sess->session_fall_back_to_erl0, 1);
4086
4087 /*
4088 * If this was not the last connection in the session, and we are
4089 * performing session reinstatement or falling back to ERL=0, call
4090 * iscsit_stop_session() without sleeping to shutdown the other
4091 * active connections.
4092 */
4093 if (atomic_read(&sess->nconn)) {
4094 if (!atomic_read(&sess->session_reinstatement) &&
4095 !atomic_read(&sess->session_fall_back_to_erl0)) {
4096 spin_unlock_bh(&sess->conn_lock);
4097 return 0;
4098 }
4099 if (!atomic_read(&sess->session_stop_active)) {
4100 atomic_set(&sess->session_stop_active, 1);
4101 spin_unlock_bh(&sess->conn_lock);
4102 iscsit_stop_session(sess, 0, 0);
4103 return 0;
4104 }
4105 spin_unlock_bh(&sess->conn_lock);
4106 return 0;
4107 }
4108
4109 /*
4110 * If this was the last connection in the session and one of the
4111 * following is occurring:
4112 *
4113 * Session Reinstatement is not being performed, and are falling back
4114 * to ERL=0 call iscsit_close_session().
4115 *
4116 * Session Logout was requested. iscsit_close_session() will be called
4117 * elsewhere.
4118 *
4119 * Session Continuation is not being performed, start the Time2Retain
4120 * handler and check if sleep_on_sess_wait_sem is active.
4121 */
4122 if (!atomic_read(&sess->session_reinstatement) &&
4123 atomic_read(&sess->session_fall_back_to_erl0)) {
4124 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004125 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004126
4127 return 0;
4128 } else if (atomic_read(&sess->session_logout)) {
4129 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4130 sess->session_state = TARG_SESS_STATE_FREE;
4131 spin_unlock_bh(&sess->conn_lock);
4132
4133 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4134 complete(&sess->session_wait_comp);
4135
4136 return 0;
4137 } else {
4138 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4139 sess->session_state = TARG_SESS_STATE_FAILED;
4140
4141 if (!atomic_read(&sess->session_continuation)) {
4142 spin_unlock_bh(&sess->conn_lock);
4143 iscsit_start_time2retain_handler(sess);
4144 } else
4145 spin_unlock_bh(&sess->conn_lock);
4146
4147 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4148 complete(&sess->session_wait_comp);
4149
4150 return 0;
4151 }
4152 spin_unlock_bh(&sess->conn_lock);
4153
4154 return 0;
4155}
4156
4157int iscsit_close_session(struct iscsi_session *sess)
4158{
4159 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4160 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4161
4162 if (atomic_read(&sess->nconn)) {
4163 pr_err("%d connection(s) still exist for iSCSI session"
4164 " to %s\n", atomic_read(&sess->nconn),
4165 sess->sess_ops->InitiatorName);
4166 BUG();
4167 }
4168
4169 spin_lock_bh(&se_tpg->session_lock);
4170 atomic_set(&sess->session_logout, 1);
4171 atomic_set(&sess->session_reinstatement, 1);
4172 iscsit_stop_time2retain_timer(sess);
4173 spin_unlock_bh(&se_tpg->session_lock);
4174
4175 /*
4176 * transport_deregister_session_configfs() will clear the
4177 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4178 * can be setting it again with __transport_register_session() in
4179 * iscsi_post_login_handler() again after the iscsit_stop_session()
4180 * completes in iscsi_np context.
4181 */
4182 transport_deregister_session_configfs(sess->se_sess);
4183
4184 /*
4185 * If any other processes are accessing this session pointer we must
4186 * wait until they have completed. If we are in an interrupt (the
4187 * time2retain handler) and contain and active session usage count we
4188 * restart the timer and exit.
4189 */
4190 if (!in_interrupt()) {
4191 if (iscsit_check_session_usage_count(sess) == 1)
4192 iscsit_stop_session(sess, 1, 1);
4193 } else {
4194 if (iscsit_check_session_usage_count(sess) == 2) {
4195 atomic_set(&sess->session_logout, 0);
4196 iscsit_start_time2retain_handler(sess);
4197 return 0;
4198 }
4199 }
4200
4201 transport_deregister_session(sess->se_sess);
4202
4203 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4204 iscsit_free_connection_recovery_entires(sess);
4205
4206 iscsit_free_all_ooo_cmdsns(sess);
4207
4208 spin_lock_bh(&se_tpg->session_lock);
4209 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4210 sess->session_state = TARG_SESS_STATE_FREE;
4211 pr_debug("Released iSCSI session from node: %s\n",
4212 sess->sess_ops->InitiatorName);
4213 tpg->nsessions--;
4214 if (tpg->tpg_tiqn)
4215 tpg->tpg_tiqn->tiqn_nsessions--;
4216
4217 pr_debug("Decremented number of active iSCSI Sessions on"
4218 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4219
4220 spin_lock(&sess_idr_lock);
4221 idr_remove(&sess_idr, sess->session_index);
4222 spin_unlock(&sess_idr_lock);
4223
4224 kfree(sess->sess_ops);
4225 sess->sess_ops = NULL;
4226 spin_unlock_bh(&se_tpg->session_lock);
4227
4228 kfree(sess);
4229 return 0;
4230}
4231
4232static void iscsit_logout_post_handler_closesession(
4233 struct iscsi_conn *conn)
4234{
4235 struct iscsi_session *sess = conn->sess;
4236
4237 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4238 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4239
4240 atomic_set(&conn->conn_logout_remove, 0);
4241 complete(&conn->conn_logout_comp);
4242
4243 iscsit_dec_conn_usage_count(conn);
4244 iscsit_stop_session(sess, 1, 1);
4245 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004246 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004247}
4248
4249static void iscsit_logout_post_handler_samecid(
4250 struct iscsi_conn *conn)
4251{
4252 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4253 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4254
4255 atomic_set(&conn->conn_logout_remove, 0);
4256 complete(&conn->conn_logout_comp);
4257
4258 iscsit_cause_connection_reinstatement(conn, 1);
4259 iscsit_dec_conn_usage_count(conn);
4260}
4261
4262static void iscsit_logout_post_handler_diffcid(
4263 struct iscsi_conn *conn,
4264 u16 cid)
4265{
4266 struct iscsi_conn *l_conn;
4267 struct iscsi_session *sess = conn->sess;
4268
4269 if (!sess)
4270 return;
4271
4272 spin_lock_bh(&sess->conn_lock);
4273 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4274 if (l_conn->cid == cid) {
4275 iscsit_inc_conn_usage_count(l_conn);
4276 break;
4277 }
4278 }
4279 spin_unlock_bh(&sess->conn_lock);
4280
4281 if (!l_conn)
4282 return;
4283
4284 if (l_conn->sock)
4285 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4286
4287 spin_lock_bh(&l_conn->state_lock);
4288 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4289 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4290 spin_unlock_bh(&l_conn->state_lock);
4291
4292 iscsit_cause_connection_reinstatement(l_conn, 1);
4293 iscsit_dec_conn_usage_count(l_conn);
4294}
4295
4296/*
4297 * Return of 0 causes the TX thread to restart.
4298 */
4299static int iscsit_logout_post_handler(
4300 struct iscsi_cmd *cmd,
4301 struct iscsi_conn *conn)
4302{
4303 int ret = 0;
4304
4305 switch (cmd->logout_reason) {
4306 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4307 switch (cmd->logout_response) {
4308 case ISCSI_LOGOUT_SUCCESS:
4309 case ISCSI_LOGOUT_CLEANUP_FAILED:
4310 default:
4311 iscsit_logout_post_handler_closesession(conn);
4312 break;
4313 }
4314 ret = 0;
4315 break;
4316 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4317 if (conn->cid == cmd->logout_cid) {
4318 switch (cmd->logout_response) {
4319 case ISCSI_LOGOUT_SUCCESS:
4320 case ISCSI_LOGOUT_CLEANUP_FAILED:
4321 default:
4322 iscsit_logout_post_handler_samecid(conn);
4323 break;
4324 }
4325 ret = 0;
4326 } else {
4327 switch (cmd->logout_response) {
4328 case ISCSI_LOGOUT_SUCCESS:
4329 iscsit_logout_post_handler_diffcid(conn,
4330 cmd->logout_cid);
4331 break;
4332 case ISCSI_LOGOUT_CID_NOT_FOUND:
4333 case ISCSI_LOGOUT_CLEANUP_FAILED:
4334 default:
4335 break;
4336 }
4337 ret = 1;
4338 }
4339 break;
4340 case ISCSI_LOGOUT_REASON_RECOVERY:
4341 switch (cmd->logout_response) {
4342 case ISCSI_LOGOUT_SUCCESS:
4343 case ISCSI_LOGOUT_CID_NOT_FOUND:
4344 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4345 case ISCSI_LOGOUT_CLEANUP_FAILED:
4346 default:
4347 break;
4348 }
4349 ret = 1;
4350 break;
4351 default:
4352 break;
4353
4354 }
4355 return ret;
4356}
4357
4358void iscsit_fail_session(struct iscsi_session *sess)
4359{
4360 struct iscsi_conn *conn;
4361
4362 spin_lock_bh(&sess->conn_lock);
4363 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4364 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4365 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4366 }
4367 spin_unlock_bh(&sess->conn_lock);
4368
4369 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4370 sess->session_state = TARG_SESS_STATE_FAILED;
4371}
4372
4373int iscsit_free_session(struct iscsi_session *sess)
4374{
4375 u16 conn_count = atomic_read(&sess->nconn);
4376 struct iscsi_conn *conn, *conn_tmp = NULL;
4377 int is_last;
4378
4379 spin_lock_bh(&sess->conn_lock);
4380 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4381
4382 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4383 conn_list) {
4384 if (conn_count == 0)
4385 break;
4386
4387 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4388 is_last = 1;
4389 } else {
4390 iscsit_inc_conn_usage_count(conn_tmp);
4391 is_last = 0;
4392 }
4393 iscsit_inc_conn_usage_count(conn);
4394
4395 spin_unlock_bh(&sess->conn_lock);
4396 iscsit_cause_connection_reinstatement(conn, 1);
4397 spin_lock_bh(&sess->conn_lock);
4398
4399 iscsit_dec_conn_usage_count(conn);
4400 if (is_last == 0)
4401 iscsit_dec_conn_usage_count(conn_tmp);
4402
4403 conn_count--;
4404 }
4405
4406 if (atomic_read(&sess->nconn)) {
4407 spin_unlock_bh(&sess->conn_lock);
4408 wait_for_completion(&sess->session_wait_comp);
4409 } else
4410 spin_unlock_bh(&sess->conn_lock);
4411
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004412 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004413 return 0;
4414}
4415
4416void iscsit_stop_session(
4417 struct iscsi_session *sess,
4418 int session_sleep,
4419 int connection_sleep)
4420{
4421 u16 conn_count = atomic_read(&sess->nconn);
4422 struct iscsi_conn *conn, *conn_tmp = NULL;
4423 int is_last;
4424
4425 spin_lock_bh(&sess->conn_lock);
4426 if (session_sleep)
4427 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4428
4429 if (connection_sleep) {
4430 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4431 conn_list) {
4432 if (conn_count == 0)
4433 break;
4434
4435 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4436 is_last = 1;
4437 } else {
4438 iscsit_inc_conn_usage_count(conn_tmp);
4439 is_last = 0;
4440 }
4441 iscsit_inc_conn_usage_count(conn);
4442
4443 spin_unlock_bh(&sess->conn_lock);
4444 iscsit_cause_connection_reinstatement(conn, 1);
4445 spin_lock_bh(&sess->conn_lock);
4446
4447 iscsit_dec_conn_usage_count(conn);
4448 if (is_last == 0)
4449 iscsit_dec_conn_usage_count(conn_tmp);
4450 conn_count--;
4451 }
4452 } else {
4453 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4454 iscsit_cause_connection_reinstatement(conn, 0);
4455 }
4456
4457 if (session_sleep && atomic_read(&sess->nconn)) {
4458 spin_unlock_bh(&sess->conn_lock);
4459 wait_for_completion(&sess->session_wait_comp);
4460 } else
4461 spin_unlock_bh(&sess->conn_lock);
4462}
4463
4464int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4465{
4466 struct iscsi_session *sess;
4467 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4468 struct se_session *se_sess, *se_sess_tmp;
4469 int session_count = 0;
4470
4471 spin_lock_bh(&se_tpg->session_lock);
4472 if (tpg->nsessions && !force) {
4473 spin_unlock_bh(&se_tpg->session_lock);
4474 return -1;
4475 }
4476
4477 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4478 sess_list) {
4479 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4480
4481 spin_lock(&sess->conn_lock);
4482 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4483 atomic_read(&sess->session_logout) ||
4484 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4485 spin_unlock(&sess->conn_lock);
4486 continue;
4487 }
4488 atomic_set(&sess->session_reinstatement, 1);
4489 spin_unlock(&sess->conn_lock);
4490 spin_unlock_bh(&se_tpg->session_lock);
4491
4492 iscsit_free_session(sess);
4493 spin_lock_bh(&se_tpg->session_lock);
4494
4495 session_count++;
4496 }
4497 spin_unlock_bh(&se_tpg->session_lock);
4498
4499 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4500 " Group: %hu\n", session_count, tpg->tpgt);
4501 return 0;
4502}
4503
4504MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4505MODULE_VERSION("4.1.x");
4506MODULE_AUTHOR("nab@Linux-iSCSI.org");
4507MODULE_LICENSE("GPL");
4508
4509module_init(iscsi_target_init_module);
4510module_exit(iscsi_target_cleanup_module);