blob: d68363a79db7fe8b47586d4ea70fddf0e16c7b33 [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
Andy Grover8b1e1242012-04-03 15:51:12 -0700596static int iscsit_add_reject(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000597 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);
Andy Grover2fbb4712012-04-03 15:51:01 -0700625 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000626 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);
Andy Grover2fbb4712012-04-03 15:51:01 -0700672 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000673 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);
Andy Grover2fbb4712012-04-03 15:51:01 -0700747 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000748 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 Grovera12f41f2012-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);
Andy Grover2fbb4712012-04-03 15:51:01 -07001043 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001044 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 Hellwig7d680f3b2011-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);
Andy Grover8b1e1242012-04-03 15:51:12 -07001440 iscsit_build_r2ts_for_cmd(cmd, conn, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001441 } 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);
Andy Grover2fbb4712012-04-03 15:51:01 -07001615 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001616 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);
Andy Grover2fbb4712012-04-03 15:51:01 -07001802 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001803 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);
Andy Grover2fbb4712012-04-03 15:51:01 -07001978 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001979 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);
Andy Grover2fbb4712012-04-03 15:51:01 -07002166 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002167 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 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002176 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002177 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);
Andy Grover2fbb4712012-04-03 15:51:01 -07002379 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002380 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
Andy Grover6f3c0e62012-04-03 15:51:09 -07002432static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2433{
2434 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2435 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2436 wait_for_completion_interruptible_timeout(
2437 &conn->tx_half_close_comp,
2438 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2439 }
2440}
2441
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002442static int iscsit_send_data_in(
2443 struct iscsi_cmd *cmd,
Andy Grover6f3c0e62012-04-03 15:51:09 -07002444 struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002445{
2446 int iov_ret = 0, set_statsn = 0;
2447 u32 iov_count = 0, tx_size = 0;
2448 struct iscsi_datain datain;
2449 struct iscsi_datain_req *dr;
2450 struct iscsi_data_rsp *hdr;
2451 struct kvec *iov;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002452 int eodr = 0;
2453 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002454
2455 memset(&datain, 0, sizeof(struct iscsi_datain));
2456 dr = iscsit_get_datain_values(cmd, &datain);
2457 if (!dr) {
2458 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2459 cmd->init_task_tag);
2460 return -1;
2461 }
2462
2463 /*
2464 * Be paranoid and double check the logic for now.
2465 */
2466 if ((datain.offset + datain.length) > cmd->data_length) {
2467 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2468 " datain.length: %u exceeds cmd->data_length: %u\n",
2469 cmd->init_task_tag, datain.offset, datain.length,
2470 cmd->data_length);
2471 return -1;
2472 }
2473
2474 spin_lock_bh(&conn->sess->session_stats_lock);
2475 conn->sess->tx_data_octets += datain.length;
2476 if (conn->sess->se_sess->se_node_acl) {
2477 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2478 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2479 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2480 }
2481 spin_unlock_bh(&conn->sess->session_stats_lock);
2482 /*
2483 * Special case for successfully execution w/ both DATAIN
2484 * and Sense Data.
2485 */
2486 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2487 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2488 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2489 else {
2490 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2491 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2492 iscsit_increment_maxcmdsn(cmd, conn->sess);
2493 cmd->stat_sn = conn->stat_sn++;
2494 set_statsn = 1;
2495 } else if (dr->dr_complete ==
2496 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2497 set_statsn = 1;
2498 }
2499
2500 hdr = (struct iscsi_data_rsp *) cmd->pdu;
2501 memset(hdr, 0, ISCSI_HDR_LEN);
2502 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2503 hdr->flags = datain.flags;
2504 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2505 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2506 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002507 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002508 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2509 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08002510 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002511 }
2512 }
2513 hton24(hdr->dlength, datain.length);
2514 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2515 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2516 (struct scsi_lun *)&hdr->lun);
2517 else
2518 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2519
2520 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2521 hdr->ttt = (hdr->flags & ISCSI_FLAG_DATA_ACK) ?
2522 cpu_to_be32(cmd->targ_xfer_tag) :
2523 0xFFFFFFFF;
2524 hdr->statsn = (set_statsn) ? cpu_to_be32(cmd->stat_sn) :
2525 0xFFFFFFFF;
2526 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2527 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2528 hdr->datasn = cpu_to_be32(datain.data_sn);
2529 hdr->offset = cpu_to_be32(datain.offset);
2530
2531 iov = &cmd->iov_data[0];
2532 iov[iov_count].iov_base = cmd->pdu;
2533 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2534 tx_size += ISCSI_HDR_LEN;
2535
2536 if (conn->conn_ops->HeaderDigest) {
2537 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2538
2539 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2540 (unsigned char *)hdr, ISCSI_HDR_LEN,
2541 0, NULL, (u8 *)header_digest);
2542
2543 iov[0].iov_len += ISCSI_CRC_LEN;
2544 tx_size += ISCSI_CRC_LEN;
2545
2546 pr_debug("Attaching CRC32 HeaderDigest"
2547 " for DataIN PDU 0x%08x\n", *header_digest);
2548 }
2549
2550 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1], datain.offset, datain.length);
2551 if (iov_ret < 0)
2552 return -1;
2553
2554 iov_count += iov_ret;
2555 tx_size += datain.length;
2556
2557 cmd->padding = ((-datain.length) & 3);
2558 if (cmd->padding) {
2559 iov[iov_count].iov_base = cmd->pad_bytes;
2560 iov[iov_count++].iov_len = cmd->padding;
2561 tx_size += cmd->padding;
2562
2563 pr_debug("Attaching %u padding bytes\n",
2564 cmd->padding);
2565 }
2566 if (conn->conn_ops->DataDigest) {
2567 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2568 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2569
2570 iov[iov_count].iov_base = &cmd->data_crc;
2571 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2572 tx_size += ISCSI_CRC_LEN;
2573
2574 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2575 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2576 }
2577
2578 cmd->iov_data_count = iov_count;
2579 cmd->tx_size = tx_size;
2580
2581 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2582 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2583 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2584 ntohl(hdr->offset), datain.length, conn->cid);
2585
Andy Grover6f3c0e62012-04-03 15:51:09 -07002586 /* sendpage is preferred but can't insert markers */
2587 if (!conn->conn_ops->IFMarker)
2588 ret = iscsit_fe_sendpage_sg(cmd, conn);
2589 else
2590 ret = iscsit_send_tx_data(cmd, conn, 0);
2591
2592 iscsit_unmap_iovec(cmd);
2593
2594 if (ret < 0) {
2595 iscsit_tx_thread_wait_for_tcp(conn);
2596 return ret;
2597 }
2598
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002599 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002600 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002601 2 : 1;
2602 iscsit_free_datain_req(cmd, dr);
2603 }
2604
Andy Grover6f3c0e62012-04-03 15:51:09 -07002605 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002606}
2607
2608static int iscsit_send_logout_response(
2609 struct iscsi_cmd *cmd,
2610 struct iscsi_conn *conn)
2611{
2612 int niov = 0, tx_size;
2613 struct iscsi_conn *logout_conn = NULL;
2614 struct iscsi_conn_recovery *cr = NULL;
2615 struct iscsi_session *sess = conn->sess;
2616 struct kvec *iov;
2617 struct iscsi_logout_rsp *hdr;
2618 /*
2619 * The actual shutting down of Sessions and/or Connections
2620 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2621 * is done in scsi_logout_post_handler().
2622 */
2623 switch (cmd->logout_reason) {
2624 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2625 pr_debug("iSCSI session logout successful, setting"
2626 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2627 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2628 break;
2629 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2630 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2631 break;
2632 /*
2633 * For CLOSECONNECTION logout requests carrying
2634 * a matching logout CID -> local CID, the reference
2635 * for the local CID will have been incremented in
2636 * iscsi_logout_closeconnection().
2637 *
2638 * For CLOSECONNECTION logout requests carrying
2639 * a different CID than the connection it arrived
2640 * on, the connection responding to cmd->logout_cid
2641 * is stopped in iscsit_logout_post_handler_diffcid().
2642 */
2643
2644 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2645 " successful.\n", cmd->logout_cid, conn->cid);
2646 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2647 break;
2648 case ISCSI_LOGOUT_REASON_RECOVERY:
2649 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2650 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2651 break;
2652 /*
2653 * If the connection is still active from our point of view
2654 * force connection recovery to occur.
2655 */
2656 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2657 cmd->logout_cid);
2658 if ((logout_conn)) {
2659 iscsit_connection_reinstatement_rcfr(logout_conn);
2660 iscsit_dec_conn_usage_count(logout_conn);
2661 }
2662
2663 cr = iscsit_get_inactive_connection_recovery_entry(
2664 conn->sess, cmd->logout_cid);
2665 if (!cr) {
2666 pr_err("Unable to locate CID: %hu for"
2667 " REMOVECONNFORRECOVERY Logout Request.\n",
2668 cmd->logout_cid);
2669 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2670 break;
2671 }
2672
2673 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2674
2675 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2676 " for recovery for CID: %hu on CID: %hu successful.\n",
2677 cmd->logout_cid, conn->cid);
2678 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2679 break;
2680 default:
2681 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2682 cmd->logout_reason);
2683 return -1;
2684 }
2685
2686 tx_size = ISCSI_HDR_LEN;
2687 hdr = (struct iscsi_logout_rsp *)cmd->pdu;
2688 memset(hdr, 0, ISCSI_HDR_LEN);
2689 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2690 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2691 hdr->response = cmd->logout_response;
2692 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2693 cmd->stat_sn = conn->stat_sn++;
2694 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2695
2696 iscsit_increment_maxcmdsn(cmd, conn->sess);
2697 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2698 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2699
2700 iov = &cmd->iov_misc[0];
2701 iov[niov].iov_base = cmd->pdu;
2702 iov[niov++].iov_len = ISCSI_HDR_LEN;
2703
2704 if (conn->conn_ops->HeaderDigest) {
2705 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2706
2707 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2708 (unsigned char *)hdr, ISCSI_HDR_LEN,
2709 0, NULL, (u8 *)header_digest);
2710
2711 iov[0].iov_len += ISCSI_CRC_LEN;
2712 tx_size += ISCSI_CRC_LEN;
2713 pr_debug("Attaching CRC32C HeaderDigest to"
2714 " Logout Response 0x%08x\n", *header_digest);
2715 }
2716 cmd->iov_misc_count = niov;
2717 cmd->tx_size = tx_size;
2718
2719 pr_debug("Sending Logout Response ITT: 0x%08x StatSN:"
2720 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2721 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2722 cmd->logout_cid, conn->cid);
2723
2724 return 0;
2725}
2726
2727/*
2728 * Unsolicited NOPIN, either requesting a response or not.
2729 */
2730static int iscsit_send_unsolicited_nopin(
2731 struct iscsi_cmd *cmd,
2732 struct iscsi_conn *conn,
2733 int want_response)
2734{
2735 int tx_size = ISCSI_HDR_LEN;
2736 struct iscsi_nopin *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002737 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002738
2739 hdr = (struct iscsi_nopin *) cmd->pdu;
2740 memset(hdr, 0, ISCSI_HDR_LEN);
2741 hdr->opcode = ISCSI_OP_NOOP_IN;
2742 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2743 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2744 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2745 cmd->stat_sn = conn->stat_sn;
2746 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2747 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2748 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2749
2750 if (conn->conn_ops->HeaderDigest) {
2751 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2752
2753 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2754 (unsigned char *)hdr, ISCSI_HDR_LEN,
2755 0, NULL, (u8 *)header_digest);
2756
2757 tx_size += ISCSI_CRC_LEN;
2758 pr_debug("Attaching CRC32C HeaderDigest to"
2759 " NopIN 0x%08x\n", *header_digest);
2760 }
2761
2762 cmd->iov_misc[0].iov_base = cmd->pdu;
2763 cmd->iov_misc[0].iov_len = tx_size;
2764 cmd->iov_misc_count = 1;
2765 cmd->tx_size = tx_size;
2766
2767 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2768 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2769
Andy Grover6f3c0e62012-04-03 15:51:09 -07002770 ret = iscsit_send_tx_data(cmd, conn, 1);
2771 if (ret < 0) {
2772 iscsit_tx_thread_wait_for_tcp(conn);
2773 return ret;
2774 }
2775
2776 spin_lock_bh(&cmd->istate_lock);
2777 cmd->i_state = want_response ?
2778 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2779 spin_unlock_bh(&cmd->istate_lock);
2780
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002781 return 0;
2782}
2783
2784static int iscsit_send_nopin_response(
2785 struct iscsi_cmd *cmd,
2786 struct iscsi_conn *conn)
2787{
2788 int niov = 0, tx_size;
2789 u32 padding = 0;
2790 struct kvec *iov;
2791 struct iscsi_nopin *hdr;
2792
2793 tx_size = ISCSI_HDR_LEN;
2794 hdr = (struct iscsi_nopin *) cmd->pdu;
2795 memset(hdr, 0, ISCSI_HDR_LEN);
2796 hdr->opcode = ISCSI_OP_NOOP_IN;
2797 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2798 hton24(hdr->dlength, cmd->buf_ptr_size);
2799 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2800 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2801 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2802 cmd->stat_sn = conn->stat_sn++;
2803 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2804
2805 iscsit_increment_maxcmdsn(cmd, conn->sess);
2806 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2807 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2808
2809 iov = &cmd->iov_misc[0];
2810 iov[niov].iov_base = cmd->pdu;
2811 iov[niov++].iov_len = ISCSI_HDR_LEN;
2812
2813 if (conn->conn_ops->HeaderDigest) {
2814 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2815
2816 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2817 (unsigned char *)hdr, ISCSI_HDR_LEN,
2818 0, NULL, (u8 *)header_digest);
2819
2820 iov[0].iov_len += ISCSI_CRC_LEN;
2821 tx_size += ISCSI_CRC_LEN;
2822 pr_debug("Attaching CRC32C HeaderDigest"
2823 " to NopIn 0x%08x\n", *header_digest);
2824 }
2825
2826 /*
2827 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2828 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2829 */
2830 if (cmd->buf_ptr_size) {
2831 iov[niov].iov_base = cmd->buf_ptr;
2832 iov[niov++].iov_len = cmd->buf_ptr_size;
2833 tx_size += cmd->buf_ptr_size;
2834
2835 pr_debug("Echoing back %u bytes of ping"
2836 " data.\n", cmd->buf_ptr_size);
2837
2838 padding = ((-cmd->buf_ptr_size) & 3);
2839 if (padding != 0) {
2840 iov[niov].iov_base = &cmd->pad_bytes;
2841 iov[niov++].iov_len = padding;
2842 tx_size += padding;
2843 pr_debug("Attaching %u additional"
2844 " padding bytes.\n", padding);
2845 }
2846 if (conn->conn_ops->DataDigest) {
2847 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2848 cmd->buf_ptr, cmd->buf_ptr_size,
2849 padding, (u8 *)&cmd->pad_bytes,
2850 (u8 *)&cmd->data_crc);
2851
2852 iov[niov].iov_base = &cmd->data_crc;
2853 iov[niov++].iov_len = ISCSI_CRC_LEN;
2854 tx_size += ISCSI_CRC_LEN;
2855 pr_debug("Attached DataDigest for %u"
2856 " bytes of ping data, CRC 0x%08x\n",
2857 cmd->buf_ptr_size, cmd->data_crc);
2858 }
2859 }
2860
2861 cmd->iov_misc_count = niov;
2862 cmd->tx_size = tx_size;
2863
2864 pr_debug("Sending NOPIN Response ITT: 0x%08x, TTT:"
2865 " 0x%08x, StatSN: 0x%08x, Length %u\n", cmd->init_task_tag,
2866 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2867
2868 return 0;
2869}
2870
Andy Grover6f3c0e62012-04-03 15:51:09 -07002871static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002872 struct iscsi_cmd *cmd,
2873 struct iscsi_conn *conn)
2874{
2875 int tx_size = 0;
2876 struct iscsi_r2t *r2t;
2877 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002878 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002879
2880 r2t = iscsit_get_r2t_from_list(cmd);
2881 if (!r2t)
2882 return -1;
2883
2884 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
2885 memset(hdr, 0, ISCSI_HDR_LEN);
2886 hdr->opcode = ISCSI_OP_R2T;
2887 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2888 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2889 (struct scsi_lun *)&hdr->lun);
2890 hdr->itt = cpu_to_be32(cmd->init_task_tag);
2891 spin_lock_bh(&conn->sess->ttt_lock);
2892 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2893 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
2894 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
2895 spin_unlock_bh(&conn->sess->ttt_lock);
2896 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
2897 hdr->statsn = cpu_to_be32(conn->stat_sn);
2898 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2899 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2900 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
2901 hdr->data_offset = cpu_to_be32(r2t->offset);
2902 hdr->data_length = cpu_to_be32(r2t->xfer_len);
2903
2904 cmd->iov_misc[0].iov_base = cmd->pdu;
2905 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
2906 tx_size += ISCSI_HDR_LEN;
2907
2908 if (conn->conn_ops->HeaderDigest) {
2909 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2910
2911 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2912 (unsigned char *)hdr, ISCSI_HDR_LEN,
2913 0, NULL, (u8 *)header_digest);
2914
2915 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
2916 tx_size += ISCSI_CRC_LEN;
2917 pr_debug("Attaching CRC32 HeaderDigest for R2T"
2918 " PDU 0x%08x\n", *header_digest);
2919 }
2920
2921 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
2922 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
2923 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
2924 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
2925 r2t->offset, r2t->xfer_len, conn->cid);
2926
2927 cmd->iov_misc_count = 1;
2928 cmd->tx_size = tx_size;
2929
2930 spin_lock_bh(&cmd->r2t_lock);
2931 r2t->sent_r2t = 1;
2932 spin_unlock_bh(&cmd->r2t_lock);
2933
Andy Grover6f3c0e62012-04-03 15:51:09 -07002934 ret = iscsit_send_tx_data(cmd, conn, 1);
2935 if (ret < 0) {
2936 iscsit_tx_thread_wait_for_tcp(conn);
2937 return ret;
2938 }
2939
2940 spin_lock_bh(&cmd->dataout_timeout_lock);
2941 iscsit_start_dataout_timer(cmd, conn);
2942 spin_unlock_bh(&cmd->dataout_timeout_lock);
2943
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002944 return 0;
2945}
2946
2947/*
Andy Grover8b1e1242012-04-03 15:51:12 -07002948 * @recovery: If called from iscsi_task_reassign_complete_write() for
2949 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002950 */
2951int iscsit_build_r2ts_for_cmd(
2952 struct iscsi_cmd *cmd,
2953 struct iscsi_conn *conn,
Andy Grover8b1e1242012-04-03 15:51:12 -07002954 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002955{
2956 int first_r2t = 1;
2957 u32 offset = 0, xfer_len = 0;
2958
2959 spin_lock_bh(&cmd->r2t_lock);
2960 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
2961 spin_unlock_bh(&cmd->r2t_lock);
2962 return 0;
2963 }
2964
Andy Grover8b1e1242012-04-03 15:51:12 -07002965 if (conn->sess->sess_ops->DataSequenceInOrder &&
2966 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07002967 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002968
2969 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
2970 if (conn->sess->sess_ops->DataSequenceInOrder) {
2971 offset = cmd->r2t_offset;
2972
Andy Grover8b1e1242012-04-03 15:51:12 -07002973 if (first_r2t && recovery) {
2974 int new_data_end = offset +
2975 conn->sess->sess_ops->MaxBurstLength -
2976 cmd->next_burst_len;
2977
2978 if (new_data_end > cmd->data_length)
2979 xfer_len = cmd->data_length - offset;
2980 else
2981 xfer_len =
2982 conn->sess->sess_ops->MaxBurstLength -
2983 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002984 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07002985 int new_data_end = offset +
2986 conn->sess->sess_ops->MaxBurstLength;
2987
2988 if (new_data_end > cmd->data_length)
2989 xfer_len = cmd->data_length - offset;
2990 else
2991 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002992 }
2993 cmd->r2t_offset += xfer_len;
2994
2995 if (cmd->r2t_offset == cmd->data_length)
2996 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
2997 } else {
2998 struct iscsi_seq *seq;
2999
3000 seq = iscsit_get_seq_holder_for_r2t(cmd);
3001 if (!seq) {
3002 spin_unlock_bh(&cmd->r2t_lock);
3003 return -1;
3004 }
3005
3006 offset = seq->offset;
3007 xfer_len = seq->xfer_len;
3008
3009 if (cmd->seq_send_order == cmd->seq_count)
3010 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3011 }
3012 cmd->outstanding_r2ts++;
3013 first_r2t = 0;
3014
3015 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3016 spin_unlock_bh(&cmd->r2t_lock);
3017 return -1;
3018 }
3019
3020 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3021 break;
3022 }
3023 spin_unlock_bh(&cmd->r2t_lock);
3024
3025 return 0;
3026}
3027
3028static int iscsit_send_status(
3029 struct iscsi_cmd *cmd,
3030 struct iscsi_conn *conn)
3031{
3032 u8 iov_count = 0, recovery;
3033 u32 padding = 0, tx_size = 0;
3034 struct iscsi_scsi_rsp *hdr;
3035 struct kvec *iov;
3036
3037 recovery = (cmd->i_state != ISTATE_SEND_STATUS);
3038 if (!recovery)
3039 cmd->stat_sn = conn->stat_sn++;
3040
3041 spin_lock_bh(&conn->sess->session_stats_lock);
3042 conn->sess->rsp_pdus++;
3043 spin_unlock_bh(&conn->sess->session_stats_lock);
3044
3045 hdr = (struct iscsi_scsi_rsp *) cmd->pdu;
3046 memset(hdr, 0, ISCSI_HDR_LEN);
3047 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3048 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3049 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3050 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003051 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003052 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3053 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003054 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003055 }
3056 hdr->response = cmd->iscsi_response;
3057 hdr->cmd_status = cmd->se_cmd.scsi_status;
3058 hdr->itt = cpu_to_be32(cmd->init_task_tag);
3059 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3060
3061 iscsit_increment_maxcmdsn(cmd, conn->sess);
3062 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3063 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3064
3065 iov = &cmd->iov_misc[0];
3066 iov[iov_count].iov_base = cmd->pdu;
3067 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3068 tx_size += ISCSI_HDR_LEN;
3069
3070 /*
3071 * Attach SENSE DATA payload to iSCSI Response PDU
3072 */
3073 if (cmd->se_cmd.sense_buffer &&
3074 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3075 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
3076 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
3077 hton24(hdr->dlength, cmd->se_cmd.scsi_sense_length);
3078 iov[iov_count].iov_base = cmd->se_cmd.sense_buffer;
3079 iov[iov_count++].iov_len =
3080 (cmd->se_cmd.scsi_sense_length + padding);
3081 tx_size += cmd->se_cmd.scsi_sense_length;
3082
3083 if (padding) {
3084 memset(cmd->se_cmd.sense_buffer +
3085 cmd->se_cmd.scsi_sense_length, 0, padding);
3086 tx_size += padding;
3087 pr_debug("Adding %u bytes of padding to"
3088 " SENSE.\n", padding);
3089 }
3090
3091 if (conn->conn_ops->DataDigest) {
3092 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3093 cmd->se_cmd.sense_buffer,
3094 (cmd->se_cmd.scsi_sense_length + padding),
3095 0, NULL, (u8 *)&cmd->data_crc);
3096
3097 iov[iov_count].iov_base = &cmd->data_crc;
3098 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3099 tx_size += ISCSI_CRC_LEN;
3100
3101 pr_debug("Attaching CRC32 DataDigest for"
3102 " SENSE, %u bytes CRC 0x%08x\n",
3103 (cmd->se_cmd.scsi_sense_length + padding),
3104 cmd->data_crc);
3105 }
3106
3107 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3108 " Response PDU\n",
3109 cmd->se_cmd.scsi_sense_length);
3110 }
3111
3112 if (conn->conn_ops->HeaderDigest) {
3113 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3114
3115 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3116 (unsigned char *)hdr, ISCSI_HDR_LEN,
3117 0, NULL, (u8 *)header_digest);
3118
3119 iov[0].iov_len += ISCSI_CRC_LEN;
3120 tx_size += ISCSI_CRC_LEN;
3121 pr_debug("Attaching CRC32 HeaderDigest for Response"
3122 " PDU 0x%08x\n", *header_digest);
3123 }
3124
3125 cmd->iov_misc_count = iov_count;
3126 cmd->tx_size = tx_size;
3127
3128 pr_debug("Built %sSCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3129 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3130 (!recovery) ? "" : "Recovery ", cmd->init_task_tag,
3131 cmd->stat_sn, 0x00, cmd->se_cmd.scsi_status, conn->cid);
3132
3133 return 0;
3134}
3135
3136static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3137{
3138 switch (se_tmr->response) {
3139 case TMR_FUNCTION_COMPLETE:
3140 return ISCSI_TMF_RSP_COMPLETE;
3141 case TMR_TASK_DOES_NOT_EXIST:
3142 return ISCSI_TMF_RSP_NO_TASK;
3143 case TMR_LUN_DOES_NOT_EXIST:
3144 return ISCSI_TMF_RSP_NO_LUN;
3145 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3146 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3147 case TMR_FUNCTION_AUTHORIZATION_FAILED:
3148 return ISCSI_TMF_RSP_AUTH_FAILED;
3149 case TMR_FUNCTION_REJECTED:
3150 default:
3151 return ISCSI_TMF_RSP_REJECTED;
3152 }
3153}
3154
3155static int iscsit_send_task_mgt_rsp(
3156 struct iscsi_cmd *cmd,
3157 struct iscsi_conn *conn)
3158{
3159 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3160 struct iscsi_tm_rsp *hdr;
3161 u32 tx_size = 0;
3162
3163 hdr = (struct iscsi_tm_rsp *) cmd->pdu;
3164 memset(hdr, 0, ISCSI_HDR_LEN);
3165 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003166 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003167 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
3168 hdr->itt = cpu_to_be32(cmd->init_task_tag);
3169 cmd->stat_sn = conn->stat_sn++;
3170 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3171
3172 iscsit_increment_maxcmdsn(cmd, conn->sess);
3173 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3174 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3175
3176 cmd->iov_misc[0].iov_base = cmd->pdu;
3177 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3178 tx_size += ISCSI_HDR_LEN;
3179
3180 if (conn->conn_ops->HeaderDigest) {
3181 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3182
3183 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3184 (unsigned char *)hdr, ISCSI_HDR_LEN,
3185 0, NULL, (u8 *)header_digest);
3186
3187 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3188 tx_size += ISCSI_CRC_LEN;
3189 pr_debug("Attaching CRC32 HeaderDigest for Task"
3190 " Mgmt Response PDU 0x%08x\n", *header_digest);
3191 }
3192
3193 cmd->iov_misc_count = 1;
3194 cmd->tx_size = tx_size;
3195
3196 pr_debug("Built Task Management Response ITT: 0x%08x,"
3197 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3198 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3199
3200 return 0;
3201}
3202
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003203static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3204{
3205 bool ret = false;
3206
3207 if (np->np_sockaddr.ss_family == AF_INET6) {
3208 const struct sockaddr_in6 sin6 = {
3209 .sin6_addr = IN6ADDR_ANY_INIT };
3210 struct sockaddr_in6 *sock_in6 =
3211 (struct sockaddr_in6 *)&np->np_sockaddr;
3212
3213 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3214 sin6.sin6_addr.s6_addr, 16))
3215 ret = true;
3216 } else {
3217 struct sockaddr_in * sock_in =
3218 (struct sockaddr_in *)&np->np_sockaddr;
3219
3220 if (sock_in->sin_addr.s_addr == INADDR_ANY)
3221 ret = true;
3222 }
3223
3224 return ret;
3225}
3226
Andy Grover8b1e1242012-04-03 15:51:12 -07003227#define SENDTARGETS_BUF_LIMIT 32768U
3228
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003229static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3230{
3231 char *payload = NULL;
3232 struct iscsi_conn *conn = cmd->conn;
3233 struct iscsi_portal_group *tpg;
3234 struct iscsi_tiqn *tiqn;
3235 struct iscsi_tpg_np *tpg_np;
3236 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003237 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003238
Andy Grover8b1e1242012-04-03 15:51:12 -07003239 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3240 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003241
3242 payload = kzalloc(buffer_len, GFP_KERNEL);
3243 if (!payload) {
3244 pr_err("Unable to allocate memory for sendtargets"
3245 " response.\n");
3246 return -ENOMEM;
3247 }
3248
3249 spin_lock(&tiqn_lock);
3250 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3251 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3252 len += 1;
3253
3254 if ((len + payload_len) > buffer_len) {
3255 spin_unlock(&tiqn->tiqn_tpg_lock);
3256 end_of_buf = 1;
3257 goto eob;
3258 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003259 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003260 payload_len += len;
3261
3262 spin_lock(&tiqn->tiqn_tpg_lock);
3263 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3264
3265 spin_lock(&tpg->tpg_state_lock);
3266 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3267 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3268 spin_unlock(&tpg->tpg_state_lock);
3269 continue;
3270 }
3271 spin_unlock(&tpg->tpg_state_lock);
3272
3273 spin_lock(&tpg->tpg_np_lock);
3274 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3275 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003276 struct iscsi_np *np = tpg_np->tpg_np;
3277 bool inaddr_any = iscsit_check_inaddr_any(np);
3278
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003279 len = sprintf(buf, "TargetAddress="
3280 "%s%s%s:%hu,%hu",
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003281 (np->np_sockaddr.ss_family == AF_INET6) ?
3282 "[" : "", (inaddr_any == false) ?
3283 np->np_ip : conn->local_ip,
3284 (np->np_sockaddr.ss_family == AF_INET6) ?
3285 "]" : "", (inaddr_any == false) ?
3286 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003287 tpg->tpgt);
3288 len += 1;
3289
3290 if ((len + payload_len) > buffer_len) {
3291 spin_unlock(&tpg->tpg_np_lock);
3292 spin_unlock(&tiqn->tiqn_tpg_lock);
3293 end_of_buf = 1;
3294 goto eob;
3295 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003296 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003297 payload_len += len;
3298 }
3299 spin_unlock(&tpg->tpg_np_lock);
3300 }
3301 spin_unlock(&tiqn->tiqn_tpg_lock);
3302eob:
3303 if (end_of_buf)
3304 break;
3305 }
3306 spin_unlock(&tiqn_lock);
3307
3308 cmd->buf_ptr = payload;
3309
3310 return payload_len;
3311}
3312
3313/*
3314 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3315 * MaxRecvDataSegmentLength.
3316 */
3317static int iscsit_send_text_rsp(
3318 struct iscsi_cmd *cmd,
3319 struct iscsi_conn *conn)
3320{
3321 struct iscsi_text_rsp *hdr;
3322 struct kvec *iov;
3323 u32 padding = 0, tx_size = 0;
3324 int text_length, iov_count = 0;
3325
3326 text_length = iscsit_build_sendtargets_response(cmd);
3327 if (text_length < 0)
3328 return text_length;
3329
3330 padding = ((-text_length) & 3);
3331 if (padding != 0) {
3332 memset(cmd->buf_ptr + text_length, 0, padding);
3333 pr_debug("Attaching %u additional bytes for"
3334 " padding.\n", padding);
3335 }
3336
3337 hdr = (struct iscsi_text_rsp *) cmd->pdu;
3338 memset(hdr, 0, ISCSI_HDR_LEN);
3339 hdr->opcode = ISCSI_OP_TEXT_RSP;
3340 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3341 hton24(hdr->dlength, text_length);
3342 hdr->itt = cpu_to_be32(cmd->init_task_tag);
3343 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3344 cmd->stat_sn = conn->stat_sn++;
3345 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3346
3347 iscsit_increment_maxcmdsn(cmd, conn->sess);
3348 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3349 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3350
3351 iov = &cmd->iov_misc[0];
3352
3353 iov[iov_count].iov_base = cmd->pdu;
3354 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3355 iov[iov_count].iov_base = cmd->buf_ptr;
3356 iov[iov_count++].iov_len = text_length + padding;
3357
3358 tx_size += (ISCSI_HDR_LEN + text_length + padding);
3359
3360 if (conn->conn_ops->HeaderDigest) {
3361 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3362
3363 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3364 (unsigned char *)hdr, ISCSI_HDR_LEN,
3365 0, NULL, (u8 *)header_digest);
3366
3367 iov[0].iov_len += ISCSI_CRC_LEN;
3368 tx_size += ISCSI_CRC_LEN;
3369 pr_debug("Attaching CRC32 HeaderDigest for"
3370 " Text Response PDU 0x%08x\n", *header_digest);
3371 }
3372
3373 if (conn->conn_ops->DataDigest) {
3374 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3375 cmd->buf_ptr, (text_length + padding),
3376 0, NULL, (u8 *)&cmd->data_crc);
3377
3378 iov[iov_count].iov_base = &cmd->data_crc;
3379 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3380 tx_size += ISCSI_CRC_LEN;
3381
3382 pr_debug("Attaching DataDigest for %u bytes of text"
3383 " data, CRC 0x%08x\n", (text_length + padding),
3384 cmd->data_crc);
3385 }
3386
3387 cmd->iov_misc_count = iov_count;
3388 cmd->tx_size = tx_size;
3389
3390 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3391 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3392 text_length, conn->cid);
3393 return 0;
3394}
3395
3396static int iscsit_send_reject(
3397 struct iscsi_cmd *cmd,
3398 struct iscsi_conn *conn)
3399{
3400 u32 iov_count = 0, tx_size = 0;
3401 struct iscsi_reject *hdr;
3402 struct kvec *iov;
3403
3404 hdr = (struct iscsi_reject *) cmd->pdu;
3405 hdr->opcode = ISCSI_OP_REJECT;
3406 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3407 hton24(hdr->dlength, ISCSI_HDR_LEN);
3408 cmd->stat_sn = conn->stat_sn++;
3409 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3410 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3411 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3412
3413 iov = &cmd->iov_misc[0];
3414
3415 iov[iov_count].iov_base = cmd->pdu;
3416 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3417 iov[iov_count].iov_base = cmd->buf_ptr;
3418 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3419
3420 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3421
3422 if (conn->conn_ops->HeaderDigest) {
3423 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3424
3425 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3426 (unsigned char *)hdr, ISCSI_HDR_LEN,
3427 0, NULL, (u8 *)header_digest);
3428
3429 iov[0].iov_len += ISCSI_CRC_LEN;
3430 tx_size += ISCSI_CRC_LEN;
3431 pr_debug("Attaching CRC32 HeaderDigest for"
3432 " REJECT PDU 0x%08x\n", *header_digest);
3433 }
3434
3435 if (conn->conn_ops->DataDigest) {
3436 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3437 (unsigned char *)cmd->buf_ptr, ISCSI_HDR_LEN,
3438 0, NULL, (u8 *)&cmd->data_crc);
3439
3440 iov[iov_count].iov_base = &cmd->data_crc;
3441 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3442 tx_size += ISCSI_CRC_LEN;
3443 pr_debug("Attaching CRC32 DataDigest for REJECT"
3444 " PDU 0x%08x\n", cmd->data_crc);
3445 }
3446
3447 cmd->iov_misc_count = iov_count;
3448 cmd->tx_size = tx_size;
3449
3450 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3451 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3452
3453 return 0;
3454}
3455
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003456void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3457{
3458 struct iscsi_thread_set *ts = conn->thread_set;
3459 int ord, cpu;
3460 /*
3461 * thread_id is assigned from iscsit_global->ts_bitmap from
3462 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3463 *
3464 * Here we use thread_id to determine which CPU that this
3465 * iSCSI connection's iscsi_thread_set will be scheduled to
3466 * execute upon.
3467 */
3468 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003469 for_each_online_cpu(cpu) {
3470 if (ord-- == 0) {
3471 cpumask_set_cpu(cpu, conn->conn_cpumask);
3472 return;
3473 }
3474 }
3475 /*
3476 * This should never be reached..
3477 */
3478 dump_stack();
3479 cpumask_setall(conn->conn_cpumask);
3480}
3481
3482static inline void iscsit_thread_check_cpumask(
3483 struct iscsi_conn *conn,
3484 struct task_struct *p,
3485 int mode)
3486{
3487 char buf[128];
3488 /*
3489 * mode == 1 signals iscsi_target_tx_thread() usage.
3490 * mode == 0 signals iscsi_target_rx_thread() usage.
3491 */
3492 if (mode == 1) {
3493 if (!conn->conn_tx_reset_cpumask)
3494 return;
3495 conn->conn_tx_reset_cpumask = 0;
3496 } else {
3497 if (!conn->conn_rx_reset_cpumask)
3498 return;
3499 conn->conn_rx_reset_cpumask = 0;
3500 }
3501 /*
3502 * Update the CPU mask for this single kthread so that
3503 * both TX and RX kthreads are scheduled to run on the
3504 * same CPU.
3505 */
3506 memset(buf, 0, 128);
3507 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003508 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3509}
3510
Andy Grover6f3c0e62012-04-03 15:51:09 -07003511static int handle_immediate_queue(struct iscsi_conn *conn)
3512{
3513 struct iscsi_queue_req *qr;
3514 struct iscsi_cmd *cmd;
3515 u8 state;
3516 int ret;
3517
3518 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3519 atomic_set(&conn->check_immediate_queue, 0);
3520 cmd = qr->cmd;
3521 state = qr->state;
3522 kmem_cache_free(lio_qr_cache, qr);
3523
3524 switch (state) {
3525 case ISTATE_SEND_R2T:
3526 ret = iscsit_send_r2t(cmd, conn);
3527 if (ret < 0)
3528 goto err;
3529 break;
3530 case ISTATE_REMOVE:
3531 if (cmd->data_direction == DMA_TO_DEVICE)
3532 iscsit_stop_dataout_timer(cmd);
3533
3534 spin_lock_bh(&conn->cmd_lock);
3535 list_del(&cmd->i_conn_node);
3536 spin_unlock_bh(&conn->cmd_lock);
3537
3538 iscsit_free_cmd(cmd);
3539 continue;
3540 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3541 iscsit_mod_nopin_response_timer(conn);
3542 ret = iscsit_send_unsolicited_nopin(cmd,
3543 conn, 1);
3544 if (ret < 0)
3545 goto err;
3546 break;
3547 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3548 ret = iscsit_send_unsolicited_nopin(cmd,
3549 conn, 0);
3550 if (ret < 0)
3551 goto err;
3552 break;
3553 default:
3554 pr_err("Unknown Opcode: 0x%02x ITT:"
3555 " 0x%08x, i_state: %d on CID: %hu\n",
3556 cmd->iscsi_opcode, cmd->init_task_tag, state,
3557 conn->cid);
3558 goto err;
3559 }
3560 }
3561
3562 return 0;
3563
3564err:
3565 return -1;
3566}
3567
3568static int handle_response_queue(struct iscsi_conn *conn)
3569{
3570 struct iscsi_queue_req *qr;
3571 struct iscsi_cmd *cmd;
3572 u8 state;
3573 int ret;
3574
3575 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3576 cmd = qr->cmd;
3577 state = qr->state;
3578 kmem_cache_free(lio_qr_cache, qr);
3579
3580check_rsp_state:
3581 switch (state) {
3582 case ISTATE_SEND_DATAIN:
3583 ret = iscsit_send_data_in(cmd, conn);
3584 if (ret < 0)
3585 goto err;
3586 else if (!ret)
3587 /* more drs */
3588 goto check_rsp_state;
3589 else if (ret == 1) {
3590 /* all done */
3591 spin_lock_bh(&cmd->istate_lock);
3592 cmd->i_state = ISTATE_SENT_STATUS;
3593 spin_unlock_bh(&cmd->istate_lock);
3594 continue;
3595 } else if (ret == 2) {
3596 /* Still must send status,
3597 SCF_TRANSPORT_TASK_SENSE was set */
3598 spin_lock_bh(&cmd->istate_lock);
3599 cmd->i_state = ISTATE_SEND_STATUS;
3600 spin_unlock_bh(&cmd->istate_lock);
3601 state = ISTATE_SEND_STATUS;
3602 goto check_rsp_state;
3603 }
3604
3605 break;
3606 case ISTATE_SEND_STATUS:
3607 case ISTATE_SEND_STATUS_RECOVERY:
3608 ret = iscsit_send_status(cmd, conn);
3609 break;
3610 case ISTATE_SEND_LOGOUTRSP:
3611 ret = iscsit_send_logout_response(cmd, conn);
3612 break;
3613 case ISTATE_SEND_ASYNCMSG:
3614 ret = iscsit_send_conn_drop_async_message(
3615 cmd, conn);
3616 break;
3617 case ISTATE_SEND_NOPIN:
3618 ret = iscsit_send_nopin_response(cmd, conn);
3619 break;
3620 case ISTATE_SEND_REJECT:
3621 ret = iscsit_send_reject(cmd, conn);
3622 break;
3623 case ISTATE_SEND_TASKMGTRSP:
3624 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3625 if (ret != 0)
3626 break;
3627 ret = iscsit_tmr_post_handler(cmd, conn);
3628 if (ret != 0)
3629 iscsit_fall_back_to_erl0(conn->sess);
3630 break;
3631 case ISTATE_SEND_TEXTRSP:
3632 ret = iscsit_send_text_rsp(cmd, conn);
3633 break;
3634 default:
3635 pr_err("Unknown Opcode: 0x%02x ITT:"
3636 " 0x%08x, i_state: %d on CID: %hu\n",
3637 cmd->iscsi_opcode, cmd->init_task_tag,
3638 state, conn->cid);
3639 goto err;
3640 }
3641 if (ret < 0)
3642 goto err;
3643
3644 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3645 iscsit_tx_thread_wait_for_tcp(conn);
3646 iscsit_unmap_iovec(cmd);
3647 goto err;
3648 }
3649 iscsit_unmap_iovec(cmd);
3650
3651 switch (state) {
3652 case ISTATE_SEND_LOGOUTRSP:
3653 if (!iscsit_logout_post_handler(cmd, conn))
3654 goto restart;
3655 /* fall through */
3656 case ISTATE_SEND_STATUS:
3657 case ISTATE_SEND_ASYNCMSG:
3658 case ISTATE_SEND_NOPIN:
3659 case ISTATE_SEND_STATUS_RECOVERY:
3660 case ISTATE_SEND_TEXTRSP:
3661 case ISTATE_SEND_TASKMGTRSP:
3662 spin_lock_bh(&cmd->istate_lock);
3663 cmd->i_state = ISTATE_SENT_STATUS;
3664 spin_unlock_bh(&cmd->istate_lock);
3665 break;
3666 case ISTATE_SEND_REJECT:
3667 if (cmd->cmd_flags & ICF_REJECT_FAIL_CONN) {
3668 cmd->cmd_flags &= ~ICF_REJECT_FAIL_CONN;
3669 complete(&cmd->reject_comp);
3670 goto err;
3671 }
3672 complete(&cmd->reject_comp);
3673 break;
3674 default:
3675 pr_err("Unknown Opcode: 0x%02x ITT:"
3676 " 0x%08x, i_state: %d on CID: %hu\n",
3677 cmd->iscsi_opcode, cmd->init_task_tag,
3678 cmd->i_state, conn->cid);
3679 goto err;
3680 }
3681
3682 if (atomic_read(&conn->check_immediate_queue))
3683 break;
3684 }
3685
3686 return 0;
3687
3688err:
3689 return -1;
3690restart:
3691 return -EAGAIN;
3692}
3693
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003694int iscsi_target_tx_thread(void *arg)
3695{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003696 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003697 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003698 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003699 /*
3700 * Allow ourselves to be interrupted by SIGINT so that a
3701 * connection recovery / failure event can be triggered externally.
3702 */
3703 allow_signal(SIGINT);
3704
3705restart:
3706 conn = iscsi_tx_thread_pre_handler(ts);
3707 if (!conn)
3708 goto out;
3709
Andy Grover6f3c0e62012-04-03 15:51:09 -07003710 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003711
3712 while (!kthread_should_stop()) {
3713 /*
3714 * Ensure that both TX and RX per connection kthreads
3715 * are scheduled to run on the same CPU.
3716 */
3717 iscsit_thread_check_cpumask(conn, current, 1);
3718
3719 schedule_timeout_interruptible(MAX_SCHEDULE_TIMEOUT);
3720
3721 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3722 signal_pending(current))
3723 goto transport_err;
3724
Andy Grover6f3c0e62012-04-03 15:51:09 -07003725 ret = handle_immediate_queue(conn);
3726 if (ret < 0)
3727 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003728
Andy Grover6f3c0e62012-04-03 15:51:09 -07003729 ret = handle_response_queue(conn);
3730 if (ret == -EAGAIN)
3731 goto restart;
3732 else if (ret < 0)
3733 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003734 }
3735
3736transport_err:
3737 iscsit_take_action_for_connection_exit(conn);
3738 goto restart;
3739out:
3740 return 0;
3741}
3742
3743int iscsi_target_rx_thread(void *arg)
3744{
3745 int ret;
3746 u8 buffer[ISCSI_HDR_LEN], opcode;
3747 u32 checksum = 0, digest = 0;
3748 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01003749 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003750 struct kvec iov;
3751 /*
3752 * Allow ourselves to be interrupted by SIGINT so that a
3753 * connection recovery / failure event can be triggered externally.
3754 */
3755 allow_signal(SIGINT);
3756
3757restart:
3758 conn = iscsi_rx_thread_pre_handler(ts);
3759 if (!conn)
3760 goto out;
3761
3762 while (!kthread_should_stop()) {
3763 /*
3764 * Ensure that both TX and RX per connection kthreads
3765 * are scheduled to run on the same CPU.
3766 */
3767 iscsit_thread_check_cpumask(conn, current, 0);
3768
3769 memset(buffer, 0, ISCSI_HDR_LEN);
3770 memset(&iov, 0, sizeof(struct kvec));
3771
3772 iov.iov_base = buffer;
3773 iov.iov_len = ISCSI_HDR_LEN;
3774
3775 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3776 if (ret != ISCSI_HDR_LEN) {
3777 iscsit_rx_thread_wait_for_tcp(conn);
3778 goto transport_err;
3779 }
3780
3781 /*
3782 * Set conn->bad_hdr for use with REJECT PDUs.
3783 */
3784 memcpy(&conn->bad_hdr, &buffer, ISCSI_HDR_LEN);
3785
3786 if (conn->conn_ops->HeaderDigest) {
3787 iov.iov_base = &digest;
3788 iov.iov_len = ISCSI_CRC_LEN;
3789
3790 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3791 if (ret != ISCSI_CRC_LEN) {
3792 iscsit_rx_thread_wait_for_tcp(conn);
3793 goto transport_err;
3794 }
3795
3796 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
3797 buffer, ISCSI_HDR_LEN,
3798 0, NULL, (u8 *)&checksum);
3799
3800 if (digest != checksum) {
3801 pr_err("HeaderDigest CRC32C failed,"
3802 " received 0x%08x, computed 0x%08x\n",
3803 digest, checksum);
3804 /*
3805 * Set the PDU to 0xff so it will intentionally
3806 * hit default in the switch below.
3807 */
3808 memset(buffer, 0xff, ISCSI_HDR_LEN);
3809 spin_lock_bh(&conn->sess->session_stats_lock);
3810 conn->sess->conn_digest_errors++;
3811 spin_unlock_bh(&conn->sess->session_stats_lock);
3812 } else {
3813 pr_debug("Got HeaderDigest CRC32C"
3814 " 0x%08x\n", checksum);
3815 }
3816 }
3817
3818 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
3819 goto transport_err;
3820
3821 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3822
3823 if (conn->sess->sess_ops->SessionType &&
3824 ((!(opcode & ISCSI_OP_TEXT)) ||
3825 (!(opcode & ISCSI_OP_LOGOUT)))) {
3826 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3827 " while in Discovery Session, rejecting.\n", opcode);
3828 iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
3829 buffer, conn);
3830 goto transport_err;
3831 }
3832
3833 switch (opcode) {
3834 case ISCSI_OP_SCSI_CMD:
3835 if (iscsit_handle_scsi_cmd(conn, buffer) < 0)
3836 goto transport_err;
3837 break;
3838 case ISCSI_OP_SCSI_DATA_OUT:
3839 if (iscsit_handle_data_out(conn, buffer) < 0)
3840 goto transport_err;
3841 break;
3842 case ISCSI_OP_NOOP_OUT:
3843 if (iscsit_handle_nop_out(conn, buffer) < 0)
3844 goto transport_err;
3845 break;
3846 case ISCSI_OP_SCSI_TMFUNC:
3847 if (iscsit_handle_task_mgt_cmd(conn, buffer) < 0)
3848 goto transport_err;
3849 break;
3850 case ISCSI_OP_TEXT:
3851 if (iscsit_handle_text_cmd(conn, buffer) < 0)
3852 goto transport_err;
3853 break;
3854 case ISCSI_OP_LOGOUT:
3855 ret = iscsit_handle_logout_cmd(conn, buffer);
3856 if (ret > 0) {
3857 wait_for_completion_timeout(&conn->conn_logout_comp,
3858 SECONDS_FOR_LOGOUT_COMP * HZ);
3859 goto transport_err;
3860 } else if (ret < 0)
3861 goto transport_err;
3862 break;
3863 case ISCSI_OP_SNACK:
3864 if (iscsit_handle_snack(conn, buffer) < 0)
3865 goto transport_err;
3866 break;
3867 default:
3868 pr_err("Got unknown iSCSI OpCode: 0x%02x\n",
3869 opcode);
3870 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3871 pr_err("Cannot recover from unknown"
3872 " opcode while ERL=0, closing iSCSI connection"
3873 ".\n");
3874 goto transport_err;
3875 }
3876 if (!conn->conn_ops->OFMarker) {
3877 pr_err("Unable to recover from unknown"
3878 " opcode while OFMarker=No, closing iSCSI"
3879 " connection.\n");
3880 goto transport_err;
3881 }
3882 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
3883 pr_err("Unable to recover from unknown"
3884 " opcode, closing iSCSI connection.\n");
3885 goto transport_err;
3886 }
3887 break;
3888 }
3889 }
3890
3891transport_err:
3892 if (!signal_pending(current))
3893 atomic_set(&conn->transport_failed, 1);
3894 iscsit_take_action_for_connection_exit(conn);
3895 goto restart;
3896out:
3897 return 0;
3898}
3899
3900static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
3901{
3902 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
3903 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003904 /*
3905 * We expect this function to only ever be called from either RX or TX
3906 * thread context via iscsit_close_connection() once the other context
3907 * has been reset -> returned sleeping pre-handler state.
3908 */
3909 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003910 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003911
Andy Grover2fbb4712012-04-03 15:51:01 -07003912 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003913 spin_unlock_bh(&conn->cmd_lock);
3914
3915 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003916
Nicholas Bellingerd2701902011-10-09 01:48:14 -07003917 iscsit_free_cmd(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003918
3919 spin_lock_bh(&conn->cmd_lock);
3920 }
3921 spin_unlock_bh(&conn->cmd_lock);
3922}
3923
3924static void iscsit_stop_timers_for_cmds(
3925 struct iscsi_conn *conn)
3926{
3927 struct iscsi_cmd *cmd;
3928
3929 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07003930 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003931 if (cmd->data_direction == DMA_TO_DEVICE)
3932 iscsit_stop_dataout_timer(cmd);
3933 }
3934 spin_unlock_bh(&conn->cmd_lock);
3935}
3936
3937int iscsit_close_connection(
3938 struct iscsi_conn *conn)
3939{
3940 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
3941 struct iscsi_session *sess = conn->sess;
3942
3943 pr_debug("Closing iSCSI connection CID %hu on SID:"
3944 " %u\n", conn->cid, sess->sid);
3945 /*
3946 * Always up conn_logout_comp just in case the RX Thread is sleeping
3947 * and the logout response never got sent because the connection
3948 * failed.
3949 */
3950 complete(&conn->conn_logout_comp);
3951
3952 iscsi_release_thread_set(conn);
3953
3954 iscsit_stop_timers_for_cmds(conn);
3955 iscsit_stop_nopin_response_timer(conn);
3956 iscsit_stop_nopin_timer(conn);
3957 iscsit_free_queue_reqs_for_conn(conn);
3958
3959 /*
3960 * During Connection recovery drop unacknowledged out of order
3961 * commands for this connection, and prepare the other commands
3962 * for realligence.
3963 *
3964 * During normal operation clear the out of order commands (but
3965 * do not free the struct iscsi_ooo_cmdsn's) and release all
3966 * struct iscsi_cmds.
3967 */
3968 if (atomic_read(&conn->connection_recovery)) {
3969 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
3970 iscsit_prepare_cmds_for_realligance(conn);
3971 } else {
3972 iscsit_clear_ooo_cmdsns_for_conn(conn);
3973 iscsit_release_commands_from_conn(conn);
3974 }
3975
3976 /*
3977 * Handle decrementing session or connection usage count if
3978 * a logout response was not able to be sent because the
3979 * connection failed. Fall back to Session Recovery here.
3980 */
3981 if (atomic_read(&conn->conn_logout_remove)) {
3982 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
3983 iscsit_dec_conn_usage_count(conn);
3984 iscsit_dec_session_usage_count(sess);
3985 }
3986 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
3987 iscsit_dec_conn_usage_count(conn);
3988
3989 atomic_set(&conn->conn_logout_remove, 0);
3990 atomic_set(&sess->session_reinstatement, 0);
3991 atomic_set(&sess->session_fall_back_to_erl0, 1);
3992 }
3993
3994 spin_lock_bh(&sess->conn_lock);
3995 list_del(&conn->conn_list);
3996
3997 /*
3998 * Attempt to let the Initiator know this connection failed by
3999 * sending an Connection Dropped Async Message on another
4000 * active connection.
4001 */
4002 if (atomic_read(&conn->connection_recovery))
4003 iscsit_build_conn_drop_async_message(conn);
4004
4005 spin_unlock_bh(&sess->conn_lock);
4006
4007 /*
4008 * If connection reinstatement is being performed on this connection,
4009 * up the connection reinstatement semaphore that is being blocked on
4010 * in iscsit_cause_connection_reinstatement().
4011 */
4012 spin_lock_bh(&conn->state_lock);
4013 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4014 spin_unlock_bh(&conn->state_lock);
4015 complete(&conn->conn_wait_comp);
4016 wait_for_completion(&conn->conn_post_wait_comp);
4017 spin_lock_bh(&conn->state_lock);
4018 }
4019
4020 /*
4021 * If connection reinstatement is being performed on this connection
4022 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4023 * connection wait rcfr semaphore that is being blocked on
4024 * an iscsit_connection_reinstatement_rcfr().
4025 */
4026 if (atomic_read(&conn->connection_wait_rcfr)) {
4027 spin_unlock_bh(&conn->state_lock);
4028 complete(&conn->conn_wait_rcfr_comp);
4029 wait_for_completion(&conn->conn_post_wait_comp);
4030 spin_lock_bh(&conn->state_lock);
4031 }
4032 atomic_set(&conn->connection_reinstatement, 1);
4033 spin_unlock_bh(&conn->state_lock);
4034
4035 /*
4036 * If any other processes are accessing this connection pointer we
4037 * must wait until they have completed.
4038 */
4039 iscsit_check_conn_usage_count(conn);
4040
4041 if (conn->conn_rx_hash.tfm)
4042 crypto_free_hash(conn->conn_rx_hash.tfm);
4043 if (conn->conn_tx_hash.tfm)
4044 crypto_free_hash(conn->conn_tx_hash.tfm);
4045
4046 if (conn->conn_cpumask)
4047 free_cpumask_var(conn->conn_cpumask);
4048
4049 kfree(conn->conn_ops);
4050 conn->conn_ops = NULL;
4051
4052 if (conn->sock) {
4053 if (conn->conn_flags & CONNFLAG_SCTP_STRUCT_FILE) {
4054 kfree(conn->sock->file);
4055 conn->sock->file = NULL;
4056 }
4057 sock_release(conn->sock);
4058 }
4059 conn->thread_set = NULL;
4060
4061 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4062 conn->conn_state = TARG_CONN_STATE_FREE;
4063 kfree(conn);
4064
4065 spin_lock_bh(&sess->conn_lock);
4066 atomic_dec(&sess->nconn);
4067 pr_debug("Decremented iSCSI connection count to %hu from node:"
4068 " %s\n", atomic_read(&sess->nconn),
4069 sess->sess_ops->InitiatorName);
4070 /*
4071 * Make sure that if one connection fails in an non ERL=2 iSCSI
4072 * Session that they all fail.
4073 */
4074 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4075 !atomic_read(&sess->session_logout))
4076 atomic_set(&sess->session_fall_back_to_erl0, 1);
4077
4078 /*
4079 * If this was not the last connection in the session, and we are
4080 * performing session reinstatement or falling back to ERL=0, call
4081 * iscsit_stop_session() without sleeping to shutdown the other
4082 * active connections.
4083 */
4084 if (atomic_read(&sess->nconn)) {
4085 if (!atomic_read(&sess->session_reinstatement) &&
4086 !atomic_read(&sess->session_fall_back_to_erl0)) {
4087 spin_unlock_bh(&sess->conn_lock);
4088 return 0;
4089 }
4090 if (!atomic_read(&sess->session_stop_active)) {
4091 atomic_set(&sess->session_stop_active, 1);
4092 spin_unlock_bh(&sess->conn_lock);
4093 iscsit_stop_session(sess, 0, 0);
4094 return 0;
4095 }
4096 spin_unlock_bh(&sess->conn_lock);
4097 return 0;
4098 }
4099
4100 /*
4101 * If this was the last connection in the session and one of the
4102 * following is occurring:
4103 *
4104 * Session Reinstatement is not being performed, and are falling back
4105 * to ERL=0 call iscsit_close_session().
4106 *
4107 * Session Logout was requested. iscsit_close_session() will be called
4108 * elsewhere.
4109 *
4110 * Session Continuation is not being performed, start the Time2Retain
4111 * handler and check if sleep_on_sess_wait_sem is active.
4112 */
4113 if (!atomic_read(&sess->session_reinstatement) &&
4114 atomic_read(&sess->session_fall_back_to_erl0)) {
4115 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004116 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004117
4118 return 0;
4119 } else if (atomic_read(&sess->session_logout)) {
4120 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4121 sess->session_state = TARG_SESS_STATE_FREE;
4122 spin_unlock_bh(&sess->conn_lock);
4123
4124 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4125 complete(&sess->session_wait_comp);
4126
4127 return 0;
4128 } else {
4129 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4130 sess->session_state = TARG_SESS_STATE_FAILED;
4131
4132 if (!atomic_read(&sess->session_continuation)) {
4133 spin_unlock_bh(&sess->conn_lock);
4134 iscsit_start_time2retain_handler(sess);
4135 } else
4136 spin_unlock_bh(&sess->conn_lock);
4137
4138 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4139 complete(&sess->session_wait_comp);
4140
4141 return 0;
4142 }
4143 spin_unlock_bh(&sess->conn_lock);
4144
4145 return 0;
4146}
4147
4148int iscsit_close_session(struct iscsi_session *sess)
4149{
4150 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4151 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4152
4153 if (atomic_read(&sess->nconn)) {
4154 pr_err("%d connection(s) still exist for iSCSI session"
4155 " to %s\n", atomic_read(&sess->nconn),
4156 sess->sess_ops->InitiatorName);
4157 BUG();
4158 }
4159
4160 spin_lock_bh(&se_tpg->session_lock);
4161 atomic_set(&sess->session_logout, 1);
4162 atomic_set(&sess->session_reinstatement, 1);
4163 iscsit_stop_time2retain_timer(sess);
4164 spin_unlock_bh(&se_tpg->session_lock);
4165
4166 /*
4167 * transport_deregister_session_configfs() will clear the
4168 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4169 * can be setting it again with __transport_register_session() in
4170 * iscsi_post_login_handler() again after the iscsit_stop_session()
4171 * completes in iscsi_np context.
4172 */
4173 transport_deregister_session_configfs(sess->se_sess);
4174
4175 /*
4176 * If any other processes are accessing this session pointer we must
4177 * wait until they have completed. If we are in an interrupt (the
4178 * time2retain handler) and contain and active session usage count we
4179 * restart the timer and exit.
4180 */
4181 if (!in_interrupt()) {
4182 if (iscsit_check_session_usage_count(sess) == 1)
4183 iscsit_stop_session(sess, 1, 1);
4184 } else {
4185 if (iscsit_check_session_usage_count(sess) == 2) {
4186 atomic_set(&sess->session_logout, 0);
4187 iscsit_start_time2retain_handler(sess);
4188 return 0;
4189 }
4190 }
4191
4192 transport_deregister_session(sess->se_sess);
4193
4194 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4195 iscsit_free_connection_recovery_entires(sess);
4196
4197 iscsit_free_all_ooo_cmdsns(sess);
4198
4199 spin_lock_bh(&se_tpg->session_lock);
4200 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4201 sess->session_state = TARG_SESS_STATE_FREE;
4202 pr_debug("Released iSCSI session from node: %s\n",
4203 sess->sess_ops->InitiatorName);
4204 tpg->nsessions--;
4205 if (tpg->tpg_tiqn)
4206 tpg->tpg_tiqn->tiqn_nsessions--;
4207
4208 pr_debug("Decremented number of active iSCSI Sessions on"
4209 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4210
4211 spin_lock(&sess_idr_lock);
4212 idr_remove(&sess_idr, sess->session_index);
4213 spin_unlock(&sess_idr_lock);
4214
4215 kfree(sess->sess_ops);
4216 sess->sess_ops = NULL;
4217 spin_unlock_bh(&se_tpg->session_lock);
4218
4219 kfree(sess);
4220 return 0;
4221}
4222
4223static void iscsit_logout_post_handler_closesession(
4224 struct iscsi_conn *conn)
4225{
4226 struct iscsi_session *sess = conn->sess;
4227
4228 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4229 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4230
4231 atomic_set(&conn->conn_logout_remove, 0);
4232 complete(&conn->conn_logout_comp);
4233
4234 iscsit_dec_conn_usage_count(conn);
4235 iscsit_stop_session(sess, 1, 1);
4236 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004237 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004238}
4239
4240static void iscsit_logout_post_handler_samecid(
4241 struct iscsi_conn *conn)
4242{
4243 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4244 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4245
4246 atomic_set(&conn->conn_logout_remove, 0);
4247 complete(&conn->conn_logout_comp);
4248
4249 iscsit_cause_connection_reinstatement(conn, 1);
4250 iscsit_dec_conn_usage_count(conn);
4251}
4252
4253static void iscsit_logout_post_handler_diffcid(
4254 struct iscsi_conn *conn,
4255 u16 cid)
4256{
4257 struct iscsi_conn *l_conn;
4258 struct iscsi_session *sess = conn->sess;
4259
4260 if (!sess)
4261 return;
4262
4263 spin_lock_bh(&sess->conn_lock);
4264 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4265 if (l_conn->cid == cid) {
4266 iscsit_inc_conn_usage_count(l_conn);
4267 break;
4268 }
4269 }
4270 spin_unlock_bh(&sess->conn_lock);
4271
4272 if (!l_conn)
4273 return;
4274
4275 if (l_conn->sock)
4276 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4277
4278 spin_lock_bh(&l_conn->state_lock);
4279 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4280 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4281 spin_unlock_bh(&l_conn->state_lock);
4282
4283 iscsit_cause_connection_reinstatement(l_conn, 1);
4284 iscsit_dec_conn_usage_count(l_conn);
4285}
4286
4287/*
4288 * Return of 0 causes the TX thread to restart.
4289 */
4290static int iscsit_logout_post_handler(
4291 struct iscsi_cmd *cmd,
4292 struct iscsi_conn *conn)
4293{
4294 int ret = 0;
4295
4296 switch (cmd->logout_reason) {
4297 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4298 switch (cmd->logout_response) {
4299 case ISCSI_LOGOUT_SUCCESS:
4300 case ISCSI_LOGOUT_CLEANUP_FAILED:
4301 default:
4302 iscsit_logout_post_handler_closesession(conn);
4303 break;
4304 }
4305 ret = 0;
4306 break;
4307 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4308 if (conn->cid == cmd->logout_cid) {
4309 switch (cmd->logout_response) {
4310 case ISCSI_LOGOUT_SUCCESS:
4311 case ISCSI_LOGOUT_CLEANUP_FAILED:
4312 default:
4313 iscsit_logout_post_handler_samecid(conn);
4314 break;
4315 }
4316 ret = 0;
4317 } else {
4318 switch (cmd->logout_response) {
4319 case ISCSI_LOGOUT_SUCCESS:
4320 iscsit_logout_post_handler_diffcid(conn,
4321 cmd->logout_cid);
4322 break;
4323 case ISCSI_LOGOUT_CID_NOT_FOUND:
4324 case ISCSI_LOGOUT_CLEANUP_FAILED:
4325 default:
4326 break;
4327 }
4328 ret = 1;
4329 }
4330 break;
4331 case ISCSI_LOGOUT_REASON_RECOVERY:
4332 switch (cmd->logout_response) {
4333 case ISCSI_LOGOUT_SUCCESS:
4334 case ISCSI_LOGOUT_CID_NOT_FOUND:
4335 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4336 case ISCSI_LOGOUT_CLEANUP_FAILED:
4337 default:
4338 break;
4339 }
4340 ret = 1;
4341 break;
4342 default:
4343 break;
4344
4345 }
4346 return ret;
4347}
4348
4349void iscsit_fail_session(struct iscsi_session *sess)
4350{
4351 struct iscsi_conn *conn;
4352
4353 spin_lock_bh(&sess->conn_lock);
4354 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4355 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4356 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4357 }
4358 spin_unlock_bh(&sess->conn_lock);
4359
4360 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4361 sess->session_state = TARG_SESS_STATE_FAILED;
4362}
4363
4364int iscsit_free_session(struct iscsi_session *sess)
4365{
4366 u16 conn_count = atomic_read(&sess->nconn);
4367 struct iscsi_conn *conn, *conn_tmp = NULL;
4368 int is_last;
4369
4370 spin_lock_bh(&sess->conn_lock);
4371 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4372
4373 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4374 conn_list) {
4375 if (conn_count == 0)
4376 break;
4377
4378 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4379 is_last = 1;
4380 } else {
4381 iscsit_inc_conn_usage_count(conn_tmp);
4382 is_last = 0;
4383 }
4384 iscsit_inc_conn_usage_count(conn);
4385
4386 spin_unlock_bh(&sess->conn_lock);
4387 iscsit_cause_connection_reinstatement(conn, 1);
4388 spin_lock_bh(&sess->conn_lock);
4389
4390 iscsit_dec_conn_usage_count(conn);
4391 if (is_last == 0)
4392 iscsit_dec_conn_usage_count(conn_tmp);
4393
4394 conn_count--;
4395 }
4396
4397 if (atomic_read(&sess->nconn)) {
4398 spin_unlock_bh(&sess->conn_lock);
4399 wait_for_completion(&sess->session_wait_comp);
4400 } else
4401 spin_unlock_bh(&sess->conn_lock);
4402
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004403 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004404 return 0;
4405}
4406
4407void iscsit_stop_session(
4408 struct iscsi_session *sess,
4409 int session_sleep,
4410 int connection_sleep)
4411{
4412 u16 conn_count = atomic_read(&sess->nconn);
4413 struct iscsi_conn *conn, *conn_tmp = NULL;
4414 int is_last;
4415
4416 spin_lock_bh(&sess->conn_lock);
4417 if (session_sleep)
4418 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4419
4420 if (connection_sleep) {
4421 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4422 conn_list) {
4423 if (conn_count == 0)
4424 break;
4425
4426 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4427 is_last = 1;
4428 } else {
4429 iscsit_inc_conn_usage_count(conn_tmp);
4430 is_last = 0;
4431 }
4432 iscsit_inc_conn_usage_count(conn);
4433
4434 spin_unlock_bh(&sess->conn_lock);
4435 iscsit_cause_connection_reinstatement(conn, 1);
4436 spin_lock_bh(&sess->conn_lock);
4437
4438 iscsit_dec_conn_usage_count(conn);
4439 if (is_last == 0)
4440 iscsit_dec_conn_usage_count(conn_tmp);
4441 conn_count--;
4442 }
4443 } else {
4444 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4445 iscsit_cause_connection_reinstatement(conn, 0);
4446 }
4447
4448 if (session_sleep && atomic_read(&sess->nconn)) {
4449 spin_unlock_bh(&sess->conn_lock);
4450 wait_for_completion(&sess->session_wait_comp);
4451 } else
4452 spin_unlock_bh(&sess->conn_lock);
4453}
4454
4455int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4456{
4457 struct iscsi_session *sess;
4458 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4459 struct se_session *se_sess, *se_sess_tmp;
4460 int session_count = 0;
4461
4462 spin_lock_bh(&se_tpg->session_lock);
4463 if (tpg->nsessions && !force) {
4464 spin_unlock_bh(&se_tpg->session_lock);
4465 return -1;
4466 }
4467
4468 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4469 sess_list) {
4470 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4471
4472 spin_lock(&sess->conn_lock);
4473 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4474 atomic_read(&sess->session_logout) ||
4475 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4476 spin_unlock(&sess->conn_lock);
4477 continue;
4478 }
4479 atomic_set(&sess->session_reinstatement, 1);
4480 spin_unlock(&sess->conn_lock);
4481 spin_unlock_bh(&se_tpg->session_lock);
4482
4483 iscsit_free_session(sess);
4484 spin_lock_bh(&se_tpg->session_lock);
4485
4486 session_count++;
4487 }
4488 spin_unlock_bh(&se_tpg->session_lock);
4489
4490 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4491 " Group: %hu\n", session_count, tpg->tpgt);
4492 return 0;
4493}
4494
4495MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4496MODULE_VERSION("4.1.x");
4497MODULE_AUTHOR("nab@Linux-iSCSI.org");
4498MODULE_LICENSE("GPL");
4499
4500module_init(iscsi_target_init_module);
4501module_exit(iscsi_target_cleanup_module);