blob: f2c3d4abfe703555d577928d88b1e0357a78775d [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 *,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -070073 struct iscsi_scsi_req *, u32);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000074
75struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
76{
77 struct iscsi_tiqn *tiqn = NULL;
78
79 spin_lock(&tiqn_lock);
80 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
81 if (!strcmp(tiqn->tiqn, buf)) {
82
83 spin_lock(&tiqn->tiqn_state_lock);
84 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
85 tiqn->tiqn_access_count++;
86 spin_unlock(&tiqn->tiqn_state_lock);
87 spin_unlock(&tiqn_lock);
88 return tiqn;
89 }
90 spin_unlock(&tiqn->tiqn_state_lock);
91 }
92 }
93 spin_unlock(&tiqn_lock);
94
95 return NULL;
96}
97
98static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
99{
100 spin_lock(&tiqn->tiqn_state_lock);
101 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
102 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
103 spin_unlock(&tiqn->tiqn_state_lock);
104 return 0;
105 }
106 spin_unlock(&tiqn->tiqn_state_lock);
107
108 return -1;
109}
110
111void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
112{
113 spin_lock(&tiqn->tiqn_state_lock);
114 tiqn->tiqn_access_count--;
115 spin_unlock(&tiqn->tiqn_state_lock);
116}
117
118/*
119 * Note that IQN formatting is expected to be done in userspace, and
120 * no explict IQN format checks are done here.
121 */
122struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
123{
124 struct iscsi_tiqn *tiqn = NULL;
125 int ret;
126
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300127 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000128 pr_err("Target IQN exceeds %d bytes\n",
129 ISCSI_IQN_LEN);
130 return ERR_PTR(-EINVAL);
131 }
132
133 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
134 if (!tiqn) {
135 pr_err("Unable to allocate struct iscsi_tiqn\n");
136 return ERR_PTR(-ENOMEM);
137 }
138
139 sprintf(tiqn->tiqn, "%s", buf);
140 INIT_LIST_HEAD(&tiqn->tiqn_list);
141 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
142 spin_lock_init(&tiqn->tiqn_state_lock);
143 spin_lock_init(&tiqn->tiqn_tpg_lock);
144 spin_lock_init(&tiqn->sess_err_stats.lock);
145 spin_lock_init(&tiqn->login_stats.lock);
146 spin_lock_init(&tiqn->logout_stats.lock);
147
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000148 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
149
Tejun Heoc9365bd2013-02-27 17:04:43 -0800150 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000151 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800152
153 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000154 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800155 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000156 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800157 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000158 kfree(tiqn);
159 return ERR_PTR(ret);
160 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800161 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000162 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800163
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000164 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800165 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000166
167 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
168
169 return tiqn;
170
171}
172
173static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
174{
175 /*
176 * Wait for accesses to said struct iscsi_tiqn to end.
177 */
178 spin_lock(&tiqn->tiqn_state_lock);
179 while (tiqn->tiqn_access_count != 0) {
180 spin_unlock(&tiqn->tiqn_state_lock);
181 msleep(10);
182 spin_lock(&tiqn->tiqn_state_lock);
183 }
184 spin_unlock(&tiqn->tiqn_state_lock);
185}
186
187void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
188{
189 /*
190 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
191 * while holding tiqn->tiqn_state_lock. This means that all subsequent
192 * attempts to access this struct iscsi_tiqn will fail from both transport
193 * fabric and control code paths.
194 */
195 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
196 pr_err("iscsit_set_tiqn_shutdown() failed\n");
197 return;
198 }
199
200 iscsit_wait_for_tiqn(tiqn);
201
202 spin_lock(&tiqn_lock);
203 list_del(&tiqn->tiqn_list);
204 idr_remove(&tiqn_idr, tiqn->tiqn_index);
205 spin_unlock(&tiqn_lock);
206
207 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
208 tiqn->tiqn);
209 kfree(tiqn);
210}
211
212int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
213{
214 int ret;
215 /*
216 * Determine if the network portal is accepting storage traffic.
217 */
218 spin_lock_bh(&np->np_thread_lock);
219 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
220 spin_unlock_bh(&np->np_thread_lock);
221 return -1;
222 }
223 if (np->np_login_tpg) {
224 pr_err("np->np_login_tpg() is not NULL!\n");
225 spin_unlock_bh(&np->np_thread_lock);
226 return -1;
227 }
228 spin_unlock_bh(&np->np_thread_lock);
229 /*
230 * Determine if the portal group is accepting storage traffic.
231 */
232 spin_lock_bh(&tpg->tpg_state_lock);
233 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
234 spin_unlock_bh(&tpg->tpg_state_lock);
235 return -1;
236 }
237 spin_unlock_bh(&tpg->tpg_state_lock);
238
239 /*
240 * Here we serialize access across the TIQN+TPG Tuple.
241 */
242 ret = mutex_lock_interruptible(&tpg->np_login_lock);
243 if ((ret != 0) || signal_pending(current))
244 return -1;
245
246 spin_lock_bh(&np->np_thread_lock);
247 np->np_login_tpg = tpg;
248 spin_unlock_bh(&np->np_thread_lock);
249
250 return 0;
251}
252
253int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
254{
255 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
256
257 spin_lock_bh(&np->np_thread_lock);
258 np->np_login_tpg = NULL;
259 spin_unlock_bh(&np->np_thread_lock);
260
261 mutex_unlock(&tpg->np_login_lock);
262
263 if (tiqn)
264 iscsit_put_tiqn_for_login(tiqn);
265
266 return 0;
267}
268
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800269bool iscsit_check_np_match(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000270 struct __kernel_sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800271 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000272 int network_transport)
273{
274 struct sockaddr_in *sock_in, *sock_in_e;
275 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800276 bool ip_match = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000277 u16 port;
278
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800279 if (sockaddr->ss_family == AF_INET6) {
280 sock_in6 = (struct sockaddr_in6 *)sockaddr;
281 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
282
283 if (!memcmp(&sock_in6->sin6_addr.in6_u,
284 &sock_in6_e->sin6_addr.in6_u,
285 sizeof(struct in6_addr)))
286 ip_match = true;
287
288 port = ntohs(sock_in6->sin6_port);
289 } else {
290 sock_in = (struct sockaddr_in *)sockaddr;
291 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
292
293 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
294 ip_match = true;
295
296 port = ntohs(sock_in->sin_port);
297 }
298
299 if ((ip_match == true) && (np->np_port == port) &&
300 (np->np_network_transport == network_transport))
301 return true;
302
303 return false;
304}
305
306static struct iscsi_np *iscsit_get_np(
307 struct __kernel_sockaddr_storage *sockaddr,
308 int network_transport)
309{
310 struct iscsi_np *np;
311 bool match;
312
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000313 spin_lock_bh(&np_lock);
314 list_for_each_entry(np, &g_np_list, np_list) {
315 spin_lock(&np->np_thread_lock);
316 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
317 spin_unlock(&np->np_thread_lock);
318 continue;
319 }
320
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800321 match = iscsit_check_np_match(sockaddr, np, network_transport);
322 if (match == true) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000323 /*
324 * Increment the np_exports reference count now to
325 * prevent iscsit_del_np() below from being called
326 * while iscsi_tpg_add_network_portal() is called.
327 */
328 np->np_exports++;
329 spin_unlock(&np->np_thread_lock);
330 spin_unlock_bh(&np_lock);
331 return np;
332 }
333 spin_unlock(&np->np_thread_lock);
334 }
335 spin_unlock_bh(&np_lock);
336
337 return NULL;
338}
339
340struct iscsi_np *iscsit_add_np(
341 struct __kernel_sockaddr_storage *sockaddr,
342 char *ip_str,
343 int network_transport)
344{
345 struct sockaddr_in *sock_in;
346 struct sockaddr_in6 *sock_in6;
347 struct iscsi_np *np;
348 int ret;
349 /*
350 * Locate the existing struct iscsi_np if already active..
351 */
352 np = iscsit_get_np(sockaddr, network_transport);
353 if (np)
354 return np;
355
356 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
357 if (!np) {
358 pr_err("Unable to allocate memory for struct iscsi_np\n");
359 return ERR_PTR(-ENOMEM);
360 }
361
362 np->np_flags |= NPF_IP_NETWORK;
363 if (sockaddr->ss_family == AF_INET6) {
364 sock_in6 = (struct sockaddr_in6 *)sockaddr;
365 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
366 np->np_port = ntohs(sock_in6->sin6_port);
367 } else {
368 sock_in = (struct sockaddr_in *)sockaddr;
369 sprintf(np->np_ip, "%s", ip_str);
370 np->np_port = ntohs(sock_in->sin_port);
371 }
372
373 np->np_network_transport = network_transport;
374 spin_lock_init(&np->np_thread_lock);
375 init_completion(&np->np_restart_comp);
376 INIT_LIST_HEAD(&np->np_list);
377
378 ret = iscsi_target_setup_login_socket(np, sockaddr);
379 if (ret != 0) {
380 kfree(np);
381 return ERR_PTR(ret);
382 }
383
384 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
385 if (IS_ERR(np->np_thread)) {
386 pr_err("Unable to create kthread: iscsi_np\n");
387 ret = PTR_ERR(np->np_thread);
388 kfree(np);
389 return ERR_PTR(ret);
390 }
391 /*
392 * Increment the np_exports reference count now to prevent
393 * iscsit_del_np() below from being run while a new call to
394 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
395 * active. We don't need to hold np->np_thread_lock at this
396 * point because iscsi_np has not been added to g_np_list yet.
397 */
398 np->np_exports = 1;
399
400 spin_lock_bh(&np_lock);
401 list_add_tail(&np->np_list, &g_np_list);
402 spin_unlock_bh(&np_lock);
403
404 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800405 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000406
407 return np;
408}
409
410int iscsit_reset_np_thread(
411 struct iscsi_np *np,
412 struct iscsi_tpg_np *tpg_np,
413 struct iscsi_portal_group *tpg)
414{
415 spin_lock_bh(&np->np_thread_lock);
416 if (tpg && tpg_np) {
417 /*
418 * The reset operation need only be performed when the
419 * passed struct iscsi_portal_group has a login in progress
420 * to one of the network portals.
421 */
422 if (tpg_np->tpg_np->np_login_tpg != tpg) {
423 spin_unlock_bh(&np->np_thread_lock);
424 return 0;
425 }
426 }
427 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
428 spin_unlock_bh(&np->np_thread_lock);
429 return 0;
430 }
431 np->np_thread_state = ISCSI_NP_THREAD_RESET;
432
433 if (np->np_thread) {
434 spin_unlock_bh(&np->np_thread_lock);
435 send_sig(SIGINT, np->np_thread, 1);
436 wait_for_completion(&np->np_restart_comp);
437 spin_lock_bh(&np->np_thread_lock);
438 }
439 spin_unlock_bh(&np->np_thread_lock);
440
441 return 0;
442}
443
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800444static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000445{
Al Virobf6932f2012-07-21 08:55:18 +0100446 if (np->np_socket)
447 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000448}
449
450int iscsit_del_np(struct iscsi_np *np)
451{
452 spin_lock_bh(&np->np_thread_lock);
453 np->np_exports--;
454 if (np->np_exports) {
455 spin_unlock_bh(&np->np_thread_lock);
456 return 0;
457 }
458 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
459 spin_unlock_bh(&np->np_thread_lock);
460
461 if (np->np_thread) {
462 /*
463 * We need to send the signal to wakeup Linux/Net
464 * which may be sleeping in sock_accept()..
465 */
466 send_sig(SIGINT, np->np_thread, 1);
467 kthread_stop(np->np_thread);
468 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800469
470 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000471
472 spin_lock_bh(&np_lock);
473 list_del(&np->np_list);
474 spin_unlock_bh(&np_lock);
475
476 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800477 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000478
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800479 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000480 kfree(np);
481 return 0;
482}
483
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700484static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
485static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
486
487static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
488{
489 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
490 return 0;
491}
492
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800493static struct iscsit_transport iscsi_target_transport = {
494 .name = "iSCSI/TCP",
495 .transport_type = ISCSI_TCP,
496 .owner = NULL,
497 .iscsit_setup_np = iscsit_setup_np,
498 .iscsit_accept_np = iscsit_accept_np,
499 .iscsit_free_np = iscsit_free_np,
Nicholas Bellingercdb72662013-03-06 22:09:17 -0800500 .iscsit_alloc_cmd = iscsit_alloc_cmd,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800501 .iscsit_get_login_rx = iscsit_get_login_rx,
502 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800503 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700504 .iscsit_immediate_queue = iscsit_immediate_queue,
505 .iscsit_response_queue = iscsit_response_queue,
506 .iscsit_queue_data_in = iscsit_queue_rsp,
507 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800508};
509
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000510static int __init iscsi_target_init_module(void)
511{
512 int ret = 0;
513
514 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
515
516 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
517 if (!iscsit_global) {
518 pr_err("Unable to allocate memory for iscsit_global\n");
519 return -1;
520 }
521 mutex_init(&auth_id_lock);
522 spin_lock_init(&sess_idr_lock);
523 idr_init(&tiqn_idr);
524 idr_init(&sess_idr);
525
526 ret = iscsi_target_register_configfs();
527 if (ret < 0)
528 goto out;
529
530 ret = iscsi_thread_set_init();
531 if (ret < 0)
532 goto configfs_out;
533
534 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
535 TARGET_THREAD_SET_COUNT) {
536 pr_err("iscsi_allocate_thread_sets() returned"
537 " unexpected value!\n");
538 goto ts_out1;
539 }
540
541 lio_cmd_cache = kmem_cache_create("lio_cmd_cache",
542 sizeof(struct iscsi_cmd), __alignof__(struct iscsi_cmd),
543 0, NULL);
544 if (!lio_cmd_cache) {
545 pr_err("Unable to kmem_cache_create() for"
546 " lio_cmd_cache\n");
547 goto ts_out2;
548 }
549
550 lio_qr_cache = kmem_cache_create("lio_qr_cache",
551 sizeof(struct iscsi_queue_req),
552 __alignof__(struct iscsi_queue_req), 0, NULL);
553 if (!lio_qr_cache) {
554 pr_err("nable to kmem_cache_create() for"
555 " lio_qr_cache\n");
556 goto cmd_out;
557 }
558
559 lio_dr_cache = kmem_cache_create("lio_dr_cache",
560 sizeof(struct iscsi_datain_req),
561 __alignof__(struct iscsi_datain_req), 0, NULL);
562 if (!lio_dr_cache) {
563 pr_err("Unable to kmem_cache_create() for"
564 " lio_dr_cache\n");
565 goto qr_out;
566 }
567
568 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
569 sizeof(struct iscsi_ooo_cmdsn),
570 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
571 if (!lio_ooo_cache) {
572 pr_err("Unable to kmem_cache_create() for"
573 " lio_ooo_cache\n");
574 goto dr_out;
575 }
576
577 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
578 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
579 0, NULL);
580 if (!lio_r2t_cache) {
581 pr_err("Unable to kmem_cache_create() for"
582 " lio_r2t_cache\n");
583 goto ooo_out;
584 }
585
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800586 iscsit_register_transport(&iscsi_target_transport);
587
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000588 if (iscsit_load_discovery_tpg() < 0)
589 goto r2t_out;
590
591 return ret;
592r2t_out:
593 kmem_cache_destroy(lio_r2t_cache);
594ooo_out:
595 kmem_cache_destroy(lio_ooo_cache);
596dr_out:
597 kmem_cache_destroy(lio_dr_cache);
598qr_out:
599 kmem_cache_destroy(lio_qr_cache);
600cmd_out:
601 kmem_cache_destroy(lio_cmd_cache);
602ts_out2:
603 iscsi_deallocate_thread_sets();
604ts_out1:
605 iscsi_thread_set_free();
606configfs_out:
607 iscsi_target_deregister_configfs();
608out:
609 kfree(iscsit_global);
610 return -ENOMEM;
611}
612
613static void __exit iscsi_target_cleanup_module(void)
614{
615 iscsi_deallocate_thread_sets();
616 iscsi_thread_set_free();
617 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800618 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000619 kmem_cache_destroy(lio_cmd_cache);
620 kmem_cache_destroy(lio_qr_cache);
621 kmem_cache_destroy(lio_dr_cache);
622 kmem_cache_destroy(lio_ooo_cache);
623 kmem_cache_destroy(lio_r2t_cache);
624
625 iscsi_target_deregister_configfs();
626
627 kfree(iscsit_global);
628}
629
Andy Grover8b1e1242012-04-03 15:51:12 -0700630static int iscsit_add_reject(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000631 u8 reason,
632 int fail_conn,
633 unsigned char *buf,
634 struct iscsi_conn *conn)
635{
636 struct iscsi_cmd *cmd;
637 struct iscsi_reject *hdr;
638 int ret;
639
640 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
641 if (!cmd)
642 return -1;
643
644 cmd->iscsi_opcode = ISCSI_OP_REJECT;
645 if (fail_conn)
646 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
647
648 hdr = (struct iscsi_reject *) cmd->pdu;
649 hdr->reason = reason;
650
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100651 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000652 if (!cmd->buf_ptr) {
653 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700654 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000655 return -1;
656 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000657
658 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700659 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000660 spin_unlock_bh(&conn->cmd_lock);
661
662 cmd->i_state = ISTATE_SEND_REJECT;
663 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
664
665 ret = wait_for_completion_interruptible(&cmd->reject_comp);
666 if (ret != 0)
667 return -1;
668
669 return (!fail_conn) ? 0 : -1;
670}
671
672int iscsit_add_reject_from_cmd(
673 u8 reason,
674 int fail_conn,
675 int add_to_conn,
676 unsigned char *buf,
677 struct iscsi_cmd *cmd)
678{
679 struct iscsi_conn *conn;
680 struct iscsi_reject *hdr;
681 int ret;
682
683 if (!cmd->conn) {
684 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
685 cmd->init_task_tag);
686 return -1;
687 }
688 conn = cmd->conn;
689
690 cmd->iscsi_opcode = ISCSI_OP_REJECT;
691 if (fail_conn)
692 cmd->cmd_flags |= ICF_REJECT_FAIL_CONN;
693
694 hdr = (struct iscsi_reject *) cmd->pdu;
695 hdr->reason = reason;
696
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100697 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000698 if (!cmd->buf_ptr) {
699 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700700 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000701 return -1;
702 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000703
704 if (add_to_conn) {
705 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700706 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000707 spin_unlock_bh(&conn->cmd_lock);
708 }
709
710 cmd->i_state = ISTATE_SEND_REJECT;
711 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
712
713 ret = wait_for_completion_interruptible(&cmd->reject_comp);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800714 /*
715 * Perform the kref_put now if se_cmd has already been setup by
716 * scsit_setup_scsi_cmd()
717 */
718 if (cmd->se_cmd.se_tfo != NULL) {
719 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
720 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
721 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000722 if (ret != 0)
723 return -1;
724
725 return (!fail_conn) ? 0 : -1;
726}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800727EXPORT_SYMBOL(iscsit_add_reject_from_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000728
729/*
730 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700731 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000732 */
733static int iscsit_map_iovec(
734 struct iscsi_cmd *cmd,
735 struct kvec *iov,
736 u32 data_offset,
737 u32 data_length)
738{
739 u32 i = 0;
740 struct scatterlist *sg;
741 unsigned int page_off;
742
743 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700744 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000745 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700746 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000747 page_off = (data_offset % PAGE_SIZE);
748
749 cmd->first_data_sg = sg;
750 cmd->first_data_sg_off = page_off;
751
752 while (data_length) {
753 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
754
755 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
756 iov[i].iov_len = cur_len;
757
758 data_length -= cur_len;
759 page_off = 0;
760 sg = sg_next(sg);
761 i++;
762 }
763
764 cmd->kmapped_nents = i;
765
766 return i;
767}
768
769static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
770{
771 u32 i;
772 struct scatterlist *sg;
773
774 sg = cmd->first_data_sg;
775
776 for (i = 0; i < cmd->kmapped_nents; i++)
777 kunmap(sg_page(&sg[i]));
778}
779
780static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
781{
782 struct iscsi_cmd *cmd;
783
784 conn->exp_statsn = exp_statsn;
785
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800786 if (conn->sess->sess_ops->RDMAExtensions)
787 return;
788
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000789 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700790 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000791 spin_lock(&cmd->istate_lock);
792 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800793 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000794 cmd->i_state = ISTATE_REMOVE;
795 spin_unlock(&cmd->istate_lock);
796 iscsit_add_cmd_to_immediate_queue(cmd, conn,
797 cmd->i_state);
798 continue;
799 }
800 spin_unlock(&cmd->istate_lock);
801 }
802 spin_unlock_bh(&conn->cmd_lock);
803}
804
805static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
806{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700807 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000808
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400809 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000810
811 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
812 if (!cmd->iov_data) {
813 pr_err("Unable to allocate cmd->iov_data\n");
814 return -ENOMEM;
815 }
816
817 cmd->orig_iov_data_count = iov_count;
818 return 0;
819}
820
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800821int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
822 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000823{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800824 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000825 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700826 int iscsi_task_attr;
827 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000828
829 spin_lock_bh(&conn->sess->session_stats_lock);
830 conn->sess->cmd_pdus++;
831 if (conn->sess->se_sess->se_node_acl) {
832 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
833 conn->sess->se_sess->se_node_acl->num_cmds++;
834 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
835 }
836 spin_unlock_bh(&conn->sess->session_stats_lock);
837
838 hdr = (struct iscsi_scsi_req *) buf;
839 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000840
841 /* FIXME; Add checks for AdditionalHeaderSegment */
842
843 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
844 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
845 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
846 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800847 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
848 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000849 }
850
851 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
852 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
853 /*
854 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
855 * that adds support for RESERVE/RELEASE. There is a bug
856 * add with this new functionality that sets R/W bits when
857 * neither CDB carries any READ or WRITE datapayloads.
858 */
859 if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) {
860 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
861 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
862 goto done;
863 }
864
865 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
866 " set when Expected Data Transfer Length is 0 for"
867 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800868 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
869 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000870 }
871done:
872
873 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
874 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
875 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
876 " MUST be set if Expected Data Transfer Length is not 0."
877 " Bad iSCSI Initiator\n");
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800878 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
879 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000880 }
881
882 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
883 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
884 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800885 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
886 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000887 }
888
889 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
890 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
891 " Scsi Command PDU.\n");
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800892 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
893 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000894 }
895
896 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
897 pr_err("ImmediateData=No but DataSegmentLength=%u,"
898 " protocol error.\n", payload_length);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800899 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
900 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000901 }
902
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400903 if ((be32_to_cpu(hdr->data_length )== payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000904 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
905 pr_err("Expected Data Transfer Length and Length of"
906 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
907 " bit is not set protocol error\n");
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800908 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
909 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000910 }
911
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400912 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000913 pr_err("DataSegmentLength: %u is greater than"
914 " EDTL: %u, protocol error.\n", payload_length,
915 hdr->data_length);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800916 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
917 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000918 }
919
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700920 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000921 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700922 " MaxXmitDataSegmentLength: %u, protocol error.\n",
923 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800924 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
925 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000926 }
927
928 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
929 pr_err("DataSegmentLength: %u is greater than"
930 " FirstBurstLength: %u, protocol error.\n",
931 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800932 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
933 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000934 }
935
936 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
937 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
938 DMA_NONE;
939
Andy Groverd28b11692012-04-03 15:51:22 -0700940 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700941 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
942 /*
943 * Figure out the SAM Task Attribute for the incoming SCSI CDB
944 */
945 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
946 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
947 sam_task_attr = MSG_SIMPLE_TAG;
948 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
949 sam_task_attr = MSG_ORDERED_TAG;
950 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
951 sam_task_attr = MSG_HEAD_TAG;
952 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
953 sam_task_attr = MSG_ACA_TAG;
954 else {
955 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
956 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
957 sam_task_attr = MSG_SIMPLE_TAG;
958 }
959
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000960 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
961 cmd->i_state = ISTATE_NEW_CMD;
962 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
963 cmd->immediate_data = (payload_length) ? 1 : 0;
964 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
965 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
966 if (cmd->unsolicited_data)
967 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
968
969 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
970 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
971 spin_lock_bh(&conn->sess->ttt_lock);
972 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
973 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
974 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
975 spin_unlock_bh(&conn->sess->ttt_lock);
976 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
977 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400978 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
979 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000980 cmd->first_burst_len = payload_length;
981
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800982 if (!conn->sess->sess_ops->RDMAExtensions &&
983 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000984 struct iscsi_datain_req *dr;
985
986 dr = iscsit_allocate_datain_req();
987 if (!dr)
988 return iscsit_add_reject_from_cmd(
989 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
990 1, 1, buf, cmd);
991
992 iscsit_attach_datain_req(cmd, dr);
993 }
994
995 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700996 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
997 */
998 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400999 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1000 cmd->data_direction, sam_task_attr,
1001 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -07001002
1003 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1004 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001005 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1006 conn->cid);
1007
1008 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001009
Christoph Hellwigde103c92012-11-06 12:24:09 -08001010 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1011 scsilun_to_int(&hdr->lun));
1012 if (cmd->sense_reason)
1013 goto attach_cmd;
1014
1015 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1016 if (cmd->sense_reason) {
1017 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
1018 return iscsit_add_reject_from_cmd(
1019 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1020 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001021 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001022
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001023 goto attach_cmd;
1024 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001025
Christoph Hellwigde103c92012-11-06 12:24:09 -08001026 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001027 return iscsit_add_reject_from_cmd(
Christoph Hellwigde103c92012-11-06 12:24:09 -08001028 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1029 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001030 }
1031
1032attach_cmd:
1033 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001034 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001035 spin_unlock_bh(&conn->cmd_lock);
1036 /*
1037 * Check if we need to delay processing because of ALUA
1038 * Active/NonOptimized primary access state..
1039 */
1040 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001041
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001042 return 0;
1043}
1044EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001045
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001046void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1047{
1048 iscsit_set_dataout_sequence_values(cmd);
1049
1050 spin_lock_bh(&cmd->dataout_timeout_lock);
1051 iscsit_start_dataout_timer(cmd, cmd->conn);
1052 spin_unlock_bh(&cmd->dataout_timeout_lock);
1053}
1054EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1055
1056int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1057 struct iscsi_scsi_req *hdr)
1058{
1059 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001060 /*
1061 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1062 * the Immediate Bit is not set, and no Immediate
1063 * Data is attached.
1064 *
1065 * A PDU/CmdSN carrying Immediate Data can only
1066 * be processed after the DataCRC has passed.
1067 * If the DataCRC fails, the CmdSN MUST NOT
1068 * be acknowledged. (See below)
1069 */
1070 if (!cmd->immediate_data) {
1071 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001072 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1073 if (!cmd->sense_reason)
1074 return 0;
1075
1076 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001077 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001078 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001079 return iscsit_add_reject_from_cmd(
1080 ISCSI_REASON_PROTOCOL_ERROR,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001081 1, 0, (unsigned char *)hdr, cmd);
1082 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001083 }
1084
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001085 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001086
1087 /*
1088 * If no Immediate Data is attached, it's OK to return now.
1089 */
1090 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001091 if (!cmd->sense_reason && cmd->unsolicited_data)
1092 iscsit_set_unsoliticed_dataout(cmd);
1093 if (!cmd->sense_reason)
1094 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001095
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001096 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001097 return 0;
1098 }
1099
1100 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001101 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1102 * execution. These exceptions are processed in CmdSN order using
1103 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001104 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001105 if (cmd->sense_reason) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001106 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1107 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001108 }
1109 /*
1110 * Call directly into transport_generic_new_cmd() to perform
1111 * the backend memory allocation.
1112 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001113 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1114 if (cmd->sense_reason) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001115 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1116 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001117 }
1118
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001119 return 0;
1120}
1121EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1122
1123static int
1124iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1125 bool dump_payload)
1126{
1127 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1128 /*
1129 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1130 */
1131 if (dump_payload == true)
1132 goto after_immediate_data;
1133
1134 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1135 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001136after_immediate_data:
1137 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1138 /*
1139 * A PDU/CmdSN carrying Immediate Data passed
1140 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1141 * Immediate Bit is not set.
1142 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001143 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001144
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001145 if (cmd->sense_reason) {
1146 if (iscsit_dump_data_payload(cmd->conn,
1147 cmd->first_burst_len, 1) < 0)
1148 return -1;
1149 } else if (cmd->unsolicited_data)
1150 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001151
1152 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1153 return iscsit_add_reject_from_cmd(
1154 ISCSI_REASON_PROTOCOL_ERROR,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001155 1, 0, (unsigned char *)hdr, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001156
1157 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1158 /*
1159 * Immediate Data failed DataCRC and ERL>=1,
1160 * silently drop this PDU and let the initiator
1161 * plug the CmdSN gap.
1162 *
1163 * FIXME: Send Unsolicited NOPIN with reserved
1164 * TTT here to help the initiator figure out
1165 * the missing CmdSN, although they should be
1166 * intelligent enough to determine the missing
1167 * CmdSN and issue a retry to plug the sequence.
1168 */
1169 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001170 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001171 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1172 return -1;
1173
1174 return 0;
1175}
1176
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001177static int
1178iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1179 unsigned char *buf)
1180{
1181 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1182 int rc, immed_data;
1183 bool dump_payload = false;
1184
1185 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1186 if (rc < 0)
1187 return rc;
1188 /*
1189 * Allocation iovecs needed for struct socket operations for
1190 * traditional iSCSI block I/O.
1191 */
1192 if (iscsit_allocate_iovecs(cmd) < 0) {
1193 return iscsit_add_reject_from_cmd(
1194 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1195 1, 0, buf, cmd);
1196 }
1197 immed_data = cmd->immediate_data;
1198
1199 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1200 if (rc < 0)
1201 return rc;
1202 else if (rc > 0)
1203 dump_payload = true;
1204
1205 if (!immed_data)
1206 return 0;
1207
1208 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1209}
1210
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001211static u32 iscsit_do_crypto_hash_sg(
1212 struct hash_desc *hash,
1213 struct iscsi_cmd *cmd,
1214 u32 data_offset,
1215 u32 data_length,
1216 u32 padding,
1217 u8 *pad_bytes)
1218{
1219 u32 data_crc;
1220 u32 i;
1221 struct scatterlist *sg;
1222 unsigned int page_off;
1223
1224 crypto_hash_init(hash);
1225
1226 sg = cmd->first_data_sg;
1227 page_off = cmd->first_data_sg_off;
1228
1229 i = 0;
1230 while (data_length) {
1231 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1232
1233 crypto_hash_update(hash, &sg[i], cur_len);
1234
1235 data_length -= cur_len;
1236 page_off = 0;
1237 i++;
1238 }
1239
1240 if (padding) {
1241 struct scatterlist pad_sg;
1242
1243 sg_init_one(&pad_sg, pad_bytes, padding);
1244 crypto_hash_update(hash, &pad_sg, padding);
1245 }
1246 crypto_hash_final(hash, (u8 *) &data_crc);
1247
1248 return data_crc;
1249}
1250
1251static void iscsit_do_crypto_hash_buf(
1252 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001253 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001254 u32 payload_length,
1255 u32 padding,
1256 u8 *pad_bytes,
1257 u8 *data_crc)
1258{
1259 struct scatterlist sg;
1260
1261 crypto_hash_init(hash);
1262
Jörn Engel8359cf42011-11-24 02:05:51 +01001263 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001264 crypto_hash_update(hash, &sg, payload_length);
1265
1266 if (padding) {
1267 sg_init_one(&sg, pad_bytes, padding);
1268 crypto_hash_update(hash, &sg, padding);
1269 }
1270 crypto_hash_final(hash, data_crc);
1271}
1272
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001273int
1274iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1275 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001276{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001277 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001278 struct iscsi_cmd *cmd = NULL;
1279 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001280 u32 payload_length = ntoh24(hdr->dlength);
1281 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001282
1283 if (!payload_length) {
1284 pr_err("DataOUT payload is ZERO, protocol error.\n");
1285 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1286 buf, conn);
1287 }
1288
1289 /* iSCSI write */
1290 spin_lock_bh(&conn->sess->session_stats_lock);
1291 conn->sess->rx_data_octets += payload_length;
1292 if (conn->sess->se_sess->se_node_acl) {
1293 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1294 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1295 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1296 }
1297 spin_unlock_bh(&conn->sess->session_stats_lock);
1298
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001299 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001300 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001301 " MaxXmitDataSegmentLength: %u\n", payload_length,
1302 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001303 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
1304 buf, conn);
1305 }
1306
1307 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1308 payload_length);
1309 if (!cmd)
1310 return 0;
1311
1312 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1313 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001314 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001315 payload_length, conn->cid);
1316
1317 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1318 pr_err("Command ITT: 0x%08x received DataOUT after"
1319 " last DataOUT received, dumping payload\n",
1320 cmd->init_task_tag);
1321 return iscsit_dump_data_payload(conn, payload_length, 1);
1322 }
1323
1324 if (cmd->data_direction != DMA_TO_DEVICE) {
1325 pr_err("Command ITT: 0x%08x received DataOUT for a"
1326 " NON-WRITE command.\n", cmd->init_task_tag);
1327 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1328 1, 0, buf, cmd);
1329 }
1330 se_cmd = &cmd->se_cmd;
1331 iscsit_mod_dataout_timer(cmd);
1332
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001333 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001334 pr_err("DataOut Offset: %u, Length %u greater than"
1335 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001336 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001337 return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
1338 1, 0, buf, cmd);
1339 }
1340
1341 if (cmd->unsolicited_data) {
1342 int dump_unsolicited_data = 0;
1343
1344 if (conn->sess->sess_ops->InitialR2T) {
1345 pr_err("Received unexpected unsolicited data"
1346 " while InitialR2T=Yes, protocol error.\n");
1347 transport_send_check_condition_and_sense(&cmd->se_cmd,
1348 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1349 return -1;
1350 }
1351 /*
1352 * Special case for dealing with Unsolicited DataOUT
1353 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1354 * failures;
1355 */
1356
1357 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001358 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001359 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001360 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001361
1362 if (dump_unsolicited_data) {
1363 /*
1364 * Check if a delayed TASK_ABORTED status needs to
1365 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1366 * received with the unsolicitied data out.
1367 */
1368 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1369 iscsit_stop_dataout_timer(cmd);
1370
1371 transport_check_aborted_status(se_cmd,
1372 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1373 return iscsit_dump_data_payload(conn, payload_length, 1);
1374 }
1375 } else {
1376 /*
1377 * For the normal solicited data path:
1378 *
1379 * Check for a delayed TASK_ABORTED status and dump any
1380 * incoming data out payload if one exists. Also, when the
1381 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1382 * data out sequence, we decrement outstanding_r2ts. Once
1383 * outstanding_r2ts reaches zero, go ahead and send the delayed
1384 * TASK_ABORTED status.
1385 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001386 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001387 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1388 if (--cmd->outstanding_r2ts < 1) {
1389 iscsit_stop_dataout_timer(cmd);
1390 transport_check_aborted_status(
1391 se_cmd, 1);
1392 }
1393
1394 return iscsit_dump_data_payload(conn, payload_length, 1);
1395 }
1396 }
1397 /*
1398 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1399 * within-command recovery checks before receiving the payload.
1400 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001401 rc = iscsit_check_pre_dataout(cmd, buf);
1402 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001403 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001404 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001405 return -1;
1406
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001407 *out_cmd = cmd;
1408 return 0;
1409}
1410EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1411
1412static int
1413iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1414 struct iscsi_data *hdr)
1415{
1416 struct kvec *iov;
1417 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1418 u32 payload_length = ntoh24(hdr->dlength);
1419 int iov_ret, data_crc_failed = 0;
1420
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001421 rx_size += payload_length;
1422 iov = &cmd->iov_data[0];
1423
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001424 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1425 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001426 if (iov_ret < 0)
1427 return -1;
1428
1429 iov_count += iov_ret;
1430
1431 padding = ((-payload_length) & 3);
1432 if (padding != 0) {
1433 iov[iov_count].iov_base = cmd->pad_bytes;
1434 iov[iov_count++].iov_len = padding;
1435 rx_size += padding;
1436 pr_debug("Receiving %u padding bytes.\n", padding);
1437 }
1438
1439 if (conn->conn_ops->DataDigest) {
1440 iov[iov_count].iov_base = &checksum;
1441 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1442 rx_size += ISCSI_CRC_LEN;
1443 }
1444
1445 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1446
1447 iscsit_unmap_iovec(cmd);
1448
1449 if (rx_got != rx_size)
1450 return -1;
1451
1452 if (conn->conn_ops->DataDigest) {
1453 u32 data_crc;
1454
1455 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001456 be32_to_cpu(hdr->offset),
1457 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001458 cmd->pad_bytes);
1459
1460 if (checksum != data_crc) {
1461 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1462 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1463 " does not match computed 0x%08x\n",
1464 hdr->itt, hdr->offset, payload_length,
1465 hdr->datasn, checksum, data_crc);
1466 data_crc_failed = 1;
1467 } else {
1468 pr_debug("Got CRC32C DataDigest 0x%08x for"
1469 " %u bytes of Data Out\n", checksum,
1470 payload_length);
1471 }
1472 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001473
1474 return data_crc_failed;
1475}
1476
1477int
1478iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1479 bool data_crc_failed)
1480{
1481 struct iscsi_conn *conn = cmd->conn;
1482 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001483 /*
1484 * Increment post receive data and CRC values or perform
1485 * within-command recovery.
1486 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001487 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1488 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001489 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001490 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001491 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001492 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1493 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001494 /*
1495 * Handle extra special case for out of order
1496 * Unsolicited Data Out.
1497 */
1498 spin_lock_bh(&cmd->istate_lock);
1499 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1500 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1501 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1502 spin_unlock_bh(&cmd->istate_lock);
1503
1504 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001505 if (ooo_cmdsn)
1506 return 0;
1507 target_execute_cmd(&cmd->se_cmd);
1508 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001509 } else /* DATAOUT_CANNOT_RECOVER */
1510 return -1;
1511
1512 return 0;
1513}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001514EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001515
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001516static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1517{
1518 struct iscsi_cmd *cmd;
1519 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1520 int rc;
1521 bool data_crc_failed = false;
1522
1523 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1524 if (rc < 0)
1525 return rc;
1526 else if (!cmd)
1527 return 0;
1528
1529 rc = iscsit_get_dataout(conn, cmd, hdr);
1530 if (rc < 0)
1531 return rc;
1532 else if (rc > 0)
1533 data_crc_failed = true;
1534
1535 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1536}
1537
Nicholas Bellinger778de362013-06-14 16:07:47 -07001538int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1539 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001540{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001541 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001542
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001543 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001544 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1545 " not set, protocol error.\n");
Nicholas Bellinger778de362013-06-14 16:07:47 -07001546 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1547 1, 0, (unsigned char *)hdr, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001548 }
1549
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001550 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001551 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001552 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001553 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001554 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001555 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1556 1, 0, (unsigned char *)hdr, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001557 }
1558
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001559 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001560 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001561 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001562 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1563 payload_length);
1564 /*
1565 * This is not a response to a Unsolicited NopIN, which means
1566 * it can either be a NOPOUT ping request (with a valid ITT),
1567 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1568 * Either way, make sure we allocate an struct iscsi_cmd, as both
1569 * can contain ping data.
1570 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001571 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001572 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1573 cmd->i_state = ISTATE_SEND_NOPIN;
1574 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1575 1 : 0);
1576 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1577 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001578 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1579 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001580 cmd->data_direction = DMA_NONE;
1581 }
1582
Nicholas Bellinger778de362013-06-14 16:07:47 -07001583 return 0;
1584}
1585EXPORT_SYMBOL(iscsit_setup_nop_out);
1586
1587int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1588 struct iscsi_nopout *hdr)
1589{
1590 struct iscsi_cmd *cmd_p = NULL;
1591 int cmdsn_ret = 0;
1592 /*
1593 * Initiator is expecting a NopIN ping reply..
1594 */
1595 if (hdr->itt != RESERVED_ITT) {
1596 BUG_ON(!cmd);
1597
1598 spin_lock_bh(&conn->cmd_lock);
1599 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1600 spin_unlock_bh(&conn->cmd_lock);
1601
1602 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1603
1604 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1605 iscsit_add_cmd_to_response_queue(cmd, conn,
1606 cmd->i_state);
1607 return 0;
1608 }
1609
1610 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1611 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1612 return 0;
1613
1614 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1615 return iscsit_add_reject_from_cmd(
1616 ISCSI_REASON_PROTOCOL_ERROR,
1617 1, 0, (unsigned char *)hdr, cmd);
1618
1619 return 0;
1620 }
1621 /*
1622 * This was a response to a unsolicited NOPIN ping.
1623 */
1624 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1625 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1626 if (!cmd_p)
1627 return -EINVAL;
1628
1629 iscsit_stop_nopin_response_timer(conn);
1630
1631 cmd_p->i_state = ISTATE_REMOVE;
1632 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1633
1634 iscsit_start_nopin_timer(conn);
1635 return 0;
1636 }
1637 /*
1638 * Otherwise, initiator is not expecting a NOPIN is response.
1639 * Just ignore for now.
1640 */
1641 return 0;
1642}
1643EXPORT_SYMBOL(iscsit_process_nop_out);
1644
1645static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1646 unsigned char *buf)
1647{
1648 unsigned char *ping_data = NULL;
1649 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1650 struct kvec *iov = NULL;
1651 u32 payload_length = ntoh24(hdr->dlength);
1652 int ret;
1653
1654 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1655 if (ret < 0)
1656 return ret;
1657 /*
1658 * Handle NOP-OUT payload for traditional iSCSI sockets
1659 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001660 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001661 u32 checksum, data_crc, padding = 0;
1662 int niov = 0, rx_got, rx_size = payload_length;
1663
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001664 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1665 if (!ping_data) {
1666 pr_err("Unable to allocate memory for"
1667 " NOPOUT ping data.\n");
1668 ret = -1;
1669 goto out;
1670 }
1671
1672 iov = &cmd->iov_misc[0];
1673 iov[niov].iov_base = ping_data;
1674 iov[niov++].iov_len = payload_length;
1675
1676 padding = ((-payload_length) & 3);
1677 if (padding != 0) {
1678 pr_debug("Receiving %u additional bytes"
1679 " for padding.\n", padding);
1680 iov[niov].iov_base = &cmd->pad_bytes;
1681 iov[niov++].iov_len = padding;
1682 rx_size += padding;
1683 }
1684 if (conn->conn_ops->DataDigest) {
1685 iov[niov].iov_base = &checksum;
1686 iov[niov++].iov_len = ISCSI_CRC_LEN;
1687 rx_size += ISCSI_CRC_LEN;
1688 }
1689
1690 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1691 if (rx_got != rx_size) {
1692 ret = -1;
1693 goto out;
1694 }
1695
1696 if (conn->conn_ops->DataDigest) {
1697 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1698 ping_data, payload_length,
1699 padding, cmd->pad_bytes,
1700 (u8 *)&data_crc);
1701
1702 if (checksum != data_crc) {
1703 pr_err("Ping data CRC32C DataDigest"
1704 " 0x%08x does not match computed 0x%08x\n",
1705 checksum, data_crc);
1706 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1707 pr_err("Unable to recover from"
1708 " NOPOUT Ping DataCRC failure while in"
1709 " ERL=0.\n");
1710 ret = -1;
1711 goto out;
1712 } else {
1713 /*
1714 * Silently drop this PDU and let the
1715 * initiator plug the CmdSN gap.
1716 */
1717 pr_debug("Dropping NOPOUT"
1718 " Command CmdSN: 0x%08x due to"
1719 " DataCRC error.\n", hdr->cmdsn);
1720 ret = 0;
1721 goto out;
1722 }
1723 } else {
1724 pr_debug("Got CRC32C DataDigest"
1725 " 0x%08x for %u bytes of ping data.\n",
1726 checksum, payload_length);
1727 }
1728 }
1729
1730 ping_data[payload_length] = '\0';
1731 /*
1732 * Attach ping data to struct iscsi_cmd->buf_ptr.
1733 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001734 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001735 cmd->buf_ptr_size = payload_length;
1736
1737 pr_debug("Got %u bytes of NOPOUT ping"
1738 " data.\n", payload_length);
1739 pr_debug("Ping Data: \"%s\"\n", ping_data);
1740 }
1741
Nicholas Bellinger778de362013-06-14 16:07:47 -07001742 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001743out:
1744 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001745 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001746
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001747 kfree(ping_data);
1748 return ret;
1749}
1750
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001751int
1752iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1753 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001754{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 struct se_tmr_req *se_tmr;
1756 struct iscsi_tmr_req *tmr_req;
1757 struct iscsi_tm *hdr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001758 int out_of_order_cmdsn = 0;
1759 int ret;
1760 u8 function;
1761
1762 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001763 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1764 function = hdr->flags;
1765
1766 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1767 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1768 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1769 hdr->rtt, hdr->refcmdsn, conn->cid);
1770
1771 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1772 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001773 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001774 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001775 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001776 }
1777
1778 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1779 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1780 pr_err("Task Management Request TASK_REASSIGN not"
1781 " issued as immediate command, bad iSCSI Initiator"
1782 "implementation\n");
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001783 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1784 1, 1, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001785 }
1786 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001787 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1788 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001789
Andy Groverd28b11692012-04-03 15:51:22 -07001790 cmd->data_direction = DMA_NONE;
1791
1792 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1793 if (!cmd->tmr_req) {
1794 pr_err("Unable to allocate memory for"
1795 " Task Management command!\n");
1796 return iscsit_add_reject_from_cmd(
1797 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1798 1, 1, buf, cmd);
1799 }
1800
1801 /*
1802 * TASK_REASSIGN for ERL=2 / connection stays inside of
1803 * LIO-Target $FABRIC_MOD
1804 */
1805 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1806
1807 u8 tcm_function;
1808 int ret;
1809
1810 transport_init_se_cmd(&cmd->se_cmd,
1811 &lio_target_fabric_configfs->tf_ops,
1812 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001813 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001814
1815 switch (function) {
1816 case ISCSI_TM_FUNC_ABORT_TASK:
1817 tcm_function = TMR_ABORT_TASK;
1818 break;
1819 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1820 tcm_function = TMR_ABORT_TASK_SET;
1821 break;
1822 case ISCSI_TM_FUNC_CLEAR_ACA:
1823 tcm_function = TMR_CLEAR_ACA;
1824 break;
1825 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1826 tcm_function = TMR_CLEAR_TASK_SET;
1827 break;
1828 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1829 tcm_function = TMR_LUN_RESET;
1830 break;
1831 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1832 tcm_function = TMR_TARGET_WARM_RESET;
1833 break;
1834 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1835 tcm_function = TMR_TARGET_COLD_RESET;
1836 break;
1837 default:
1838 pr_err("Unknown iSCSI TMR Function:"
1839 " 0x%02x\n", function);
1840 return iscsit_add_reject_from_cmd(
1841 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1842 1, 1, buf, cmd);
1843 }
1844
1845 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1846 tcm_function, GFP_KERNEL);
1847 if (ret < 0)
1848 return iscsit_add_reject_from_cmd(
1849 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1850 1, 1, buf, cmd);
1851
1852 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1853 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001854
1855 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1856 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1857 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1858 cmd->init_task_tag = hdr->itt;
1859 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001860 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1861 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001862 se_tmr = cmd->se_cmd.se_tmr_req;
1863 tmr_req = cmd->tmr_req;
1864 /*
1865 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1866 */
1867 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001868 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1869 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001870 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001871 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1872 goto attach;
1873 }
1874 }
1875
1876 switch (function) {
1877 case ISCSI_TM_FUNC_ABORT_TASK:
1878 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001879 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001880 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001881 break;
1882 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1883 case ISCSI_TM_FUNC_CLEAR_ACA:
1884 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1885 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1886 break;
1887 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1888 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001889 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1890 goto attach;
1891 }
1892 break;
1893 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1894 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001895 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1896 goto attach;
1897 }
1898 break;
1899 case ISCSI_TM_FUNC_TASK_REASSIGN:
1900 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1901 /*
1902 * Perform sanity checks on the ExpDataSN only if the
1903 * TASK_REASSIGN was successful.
1904 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001905 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001906 break;
1907
1908 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
1909 return iscsit_add_reject_from_cmd(
1910 ISCSI_REASON_BOOKMARK_INVALID, 1, 1,
1911 buf, cmd);
1912 break;
1913 default:
1914 pr_err("Unknown TMR function: 0x%02x, protocol"
1915 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001916 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1917 goto attach;
1918 }
1919
1920 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1921 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1922 se_tmr->call_transport = 1;
1923attach:
1924 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001925 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001926 spin_unlock_bh(&conn->cmd_lock);
1927
1928 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1929 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1930 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1931 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001932 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001933 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001934 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001935 return iscsit_add_reject_from_cmd(
1936 ISCSI_REASON_PROTOCOL_ERROR,
1937 1, 0, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001938 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001939 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001940
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001941 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001942 return 0;
1943 /*
1944 * Found the referenced task, send to transport for processing.
1945 */
1946 if (se_tmr->call_transport)
1947 return transport_generic_handle_tmr(&cmd->se_cmd);
1948
1949 /*
1950 * Could not find the referenced LUN, task, or Task Management
1951 * command not authorized or supported. Change state and
1952 * let the tx_thread send the response.
1953 *
1954 * For connection recovery, this is also the default action for
1955 * TMR TASK_REASSIGN.
1956 */
1957 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1958 return 0;
1959}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001960EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001961
1962/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001963int
1964iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1965 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001966{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001967 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001968
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001969 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001970 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001971 "greater than MaxXmitDataSegmentLength %u.\n",
1972 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001973 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
1974 1, 0, (unsigned char *)hdr, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001975 }
1976
1977 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1978 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1979 hdr->exp_statsn, payload_length);
1980
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001981 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1982 cmd->i_state = ISTATE_SEND_TEXTRSP;
1983 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1984 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1985 cmd->targ_xfer_tag = 0xFFFFFFFF;
1986 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1987 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1988 cmd->data_direction = DMA_NONE;
1989
1990 return 0;
1991}
1992EXPORT_SYMBOL(iscsit_setup_text_cmd);
1993
1994int
1995iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1996 struct iscsi_text *hdr)
1997{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001998 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001999 int cmdsn_ret;
2000
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002001 if (!text_in) {
2002 pr_err("Unable to locate text_in buffer for sendtargets"
2003 " discovery\n");
2004 goto reject;
2005 }
2006 if (strncmp("SendTargets", text_in, 11) != 0) {
2007 pr_err("Received Text Data that is not"
2008 " SendTargets, cannot continue.\n");
2009 goto reject;
2010 }
2011 text_ptr = strchr(text_in, '=');
2012 if (!text_ptr) {
2013 pr_err("No \"=\" separator found in Text Data,"
2014 " cannot continue.\n");
2015 goto reject;
2016 }
2017 if (!strncmp("=All", text_ptr, 4)) {
2018 cmd->cmd_flags |= IFC_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002019 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2020 !strncmp("=eui.", text_ptr, 5)) {
2021 cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002022 } else {
2023 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2024 goto reject;
2025 }
2026
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002027 spin_lock_bh(&conn->cmd_lock);
2028 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2029 spin_unlock_bh(&conn->cmd_lock);
2030
2031 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2032
2033 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2034 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2035 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2036 return iscsit_add_reject_from_cmd(
2037 ISCSI_REASON_PROTOCOL_ERROR,
2038 1, 0, (unsigned char *)hdr, cmd);
2039 return 0;
2040 }
2041
2042 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002043
2044reject:
2045 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
2046 0, 0, (unsigned char *)hdr, cmd);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002047}
2048EXPORT_SYMBOL(iscsit_process_text_cmd);
2049
2050static int
2051iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2052 unsigned char *buf)
2053{
2054 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2055 char *text_in = NULL;
2056 u32 payload_length = ntoh24(hdr->dlength);
2057 int rx_size, rc;
2058
2059 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2060 if (rc < 0)
2061 return rc;
2062
2063 rx_size = payload_length;
2064 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002065 u32 checksum = 0, data_crc = 0;
2066 u32 padding = 0, pad_bytes = 0;
2067 int niov = 0, rx_got;
2068 struct kvec iov[3];
2069
2070 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002071 if (!text_in) {
2072 pr_err("Unable to allocate memory for"
2073 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002074 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002075 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002076 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002077
2078 memset(iov, 0, 3 * sizeof(struct kvec));
2079 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002080 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002081
2082 padding = ((-payload_length) & 3);
2083 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002084 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002085 iov[niov++].iov_len = padding;
2086 rx_size += padding;
2087 pr_debug("Receiving %u additional bytes"
2088 " for padding.\n", padding);
2089 }
2090 if (conn->conn_ops->DataDigest) {
2091 iov[niov].iov_base = &checksum;
2092 iov[niov++].iov_len = ISCSI_CRC_LEN;
2093 rx_size += ISCSI_CRC_LEN;
2094 }
2095
2096 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002097 if (rx_got != rx_size)
2098 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002099
2100 if (conn->conn_ops->DataDigest) {
2101 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002102 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002103 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002104 (u8 *)&data_crc);
2105
2106 if (checksum != data_crc) {
2107 pr_err("Text data CRC32C DataDigest"
2108 " 0x%08x does not match computed"
2109 " 0x%08x\n", checksum, data_crc);
2110 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2111 pr_err("Unable to recover from"
2112 " Text Data digest failure while in"
2113 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002114 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002115 } else {
2116 /*
2117 * Silently drop this PDU and let the
2118 * initiator plug the CmdSN gap.
2119 */
2120 pr_debug("Dropping Text"
2121 " Command CmdSN: 0x%08x due to"
2122 " DataCRC error.\n", hdr->cmdsn);
2123 kfree(text_in);
2124 return 0;
2125 }
2126 } else {
2127 pr_debug("Got CRC32C DataDigest"
2128 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002129 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002130 }
2131 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002132 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002133 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002134 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002135 }
2136
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002137 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002138
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002139reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002140 kfree(cmd->text_in_ptr);
2141 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002142 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
2143 0, 0, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002144}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002145EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002146
2147int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2148{
2149 struct iscsi_conn *conn_p;
2150 struct iscsi_session *sess = conn->sess;
2151
2152 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2153 " for SID: %u.\n", conn->cid, conn->sess->sid);
2154
2155 atomic_set(&sess->session_logout, 1);
2156 atomic_set(&conn->conn_logout_remove, 1);
2157 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2158
2159 iscsit_inc_conn_usage_count(conn);
2160 iscsit_inc_session_usage_count(sess);
2161
2162 spin_lock_bh(&sess->conn_lock);
2163 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2164 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2165 continue;
2166
2167 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2168 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2169 }
2170 spin_unlock_bh(&sess->conn_lock);
2171
2172 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2173
2174 return 0;
2175}
2176
2177int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2178{
2179 struct iscsi_conn *l_conn;
2180 struct iscsi_session *sess = conn->sess;
2181
2182 pr_debug("Received logout request CLOSECONNECTION for CID:"
2183 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2184
2185 /*
2186 * A Logout Request with a CLOSECONNECTION reason code for a CID
2187 * can arrive on a connection with a differing CID.
2188 */
2189 if (conn->cid == cmd->logout_cid) {
2190 spin_lock_bh(&conn->state_lock);
2191 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2192 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2193
2194 atomic_set(&conn->conn_logout_remove, 1);
2195 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2196 iscsit_inc_conn_usage_count(conn);
2197
2198 spin_unlock_bh(&conn->state_lock);
2199 } else {
2200 /*
2201 * Handle all different cid CLOSECONNECTION requests in
2202 * iscsit_logout_post_handler_diffcid() as to give enough
2203 * time for any non immediate command's CmdSN to be
2204 * acknowledged on the connection in question.
2205 *
2206 * Here we simply make sure the CID is still around.
2207 */
2208 l_conn = iscsit_get_conn_from_cid(sess,
2209 cmd->logout_cid);
2210 if (!l_conn) {
2211 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2212 iscsit_add_cmd_to_response_queue(cmd, conn,
2213 cmd->i_state);
2214 return 0;
2215 }
2216
2217 iscsit_dec_conn_usage_count(l_conn);
2218 }
2219
2220 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2221
2222 return 0;
2223}
2224
2225int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2226{
2227 struct iscsi_session *sess = conn->sess;
2228
2229 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2230 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2231
2232 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2233 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2234 " while ERL!=2.\n");
2235 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2236 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2237 return 0;
2238 }
2239
2240 if (conn->cid == cmd->logout_cid) {
2241 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2242 " with CID: %hu on CID: %hu, implementation error.\n",
2243 cmd->logout_cid, conn->cid);
2244 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2245 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2246 return 0;
2247 }
2248
2249 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2250
2251 return 0;
2252}
2253
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002254int
2255iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2256 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002257{
2258 int cmdsn_ret, logout_remove = 0;
2259 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002260 struct iscsi_logout *hdr;
2261 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2262
2263 hdr = (struct iscsi_logout *) buf;
2264 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002265
2266 if (tiqn) {
2267 spin_lock(&tiqn->logout_stats.lock);
2268 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2269 tiqn->logout_stats.normal_logouts++;
2270 else
2271 tiqn->logout_stats.abnormal_logouts++;
2272 spin_unlock(&tiqn->logout_stats.lock);
2273 }
2274
2275 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2276 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2277 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2278 hdr->cid, conn->cid);
2279
2280 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2281 pr_err("Received logout request on connection that"
2282 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002283 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002284 return 0;
2285 }
2286
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002287 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2288 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2289 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2290 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2291 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002292 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2293 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2294 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002295 cmd->logout_reason = reason_code;
2296 cmd->data_direction = DMA_NONE;
2297
2298 /*
2299 * We need to sleep in these cases (by returning 1) until the Logout
2300 * Response gets sent in the tx thread.
2301 */
2302 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2303 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002304 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002305 logout_remove = 1;
2306
2307 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002308 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002309 spin_unlock_bh(&conn->cmd_lock);
2310
2311 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002312 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002313
2314 /*
2315 * Immediate commands are executed, well, immediately.
2316 * Non-Immediate Logout Commands are executed in CmdSN order.
2317 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002318 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002319 int ret = iscsit_execute_cmd(cmd, 0);
2320
2321 if (ret < 0)
2322 return ret;
2323 } else {
2324 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2325 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2326 logout_remove = 0;
2327 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2328 return iscsit_add_reject_from_cmd(
2329 ISCSI_REASON_PROTOCOL_ERROR,
2330 1, 0, buf, cmd);
2331 }
2332 }
2333
2334 return logout_remove;
2335}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002336EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002337
2338static int iscsit_handle_snack(
2339 struct iscsi_conn *conn,
2340 unsigned char *buf)
2341{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002342 struct iscsi_snack *hdr;
2343
2344 hdr = (struct iscsi_snack *) buf;
2345 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002346
2347 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2348 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2349 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2350 hdr->begrun, hdr->runlength, conn->cid);
2351
2352 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2353 pr_err("Initiator sent SNACK request while in"
2354 " ErrorRecoveryLevel=0.\n");
2355 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2356 buf, conn);
2357 }
2358 /*
2359 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2360 * call from inside iscsi_send_recovery_datain_or_r2t().
2361 */
2362 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2363 case 0:
2364 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002365 hdr->itt,
2366 be32_to_cpu(hdr->ttt),
2367 be32_to_cpu(hdr->begrun),
2368 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002369 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002370 return iscsit_handle_status_snack(conn, hdr->itt,
2371 be32_to_cpu(hdr->ttt),
2372 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002373 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002374 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2375 be32_to_cpu(hdr->begrun),
2376 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002377 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2378 /* FIXME: Support R-Data SNACK */
2379 pr_err("R-Data SNACK Not Supported.\n");
2380 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2381 buf, conn);
2382 default:
2383 pr_err("Unknown SNACK type 0x%02x, protocol"
2384 " error.\n", hdr->flags & 0x0f);
2385 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2386 buf, conn);
2387 }
2388
2389 return 0;
2390}
2391
2392static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2393{
2394 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2395 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2396 wait_for_completion_interruptible_timeout(
2397 &conn->rx_half_close_comp,
2398 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2399 }
2400}
2401
2402static int iscsit_handle_immediate_data(
2403 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002404 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002405 u32 length)
2406{
2407 int iov_ret, rx_got = 0, rx_size = 0;
2408 u32 checksum, iov_count = 0, padding = 0;
2409 struct iscsi_conn *conn = cmd->conn;
2410 struct kvec *iov;
2411
2412 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2413 if (iov_ret < 0)
2414 return IMMEDIATE_DATA_CANNOT_RECOVER;
2415
2416 rx_size = length;
2417 iov_count = iov_ret;
2418 iov = &cmd->iov_data[0];
2419
2420 padding = ((-length) & 3);
2421 if (padding != 0) {
2422 iov[iov_count].iov_base = cmd->pad_bytes;
2423 iov[iov_count++].iov_len = padding;
2424 rx_size += padding;
2425 }
2426
2427 if (conn->conn_ops->DataDigest) {
2428 iov[iov_count].iov_base = &checksum;
2429 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2430 rx_size += ISCSI_CRC_LEN;
2431 }
2432
2433 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2434
2435 iscsit_unmap_iovec(cmd);
2436
2437 if (rx_got != rx_size) {
2438 iscsit_rx_thread_wait_for_tcp(conn);
2439 return IMMEDIATE_DATA_CANNOT_RECOVER;
2440 }
2441
2442 if (conn->conn_ops->DataDigest) {
2443 u32 data_crc;
2444
2445 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2446 cmd->write_data_done, length, padding,
2447 cmd->pad_bytes);
2448
2449 if (checksum != data_crc) {
2450 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2451 " does not match computed 0x%08x\n", checksum,
2452 data_crc);
2453
2454 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2455 pr_err("Unable to recover from"
2456 " Immediate Data digest failure while"
2457 " in ERL=0.\n");
2458 iscsit_add_reject_from_cmd(
2459 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002460 1, 0, (unsigned char *)hdr, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002461 return IMMEDIATE_DATA_CANNOT_RECOVER;
2462 } else {
2463 iscsit_add_reject_from_cmd(
2464 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002465 0, 0, (unsigned char *)hdr, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002466 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2467 }
2468 } else {
2469 pr_debug("Got CRC32C DataDigest 0x%08x for"
2470 " %u bytes of Immediate Data\n", checksum,
2471 length);
2472 }
2473 }
2474
2475 cmd->write_data_done += length;
2476
Andy Groverebf1d952012-04-03 15:51:24 -07002477 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002478 spin_lock_bh(&cmd->istate_lock);
2479 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2480 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2481 spin_unlock_bh(&cmd->istate_lock);
2482 }
2483
2484 return IMMEDIATE_DATA_NORMAL_OPERATION;
2485}
2486
2487/*
2488 * Called with sess->conn_lock held.
2489 */
2490/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2491 with active network interface */
2492static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2493{
2494 struct iscsi_cmd *cmd;
2495 struct iscsi_conn *conn_p;
2496
2497 /*
2498 * Only send a Asynchronous Message on connections whos network
2499 * interface is still functional.
2500 */
2501 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2502 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2503 iscsit_inc_conn_usage_count(conn_p);
2504 break;
2505 }
2506 }
2507
2508 if (!conn_p)
2509 return;
2510
Wei Yongjun3c989d72012-11-23 12:07:39 +08002511 cmd = iscsit_allocate_cmd(conn_p, GFP_ATOMIC);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002512 if (!cmd) {
2513 iscsit_dec_conn_usage_count(conn_p);
2514 return;
2515 }
2516
2517 cmd->logout_cid = conn->cid;
2518 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2519 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2520
2521 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002522 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002523 spin_unlock_bh(&conn_p->cmd_lock);
2524
2525 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2526 iscsit_dec_conn_usage_count(conn_p);
2527}
2528
2529static int iscsit_send_conn_drop_async_message(
2530 struct iscsi_cmd *cmd,
2531 struct iscsi_conn *conn)
2532{
2533 struct iscsi_async *hdr;
2534
2535 cmd->tx_size = ISCSI_HDR_LEN;
2536 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2537
2538 hdr = (struct iscsi_async *) cmd->pdu;
2539 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2540 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002541 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002542 cmd->targ_xfer_tag = 0xFFFFFFFF;
2543 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2544 cmd->stat_sn = conn->stat_sn++;
2545 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2546 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2547 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2548 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2549 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2550 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2551 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2552
2553 if (conn->conn_ops->HeaderDigest) {
2554 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2555
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002556 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2557 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002558
2559 cmd->tx_size += ISCSI_CRC_LEN;
2560 pr_debug("Attaching CRC32C HeaderDigest to"
2561 " Async Message 0x%08x\n", *header_digest);
2562 }
2563
2564 cmd->iov_misc[0].iov_base = cmd->pdu;
2565 cmd->iov_misc[0].iov_len = cmd->tx_size;
2566 cmd->iov_misc_count = 1;
2567
2568 pr_debug("Sending Connection Dropped Async Message StatSN:"
2569 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2570 cmd->logout_cid, conn->cid);
2571 return 0;
2572}
2573
Andy Grover6f3c0e62012-04-03 15:51:09 -07002574static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2575{
2576 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2577 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2578 wait_for_completion_interruptible_timeout(
2579 &conn->tx_half_close_comp,
2580 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2581 }
2582}
2583
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002584static void
2585iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2586 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2587 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002588{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002589 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2590 hdr->flags = datain->flags;
2591 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2592 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2593 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2594 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2595 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2596 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2597 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2598 }
2599 }
2600 hton24(hdr->dlength, datain->length);
2601 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2602 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2603 (struct scsi_lun *)&hdr->lun);
2604 else
2605 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2606
2607 hdr->itt = cmd->init_task_tag;
2608
2609 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2610 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2611 else
2612 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2613 if (set_statsn)
2614 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2615 else
2616 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2617
2618 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2619 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2620 hdr->datasn = cpu_to_be32(datain->data_sn);
2621 hdr->offset = cpu_to_be32(datain->offset);
2622
2623 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2624 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2625 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2626 ntohl(hdr->offset), datain->length, conn->cid);
2627}
2628
2629static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2630{
2631 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002632 struct iscsi_datain datain;
2633 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002634 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002635 u32 iov_count = 0, tx_size = 0;
2636 int eodr = 0, ret, iov_ret;
2637 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002638
2639 memset(&datain, 0, sizeof(struct iscsi_datain));
2640 dr = iscsit_get_datain_values(cmd, &datain);
2641 if (!dr) {
2642 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2643 cmd->init_task_tag);
2644 return -1;
2645 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002646 /*
2647 * Be paranoid and double check the logic for now.
2648 */
Andy Groverebf1d952012-04-03 15:51:24 -07002649 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002650 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2651 " datain.length: %u exceeds cmd->data_length: %u\n",
2652 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002653 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002654 return -1;
2655 }
2656
2657 spin_lock_bh(&conn->sess->session_stats_lock);
2658 conn->sess->tx_data_octets += datain.length;
2659 if (conn->sess->se_sess->se_node_acl) {
2660 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2661 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2662 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2663 }
2664 spin_unlock_bh(&conn->sess->session_stats_lock);
2665 /*
2666 * Special case for successfully execution w/ both DATAIN
2667 * and Sense Data.
2668 */
2669 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2670 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2671 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2672 else {
2673 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2674 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2675 iscsit_increment_maxcmdsn(cmd, conn->sess);
2676 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002677 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002678 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002679 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2680 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002681 }
2682
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002683 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002684
2685 iov = &cmd->iov_data[0];
2686 iov[iov_count].iov_base = cmd->pdu;
2687 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2688 tx_size += ISCSI_HDR_LEN;
2689
2690 if (conn->conn_ops->HeaderDigest) {
2691 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2692
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002693 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2694 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002695
2696 iov[0].iov_len += ISCSI_CRC_LEN;
2697 tx_size += ISCSI_CRC_LEN;
2698
2699 pr_debug("Attaching CRC32 HeaderDigest"
2700 " for DataIN PDU 0x%08x\n", *header_digest);
2701 }
2702
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002703 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2704 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002705 if (iov_ret < 0)
2706 return -1;
2707
2708 iov_count += iov_ret;
2709 tx_size += datain.length;
2710
2711 cmd->padding = ((-datain.length) & 3);
2712 if (cmd->padding) {
2713 iov[iov_count].iov_base = cmd->pad_bytes;
2714 iov[iov_count++].iov_len = cmd->padding;
2715 tx_size += cmd->padding;
2716
2717 pr_debug("Attaching %u padding bytes\n",
2718 cmd->padding);
2719 }
2720 if (conn->conn_ops->DataDigest) {
2721 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2722 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2723
2724 iov[iov_count].iov_base = &cmd->data_crc;
2725 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2726 tx_size += ISCSI_CRC_LEN;
2727
2728 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2729 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2730 }
2731
2732 cmd->iov_data_count = iov_count;
2733 cmd->tx_size = tx_size;
2734
Andy Grover6f3c0e62012-04-03 15:51:09 -07002735 /* sendpage is preferred but can't insert markers */
2736 if (!conn->conn_ops->IFMarker)
2737 ret = iscsit_fe_sendpage_sg(cmd, conn);
2738 else
2739 ret = iscsit_send_tx_data(cmd, conn, 0);
2740
2741 iscsit_unmap_iovec(cmd);
2742
2743 if (ret < 0) {
2744 iscsit_tx_thread_wait_for_tcp(conn);
2745 return ret;
2746 }
2747
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002748 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002749 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002750 2 : 1;
2751 iscsit_free_datain_req(cmd, dr);
2752 }
2753
Andy Grover6f3c0e62012-04-03 15:51:09 -07002754 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002755}
2756
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002757int
2758iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2759 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002760{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002761 struct iscsi_conn *logout_conn = NULL;
2762 struct iscsi_conn_recovery *cr = NULL;
2763 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002764 /*
2765 * The actual shutting down of Sessions and/or Connections
2766 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2767 * is done in scsi_logout_post_handler().
2768 */
2769 switch (cmd->logout_reason) {
2770 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2771 pr_debug("iSCSI session logout successful, setting"
2772 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2773 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2774 break;
2775 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2776 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2777 break;
2778 /*
2779 * For CLOSECONNECTION logout requests carrying
2780 * a matching logout CID -> local CID, the reference
2781 * for the local CID will have been incremented in
2782 * iscsi_logout_closeconnection().
2783 *
2784 * For CLOSECONNECTION logout requests carrying
2785 * a different CID than the connection it arrived
2786 * on, the connection responding to cmd->logout_cid
2787 * is stopped in iscsit_logout_post_handler_diffcid().
2788 */
2789
2790 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2791 " successful.\n", cmd->logout_cid, conn->cid);
2792 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2793 break;
2794 case ISCSI_LOGOUT_REASON_RECOVERY:
2795 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2796 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2797 break;
2798 /*
2799 * If the connection is still active from our point of view
2800 * force connection recovery to occur.
2801 */
2802 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2803 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002804 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002805 iscsit_connection_reinstatement_rcfr(logout_conn);
2806 iscsit_dec_conn_usage_count(logout_conn);
2807 }
2808
2809 cr = iscsit_get_inactive_connection_recovery_entry(
2810 conn->sess, cmd->logout_cid);
2811 if (!cr) {
2812 pr_err("Unable to locate CID: %hu for"
2813 " REMOVECONNFORRECOVERY Logout Request.\n",
2814 cmd->logout_cid);
2815 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2816 break;
2817 }
2818
2819 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2820
2821 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2822 " for recovery for CID: %hu on CID: %hu successful.\n",
2823 cmd->logout_cid, conn->cid);
2824 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2825 break;
2826 default:
2827 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2828 cmd->logout_reason);
2829 return -1;
2830 }
2831
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002832 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2833 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2834 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002835 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002836 cmd->stat_sn = conn->stat_sn++;
2837 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2838
2839 iscsit_increment_maxcmdsn(cmd, conn->sess);
2840 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2841 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2842
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002843 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2844 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2845 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2846 cmd->logout_cid, conn->cid);
2847
2848 return 0;
2849}
2850EXPORT_SYMBOL(iscsit_build_logout_rsp);
2851
2852static int
2853iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2854{
2855 struct kvec *iov;
2856 int niov = 0, tx_size, rc;
2857
2858 rc = iscsit_build_logout_rsp(cmd, conn,
2859 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2860 if (rc < 0)
2861 return rc;
2862
2863 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002864 iov = &cmd->iov_misc[0];
2865 iov[niov].iov_base = cmd->pdu;
2866 iov[niov++].iov_len = ISCSI_HDR_LEN;
2867
2868 if (conn->conn_ops->HeaderDigest) {
2869 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2870
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002871 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2872 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002873
2874 iov[0].iov_len += ISCSI_CRC_LEN;
2875 tx_size += ISCSI_CRC_LEN;
2876 pr_debug("Attaching CRC32C HeaderDigest to"
2877 " Logout Response 0x%08x\n", *header_digest);
2878 }
2879 cmd->iov_misc_count = niov;
2880 cmd->tx_size = tx_size;
2881
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002882 return 0;
2883}
2884
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002885void
2886iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2887 struct iscsi_nopin *hdr, bool nopout_response)
2888{
2889 hdr->opcode = ISCSI_OP_NOOP_IN;
2890 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2891 hton24(hdr->dlength, cmd->buf_ptr_size);
2892 if (nopout_response)
2893 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2894 hdr->itt = cmd->init_task_tag;
2895 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2896 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2897 conn->stat_sn;
2898 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2899
2900 if (nopout_response)
2901 iscsit_increment_maxcmdsn(cmd, conn->sess);
2902
2903 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2904 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2905
2906 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2907 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2908 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2909 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2910}
2911EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2912
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002913/*
2914 * Unsolicited NOPIN, either requesting a response or not.
2915 */
2916static int iscsit_send_unsolicited_nopin(
2917 struct iscsi_cmd *cmd,
2918 struct iscsi_conn *conn,
2919 int want_response)
2920{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002921 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2922 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002923
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002924 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002925
2926 if (conn->conn_ops->HeaderDigest) {
2927 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2928
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002929 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2930 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002931
2932 tx_size += ISCSI_CRC_LEN;
2933 pr_debug("Attaching CRC32C HeaderDigest to"
2934 " NopIN 0x%08x\n", *header_digest);
2935 }
2936
2937 cmd->iov_misc[0].iov_base = cmd->pdu;
2938 cmd->iov_misc[0].iov_len = tx_size;
2939 cmd->iov_misc_count = 1;
2940 cmd->tx_size = tx_size;
2941
2942 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2943 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2944
Andy Grover6f3c0e62012-04-03 15:51:09 -07002945 ret = iscsit_send_tx_data(cmd, conn, 1);
2946 if (ret < 0) {
2947 iscsit_tx_thread_wait_for_tcp(conn);
2948 return ret;
2949 }
2950
2951 spin_lock_bh(&cmd->istate_lock);
2952 cmd->i_state = want_response ?
2953 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2954 spin_unlock_bh(&cmd->istate_lock);
2955
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002956 return 0;
2957}
2958
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002959static int
2960iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002961{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002962 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002963 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002964 u32 padding = 0;
2965 int niov = 0, tx_size;
2966
2967 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002968
2969 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002970 iov = &cmd->iov_misc[0];
2971 iov[niov].iov_base = cmd->pdu;
2972 iov[niov++].iov_len = ISCSI_HDR_LEN;
2973
2974 if (conn->conn_ops->HeaderDigest) {
2975 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2976
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002977 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2978 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002979
2980 iov[0].iov_len += ISCSI_CRC_LEN;
2981 tx_size += ISCSI_CRC_LEN;
2982 pr_debug("Attaching CRC32C HeaderDigest"
2983 " to NopIn 0x%08x\n", *header_digest);
2984 }
2985
2986 /*
2987 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2988 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2989 */
2990 if (cmd->buf_ptr_size) {
2991 iov[niov].iov_base = cmd->buf_ptr;
2992 iov[niov++].iov_len = cmd->buf_ptr_size;
2993 tx_size += cmd->buf_ptr_size;
2994
2995 pr_debug("Echoing back %u bytes of ping"
2996 " data.\n", cmd->buf_ptr_size);
2997
2998 padding = ((-cmd->buf_ptr_size) & 3);
2999 if (padding != 0) {
3000 iov[niov].iov_base = &cmd->pad_bytes;
3001 iov[niov++].iov_len = padding;
3002 tx_size += padding;
3003 pr_debug("Attaching %u additional"
3004 " padding bytes.\n", padding);
3005 }
3006 if (conn->conn_ops->DataDigest) {
3007 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3008 cmd->buf_ptr, cmd->buf_ptr_size,
3009 padding, (u8 *)&cmd->pad_bytes,
3010 (u8 *)&cmd->data_crc);
3011
3012 iov[niov].iov_base = &cmd->data_crc;
3013 iov[niov++].iov_len = ISCSI_CRC_LEN;
3014 tx_size += ISCSI_CRC_LEN;
3015 pr_debug("Attached DataDigest for %u"
3016 " bytes of ping data, CRC 0x%08x\n",
3017 cmd->buf_ptr_size, cmd->data_crc);
3018 }
3019 }
3020
3021 cmd->iov_misc_count = niov;
3022 cmd->tx_size = tx_size;
3023
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003024 return 0;
3025}
3026
Andy Grover6f3c0e62012-04-03 15:51:09 -07003027static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028 struct iscsi_cmd *cmd,
3029 struct iscsi_conn *conn)
3030{
3031 int tx_size = 0;
3032 struct iscsi_r2t *r2t;
3033 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003034 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003035
3036 r2t = iscsit_get_r2t_from_list(cmd);
3037 if (!r2t)
3038 return -1;
3039
3040 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3041 memset(hdr, 0, ISCSI_HDR_LEN);
3042 hdr->opcode = ISCSI_OP_R2T;
3043 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3044 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3045 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003046 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003047 spin_lock_bh(&conn->sess->ttt_lock);
3048 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3049 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3050 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3051 spin_unlock_bh(&conn->sess->ttt_lock);
3052 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3053 hdr->statsn = cpu_to_be32(conn->stat_sn);
3054 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3055 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3056 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3057 hdr->data_offset = cpu_to_be32(r2t->offset);
3058 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3059
3060 cmd->iov_misc[0].iov_base = cmd->pdu;
3061 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3062 tx_size += ISCSI_HDR_LEN;
3063
3064 if (conn->conn_ops->HeaderDigest) {
3065 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3066
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003067 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3068 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003069
3070 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3071 tx_size += ISCSI_CRC_LEN;
3072 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3073 " PDU 0x%08x\n", *header_digest);
3074 }
3075
3076 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3077 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3078 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3079 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3080 r2t->offset, r2t->xfer_len, conn->cid);
3081
3082 cmd->iov_misc_count = 1;
3083 cmd->tx_size = tx_size;
3084
3085 spin_lock_bh(&cmd->r2t_lock);
3086 r2t->sent_r2t = 1;
3087 spin_unlock_bh(&cmd->r2t_lock);
3088
Andy Grover6f3c0e62012-04-03 15:51:09 -07003089 ret = iscsit_send_tx_data(cmd, conn, 1);
3090 if (ret < 0) {
3091 iscsit_tx_thread_wait_for_tcp(conn);
3092 return ret;
3093 }
3094
3095 spin_lock_bh(&cmd->dataout_timeout_lock);
3096 iscsit_start_dataout_timer(cmd, conn);
3097 spin_unlock_bh(&cmd->dataout_timeout_lock);
3098
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003099 return 0;
3100}
3101
3102/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003103 * @recovery: If called from iscsi_task_reassign_complete_write() for
3104 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003105 */
3106int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003107 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003108 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003109 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003110{
3111 int first_r2t = 1;
3112 u32 offset = 0, xfer_len = 0;
3113
3114 spin_lock_bh(&cmd->r2t_lock);
3115 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3116 spin_unlock_bh(&cmd->r2t_lock);
3117 return 0;
3118 }
3119
Andy Grover8b1e1242012-04-03 15:51:12 -07003120 if (conn->sess->sess_ops->DataSequenceInOrder &&
3121 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003122 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003123
3124 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3125 if (conn->sess->sess_ops->DataSequenceInOrder) {
3126 offset = cmd->r2t_offset;
3127
Andy Grover8b1e1242012-04-03 15:51:12 -07003128 if (first_r2t && recovery) {
3129 int new_data_end = offset +
3130 conn->sess->sess_ops->MaxBurstLength -
3131 cmd->next_burst_len;
3132
Andy Groverebf1d952012-04-03 15:51:24 -07003133 if (new_data_end > cmd->se_cmd.data_length)
3134 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003135 else
3136 xfer_len =
3137 conn->sess->sess_ops->MaxBurstLength -
3138 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003139 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003140 int new_data_end = offset +
3141 conn->sess->sess_ops->MaxBurstLength;
3142
Andy Groverebf1d952012-04-03 15:51:24 -07003143 if (new_data_end > cmd->se_cmd.data_length)
3144 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003145 else
3146 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003147 }
3148 cmd->r2t_offset += xfer_len;
3149
Andy Groverebf1d952012-04-03 15:51:24 -07003150 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003151 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3152 } else {
3153 struct iscsi_seq *seq;
3154
3155 seq = iscsit_get_seq_holder_for_r2t(cmd);
3156 if (!seq) {
3157 spin_unlock_bh(&cmd->r2t_lock);
3158 return -1;
3159 }
3160
3161 offset = seq->offset;
3162 xfer_len = seq->xfer_len;
3163
3164 if (cmd->seq_send_order == cmd->seq_count)
3165 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3166 }
3167 cmd->outstanding_r2ts++;
3168 first_r2t = 0;
3169
3170 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3171 spin_unlock_bh(&cmd->r2t_lock);
3172 return -1;
3173 }
3174
3175 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3176 break;
3177 }
3178 spin_unlock_bh(&cmd->r2t_lock);
3179
3180 return 0;
3181}
3182
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003183void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3184 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003185{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003186 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003187 cmd->stat_sn = conn->stat_sn++;
3188
3189 spin_lock_bh(&conn->sess->session_stats_lock);
3190 conn->sess->rsp_pdus++;
3191 spin_unlock_bh(&conn->sess->session_stats_lock);
3192
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003193 memset(hdr, 0, ISCSI_HDR_LEN);
3194 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3195 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3196 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3197 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003198 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003199 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3200 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003201 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003202 }
3203 hdr->response = cmd->iscsi_response;
3204 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003205 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003206 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3207
3208 iscsit_increment_maxcmdsn(cmd, conn->sess);
3209 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3210 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3211
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003212 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3213 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3214 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3215 cmd->se_cmd.scsi_status, conn->cid);
3216}
3217EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3218
3219static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3220{
3221 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3222 struct kvec *iov;
3223 u32 padding = 0, tx_size = 0;
3224 int iov_count = 0;
3225 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3226
3227 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3228
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003229 iov = &cmd->iov_misc[0];
3230 iov[iov_count].iov_base = cmd->pdu;
3231 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3232 tx_size += ISCSI_HDR_LEN;
3233
3234 /*
3235 * Attach SENSE DATA payload to iSCSI Response PDU
3236 */
3237 if (cmd->se_cmd.sense_buffer &&
3238 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3239 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003240 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3241 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3242
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003243 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003244 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003245 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003246 iov[iov_count++].iov_len =
3247 (cmd->se_cmd.scsi_sense_length + padding);
3248 tx_size += cmd->se_cmd.scsi_sense_length;
3249
3250 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003251 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003252 cmd->se_cmd.scsi_sense_length, 0, padding);
3253 tx_size += padding;
3254 pr_debug("Adding %u bytes of padding to"
3255 " SENSE.\n", padding);
3256 }
3257
3258 if (conn->conn_ops->DataDigest) {
3259 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003260 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003261 (cmd->se_cmd.scsi_sense_length + padding),
3262 0, NULL, (u8 *)&cmd->data_crc);
3263
3264 iov[iov_count].iov_base = &cmd->data_crc;
3265 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3266 tx_size += ISCSI_CRC_LEN;
3267
3268 pr_debug("Attaching CRC32 DataDigest for"
3269 " SENSE, %u bytes CRC 0x%08x\n",
3270 (cmd->se_cmd.scsi_sense_length + padding),
3271 cmd->data_crc);
3272 }
3273
3274 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3275 " Response PDU\n",
3276 cmd->se_cmd.scsi_sense_length);
3277 }
3278
3279 if (conn->conn_ops->HeaderDigest) {
3280 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3281
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003282 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3283 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003284
3285 iov[0].iov_len += ISCSI_CRC_LEN;
3286 tx_size += ISCSI_CRC_LEN;
3287 pr_debug("Attaching CRC32 HeaderDigest for Response"
3288 " PDU 0x%08x\n", *header_digest);
3289 }
3290
3291 cmd->iov_misc_count = iov_count;
3292 cmd->tx_size = tx_size;
3293
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003294 return 0;
3295}
3296
3297static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3298{
3299 switch (se_tmr->response) {
3300 case TMR_FUNCTION_COMPLETE:
3301 return ISCSI_TMF_RSP_COMPLETE;
3302 case TMR_TASK_DOES_NOT_EXIST:
3303 return ISCSI_TMF_RSP_NO_TASK;
3304 case TMR_LUN_DOES_NOT_EXIST:
3305 return ISCSI_TMF_RSP_NO_LUN;
3306 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3307 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3308 case TMR_FUNCTION_AUTHORIZATION_FAILED:
3309 return ISCSI_TMF_RSP_AUTH_FAILED;
3310 case TMR_FUNCTION_REJECTED:
3311 default:
3312 return ISCSI_TMF_RSP_REJECTED;
3313 }
3314}
3315
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003316void
3317iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3318 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003319{
3320 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003321
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003322 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003323 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003324 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003325 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003326 cmd->stat_sn = conn->stat_sn++;
3327 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3328
3329 iscsit_increment_maxcmdsn(cmd, conn->sess);
3330 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3331 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3332
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003333 pr_debug("Built Task Management Response ITT: 0x%08x,"
3334 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3335 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3336}
3337EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3338
3339static int
3340iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3341{
3342 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3343 u32 tx_size = 0;
3344
3345 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3346
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003347 cmd->iov_misc[0].iov_base = cmd->pdu;
3348 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3349 tx_size += ISCSI_HDR_LEN;
3350
3351 if (conn->conn_ops->HeaderDigest) {
3352 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3353
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003354 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3355 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003356
3357 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3358 tx_size += ISCSI_CRC_LEN;
3359 pr_debug("Attaching CRC32 HeaderDigest for Task"
3360 " Mgmt Response PDU 0x%08x\n", *header_digest);
3361 }
3362
3363 cmd->iov_misc_count = 1;
3364 cmd->tx_size = tx_size;
3365
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003366 return 0;
3367}
3368
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003369static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3370{
3371 bool ret = false;
3372
3373 if (np->np_sockaddr.ss_family == AF_INET6) {
3374 const struct sockaddr_in6 sin6 = {
3375 .sin6_addr = IN6ADDR_ANY_INIT };
3376 struct sockaddr_in6 *sock_in6 =
3377 (struct sockaddr_in6 *)&np->np_sockaddr;
3378
3379 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3380 sin6.sin6_addr.s6_addr, 16))
3381 ret = true;
3382 } else {
3383 struct sockaddr_in * sock_in =
3384 (struct sockaddr_in *)&np->np_sockaddr;
3385
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003386 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003387 ret = true;
3388 }
3389
3390 return ret;
3391}
3392
Andy Grover8b1e1242012-04-03 15:51:12 -07003393#define SENDTARGETS_BUF_LIMIT 32768U
3394
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003395static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3396{
3397 char *payload = NULL;
3398 struct iscsi_conn *conn = cmd->conn;
3399 struct iscsi_portal_group *tpg;
3400 struct iscsi_tiqn *tiqn;
3401 struct iscsi_tpg_np *tpg_np;
3402 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003403 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003404 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003405
Andy Grover8b1e1242012-04-03 15:51:12 -07003406 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3407 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003408
3409 payload = kzalloc(buffer_len, GFP_KERNEL);
3410 if (!payload) {
3411 pr_err("Unable to allocate memory for sendtargets"
3412 " response.\n");
3413 return -ENOMEM;
3414 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003415 /*
3416 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3417 * explicit case..
3418 */
3419 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) {
3420 text_ptr = strchr(text_in, '=');
3421 if (!text_ptr) {
3422 pr_err("Unable to locate '=' string in text_in:"
3423 " %s\n", text_in);
3424 return -EINVAL;
3425 }
3426 /*
3427 * Skip over '=' character..
3428 */
3429 text_ptr += 1;
3430 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003431
3432 spin_lock(&tiqn_lock);
3433 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003434 if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) &&
3435 strcmp(tiqn->tiqn, text_ptr)) {
3436 continue;
3437 }
3438
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003439 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3440 len += 1;
3441
3442 if ((len + payload_len) > buffer_len) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003443 end_of_buf = 1;
3444 goto eob;
3445 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003446 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003447 payload_len += len;
3448
3449 spin_lock(&tiqn->tiqn_tpg_lock);
3450 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3451
3452 spin_lock(&tpg->tpg_state_lock);
3453 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3454 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3455 spin_unlock(&tpg->tpg_state_lock);
3456 continue;
3457 }
3458 spin_unlock(&tpg->tpg_state_lock);
3459
3460 spin_lock(&tpg->tpg_np_lock);
3461 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3462 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003463 struct iscsi_np *np = tpg_np->tpg_np;
3464 bool inaddr_any = iscsit_check_inaddr_any(np);
3465
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003466 len = sprintf(buf, "TargetAddress="
3467 "%s%s%s:%hu,%hu",
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003468 (np->np_sockaddr.ss_family == AF_INET6) ?
3469 "[" : "", (inaddr_any == false) ?
3470 np->np_ip : conn->local_ip,
3471 (np->np_sockaddr.ss_family == AF_INET6) ?
3472 "]" : "", (inaddr_any == false) ?
3473 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003474 tpg->tpgt);
3475 len += 1;
3476
3477 if ((len + payload_len) > buffer_len) {
3478 spin_unlock(&tpg->tpg_np_lock);
3479 spin_unlock(&tiqn->tiqn_tpg_lock);
3480 end_of_buf = 1;
3481 goto eob;
3482 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003483 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003484 payload_len += len;
3485 }
3486 spin_unlock(&tpg->tpg_np_lock);
3487 }
3488 spin_unlock(&tiqn->tiqn_tpg_lock);
3489eob:
3490 if (end_of_buf)
3491 break;
Nicholas Bellinger66658892013-06-19 22:45:42 -07003492
3493 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE)
3494 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003495 }
3496 spin_unlock(&tiqn_lock);
3497
3498 cmd->buf_ptr = payload;
3499
3500 return payload_len;
3501}
3502
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003503int
3504iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3505 struct iscsi_text_rsp *hdr)
3506{
3507 int text_length, padding;
3508
3509 text_length = iscsit_build_sendtargets_response(cmd);
3510 if (text_length < 0)
3511 return text_length;
3512
3513 hdr->opcode = ISCSI_OP_TEXT_RSP;
3514 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3515 padding = ((-text_length) & 3);
3516 hton24(hdr->dlength, text_length);
3517 hdr->itt = cmd->init_task_tag;
3518 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3519 cmd->stat_sn = conn->stat_sn++;
3520 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3521
3522 iscsit_increment_maxcmdsn(cmd, conn->sess);
3523 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3524 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3525
3526 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3527 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3528 text_length, conn->cid);
3529
3530 return text_length + padding;
3531}
3532EXPORT_SYMBOL(iscsit_build_text_rsp);
3533
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003534/*
3535 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3536 * MaxRecvDataSegmentLength.
3537 */
3538static int iscsit_send_text_rsp(
3539 struct iscsi_cmd *cmd,
3540 struct iscsi_conn *conn)
3541{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003542 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003543 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003544 u32 tx_size = 0;
3545 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003546
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003547 rc = iscsit_build_text_rsp(cmd, conn, hdr);
3548 if (rc < 0)
3549 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003550
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003551 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003552 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003553 iov[iov_count].iov_base = cmd->pdu;
3554 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3555 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003556 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003557
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003558 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003559
3560 if (conn->conn_ops->HeaderDigest) {
3561 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3562
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003563 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3564 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003565
3566 iov[0].iov_len += ISCSI_CRC_LEN;
3567 tx_size += ISCSI_CRC_LEN;
3568 pr_debug("Attaching CRC32 HeaderDigest for"
3569 " Text Response PDU 0x%08x\n", *header_digest);
3570 }
3571
3572 if (conn->conn_ops->DataDigest) {
3573 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003574 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003575 0, NULL, (u8 *)&cmd->data_crc);
3576
3577 iov[iov_count].iov_base = &cmd->data_crc;
3578 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3579 tx_size += ISCSI_CRC_LEN;
3580
3581 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003582 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003583 cmd->data_crc);
3584 }
3585
3586 cmd->iov_misc_count = iov_count;
3587 cmd->tx_size = tx_size;
3588
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003589 return 0;
3590}
3591
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003592void
3593iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3594 struct iscsi_reject *hdr)
3595{
3596 hdr->opcode = ISCSI_OP_REJECT;
3597 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3598 hton24(hdr->dlength, ISCSI_HDR_LEN);
3599 hdr->ffffffff = cpu_to_be32(0xffffffff);
3600 cmd->stat_sn = conn->stat_sn++;
3601 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3602 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3603 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3604
3605}
3606EXPORT_SYMBOL(iscsit_build_reject);
3607
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003608static int iscsit_send_reject(
3609 struct iscsi_cmd *cmd,
3610 struct iscsi_conn *conn)
3611{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003612 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003613 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003614 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003615
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003616 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003617
3618 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003619 iov[iov_count].iov_base = cmd->pdu;
3620 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3621 iov[iov_count].iov_base = cmd->buf_ptr;
3622 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3623
3624 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3625
3626 if (conn->conn_ops->HeaderDigest) {
3627 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3628
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003629 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3630 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003631
3632 iov[0].iov_len += ISCSI_CRC_LEN;
3633 tx_size += ISCSI_CRC_LEN;
3634 pr_debug("Attaching CRC32 HeaderDigest for"
3635 " REJECT PDU 0x%08x\n", *header_digest);
3636 }
3637
3638 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003639 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3640 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003641
3642 iov[iov_count].iov_base = &cmd->data_crc;
3643 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3644 tx_size += ISCSI_CRC_LEN;
3645 pr_debug("Attaching CRC32 DataDigest for REJECT"
3646 " PDU 0x%08x\n", cmd->data_crc);
3647 }
3648
3649 cmd->iov_misc_count = iov_count;
3650 cmd->tx_size = tx_size;
3651
3652 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3653 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3654
3655 return 0;
3656}
3657
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003658void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3659{
3660 struct iscsi_thread_set *ts = conn->thread_set;
3661 int ord, cpu;
3662 /*
3663 * thread_id is assigned from iscsit_global->ts_bitmap from
3664 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3665 *
3666 * Here we use thread_id to determine which CPU that this
3667 * iSCSI connection's iscsi_thread_set will be scheduled to
3668 * execute upon.
3669 */
3670 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003671 for_each_online_cpu(cpu) {
3672 if (ord-- == 0) {
3673 cpumask_set_cpu(cpu, conn->conn_cpumask);
3674 return;
3675 }
3676 }
3677 /*
3678 * This should never be reached..
3679 */
3680 dump_stack();
3681 cpumask_setall(conn->conn_cpumask);
3682}
3683
3684static inline void iscsit_thread_check_cpumask(
3685 struct iscsi_conn *conn,
3686 struct task_struct *p,
3687 int mode)
3688{
3689 char buf[128];
3690 /*
3691 * mode == 1 signals iscsi_target_tx_thread() usage.
3692 * mode == 0 signals iscsi_target_rx_thread() usage.
3693 */
3694 if (mode == 1) {
3695 if (!conn->conn_tx_reset_cpumask)
3696 return;
3697 conn->conn_tx_reset_cpumask = 0;
3698 } else {
3699 if (!conn->conn_rx_reset_cpumask)
3700 return;
3701 conn->conn_rx_reset_cpumask = 0;
3702 }
3703 /*
3704 * Update the CPU mask for this single kthread so that
3705 * both TX and RX kthreads are scheduled to run on the
3706 * same CPU.
3707 */
3708 memset(buf, 0, 128);
3709 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003710 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3711}
3712
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003713static int
3714iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003715{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003716 int ret;
3717
3718 switch (state) {
3719 case ISTATE_SEND_R2T:
3720 ret = iscsit_send_r2t(cmd, conn);
3721 if (ret < 0)
3722 goto err;
3723 break;
3724 case ISTATE_REMOVE:
3725 spin_lock_bh(&conn->cmd_lock);
3726 list_del(&cmd->i_conn_node);
3727 spin_unlock_bh(&conn->cmd_lock);
3728
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003729 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003730 break;
3731 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3732 iscsit_mod_nopin_response_timer(conn);
3733 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3734 if (ret < 0)
3735 goto err;
3736 break;
3737 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3738 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3739 if (ret < 0)
3740 goto err;
3741 break;
3742 default:
3743 pr_err("Unknown Opcode: 0x%02x ITT:"
3744 " 0x%08x, i_state: %d on CID: %hu\n",
3745 cmd->iscsi_opcode, cmd->init_task_tag, state,
3746 conn->cid);
3747 goto err;
3748 }
3749
3750 return 0;
3751
3752err:
3753 return -1;
3754}
3755
3756static int
3757iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3758{
3759 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003760 struct iscsi_queue_req *qr;
3761 struct iscsi_cmd *cmd;
3762 u8 state;
3763 int ret;
3764
3765 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3766 atomic_set(&conn->check_immediate_queue, 0);
3767 cmd = qr->cmd;
3768 state = qr->state;
3769 kmem_cache_free(lio_qr_cache, qr);
3770
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003771 ret = t->iscsit_immediate_queue(conn, cmd, state);
3772 if (ret < 0)
3773 return ret;
3774 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003775
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003776 return 0;
3777}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003778
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003779static int
3780iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3781{
3782 int ret;
3783
3784check_rsp_state:
3785 switch (state) {
3786 case ISTATE_SEND_DATAIN:
3787 ret = iscsit_send_datain(cmd, conn);
3788 if (ret < 0)
3789 goto err;
3790 else if (!ret)
3791 /* more drs */
3792 goto check_rsp_state;
3793 else if (ret == 1) {
3794 /* all done */
3795 spin_lock_bh(&cmd->istate_lock);
3796 cmd->i_state = ISTATE_SENT_STATUS;
3797 spin_unlock_bh(&cmd->istate_lock);
3798
3799 if (atomic_read(&conn->check_immediate_queue))
3800 return 1;
3801
3802 return 0;
3803 } else if (ret == 2) {
3804 /* Still must send status,
3805 SCF_TRANSPORT_TASK_SENSE was set */
3806 spin_lock_bh(&cmd->istate_lock);
3807 cmd->i_state = ISTATE_SEND_STATUS;
3808 spin_unlock_bh(&cmd->istate_lock);
3809 state = ISTATE_SEND_STATUS;
3810 goto check_rsp_state;
3811 }
3812
3813 break;
3814 case ISTATE_SEND_STATUS:
3815 case ISTATE_SEND_STATUS_RECOVERY:
3816 ret = iscsit_send_response(cmd, conn);
3817 break;
3818 case ISTATE_SEND_LOGOUTRSP:
3819 ret = iscsit_send_logout(cmd, conn);
3820 break;
3821 case ISTATE_SEND_ASYNCMSG:
3822 ret = iscsit_send_conn_drop_async_message(
3823 cmd, conn);
3824 break;
3825 case ISTATE_SEND_NOPIN:
3826 ret = iscsit_send_nopin(cmd, conn);
3827 break;
3828 case ISTATE_SEND_REJECT:
3829 ret = iscsit_send_reject(cmd, conn);
3830 break;
3831 case ISTATE_SEND_TASKMGTRSP:
3832 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3833 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003834 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003835 ret = iscsit_tmr_post_handler(cmd, conn);
3836 if (ret != 0)
3837 iscsit_fall_back_to_erl0(conn->sess);
3838 break;
3839 case ISTATE_SEND_TEXTRSP:
3840 ret = iscsit_send_text_rsp(cmd, conn);
3841 break;
3842 default:
3843 pr_err("Unknown Opcode: 0x%02x ITT:"
3844 " 0x%08x, i_state: %d on CID: %hu\n",
3845 cmd->iscsi_opcode, cmd->init_task_tag,
3846 state, conn->cid);
3847 goto err;
3848 }
3849 if (ret < 0)
3850 goto err;
3851
3852 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3853 iscsit_tx_thread_wait_for_tcp(conn);
3854 iscsit_unmap_iovec(cmd);
3855 goto err;
3856 }
3857 iscsit_unmap_iovec(cmd);
3858
3859 switch (state) {
3860 case ISTATE_SEND_LOGOUTRSP:
3861 if (!iscsit_logout_post_handler(cmd, conn))
3862 goto restart;
3863 /* fall through */
3864 case ISTATE_SEND_STATUS:
3865 case ISTATE_SEND_ASYNCMSG:
3866 case ISTATE_SEND_NOPIN:
3867 case ISTATE_SEND_STATUS_RECOVERY:
3868 case ISTATE_SEND_TEXTRSP:
3869 case ISTATE_SEND_TASKMGTRSP:
3870 spin_lock_bh(&cmd->istate_lock);
3871 cmd->i_state = ISTATE_SENT_STATUS;
3872 spin_unlock_bh(&cmd->istate_lock);
3873 break;
3874 case ISTATE_SEND_REJECT:
3875 if (cmd->cmd_flags & ICF_REJECT_FAIL_CONN) {
3876 cmd->cmd_flags &= ~ICF_REJECT_FAIL_CONN;
3877 complete(&cmd->reject_comp);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003878 goto err;
3879 }
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003880 complete(&cmd->reject_comp);
3881 break;
3882 default:
3883 pr_err("Unknown Opcode: 0x%02x ITT:"
3884 " 0x%08x, i_state: %d on CID: %hu\n",
3885 cmd->iscsi_opcode, cmd->init_task_tag,
3886 cmd->i_state, conn->cid);
3887 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003888 }
3889
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003890 if (atomic_read(&conn->check_immediate_queue))
3891 return 1;
3892
Andy Grover6f3c0e62012-04-03 15:51:09 -07003893 return 0;
3894
3895err:
3896 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003897restart:
3898 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003899}
3900
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003901static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003902{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003903 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003904 struct iscsi_queue_req *qr;
3905 struct iscsi_cmd *cmd;
3906 u8 state;
3907 int ret;
3908
3909 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3910 cmd = qr->cmd;
3911 state = qr->state;
3912 kmem_cache_free(lio_qr_cache, qr);
3913
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003914 ret = t->iscsit_response_queue(conn, cmd, state);
3915 if (ret == 1 || ret < 0)
3916 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003917 }
3918
3919 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003920}
3921
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003922int iscsi_target_tx_thread(void *arg)
3923{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003924 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003925 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003926 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003927 /*
3928 * Allow ourselves to be interrupted by SIGINT so that a
3929 * connection recovery / failure event can be triggered externally.
3930 */
3931 allow_signal(SIGINT);
3932
3933restart:
3934 conn = iscsi_tx_thread_pre_handler(ts);
3935 if (!conn)
3936 goto out;
3937
Andy Grover6f3c0e62012-04-03 15:51:09 -07003938 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003939
3940 while (!kthread_should_stop()) {
3941 /*
3942 * Ensure that both TX and RX per connection kthreads
3943 * are scheduled to run on the same CPU.
3944 */
3945 iscsit_thread_check_cpumask(conn, current, 1);
3946
Roland Dreierd5627ac2012-10-31 09:16:46 -07003947 wait_event_interruptible(conn->queues_wq,
3948 !iscsit_conn_all_queues_empty(conn) ||
3949 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003950
3951 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3952 signal_pending(current))
3953 goto transport_err;
3954
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003955get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003956 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003957 if (ret < 0)
3958 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003959
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003960 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003961 if (ret == 1)
3962 goto get_immediate;
3963 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003964 goto restart;
3965 else if (ret < 0)
3966 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003967 }
3968
3969transport_err:
3970 iscsit_take_action_for_connection_exit(conn);
3971 goto restart;
3972out:
3973 return 0;
3974}
3975
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003976static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3977{
3978 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3979 struct iscsi_cmd *cmd;
3980 int ret = 0;
3981
3982 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3983 case ISCSI_OP_SCSI_CMD:
3984 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3985 if (!cmd)
3986 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
3987 1, buf, conn);
3988
3989 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3990 break;
3991 case ISCSI_OP_SCSI_DATA_OUT:
3992 ret = iscsit_handle_data_out(conn, buf);
3993 break;
3994 case ISCSI_OP_NOOP_OUT:
3995 cmd = NULL;
3996 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3997 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3998 if (!cmd)
3999 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
4000 1, buf, conn);
4001 }
4002 ret = iscsit_handle_nop_out(conn, cmd, buf);
4003 break;
4004 case ISCSI_OP_SCSI_TMFUNC:
4005 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
4006 if (!cmd)
4007 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
4008 1, buf, conn);
4009
4010 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
4011 break;
4012 case ISCSI_OP_TEXT:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004013 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
4014 if (!cmd)
4015 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
4016 1, buf, conn);
4017
4018 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004019 break;
4020 case ISCSI_OP_LOGOUT:
4021 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
4022 if (!cmd)
4023 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
4024 1, buf, conn);
4025
4026 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4027 if (ret > 0)
4028 wait_for_completion_timeout(&conn->conn_logout_comp,
4029 SECONDS_FOR_LOGOUT_COMP * HZ);
4030 break;
4031 case ISCSI_OP_SNACK:
4032 ret = iscsit_handle_snack(conn, buf);
4033 break;
4034 default:
4035 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4036 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4037 pr_err("Cannot recover from unknown"
4038 " opcode while ERL=0, closing iSCSI connection.\n");
4039 return -1;
4040 }
4041 if (!conn->conn_ops->OFMarker) {
4042 pr_err("Unable to recover from unknown"
4043 " opcode while OFMarker=No, closing iSCSI"
4044 " connection.\n");
4045 return -1;
4046 }
4047 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4048 pr_err("Unable to recover from unknown"
4049 " opcode, closing iSCSI connection.\n");
4050 return -1;
4051 }
4052 break;
4053 }
4054
4055 return ret;
4056}
4057
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004058int iscsi_target_rx_thread(void *arg)
4059{
4060 int ret;
4061 u8 buffer[ISCSI_HDR_LEN], opcode;
4062 u32 checksum = 0, digest = 0;
4063 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004064 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004065 struct kvec iov;
4066 /*
4067 * Allow ourselves to be interrupted by SIGINT so that a
4068 * connection recovery / failure event can be triggered externally.
4069 */
4070 allow_signal(SIGINT);
4071
4072restart:
4073 conn = iscsi_rx_thread_pre_handler(ts);
4074 if (!conn)
4075 goto out;
4076
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004077 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4078 struct completion comp;
4079 int rc;
4080
4081 init_completion(&comp);
4082 rc = wait_for_completion_interruptible(&comp);
4083 if (rc < 0)
4084 goto transport_err;
4085
4086 goto out;
4087 }
4088
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004089 while (!kthread_should_stop()) {
4090 /*
4091 * Ensure that both TX and RX per connection kthreads
4092 * are scheduled to run on the same CPU.
4093 */
4094 iscsit_thread_check_cpumask(conn, current, 0);
4095
4096 memset(buffer, 0, ISCSI_HDR_LEN);
4097 memset(&iov, 0, sizeof(struct kvec));
4098
4099 iov.iov_base = buffer;
4100 iov.iov_len = ISCSI_HDR_LEN;
4101
4102 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4103 if (ret != ISCSI_HDR_LEN) {
4104 iscsit_rx_thread_wait_for_tcp(conn);
4105 goto transport_err;
4106 }
4107
4108 /*
4109 * Set conn->bad_hdr for use with REJECT PDUs.
4110 */
4111 memcpy(&conn->bad_hdr, &buffer, ISCSI_HDR_LEN);
4112
4113 if (conn->conn_ops->HeaderDigest) {
4114 iov.iov_base = &digest;
4115 iov.iov_len = ISCSI_CRC_LEN;
4116
4117 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4118 if (ret != ISCSI_CRC_LEN) {
4119 iscsit_rx_thread_wait_for_tcp(conn);
4120 goto transport_err;
4121 }
4122
4123 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4124 buffer, ISCSI_HDR_LEN,
4125 0, NULL, (u8 *)&checksum);
4126
4127 if (digest != checksum) {
4128 pr_err("HeaderDigest CRC32C failed,"
4129 " received 0x%08x, computed 0x%08x\n",
4130 digest, checksum);
4131 /*
4132 * Set the PDU to 0xff so it will intentionally
4133 * hit default in the switch below.
4134 */
4135 memset(buffer, 0xff, ISCSI_HDR_LEN);
4136 spin_lock_bh(&conn->sess->session_stats_lock);
4137 conn->sess->conn_digest_errors++;
4138 spin_unlock_bh(&conn->sess->session_stats_lock);
4139 } else {
4140 pr_debug("Got HeaderDigest CRC32C"
4141 " 0x%08x\n", checksum);
4142 }
4143 }
4144
4145 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4146 goto transport_err;
4147
4148 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4149
4150 if (conn->sess->sess_ops->SessionType &&
4151 ((!(opcode & ISCSI_OP_TEXT)) ||
4152 (!(opcode & ISCSI_OP_LOGOUT)))) {
4153 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4154 " while in Discovery Session, rejecting.\n", opcode);
4155 iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
4156 buffer, conn);
4157 goto transport_err;
4158 }
4159
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004160 ret = iscsi_target_rx_opcode(conn, buffer);
4161 if (ret < 0)
4162 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004163 }
4164
4165transport_err:
4166 if (!signal_pending(current))
4167 atomic_set(&conn->transport_failed, 1);
4168 iscsit_take_action_for_connection_exit(conn);
4169 goto restart;
4170out:
4171 return 0;
4172}
4173
4174static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4175{
4176 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4177 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004178 /*
4179 * We expect this function to only ever be called from either RX or TX
4180 * thread context via iscsit_close_connection() once the other context
4181 * has been reset -> returned sleeping pre-handler state.
4182 */
4183 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004184 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004185
Andy Grover2fbb4712012-04-03 15:51:01 -07004186 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004187 spin_unlock_bh(&conn->cmd_lock);
4188
4189 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004190
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004191 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004192
4193 spin_lock_bh(&conn->cmd_lock);
4194 }
4195 spin_unlock_bh(&conn->cmd_lock);
4196}
4197
4198static void iscsit_stop_timers_for_cmds(
4199 struct iscsi_conn *conn)
4200{
4201 struct iscsi_cmd *cmd;
4202
4203 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004204 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004205 if (cmd->data_direction == DMA_TO_DEVICE)
4206 iscsit_stop_dataout_timer(cmd);
4207 }
4208 spin_unlock_bh(&conn->cmd_lock);
4209}
4210
4211int iscsit_close_connection(
4212 struct iscsi_conn *conn)
4213{
4214 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4215 struct iscsi_session *sess = conn->sess;
4216
4217 pr_debug("Closing iSCSI connection CID %hu on SID:"
4218 " %u\n", conn->cid, sess->sid);
4219 /*
4220 * Always up conn_logout_comp just in case the RX Thread is sleeping
4221 * and the logout response never got sent because the connection
4222 * failed.
4223 */
4224 complete(&conn->conn_logout_comp);
4225
4226 iscsi_release_thread_set(conn);
4227
4228 iscsit_stop_timers_for_cmds(conn);
4229 iscsit_stop_nopin_response_timer(conn);
4230 iscsit_stop_nopin_timer(conn);
4231 iscsit_free_queue_reqs_for_conn(conn);
4232
4233 /*
4234 * During Connection recovery drop unacknowledged out of order
4235 * commands for this connection, and prepare the other commands
4236 * for realligence.
4237 *
4238 * During normal operation clear the out of order commands (but
4239 * do not free the struct iscsi_ooo_cmdsn's) and release all
4240 * struct iscsi_cmds.
4241 */
4242 if (atomic_read(&conn->connection_recovery)) {
4243 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4244 iscsit_prepare_cmds_for_realligance(conn);
4245 } else {
4246 iscsit_clear_ooo_cmdsns_for_conn(conn);
4247 iscsit_release_commands_from_conn(conn);
4248 }
4249
4250 /*
4251 * Handle decrementing session or connection usage count if
4252 * a logout response was not able to be sent because the
4253 * connection failed. Fall back to Session Recovery here.
4254 */
4255 if (atomic_read(&conn->conn_logout_remove)) {
4256 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4257 iscsit_dec_conn_usage_count(conn);
4258 iscsit_dec_session_usage_count(sess);
4259 }
4260 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4261 iscsit_dec_conn_usage_count(conn);
4262
4263 atomic_set(&conn->conn_logout_remove, 0);
4264 atomic_set(&sess->session_reinstatement, 0);
4265 atomic_set(&sess->session_fall_back_to_erl0, 1);
4266 }
4267
4268 spin_lock_bh(&sess->conn_lock);
4269 list_del(&conn->conn_list);
4270
4271 /*
4272 * Attempt to let the Initiator know this connection failed by
4273 * sending an Connection Dropped Async Message on another
4274 * active connection.
4275 */
4276 if (atomic_read(&conn->connection_recovery))
4277 iscsit_build_conn_drop_async_message(conn);
4278
4279 spin_unlock_bh(&sess->conn_lock);
4280
4281 /*
4282 * If connection reinstatement is being performed on this connection,
4283 * up the connection reinstatement semaphore that is being blocked on
4284 * in iscsit_cause_connection_reinstatement().
4285 */
4286 spin_lock_bh(&conn->state_lock);
4287 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4288 spin_unlock_bh(&conn->state_lock);
4289 complete(&conn->conn_wait_comp);
4290 wait_for_completion(&conn->conn_post_wait_comp);
4291 spin_lock_bh(&conn->state_lock);
4292 }
4293
4294 /*
4295 * If connection reinstatement is being performed on this connection
4296 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4297 * connection wait rcfr semaphore that is being blocked on
4298 * an iscsit_connection_reinstatement_rcfr().
4299 */
4300 if (atomic_read(&conn->connection_wait_rcfr)) {
4301 spin_unlock_bh(&conn->state_lock);
4302 complete(&conn->conn_wait_rcfr_comp);
4303 wait_for_completion(&conn->conn_post_wait_comp);
4304 spin_lock_bh(&conn->state_lock);
4305 }
4306 atomic_set(&conn->connection_reinstatement, 1);
4307 spin_unlock_bh(&conn->state_lock);
4308
4309 /*
4310 * If any other processes are accessing this connection pointer we
4311 * must wait until they have completed.
4312 */
4313 iscsit_check_conn_usage_count(conn);
4314
4315 if (conn->conn_rx_hash.tfm)
4316 crypto_free_hash(conn->conn_rx_hash.tfm);
4317 if (conn->conn_tx_hash.tfm)
4318 crypto_free_hash(conn->conn_tx_hash.tfm);
4319
4320 if (conn->conn_cpumask)
4321 free_cpumask_var(conn->conn_cpumask);
4322
4323 kfree(conn->conn_ops);
4324 conn->conn_ops = NULL;
4325
Al Virobf6932f2012-07-21 08:55:18 +01004326 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004327 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004328
4329 if (conn->conn_transport->iscsit_free_conn)
4330 conn->conn_transport->iscsit_free_conn(conn);
4331
4332 iscsit_put_transport(conn->conn_transport);
4333
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004334 conn->thread_set = NULL;
4335
4336 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4337 conn->conn_state = TARG_CONN_STATE_FREE;
4338 kfree(conn);
4339
4340 spin_lock_bh(&sess->conn_lock);
4341 atomic_dec(&sess->nconn);
4342 pr_debug("Decremented iSCSI connection count to %hu from node:"
4343 " %s\n", atomic_read(&sess->nconn),
4344 sess->sess_ops->InitiatorName);
4345 /*
4346 * Make sure that if one connection fails in an non ERL=2 iSCSI
4347 * Session that they all fail.
4348 */
4349 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4350 !atomic_read(&sess->session_logout))
4351 atomic_set(&sess->session_fall_back_to_erl0, 1);
4352
4353 /*
4354 * If this was not the last connection in the session, and we are
4355 * performing session reinstatement or falling back to ERL=0, call
4356 * iscsit_stop_session() without sleeping to shutdown the other
4357 * active connections.
4358 */
4359 if (atomic_read(&sess->nconn)) {
4360 if (!atomic_read(&sess->session_reinstatement) &&
4361 !atomic_read(&sess->session_fall_back_to_erl0)) {
4362 spin_unlock_bh(&sess->conn_lock);
4363 return 0;
4364 }
4365 if (!atomic_read(&sess->session_stop_active)) {
4366 atomic_set(&sess->session_stop_active, 1);
4367 spin_unlock_bh(&sess->conn_lock);
4368 iscsit_stop_session(sess, 0, 0);
4369 return 0;
4370 }
4371 spin_unlock_bh(&sess->conn_lock);
4372 return 0;
4373 }
4374
4375 /*
4376 * If this was the last connection in the session and one of the
4377 * following is occurring:
4378 *
4379 * Session Reinstatement is not being performed, and are falling back
4380 * to ERL=0 call iscsit_close_session().
4381 *
4382 * Session Logout was requested. iscsit_close_session() will be called
4383 * elsewhere.
4384 *
4385 * Session Continuation is not being performed, start the Time2Retain
4386 * handler and check if sleep_on_sess_wait_sem is active.
4387 */
4388 if (!atomic_read(&sess->session_reinstatement) &&
4389 atomic_read(&sess->session_fall_back_to_erl0)) {
4390 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004391 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004392
4393 return 0;
4394 } else if (atomic_read(&sess->session_logout)) {
4395 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4396 sess->session_state = TARG_SESS_STATE_FREE;
4397 spin_unlock_bh(&sess->conn_lock);
4398
4399 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4400 complete(&sess->session_wait_comp);
4401
4402 return 0;
4403 } else {
4404 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4405 sess->session_state = TARG_SESS_STATE_FAILED;
4406
4407 if (!atomic_read(&sess->session_continuation)) {
4408 spin_unlock_bh(&sess->conn_lock);
4409 iscsit_start_time2retain_handler(sess);
4410 } else
4411 spin_unlock_bh(&sess->conn_lock);
4412
4413 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4414 complete(&sess->session_wait_comp);
4415
4416 return 0;
4417 }
4418 spin_unlock_bh(&sess->conn_lock);
4419
4420 return 0;
4421}
4422
4423int iscsit_close_session(struct iscsi_session *sess)
4424{
4425 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4426 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4427
4428 if (atomic_read(&sess->nconn)) {
4429 pr_err("%d connection(s) still exist for iSCSI session"
4430 " to %s\n", atomic_read(&sess->nconn),
4431 sess->sess_ops->InitiatorName);
4432 BUG();
4433 }
4434
4435 spin_lock_bh(&se_tpg->session_lock);
4436 atomic_set(&sess->session_logout, 1);
4437 atomic_set(&sess->session_reinstatement, 1);
4438 iscsit_stop_time2retain_timer(sess);
4439 spin_unlock_bh(&se_tpg->session_lock);
4440
4441 /*
4442 * transport_deregister_session_configfs() will clear the
4443 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4444 * can be setting it again with __transport_register_session() in
4445 * iscsi_post_login_handler() again after the iscsit_stop_session()
4446 * completes in iscsi_np context.
4447 */
4448 transport_deregister_session_configfs(sess->se_sess);
4449
4450 /*
4451 * If any other processes are accessing this session pointer we must
4452 * wait until they have completed. If we are in an interrupt (the
4453 * time2retain handler) and contain and active session usage count we
4454 * restart the timer and exit.
4455 */
4456 if (!in_interrupt()) {
4457 if (iscsit_check_session_usage_count(sess) == 1)
4458 iscsit_stop_session(sess, 1, 1);
4459 } else {
4460 if (iscsit_check_session_usage_count(sess) == 2) {
4461 atomic_set(&sess->session_logout, 0);
4462 iscsit_start_time2retain_handler(sess);
4463 return 0;
4464 }
4465 }
4466
4467 transport_deregister_session(sess->se_sess);
4468
4469 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4470 iscsit_free_connection_recovery_entires(sess);
4471
4472 iscsit_free_all_ooo_cmdsns(sess);
4473
4474 spin_lock_bh(&se_tpg->session_lock);
4475 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4476 sess->session_state = TARG_SESS_STATE_FREE;
4477 pr_debug("Released iSCSI session from node: %s\n",
4478 sess->sess_ops->InitiatorName);
4479 tpg->nsessions--;
4480 if (tpg->tpg_tiqn)
4481 tpg->tpg_tiqn->tiqn_nsessions--;
4482
4483 pr_debug("Decremented number of active iSCSI Sessions on"
4484 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4485
4486 spin_lock(&sess_idr_lock);
4487 idr_remove(&sess_idr, sess->session_index);
4488 spin_unlock(&sess_idr_lock);
4489
4490 kfree(sess->sess_ops);
4491 sess->sess_ops = NULL;
4492 spin_unlock_bh(&se_tpg->session_lock);
4493
4494 kfree(sess);
4495 return 0;
4496}
4497
4498static void iscsit_logout_post_handler_closesession(
4499 struct iscsi_conn *conn)
4500{
4501 struct iscsi_session *sess = conn->sess;
4502
4503 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4504 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4505
4506 atomic_set(&conn->conn_logout_remove, 0);
4507 complete(&conn->conn_logout_comp);
4508
4509 iscsit_dec_conn_usage_count(conn);
4510 iscsit_stop_session(sess, 1, 1);
4511 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004512 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004513}
4514
4515static void iscsit_logout_post_handler_samecid(
4516 struct iscsi_conn *conn)
4517{
4518 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4519 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4520
4521 atomic_set(&conn->conn_logout_remove, 0);
4522 complete(&conn->conn_logout_comp);
4523
4524 iscsit_cause_connection_reinstatement(conn, 1);
4525 iscsit_dec_conn_usage_count(conn);
4526}
4527
4528static void iscsit_logout_post_handler_diffcid(
4529 struct iscsi_conn *conn,
4530 u16 cid)
4531{
4532 struct iscsi_conn *l_conn;
4533 struct iscsi_session *sess = conn->sess;
4534
4535 if (!sess)
4536 return;
4537
4538 spin_lock_bh(&sess->conn_lock);
4539 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4540 if (l_conn->cid == cid) {
4541 iscsit_inc_conn_usage_count(l_conn);
4542 break;
4543 }
4544 }
4545 spin_unlock_bh(&sess->conn_lock);
4546
4547 if (!l_conn)
4548 return;
4549
4550 if (l_conn->sock)
4551 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4552
4553 spin_lock_bh(&l_conn->state_lock);
4554 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4555 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4556 spin_unlock_bh(&l_conn->state_lock);
4557
4558 iscsit_cause_connection_reinstatement(l_conn, 1);
4559 iscsit_dec_conn_usage_count(l_conn);
4560}
4561
4562/*
4563 * Return of 0 causes the TX thread to restart.
4564 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004565int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004566 struct iscsi_cmd *cmd,
4567 struct iscsi_conn *conn)
4568{
4569 int ret = 0;
4570
4571 switch (cmd->logout_reason) {
4572 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4573 switch (cmd->logout_response) {
4574 case ISCSI_LOGOUT_SUCCESS:
4575 case ISCSI_LOGOUT_CLEANUP_FAILED:
4576 default:
4577 iscsit_logout_post_handler_closesession(conn);
4578 break;
4579 }
4580 ret = 0;
4581 break;
4582 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4583 if (conn->cid == cmd->logout_cid) {
4584 switch (cmd->logout_response) {
4585 case ISCSI_LOGOUT_SUCCESS:
4586 case ISCSI_LOGOUT_CLEANUP_FAILED:
4587 default:
4588 iscsit_logout_post_handler_samecid(conn);
4589 break;
4590 }
4591 ret = 0;
4592 } else {
4593 switch (cmd->logout_response) {
4594 case ISCSI_LOGOUT_SUCCESS:
4595 iscsit_logout_post_handler_diffcid(conn,
4596 cmd->logout_cid);
4597 break;
4598 case ISCSI_LOGOUT_CID_NOT_FOUND:
4599 case ISCSI_LOGOUT_CLEANUP_FAILED:
4600 default:
4601 break;
4602 }
4603 ret = 1;
4604 }
4605 break;
4606 case ISCSI_LOGOUT_REASON_RECOVERY:
4607 switch (cmd->logout_response) {
4608 case ISCSI_LOGOUT_SUCCESS:
4609 case ISCSI_LOGOUT_CID_NOT_FOUND:
4610 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4611 case ISCSI_LOGOUT_CLEANUP_FAILED:
4612 default:
4613 break;
4614 }
4615 ret = 1;
4616 break;
4617 default:
4618 break;
4619
4620 }
4621 return ret;
4622}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004623EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004624
4625void iscsit_fail_session(struct iscsi_session *sess)
4626{
4627 struct iscsi_conn *conn;
4628
4629 spin_lock_bh(&sess->conn_lock);
4630 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4631 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4632 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4633 }
4634 spin_unlock_bh(&sess->conn_lock);
4635
4636 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4637 sess->session_state = TARG_SESS_STATE_FAILED;
4638}
4639
4640int iscsit_free_session(struct iscsi_session *sess)
4641{
4642 u16 conn_count = atomic_read(&sess->nconn);
4643 struct iscsi_conn *conn, *conn_tmp = NULL;
4644 int is_last;
4645
4646 spin_lock_bh(&sess->conn_lock);
4647 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4648
4649 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4650 conn_list) {
4651 if (conn_count == 0)
4652 break;
4653
4654 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4655 is_last = 1;
4656 } else {
4657 iscsit_inc_conn_usage_count(conn_tmp);
4658 is_last = 0;
4659 }
4660 iscsit_inc_conn_usage_count(conn);
4661
4662 spin_unlock_bh(&sess->conn_lock);
4663 iscsit_cause_connection_reinstatement(conn, 1);
4664 spin_lock_bh(&sess->conn_lock);
4665
4666 iscsit_dec_conn_usage_count(conn);
4667 if (is_last == 0)
4668 iscsit_dec_conn_usage_count(conn_tmp);
4669
4670 conn_count--;
4671 }
4672
4673 if (atomic_read(&sess->nconn)) {
4674 spin_unlock_bh(&sess->conn_lock);
4675 wait_for_completion(&sess->session_wait_comp);
4676 } else
4677 spin_unlock_bh(&sess->conn_lock);
4678
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004679 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004680 return 0;
4681}
4682
4683void iscsit_stop_session(
4684 struct iscsi_session *sess,
4685 int session_sleep,
4686 int connection_sleep)
4687{
4688 u16 conn_count = atomic_read(&sess->nconn);
4689 struct iscsi_conn *conn, *conn_tmp = NULL;
4690 int is_last;
4691
4692 spin_lock_bh(&sess->conn_lock);
4693 if (session_sleep)
4694 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4695
4696 if (connection_sleep) {
4697 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4698 conn_list) {
4699 if (conn_count == 0)
4700 break;
4701
4702 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4703 is_last = 1;
4704 } else {
4705 iscsit_inc_conn_usage_count(conn_tmp);
4706 is_last = 0;
4707 }
4708 iscsit_inc_conn_usage_count(conn);
4709
4710 spin_unlock_bh(&sess->conn_lock);
4711 iscsit_cause_connection_reinstatement(conn, 1);
4712 spin_lock_bh(&sess->conn_lock);
4713
4714 iscsit_dec_conn_usage_count(conn);
4715 if (is_last == 0)
4716 iscsit_dec_conn_usage_count(conn_tmp);
4717 conn_count--;
4718 }
4719 } else {
4720 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4721 iscsit_cause_connection_reinstatement(conn, 0);
4722 }
4723
4724 if (session_sleep && atomic_read(&sess->nconn)) {
4725 spin_unlock_bh(&sess->conn_lock);
4726 wait_for_completion(&sess->session_wait_comp);
4727 } else
4728 spin_unlock_bh(&sess->conn_lock);
4729}
4730
4731int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4732{
4733 struct iscsi_session *sess;
4734 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4735 struct se_session *se_sess, *se_sess_tmp;
4736 int session_count = 0;
4737
4738 spin_lock_bh(&se_tpg->session_lock);
4739 if (tpg->nsessions && !force) {
4740 spin_unlock_bh(&se_tpg->session_lock);
4741 return -1;
4742 }
4743
4744 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4745 sess_list) {
4746 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4747
4748 spin_lock(&sess->conn_lock);
4749 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4750 atomic_read(&sess->session_logout) ||
4751 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4752 spin_unlock(&sess->conn_lock);
4753 continue;
4754 }
4755 atomic_set(&sess->session_reinstatement, 1);
4756 spin_unlock(&sess->conn_lock);
4757 spin_unlock_bh(&se_tpg->session_lock);
4758
4759 iscsit_free_session(sess);
4760 spin_lock_bh(&se_tpg->session_lock);
4761
4762 session_count++;
4763 }
4764 spin_unlock_bh(&se_tpg->session_lock);
4765
4766 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4767 " Group: %hu\n", session_count, tpg->tpgt);
4768 return 0;
4769}
4770
4771MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4772MODULE_VERSION("4.1.x");
4773MODULE_AUTHOR("nab@Linux-iSCSI.org");
4774MODULE_LICENSE("GPL");
4775
4776module_init(iscsi_target_init_module);
4777module_exit(iscsi_target_cleanup_module);