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