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