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