blob: 4319dad7d9195f6c210ebac4ab7f380386c4d3fe [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 Bellingerba159912013-07-03 03:48:24 -0700631 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000632 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700633 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000634{
635 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000636
637 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
638 if (!cmd)
639 return -1;
640
641 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700642 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000643
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100644 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000645 if (!cmd->buf_ptr) {
646 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700647 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000648 return -1;
649 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000650
651 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700652 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000653 spin_unlock_bh(&conn->cmd_lock);
654
655 cmd->i_state = ISTATE_SEND_REJECT;
656 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
657
Nicholas Bellingerba159912013-07-03 03:48:24 -0700658 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000659}
660
Nicholas Bellingerba159912013-07-03 03:48:24 -0700661static int iscsit_add_reject_from_cmd(
662 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000663 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700664 bool add_to_conn,
665 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000666{
667 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000668
669 if (!cmd->conn) {
670 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
671 cmd->init_task_tag);
672 return -1;
673 }
674 conn = cmd->conn;
675
676 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700677 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000678
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100679 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000680 if (!cmd->buf_ptr) {
681 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700682 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000683 return -1;
684 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000685
686 if (add_to_conn) {
687 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700688 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000689 spin_unlock_bh(&conn->cmd_lock);
690 }
691
692 cmd->i_state = ISTATE_SEND_REJECT;
693 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800694 /*
695 * Perform the kref_put now if se_cmd has already been setup by
696 * scsit_setup_scsi_cmd()
697 */
698 if (cmd->se_cmd.se_tfo != NULL) {
699 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
700 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
701 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700702 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000703}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700704
705static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
706 unsigned char *buf)
707{
708 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
709}
710
711int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
712{
713 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
714}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000715
716/*
717 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700718 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000719 */
720static int iscsit_map_iovec(
721 struct iscsi_cmd *cmd,
722 struct kvec *iov,
723 u32 data_offset,
724 u32 data_length)
725{
726 u32 i = 0;
727 struct scatterlist *sg;
728 unsigned int page_off;
729
730 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700731 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000732 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700733 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000734 page_off = (data_offset % PAGE_SIZE);
735
736 cmd->first_data_sg = sg;
737 cmd->first_data_sg_off = page_off;
738
739 while (data_length) {
740 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
741
742 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
743 iov[i].iov_len = cur_len;
744
745 data_length -= cur_len;
746 page_off = 0;
747 sg = sg_next(sg);
748 i++;
749 }
750
751 cmd->kmapped_nents = i;
752
753 return i;
754}
755
756static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
757{
758 u32 i;
759 struct scatterlist *sg;
760
761 sg = cmd->first_data_sg;
762
763 for (i = 0; i < cmd->kmapped_nents; i++)
764 kunmap(sg_page(&sg[i]));
765}
766
767static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
768{
769 struct iscsi_cmd *cmd;
770
771 conn->exp_statsn = exp_statsn;
772
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800773 if (conn->sess->sess_ops->RDMAExtensions)
774 return;
775
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000776 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700777 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000778 spin_lock(&cmd->istate_lock);
779 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800780 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000781 cmd->i_state = ISTATE_REMOVE;
782 spin_unlock(&cmd->istate_lock);
783 iscsit_add_cmd_to_immediate_queue(cmd, conn,
784 cmd->i_state);
785 continue;
786 }
787 spin_unlock(&cmd->istate_lock);
788 }
789 spin_unlock_bh(&conn->cmd_lock);
790}
791
792static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
793{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700794 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000795
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400796 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000797
798 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
799 if (!cmd->iov_data) {
800 pr_err("Unable to allocate cmd->iov_data\n");
801 return -ENOMEM;
802 }
803
804 cmd->orig_iov_data_count = iov_count;
805 return 0;
806}
807
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800808int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
809 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000810{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800811 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000812 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700813 int iscsi_task_attr;
814 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000815
816 spin_lock_bh(&conn->sess->session_stats_lock);
817 conn->sess->cmd_pdus++;
818 if (conn->sess->se_sess->se_node_acl) {
819 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
820 conn->sess->se_sess->se_node_acl->num_cmds++;
821 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
822 }
823 spin_unlock_bh(&conn->sess->session_stats_lock);
824
825 hdr = (struct iscsi_scsi_req *) buf;
826 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000827
828 /* FIXME; Add checks for AdditionalHeaderSegment */
829
830 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
831 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
832 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
833 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700834 return iscsit_add_reject_cmd(cmd,
835 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000836 }
837
838 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
839 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
840 /*
841 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
842 * that adds support for RESERVE/RELEASE. There is a bug
843 * add with this new functionality that sets R/W bits when
844 * neither CDB carries any READ or WRITE datapayloads.
845 */
846 if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) {
847 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
848 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
849 goto done;
850 }
851
852 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
853 " set when Expected Data Transfer Length is 0 for"
854 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700855 return iscsit_add_reject_cmd(cmd,
856 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000857 }
858done:
859
860 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
861 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
862 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
863 " MUST be set if Expected Data Transfer Length is not 0."
864 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700865 return iscsit_add_reject_cmd(cmd,
866 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000867 }
868
869 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
870 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
871 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700872 return iscsit_add_reject_cmd(cmd,
873 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000874 }
875
876 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
877 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
878 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700879 return iscsit_add_reject_cmd(cmd,
880 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000881 }
882
883 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
884 pr_err("ImmediateData=No but DataSegmentLength=%u,"
885 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700886 return iscsit_add_reject_cmd(cmd,
887 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000888 }
889
Nicholas Bellingerba159912013-07-03 03:48:24 -0700890 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000891 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
892 pr_err("Expected Data Transfer Length and Length of"
893 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
894 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700895 return iscsit_add_reject_cmd(cmd,
896 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000897 }
898
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400899 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000900 pr_err("DataSegmentLength: %u is greater than"
901 " EDTL: %u, protocol error.\n", payload_length,
902 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700903 return iscsit_add_reject_cmd(cmd,
904 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000905 }
906
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700907 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000908 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700909 " MaxXmitDataSegmentLength: %u, protocol error.\n",
910 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700911 return iscsit_add_reject_cmd(cmd,
912 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000913 }
914
915 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
916 pr_err("DataSegmentLength: %u is greater than"
917 " FirstBurstLength: %u, protocol error.\n",
918 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700919 return iscsit_add_reject_cmd(cmd,
920 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000921 }
922
923 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
924 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
925 DMA_NONE;
926
Andy Groverd28b11692012-04-03 15:51:22 -0700927 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700928 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
929 /*
930 * Figure out the SAM Task Attribute for the incoming SCSI CDB
931 */
932 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
933 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
934 sam_task_attr = MSG_SIMPLE_TAG;
935 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
936 sam_task_attr = MSG_ORDERED_TAG;
937 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
938 sam_task_attr = MSG_HEAD_TAG;
939 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
940 sam_task_attr = MSG_ACA_TAG;
941 else {
942 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
943 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
944 sam_task_attr = MSG_SIMPLE_TAG;
945 }
946
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000947 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
948 cmd->i_state = ISTATE_NEW_CMD;
949 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
950 cmd->immediate_data = (payload_length) ? 1 : 0;
951 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
952 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
953 if (cmd->unsolicited_data)
954 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
955
956 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
957 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
958 spin_lock_bh(&conn->sess->ttt_lock);
959 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
960 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
961 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
962 spin_unlock_bh(&conn->sess->ttt_lock);
963 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
964 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400965 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
966 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000967 cmd->first_burst_len = payload_length;
968
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800969 if (!conn->sess->sess_ops->RDMAExtensions &&
970 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000971 struct iscsi_datain_req *dr;
972
973 dr = iscsit_allocate_datain_req();
974 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700975 return iscsit_add_reject_cmd(cmd,
976 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000977
978 iscsit_attach_datain_req(cmd, dr);
979 }
980
981 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700982 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
983 */
984 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400985 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
986 cmd->data_direction, sam_task_attr,
987 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700988
989 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
990 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800991 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
992 conn->cid);
993
994 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -0700995
Christoph Hellwigde103c92012-11-06 12:24:09 -0800996 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
997 scsilun_to_int(&hdr->lun));
998 if (cmd->sense_reason)
999 goto attach_cmd;
1000
1001 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1002 if (cmd->sense_reason) {
1003 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001004 return iscsit_add_reject_cmd(cmd,
1005 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001006 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001007
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001008 goto attach_cmd;
1009 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001010
Christoph Hellwigde103c92012-11-06 12:24:09 -08001011 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001012 return iscsit_add_reject_cmd(cmd,
1013 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001014 }
1015
1016attach_cmd:
1017 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001018 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001019 spin_unlock_bh(&conn->cmd_lock);
1020 /*
1021 * Check if we need to delay processing because of ALUA
1022 * Active/NonOptimized primary access state..
1023 */
1024 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001025
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001026 return 0;
1027}
1028EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001029
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001030void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1031{
1032 iscsit_set_dataout_sequence_values(cmd);
1033
1034 spin_lock_bh(&cmd->dataout_timeout_lock);
1035 iscsit_start_dataout_timer(cmd, cmd->conn);
1036 spin_unlock_bh(&cmd->dataout_timeout_lock);
1037}
1038EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1039
1040int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1041 struct iscsi_scsi_req *hdr)
1042{
1043 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001044 /*
1045 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1046 * the Immediate Bit is not set, and no Immediate
1047 * Data is attached.
1048 *
1049 * A PDU/CmdSN carrying Immediate Data can only
1050 * be processed after the DataCRC has passed.
1051 * If the DataCRC fails, the CmdSN MUST NOT
1052 * be acknowledged. (See below)
1053 */
1054 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001055 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1056 (unsigned char *)hdr, hdr->cmdsn);
1057 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1058 return -1;
1059 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001060 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001061 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001062 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001063 }
1064
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001065 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001066
1067 /*
1068 * If no Immediate Data is attached, it's OK to return now.
1069 */
1070 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001071 if (!cmd->sense_reason && cmd->unsolicited_data)
1072 iscsit_set_unsoliticed_dataout(cmd);
1073 if (!cmd->sense_reason)
1074 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001075
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001076 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001077 return 0;
1078 }
1079
1080 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001081 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1082 * execution. These exceptions are processed in CmdSN order using
1083 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001084 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001085 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001086 if (cmd->reject_reason)
1087 return 0;
1088
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001089 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1090 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001091 }
1092 /*
1093 * Call directly into transport_generic_new_cmd() to perform
1094 * the backend memory allocation.
1095 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001096 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001097 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001098 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001099
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001100 return 0;
1101}
1102EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1103
1104static int
1105iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1106 bool dump_payload)
1107{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001108 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001109 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1110 /*
1111 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1112 */
1113 if (dump_payload == true)
1114 goto after_immediate_data;
1115
1116 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1117 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001118after_immediate_data:
1119 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1120 /*
1121 * A PDU/CmdSN carrying Immediate Data passed
1122 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1123 * Immediate Bit is not set.
1124 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001125 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1126 (unsigned char *)hdr, hdr->cmdsn);
1127 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
1128 return -1;
1129 } else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1130 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1131 return 0;
1132 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001133
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001134 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001135 int rc;
1136
1137 rc = iscsit_dump_data_payload(cmd->conn,
1138 cmd->first_burst_len, 1);
1139 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1140 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001141 } else if (cmd->unsolicited_data)
1142 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001143
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001144 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1145 /*
1146 * Immediate Data failed DataCRC and ERL>=1,
1147 * silently drop this PDU and let the initiator
1148 * plug the CmdSN gap.
1149 *
1150 * FIXME: Send Unsolicited NOPIN with reserved
1151 * TTT here to help the initiator figure out
1152 * the missing CmdSN, although they should be
1153 * intelligent enough to determine the missing
1154 * CmdSN and issue a retry to plug the sequence.
1155 */
1156 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001157 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001158 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1159 return -1;
1160
1161 return 0;
1162}
1163
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001164static int
1165iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1166 unsigned char *buf)
1167{
1168 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1169 int rc, immed_data;
1170 bool dump_payload = false;
1171
1172 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1173 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001174 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001175 /*
1176 * Allocation iovecs needed for struct socket operations for
1177 * traditional iSCSI block I/O.
1178 */
1179 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001180 return iscsit_add_reject_cmd(cmd,
1181 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001182 }
1183 immed_data = cmd->immediate_data;
1184
1185 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1186 if (rc < 0)
1187 return rc;
1188 else if (rc > 0)
1189 dump_payload = true;
1190
1191 if (!immed_data)
1192 return 0;
1193
1194 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1195}
1196
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001197static u32 iscsit_do_crypto_hash_sg(
1198 struct hash_desc *hash,
1199 struct iscsi_cmd *cmd,
1200 u32 data_offset,
1201 u32 data_length,
1202 u32 padding,
1203 u8 *pad_bytes)
1204{
1205 u32 data_crc;
1206 u32 i;
1207 struct scatterlist *sg;
1208 unsigned int page_off;
1209
1210 crypto_hash_init(hash);
1211
1212 sg = cmd->first_data_sg;
1213 page_off = cmd->first_data_sg_off;
1214
1215 i = 0;
1216 while (data_length) {
1217 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1218
1219 crypto_hash_update(hash, &sg[i], cur_len);
1220
1221 data_length -= cur_len;
1222 page_off = 0;
1223 i++;
1224 }
1225
1226 if (padding) {
1227 struct scatterlist pad_sg;
1228
1229 sg_init_one(&pad_sg, pad_bytes, padding);
1230 crypto_hash_update(hash, &pad_sg, padding);
1231 }
1232 crypto_hash_final(hash, (u8 *) &data_crc);
1233
1234 return data_crc;
1235}
1236
1237static void iscsit_do_crypto_hash_buf(
1238 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001239 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001240 u32 payload_length,
1241 u32 padding,
1242 u8 *pad_bytes,
1243 u8 *data_crc)
1244{
1245 struct scatterlist sg;
1246
1247 crypto_hash_init(hash);
1248
Jörn Engel8359cf42011-11-24 02:05:51 +01001249 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001250 crypto_hash_update(hash, &sg, payload_length);
1251
1252 if (padding) {
1253 sg_init_one(&sg, pad_bytes, padding);
1254 crypto_hash_update(hash, &sg, padding);
1255 }
1256 crypto_hash_final(hash, data_crc);
1257}
1258
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001259int
1260iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1261 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001262{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001263 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001264 struct iscsi_cmd *cmd = NULL;
1265 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001266 u32 payload_length = ntoh24(hdr->dlength);
1267 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001268
1269 if (!payload_length) {
1270 pr_err("DataOUT payload is ZERO, protocol error.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001271 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1272 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001273 }
1274
1275 /* iSCSI write */
1276 spin_lock_bh(&conn->sess->session_stats_lock);
1277 conn->sess->rx_data_octets += payload_length;
1278 if (conn->sess->se_sess->se_node_acl) {
1279 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1280 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1281 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1282 }
1283 spin_unlock_bh(&conn->sess->session_stats_lock);
1284
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001285 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001286 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001287 " MaxXmitDataSegmentLength: %u\n", payload_length,
1288 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001289 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1290 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001291 }
1292
1293 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1294 payload_length);
1295 if (!cmd)
1296 return 0;
1297
1298 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1299 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001300 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001301 payload_length, conn->cid);
1302
1303 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1304 pr_err("Command ITT: 0x%08x received DataOUT after"
1305 " last DataOUT received, dumping payload\n",
1306 cmd->init_task_tag);
1307 return iscsit_dump_data_payload(conn, payload_length, 1);
1308 }
1309
1310 if (cmd->data_direction != DMA_TO_DEVICE) {
1311 pr_err("Command ITT: 0x%08x received DataOUT for a"
1312 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001313 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001314 }
1315 se_cmd = &cmd->se_cmd;
1316 iscsit_mod_dataout_timer(cmd);
1317
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001318 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001319 pr_err("DataOut Offset: %u, Length %u greater than"
1320 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001321 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001322 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001323 }
1324
1325 if (cmd->unsolicited_data) {
1326 int dump_unsolicited_data = 0;
1327
1328 if (conn->sess->sess_ops->InitialR2T) {
1329 pr_err("Received unexpected unsolicited data"
1330 " while InitialR2T=Yes, protocol error.\n");
1331 transport_send_check_condition_and_sense(&cmd->se_cmd,
1332 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1333 return -1;
1334 }
1335 /*
1336 * Special case for dealing with Unsolicited DataOUT
1337 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1338 * failures;
1339 */
1340
1341 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001342 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001343 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001344 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001345
1346 if (dump_unsolicited_data) {
1347 /*
1348 * Check if a delayed TASK_ABORTED status needs to
1349 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1350 * received with the unsolicitied data out.
1351 */
1352 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1353 iscsit_stop_dataout_timer(cmd);
1354
1355 transport_check_aborted_status(se_cmd,
1356 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1357 return iscsit_dump_data_payload(conn, payload_length, 1);
1358 }
1359 } else {
1360 /*
1361 * For the normal solicited data path:
1362 *
1363 * Check for a delayed TASK_ABORTED status and dump any
1364 * incoming data out payload if one exists. Also, when the
1365 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1366 * data out sequence, we decrement outstanding_r2ts. Once
1367 * outstanding_r2ts reaches zero, go ahead and send the delayed
1368 * TASK_ABORTED status.
1369 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001370 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001371 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1372 if (--cmd->outstanding_r2ts < 1) {
1373 iscsit_stop_dataout_timer(cmd);
1374 transport_check_aborted_status(
1375 se_cmd, 1);
1376 }
1377
1378 return iscsit_dump_data_payload(conn, payload_length, 1);
1379 }
1380 }
1381 /*
1382 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1383 * within-command recovery checks before receiving the payload.
1384 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001385 rc = iscsit_check_pre_dataout(cmd, buf);
1386 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001387 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001388 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001389 return -1;
1390
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001391 *out_cmd = cmd;
1392 return 0;
1393}
1394EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1395
1396static int
1397iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1398 struct iscsi_data *hdr)
1399{
1400 struct kvec *iov;
1401 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1402 u32 payload_length = ntoh24(hdr->dlength);
1403 int iov_ret, data_crc_failed = 0;
1404
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001405 rx_size += payload_length;
1406 iov = &cmd->iov_data[0];
1407
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001408 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1409 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001410 if (iov_ret < 0)
1411 return -1;
1412
1413 iov_count += iov_ret;
1414
1415 padding = ((-payload_length) & 3);
1416 if (padding != 0) {
1417 iov[iov_count].iov_base = cmd->pad_bytes;
1418 iov[iov_count++].iov_len = padding;
1419 rx_size += padding;
1420 pr_debug("Receiving %u padding bytes.\n", padding);
1421 }
1422
1423 if (conn->conn_ops->DataDigest) {
1424 iov[iov_count].iov_base = &checksum;
1425 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1426 rx_size += ISCSI_CRC_LEN;
1427 }
1428
1429 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1430
1431 iscsit_unmap_iovec(cmd);
1432
1433 if (rx_got != rx_size)
1434 return -1;
1435
1436 if (conn->conn_ops->DataDigest) {
1437 u32 data_crc;
1438
1439 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001440 be32_to_cpu(hdr->offset),
1441 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001442 cmd->pad_bytes);
1443
1444 if (checksum != data_crc) {
1445 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1446 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1447 " does not match computed 0x%08x\n",
1448 hdr->itt, hdr->offset, payload_length,
1449 hdr->datasn, checksum, data_crc);
1450 data_crc_failed = 1;
1451 } else {
1452 pr_debug("Got CRC32C DataDigest 0x%08x for"
1453 " %u bytes of Data Out\n", checksum,
1454 payload_length);
1455 }
1456 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001457
1458 return data_crc_failed;
1459}
1460
1461int
1462iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1463 bool data_crc_failed)
1464{
1465 struct iscsi_conn *conn = cmd->conn;
1466 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001467 /*
1468 * Increment post receive data and CRC values or perform
1469 * within-command recovery.
1470 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001471 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1472 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001473 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001474 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001475 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001476 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1477 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001478 /*
1479 * Handle extra special case for out of order
1480 * Unsolicited Data Out.
1481 */
1482 spin_lock_bh(&cmd->istate_lock);
1483 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1484 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1485 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1486 spin_unlock_bh(&cmd->istate_lock);
1487
1488 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001489 if (ooo_cmdsn)
1490 return 0;
1491 target_execute_cmd(&cmd->se_cmd);
1492 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001493 } else /* DATAOUT_CANNOT_RECOVER */
1494 return -1;
1495
1496 return 0;
1497}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001498EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001499
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001500static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1501{
1502 struct iscsi_cmd *cmd;
1503 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1504 int rc;
1505 bool data_crc_failed = false;
1506
1507 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1508 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001509 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001510 else if (!cmd)
1511 return 0;
1512
1513 rc = iscsit_get_dataout(conn, cmd, hdr);
1514 if (rc < 0)
1515 return rc;
1516 else if (rc > 0)
1517 data_crc_failed = true;
1518
1519 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1520}
1521
Nicholas Bellinger778de362013-06-14 16:07:47 -07001522int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1523 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001524{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001525 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001526
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001527 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001528 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1529 " not set, protocol error.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001530 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1531 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001532 }
1533
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001534 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001535 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001536 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001537 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001538 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001539 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1540 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001541 }
1542
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001543 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001544 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001545 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001546 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1547 payload_length);
1548 /*
1549 * This is not a response to a Unsolicited NopIN, which means
1550 * it can either be a NOPOUT ping request (with a valid ITT),
1551 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1552 * Either way, make sure we allocate an struct iscsi_cmd, as both
1553 * can contain ping data.
1554 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001555 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001556 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1557 cmd->i_state = ISTATE_SEND_NOPIN;
1558 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1559 1 : 0);
1560 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1561 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001562 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1563 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001564 cmd->data_direction = DMA_NONE;
1565 }
1566
Nicholas Bellinger778de362013-06-14 16:07:47 -07001567 return 0;
1568}
1569EXPORT_SYMBOL(iscsit_setup_nop_out);
1570
1571int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1572 struct iscsi_nopout *hdr)
1573{
1574 struct iscsi_cmd *cmd_p = NULL;
1575 int cmdsn_ret = 0;
1576 /*
1577 * Initiator is expecting a NopIN ping reply..
1578 */
1579 if (hdr->itt != RESERVED_ITT) {
1580 BUG_ON(!cmd);
1581
1582 spin_lock_bh(&conn->cmd_lock);
1583 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1584 spin_unlock_bh(&conn->cmd_lock);
1585
1586 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1587
1588 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1589 iscsit_add_cmd_to_response_queue(cmd, conn,
1590 cmd->i_state);
1591 return 0;
1592 }
1593
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001594 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1595 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001596 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1597 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001598 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001599 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001600
1601 return 0;
1602 }
1603 /*
1604 * This was a response to a unsolicited NOPIN ping.
1605 */
1606 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1607 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1608 if (!cmd_p)
1609 return -EINVAL;
1610
1611 iscsit_stop_nopin_response_timer(conn);
1612
1613 cmd_p->i_state = ISTATE_REMOVE;
1614 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1615
1616 iscsit_start_nopin_timer(conn);
1617 return 0;
1618 }
1619 /*
1620 * Otherwise, initiator is not expecting a NOPIN is response.
1621 * Just ignore for now.
1622 */
1623 return 0;
1624}
1625EXPORT_SYMBOL(iscsit_process_nop_out);
1626
1627static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1628 unsigned char *buf)
1629{
1630 unsigned char *ping_data = NULL;
1631 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1632 struct kvec *iov = NULL;
1633 u32 payload_length = ntoh24(hdr->dlength);
1634 int ret;
1635
1636 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1637 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001638 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001639 /*
1640 * Handle NOP-OUT payload for traditional iSCSI sockets
1641 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001642 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001643 u32 checksum, data_crc, padding = 0;
1644 int niov = 0, rx_got, rx_size = payload_length;
1645
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001646 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1647 if (!ping_data) {
1648 pr_err("Unable to allocate memory for"
1649 " NOPOUT ping data.\n");
1650 ret = -1;
1651 goto out;
1652 }
1653
1654 iov = &cmd->iov_misc[0];
1655 iov[niov].iov_base = ping_data;
1656 iov[niov++].iov_len = payload_length;
1657
1658 padding = ((-payload_length) & 3);
1659 if (padding != 0) {
1660 pr_debug("Receiving %u additional bytes"
1661 " for padding.\n", padding);
1662 iov[niov].iov_base = &cmd->pad_bytes;
1663 iov[niov++].iov_len = padding;
1664 rx_size += padding;
1665 }
1666 if (conn->conn_ops->DataDigest) {
1667 iov[niov].iov_base = &checksum;
1668 iov[niov++].iov_len = ISCSI_CRC_LEN;
1669 rx_size += ISCSI_CRC_LEN;
1670 }
1671
1672 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1673 if (rx_got != rx_size) {
1674 ret = -1;
1675 goto out;
1676 }
1677
1678 if (conn->conn_ops->DataDigest) {
1679 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1680 ping_data, payload_length,
1681 padding, cmd->pad_bytes,
1682 (u8 *)&data_crc);
1683
1684 if (checksum != data_crc) {
1685 pr_err("Ping data CRC32C DataDigest"
1686 " 0x%08x does not match computed 0x%08x\n",
1687 checksum, data_crc);
1688 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1689 pr_err("Unable to recover from"
1690 " NOPOUT Ping DataCRC failure while in"
1691 " ERL=0.\n");
1692 ret = -1;
1693 goto out;
1694 } else {
1695 /*
1696 * Silently drop this PDU and let the
1697 * initiator plug the CmdSN gap.
1698 */
1699 pr_debug("Dropping NOPOUT"
1700 " Command CmdSN: 0x%08x due to"
1701 " DataCRC error.\n", hdr->cmdsn);
1702 ret = 0;
1703 goto out;
1704 }
1705 } else {
1706 pr_debug("Got CRC32C DataDigest"
1707 " 0x%08x for %u bytes of ping data.\n",
1708 checksum, payload_length);
1709 }
1710 }
1711
1712 ping_data[payload_length] = '\0';
1713 /*
1714 * Attach ping data to struct iscsi_cmd->buf_ptr.
1715 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001716 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001717 cmd->buf_ptr_size = payload_length;
1718
1719 pr_debug("Got %u bytes of NOPOUT ping"
1720 " data.\n", payload_length);
1721 pr_debug("Ping Data: \"%s\"\n", ping_data);
1722 }
1723
Nicholas Bellinger778de362013-06-14 16:07:47 -07001724 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001725out:
1726 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001727 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001728
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001729 kfree(ping_data);
1730 return ret;
1731}
1732
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001733int
1734iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1735 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001736{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001737 struct se_tmr_req *se_tmr;
1738 struct iscsi_tmr_req *tmr_req;
1739 struct iscsi_tm *hdr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001740 int out_of_order_cmdsn = 0;
1741 int ret;
1742 u8 function;
1743
1744 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001745 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1746 function = hdr->flags;
1747
1748 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1749 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1750 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1751 hdr->rtt, hdr->refcmdsn, conn->cid);
1752
1753 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1754 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001755 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001756 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001757 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001758 }
1759
1760 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1761 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1762 pr_err("Task Management Request TASK_REASSIGN not"
1763 " issued as immediate command, bad iSCSI Initiator"
1764 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001765 return iscsit_add_reject_cmd(cmd,
1766 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001767 }
1768 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001769 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1770 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001771
Andy Groverd28b11692012-04-03 15:51:22 -07001772 cmd->data_direction = DMA_NONE;
1773
1774 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1775 if (!cmd->tmr_req) {
1776 pr_err("Unable to allocate memory for"
1777 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001778 return iscsit_add_reject_cmd(cmd,
1779 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1780 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001781 }
1782
1783 /*
1784 * TASK_REASSIGN for ERL=2 / connection stays inside of
1785 * LIO-Target $FABRIC_MOD
1786 */
1787 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1788
1789 u8 tcm_function;
1790 int ret;
1791
1792 transport_init_se_cmd(&cmd->se_cmd,
1793 &lio_target_fabric_configfs->tf_ops,
1794 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001795 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001796
1797 switch (function) {
1798 case ISCSI_TM_FUNC_ABORT_TASK:
1799 tcm_function = TMR_ABORT_TASK;
1800 break;
1801 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1802 tcm_function = TMR_ABORT_TASK_SET;
1803 break;
1804 case ISCSI_TM_FUNC_CLEAR_ACA:
1805 tcm_function = TMR_CLEAR_ACA;
1806 break;
1807 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1808 tcm_function = TMR_CLEAR_TASK_SET;
1809 break;
1810 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1811 tcm_function = TMR_LUN_RESET;
1812 break;
1813 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1814 tcm_function = TMR_TARGET_WARM_RESET;
1815 break;
1816 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1817 tcm_function = TMR_TARGET_COLD_RESET;
1818 break;
1819 default:
1820 pr_err("Unknown iSCSI TMR Function:"
1821 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001822 return iscsit_add_reject_cmd(cmd,
1823 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001824 }
1825
1826 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1827 tcm_function, GFP_KERNEL);
1828 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001829 return iscsit_add_reject_cmd(cmd,
1830 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001831
1832 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1833 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001834
1835 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1836 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1837 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1838 cmd->init_task_tag = hdr->itt;
1839 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001840 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1841 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001842 se_tmr = cmd->se_cmd.se_tmr_req;
1843 tmr_req = cmd->tmr_req;
1844 /*
1845 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1846 */
1847 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001848 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1849 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001850 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001851 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1852 goto attach;
1853 }
1854 }
1855
1856 switch (function) {
1857 case ISCSI_TM_FUNC_ABORT_TASK:
1858 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001859 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001860 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001861 break;
1862 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1863 case ISCSI_TM_FUNC_CLEAR_ACA:
1864 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1865 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1866 break;
1867 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1868 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001869 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1870 goto attach;
1871 }
1872 break;
1873 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1874 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001875 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1876 goto attach;
1877 }
1878 break;
1879 case ISCSI_TM_FUNC_TASK_REASSIGN:
1880 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1881 /*
1882 * Perform sanity checks on the ExpDataSN only if the
1883 * TASK_REASSIGN was successful.
1884 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001885 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001886 break;
1887
1888 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001889 return iscsit_add_reject_cmd(cmd,
1890 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001891 break;
1892 default:
1893 pr_err("Unknown TMR function: 0x%02x, protocol"
1894 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001895 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1896 goto attach;
1897 }
1898
1899 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1900 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1901 se_tmr->call_transport = 1;
1902attach:
1903 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001904 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001905 spin_unlock_bh(&conn->cmd_lock);
1906
1907 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001908 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001909 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1910 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001911 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001912 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001913 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001914 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001915 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001916 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001917
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001918 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001919 return 0;
1920 /*
1921 * Found the referenced task, send to transport for processing.
1922 */
1923 if (se_tmr->call_transport)
1924 return transport_generic_handle_tmr(&cmd->se_cmd);
1925
1926 /*
1927 * Could not find the referenced LUN, task, or Task Management
1928 * command not authorized or supported. Change state and
1929 * let the tx_thread send the response.
1930 *
1931 * For connection recovery, this is also the default action for
1932 * TMR TASK_REASSIGN.
1933 */
1934 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1935 return 0;
1936}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001937EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001938
1939/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001940int
1941iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1942 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001943{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001944 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001945
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001946 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001947 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001948 "greater than MaxXmitDataSegmentLength %u.\n",
1949 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001950 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1951 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001952 }
1953
1954 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1955 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1956 hdr->exp_statsn, payload_length);
1957
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001958 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1959 cmd->i_state = ISTATE_SEND_TEXTRSP;
1960 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1961 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1962 cmd->targ_xfer_tag = 0xFFFFFFFF;
1963 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1964 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1965 cmd->data_direction = DMA_NONE;
1966
1967 return 0;
1968}
1969EXPORT_SYMBOL(iscsit_setup_text_cmd);
1970
1971int
1972iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1973 struct iscsi_text *hdr)
1974{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001975 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001976 int cmdsn_ret;
1977
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001978 if (!text_in) {
1979 pr_err("Unable to locate text_in buffer for sendtargets"
1980 " discovery\n");
1981 goto reject;
1982 }
1983 if (strncmp("SendTargets", text_in, 11) != 0) {
1984 pr_err("Received Text Data that is not"
1985 " SendTargets, cannot continue.\n");
1986 goto reject;
1987 }
1988 text_ptr = strchr(text_in, '=');
1989 if (!text_ptr) {
1990 pr_err("No \"=\" separator found in Text Data,"
1991 " cannot continue.\n");
1992 goto reject;
1993 }
1994 if (!strncmp("=All", text_ptr, 4)) {
1995 cmd->cmd_flags |= IFC_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07001996 } else if (!strncmp("=iqn.", text_ptr, 5) ||
1997 !strncmp("=eui.", text_ptr, 5)) {
1998 cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001999 } else {
2000 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2001 goto reject;
2002 }
2003
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002004 spin_lock_bh(&conn->cmd_lock);
2005 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2006 spin_unlock_bh(&conn->cmd_lock);
2007
2008 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2009
2010 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002011 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2012 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002013 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002014 return -1;
2015
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002016 return 0;
2017 }
2018
2019 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002020
2021reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002022 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2023 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002024}
2025EXPORT_SYMBOL(iscsit_process_text_cmd);
2026
2027static int
2028iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2029 unsigned char *buf)
2030{
2031 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2032 char *text_in = NULL;
2033 u32 payload_length = ntoh24(hdr->dlength);
2034 int rx_size, rc;
2035
2036 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2037 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002038 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002039
2040 rx_size = payload_length;
2041 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002042 u32 checksum = 0, data_crc = 0;
2043 u32 padding = 0, pad_bytes = 0;
2044 int niov = 0, rx_got;
2045 struct kvec iov[3];
2046
2047 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002048 if (!text_in) {
2049 pr_err("Unable to allocate memory for"
2050 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002051 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002052 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002053 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002054
2055 memset(iov, 0, 3 * sizeof(struct kvec));
2056 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002057 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002058
2059 padding = ((-payload_length) & 3);
2060 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002061 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002062 iov[niov++].iov_len = padding;
2063 rx_size += padding;
2064 pr_debug("Receiving %u additional bytes"
2065 " for padding.\n", padding);
2066 }
2067 if (conn->conn_ops->DataDigest) {
2068 iov[niov].iov_base = &checksum;
2069 iov[niov++].iov_len = ISCSI_CRC_LEN;
2070 rx_size += ISCSI_CRC_LEN;
2071 }
2072
2073 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002074 if (rx_got != rx_size)
2075 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002076
2077 if (conn->conn_ops->DataDigest) {
2078 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002079 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002080 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002081 (u8 *)&data_crc);
2082
2083 if (checksum != data_crc) {
2084 pr_err("Text data CRC32C DataDigest"
2085 " 0x%08x does not match computed"
2086 " 0x%08x\n", checksum, data_crc);
2087 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2088 pr_err("Unable to recover from"
2089 " Text Data digest failure while in"
2090 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002091 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002092 } else {
2093 /*
2094 * Silently drop this PDU and let the
2095 * initiator plug the CmdSN gap.
2096 */
2097 pr_debug("Dropping Text"
2098 " Command CmdSN: 0x%08x due to"
2099 " DataCRC error.\n", hdr->cmdsn);
2100 kfree(text_in);
2101 return 0;
2102 }
2103 } else {
2104 pr_debug("Got CRC32C DataDigest"
2105 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002106 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002107 }
2108 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002109 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002110 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002111 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002112 }
2113
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002114 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002115
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002116reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002117 kfree(cmd->text_in_ptr);
2118 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002119 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002120}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002121EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002122
2123int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2124{
2125 struct iscsi_conn *conn_p;
2126 struct iscsi_session *sess = conn->sess;
2127
2128 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2129 " for SID: %u.\n", conn->cid, conn->sess->sid);
2130
2131 atomic_set(&sess->session_logout, 1);
2132 atomic_set(&conn->conn_logout_remove, 1);
2133 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2134
2135 iscsit_inc_conn_usage_count(conn);
2136 iscsit_inc_session_usage_count(sess);
2137
2138 spin_lock_bh(&sess->conn_lock);
2139 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2140 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2141 continue;
2142
2143 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2144 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2145 }
2146 spin_unlock_bh(&sess->conn_lock);
2147
2148 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2149
2150 return 0;
2151}
2152
2153int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2154{
2155 struct iscsi_conn *l_conn;
2156 struct iscsi_session *sess = conn->sess;
2157
2158 pr_debug("Received logout request CLOSECONNECTION for CID:"
2159 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2160
2161 /*
2162 * A Logout Request with a CLOSECONNECTION reason code for a CID
2163 * can arrive on a connection with a differing CID.
2164 */
2165 if (conn->cid == cmd->logout_cid) {
2166 spin_lock_bh(&conn->state_lock);
2167 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2168 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2169
2170 atomic_set(&conn->conn_logout_remove, 1);
2171 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2172 iscsit_inc_conn_usage_count(conn);
2173
2174 spin_unlock_bh(&conn->state_lock);
2175 } else {
2176 /*
2177 * Handle all different cid CLOSECONNECTION requests in
2178 * iscsit_logout_post_handler_diffcid() as to give enough
2179 * time for any non immediate command's CmdSN to be
2180 * acknowledged on the connection in question.
2181 *
2182 * Here we simply make sure the CID is still around.
2183 */
2184 l_conn = iscsit_get_conn_from_cid(sess,
2185 cmd->logout_cid);
2186 if (!l_conn) {
2187 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2188 iscsit_add_cmd_to_response_queue(cmd, conn,
2189 cmd->i_state);
2190 return 0;
2191 }
2192
2193 iscsit_dec_conn_usage_count(l_conn);
2194 }
2195
2196 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2197
2198 return 0;
2199}
2200
2201int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2202{
2203 struct iscsi_session *sess = conn->sess;
2204
2205 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2206 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2207
2208 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2209 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2210 " while ERL!=2.\n");
2211 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2212 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2213 return 0;
2214 }
2215
2216 if (conn->cid == cmd->logout_cid) {
2217 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2218 " with CID: %hu on CID: %hu, implementation error.\n",
2219 cmd->logout_cid, conn->cid);
2220 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2221 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2222 return 0;
2223 }
2224
2225 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2226
2227 return 0;
2228}
2229
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002230int
2231iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2232 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002233{
2234 int cmdsn_ret, logout_remove = 0;
2235 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002236 struct iscsi_logout *hdr;
2237 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2238
2239 hdr = (struct iscsi_logout *) buf;
2240 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002241
2242 if (tiqn) {
2243 spin_lock(&tiqn->logout_stats.lock);
2244 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2245 tiqn->logout_stats.normal_logouts++;
2246 else
2247 tiqn->logout_stats.abnormal_logouts++;
2248 spin_unlock(&tiqn->logout_stats.lock);
2249 }
2250
2251 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2252 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2253 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2254 hdr->cid, conn->cid);
2255
2256 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2257 pr_err("Received logout request on connection that"
2258 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002259 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002260 return 0;
2261 }
2262
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002263 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2264 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2265 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2266 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2267 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002268 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2269 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2270 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002271 cmd->logout_reason = reason_code;
2272 cmd->data_direction = DMA_NONE;
2273
2274 /*
2275 * We need to sleep in these cases (by returning 1) until the Logout
2276 * Response gets sent in the tx thread.
2277 */
2278 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2279 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002280 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002281 logout_remove = 1;
2282
2283 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002284 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002285 spin_unlock_bh(&conn->cmd_lock);
2286
2287 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002288 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002289
2290 /*
2291 * Immediate commands are executed, well, immediately.
2292 * Non-Immediate Logout Commands are executed in CmdSN order.
2293 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002294 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002295 int ret = iscsit_execute_cmd(cmd, 0);
2296
2297 if (ret < 0)
2298 return ret;
2299 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002300 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002301 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002302 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002303 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2304 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002305 }
2306
2307 return logout_remove;
2308}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002309EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002310
2311static int iscsit_handle_snack(
2312 struct iscsi_conn *conn,
2313 unsigned char *buf)
2314{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002315 struct iscsi_snack *hdr;
2316
2317 hdr = (struct iscsi_snack *) buf;
2318 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002319
2320 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2321 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2322 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2323 hdr->begrun, hdr->runlength, conn->cid);
2324
2325 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2326 pr_err("Initiator sent SNACK request while in"
2327 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002328 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2329 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002330 }
2331 /*
2332 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2333 * call from inside iscsi_send_recovery_datain_or_r2t().
2334 */
2335 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2336 case 0:
2337 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002338 hdr->itt,
2339 be32_to_cpu(hdr->ttt),
2340 be32_to_cpu(hdr->begrun),
2341 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002342 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002343 return iscsit_handle_status_snack(conn, hdr->itt,
2344 be32_to_cpu(hdr->ttt),
2345 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002346 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002347 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2348 be32_to_cpu(hdr->begrun),
2349 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002350 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2351 /* FIXME: Support R-Data SNACK */
2352 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002353 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2354 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002355 default:
2356 pr_err("Unknown SNACK type 0x%02x, protocol"
2357 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002358 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2359 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002360 }
2361
2362 return 0;
2363}
2364
2365static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2366{
2367 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2368 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2369 wait_for_completion_interruptible_timeout(
2370 &conn->rx_half_close_comp,
2371 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2372 }
2373}
2374
2375static int iscsit_handle_immediate_data(
2376 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002377 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002378 u32 length)
2379{
2380 int iov_ret, rx_got = 0, rx_size = 0;
2381 u32 checksum, iov_count = 0, padding = 0;
2382 struct iscsi_conn *conn = cmd->conn;
2383 struct kvec *iov;
2384
2385 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2386 if (iov_ret < 0)
2387 return IMMEDIATE_DATA_CANNOT_RECOVER;
2388
2389 rx_size = length;
2390 iov_count = iov_ret;
2391 iov = &cmd->iov_data[0];
2392
2393 padding = ((-length) & 3);
2394 if (padding != 0) {
2395 iov[iov_count].iov_base = cmd->pad_bytes;
2396 iov[iov_count++].iov_len = padding;
2397 rx_size += padding;
2398 }
2399
2400 if (conn->conn_ops->DataDigest) {
2401 iov[iov_count].iov_base = &checksum;
2402 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2403 rx_size += ISCSI_CRC_LEN;
2404 }
2405
2406 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2407
2408 iscsit_unmap_iovec(cmd);
2409
2410 if (rx_got != rx_size) {
2411 iscsit_rx_thread_wait_for_tcp(conn);
2412 return IMMEDIATE_DATA_CANNOT_RECOVER;
2413 }
2414
2415 if (conn->conn_ops->DataDigest) {
2416 u32 data_crc;
2417
2418 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2419 cmd->write_data_done, length, padding,
2420 cmd->pad_bytes);
2421
2422 if (checksum != data_crc) {
2423 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2424 " does not match computed 0x%08x\n", checksum,
2425 data_crc);
2426
2427 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2428 pr_err("Unable to recover from"
2429 " Immediate Data digest failure while"
2430 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002431 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002432 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002433 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002434 return IMMEDIATE_DATA_CANNOT_RECOVER;
2435 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002436 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002437 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002438 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002439 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2440 }
2441 } else {
2442 pr_debug("Got CRC32C DataDigest 0x%08x for"
2443 " %u bytes of Immediate Data\n", checksum,
2444 length);
2445 }
2446 }
2447
2448 cmd->write_data_done += length;
2449
Andy Groverebf1d952012-04-03 15:51:24 -07002450 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002451 spin_lock_bh(&cmd->istate_lock);
2452 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2453 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2454 spin_unlock_bh(&cmd->istate_lock);
2455 }
2456
2457 return IMMEDIATE_DATA_NORMAL_OPERATION;
2458}
2459
2460/*
2461 * Called with sess->conn_lock held.
2462 */
2463/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2464 with active network interface */
2465static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2466{
2467 struct iscsi_cmd *cmd;
2468 struct iscsi_conn *conn_p;
2469
2470 /*
2471 * Only send a Asynchronous Message on connections whos network
2472 * interface is still functional.
2473 */
2474 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2475 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2476 iscsit_inc_conn_usage_count(conn_p);
2477 break;
2478 }
2479 }
2480
2481 if (!conn_p)
2482 return;
2483
Wei Yongjun3c989d72012-11-23 12:07:39 +08002484 cmd = iscsit_allocate_cmd(conn_p, GFP_ATOMIC);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002485 if (!cmd) {
2486 iscsit_dec_conn_usage_count(conn_p);
2487 return;
2488 }
2489
2490 cmd->logout_cid = conn->cid;
2491 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2492 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2493
2494 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002495 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002496 spin_unlock_bh(&conn_p->cmd_lock);
2497
2498 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2499 iscsit_dec_conn_usage_count(conn_p);
2500}
2501
2502static int iscsit_send_conn_drop_async_message(
2503 struct iscsi_cmd *cmd,
2504 struct iscsi_conn *conn)
2505{
2506 struct iscsi_async *hdr;
2507
2508 cmd->tx_size = ISCSI_HDR_LEN;
2509 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2510
2511 hdr = (struct iscsi_async *) cmd->pdu;
2512 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2513 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002514 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002515 cmd->targ_xfer_tag = 0xFFFFFFFF;
2516 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2517 cmd->stat_sn = conn->stat_sn++;
2518 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2519 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2520 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2521 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2522 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2523 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2524 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2525
2526 if (conn->conn_ops->HeaderDigest) {
2527 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2528
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002529 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2530 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002531
2532 cmd->tx_size += ISCSI_CRC_LEN;
2533 pr_debug("Attaching CRC32C HeaderDigest to"
2534 " Async Message 0x%08x\n", *header_digest);
2535 }
2536
2537 cmd->iov_misc[0].iov_base = cmd->pdu;
2538 cmd->iov_misc[0].iov_len = cmd->tx_size;
2539 cmd->iov_misc_count = 1;
2540
2541 pr_debug("Sending Connection Dropped Async Message StatSN:"
2542 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2543 cmd->logout_cid, conn->cid);
2544 return 0;
2545}
2546
Andy Grover6f3c0e62012-04-03 15:51:09 -07002547static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2548{
2549 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2550 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2551 wait_for_completion_interruptible_timeout(
2552 &conn->tx_half_close_comp,
2553 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2554 }
2555}
2556
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002557static void
2558iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2559 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2560 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002561{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002562 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2563 hdr->flags = datain->flags;
2564 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2565 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2566 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2567 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2568 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2569 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2570 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2571 }
2572 }
2573 hton24(hdr->dlength, datain->length);
2574 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2575 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2576 (struct scsi_lun *)&hdr->lun);
2577 else
2578 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2579
2580 hdr->itt = cmd->init_task_tag;
2581
2582 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2583 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2584 else
2585 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2586 if (set_statsn)
2587 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2588 else
2589 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2590
2591 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2592 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2593 hdr->datasn = cpu_to_be32(datain->data_sn);
2594 hdr->offset = cpu_to_be32(datain->offset);
2595
2596 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2597 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2598 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2599 ntohl(hdr->offset), datain->length, conn->cid);
2600}
2601
2602static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2603{
2604 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002605 struct iscsi_datain datain;
2606 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002607 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002608 u32 iov_count = 0, tx_size = 0;
2609 int eodr = 0, ret, iov_ret;
2610 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002611
2612 memset(&datain, 0, sizeof(struct iscsi_datain));
2613 dr = iscsit_get_datain_values(cmd, &datain);
2614 if (!dr) {
2615 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2616 cmd->init_task_tag);
2617 return -1;
2618 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002619 /*
2620 * Be paranoid and double check the logic for now.
2621 */
Andy Groverebf1d952012-04-03 15:51:24 -07002622 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002623 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2624 " datain.length: %u exceeds cmd->data_length: %u\n",
2625 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002626 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002627 return -1;
2628 }
2629
2630 spin_lock_bh(&conn->sess->session_stats_lock);
2631 conn->sess->tx_data_octets += datain.length;
2632 if (conn->sess->se_sess->se_node_acl) {
2633 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2634 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2635 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2636 }
2637 spin_unlock_bh(&conn->sess->session_stats_lock);
2638 /*
2639 * Special case for successfully execution w/ both DATAIN
2640 * and Sense Data.
2641 */
2642 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2643 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2644 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2645 else {
2646 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2647 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2648 iscsit_increment_maxcmdsn(cmd, conn->sess);
2649 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002650 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002651 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002652 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2653 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002654 }
2655
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002656 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002657
2658 iov = &cmd->iov_data[0];
2659 iov[iov_count].iov_base = cmd->pdu;
2660 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2661 tx_size += ISCSI_HDR_LEN;
2662
2663 if (conn->conn_ops->HeaderDigest) {
2664 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2665
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002666 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2667 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002668
2669 iov[0].iov_len += ISCSI_CRC_LEN;
2670 tx_size += ISCSI_CRC_LEN;
2671
2672 pr_debug("Attaching CRC32 HeaderDigest"
2673 " for DataIN PDU 0x%08x\n", *header_digest);
2674 }
2675
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002676 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2677 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002678 if (iov_ret < 0)
2679 return -1;
2680
2681 iov_count += iov_ret;
2682 tx_size += datain.length;
2683
2684 cmd->padding = ((-datain.length) & 3);
2685 if (cmd->padding) {
2686 iov[iov_count].iov_base = cmd->pad_bytes;
2687 iov[iov_count++].iov_len = cmd->padding;
2688 tx_size += cmd->padding;
2689
2690 pr_debug("Attaching %u padding bytes\n",
2691 cmd->padding);
2692 }
2693 if (conn->conn_ops->DataDigest) {
2694 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2695 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2696
2697 iov[iov_count].iov_base = &cmd->data_crc;
2698 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2699 tx_size += ISCSI_CRC_LEN;
2700
2701 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2702 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2703 }
2704
2705 cmd->iov_data_count = iov_count;
2706 cmd->tx_size = tx_size;
2707
Andy Grover6f3c0e62012-04-03 15:51:09 -07002708 /* sendpage is preferred but can't insert markers */
2709 if (!conn->conn_ops->IFMarker)
2710 ret = iscsit_fe_sendpage_sg(cmd, conn);
2711 else
2712 ret = iscsit_send_tx_data(cmd, conn, 0);
2713
2714 iscsit_unmap_iovec(cmd);
2715
2716 if (ret < 0) {
2717 iscsit_tx_thread_wait_for_tcp(conn);
2718 return ret;
2719 }
2720
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002721 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002722 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002723 2 : 1;
2724 iscsit_free_datain_req(cmd, dr);
2725 }
2726
Andy Grover6f3c0e62012-04-03 15:51:09 -07002727 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002728}
2729
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002730int
2731iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2732 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002733{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002734 struct iscsi_conn *logout_conn = NULL;
2735 struct iscsi_conn_recovery *cr = NULL;
2736 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002737 /*
2738 * The actual shutting down of Sessions and/or Connections
2739 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2740 * is done in scsi_logout_post_handler().
2741 */
2742 switch (cmd->logout_reason) {
2743 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2744 pr_debug("iSCSI session logout successful, setting"
2745 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2746 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2747 break;
2748 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2749 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2750 break;
2751 /*
2752 * For CLOSECONNECTION logout requests carrying
2753 * a matching logout CID -> local CID, the reference
2754 * for the local CID will have been incremented in
2755 * iscsi_logout_closeconnection().
2756 *
2757 * For CLOSECONNECTION logout requests carrying
2758 * a different CID than the connection it arrived
2759 * on, the connection responding to cmd->logout_cid
2760 * is stopped in iscsit_logout_post_handler_diffcid().
2761 */
2762
2763 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2764 " successful.\n", cmd->logout_cid, conn->cid);
2765 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2766 break;
2767 case ISCSI_LOGOUT_REASON_RECOVERY:
2768 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2769 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2770 break;
2771 /*
2772 * If the connection is still active from our point of view
2773 * force connection recovery to occur.
2774 */
2775 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2776 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002777 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002778 iscsit_connection_reinstatement_rcfr(logout_conn);
2779 iscsit_dec_conn_usage_count(logout_conn);
2780 }
2781
2782 cr = iscsit_get_inactive_connection_recovery_entry(
2783 conn->sess, cmd->logout_cid);
2784 if (!cr) {
2785 pr_err("Unable to locate CID: %hu for"
2786 " REMOVECONNFORRECOVERY Logout Request.\n",
2787 cmd->logout_cid);
2788 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2789 break;
2790 }
2791
2792 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2793
2794 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2795 " for recovery for CID: %hu on CID: %hu successful.\n",
2796 cmd->logout_cid, conn->cid);
2797 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2798 break;
2799 default:
2800 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2801 cmd->logout_reason);
2802 return -1;
2803 }
2804
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002805 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2806 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2807 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002808 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002809 cmd->stat_sn = conn->stat_sn++;
2810 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2811
2812 iscsit_increment_maxcmdsn(cmd, conn->sess);
2813 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2814 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2815
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002816 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2817 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2818 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2819 cmd->logout_cid, conn->cid);
2820
2821 return 0;
2822}
2823EXPORT_SYMBOL(iscsit_build_logout_rsp);
2824
2825static int
2826iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2827{
2828 struct kvec *iov;
2829 int niov = 0, tx_size, rc;
2830
2831 rc = iscsit_build_logout_rsp(cmd, conn,
2832 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2833 if (rc < 0)
2834 return rc;
2835
2836 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002837 iov = &cmd->iov_misc[0];
2838 iov[niov].iov_base = cmd->pdu;
2839 iov[niov++].iov_len = ISCSI_HDR_LEN;
2840
2841 if (conn->conn_ops->HeaderDigest) {
2842 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2843
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002844 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2845 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002846
2847 iov[0].iov_len += ISCSI_CRC_LEN;
2848 tx_size += ISCSI_CRC_LEN;
2849 pr_debug("Attaching CRC32C HeaderDigest to"
2850 " Logout Response 0x%08x\n", *header_digest);
2851 }
2852 cmd->iov_misc_count = niov;
2853 cmd->tx_size = tx_size;
2854
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002855 return 0;
2856}
2857
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002858void
2859iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2860 struct iscsi_nopin *hdr, bool nopout_response)
2861{
2862 hdr->opcode = ISCSI_OP_NOOP_IN;
2863 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2864 hton24(hdr->dlength, cmd->buf_ptr_size);
2865 if (nopout_response)
2866 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2867 hdr->itt = cmd->init_task_tag;
2868 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2869 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2870 conn->stat_sn;
2871 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2872
2873 if (nopout_response)
2874 iscsit_increment_maxcmdsn(cmd, conn->sess);
2875
2876 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2877 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2878
2879 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2880 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2881 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2882 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2883}
2884EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2885
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002886/*
2887 * Unsolicited NOPIN, either requesting a response or not.
2888 */
2889static int iscsit_send_unsolicited_nopin(
2890 struct iscsi_cmd *cmd,
2891 struct iscsi_conn *conn,
2892 int want_response)
2893{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002894 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2895 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002896
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002897 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002898
2899 if (conn->conn_ops->HeaderDigest) {
2900 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2901
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002902 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2903 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002904
2905 tx_size += ISCSI_CRC_LEN;
2906 pr_debug("Attaching CRC32C HeaderDigest to"
2907 " NopIN 0x%08x\n", *header_digest);
2908 }
2909
2910 cmd->iov_misc[0].iov_base = cmd->pdu;
2911 cmd->iov_misc[0].iov_len = tx_size;
2912 cmd->iov_misc_count = 1;
2913 cmd->tx_size = tx_size;
2914
2915 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2916 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2917
Andy Grover6f3c0e62012-04-03 15:51:09 -07002918 ret = iscsit_send_tx_data(cmd, conn, 1);
2919 if (ret < 0) {
2920 iscsit_tx_thread_wait_for_tcp(conn);
2921 return ret;
2922 }
2923
2924 spin_lock_bh(&cmd->istate_lock);
2925 cmd->i_state = want_response ?
2926 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2927 spin_unlock_bh(&cmd->istate_lock);
2928
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002929 return 0;
2930}
2931
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002932static int
2933iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002934{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002935 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002936 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002937 u32 padding = 0;
2938 int niov = 0, tx_size;
2939
2940 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002941
2942 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002943 iov = &cmd->iov_misc[0];
2944 iov[niov].iov_base = cmd->pdu;
2945 iov[niov++].iov_len = ISCSI_HDR_LEN;
2946
2947 if (conn->conn_ops->HeaderDigest) {
2948 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2949
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002950 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2951 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002952
2953 iov[0].iov_len += ISCSI_CRC_LEN;
2954 tx_size += ISCSI_CRC_LEN;
2955 pr_debug("Attaching CRC32C HeaderDigest"
2956 " to NopIn 0x%08x\n", *header_digest);
2957 }
2958
2959 /*
2960 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2961 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2962 */
2963 if (cmd->buf_ptr_size) {
2964 iov[niov].iov_base = cmd->buf_ptr;
2965 iov[niov++].iov_len = cmd->buf_ptr_size;
2966 tx_size += cmd->buf_ptr_size;
2967
2968 pr_debug("Echoing back %u bytes of ping"
2969 " data.\n", cmd->buf_ptr_size);
2970
2971 padding = ((-cmd->buf_ptr_size) & 3);
2972 if (padding != 0) {
2973 iov[niov].iov_base = &cmd->pad_bytes;
2974 iov[niov++].iov_len = padding;
2975 tx_size += padding;
2976 pr_debug("Attaching %u additional"
2977 " padding bytes.\n", padding);
2978 }
2979 if (conn->conn_ops->DataDigest) {
2980 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2981 cmd->buf_ptr, cmd->buf_ptr_size,
2982 padding, (u8 *)&cmd->pad_bytes,
2983 (u8 *)&cmd->data_crc);
2984
2985 iov[niov].iov_base = &cmd->data_crc;
2986 iov[niov++].iov_len = ISCSI_CRC_LEN;
2987 tx_size += ISCSI_CRC_LEN;
2988 pr_debug("Attached DataDigest for %u"
2989 " bytes of ping data, CRC 0x%08x\n",
2990 cmd->buf_ptr_size, cmd->data_crc);
2991 }
2992 }
2993
2994 cmd->iov_misc_count = niov;
2995 cmd->tx_size = tx_size;
2996
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002997 return 0;
2998}
2999
Andy Grover6f3c0e62012-04-03 15:51:09 -07003000static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003001 struct iscsi_cmd *cmd,
3002 struct iscsi_conn *conn)
3003{
3004 int tx_size = 0;
3005 struct iscsi_r2t *r2t;
3006 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003007 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003008
3009 r2t = iscsit_get_r2t_from_list(cmd);
3010 if (!r2t)
3011 return -1;
3012
3013 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3014 memset(hdr, 0, ISCSI_HDR_LEN);
3015 hdr->opcode = ISCSI_OP_R2T;
3016 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3017 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3018 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003019 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003020 spin_lock_bh(&conn->sess->ttt_lock);
3021 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3022 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3023 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3024 spin_unlock_bh(&conn->sess->ttt_lock);
3025 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3026 hdr->statsn = cpu_to_be32(conn->stat_sn);
3027 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3028 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3029 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3030 hdr->data_offset = cpu_to_be32(r2t->offset);
3031 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3032
3033 cmd->iov_misc[0].iov_base = cmd->pdu;
3034 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3035 tx_size += ISCSI_HDR_LEN;
3036
3037 if (conn->conn_ops->HeaderDigest) {
3038 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3039
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003040 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3041 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003042
3043 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3044 tx_size += ISCSI_CRC_LEN;
3045 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3046 " PDU 0x%08x\n", *header_digest);
3047 }
3048
3049 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3050 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3051 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3052 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3053 r2t->offset, r2t->xfer_len, conn->cid);
3054
3055 cmd->iov_misc_count = 1;
3056 cmd->tx_size = tx_size;
3057
3058 spin_lock_bh(&cmd->r2t_lock);
3059 r2t->sent_r2t = 1;
3060 spin_unlock_bh(&cmd->r2t_lock);
3061
Andy Grover6f3c0e62012-04-03 15:51:09 -07003062 ret = iscsit_send_tx_data(cmd, conn, 1);
3063 if (ret < 0) {
3064 iscsit_tx_thread_wait_for_tcp(conn);
3065 return ret;
3066 }
3067
3068 spin_lock_bh(&cmd->dataout_timeout_lock);
3069 iscsit_start_dataout_timer(cmd, conn);
3070 spin_unlock_bh(&cmd->dataout_timeout_lock);
3071
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003072 return 0;
3073}
3074
3075/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003076 * @recovery: If called from iscsi_task_reassign_complete_write() for
3077 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003078 */
3079int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003080 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003081 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003082 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003083{
3084 int first_r2t = 1;
3085 u32 offset = 0, xfer_len = 0;
3086
3087 spin_lock_bh(&cmd->r2t_lock);
3088 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3089 spin_unlock_bh(&cmd->r2t_lock);
3090 return 0;
3091 }
3092
Andy Grover8b1e1242012-04-03 15:51:12 -07003093 if (conn->sess->sess_ops->DataSequenceInOrder &&
3094 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003095 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003096
3097 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3098 if (conn->sess->sess_ops->DataSequenceInOrder) {
3099 offset = cmd->r2t_offset;
3100
Andy Grover8b1e1242012-04-03 15:51:12 -07003101 if (first_r2t && recovery) {
3102 int new_data_end = offset +
3103 conn->sess->sess_ops->MaxBurstLength -
3104 cmd->next_burst_len;
3105
Andy Groverebf1d952012-04-03 15:51:24 -07003106 if (new_data_end > cmd->se_cmd.data_length)
3107 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003108 else
3109 xfer_len =
3110 conn->sess->sess_ops->MaxBurstLength -
3111 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003112 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003113 int new_data_end = offset +
3114 conn->sess->sess_ops->MaxBurstLength;
3115
Andy Groverebf1d952012-04-03 15:51:24 -07003116 if (new_data_end > cmd->se_cmd.data_length)
3117 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003118 else
3119 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003120 }
3121 cmd->r2t_offset += xfer_len;
3122
Andy Groverebf1d952012-04-03 15:51:24 -07003123 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003124 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3125 } else {
3126 struct iscsi_seq *seq;
3127
3128 seq = iscsit_get_seq_holder_for_r2t(cmd);
3129 if (!seq) {
3130 spin_unlock_bh(&cmd->r2t_lock);
3131 return -1;
3132 }
3133
3134 offset = seq->offset;
3135 xfer_len = seq->xfer_len;
3136
3137 if (cmd->seq_send_order == cmd->seq_count)
3138 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3139 }
3140 cmd->outstanding_r2ts++;
3141 first_r2t = 0;
3142
3143 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3144 spin_unlock_bh(&cmd->r2t_lock);
3145 return -1;
3146 }
3147
3148 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3149 break;
3150 }
3151 spin_unlock_bh(&cmd->r2t_lock);
3152
3153 return 0;
3154}
3155
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003156void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3157 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003158{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003159 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003160 cmd->stat_sn = conn->stat_sn++;
3161
3162 spin_lock_bh(&conn->sess->session_stats_lock);
3163 conn->sess->rsp_pdus++;
3164 spin_unlock_bh(&conn->sess->session_stats_lock);
3165
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003166 memset(hdr, 0, ISCSI_HDR_LEN);
3167 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3168 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3169 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3170 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003171 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003172 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3173 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003174 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003175 }
3176 hdr->response = cmd->iscsi_response;
3177 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003178 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003179 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3180
3181 iscsit_increment_maxcmdsn(cmd, conn->sess);
3182 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3183 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3184
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003185 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3186 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3187 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3188 cmd->se_cmd.scsi_status, conn->cid);
3189}
3190EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3191
3192static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3193{
3194 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3195 struct kvec *iov;
3196 u32 padding = 0, tx_size = 0;
3197 int iov_count = 0;
3198 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3199
3200 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3201
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003202 iov = &cmd->iov_misc[0];
3203 iov[iov_count].iov_base = cmd->pdu;
3204 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3205 tx_size += ISCSI_HDR_LEN;
3206
3207 /*
3208 * Attach SENSE DATA payload to iSCSI Response PDU
3209 */
3210 if (cmd->se_cmd.sense_buffer &&
3211 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3212 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003213 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3214 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3215
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003216 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003217 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003218 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003219 iov[iov_count++].iov_len =
3220 (cmd->se_cmd.scsi_sense_length + padding);
3221 tx_size += cmd->se_cmd.scsi_sense_length;
3222
3223 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003224 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003225 cmd->se_cmd.scsi_sense_length, 0, padding);
3226 tx_size += padding;
3227 pr_debug("Adding %u bytes of padding to"
3228 " SENSE.\n", padding);
3229 }
3230
3231 if (conn->conn_ops->DataDigest) {
3232 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003233 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003234 (cmd->se_cmd.scsi_sense_length + padding),
3235 0, NULL, (u8 *)&cmd->data_crc);
3236
3237 iov[iov_count].iov_base = &cmd->data_crc;
3238 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3239 tx_size += ISCSI_CRC_LEN;
3240
3241 pr_debug("Attaching CRC32 DataDigest for"
3242 " SENSE, %u bytes CRC 0x%08x\n",
3243 (cmd->se_cmd.scsi_sense_length + padding),
3244 cmd->data_crc);
3245 }
3246
3247 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3248 " Response PDU\n",
3249 cmd->se_cmd.scsi_sense_length);
3250 }
3251
3252 if (conn->conn_ops->HeaderDigest) {
3253 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3254
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003255 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3256 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003257
3258 iov[0].iov_len += ISCSI_CRC_LEN;
3259 tx_size += ISCSI_CRC_LEN;
3260 pr_debug("Attaching CRC32 HeaderDigest for Response"
3261 " PDU 0x%08x\n", *header_digest);
3262 }
3263
3264 cmd->iov_misc_count = iov_count;
3265 cmd->tx_size = tx_size;
3266
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003267 return 0;
3268}
3269
3270static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3271{
3272 switch (se_tmr->response) {
3273 case TMR_FUNCTION_COMPLETE:
3274 return ISCSI_TMF_RSP_COMPLETE;
3275 case TMR_TASK_DOES_NOT_EXIST:
3276 return ISCSI_TMF_RSP_NO_TASK;
3277 case TMR_LUN_DOES_NOT_EXIST:
3278 return ISCSI_TMF_RSP_NO_LUN;
3279 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3280 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3281 case TMR_FUNCTION_AUTHORIZATION_FAILED:
3282 return ISCSI_TMF_RSP_AUTH_FAILED;
3283 case TMR_FUNCTION_REJECTED:
3284 default:
3285 return ISCSI_TMF_RSP_REJECTED;
3286 }
3287}
3288
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003289void
3290iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3291 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003292{
3293 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003294
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003295 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003296 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003297 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003298 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003299 cmd->stat_sn = conn->stat_sn++;
3300 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3301
3302 iscsit_increment_maxcmdsn(cmd, conn->sess);
3303 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3304 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3305
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003306 pr_debug("Built Task Management Response ITT: 0x%08x,"
3307 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3308 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3309}
3310EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3311
3312static int
3313iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3314{
3315 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3316 u32 tx_size = 0;
3317
3318 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3319
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003320 cmd->iov_misc[0].iov_base = cmd->pdu;
3321 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3322 tx_size += ISCSI_HDR_LEN;
3323
3324 if (conn->conn_ops->HeaderDigest) {
3325 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3326
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003327 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3328 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003329
3330 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3331 tx_size += ISCSI_CRC_LEN;
3332 pr_debug("Attaching CRC32 HeaderDigest for Task"
3333 " Mgmt Response PDU 0x%08x\n", *header_digest);
3334 }
3335
3336 cmd->iov_misc_count = 1;
3337 cmd->tx_size = tx_size;
3338
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003339 return 0;
3340}
3341
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003342static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3343{
3344 bool ret = false;
3345
3346 if (np->np_sockaddr.ss_family == AF_INET6) {
3347 const struct sockaddr_in6 sin6 = {
3348 .sin6_addr = IN6ADDR_ANY_INIT };
3349 struct sockaddr_in6 *sock_in6 =
3350 (struct sockaddr_in6 *)&np->np_sockaddr;
3351
3352 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3353 sin6.sin6_addr.s6_addr, 16))
3354 ret = true;
3355 } else {
3356 struct sockaddr_in * sock_in =
3357 (struct sockaddr_in *)&np->np_sockaddr;
3358
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003359 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003360 ret = true;
3361 }
3362
3363 return ret;
3364}
3365
Andy Grover8b1e1242012-04-03 15:51:12 -07003366#define SENDTARGETS_BUF_LIMIT 32768U
3367
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003368static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3369{
3370 char *payload = NULL;
3371 struct iscsi_conn *conn = cmd->conn;
3372 struct iscsi_portal_group *tpg;
3373 struct iscsi_tiqn *tiqn;
3374 struct iscsi_tpg_np *tpg_np;
3375 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003376 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003377 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003378
Andy Grover8b1e1242012-04-03 15:51:12 -07003379 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3380 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003381
3382 payload = kzalloc(buffer_len, GFP_KERNEL);
3383 if (!payload) {
3384 pr_err("Unable to allocate memory for sendtargets"
3385 " response.\n");
3386 return -ENOMEM;
3387 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003388 /*
3389 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3390 * explicit case..
3391 */
3392 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) {
3393 text_ptr = strchr(text_in, '=');
3394 if (!text_ptr) {
3395 pr_err("Unable to locate '=' string in text_in:"
3396 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003397 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003398 return -EINVAL;
3399 }
3400 /*
3401 * Skip over '=' character..
3402 */
3403 text_ptr += 1;
3404 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003405
3406 spin_lock(&tiqn_lock);
3407 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003408 if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) &&
3409 strcmp(tiqn->tiqn, text_ptr)) {
3410 continue;
3411 }
3412
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003413 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3414 len += 1;
3415
3416 if ((len + payload_len) > buffer_len) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003417 end_of_buf = 1;
3418 goto eob;
3419 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003420 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003421 payload_len += len;
3422
3423 spin_lock(&tiqn->tiqn_tpg_lock);
3424 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3425
3426 spin_lock(&tpg->tpg_state_lock);
3427 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3428 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3429 spin_unlock(&tpg->tpg_state_lock);
3430 continue;
3431 }
3432 spin_unlock(&tpg->tpg_state_lock);
3433
3434 spin_lock(&tpg->tpg_np_lock);
3435 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3436 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003437 struct iscsi_np *np = tpg_np->tpg_np;
3438 bool inaddr_any = iscsit_check_inaddr_any(np);
3439
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003440 len = sprintf(buf, "TargetAddress="
3441 "%s%s%s:%hu,%hu",
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003442 (np->np_sockaddr.ss_family == AF_INET6) ?
3443 "[" : "", (inaddr_any == false) ?
3444 np->np_ip : conn->local_ip,
3445 (np->np_sockaddr.ss_family == AF_INET6) ?
3446 "]" : "", (inaddr_any == false) ?
3447 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003448 tpg->tpgt);
3449 len += 1;
3450
3451 if ((len + payload_len) > buffer_len) {
3452 spin_unlock(&tpg->tpg_np_lock);
3453 spin_unlock(&tiqn->tiqn_tpg_lock);
3454 end_of_buf = 1;
3455 goto eob;
3456 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003457 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003458 payload_len += len;
3459 }
3460 spin_unlock(&tpg->tpg_np_lock);
3461 }
3462 spin_unlock(&tiqn->tiqn_tpg_lock);
3463eob:
3464 if (end_of_buf)
3465 break;
Nicholas Bellinger66658892013-06-19 22:45:42 -07003466
3467 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE)
3468 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003469 }
3470 spin_unlock(&tiqn_lock);
3471
3472 cmd->buf_ptr = payload;
3473
3474 return payload_len;
3475}
3476
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003477int
3478iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3479 struct iscsi_text_rsp *hdr)
3480{
3481 int text_length, padding;
3482
3483 text_length = iscsit_build_sendtargets_response(cmd);
3484 if (text_length < 0)
3485 return text_length;
3486
3487 hdr->opcode = ISCSI_OP_TEXT_RSP;
3488 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3489 padding = ((-text_length) & 3);
3490 hton24(hdr->dlength, text_length);
3491 hdr->itt = cmd->init_task_tag;
3492 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3493 cmd->stat_sn = conn->stat_sn++;
3494 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3495
3496 iscsit_increment_maxcmdsn(cmd, conn->sess);
3497 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3498 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3499
3500 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3501 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3502 text_length, conn->cid);
3503
3504 return text_length + padding;
3505}
3506EXPORT_SYMBOL(iscsit_build_text_rsp);
3507
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003508/*
3509 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3510 * MaxRecvDataSegmentLength.
3511 */
3512static int iscsit_send_text_rsp(
3513 struct iscsi_cmd *cmd,
3514 struct iscsi_conn *conn)
3515{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003516 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003517 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003518 u32 tx_size = 0;
3519 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003520
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003521 rc = iscsit_build_text_rsp(cmd, conn, hdr);
3522 if (rc < 0)
3523 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003524
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003525 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003526 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003527 iov[iov_count].iov_base = cmd->pdu;
3528 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3529 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003530 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003531
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003532 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003533
3534 if (conn->conn_ops->HeaderDigest) {
3535 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3536
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003537 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3538 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003539
3540 iov[0].iov_len += ISCSI_CRC_LEN;
3541 tx_size += ISCSI_CRC_LEN;
3542 pr_debug("Attaching CRC32 HeaderDigest for"
3543 " Text Response PDU 0x%08x\n", *header_digest);
3544 }
3545
3546 if (conn->conn_ops->DataDigest) {
3547 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003548 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003549 0, NULL, (u8 *)&cmd->data_crc);
3550
3551 iov[iov_count].iov_base = &cmd->data_crc;
3552 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3553 tx_size += ISCSI_CRC_LEN;
3554
3555 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003556 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003557 cmd->data_crc);
3558 }
3559
3560 cmd->iov_misc_count = iov_count;
3561 cmd->tx_size = tx_size;
3562
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003563 return 0;
3564}
3565
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003566void
3567iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3568 struct iscsi_reject *hdr)
3569{
3570 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003571 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003572 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3573 hton24(hdr->dlength, ISCSI_HDR_LEN);
3574 hdr->ffffffff = cpu_to_be32(0xffffffff);
3575 cmd->stat_sn = conn->stat_sn++;
3576 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3577 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3578 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3579
3580}
3581EXPORT_SYMBOL(iscsit_build_reject);
3582
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003583static int iscsit_send_reject(
3584 struct iscsi_cmd *cmd,
3585 struct iscsi_conn *conn)
3586{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003587 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003588 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003589 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003590
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003591 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003592
3593 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003594 iov[iov_count].iov_base = cmd->pdu;
3595 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3596 iov[iov_count].iov_base = cmd->buf_ptr;
3597 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3598
3599 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3600
3601 if (conn->conn_ops->HeaderDigest) {
3602 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3603
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003604 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3605 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003606
3607 iov[0].iov_len += ISCSI_CRC_LEN;
3608 tx_size += ISCSI_CRC_LEN;
3609 pr_debug("Attaching CRC32 HeaderDigest for"
3610 " REJECT PDU 0x%08x\n", *header_digest);
3611 }
3612
3613 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003614 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3615 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003616
3617 iov[iov_count].iov_base = &cmd->data_crc;
3618 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3619 tx_size += ISCSI_CRC_LEN;
3620 pr_debug("Attaching CRC32 DataDigest for REJECT"
3621 " PDU 0x%08x\n", cmd->data_crc);
3622 }
3623
3624 cmd->iov_misc_count = iov_count;
3625 cmd->tx_size = tx_size;
3626
3627 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3628 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3629
3630 return 0;
3631}
3632
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003633void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3634{
3635 struct iscsi_thread_set *ts = conn->thread_set;
3636 int ord, cpu;
3637 /*
3638 * thread_id is assigned from iscsit_global->ts_bitmap from
3639 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3640 *
3641 * Here we use thread_id to determine which CPU that this
3642 * iSCSI connection's iscsi_thread_set will be scheduled to
3643 * execute upon.
3644 */
3645 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003646 for_each_online_cpu(cpu) {
3647 if (ord-- == 0) {
3648 cpumask_set_cpu(cpu, conn->conn_cpumask);
3649 return;
3650 }
3651 }
3652 /*
3653 * This should never be reached..
3654 */
3655 dump_stack();
3656 cpumask_setall(conn->conn_cpumask);
3657}
3658
3659static inline void iscsit_thread_check_cpumask(
3660 struct iscsi_conn *conn,
3661 struct task_struct *p,
3662 int mode)
3663{
3664 char buf[128];
3665 /*
3666 * mode == 1 signals iscsi_target_tx_thread() usage.
3667 * mode == 0 signals iscsi_target_rx_thread() usage.
3668 */
3669 if (mode == 1) {
3670 if (!conn->conn_tx_reset_cpumask)
3671 return;
3672 conn->conn_tx_reset_cpumask = 0;
3673 } else {
3674 if (!conn->conn_rx_reset_cpumask)
3675 return;
3676 conn->conn_rx_reset_cpumask = 0;
3677 }
3678 /*
3679 * Update the CPU mask for this single kthread so that
3680 * both TX and RX kthreads are scheduled to run on the
3681 * same CPU.
3682 */
3683 memset(buf, 0, 128);
3684 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003685 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3686}
3687
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003688static int
3689iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003690{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003691 int ret;
3692
3693 switch (state) {
3694 case ISTATE_SEND_R2T:
3695 ret = iscsit_send_r2t(cmd, conn);
3696 if (ret < 0)
3697 goto err;
3698 break;
3699 case ISTATE_REMOVE:
3700 spin_lock_bh(&conn->cmd_lock);
3701 list_del(&cmd->i_conn_node);
3702 spin_unlock_bh(&conn->cmd_lock);
3703
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003704 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003705 break;
3706 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3707 iscsit_mod_nopin_response_timer(conn);
3708 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3709 if (ret < 0)
3710 goto err;
3711 break;
3712 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3713 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3714 if (ret < 0)
3715 goto err;
3716 break;
3717 default:
3718 pr_err("Unknown Opcode: 0x%02x ITT:"
3719 " 0x%08x, i_state: %d on CID: %hu\n",
3720 cmd->iscsi_opcode, cmd->init_task_tag, state,
3721 conn->cid);
3722 goto err;
3723 }
3724
3725 return 0;
3726
3727err:
3728 return -1;
3729}
3730
3731static int
3732iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3733{
3734 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003735 struct iscsi_queue_req *qr;
3736 struct iscsi_cmd *cmd;
3737 u8 state;
3738 int ret;
3739
3740 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3741 atomic_set(&conn->check_immediate_queue, 0);
3742 cmd = qr->cmd;
3743 state = qr->state;
3744 kmem_cache_free(lio_qr_cache, qr);
3745
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003746 ret = t->iscsit_immediate_queue(conn, cmd, state);
3747 if (ret < 0)
3748 return ret;
3749 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003750
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003751 return 0;
3752}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003753
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003754static int
3755iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3756{
3757 int ret;
3758
3759check_rsp_state:
3760 switch (state) {
3761 case ISTATE_SEND_DATAIN:
3762 ret = iscsit_send_datain(cmd, conn);
3763 if (ret < 0)
3764 goto err;
3765 else if (!ret)
3766 /* more drs */
3767 goto check_rsp_state;
3768 else if (ret == 1) {
3769 /* all done */
3770 spin_lock_bh(&cmd->istate_lock);
3771 cmd->i_state = ISTATE_SENT_STATUS;
3772 spin_unlock_bh(&cmd->istate_lock);
3773
3774 if (atomic_read(&conn->check_immediate_queue))
3775 return 1;
3776
3777 return 0;
3778 } else if (ret == 2) {
3779 /* Still must send status,
3780 SCF_TRANSPORT_TASK_SENSE was set */
3781 spin_lock_bh(&cmd->istate_lock);
3782 cmd->i_state = ISTATE_SEND_STATUS;
3783 spin_unlock_bh(&cmd->istate_lock);
3784 state = ISTATE_SEND_STATUS;
3785 goto check_rsp_state;
3786 }
3787
3788 break;
3789 case ISTATE_SEND_STATUS:
3790 case ISTATE_SEND_STATUS_RECOVERY:
3791 ret = iscsit_send_response(cmd, conn);
3792 break;
3793 case ISTATE_SEND_LOGOUTRSP:
3794 ret = iscsit_send_logout(cmd, conn);
3795 break;
3796 case ISTATE_SEND_ASYNCMSG:
3797 ret = iscsit_send_conn_drop_async_message(
3798 cmd, conn);
3799 break;
3800 case ISTATE_SEND_NOPIN:
3801 ret = iscsit_send_nopin(cmd, conn);
3802 break;
3803 case ISTATE_SEND_REJECT:
3804 ret = iscsit_send_reject(cmd, conn);
3805 break;
3806 case ISTATE_SEND_TASKMGTRSP:
3807 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3808 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003809 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003810 ret = iscsit_tmr_post_handler(cmd, conn);
3811 if (ret != 0)
3812 iscsit_fall_back_to_erl0(conn->sess);
3813 break;
3814 case ISTATE_SEND_TEXTRSP:
3815 ret = iscsit_send_text_rsp(cmd, conn);
3816 break;
3817 default:
3818 pr_err("Unknown Opcode: 0x%02x ITT:"
3819 " 0x%08x, i_state: %d on CID: %hu\n",
3820 cmd->iscsi_opcode, cmd->init_task_tag,
3821 state, conn->cid);
3822 goto err;
3823 }
3824 if (ret < 0)
3825 goto err;
3826
3827 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3828 iscsit_tx_thread_wait_for_tcp(conn);
3829 iscsit_unmap_iovec(cmd);
3830 goto err;
3831 }
3832 iscsit_unmap_iovec(cmd);
3833
3834 switch (state) {
3835 case ISTATE_SEND_LOGOUTRSP:
3836 if (!iscsit_logout_post_handler(cmd, conn))
3837 goto restart;
3838 /* fall through */
3839 case ISTATE_SEND_STATUS:
3840 case ISTATE_SEND_ASYNCMSG:
3841 case ISTATE_SEND_NOPIN:
3842 case ISTATE_SEND_STATUS_RECOVERY:
3843 case ISTATE_SEND_TEXTRSP:
3844 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003845 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003846 spin_lock_bh(&cmd->istate_lock);
3847 cmd->i_state = ISTATE_SENT_STATUS;
3848 spin_unlock_bh(&cmd->istate_lock);
3849 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003850 default:
3851 pr_err("Unknown Opcode: 0x%02x ITT:"
3852 " 0x%08x, i_state: %d on CID: %hu\n",
3853 cmd->iscsi_opcode, cmd->init_task_tag,
3854 cmd->i_state, conn->cid);
3855 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003856 }
3857
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003858 if (atomic_read(&conn->check_immediate_queue))
3859 return 1;
3860
Andy Grover6f3c0e62012-04-03 15:51:09 -07003861 return 0;
3862
3863err:
3864 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003865restart:
3866 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003867}
3868
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003869static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003870{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003871 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003872 struct iscsi_queue_req *qr;
3873 struct iscsi_cmd *cmd;
3874 u8 state;
3875 int ret;
3876
3877 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3878 cmd = qr->cmd;
3879 state = qr->state;
3880 kmem_cache_free(lio_qr_cache, qr);
3881
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003882 ret = t->iscsit_response_queue(conn, cmd, state);
3883 if (ret == 1 || ret < 0)
3884 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003885 }
3886
3887 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003888}
3889
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003890int iscsi_target_tx_thread(void *arg)
3891{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003892 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003893 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003894 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003895 /*
3896 * Allow ourselves to be interrupted by SIGINT so that a
3897 * connection recovery / failure event can be triggered externally.
3898 */
3899 allow_signal(SIGINT);
3900
3901restart:
3902 conn = iscsi_tx_thread_pre_handler(ts);
3903 if (!conn)
3904 goto out;
3905
Andy Grover6f3c0e62012-04-03 15:51:09 -07003906 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003907
3908 while (!kthread_should_stop()) {
3909 /*
3910 * Ensure that both TX and RX per connection kthreads
3911 * are scheduled to run on the same CPU.
3912 */
3913 iscsit_thread_check_cpumask(conn, current, 1);
3914
Roland Dreierd5627ac2012-10-31 09:16:46 -07003915 wait_event_interruptible(conn->queues_wq,
3916 !iscsit_conn_all_queues_empty(conn) ||
3917 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003918
3919 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3920 signal_pending(current))
3921 goto transport_err;
3922
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003923get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003924 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003925 if (ret < 0)
3926 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003927
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003928 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003929 if (ret == 1)
3930 goto get_immediate;
3931 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003932 goto restart;
3933 else if (ret < 0)
3934 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003935 }
3936
3937transport_err:
3938 iscsit_take_action_for_connection_exit(conn);
3939 goto restart;
3940out:
3941 return 0;
3942}
3943
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003944static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3945{
3946 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3947 struct iscsi_cmd *cmd;
3948 int ret = 0;
3949
3950 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3951 case ISCSI_OP_SCSI_CMD:
3952 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3953 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003954 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003955
3956 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3957 break;
3958 case ISCSI_OP_SCSI_DATA_OUT:
3959 ret = iscsit_handle_data_out(conn, buf);
3960 break;
3961 case ISCSI_OP_NOOP_OUT:
3962 cmd = NULL;
3963 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3964 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3965 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003966 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003967 }
3968 ret = iscsit_handle_nop_out(conn, cmd, buf);
3969 break;
3970 case ISCSI_OP_SCSI_TMFUNC:
3971 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3972 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003973 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003974
3975 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3976 break;
3977 case ISCSI_OP_TEXT:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003978 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3979 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003980 goto reject;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003981
3982 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003983 break;
3984 case ISCSI_OP_LOGOUT:
3985 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3986 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003987 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003988
3989 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3990 if (ret > 0)
3991 wait_for_completion_timeout(&conn->conn_logout_comp,
3992 SECONDS_FOR_LOGOUT_COMP * HZ);
3993 break;
3994 case ISCSI_OP_SNACK:
3995 ret = iscsit_handle_snack(conn, buf);
3996 break;
3997 default:
3998 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3999 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4000 pr_err("Cannot recover from unknown"
4001 " opcode while ERL=0, closing iSCSI connection.\n");
4002 return -1;
4003 }
4004 if (!conn->conn_ops->OFMarker) {
4005 pr_err("Unable to recover from unknown"
4006 " opcode while OFMarker=No, closing iSCSI"
4007 " connection.\n");
4008 return -1;
4009 }
4010 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4011 pr_err("Unable to recover from unknown"
4012 " opcode, closing iSCSI connection.\n");
4013 return -1;
4014 }
4015 break;
4016 }
4017
4018 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004019reject:
4020 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004021}
4022
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004023int iscsi_target_rx_thread(void *arg)
4024{
4025 int ret;
4026 u8 buffer[ISCSI_HDR_LEN], opcode;
4027 u32 checksum = 0, digest = 0;
4028 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004029 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004030 struct kvec iov;
4031 /*
4032 * Allow ourselves to be interrupted by SIGINT so that a
4033 * connection recovery / failure event can be triggered externally.
4034 */
4035 allow_signal(SIGINT);
4036
4037restart:
4038 conn = iscsi_rx_thread_pre_handler(ts);
4039 if (!conn)
4040 goto out;
4041
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004042 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4043 struct completion comp;
4044 int rc;
4045
4046 init_completion(&comp);
4047 rc = wait_for_completion_interruptible(&comp);
4048 if (rc < 0)
4049 goto transport_err;
4050
4051 goto out;
4052 }
4053
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004054 while (!kthread_should_stop()) {
4055 /*
4056 * Ensure that both TX and RX per connection kthreads
4057 * are scheduled to run on the same CPU.
4058 */
4059 iscsit_thread_check_cpumask(conn, current, 0);
4060
4061 memset(buffer, 0, ISCSI_HDR_LEN);
4062 memset(&iov, 0, sizeof(struct kvec));
4063
4064 iov.iov_base = buffer;
4065 iov.iov_len = ISCSI_HDR_LEN;
4066
4067 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4068 if (ret != ISCSI_HDR_LEN) {
4069 iscsit_rx_thread_wait_for_tcp(conn);
4070 goto transport_err;
4071 }
4072
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004073 if (conn->conn_ops->HeaderDigest) {
4074 iov.iov_base = &digest;
4075 iov.iov_len = ISCSI_CRC_LEN;
4076
4077 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4078 if (ret != ISCSI_CRC_LEN) {
4079 iscsit_rx_thread_wait_for_tcp(conn);
4080 goto transport_err;
4081 }
4082
4083 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4084 buffer, ISCSI_HDR_LEN,
4085 0, NULL, (u8 *)&checksum);
4086
4087 if (digest != checksum) {
4088 pr_err("HeaderDigest CRC32C failed,"
4089 " received 0x%08x, computed 0x%08x\n",
4090 digest, checksum);
4091 /*
4092 * Set the PDU to 0xff so it will intentionally
4093 * hit default in the switch below.
4094 */
4095 memset(buffer, 0xff, ISCSI_HDR_LEN);
4096 spin_lock_bh(&conn->sess->session_stats_lock);
4097 conn->sess->conn_digest_errors++;
4098 spin_unlock_bh(&conn->sess->session_stats_lock);
4099 } else {
4100 pr_debug("Got HeaderDigest CRC32C"
4101 " 0x%08x\n", checksum);
4102 }
4103 }
4104
4105 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4106 goto transport_err;
4107
4108 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4109
4110 if (conn->sess->sess_ops->SessionType &&
4111 ((!(opcode & ISCSI_OP_TEXT)) ||
4112 (!(opcode & ISCSI_OP_LOGOUT)))) {
4113 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4114 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004115 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4116 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004117 goto transport_err;
4118 }
4119
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004120 ret = iscsi_target_rx_opcode(conn, buffer);
4121 if (ret < 0)
4122 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004123 }
4124
4125transport_err:
4126 if (!signal_pending(current))
4127 atomic_set(&conn->transport_failed, 1);
4128 iscsit_take_action_for_connection_exit(conn);
4129 goto restart;
4130out:
4131 return 0;
4132}
4133
4134static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4135{
4136 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4137 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004138 /*
4139 * We expect this function to only ever be called from either RX or TX
4140 * thread context via iscsit_close_connection() once the other context
4141 * has been reset -> returned sleeping pre-handler state.
4142 */
4143 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004144 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004145
Andy Grover2fbb4712012-04-03 15:51:01 -07004146 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004147 spin_unlock_bh(&conn->cmd_lock);
4148
4149 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004150
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004151 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004152
4153 spin_lock_bh(&conn->cmd_lock);
4154 }
4155 spin_unlock_bh(&conn->cmd_lock);
4156}
4157
4158static void iscsit_stop_timers_for_cmds(
4159 struct iscsi_conn *conn)
4160{
4161 struct iscsi_cmd *cmd;
4162
4163 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004164 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004165 if (cmd->data_direction == DMA_TO_DEVICE)
4166 iscsit_stop_dataout_timer(cmd);
4167 }
4168 spin_unlock_bh(&conn->cmd_lock);
4169}
4170
4171int iscsit_close_connection(
4172 struct iscsi_conn *conn)
4173{
4174 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4175 struct iscsi_session *sess = conn->sess;
4176
4177 pr_debug("Closing iSCSI connection CID %hu on SID:"
4178 " %u\n", conn->cid, sess->sid);
4179 /*
4180 * Always up conn_logout_comp just in case the RX Thread is sleeping
4181 * and the logout response never got sent because the connection
4182 * failed.
4183 */
4184 complete(&conn->conn_logout_comp);
4185
4186 iscsi_release_thread_set(conn);
4187
4188 iscsit_stop_timers_for_cmds(conn);
4189 iscsit_stop_nopin_response_timer(conn);
4190 iscsit_stop_nopin_timer(conn);
4191 iscsit_free_queue_reqs_for_conn(conn);
4192
4193 /*
4194 * During Connection recovery drop unacknowledged out of order
4195 * commands for this connection, and prepare the other commands
4196 * for realligence.
4197 *
4198 * During normal operation clear the out of order commands (but
4199 * do not free the struct iscsi_ooo_cmdsn's) and release all
4200 * struct iscsi_cmds.
4201 */
4202 if (atomic_read(&conn->connection_recovery)) {
4203 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4204 iscsit_prepare_cmds_for_realligance(conn);
4205 } else {
4206 iscsit_clear_ooo_cmdsns_for_conn(conn);
4207 iscsit_release_commands_from_conn(conn);
4208 }
4209
4210 /*
4211 * Handle decrementing session or connection usage count if
4212 * a logout response was not able to be sent because the
4213 * connection failed. Fall back to Session Recovery here.
4214 */
4215 if (atomic_read(&conn->conn_logout_remove)) {
4216 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4217 iscsit_dec_conn_usage_count(conn);
4218 iscsit_dec_session_usage_count(sess);
4219 }
4220 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4221 iscsit_dec_conn_usage_count(conn);
4222
4223 atomic_set(&conn->conn_logout_remove, 0);
4224 atomic_set(&sess->session_reinstatement, 0);
4225 atomic_set(&sess->session_fall_back_to_erl0, 1);
4226 }
4227
4228 spin_lock_bh(&sess->conn_lock);
4229 list_del(&conn->conn_list);
4230
4231 /*
4232 * Attempt to let the Initiator know this connection failed by
4233 * sending an Connection Dropped Async Message on another
4234 * active connection.
4235 */
4236 if (atomic_read(&conn->connection_recovery))
4237 iscsit_build_conn_drop_async_message(conn);
4238
4239 spin_unlock_bh(&sess->conn_lock);
4240
4241 /*
4242 * If connection reinstatement is being performed on this connection,
4243 * up the connection reinstatement semaphore that is being blocked on
4244 * in iscsit_cause_connection_reinstatement().
4245 */
4246 spin_lock_bh(&conn->state_lock);
4247 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4248 spin_unlock_bh(&conn->state_lock);
4249 complete(&conn->conn_wait_comp);
4250 wait_for_completion(&conn->conn_post_wait_comp);
4251 spin_lock_bh(&conn->state_lock);
4252 }
4253
4254 /*
4255 * If connection reinstatement is being performed on this connection
4256 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4257 * connection wait rcfr semaphore that is being blocked on
4258 * an iscsit_connection_reinstatement_rcfr().
4259 */
4260 if (atomic_read(&conn->connection_wait_rcfr)) {
4261 spin_unlock_bh(&conn->state_lock);
4262 complete(&conn->conn_wait_rcfr_comp);
4263 wait_for_completion(&conn->conn_post_wait_comp);
4264 spin_lock_bh(&conn->state_lock);
4265 }
4266 atomic_set(&conn->connection_reinstatement, 1);
4267 spin_unlock_bh(&conn->state_lock);
4268
4269 /*
4270 * If any other processes are accessing this connection pointer we
4271 * must wait until they have completed.
4272 */
4273 iscsit_check_conn_usage_count(conn);
4274
4275 if (conn->conn_rx_hash.tfm)
4276 crypto_free_hash(conn->conn_rx_hash.tfm);
4277 if (conn->conn_tx_hash.tfm)
4278 crypto_free_hash(conn->conn_tx_hash.tfm);
4279
4280 if (conn->conn_cpumask)
4281 free_cpumask_var(conn->conn_cpumask);
4282
4283 kfree(conn->conn_ops);
4284 conn->conn_ops = NULL;
4285
Al Virobf6932f2012-07-21 08:55:18 +01004286 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004287 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004288
4289 if (conn->conn_transport->iscsit_free_conn)
4290 conn->conn_transport->iscsit_free_conn(conn);
4291
4292 iscsit_put_transport(conn->conn_transport);
4293
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004294 conn->thread_set = NULL;
4295
4296 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4297 conn->conn_state = TARG_CONN_STATE_FREE;
4298 kfree(conn);
4299
4300 spin_lock_bh(&sess->conn_lock);
4301 atomic_dec(&sess->nconn);
4302 pr_debug("Decremented iSCSI connection count to %hu from node:"
4303 " %s\n", atomic_read(&sess->nconn),
4304 sess->sess_ops->InitiatorName);
4305 /*
4306 * Make sure that if one connection fails in an non ERL=2 iSCSI
4307 * Session that they all fail.
4308 */
4309 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4310 !atomic_read(&sess->session_logout))
4311 atomic_set(&sess->session_fall_back_to_erl0, 1);
4312
4313 /*
4314 * If this was not the last connection in the session, and we are
4315 * performing session reinstatement or falling back to ERL=0, call
4316 * iscsit_stop_session() without sleeping to shutdown the other
4317 * active connections.
4318 */
4319 if (atomic_read(&sess->nconn)) {
4320 if (!atomic_read(&sess->session_reinstatement) &&
4321 !atomic_read(&sess->session_fall_back_to_erl0)) {
4322 spin_unlock_bh(&sess->conn_lock);
4323 return 0;
4324 }
4325 if (!atomic_read(&sess->session_stop_active)) {
4326 atomic_set(&sess->session_stop_active, 1);
4327 spin_unlock_bh(&sess->conn_lock);
4328 iscsit_stop_session(sess, 0, 0);
4329 return 0;
4330 }
4331 spin_unlock_bh(&sess->conn_lock);
4332 return 0;
4333 }
4334
4335 /*
4336 * If this was the last connection in the session and one of the
4337 * following is occurring:
4338 *
4339 * Session Reinstatement is not being performed, and are falling back
4340 * to ERL=0 call iscsit_close_session().
4341 *
4342 * Session Logout was requested. iscsit_close_session() will be called
4343 * elsewhere.
4344 *
4345 * Session Continuation is not being performed, start the Time2Retain
4346 * handler and check if sleep_on_sess_wait_sem is active.
4347 */
4348 if (!atomic_read(&sess->session_reinstatement) &&
4349 atomic_read(&sess->session_fall_back_to_erl0)) {
4350 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004351 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004352
4353 return 0;
4354 } else if (atomic_read(&sess->session_logout)) {
4355 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4356 sess->session_state = TARG_SESS_STATE_FREE;
4357 spin_unlock_bh(&sess->conn_lock);
4358
4359 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4360 complete(&sess->session_wait_comp);
4361
4362 return 0;
4363 } else {
4364 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4365 sess->session_state = TARG_SESS_STATE_FAILED;
4366
4367 if (!atomic_read(&sess->session_continuation)) {
4368 spin_unlock_bh(&sess->conn_lock);
4369 iscsit_start_time2retain_handler(sess);
4370 } else
4371 spin_unlock_bh(&sess->conn_lock);
4372
4373 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4374 complete(&sess->session_wait_comp);
4375
4376 return 0;
4377 }
4378 spin_unlock_bh(&sess->conn_lock);
4379
4380 return 0;
4381}
4382
4383int iscsit_close_session(struct iscsi_session *sess)
4384{
4385 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4386 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4387
4388 if (atomic_read(&sess->nconn)) {
4389 pr_err("%d connection(s) still exist for iSCSI session"
4390 " to %s\n", atomic_read(&sess->nconn),
4391 sess->sess_ops->InitiatorName);
4392 BUG();
4393 }
4394
4395 spin_lock_bh(&se_tpg->session_lock);
4396 atomic_set(&sess->session_logout, 1);
4397 atomic_set(&sess->session_reinstatement, 1);
4398 iscsit_stop_time2retain_timer(sess);
4399 spin_unlock_bh(&se_tpg->session_lock);
4400
4401 /*
4402 * transport_deregister_session_configfs() will clear the
4403 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4404 * can be setting it again with __transport_register_session() in
4405 * iscsi_post_login_handler() again after the iscsit_stop_session()
4406 * completes in iscsi_np context.
4407 */
4408 transport_deregister_session_configfs(sess->se_sess);
4409
4410 /*
4411 * If any other processes are accessing this session pointer we must
4412 * wait until they have completed. If we are in an interrupt (the
4413 * time2retain handler) and contain and active session usage count we
4414 * restart the timer and exit.
4415 */
4416 if (!in_interrupt()) {
4417 if (iscsit_check_session_usage_count(sess) == 1)
4418 iscsit_stop_session(sess, 1, 1);
4419 } else {
4420 if (iscsit_check_session_usage_count(sess) == 2) {
4421 atomic_set(&sess->session_logout, 0);
4422 iscsit_start_time2retain_handler(sess);
4423 return 0;
4424 }
4425 }
4426
4427 transport_deregister_session(sess->se_sess);
4428
4429 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4430 iscsit_free_connection_recovery_entires(sess);
4431
4432 iscsit_free_all_ooo_cmdsns(sess);
4433
4434 spin_lock_bh(&se_tpg->session_lock);
4435 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4436 sess->session_state = TARG_SESS_STATE_FREE;
4437 pr_debug("Released iSCSI session from node: %s\n",
4438 sess->sess_ops->InitiatorName);
4439 tpg->nsessions--;
4440 if (tpg->tpg_tiqn)
4441 tpg->tpg_tiqn->tiqn_nsessions--;
4442
4443 pr_debug("Decremented number of active iSCSI Sessions on"
4444 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4445
4446 spin_lock(&sess_idr_lock);
4447 idr_remove(&sess_idr, sess->session_index);
4448 spin_unlock(&sess_idr_lock);
4449
4450 kfree(sess->sess_ops);
4451 sess->sess_ops = NULL;
4452 spin_unlock_bh(&se_tpg->session_lock);
4453
4454 kfree(sess);
4455 return 0;
4456}
4457
4458static void iscsit_logout_post_handler_closesession(
4459 struct iscsi_conn *conn)
4460{
4461 struct iscsi_session *sess = conn->sess;
4462
4463 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4464 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4465
4466 atomic_set(&conn->conn_logout_remove, 0);
4467 complete(&conn->conn_logout_comp);
4468
4469 iscsit_dec_conn_usage_count(conn);
4470 iscsit_stop_session(sess, 1, 1);
4471 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004472 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004473}
4474
4475static void iscsit_logout_post_handler_samecid(
4476 struct iscsi_conn *conn)
4477{
4478 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4479 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4480
4481 atomic_set(&conn->conn_logout_remove, 0);
4482 complete(&conn->conn_logout_comp);
4483
4484 iscsit_cause_connection_reinstatement(conn, 1);
4485 iscsit_dec_conn_usage_count(conn);
4486}
4487
4488static void iscsit_logout_post_handler_diffcid(
4489 struct iscsi_conn *conn,
4490 u16 cid)
4491{
4492 struct iscsi_conn *l_conn;
4493 struct iscsi_session *sess = conn->sess;
4494
4495 if (!sess)
4496 return;
4497
4498 spin_lock_bh(&sess->conn_lock);
4499 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4500 if (l_conn->cid == cid) {
4501 iscsit_inc_conn_usage_count(l_conn);
4502 break;
4503 }
4504 }
4505 spin_unlock_bh(&sess->conn_lock);
4506
4507 if (!l_conn)
4508 return;
4509
4510 if (l_conn->sock)
4511 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4512
4513 spin_lock_bh(&l_conn->state_lock);
4514 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4515 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4516 spin_unlock_bh(&l_conn->state_lock);
4517
4518 iscsit_cause_connection_reinstatement(l_conn, 1);
4519 iscsit_dec_conn_usage_count(l_conn);
4520}
4521
4522/*
4523 * Return of 0 causes the TX thread to restart.
4524 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004525int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004526 struct iscsi_cmd *cmd,
4527 struct iscsi_conn *conn)
4528{
4529 int ret = 0;
4530
4531 switch (cmd->logout_reason) {
4532 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4533 switch (cmd->logout_response) {
4534 case ISCSI_LOGOUT_SUCCESS:
4535 case ISCSI_LOGOUT_CLEANUP_FAILED:
4536 default:
4537 iscsit_logout_post_handler_closesession(conn);
4538 break;
4539 }
4540 ret = 0;
4541 break;
4542 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4543 if (conn->cid == cmd->logout_cid) {
4544 switch (cmd->logout_response) {
4545 case ISCSI_LOGOUT_SUCCESS:
4546 case ISCSI_LOGOUT_CLEANUP_FAILED:
4547 default:
4548 iscsit_logout_post_handler_samecid(conn);
4549 break;
4550 }
4551 ret = 0;
4552 } else {
4553 switch (cmd->logout_response) {
4554 case ISCSI_LOGOUT_SUCCESS:
4555 iscsit_logout_post_handler_diffcid(conn,
4556 cmd->logout_cid);
4557 break;
4558 case ISCSI_LOGOUT_CID_NOT_FOUND:
4559 case ISCSI_LOGOUT_CLEANUP_FAILED:
4560 default:
4561 break;
4562 }
4563 ret = 1;
4564 }
4565 break;
4566 case ISCSI_LOGOUT_REASON_RECOVERY:
4567 switch (cmd->logout_response) {
4568 case ISCSI_LOGOUT_SUCCESS:
4569 case ISCSI_LOGOUT_CID_NOT_FOUND:
4570 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4571 case ISCSI_LOGOUT_CLEANUP_FAILED:
4572 default:
4573 break;
4574 }
4575 ret = 1;
4576 break;
4577 default:
4578 break;
4579
4580 }
4581 return ret;
4582}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004583EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004584
4585void iscsit_fail_session(struct iscsi_session *sess)
4586{
4587 struct iscsi_conn *conn;
4588
4589 spin_lock_bh(&sess->conn_lock);
4590 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4591 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4592 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4593 }
4594 spin_unlock_bh(&sess->conn_lock);
4595
4596 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4597 sess->session_state = TARG_SESS_STATE_FAILED;
4598}
4599
4600int iscsit_free_session(struct iscsi_session *sess)
4601{
4602 u16 conn_count = atomic_read(&sess->nconn);
4603 struct iscsi_conn *conn, *conn_tmp = NULL;
4604 int is_last;
4605
4606 spin_lock_bh(&sess->conn_lock);
4607 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4608
4609 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4610 conn_list) {
4611 if (conn_count == 0)
4612 break;
4613
4614 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4615 is_last = 1;
4616 } else {
4617 iscsit_inc_conn_usage_count(conn_tmp);
4618 is_last = 0;
4619 }
4620 iscsit_inc_conn_usage_count(conn);
4621
4622 spin_unlock_bh(&sess->conn_lock);
4623 iscsit_cause_connection_reinstatement(conn, 1);
4624 spin_lock_bh(&sess->conn_lock);
4625
4626 iscsit_dec_conn_usage_count(conn);
4627 if (is_last == 0)
4628 iscsit_dec_conn_usage_count(conn_tmp);
4629
4630 conn_count--;
4631 }
4632
4633 if (atomic_read(&sess->nconn)) {
4634 spin_unlock_bh(&sess->conn_lock);
4635 wait_for_completion(&sess->session_wait_comp);
4636 } else
4637 spin_unlock_bh(&sess->conn_lock);
4638
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004639 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004640 return 0;
4641}
4642
4643void iscsit_stop_session(
4644 struct iscsi_session *sess,
4645 int session_sleep,
4646 int connection_sleep)
4647{
4648 u16 conn_count = atomic_read(&sess->nconn);
4649 struct iscsi_conn *conn, *conn_tmp = NULL;
4650 int is_last;
4651
4652 spin_lock_bh(&sess->conn_lock);
4653 if (session_sleep)
4654 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4655
4656 if (connection_sleep) {
4657 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4658 conn_list) {
4659 if (conn_count == 0)
4660 break;
4661
4662 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4663 is_last = 1;
4664 } else {
4665 iscsit_inc_conn_usage_count(conn_tmp);
4666 is_last = 0;
4667 }
4668 iscsit_inc_conn_usage_count(conn);
4669
4670 spin_unlock_bh(&sess->conn_lock);
4671 iscsit_cause_connection_reinstatement(conn, 1);
4672 spin_lock_bh(&sess->conn_lock);
4673
4674 iscsit_dec_conn_usage_count(conn);
4675 if (is_last == 0)
4676 iscsit_dec_conn_usage_count(conn_tmp);
4677 conn_count--;
4678 }
4679 } else {
4680 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4681 iscsit_cause_connection_reinstatement(conn, 0);
4682 }
4683
4684 if (session_sleep && atomic_read(&sess->nconn)) {
4685 spin_unlock_bh(&sess->conn_lock);
4686 wait_for_completion(&sess->session_wait_comp);
4687 } else
4688 spin_unlock_bh(&sess->conn_lock);
4689}
4690
4691int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4692{
4693 struct iscsi_session *sess;
4694 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4695 struct se_session *se_sess, *se_sess_tmp;
4696 int session_count = 0;
4697
4698 spin_lock_bh(&se_tpg->session_lock);
4699 if (tpg->nsessions && !force) {
4700 spin_unlock_bh(&se_tpg->session_lock);
4701 return -1;
4702 }
4703
4704 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4705 sess_list) {
4706 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4707
4708 spin_lock(&sess->conn_lock);
4709 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4710 atomic_read(&sess->session_logout) ||
4711 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4712 spin_unlock(&sess->conn_lock);
4713 continue;
4714 }
4715 atomic_set(&sess->session_reinstatement, 1);
4716 spin_unlock(&sess->conn_lock);
4717 spin_unlock_bh(&se_tpg->session_lock);
4718
4719 iscsit_free_session(sess);
4720 spin_lock_bh(&se_tpg->session_lock);
4721
4722 session_count++;
4723 }
4724 spin_unlock_bh(&se_tpg->session_lock);
4725
4726 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4727 " Group: %hu\n", session_count, tpg->tpgt);
4728 return 0;
4729}
4730
4731MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4732MODULE_VERSION("4.1.x");
4733MODULE_AUTHOR("nab@Linux-iSCSI.org");
4734MODULE_LICENSE("GPL");
4735
4736module_init(iscsi_target_init_module);
4737module_exit(iscsi_target_cleanup_module);