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