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