blob: 1f79a168f1c18c11fb406a96b91709455b4646fa [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{
1998 int cmdsn_ret;
1999
2000 spin_lock_bh(&conn->cmd_lock);
2001 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2002 spin_unlock_bh(&conn->cmd_lock);
2003
2004 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2005
2006 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2007 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2008 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2009 return iscsit_add_reject_from_cmd(
2010 ISCSI_REASON_PROTOCOL_ERROR,
2011 1, 0, (unsigned char *)hdr, cmd);
2012 return 0;
2013 }
2014
2015 return iscsit_execute_cmd(cmd, 0);
2016}
2017EXPORT_SYMBOL(iscsit_process_text_cmd);
2018
2019static int
2020iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2021 unsigned char *buf)
2022{
2023 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2024 char *text_in = NULL;
2025 u32 payload_length = ntoh24(hdr->dlength);
2026 int rx_size, rc;
2027
2028 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2029 if (rc < 0)
2030 return rc;
2031
2032 rx_size = payload_length;
2033 if (payload_length) {
2034 char *text_ptr;
2035 u32 checksum = 0, data_crc = 0;
2036 u32 padding = 0, pad_bytes = 0;
2037 int niov = 0, rx_got;
2038 struct kvec iov[3];
2039
2040 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002041 if (!text_in) {
2042 pr_err("Unable to allocate memory for"
2043 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002044 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002045 }
2046
2047 memset(iov, 0, 3 * sizeof(struct kvec));
2048 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002049 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002050
2051 padding = ((-payload_length) & 3);
2052 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002053 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002054 iov[niov++].iov_len = padding;
2055 rx_size += padding;
2056 pr_debug("Receiving %u additional bytes"
2057 " for padding.\n", padding);
2058 }
2059 if (conn->conn_ops->DataDigest) {
2060 iov[niov].iov_base = &checksum;
2061 iov[niov++].iov_len = ISCSI_CRC_LEN;
2062 rx_size += ISCSI_CRC_LEN;
2063 }
2064
2065 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002066 if (rx_got != rx_size)
2067 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002068
2069 if (conn->conn_ops->DataDigest) {
2070 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002071 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002072 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002073 (u8 *)&data_crc);
2074
2075 if (checksum != data_crc) {
2076 pr_err("Text data CRC32C DataDigest"
2077 " 0x%08x does not match computed"
2078 " 0x%08x\n", checksum, data_crc);
2079 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2080 pr_err("Unable to recover from"
2081 " Text Data digest failure while in"
2082 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002083 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002084 } else {
2085 /*
2086 * Silently drop this PDU and let the
2087 * initiator plug the CmdSN gap.
2088 */
2089 pr_debug("Dropping Text"
2090 " Command CmdSN: 0x%08x due to"
2091 " DataCRC error.\n", hdr->cmdsn);
2092 kfree(text_in);
2093 return 0;
2094 }
2095 } else {
2096 pr_debug("Got CRC32C DataDigest"
2097 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002098 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002099 }
2100 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002101 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002102 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002103 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002104
2105 if (strncmp("SendTargets", text_in, 11) != 0) {
2106 pr_err("Received Text Data that is not"
2107 " SendTargets, cannot continue.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002108 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002109 }
2110 text_ptr = strchr(text_in, '=');
2111 if (!text_ptr) {
2112 pr_err("No \"=\" separator found in Text Data,"
2113 " cannot continue.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002114 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002115 }
2116 if (strncmp("=All", text_ptr, 4) != 0) {
2117 pr_err("Unable to locate All value for"
2118 " SendTargets key, cannot continue.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002119 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002120 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002121 kfree(text_in);
2122 }
2123
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002124 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002125
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002126reject:
2127 kfree(text_in);
2128 return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
2129 0, 0, buf, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002130}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002131EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002132
2133int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2134{
2135 struct iscsi_conn *conn_p;
2136 struct iscsi_session *sess = conn->sess;
2137
2138 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2139 " for SID: %u.\n", conn->cid, conn->sess->sid);
2140
2141 atomic_set(&sess->session_logout, 1);
2142 atomic_set(&conn->conn_logout_remove, 1);
2143 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2144
2145 iscsit_inc_conn_usage_count(conn);
2146 iscsit_inc_session_usage_count(sess);
2147
2148 spin_lock_bh(&sess->conn_lock);
2149 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2150 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2151 continue;
2152
2153 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2154 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2155 }
2156 spin_unlock_bh(&sess->conn_lock);
2157
2158 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2159
2160 return 0;
2161}
2162
2163int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2164{
2165 struct iscsi_conn *l_conn;
2166 struct iscsi_session *sess = conn->sess;
2167
2168 pr_debug("Received logout request CLOSECONNECTION for CID:"
2169 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2170
2171 /*
2172 * A Logout Request with a CLOSECONNECTION reason code for a CID
2173 * can arrive on a connection with a differing CID.
2174 */
2175 if (conn->cid == cmd->logout_cid) {
2176 spin_lock_bh(&conn->state_lock);
2177 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2178 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2179
2180 atomic_set(&conn->conn_logout_remove, 1);
2181 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2182 iscsit_inc_conn_usage_count(conn);
2183
2184 spin_unlock_bh(&conn->state_lock);
2185 } else {
2186 /*
2187 * Handle all different cid CLOSECONNECTION requests in
2188 * iscsit_logout_post_handler_diffcid() as to give enough
2189 * time for any non immediate command's CmdSN to be
2190 * acknowledged on the connection in question.
2191 *
2192 * Here we simply make sure the CID is still around.
2193 */
2194 l_conn = iscsit_get_conn_from_cid(sess,
2195 cmd->logout_cid);
2196 if (!l_conn) {
2197 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2198 iscsit_add_cmd_to_response_queue(cmd, conn,
2199 cmd->i_state);
2200 return 0;
2201 }
2202
2203 iscsit_dec_conn_usage_count(l_conn);
2204 }
2205
2206 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2207
2208 return 0;
2209}
2210
2211int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2212{
2213 struct iscsi_session *sess = conn->sess;
2214
2215 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2216 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2217
2218 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2219 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2220 " while ERL!=2.\n");
2221 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2222 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2223 return 0;
2224 }
2225
2226 if (conn->cid == cmd->logout_cid) {
2227 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2228 " with CID: %hu on CID: %hu, implementation error.\n",
2229 cmd->logout_cid, conn->cid);
2230 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2231 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2232 return 0;
2233 }
2234
2235 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2236
2237 return 0;
2238}
2239
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002240int
2241iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2242 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002243{
2244 int cmdsn_ret, logout_remove = 0;
2245 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002246 struct iscsi_logout *hdr;
2247 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2248
2249 hdr = (struct iscsi_logout *) buf;
2250 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002251
2252 if (tiqn) {
2253 spin_lock(&tiqn->logout_stats.lock);
2254 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2255 tiqn->logout_stats.normal_logouts++;
2256 else
2257 tiqn->logout_stats.abnormal_logouts++;
2258 spin_unlock(&tiqn->logout_stats.lock);
2259 }
2260
2261 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2262 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2263 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2264 hdr->cid, conn->cid);
2265
2266 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2267 pr_err("Received logout request on connection that"
2268 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002269 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002270 return 0;
2271 }
2272
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002273 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2274 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2275 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2276 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2277 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002278 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2279 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2280 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002281 cmd->logout_reason = reason_code;
2282 cmd->data_direction = DMA_NONE;
2283
2284 /*
2285 * We need to sleep in these cases (by returning 1) until the Logout
2286 * Response gets sent in the tx thread.
2287 */
2288 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2289 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002290 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002291 logout_remove = 1;
2292
2293 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002294 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002295 spin_unlock_bh(&conn->cmd_lock);
2296
2297 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002298 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002299
2300 /*
2301 * Immediate commands are executed, well, immediately.
2302 * Non-Immediate Logout Commands are executed in CmdSN order.
2303 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002304 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002305 int ret = iscsit_execute_cmd(cmd, 0);
2306
2307 if (ret < 0)
2308 return ret;
2309 } else {
2310 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2311 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2312 logout_remove = 0;
2313 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2314 return iscsit_add_reject_from_cmd(
2315 ISCSI_REASON_PROTOCOL_ERROR,
2316 1, 0, buf, cmd);
2317 }
2318 }
2319
2320 return logout_remove;
2321}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002322EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002323
2324static int iscsit_handle_snack(
2325 struct iscsi_conn *conn,
2326 unsigned char *buf)
2327{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002328 struct iscsi_snack *hdr;
2329
2330 hdr = (struct iscsi_snack *) buf;
2331 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002332
2333 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2334 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2335 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2336 hdr->begrun, hdr->runlength, conn->cid);
2337
2338 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2339 pr_err("Initiator sent SNACK request while in"
2340 " ErrorRecoveryLevel=0.\n");
2341 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2342 buf, conn);
2343 }
2344 /*
2345 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2346 * call from inside iscsi_send_recovery_datain_or_r2t().
2347 */
2348 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2349 case 0:
2350 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002351 hdr->itt,
2352 be32_to_cpu(hdr->ttt),
2353 be32_to_cpu(hdr->begrun),
2354 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002355 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002356 return iscsit_handle_status_snack(conn, hdr->itt,
2357 be32_to_cpu(hdr->ttt),
2358 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002359 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002360 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2361 be32_to_cpu(hdr->begrun),
2362 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002363 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2364 /* FIXME: Support R-Data SNACK */
2365 pr_err("R-Data SNACK Not Supported.\n");
2366 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2367 buf, conn);
2368 default:
2369 pr_err("Unknown SNACK type 0x%02x, protocol"
2370 " error.\n", hdr->flags & 0x0f);
2371 return iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
2372 buf, conn);
2373 }
2374
2375 return 0;
2376}
2377
2378static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2379{
2380 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2381 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2382 wait_for_completion_interruptible_timeout(
2383 &conn->rx_half_close_comp,
2384 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2385 }
2386}
2387
2388static int iscsit_handle_immediate_data(
2389 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002390 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002391 u32 length)
2392{
2393 int iov_ret, rx_got = 0, rx_size = 0;
2394 u32 checksum, iov_count = 0, padding = 0;
2395 struct iscsi_conn *conn = cmd->conn;
2396 struct kvec *iov;
2397
2398 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2399 if (iov_ret < 0)
2400 return IMMEDIATE_DATA_CANNOT_RECOVER;
2401
2402 rx_size = length;
2403 iov_count = iov_ret;
2404 iov = &cmd->iov_data[0];
2405
2406 padding = ((-length) & 3);
2407 if (padding != 0) {
2408 iov[iov_count].iov_base = cmd->pad_bytes;
2409 iov[iov_count++].iov_len = padding;
2410 rx_size += padding;
2411 }
2412
2413 if (conn->conn_ops->DataDigest) {
2414 iov[iov_count].iov_base = &checksum;
2415 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2416 rx_size += ISCSI_CRC_LEN;
2417 }
2418
2419 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2420
2421 iscsit_unmap_iovec(cmd);
2422
2423 if (rx_got != rx_size) {
2424 iscsit_rx_thread_wait_for_tcp(conn);
2425 return IMMEDIATE_DATA_CANNOT_RECOVER;
2426 }
2427
2428 if (conn->conn_ops->DataDigest) {
2429 u32 data_crc;
2430
2431 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2432 cmd->write_data_done, length, padding,
2433 cmd->pad_bytes);
2434
2435 if (checksum != data_crc) {
2436 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2437 " does not match computed 0x%08x\n", checksum,
2438 data_crc);
2439
2440 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2441 pr_err("Unable to recover from"
2442 " Immediate Data digest failure while"
2443 " in ERL=0.\n");
2444 iscsit_add_reject_from_cmd(
2445 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002446 1, 0, (unsigned char *)hdr, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002447 return IMMEDIATE_DATA_CANNOT_RECOVER;
2448 } else {
2449 iscsit_add_reject_from_cmd(
2450 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002451 0, 0, (unsigned char *)hdr, cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002452 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2453 }
2454 } else {
2455 pr_debug("Got CRC32C DataDigest 0x%08x for"
2456 " %u bytes of Immediate Data\n", checksum,
2457 length);
2458 }
2459 }
2460
2461 cmd->write_data_done += length;
2462
Andy Groverebf1d952012-04-03 15:51:24 -07002463 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002464 spin_lock_bh(&cmd->istate_lock);
2465 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2466 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2467 spin_unlock_bh(&cmd->istate_lock);
2468 }
2469
2470 return IMMEDIATE_DATA_NORMAL_OPERATION;
2471}
2472
2473/*
2474 * Called with sess->conn_lock held.
2475 */
2476/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2477 with active network interface */
2478static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2479{
2480 struct iscsi_cmd *cmd;
2481 struct iscsi_conn *conn_p;
2482
2483 /*
2484 * Only send a Asynchronous Message on connections whos network
2485 * interface is still functional.
2486 */
2487 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2488 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2489 iscsit_inc_conn_usage_count(conn_p);
2490 break;
2491 }
2492 }
2493
2494 if (!conn_p)
2495 return;
2496
Wei Yongjun3c989d72012-11-23 12:07:39 +08002497 cmd = iscsit_allocate_cmd(conn_p, GFP_ATOMIC);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002498 if (!cmd) {
2499 iscsit_dec_conn_usage_count(conn_p);
2500 return;
2501 }
2502
2503 cmd->logout_cid = conn->cid;
2504 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2505 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2506
2507 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002508 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002509 spin_unlock_bh(&conn_p->cmd_lock);
2510
2511 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2512 iscsit_dec_conn_usage_count(conn_p);
2513}
2514
2515static int iscsit_send_conn_drop_async_message(
2516 struct iscsi_cmd *cmd,
2517 struct iscsi_conn *conn)
2518{
2519 struct iscsi_async *hdr;
2520
2521 cmd->tx_size = ISCSI_HDR_LEN;
2522 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2523
2524 hdr = (struct iscsi_async *) cmd->pdu;
2525 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2526 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002527 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002528 cmd->targ_xfer_tag = 0xFFFFFFFF;
2529 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2530 cmd->stat_sn = conn->stat_sn++;
2531 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2532 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2533 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2534 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2535 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2536 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2537 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2538
2539 if (conn->conn_ops->HeaderDigest) {
2540 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2541
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002542 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2543 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002544
2545 cmd->tx_size += ISCSI_CRC_LEN;
2546 pr_debug("Attaching CRC32C HeaderDigest to"
2547 " Async Message 0x%08x\n", *header_digest);
2548 }
2549
2550 cmd->iov_misc[0].iov_base = cmd->pdu;
2551 cmd->iov_misc[0].iov_len = cmd->tx_size;
2552 cmd->iov_misc_count = 1;
2553
2554 pr_debug("Sending Connection Dropped Async Message StatSN:"
2555 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2556 cmd->logout_cid, conn->cid);
2557 return 0;
2558}
2559
Andy Grover6f3c0e62012-04-03 15:51:09 -07002560static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2561{
2562 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2563 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2564 wait_for_completion_interruptible_timeout(
2565 &conn->tx_half_close_comp,
2566 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2567 }
2568}
2569
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002570static void
2571iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2572 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2573 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002574{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002575 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2576 hdr->flags = datain->flags;
2577 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2578 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2579 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2580 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2581 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2582 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2583 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2584 }
2585 }
2586 hton24(hdr->dlength, datain->length);
2587 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2588 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2589 (struct scsi_lun *)&hdr->lun);
2590 else
2591 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2592
2593 hdr->itt = cmd->init_task_tag;
2594
2595 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2596 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2597 else
2598 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2599 if (set_statsn)
2600 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2601 else
2602 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2603
2604 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2605 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2606 hdr->datasn = cpu_to_be32(datain->data_sn);
2607 hdr->offset = cpu_to_be32(datain->offset);
2608
2609 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2610 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2611 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2612 ntohl(hdr->offset), datain->length, conn->cid);
2613}
2614
2615static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2616{
2617 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002618 struct iscsi_datain datain;
2619 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002620 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002621 u32 iov_count = 0, tx_size = 0;
2622 int eodr = 0, ret, iov_ret;
2623 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002624
2625 memset(&datain, 0, sizeof(struct iscsi_datain));
2626 dr = iscsit_get_datain_values(cmd, &datain);
2627 if (!dr) {
2628 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2629 cmd->init_task_tag);
2630 return -1;
2631 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002632 /*
2633 * Be paranoid and double check the logic for now.
2634 */
Andy Groverebf1d952012-04-03 15:51:24 -07002635 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002636 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2637 " datain.length: %u exceeds cmd->data_length: %u\n",
2638 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002639 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002640 return -1;
2641 }
2642
2643 spin_lock_bh(&conn->sess->session_stats_lock);
2644 conn->sess->tx_data_octets += datain.length;
2645 if (conn->sess->se_sess->se_node_acl) {
2646 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2647 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2648 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2649 }
2650 spin_unlock_bh(&conn->sess->session_stats_lock);
2651 /*
2652 * Special case for successfully execution w/ both DATAIN
2653 * and Sense Data.
2654 */
2655 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2656 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2657 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2658 else {
2659 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2660 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2661 iscsit_increment_maxcmdsn(cmd, conn->sess);
2662 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002663 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002664 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002665 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2666 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002667 }
2668
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002669 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002670
2671 iov = &cmd->iov_data[0];
2672 iov[iov_count].iov_base = cmd->pdu;
2673 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2674 tx_size += ISCSI_HDR_LEN;
2675
2676 if (conn->conn_ops->HeaderDigest) {
2677 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2678
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002679 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2680 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002681
2682 iov[0].iov_len += ISCSI_CRC_LEN;
2683 tx_size += ISCSI_CRC_LEN;
2684
2685 pr_debug("Attaching CRC32 HeaderDigest"
2686 " for DataIN PDU 0x%08x\n", *header_digest);
2687 }
2688
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002689 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2690 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002691 if (iov_ret < 0)
2692 return -1;
2693
2694 iov_count += iov_ret;
2695 tx_size += datain.length;
2696
2697 cmd->padding = ((-datain.length) & 3);
2698 if (cmd->padding) {
2699 iov[iov_count].iov_base = cmd->pad_bytes;
2700 iov[iov_count++].iov_len = cmd->padding;
2701 tx_size += cmd->padding;
2702
2703 pr_debug("Attaching %u padding bytes\n",
2704 cmd->padding);
2705 }
2706 if (conn->conn_ops->DataDigest) {
2707 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2708 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2709
2710 iov[iov_count].iov_base = &cmd->data_crc;
2711 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2712 tx_size += ISCSI_CRC_LEN;
2713
2714 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2715 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2716 }
2717
2718 cmd->iov_data_count = iov_count;
2719 cmd->tx_size = tx_size;
2720
Andy Grover6f3c0e62012-04-03 15:51:09 -07002721 /* sendpage is preferred but can't insert markers */
2722 if (!conn->conn_ops->IFMarker)
2723 ret = iscsit_fe_sendpage_sg(cmd, conn);
2724 else
2725 ret = iscsit_send_tx_data(cmd, conn, 0);
2726
2727 iscsit_unmap_iovec(cmd);
2728
2729 if (ret < 0) {
2730 iscsit_tx_thread_wait_for_tcp(conn);
2731 return ret;
2732 }
2733
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002734 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002735 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002736 2 : 1;
2737 iscsit_free_datain_req(cmd, dr);
2738 }
2739
Andy Grover6f3c0e62012-04-03 15:51:09 -07002740 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002741}
2742
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002743int
2744iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2745 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002746{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002747 struct iscsi_conn *logout_conn = NULL;
2748 struct iscsi_conn_recovery *cr = NULL;
2749 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002750 /*
2751 * The actual shutting down of Sessions and/or Connections
2752 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2753 * is done in scsi_logout_post_handler().
2754 */
2755 switch (cmd->logout_reason) {
2756 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2757 pr_debug("iSCSI session logout successful, setting"
2758 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2759 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2760 break;
2761 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2762 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2763 break;
2764 /*
2765 * For CLOSECONNECTION logout requests carrying
2766 * a matching logout CID -> local CID, the reference
2767 * for the local CID will have been incremented in
2768 * iscsi_logout_closeconnection().
2769 *
2770 * For CLOSECONNECTION logout requests carrying
2771 * a different CID than the connection it arrived
2772 * on, the connection responding to cmd->logout_cid
2773 * is stopped in iscsit_logout_post_handler_diffcid().
2774 */
2775
2776 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2777 " successful.\n", cmd->logout_cid, conn->cid);
2778 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2779 break;
2780 case ISCSI_LOGOUT_REASON_RECOVERY:
2781 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2782 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2783 break;
2784 /*
2785 * If the connection is still active from our point of view
2786 * force connection recovery to occur.
2787 */
2788 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2789 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002790 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002791 iscsit_connection_reinstatement_rcfr(logout_conn);
2792 iscsit_dec_conn_usage_count(logout_conn);
2793 }
2794
2795 cr = iscsit_get_inactive_connection_recovery_entry(
2796 conn->sess, cmd->logout_cid);
2797 if (!cr) {
2798 pr_err("Unable to locate CID: %hu for"
2799 " REMOVECONNFORRECOVERY Logout Request.\n",
2800 cmd->logout_cid);
2801 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2802 break;
2803 }
2804
2805 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2806
2807 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2808 " for recovery for CID: %hu on CID: %hu successful.\n",
2809 cmd->logout_cid, conn->cid);
2810 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2811 break;
2812 default:
2813 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2814 cmd->logout_reason);
2815 return -1;
2816 }
2817
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002818 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2819 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2820 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002821 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002822 cmd->stat_sn = conn->stat_sn++;
2823 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2824
2825 iscsit_increment_maxcmdsn(cmd, conn->sess);
2826 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2827 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2828
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002829 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2830 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2831 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2832 cmd->logout_cid, conn->cid);
2833
2834 return 0;
2835}
2836EXPORT_SYMBOL(iscsit_build_logout_rsp);
2837
2838static int
2839iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2840{
2841 struct kvec *iov;
2842 int niov = 0, tx_size, rc;
2843
2844 rc = iscsit_build_logout_rsp(cmd, conn,
2845 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2846 if (rc < 0)
2847 return rc;
2848
2849 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002850 iov = &cmd->iov_misc[0];
2851 iov[niov].iov_base = cmd->pdu;
2852 iov[niov++].iov_len = ISCSI_HDR_LEN;
2853
2854 if (conn->conn_ops->HeaderDigest) {
2855 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2856
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002857 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2858 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002859
2860 iov[0].iov_len += ISCSI_CRC_LEN;
2861 tx_size += ISCSI_CRC_LEN;
2862 pr_debug("Attaching CRC32C HeaderDigest to"
2863 " Logout Response 0x%08x\n", *header_digest);
2864 }
2865 cmd->iov_misc_count = niov;
2866 cmd->tx_size = tx_size;
2867
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002868 return 0;
2869}
2870
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002871void
2872iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2873 struct iscsi_nopin *hdr, bool nopout_response)
2874{
2875 hdr->opcode = ISCSI_OP_NOOP_IN;
2876 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2877 hton24(hdr->dlength, cmd->buf_ptr_size);
2878 if (nopout_response)
2879 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2880 hdr->itt = cmd->init_task_tag;
2881 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2882 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2883 conn->stat_sn;
2884 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2885
2886 if (nopout_response)
2887 iscsit_increment_maxcmdsn(cmd, conn->sess);
2888
2889 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2890 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2891
2892 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2893 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2894 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2895 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2896}
2897EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2898
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002899/*
2900 * Unsolicited NOPIN, either requesting a response or not.
2901 */
2902static int iscsit_send_unsolicited_nopin(
2903 struct iscsi_cmd *cmd,
2904 struct iscsi_conn *conn,
2905 int want_response)
2906{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002907 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2908 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002909
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002910 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002911
2912 if (conn->conn_ops->HeaderDigest) {
2913 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2914
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002915 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2916 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002917
2918 tx_size += ISCSI_CRC_LEN;
2919 pr_debug("Attaching CRC32C HeaderDigest to"
2920 " NopIN 0x%08x\n", *header_digest);
2921 }
2922
2923 cmd->iov_misc[0].iov_base = cmd->pdu;
2924 cmd->iov_misc[0].iov_len = tx_size;
2925 cmd->iov_misc_count = 1;
2926 cmd->tx_size = tx_size;
2927
2928 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2929 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2930
Andy Grover6f3c0e62012-04-03 15:51:09 -07002931 ret = iscsit_send_tx_data(cmd, conn, 1);
2932 if (ret < 0) {
2933 iscsit_tx_thread_wait_for_tcp(conn);
2934 return ret;
2935 }
2936
2937 spin_lock_bh(&cmd->istate_lock);
2938 cmd->i_state = want_response ?
2939 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2940 spin_unlock_bh(&cmd->istate_lock);
2941
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002942 return 0;
2943}
2944
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002945static int
2946iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002947{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002948 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002949 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002950 u32 padding = 0;
2951 int niov = 0, tx_size;
2952
2953 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002954
2955 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002956 iov = &cmd->iov_misc[0];
2957 iov[niov].iov_base = cmd->pdu;
2958 iov[niov++].iov_len = ISCSI_HDR_LEN;
2959
2960 if (conn->conn_ops->HeaderDigest) {
2961 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2962
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002963 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2964 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002965
2966 iov[0].iov_len += ISCSI_CRC_LEN;
2967 tx_size += ISCSI_CRC_LEN;
2968 pr_debug("Attaching CRC32C HeaderDigest"
2969 " to NopIn 0x%08x\n", *header_digest);
2970 }
2971
2972 /*
2973 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2974 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2975 */
2976 if (cmd->buf_ptr_size) {
2977 iov[niov].iov_base = cmd->buf_ptr;
2978 iov[niov++].iov_len = cmd->buf_ptr_size;
2979 tx_size += cmd->buf_ptr_size;
2980
2981 pr_debug("Echoing back %u bytes of ping"
2982 " data.\n", cmd->buf_ptr_size);
2983
2984 padding = ((-cmd->buf_ptr_size) & 3);
2985 if (padding != 0) {
2986 iov[niov].iov_base = &cmd->pad_bytes;
2987 iov[niov++].iov_len = padding;
2988 tx_size += padding;
2989 pr_debug("Attaching %u additional"
2990 " padding bytes.\n", padding);
2991 }
2992 if (conn->conn_ops->DataDigest) {
2993 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2994 cmd->buf_ptr, cmd->buf_ptr_size,
2995 padding, (u8 *)&cmd->pad_bytes,
2996 (u8 *)&cmd->data_crc);
2997
2998 iov[niov].iov_base = &cmd->data_crc;
2999 iov[niov++].iov_len = ISCSI_CRC_LEN;
3000 tx_size += ISCSI_CRC_LEN;
3001 pr_debug("Attached DataDigest for %u"
3002 " bytes of ping data, CRC 0x%08x\n",
3003 cmd->buf_ptr_size, cmd->data_crc);
3004 }
3005 }
3006
3007 cmd->iov_misc_count = niov;
3008 cmd->tx_size = tx_size;
3009
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003010 return 0;
3011}
3012
Andy Grover6f3c0e62012-04-03 15:51:09 -07003013static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003014 struct iscsi_cmd *cmd,
3015 struct iscsi_conn *conn)
3016{
3017 int tx_size = 0;
3018 struct iscsi_r2t *r2t;
3019 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003020 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003021
3022 r2t = iscsit_get_r2t_from_list(cmd);
3023 if (!r2t)
3024 return -1;
3025
3026 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3027 memset(hdr, 0, ISCSI_HDR_LEN);
3028 hdr->opcode = ISCSI_OP_R2T;
3029 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3030 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3031 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003032 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003033 spin_lock_bh(&conn->sess->ttt_lock);
3034 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3035 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3036 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3037 spin_unlock_bh(&conn->sess->ttt_lock);
3038 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3039 hdr->statsn = cpu_to_be32(conn->stat_sn);
3040 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3041 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3042 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3043 hdr->data_offset = cpu_to_be32(r2t->offset);
3044 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3045
3046 cmd->iov_misc[0].iov_base = cmd->pdu;
3047 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3048 tx_size += ISCSI_HDR_LEN;
3049
3050 if (conn->conn_ops->HeaderDigest) {
3051 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3052
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003053 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3054 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003055
3056 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3057 tx_size += ISCSI_CRC_LEN;
3058 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3059 " PDU 0x%08x\n", *header_digest);
3060 }
3061
3062 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3063 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3064 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3065 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3066 r2t->offset, r2t->xfer_len, conn->cid);
3067
3068 cmd->iov_misc_count = 1;
3069 cmd->tx_size = tx_size;
3070
3071 spin_lock_bh(&cmd->r2t_lock);
3072 r2t->sent_r2t = 1;
3073 spin_unlock_bh(&cmd->r2t_lock);
3074
Andy Grover6f3c0e62012-04-03 15:51:09 -07003075 ret = iscsit_send_tx_data(cmd, conn, 1);
3076 if (ret < 0) {
3077 iscsit_tx_thread_wait_for_tcp(conn);
3078 return ret;
3079 }
3080
3081 spin_lock_bh(&cmd->dataout_timeout_lock);
3082 iscsit_start_dataout_timer(cmd, conn);
3083 spin_unlock_bh(&cmd->dataout_timeout_lock);
3084
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003085 return 0;
3086}
3087
3088/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003089 * @recovery: If called from iscsi_task_reassign_complete_write() for
3090 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003091 */
3092int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003093 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003094 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003095 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003096{
3097 int first_r2t = 1;
3098 u32 offset = 0, xfer_len = 0;
3099
3100 spin_lock_bh(&cmd->r2t_lock);
3101 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3102 spin_unlock_bh(&cmd->r2t_lock);
3103 return 0;
3104 }
3105
Andy Grover8b1e1242012-04-03 15:51:12 -07003106 if (conn->sess->sess_ops->DataSequenceInOrder &&
3107 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003108 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003109
3110 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3111 if (conn->sess->sess_ops->DataSequenceInOrder) {
3112 offset = cmd->r2t_offset;
3113
Andy Grover8b1e1242012-04-03 15:51:12 -07003114 if (first_r2t && recovery) {
3115 int new_data_end = offset +
3116 conn->sess->sess_ops->MaxBurstLength -
3117 cmd->next_burst_len;
3118
Andy Groverebf1d952012-04-03 15:51:24 -07003119 if (new_data_end > cmd->se_cmd.data_length)
3120 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003121 else
3122 xfer_len =
3123 conn->sess->sess_ops->MaxBurstLength -
3124 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003125 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003126 int new_data_end = offset +
3127 conn->sess->sess_ops->MaxBurstLength;
3128
Andy Groverebf1d952012-04-03 15:51:24 -07003129 if (new_data_end > cmd->se_cmd.data_length)
3130 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003131 else
3132 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003133 }
3134 cmd->r2t_offset += xfer_len;
3135
Andy Groverebf1d952012-04-03 15:51:24 -07003136 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003137 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3138 } else {
3139 struct iscsi_seq *seq;
3140
3141 seq = iscsit_get_seq_holder_for_r2t(cmd);
3142 if (!seq) {
3143 spin_unlock_bh(&cmd->r2t_lock);
3144 return -1;
3145 }
3146
3147 offset = seq->offset;
3148 xfer_len = seq->xfer_len;
3149
3150 if (cmd->seq_send_order == cmd->seq_count)
3151 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3152 }
3153 cmd->outstanding_r2ts++;
3154 first_r2t = 0;
3155
3156 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3157 spin_unlock_bh(&cmd->r2t_lock);
3158 return -1;
3159 }
3160
3161 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3162 break;
3163 }
3164 spin_unlock_bh(&cmd->r2t_lock);
3165
3166 return 0;
3167}
3168
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003169void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3170 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003171{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003172 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003173 cmd->stat_sn = conn->stat_sn++;
3174
3175 spin_lock_bh(&conn->sess->session_stats_lock);
3176 conn->sess->rsp_pdus++;
3177 spin_unlock_bh(&conn->sess->session_stats_lock);
3178
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003179 memset(hdr, 0, ISCSI_HDR_LEN);
3180 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3181 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3182 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3183 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003184 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003185 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3186 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003187 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003188 }
3189 hdr->response = cmd->iscsi_response;
3190 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003191 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003192 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3193
3194 iscsit_increment_maxcmdsn(cmd, conn->sess);
3195 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3196 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3197
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003198 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3199 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3200 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3201 cmd->se_cmd.scsi_status, conn->cid);
3202}
3203EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3204
3205static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3206{
3207 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3208 struct kvec *iov;
3209 u32 padding = 0, tx_size = 0;
3210 int iov_count = 0;
3211 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3212
3213 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3214
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003215 iov = &cmd->iov_misc[0];
3216 iov[iov_count].iov_base = cmd->pdu;
3217 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3218 tx_size += ISCSI_HDR_LEN;
3219
3220 /*
3221 * Attach SENSE DATA payload to iSCSI Response PDU
3222 */
3223 if (cmd->se_cmd.sense_buffer &&
3224 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3225 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003226 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3227 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3228
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003229 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003230 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003231 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003232 iov[iov_count++].iov_len =
3233 (cmd->se_cmd.scsi_sense_length + padding);
3234 tx_size += cmd->se_cmd.scsi_sense_length;
3235
3236 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003237 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003238 cmd->se_cmd.scsi_sense_length, 0, padding);
3239 tx_size += padding;
3240 pr_debug("Adding %u bytes of padding to"
3241 " SENSE.\n", padding);
3242 }
3243
3244 if (conn->conn_ops->DataDigest) {
3245 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003246 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003247 (cmd->se_cmd.scsi_sense_length + padding),
3248 0, NULL, (u8 *)&cmd->data_crc);
3249
3250 iov[iov_count].iov_base = &cmd->data_crc;
3251 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3252 tx_size += ISCSI_CRC_LEN;
3253
3254 pr_debug("Attaching CRC32 DataDigest for"
3255 " SENSE, %u bytes CRC 0x%08x\n",
3256 (cmd->se_cmd.scsi_sense_length + padding),
3257 cmd->data_crc);
3258 }
3259
3260 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3261 " Response PDU\n",
3262 cmd->se_cmd.scsi_sense_length);
3263 }
3264
3265 if (conn->conn_ops->HeaderDigest) {
3266 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3267
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003268 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3269 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003270
3271 iov[0].iov_len += ISCSI_CRC_LEN;
3272 tx_size += ISCSI_CRC_LEN;
3273 pr_debug("Attaching CRC32 HeaderDigest for Response"
3274 " PDU 0x%08x\n", *header_digest);
3275 }
3276
3277 cmd->iov_misc_count = iov_count;
3278 cmd->tx_size = tx_size;
3279
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003280 return 0;
3281}
3282
3283static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3284{
3285 switch (se_tmr->response) {
3286 case TMR_FUNCTION_COMPLETE:
3287 return ISCSI_TMF_RSP_COMPLETE;
3288 case TMR_TASK_DOES_NOT_EXIST:
3289 return ISCSI_TMF_RSP_NO_TASK;
3290 case TMR_LUN_DOES_NOT_EXIST:
3291 return ISCSI_TMF_RSP_NO_LUN;
3292 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3293 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3294 case TMR_FUNCTION_AUTHORIZATION_FAILED:
3295 return ISCSI_TMF_RSP_AUTH_FAILED;
3296 case TMR_FUNCTION_REJECTED:
3297 default:
3298 return ISCSI_TMF_RSP_REJECTED;
3299 }
3300}
3301
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003302void
3303iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3304 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003305{
3306 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003307
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003308 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003309 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003310 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003311 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003312 cmd->stat_sn = conn->stat_sn++;
3313 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3314
3315 iscsit_increment_maxcmdsn(cmd, conn->sess);
3316 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3317 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3318
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003319 pr_debug("Built Task Management Response ITT: 0x%08x,"
3320 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3321 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3322}
3323EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3324
3325static int
3326iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3327{
3328 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3329 u32 tx_size = 0;
3330
3331 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3332
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003333 cmd->iov_misc[0].iov_base = cmd->pdu;
3334 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3335 tx_size += ISCSI_HDR_LEN;
3336
3337 if (conn->conn_ops->HeaderDigest) {
3338 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3339
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003340 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3341 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003342
3343 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3344 tx_size += ISCSI_CRC_LEN;
3345 pr_debug("Attaching CRC32 HeaderDigest for Task"
3346 " Mgmt Response PDU 0x%08x\n", *header_digest);
3347 }
3348
3349 cmd->iov_misc_count = 1;
3350 cmd->tx_size = tx_size;
3351
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003352 return 0;
3353}
3354
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003355static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3356{
3357 bool ret = false;
3358
3359 if (np->np_sockaddr.ss_family == AF_INET6) {
3360 const struct sockaddr_in6 sin6 = {
3361 .sin6_addr = IN6ADDR_ANY_INIT };
3362 struct sockaddr_in6 *sock_in6 =
3363 (struct sockaddr_in6 *)&np->np_sockaddr;
3364
3365 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3366 sin6.sin6_addr.s6_addr, 16))
3367 ret = true;
3368 } else {
3369 struct sockaddr_in * sock_in =
3370 (struct sockaddr_in *)&np->np_sockaddr;
3371
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003372 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003373 ret = true;
3374 }
3375
3376 return ret;
3377}
3378
Andy Grover8b1e1242012-04-03 15:51:12 -07003379#define SENDTARGETS_BUF_LIMIT 32768U
3380
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003381static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3382{
3383 char *payload = NULL;
3384 struct iscsi_conn *conn = cmd->conn;
3385 struct iscsi_portal_group *tpg;
3386 struct iscsi_tiqn *tiqn;
3387 struct iscsi_tpg_np *tpg_np;
3388 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003389 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003390
Andy Grover8b1e1242012-04-03 15:51:12 -07003391 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3392 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003393
3394 payload = kzalloc(buffer_len, GFP_KERNEL);
3395 if (!payload) {
3396 pr_err("Unable to allocate memory for sendtargets"
3397 " response.\n");
3398 return -ENOMEM;
3399 }
3400
3401 spin_lock(&tiqn_lock);
3402 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3403 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3404 len += 1;
3405
3406 if ((len + payload_len) > buffer_len) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003407 end_of_buf = 1;
3408 goto eob;
3409 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003410 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003411 payload_len += len;
3412
3413 spin_lock(&tiqn->tiqn_tpg_lock);
3414 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3415
3416 spin_lock(&tpg->tpg_state_lock);
3417 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3418 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3419 spin_unlock(&tpg->tpg_state_lock);
3420 continue;
3421 }
3422 spin_unlock(&tpg->tpg_state_lock);
3423
3424 spin_lock(&tpg->tpg_np_lock);
3425 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3426 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003427 struct iscsi_np *np = tpg_np->tpg_np;
3428 bool inaddr_any = iscsit_check_inaddr_any(np);
3429
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003430 len = sprintf(buf, "TargetAddress="
3431 "%s%s%s:%hu,%hu",
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003432 (np->np_sockaddr.ss_family == AF_INET6) ?
3433 "[" : "", (inaddr_any == false) ?
3434 np->np_ip : conn->local_ip,
3435 (np->np_sockaddr.ss_family == AF_INET6) ?
3436 "]" : "", (inaddr_any == false) ?
3437 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003438 tpg->tpgt);
3439 len += 1;
3440
3441 if ((len + payload_len) > buffer_len) {
3442 spin_unlock(&tpg->tpg_np_lock);
3443 spin_unlock(&tiqn->tiqn_tpg_lock);
3444 end_of_buf = 1;
3445 goto eob;
3446 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003447 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003448 payload_len += len;
3449 }
3450 spin_unlock(&tpg->tpg_np_lock);
3451 }
3452 spin_unlock(&tiqn->tiqn_tpg_lock);
3453eob:
3454 if (end_of_buf)
3455 break;
3456 }
3457 spin_unlock(&tiqn_lock);
3458
3459 cmd->buf_ptr = payload;
3460
3461 return payload_len;
3462}
3463
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003464int
3465iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3466 struct iscsi_text_rsp *hdr)
3467{
3468 int text_length, padding;
3469
3470 text_length = iscsit_build_sendtargets_response(cmd);
3471 if (text_length < 0)
3472 return text_length;
3473
3474 hdr->opcode = ISCSI_OP_TEXT_RSP;
3475 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3476 padding = ((-text_length) & 3);
3477 hton24(hdr->dlength, text_length);
3478 hdr->itt = cmd->init_task_tag;
3479 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3480 cmd->stat_sn = conn->stat_sn++;
3481 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3482
3483 iscsit_increment_maxcmdsn(cmd, conn->sess);
3484 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3485 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3486
3487 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3488 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3489 text_length, conn->cid);
3490
3491 return text_length + padding;
3492}
3493EXPORT_SYMBOL(iscsit_build_text_rsp);
3494
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003495/*
3496 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3497 * MaxRecvDataSegmentLength.
3498 */
3499static int iscsit_send_text_rsp(
3500 struct iscsi_cmd *cmd,
3501 struct iscsi_conn *conn)
3502{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003503 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003504 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003505 u32 tx_size = 0;
3506 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003507
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003508 rc = iscsit_build_text_rsp(cmd, conn, hdr);
3509 if (rc < 0)
3510 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003511
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003512 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003513 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003514 iov[iov_count].iov_base = cmd->pdu;
3515 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3516 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003517 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003518
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003519 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003520
3521 if (conn->conn_ops->HeaderDigest) {
3522 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3523
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003524 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3525 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003526
3527 iov[0].iov_len += ISCSI_CRC_LEN;
3528 tx_size += ISCSI_CRC_LEN;
3529 pr_debug("Attaching CRC32 HeaderDigest for"
3530 " Text Response PDU 0x%08x\n", *header_digest);
3531 }
3532
3533 if (conn->conn_ops->DataDigest) {
3534 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003535 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003536 0, NULL, (u8 *)&cmd->data_crc);
3537
3538 iov[iov_count].iov_base = &cmd->data_crc;
3539 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3540 tx_size += ISCSI_CRC_LEN;
3541
3542 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003543 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003544 cmd->data_crc);
3545 }
3546
3547 cmd->iov_misc_count = iov_count;
3548 cmd->tx_size = tx_size;
3549
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003550 return 0;
3551}
3552
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003553void
3554iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3555 struct iscsi_reject *hdr)
3556{
3557 hdr->opcode = ISCSI_OP_REJECT;
3558 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3559 hton24(hdr->dlength, ISCSI_HDR_LEN);
3560 hdr->ffffffff = cpu_to_be32(0xffffffff);
3561 cmd->stat_sn = conn->stat_sn++;
3562 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3563 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3564 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3565
3566}
3567EXPORT_SYMBOL(iscsit_build_reject);
3568
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003569static int iscsit_send_reject(
3570 struct iscsi_cmd *cmd,
3571 struct iscsi_conn *conn)
3572{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003573 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003574 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003575 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003576
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003577 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003578
3579 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003580 iov[iov_count].iov_base = cmd->pdu;
3581 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3582 iov[iov_count].iov_base = cmd->buf_ptr;
3583 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3584
3585 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3586
3587 if (conn->conn_ops->HeaderDigest) {
3588 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3589
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003590 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3591 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003592
3593 iov[0].iov_len += ISCSI_CRC_LEN;
3594 tx_size += ISCSI_CRC_LEN;
3595 pr_debug("Attaching CRC32 HeaderDigest for"
3596 " REJECT PDU 0x%08x\n", *header_digest);
3597 }
3598
3599 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003600 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3601 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003602
3603 iov[iov_count].iov_base = &cmd->data_crc;
3604 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3605 tx_size += ISCSI_CRC_LEN;
3606 pr_debug("Attaching CRC32 DataDigest for REJECT"
3607 " PDU 0x%08x\n", cmd->data_crc);
3608 }
3609
3610 cmd->iov_misc_count = iov_count;
3611 cmd->tx_size = tx_size;
3612
3613 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3614 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3615
3616 return 0;
3617}
3618
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003619void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3620{
3621 struct iscsi_thread_set *ts = conn->thread_set;
3622 int ord, cpu;
3623 /*
3624 * thread_id is assigned from iscsit_global->ts_bitmap from
3625 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3626 *
3627 * Here we use thread_id to determine which CPU that this
3628 * iSCSI connection's iscsi_thread_set will be scheduled to
3629 * execute upon.
3630 */
3631 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003632 for_each_online_cpu(cpu) {
3633 if (ord-- == 0) {
3634 cpumask_set_cpu(cpu, conn->conn_cpumask);
3635 return;
3636 }
3637 }
3638 /*
3639 * This should never be reached..
3640 */
3641 dump_stack();
3642 cpumask_setall(conn->conn_cpumask);
3643}
3644
3645static inline void iscsit_thread_check_cpumask(
3646 struct iscsi_conn *conn,
3647 struct task_struct *p,
3648 int mode)
3649{
3650 char buf[128];
3651 /*
3652 * mode == 1 signals iscsi_target_tx_thread() usage.
3653 * mode == 0 signals iscsi_target_rx_thread() usage.
3654 */
3655 if (mode == 1) {
3656 if (!conn->conn_tx_reset_cpumask)
3657 return;
3658 conn->conn_tx_reset_cpumask = 0;
3659 } else {
3660 if (!conn->conn_rx_reset_cpumask)
3661 return;
3662 conn->conn_rx_reset_cpumask = 0;
3663 }
3664 /*
3665 * Update the CPU mask for this single kthread so that
3666 * both TX and RX kthreads are scheduled to run on the
3667 * same CPU.
3668 */
3669 memset(buf, 0, 128);
3670 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003671 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3672}
3673
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003674static int
3675iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003676{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003677 int ret;
3678
3679 switch (state) {
3680 case ISTATE_SEND_R2T:
3681 ret = iscsit_send_r2t(cmd, conn);
3682 if (ret < 0)
3683 goto err;
3684 break;
3685 case ISTATE_REMOVE:
3686 spin_lock_bh(&conn->cmd_lock);
3687 list_del(&cmd->i_conn_node);
3688 spin_unlock_bh(&conn->cmd_lock);
3689
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003690 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003691 break;
3692 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3693 iscsit_mod_nopin_response_timer(conn);
3694 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3695 if (ret < 0)
3696 goto err;
3697 break;
3698 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3699 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3700 if (ret < 0)
3701 goto err;
3702 break;
3703 default:
3704 pr_err("Unknown Opcode: 0x%02x ITT:"
3705 " 0x%08x, i_state: %d on CID: %hu\n",
3706 cmd->iscsi_opcode, cmd->init_task_tag, state,
3707 conn->cid);
3708 goto err;
3709 }
3710
3711 return 0;
3712
3713err:
3714 return -1;
3715}
3716
3717static int
3718iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3719{
3720 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003721 struct iscsi_queue_req *qr;
3722 struct iscsi_cmd *cmd;
3723 u8 state;
3724 int ret;
3725
3726 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3727 atomic_set(&conn->check_immediate_queue, 0);
3728 cmd = qr->cmd;
3729 state = qr->state;
3730 kmem_cache_free(lio_qr_cache, qr);
3731
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003732 ret = t->iscsit_immediate_queue(conn, cmd, state);
3733 if (ret < 0)
3734 return ret;
3735 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003736
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003737 return 0;
3738}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003739
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003740static int
3741iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3742{
3743 int ret;
3744
3745check_rsp_state:
3746 switch (state) {
3747 case ISTATE_SEND_DATAIN:
3748 ret = iscsit_send_datain(cmd, conn);
3749 if (ret < 0)
3750 goto err;
3751 else if (!ret)
3752 /* more drs */
3753 goto check_rsp_state;
3754 else if (ret == 1) {
3755 /* all done */
3756 spin_lock_bh(&cmd->istate_lock);
3757 cmd->i_state = ISTATE_SENT_STATUS;
3758 spin_unlock_bh(&cmd->istate_lock);
3759
3760 if (atomic_read(&conn->check_immediate_queue))
3761 return 1;
3762
3763 return 0;
3764 } else if (ret == 2) {
3765 /* Still must send status,
3766 SCF_TRANSPORT_TASK_SENSE was set */
3767 spin_lock_bh(&cmd->istate_lock);
3768 cmd->i_state = ISTATE_SEND_STATUS;
3769 spin_unlock_bh(&cmd->istate_lock);
3770 state = ISTATE_SEND_STATUS;
3771 goto check_rsp_state;
3772 }
3773
3774 break;
3775 case ISTATE_SEND_STATUS:
3776 case ISTATE_SEND_STATUS_RECOVERY:
3777 ret = iscsit_send_response(cmd, conn);
3778 break;
3779 case ISTATE_SEND_LOGOUTRSP:
3780 ret = iscsit_send_logout(cmd, conn);
3781 break;
3782 case ISTATE_SEND_ASYNCMSG:
3783 ret = iscsit_send_conn_drop_async_message(
3784 cmd, conn);
3785 break;
3786 case ISTATE_SEND_NOPIN:
3787 ret = iscsit_send_nopin(cmd, conn);
3788 break;
3789 case ISTATE_SEND_REJECT:
3790 ret = iscsit_send_reject(cmd, conn);
3791 break;
3792 case ISTATE_SEND_TASKMGTRSP:
3793 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3794 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003795 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003796 ret = iscsit_tmr_post_handler(cmd, conn);
3797 if (ret != 0)
3798 iscsit_fall_back_to_erl0(conn->sess);
3799 break;
3800 case ISTATE_SEND_TEXTRSP:
3801 ret = iscsit_send_text_rsp(cmd, conn);
3802 break;
3803 default:
3804 pr_err("Unknown Opcode: 0x%02x ITT:"
3805 " 0x%08x, i_state: %d on CID: %hu\n",
3806 cmd->iscsi_opcode, cmd->init_task_tag,
3807 state, conn->cid);
3808 goto err;
3809 }
3810 if (ret < 0)
3811 goto err;
3812
3813 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3814 iscsit_tx_thread_wait_for_tcp(conn);
3815 iscsit_unmap_iovec(cmd);
3816 goto err;
3817 }
3818 iscsit_unmap_iovec(cmd);
3819
3820 switch (state) {
3821 case ISTATE_SEND_LOGOUTRSP:
3822 if (!iscsit_logout_post_handler(cmd, conn))
3823 goto restart;
3824 /* fall through */
3825 case ISTATE_SEND_STATUS:
3826 case ISTATE_SEND_ASYNCMSG:
3827 case ISTATE_SEND_NOPIN:
3828 case ISTATE_SEND_STATUS_RECOVERY:
3829 case ISTATE_SEND_TEXTRSP:
3830 case ISTATE_SEND_TASKMGTRSP:
3831 spin_lock_bh(&cmd->istate_lock);
3832 cmd->i_state = ISTATE_SENT_STATUS;
3833 spin_unlock_bh(&cmd->istate_lock);
3834 break;
3835 case ISTATE_SEND_REJECT:
3836 if (cmd->cmd_flags & ICF_REJECT_FAIL_CONN) {
3837 cmd->cmd_flags &= ~ICF_REJECT_FAIL_CONN;
3838 complete(&cmd->reject_comp);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003839 goto err;
3840 }
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003841 complete(&cmd->reject_comp);
3842 break;
3843 default:
3844 pr_err("Unknown Opcode: 0x%02x ITT:"
3845 " 0x%08x, i_state: %d on CID: %hu\n",
3846 cmd->iscsi_opcode, cmd->init_task_tag,
3847 cmd->i_state, conn->cid);
3848 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003849 }
3850
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003851 if (atomic_read(&conn->check_immediate_queue))
3852 return 1;
3853
Andy Grover6f3c0e62012-04-03 15:51:09 -07003854 return 0;
3855
3856err:
3857 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003858restart:
3859 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003860}
3861
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003862static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003863{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003864 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003865 struct iscsi_queue_req *qr;
3866 struct iscsi_cmd *cmd;
3867 u8 state;
3868 int ret;
3869
3870 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3871 cmd = qr->cmd;
3872 state = qr->state;
3873 kmem_cache_free(lio_qr_cache, qr);
3874
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003875 ret = t->iscsit_response_queue(conn, cmd, state);
3876 if (ret == 1 || ret < 0)
3877 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003878 }
3879
3880 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003881}
3882
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003883int iscsi_target_tx_thread(void *arg)
3884{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003885 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003886 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003887 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003888 /*
3889 * Allow ourselves to be interrupted by SIGINT so that a
3890 * connection recovery / failure event can be triggered externally.
3891 */
3892 allow_signal(SIGINT);
3893
3894restart:
3895 conn = iscsi_tx_thread_pre_handler(ts);
3896 if (!conn)
3897 goto out;
3898
Andy Grover6f3c0e62012-04-03 15:51:09 -07003899 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003900
3901 while (!kthread_should_stop()) {
3902 /*
3903 * Ensure that both TX and RX per connection kthreads
3904 * are scheduled to run on the same CPU.
3905 */
3906 iscsit_thread_check_cpumask(conn, current, 1);
3907
Roland Dreierd5627ac2012-10-31 09:16:46 -07003908 wait_event_interruptible(conn->queues_wq,
3909 !iscsit_conn_all_queues_empty(conn) ||
3910 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003911
3912 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3913 signal_pending(current))
3914 goto transport_err;
3915
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003916get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003917 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003918 if (ret < 0)
3919 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003920
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003921 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003922 if (ret == 1)
3923 goto get_immediate;
3924 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003925 goto restart;
3926 else if (ret < 0)
3927 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003928 }
3929
3930transport_err:
3931 iscsit_take_action_for_connection_exit(conn);
3932 goto restart;
3933out:
3934 return 0;
3935}
3936
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003937static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3938{
3939 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3940 struct iscsi_cmd *cmd;
3941 int ret = 0;
3942
3943 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3944 case ISCSI_OP_SCSI_CMD:
3945 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3946 if (!cmd)
3947 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
3948 1, buf, conn);
3949
3950 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3951 break;
3952 case ISCSI_OP_SCSI_DATA_OUT:
3953 ret = iscsit_handle_data_out(conn, buf);
3954 break;
3955 case ISCSI_OP_NOOP_OUT:
3956 cmd = NULL;
3957 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3958 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3959 if (!cmd)
3960 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
3961 1, buf, conn);
3962 }
3963 ret = iscsit_handle_nop_out(conn, cmd, buf);
3964 break;
3965 case ISCSI_OP_SCSI_TMFUNC:
3966 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3967 if (!cmd)
3968 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
3969 1, buf, conn);
3970
3971 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3972 break;
3973 case ISCSI_OP_TEXT:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003974 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3975 if (!cmd)
3976 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
3977 1, buf, conn);
3978
3979 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003980 break;
3981 case ISCSI_OP_LOGOUT:
3982 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3983 if (!cmd)
3984 return iscsit_add_reject(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
3985 1, buf, conn);
3986
3987 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3988 if (ret > 0)
3989 wait_for_completion_timeout(&conn->conn_logout_comp,
3990 SECONDS_FOR_LOGOUT_COMP * HZ);
3991 break;
3992 case ISCSI_OP_SNACK:
3993 ret = iscsit_handle_snack(conn, buf);
3994 break;
3995 default:
3996 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3997 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3998 pr_err("Cannot recover from unknown"
3999 " opcode while ERL=0, closing iSCSI connection.\n");
4000 return -1;
4001 }
4002 if (!conn->conn_ops->OFMarker) {
4003 pr_err("Unable to recover from unknown"
4004 " opcode while OFMarker=No, closing iSCSI"
4005 " connection.\n");
4006 return -1;
4007 }
4008 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4009 pr_err("Unable to recover from unknown"
4010 " opcode, closing iSCSI connection.\n");
4011 return -1;
4012 }
4013 break;
4014 }
4015
4016 return ret;
4017}
4018
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004019int iscsi_target_rx_thread(void *arg)
4020{
4021 int ret;
4022 u8 buffer[ISCSI_HDR_LEN], opcode;
4023 u32 checksum = 0, digest = 0;
4024 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004025 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004026 struct kvec iov;
4027 /*
4028 * Allow ourselves to be interrupted by SIGINT so that a
4029 * connection recovery / failure event can be triggered externally.
4030 */
4031 allow_signal(SIGINT);
4032
4033restart:
4034 conn = iscsi_rx_thread_pre_handler(ts);
4035 if (!conn)
4036 goto out;
4037
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004038 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4039 struct completion comp;
4040 int rc;
4041
4042 init_completion(&comp);
4043 rc = wait_for_completion_interruptible(&comp);
4044 if (rc < 0)
4045 goto transport_err;
4046
4047 goto out;
4048 }
4049
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004050 while (!kthread_should_stop()) {
4051 /*
4052 * Ensure that both TX and RX per connection kthreads
4053 * are scheduled to run on the same CPU.
4054 */
4055 iscsit_thread_check_cpumask(conn, current, 0);
4056
4057 memset(buffer, 0, ISCSI_HDR_LEN);
4058 memset(&iov, 0, sizeof(struct kvec));
4059
4060 iov.iov_base = buffer;
4061 iov.iov_len = ISCSI_HDR_LEN;
4062
4063 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4064 if (ret != ISCSI_HDR_LEN) {
4065 iscsit_rx_thread_wait_for_tcp(conn);
4066 goto transport_err;
4067 }
4068
4069 /*
4070 * Set conn->bad_hdr for use with REJECT PDUs.
4071 */
4072 memcpy(&conn->bad_hdr, &buffer, ISCSI_HDR_LEN);
4073
4074 if (conn->conn_ops->HeaderDigest) {
4075 iov.iov_base = &digest;
4076 iov.iov_len = ISCSI_CRC_LEN;
4077
4078 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4079 if (ret != ISCSI_CRC_LEN) {
4080 iscsit_rx_thread_wait_for_tcp(conn);
4081 goto transport_err;
4082 }
4083
4084 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4085 buffer, ISCSI_HDR_LEN,
4086 0, NULL, (u8 *)&checksum);
4087
4088 if (digest != checksum) {
4089 pr_err("HeaderDigest CRC32C failed,"
4090 " received 0x%08x, computed 0x%08x\n",
4091 digest, checksum);
4092 /*
4093 * Set the PDU to 0xff so it will intentionally
4094 * hit default in the switch below.
4095 */
4096 memset(buffer, 0xff, ISCSI_HDR_LEN);
4097 spin_lock_bh(&conn->sess->session_stats_lock);
4098 conn->sess->conn_digest_errors++;
4099 spin_unlock_bh(&conn->sess->session_stats_lock);
4100 } else {
4101 pr_debug("Got HeaderDigest CRC32C"
4102 " 0x%08x\n", checksum);
4103 }
4104 }
4105
4106 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4107 goto transport_err;
4108
4109 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4110
4111 if (conn->sess->sess_ops->SessionType &&
4112 ((!(opcode & ISCSI_OP_TEXT)) ||
4113 (!(opcode & ISCSI_OP_LOGOUT)))) {
4114 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4115 " while in Discovery Session, rejecting.\n", opcode);
4116 iscsit_add_reject(ISCSI_REASON_PROTOCOL_ERROR, 1,
4117 buffer, conn);
4118 goto transport_err;
4119 }
4120
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004121 ret = iscsi_target_rx_opcode(conn, buffer);
4122 if (ret < 0)
4123 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004124 }
4125
4126transport_err:
4127 if (!signal_pending(current))
4128 atomic_set(&conn->transport_failed, 1);
4129 iscsit_take_action_for_connection_exit(conn);
4130 goto restart;
4131out:
4132 return 0;
4133}
4134
4135static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4136{
4137 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4138 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004139 /*
4140 * We expect this function to only ever be called from either RX or TX
4141 * thread context via iscsit_close_connection() once the other context
4142 * has been reset -> returned sleeping pre-handler state.
4143 */
4144 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004145 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004146
Andy Grover2fbb4712012-04-03 15:51:01 -07004147 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004148 spin_unlock_bh(&conn->cmd_lock);
4149
4150 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004151
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004152 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004153
4154 spin_lock_bh(&conn->cmd_lock);
4155 }
4156 spin_unlock_bh(&conn->cmd_lock);
4157}
4158
4159static void iscsit_stop_timers_for_cmds(
4160 struct iscsi_conn *conn)
4161{
4162 struct iscsi_cmd *cmd;
4163
4164 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004165 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004166 if (cmd->data_direction == DMA_TO_DEVICE)
4167 iscsit_stop_dataout_timer(cmd);
4168 }
4169 spin_unlock_bh(&conn->cmd_lock);
4170}
4171
4172int iscsit_close_connection(
4173 struct iscsi_conn *conn)
4174{
4175 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4176 struct iscsi_session *sess = conn->sess;
4177
4178 pr_debug("Closing iSCSI connection CID %hu on SID:"
4179 " %u\n", conn->cid, sess->sid);
4180 /*
4181 * Always up conn_logout_comp just in case the RX Thread is sleeping
4182 * and the logout response never got sent because the connection
4183 * failed.
4184 */
4185 complete(&conn->conn_logout_comp);
4186
4187 iscsi_release_thread_set(conn);
4188
4189 iscsit_stop_timers_for_cmds(conn);
4190 iscsit_stop_nopin_response_timer(conn);
4191 iscsit_stop_nopin_timer(conn);
4192 iscsit_free_queue_reqs_for_conn(conn);
4193
4194 /*
4195 * During Connection recovery drop unacknowledged out of order
4196 * commands for this connection, and prepare the other commands
4197 * for realligence.
4198 *
4199 * During normal operation clear the out of order commands (but
4200 * do not free the struct iscsi_ooo_cmdsn's) and release all
4201 * struct iscsi_cmds.
4202 */
4203 if (atomic_read(&conn->connection_recovery)) {
4204 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4205 iscsit_prepare_cmds_for_realligance(conn);
4206 } else {
4207 iscsit_clear_ooo_cmdsns_for_conn(conn);
4208 iscsit_release_commands_from_conn(conn);
4209 }
4210
4211 /*
4212 * Handle decrementing session or connection usage count if
4213 * a logout response was not able to be sent because the
4214 * connection failed. Fall back to Session Recovery here.
4215 */
4216 if (atomic_read(&conn->conn_logout_remove)) {
4217 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4218 iscsit_dec_conn_usage_count(conn);
4219 iscsit_dec_session_usage_count(sess);
4220 }
4221 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4222 iscsit_dec_conn_usage_count(conn);
4223
4224 atomic_set(&conn->conn_logout_remove, 0);
4225 atomic_set(&sess->session_reinstatement, 0);
4226 atomic_set(&sess->session_fall_back_to_erl0, 1);
4227 }
4228
4229 spin_lock_bh(&sess->conn_lock);
4230 list_del(&conn->conn_list);
4231
4232 /*
4233 * Attempt to let the Initiator know this connection failed by
4234 * sending an Connection Dropped Async Message on another
4235 * active connection.
4236 */
4237 if (atomic_read(&conn->connection_recovery))
4238 iscsit_build_conn_drop_async_message(conn);
4239
4240 spin_unlock_bh(&sess->conn_lock);
4241
4242 /*
4243 * If connection reinstatement is being performed on this connection,
4244 * up the connection reinstatement semaphore that is being blocked on
4245 * in iscsit_cause_connection_reinstatement().
4246 */
4247 spin_lock_bh(&conn->state_lock);
4248 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4249 spin_unlock_bh(&conn->state_lock);
4250 complete(&conn->conn_wait_comp);
4251 wait_for_completion(&conn->conn_post_wait_comp);
4252 spin_lock_bh(&conn->state_lock);
4253 }
4254
4255 /*
4256 * If connection reinstatement is being performed on this connection
4257 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4258 * connection wait rcfr semaphore that is being blocked on
4259 * an iscsit_connection_reinstatement_rcfr().
4260 */
4261 if (atomic_read(&conn->connection_wait_rcfr)) {
4262 spin_unlock_bh(&conn->state_lock);
4263 complete(&conn->conn_wait_rcfr_comp);
4264 wait_for_completion(&conn->conn_post_wait_comp);
4265 spin_lock_bh(&conn->state_lock);
4266 }
4267 atomic_set(&conn->connection_reinstatement, 1);
4268 spin_unlock_bh(&conn->state_lock);
4269
4270 /*
4271 * If any other processes are accessing this connection pointer we
4272 * must wait until they have completed.
4273 */
4274 iscsit_check_conn_usage_count(conn);
4275
4276 if (conn->conn_rx_hash.tfm)
4277 crypto_free_hash(conn->conn_rx_hash.tfm);
4278 if (conn->conn_tx_hash.tfm)
4279 crypto_free_hash(conn->conn_tx_hash.tfm);
4280
4281 if (conn->conn_cpumask)
4282 free_cpumask_var(conn->conn_cpumask);
4283
4284 kfree(conn->conn_ops);
4285 conn->conn_ops = NULL;
4286
Al Virobf6932f2012-07-21 08:55:18 +01004287 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004288 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004289
4290 if (conn->conn_transport->iscsit_free_conn)
4291 conn->conn_transport->iscsit_free_conn(conn);
4292
4293 iscsit_put_transport(conn->conn_transport);
4294
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004295 conn->thread_set = NULL;
4296
4297 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4298 conn->conn_state = TARG_CONN_STATE_FREE;
4299 kfree(conn);
4300
4301 spin_lock_bh(&sess->conn_lock);
4302 atomic_dec(&sess->nconn);
4303 pr_debug("Decremented iSCSI connection count to %hu from node:"
4304 " %s\n", atomic_read(&sess->nconn),
4305 sess->sess_ops->InitiatorName);
4306 /*
4307 * Make sure that if one connection fails in an non ERL=2 iSCSI
4308 * Session that they all fail.
4309 */
4310 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4311 !atomic_read(&sess->session_logout))
4312 atomic_set(&sess->session_fall_back_to_erl0, 1);
4313
4314 /*
4315 * If this was not the last connection in the session, and we are
4316 * performing session reinstatement or falling back to ERL=0, call
4317 * iscsit_stop_session() without sleeping to shutdown the other
4318 * active connections.
4319 */
4320 if (atomic_read(&sess->nconn)) {
4321 if (!atomic_read(&sess->session_reinstatement) &&
4322 !atomic_read(&sess->session_fall_back_to_erl0)) {
4323 spin_unlock_bh(&sess->conn_lock);
4324 return 0;
4325 }
4326 if (!atomic_read(&sess->session_stop_active)) {
4327 atomic_set(&sess->session_stop_active, 1);
4328 spin_unlock_bh(&sess->conn_lock);
4329 iscsit_stop_session(sess, 0, 0);
4330 return 0;
4331 }
4332 spin_unlock_bh(&sess->conn_lock);
4333 return 0;
4334 }
4335
4336 /*
4337 * If this was the last connection in the session and one of the
4338 * following is occurring:
4339 *
4340 * Session Reinstatement is not being performed, and are falling back
4341 * to ERL=0 call iscsit_close_session().
4342 *
4343 * Session Logout was requested. iscsit_close_session() will be called
4344 * elsewhere.
4345 *
4346 * Session Continuation is not being performed, start the Time2Retain
4347 * handler and check if sleep_on_sess_wait_sem is active.
4348 */
4349 if (!atomic_read(&sess->session_reinstatement) &&
4350 atomic_read(&sess->session_fall_back_to_erl0)) {
4351 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004352 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004353
4354 return 0;
4355 } else if (atomic_read(&sess->session_logout)) {
4356 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4357 sess->session_state = TARG_SESS_STATE_FREE;
4358 spin_unlock_bh(&sess->conn_lock);
4359
4360 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4361 complete(&sess->session_wait_comp);
4362
4363 return 0;
4364 } else {
4365 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4366 sess->session_state = TARG_SESS_STATE_FAILED;
4367
4368 if (!atomic_read(&sess->session_continuation)) {
4369 spin_unlock_bh(&sess->conn_lock);
4370 iscsit_start_time2retain_handler(sess);
4371 } else
4372 spin_unlock_bh(&sess->conn_lock);
4373
4374 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4375 complete(&sess->session_wait_comp);
4376
4377 return 0;
4378 }
4379 spin_unlock_bh(&sess->conn_lock);
4380
4381 return 0;
4382}
4383
4384int iscsit_close_session(struct iscsi_session *sess)
4385{
4386 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4387 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4388
4389 if (atomic_read(&sess->nconn)) {
4390 pr_err("%d connection(s) still exist for iSCSI session"
4391 " to %s\n", atomic_read(&sess->nconn),
4392 sess->sess_ops->InitiatorName);
4393 BUG();
4394 }
4395
4396 spin_lock_bh(&se_tpg->session_lock);
4397 atomic_set(&sess->session_logout, 1);
4398 atomic_set(&sess->session_reinstatement, 1);
4399 iscsit_stop_time2retain_timer(sess);
4400 spin_unlock_bh(&se_tpg->session_lock);
4401
4402 /*
4403 * transport_deregister_session_configfs() will clear the
4404 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4405 * can be setting it again with __transport_register_session() in
4406 * iscsi_post_login_handler() again after the iscsit_stop_session()
4407 * completes in iscsi_np context.
4408 */
4409 transport_deregister_session_configfs(sess->se_sess);
4410
4411 /*
4412 * If any other processes are accessing this session pointer we must
4413 * wait until they have completed. If we are in an interrupt (the
4414 * time2retain handler) and contain and active session usage count we
4415 * restart the timer and exit.
4416 */
4417 if (!in_interrupt()) {
4418 if (iscsit_check_session_usage_count(sess) == 1)
4419 iscsit_stop_session(sess, 1, 1);
4420 } else {
4421 if (iscsit_check_session_usage_count(sess) == 2) {
4422 atomic_set(&sess->session_logout, 0);
4423 iscsit_start_time2retain_handler(sess);
4424 return 0;
4425 }
4426 }
4427
4428 transport_deregister_session(sess->se_sess);
4429
4430 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4431 iscsit_free_connection_recovery_entires(sess);
4432
4433 iscsit_free_all_ooo_cmdsns(sess);
4434
4435 spin_lock_bh(&se_tpg->session_lock);
4436 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4437 sess->session_state = TARG_SESS_STATE_FREE;
4438 pr_debug("Released iSCSI session from node: %s\n",
4439 sess->sess_ops->InitiatorName);
4440 tpg->nsessions--;
4441 if (tpg->tpg_tiqn)
4442 tpg->tpg_tiqn->tiqn_nsessions--;
4443
4444 pr_debug("Decremented number of active iSCSI Sessions on"
4445 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4446
4447 spin_lock(&sess_idr_lock);
4448 idr_remove(&sess_idr, sess->session_index);
4449 spin_unlock(&sess_idr_lock);
4450
4451 kfree(sess->sess_ops);
4452 sess->sess_ops = NULL;
4453 spin_unlock_bh(&se_tpg->session_lock);
4454
4455 kfree(sess);
4456 return 0;
4457}
4458
4459static void iscsit_logout_post_handler_closesession(
4460 struct iscsi_conn *conn)
4461{
4462 struct iscsi_session *sess = conn->sess;
4463
4464 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4465 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4466
4467 atomic_set(&conn->conn_logout_remove, 0);
4468 complete(&conn->conn_logout_comp);
4469
4470 iscsit_dec_conn_usage_count(conn);
4471 iscsit_stop_session(sess, 1, 1);
4472 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004473 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004474}
4475
4476static void iscsit_logout_post_handler_samecid(
4477 struct iscsi_conn *conn)
4478{
4479 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4480 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4481
4482 atomic_set(&conn->conn_logout_remove, 0);
4483 complete(&conn->conn_logout_comp);
4484
4485 iscsit_cause_connection_reinstatement(conn, 1);
4486 iscsit_dec_conn_usage_count(conn);
4487}
4488
4489static void iscsit_logout_post_handler_diffcid(
4490 struct iscsi_conn *conn,
4491 u16 cid)
4492{
4493 struct iscsi_conn *l_conn;
4494 struct iscsi_session *sess = conn->sess;
4495
4496 if (!sess)
4497 return;
4498
4499 spin_lock_bh(&sess->conn_lock);
4500 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4501 if (l_conn->cid == cid) {
4502 iscsit_inc_conn_usage_count(l_conn);
4503 break;
4504 }
4505 }
4506 spin_unlock_bh(&sess->conn_lock);
4507
4508 if (!l_conn)
4509 return;
4510
4511 if (l_conn->sock)
4512 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4513
4514 spin_lock_bh(&l_conn->state_lock);
4515 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4516 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4517 spin_unlock_bh(&l_conn->state_lock);
4518
4519 iscsit_cause_connection_reinstatement(l_conn, 1);
4520 iscsit_dec_conn_usage_count(l_conn);
4521}
4522
4523/*
4524 * Return of 0 causes the TX thread to restart.
4525 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004526int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004527 struct iscsi_cmd *cmd,
4528 struct iscsi_conn *conn)
4529{
4530 int ret = 0;
4531
4532 switch (cmd->logout_reason) {
4533 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4534 switch (cmd->logout_response) {
4535 case ISCSI_LOGOUT_SUCCESS:
4536 case ISCSI_LOGOUT_CLEANUP_FAILED:
4537 default:
4538 iscsit_logout_post_handler_closesession(conn);
4539 break;
4540 }
4541 ret = 0;
4542 break;
4543 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4544 if (conn->cid == cmd->logout_cid) {
4545 switch (cmd->logout_response) {
4546 case ISCSI_LOGOUT_SUCCESS:
4547 case ISCSI_LOGOUT_CLEANUP_FAILED:
4548 default:
4549 iscsit_logout_post_handler_samecid(conn);
4550 break;
4551 }
4552 ret = 0;
4553 } else {
4554 switch (cmd->logout_response) {
4555 case ISCSI_LOGOUT_SUCCESS:
4556 iscsit_logout_post_handler_diffcid(conn,
4557 cmd->logout_cid);
4558 break;
4559 case ISCSI_LOGOUT_CID_NOT_FOUND:
4560 case ISCSI_LOGOUT_CLEANUP_FAILED:
4561 default:
4562 break;
4563 }
4564 ret = 1;
4565 }
4566 break;
4567 case ISCSI_LOGOUT_REASON_RECOVERY:
4568 switch (cmd->logout_response) {
4569 case ISCSI_LOGOUT_SUCCESS:
4570 case ISCSI_LOGOUT_CID_NOT_FOUND:
4571 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4572 case ISCSI_LOGOUT_CLEANUP_FAILED:
4573 default:
4574 break;
4575 }
4576 ret = 1;
4577 break;
4578 default:
4579 break;
4580
4581 }
4582 return ret;
4583}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004584EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004585
4586void iscsit_fail_session(struct iscsi_session *sess)
4587{
4588 struct iscsi_conn *conn;
4589
4590 spin_lock_bh(&sess->conn_lock);
4591 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4592 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4593 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4594 }
4595 spin_unlock_bh(&sess->conn_lock);
4596
4597 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4598 sess->session_state = TARG_SESS_STATE_FAILED;
4599}
4600
4601int iscsit_free_session(struct iscsi_session *sess)
4602{
4603 u16 conn_count = atomic_read(&sess->nconn);
4604 struct iscsi_conn *conn, *conn_tmp = NULL;
4605 int is_last;
4606
4607 spin_lock_bh(&sess->conn_lock);
4608 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4609
4610 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4611 conn_list) {
4612 if (conn_count == 0)
4613 break;
4614
4615 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4616 is_last = 1;
4617 } else {
4618 iscsit_inc_conn_usage_count(conn_tmp);
4619 is_last = 0;
4620 }
4621 iscsit_inc_conn_usage_count(conn);
4622
4623 spin_unlock_bh(&sess->conn_lock);
4624 iscsit_cause_connection_reinstatement(conn, 1);
4625 spin_lock_bh(&sess->conn_lock);
4626
4627 iscsit_dec_conn_usage_count(conn);
4628 if (is_last == 0)
4629 iscsit_dec_conn_usage_count(conn_tmp);
4630
4631 conn_count--;
4632 }
4633
4634 if (atomic_read(&sess->nconn)) {
4635 spin_unlock_bh(&sess->conn_lock);
4636 wait_for_completion(&sess->session_wait_comp);
4637 } else
4638 spin_unlock_bh(&sess->conn_lock);
4639
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004640 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004641 return 0;
4642}
4643
4644void iscsit_stop_session(
4645 struct iscsi_session *sess,
4646 int session_sleep,
4647 int connection_sleep)
4648{
4649 u16 conn_count = atomic_read(&sess->nconn);
4650 struct iscsi_conn *conn, *conn_tmp = NULL;
4651 int is_last;
4652
4653 spin_lock_bh(&sess->conn_lock);
4654 if (session_sleep)
4655 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4656
4657 if (connection_sleep) {
4658 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4659 conn_list) {
4660 if (conn_count == 0)
4661 break;
4662
4663 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4664 is_last = 1;
4665 } else {
4666 iscsit_inc_conn_usage_count(conn_tmp);
4667 is_last = 0;
4668 }
4669 iscsit_inc_conn_usage_count(conn);
4670
4671 spin_unlock_bh(&sess->conn_lock);
4672 iscsit_cause_connection_reinstatement(conn, 1);
4673 spin_lock_bh(&sess->conn_lock);
4674
4675 iscsit_dec_conn_usage_count(conn);
4676 if (is_last == 0)
4677 iscsit_dec_conn_usage_count(conn_tmp);
4678 conn_count--;
4679 }
4680 } else {
4681 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4682 iscsit_cause_connection_reinstatement(conn, 0);
4683 }
4684
4685 if (session_sleep && atomic_read(&sess->nconn)) {
4686 spin_unlock_bh(&sess->conn_lock);
4687 wait_for_completion(&sess->session_wait_comp);
4688 } else
4689 spin_unlock_bh(&sess->conn_lock);
4690}
4691
4692int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4693{
4694 struct iscsi_session *sess;
4695 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4696 struct se_session *se_sess, *se_sess_tmp;
4697 int session_count = 0;
4698
4699 spin_lock_bh(&se_tpg->session_lock);
4700 if (tpg->nsessions && !force) {
4701 spin_unlock_bh(&se_tpg->session_lock);
4702 return -1;
4703 }
4704
4705 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4706 sess_list) {
4707 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4708
4709 spin_lock(&sess->conn_lock);
4710 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4711 atomic_read(&sess->session_logout) ||
4712 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4713 spin_unlock(&sess->conn_lock);
4714 continue;
4715 }
4716 atomic_set(&sess->session_reinstatement, 1);
4717 spin_unlock(&sess->conn_lock);
4718 spin_unlock_bh(&se_tpg->session_lock);
4719
4720 iscsit_free_session(sess);
4721 spin_lock_bh(&se_tpg->session_lock);
4722
4723 session_count++;
4724 }
4725 spin_unlock_bh(&se_tpg->session_lock);
4726
4727 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4728 " Group: %hu\n", session_count, tpg->tpgt);
4729 return 0;
4730}
4731
4732MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4733MODULE_VERSION("4.1.x");
4734MODULE_AUTHOR("nab@Linux-iSCSI.org");
4735MODULE_LICENSE("GPL");
4736
4737module_init(iscsi_target_init_module);
4738module_exit(iscsi_target_cleanup_module);