blob: 3a179302b9044318ac41d5e81b4cdfa853274c5d [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 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001090 }
1091 /*
1092 * Call directly into transport_generic_new_cmd() to perform
1093 * the backend memory allocation.
1094 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001095 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001096 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001097 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001098
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001099 return 0;
1100}
1101EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1102
1103static int
1104iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1105 bool dump_payload)
1106{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001107 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001108 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1109 /*
1110 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1111 */
1112 if (dump_payload == true)
1113 goto after_immediate_data;
1114
1115 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1116 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001117after_immediate_data:
1118 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1119 /*
1120 * A PDU/CmdSN carrying Immediate Data passed
1121 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1122 * Immediate Bit is not set.
1123 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001124 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1125 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001126 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001127 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001128
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001129 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001130 int rc;
1131
1132 rc = iscsit_dump_data_payload(cmd->conn,
1133 cmd->first_burst_len, 1);
1134 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1135 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001136 } else if (cmd->unsolicited_data)
1137 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001138
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001139 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1140 /*
1141 * Immediate Data failed DataCRC and ERL>=1,
1142 * silently drop this PDU and let the initiator
1143 * plug the CmdSN gap.
1144 *
1145 * FIXME: Send Unsolicited NOPIN with reserved
1146 * TTT here to help the initiator figure out
1147 * the missing CmdSN, although they should be
1148 * intelligent enough to determine the missing
1149 * CmdSN and issue a retry to plug the sequence.
1150 */
1151 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001152 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001153 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1154 return -1;
1155
1156 return 0;
1157}
1158
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001159static int
1160iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1161 unsigned char *buf)
1162{
1163 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1164 int rc, immed_data;
1165 bool dump_payload = false;
1166
1167 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1168 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001169 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001170 /*
1171 * Allocation iovecs needed for struct socket operations for
1172 * traditional iSCSI block I/O.
1173 */
1174 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001175 return iscsit_add_reject_cmd(cmd,
1176 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001177 }
1178 immed_data = cmd->immediate_data;
1179
1180 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1181 if (rc < 0)
1182 return rc;
1183 else if (rc > 0)
1184 dump_payload = true;
1185
1186 if (!immed_data)
1187 return 0;
1188
1189 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1190}
1191
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001192static u32 iscsit_do_crypto_hash_sg(
1193 struct hash_desc *hash,
1194 struct iscsi_cmd *cmd,
1195 u32 data_offset,
1196 u32 data_length,
1197 u32 padding,
1198 u8 *pad_bytes)
1199{
1200 u32 data_crc;
1201 u32 i;
1202 struct scatterlist *sg;
1203 unsigned int page_off;
1204
1205 crypto_hash_init(hash);
1206
1207 sg = cmd->first_data_sg;
1208 page_off = cmd->first_data_sg_off;
1209
1210 i = 0;
1211 while (data_length) {
1212 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1213
1214 crypto_hash_update(hash, &sg[i], cur_len);
1215
1216 data_length -= cur_len;
1217 page_off = 0;
1218 i++;
1219 }
1220
1221 if (padding) {
1222 struct scatterlist pad_sg;
1223
1224 sg_init_one(&pad_sg, pad_bytes, padding);
1225 crypto_hash_update(hash, &pad_sg, padding);
1226 }
1227 crypto_hash_final(hash, (u8 *) &data_crc);
1228
1229 return data_crc;
1230}
1231
1232static void iscsit_do_crypto_hash_buf(
1233 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001234 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001235 u32 payload_length,
1236 u32 padding,
1237 u8 *pad_bytes,
1238 u8 *data_crc)
1239{
1240 struct scatterlist sg;
1241
1242 crypto_hash_init(hash);
1243
Jörn Engel8359cf42011-11-24 02:05:51 +01001244 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001245 crypto_hash_update(hash, &sg, payload_length);
1246
1247 if (padding) {
1248 sg_init_one(&sg, pad_bytes, padding);
1249 crypto_hash_update(hash, &sg, padding);
1250 }
1251 crypto_hash_final(hash, data_crc);
1252}
1253
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001254int
1255iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1256 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001257{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001258 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001259 struct iscsi_cmd *cmd = NULL;
1260 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001261 u32 payload_length = ntoh24(hdr->dlength);
1262 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001263
1264 if (!payload_length) {
1265 pr_err("DataOUT payload is ZERO, protocol error.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001266 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1267 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001268 }
1269
1270 /* iSCSI write */
1271 spin_lock_bh(&conn->sess->session_stats_lock);
1272 conn->sess->rx_data_octets += payload_length;
1273 if (conn->sess->se_sess->se_node_acl) {
1274 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1275 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1276 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1277 }
1278 spin_unlock_bh(&conn->sess->session_stats_lock);
1279
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001280 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001281 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001282 " MaxXmitDataSegmentLength: %u\n", payload_length,
1283 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001284 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1285 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001286 }
1287
1288 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1289 payload_length);
1290 if (!cmd)
1291 return 0;
1292
1293 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1294 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001295 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001296 payload_length, conn->cid);
1297
1298 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1299 pr_err("Command ITT: 0x%08x received DataOUT after"
1300 " last DataOUT received, dumping payload\n",
1301 cmd->init_task_tag);
1302 return iscsit_dump_data_payload(conn, payload_length, 1);
1303 }
1304
1305 if (cmd->data_direction != DMA_TO_DEVICE) {
1306 pr_err("Command ITT: 0x%08x received DataOUT for a"
1307 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001308 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001309 }
1310 se_cmd = &cmd->se_cmd;
1311 iscsit_mod_dataout_timer(cmd);
1312
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001313 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001314 pr_err("DataOut Offset: %u, Length %u greater than"
1315 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001316 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001317 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001318 }
1319
1320 if (cmd->unsolicited_data) {
1321 int dump_unsolicited_data = 0;
1322
1323 if (conn->sess->sess_ops->InitialR2T) {
1324 pr_err("Received unexpected unsolicited data"
1325 " while InitialR2T=Yes, protocol error.\n");
1326 transport_send_check_condition_and_sense(&cmd->se_cmd,
1327 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1328 return -1;
1329 }
1330 /*
1331 * Special case for dealing with Unsolicited DataOUT
1332 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1333 * failures;
1334 */
1335
1336 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001337 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001338 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001339 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001340
1341 if (dump_unsolicited_data) {
1342 /*
1343 * Check if a delayed TASK_ABORTED status needs to
1344 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1345 * received with the unsolicitied data out.
1346 */
1347 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1348 iscsit_stop_dataout_timer(cmd);
1349
1350 transport_check_aborted_status(se_cmd,
1351 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1352 return iscsit_dump_data_payload(conn, payload_length, 1);
1353 }
1354 } else {
1355 /*
1356 * For the normal solicited data path:
1357 *
1358 * Check for a delayed TASK_ABORTED status and dump any
1359 * incoming data out payload if one exists. Also, when the
1360 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1361 * data out sequence, we decrement outstanding_r2ts. Once
1362 * outstanding_r2ts reaches zero, go ahead and send the delayed
1363 * TASK_ABORTED status.
1364 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001365 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001366 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1367 if (--cmd->outstanding_r2ts < 1) {
1368 iscsit_stop_dataout_timer(cmd);
1369 transport_check_aborted_status(
1370 se_cmd, 1);
1371 }
1372
1373 return iscsit_dump_data_payload(conn, payload_length, 1);
1374 }
1375 }
1376 /*
1377 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1378 * within-command recovery checks before receiving the payload.
1379 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001380 rc = iscsit_check_pre_dataout(cmd, buf);
1381 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001382 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001383 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001384 return -1;
1385
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001386 *out_cmd = cmd;
1387 return 0;
1388}
1389EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1390
1391static int
1392iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1393 struct iscsi_data *hdr)
1394{
1395 struct kvec *iov;
1396 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1397 u32 payload_length = ntoh24(hdr->dlength);
1398 int iov_ret, data_crc_failed = 0;
1399
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001400 rx_size += payload_length;
1401 iov = &cmd->iov_data[0];
1402
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001403 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1404 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001405 if (iov_ret < 0)
1406 return -1;
1407
1408 iov_count += iov_ret;
1409
1410 padding = ((-payload_length) & 3);
1411 if (padding != 0) {
1412 iov[iov_count].iov_base = cmd->pad_bytes;
1413 iov[iov_count++].iov_len = padding;
1414 rx_size += padding;
1415 pr_debug("Receiving %u padding bytes.\n", padding);
1416 }
1417
1418 if (conn->conn_ops->DataDigest) {
1419 iov[iov_count].iov_base = &checksum;
1420 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1421 rx_size += ISCSI_CRC_LEN;
1422 }
1423
1424 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1425
1426 iscsit_unmap_iovec(cmd);
1427
1428 if (rx_got != rx_size)
1429 return -1;
1430
1431 if (conn->conn_ops->DataDigest) {
1432 u32 data_crc;
1433
1434 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001435 be32_to_cpu(hdr->offset),
1436 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001437 cmd->pad_bytes);
1438
1439 if (checksum != data_crc) {
1440 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1441 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1442 " does not match computed 0x%08x\n",
1443 hdr->itt, hdr->offset, payload_length,
1444 hdr->datasn, checksum, data_crc);
1445 data_crc_failed = 1;
1446 } else {
1447 pr_debug("Got CRC32C DataDigest 0x%08x for"
1448 " %u bytes of Data Out\n", checksum,
1449 payload_length);
1450 }
1451 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001452
1453 return data_crc_failed;
1454}
1455
1456int
1457iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1458 bool data_crc_failed)
1459{
1460 struct iscsi_conn *conn = cmd->conn;
1461 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001462 /*
1463 * Increment post receive data and CRC values or perform
1464 * within-command recovery.
1465 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001466 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1467 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001468 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001469 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001470 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001471 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1472 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001473 /*
1474 * Handle extra special case for out of order
1475 * Unsolicited Data Out.
1476 */
1477 spin_lock_bh(&cmd->istate_lock);
1478 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1479 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1480 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1481 spin_unlock_bh(&cmd->istate_lock);
1482
1483 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001484 if (ooo_cmdsn)
1485 return 0;
1486 target_execute_cmd(&cmd->se_cmd);
1487 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001488 } else /* DATAOUT_CANNOT_RECOVER */
1489 return -1;
1490
1491 return 0;
1492}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001493EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001494
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001495static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1496{
1497 struct iscsi_cmd *cmd;
1498 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1499 int rc;
1500 bool data_crc_failed = false;
1501
1502 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1503 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001504 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001505 else if (!cmd)
1506 return 0;
1507
1508 rc = iscsit_get_dataout(conn, cmd, hdr);
1509 if (rc < 0)
1510 return rc;
1511 else if (rc > 0)
1512 data_crc_failed = true;
1513
1514 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1515}
1516
Nicholas Bellinger778de362013-06-14 16:07:47 -07001517int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1518 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001519{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001520 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001521
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001522 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001523 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1524 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001525 if (!cmd)
1526 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1527 (unsigned char *)hdr);
1528
Nicholas Bellingerba159912013-07-03 03:48:24 -07001529 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1530 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001531 }
1532
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001533 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001534 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001535 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001536 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001537 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001538 if (!cmd)
1539 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1540 (unsigned char *)hdr);
1541
Nicholas Bellingerba159912013-07-03 03:48:24 -07001542 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1543 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001544 }
1545
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001546 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001547 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001548 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001549 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1550 payload_length);
1551 /*
1552 * This is not a response to a Unsolicited NopIN, which means
1553 * it can either be a NOPOUT ping request (with a valid ITT),
1554 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1555 * Either way, make sure we allocate an struct iscsi_cmd, as both
1556 * can contain ping data.
1557 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001558 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001559 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1560 cmd->i_state = ISTATE_SEND_NOPIN;
1561 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1562 1 : 0);
1563 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1564 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001565 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1566 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001567 cmd->data_direction = DMA_NONE;
1568 }
1569
Nicholas Bellinger778de362013-06-14 16:07:47 -07001570 return 0;
1571}
1572EXPORT_SYMBOL(iscsit_setup_nop_out);
1573
1574int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1575 struct iscsi_nopout *hdr)
1576{
1577 struct iscsi_cmd *cmd_p = NULL;
1578 int cmdsn_ret = 0;
1579 /*
1580 * Initiator is expecting a NopIN ping reply..
1581 */
1582 if (hdr->itt != RESERVED_ITT) {
1583 BUG_ON(!cmd);
1584
1585 spin_lock_bh(&conn->cmd_lock);
1586 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1587 spin_unlock_bh(&conn->cmd_lock);
1588
1589 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1590
1591 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1592 iscsit_add_cmd_to_response_queue(cmd, conn,
1593 cmd->i_state);
1594 return 0;
1595 }
1596
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001597 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1598 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001599 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1600 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001601 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001602 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001603
1604 return 0;
1605 }
1606 /*
1607 * This was a response to a unsolicited NOPIN ping.
1608 */
1609 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1610 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1611 if (!cmd_p)
1612 return -EINVAL;
1613
1614 iscsit_stop_nopin_response_timer(conn);
1615
1616 cmd_p->i_state = ISTATE_REMOVE;
1617 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1618
1619 iscsit_start_nopin_timer(conn);
1620 return 0;
1621 }
1622 /*
1623 * Otherwise, initiator is not expecting a NOPIN is response.
1624 * Just ignore for now.
1625 */
1626 return 0;
1627}
1628EXPORT_SYMBOL(iscsit_process_nop_out);
1629
1630static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1631 unsigned char *buf)
1632{
1633 unsigned char *ping_data = NULL;
1634 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1635 struct kvec *iov = NULL;
1636 u32 payload_length = ntoh24(hdr->dlength);
1637 int ret;
1638
1639 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1640 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001641 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001642 /*
1643 * Handle NOP-OUT payload for traditional iSCSI sockets
1644 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001645 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001646 u32 checksum, data_crc, padding = 0;
1647 int niov = 0, rx_got, rx_size = payload_length;
1648
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001649 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1650 if (!ping_data) {
1651 pr_err("Unable to allocate memory for"
1652 " NOPOUT ping data.\n");
1653 ret = -1;
1654 goto out;
1655 }
1656
1657 iov = &cmd->iov_misc[0];
1658 iov[niov].iov_base = ping_data;
1659 iov[niov++].iov_len = payload_length;
1660
1661 padding = ((-payload_length) & 3);
1662 if (padding != 0) {
1663 pr_debug("Receiving %u additional bytes"
1664 " for padding.\n", padding);
1665 iov[niov].iov_base = &cmd->pad_bytes;
1666 iov[niov++].iov_len = padding;
1667 rx_size += padding;
1668 }
1669 if (conn->conn_ops->DataDigest) {
1670 iov[niov].iov_base = &checksum;
1671 iov[niov++].iov_len = ISCSI_CRC_LEN;
1672 rx_size += ISCSI_CRC_LEN;
1673 }
1674
1675 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1676 if (rx_got != rx_size) {
1677 ret = -1;
1678 goto out;
1679 }
1680
1681 if (conn->conn_ops->DataDigest) {
1682 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1683 ping_data, payload_length,
1684 padding, cmd->pad_bytes,
1685 (u8 *)&data_crc);
1686
1687 if (checksum != data_crc) {
1688 pr_err("Ping data CRC32C DataDigest"
1689 " 0x%08x does not match computed 0x%08x\n",
1690 checksum, data_crc);
1691 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1692 pr_err("Unable to recover from"
1693 " NOPOUT Ping DataCRC failure while in"
1694 " ERL=0.\n");
1695 ret = -1;
1696 goto out;
1697 } else {
1698 /*
1699 * Silently drop this PDU and let the
1700 * initiator plug the CmdSN gap.
1701 */
1702 pr_debug("Dropping NOPOUT"
1703 " Command CmdSN: 0x%08x due to"
1704 " DataCRC error.\n", hdr->cmdsn);
1705 ret = 0;
1706 goto out;
1707 }
1708 } else {
1709 pr_debug("Got CRC32C DataDigest"
1710 " 0x%08x for %u bytes of ping data.\n",
1711 checksum, payload_length);
1712 }
1713 }
1714
1715 ping_data[payload_length] = '\0';
1716 /*
1717 * Attach ping data to struct iscsi_cmd->buf_ptr.
1718 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001719 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001720 cmd->buf_ptr_size = payload_length;
1721
1722 pr_debug("Got %u bytes of NOPOUT ping"
1723 " data.\n", payload_length);
1724 pr_debug("Ping Data: \"%s\"\n", ping_data);
1725 }
1726
Nicholas Bellinger778de362013-06-14 16:07:47 -07001727 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001728out:
1729 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001730 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001731
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001732 kfree(ping_data);
1733 return ret;
1734}
1735
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001736int
1737iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1738 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001739{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001740 struct se_tmr_req *se_tmr;
1741 struct iscsi_tmr_req *tmr_req;
1742 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001743 int out_of_order_cmdsn = 0, ret;
1744 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001745 u8 function;
1746
1747 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001748 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1749 function = hdr->flags;
1750
1751 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1752 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1753 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1754 hdr->rtt, hdr->refcmdsn, conn->cid);
1755
1756 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1757 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001758 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001759 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001760 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001761 }
1762
1763 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1764 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1765 pr_err("Task Management Request TASK_REASSIGN not"
1766 " issued as immediate command, bad iSCSI Initiator"
1767 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001768 return iscsit_add_reject_cmd(cmd,
1769 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001770 }
1771 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001772 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1773 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001774
Andy Groverd28b11692012-04-03 15:51:22 -07001775 cmd->data_direction = DMA_NONE;
1776
1777 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1778 if (!cmd->tmr_req) {
1779 pr_err("Unable to allocate memory for"
1780 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001781 return iscsit_add_reject_cmd(cmd,
1782 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1783 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001784 }
1785
1786 /*
1787 * TASK_REASSIGN for ERL=2 / connection stays inside of
1788 * LIO-Target $FABRIC_MOD
1789 */
1790 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1791
1792 u8 tcm_function;
1793 int ret;
1794
1795 transport_init_se_cmd(&cmd->se_cmd,
1796 &lio_target_fabric_configfs->tf_ops,
1797 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001798 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001799
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001800 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1801 sess_ref = true;
1802
Andy Groverd28b11692012-04-03 15:51:22 -07001803 switch (function) {
1804 case ISCSI_TM_FUNC_ABORT_TASK:
1805 tcm_function = TMR_ABORT_TASK;
1806 break;
1807 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1808 tcm_function = TMR_ABORT_TASK_SET;
1809 break;
1810 case ISCSI_TM_FUNC_CLEAR_ACA:
1811 tcm_function = TMR_CLEAR_ACA;
1812 break;
1813 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1814 tcm_function = TMR_CLEAR_TASK_SET;
1815 break;
1816 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1817 tcm_function = TMR_LUN_RESET;
1818 break;
1819 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1820 tcm_function = TMR_TARGET_WARM_RESET;
1821 break;
1822 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1823 tcm_function = TMR_TARGET_COLD_RESET;
1824 break;
1825 default:
1826 pr_err("Unknown iSCSI TMR Function:"
1827 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001828 return iscsit_add_reject_cmd(cmd,
1829 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001830 }
1831
1832 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1833 tcm_function, GFP_KERNEL);
1834 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001835 return iscsit_add_reject_cmd(cmd,
1836 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001837
1838 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1839 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001840
1841 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1842 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1843 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1844 cmd->init_task_tag = hdr->itt;
1845 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001846 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1847 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001848 se_tmr = cmd->se_cmd.se_tmr_req;
1849 tmr_req = cmd->tmr_req;
1850 /*
1851 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1852 */
1853 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001854 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1855 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001856 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001857 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1858 goto attach;
1859 }
1860 }
1861
1862 switch (function) {
1863 case ISCSI_TM_FUNC_ABORT_TASK:
1864 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001865 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001866 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001867 break;
1868 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1869 case ISCSI_TM_FUNC_CLEAR_ACA:
1870 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1871 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1872 break;
1873 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1874 if (iscsit_tmr_task_warm_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_TARGET_COLD_RESET:
1880 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001881 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1882 goto attach;
1883 }
1884 break;
1885 case ISCSI_TM_FUNC_TASK_REASSIGN:
1886 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1887 /*
1888 * Perform sanity checks on the ExpDataSN only if the
1889 * TASK_REASSIGN was successful.
1890 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001891 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001892 break;
1893
1894 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001895 return iscsit_add_reject_cmd(cmd,
1896 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001897 break;
1898 default:
1899 pr_err("Unknown TMR function: 0x%02x, protocol"
1900 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001901 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1902 goto attach;
1903 }
1904
1905 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1906 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1907 se_tmr->call_transport = 1;
1908attach:
1909 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001910 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001911 spin_unlock_bh(&conn->cmd_lock);
1912
1913 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001914 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001915 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1916 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001917 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001918 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001919 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001920 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001921 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001922 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001923
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001924 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001925 return 0;
1926 /*
1927 * Found the referenced task, send to transport for processing.
1928 */
1929 if (se_tmr->call_transport)
1930 return transport_generic_handle_tmr(&cmd->se_cmd);
1931
1932 /*
1933 * Could not find the referenced LUN, task, or Task Management
1934 * command not authorized or supported. Change state and
1935 * let the tx_thread send the response.
1936 *
1937 * For connection recovery, this is also the default action for
1938 * TMR TASK_REASSIGN.
1939 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001940 if (sess_ref) {
1941 pr_debug("Handle TMR, using sess_ref=true check\n");
1942 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1943 }
1944
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001945 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1946 return 0;
1947}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001948EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001949
1950/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001951int
1952iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1953 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001954{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001955 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001956
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001957 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001958 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001959 "greater than MaxXmitDataSegmentLength %u.\n",
1960 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001961 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1962 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001963 }
1964
1965 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1966 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1967 hdr->exp_statsn, payload_length);
1968
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001969 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1970 cmd->i_state = ISTATE_SEND_TEXTRSP;
1971 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1972 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1973 cmd->targ_xfer_tag = 0xFFFFFFFF;
1974 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1975 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1976 cmd->data_direction = DMA_NONE;
1977
1978 return 0;
1979}
1980EXPORT_SYMBOL(iscsit_setup_text_cmd);
1981
1982int
1983iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1984 struct iscsi_text *hdr)
1985{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001986 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001987 int cmdsn_ret;
1988
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001989 if (!text_in) {
1990 pr_err("Unable to locate text_in buffer for sendtargets"
1991 " discovery\n");
1992 goto reject;
1993 }
1994 if (strncmp("SendTargets", text_in, 11) != 0) {
1995 pr_err("Received Text Data that is not"
1996 " SendTargets, cannot continue.\n");
1997 goto reject;
1998 }
1999 text_ptr = strchr(text_in, '=');
2000 if (!text_ptr) {
2001 pr_err("No \"=\" separator found in Text Data,"
2002 " cannot continue.\n");
2003 goto reject;
2004 }
2005 if (!strncmp("=All", text_ptr, 4)) {
2006 cmd->cmd_flags |= IFC_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002007 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2008 !strncmp("=eui.", text_ptr, 5)) {
2009 cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002010 } else {
2011 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2012 goto reject;
2013 }
2014
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002015 spin_lock_bh(&conn->cmd_lock);
2016 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2017 spin_unlock_bh(&conn->cmd_lock);
2018
2019 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2020
2021 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002022 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2023 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002024 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002025 return -1;
2026
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002027 return 0;
2028 }
2029
2030 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002031
2032reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002033 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2034 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002035}
2036EXPORT_SYMBOL(iscsit_process_text_cmd);
2037
2038static int
2039iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2040 unsigned char *buf)
2041{
2042 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2043 char *text_in = NULL;
2044 u32 payload_length = ntoh24(hdr->dlength);
2045 int rx_size, rc;
2046
2047 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2048 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002049 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002050
2051 rx_size = payload_length;
2052 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002053 u32 checksum = 0, data_crc = 0;
2054 u32 padding = 0, pad_bytes = 0;
2055 int niov = 0, rx_got;
2056 struct kvec iov[3];
2057
2058 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002059 if (!text_in) {
2060 pr_err("Unable to allocate memory for"
2061 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002062 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002063 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002064 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002065
2066 memset(iov, 0, 3 * sizeof(struct kvec));
2067 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002068 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002069
2070 padding = ((-payload_length) & 3);
2071 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002072 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002073 iov[niov++].iov_len = padding;
2074 rx_size += padding;
2075 pr_debug("Receiving %u additional bytes"
2076 " for padding.\n", padding);
2077 }
2078 if (conn->conn_ops->DataDigest) {
2079 iov[niov].iov_base = &checksum;
2080 iov[niov++].iov_len = ISCSI_CRC_LEN;
2081 rx_size += ISCSI_CRC_LEN;
2082 }
2083
2084 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002085 if (rx_got != rx_size)
2086 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002087
2088 if (conn->conn_ops->DataDigest) {
2089 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002090 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002091 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002092 (u8 *)&data_crc);
2093
2094 if (checksum != data_crc) {
2095 pr_err("Text data CRC32C DataDigest"
2096 " 0x%08x does not match computed"
2097 " 0x%08x\n", checksum, data_crc);
2098 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2099 pr_err("Unable to recover from"
2100 " Text Data digest failure while in"
2101 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002102 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002103 } else {
2104 /*
2105 * Silently drop this PDU and let the
2106 * initiator plug the CmdSN gap.
2107 */
2108 pr_debug("Dropping Text"
2109 " Command CmdSN: 0x%08x due to"
2110 " DataCRC error.\n", hdr->cmdsn);
2111 kfree(text_in);
2112 return 0;
2113 }
2114 } else {
2115 pr_debug("Got CRC32C DataDigest"
2116 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002117 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002118 }
2119 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002120 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002121 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002122 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002123 }
2124
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002125 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002126
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002127reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002128 kfree(cmd->text_in_ptr);
2129 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002130 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002131}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002132EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002133
2134int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2135{
2136 struct iscsi_conn *conn_p;
2137 struct iscsi_session *sess = conn->sess;
2138
2139 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2140 " for SID: %u.\n", conn->cid, conn->sess->sid);
2141
2142 atomic_set(&sess->session_logout, 1);
2143 atomic_set(&conn->conn_logout_remove, 1);
2144 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2145
2146 iscsit_inc_conn_usage_count(conn);
2147 iscsit_inc_session_usage_count(sess);
2148
2149 spin_lock_bh(&sess->conn_lock);
2150 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2151 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2152 continue;
2153
2154 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2155 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2156 }
2157 spin_unlock_bh(&sess->conn_lock);
2158
2159 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2160
2161 return 0;
2162}
2163
2164int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2165{
2166 struct iscsi_conn *l_conn;
2167 struct iscsi_session *sess = conn->sess;
2168
2169 pr_debug("Received logout request CLOSECONNECTION for CID:"
2170 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2171
2172 /*
2173 * A Logout Request with a CLOSECONNECTION reason code for a CID
2174 * can arrive on a connection with a differing CID.
2175 */
2176 if (conn->cid == cmd->logout_cid) {
2177 spin_lock_bh(&conn->state_lock);
2178 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2179 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2180
2181 atomic_set(&conn->conn_logout_remove, 1);
2182 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2183 iscsit_inc_conn_usage_count(conn);
2184
2185 spin_unlock_bh(&conn->state_lock);
2186 } else {
2187 /*
2188 * Handle all different cid CLOSECONNECTION requests in
2189 * iscsit_logout_post_handler_diffcid() as to give enough
2190 * time for any non immediate command's CmdSN to be
2191 * acknowledged on the connection in question.
2192 *
2193 * Here we simply make sure the CID is still around.
2194 */
2195 l_conn = iscsit_get_conn_from_cid(sess,
2196 cmd->logout_cid);
2197 if (!l_conn) {
2198 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2199 iscsit_add_cmd_to_response_queue(cmd, conn,
2200 cmd->i_state);
2201 return 0;
2202 }
2203
2204 iscsit_dec_conn_usage_count(l_conn);
2205 }
2206
2207 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2208
2209 return 0;
2210}
2211
2212int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2213{
2214 struct iscsi_session *sess = conn->sess;
2215
2216 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2217 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2218
2219 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2220 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2221 " while ERL!=2.\n");
2222 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2223 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2224 return 0;
2225 }
2226
2227 if (conn->cid == cmd->logout_cid) {
2228 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2229 " with CID: %hu on CID: %hu, implementation error.\n",
2230 cmd->logout_cid, conn->cid);
2231 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2232 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2233 return 0;
2234 }
2235
2236 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2237
2238 return 0;
2239}
2240
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002241int
2242iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2243 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002244{
2245 int cmdsn_ret, logout_remove = 0;
2246 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002247 struct iscsi_logout *hdr;
2248 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2249
2250 hdr = (struct iscsi_logout *) buf;
2251 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002252
2253 if (tiqn) {
2254 spin_lock(&tiqn->logout_stats.lock);
2255 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2256 tiqn->logout_stats.normal_logouts++;
2257 else
2258 tiqn->logout_stats.abnormal_logouts++;
2259 spin_unlock(&tiqn->logout_stats.lock);
2260 }
2261
2262 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2263 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2264 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2265 hdr->cid, conn->cid);
2266
2267 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2268 pr_err("Received logout request on connection that"
2269 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002270 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002271 return 0;
2272 }
2273
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002274 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2275 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2276 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2277 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2278 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002279 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2280 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2281 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002282 cmd->logout_reason = reason_code;
2283 cmd->data_direction = DMA_NONE;
2284
2285 /*
2286 * We need to sleep in these cases (by returning 1) until the Logout
2287 * Response gets sent in the tx thread.
2288 */
2289 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2290 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002291 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002292 logout_remove = 1;
2293
2294 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002295 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002296 spin_unlock_bh(&conn->cmd_lock);
2297
2298 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002299 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002300
2301 /*
2302 * Immediate commands are executed, well, immediately.
2303 * Non-Immediate Logout Commands are executed in CmdSN order.
2304 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002305 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002306 int ret = iscsit_execute_cmd(cmd, 0);
2307
2308 if (ret < 0)
2309 return ret;
2310 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002311 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002312 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002313 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002314 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2315 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002316 }
2317
2318 return logout_remove;
2319}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002320EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002321
2322static int iscsit_handle_snack(
2323 struct iscsi_conn *conn,
2324 unsigned char *buf)
2325{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002326 struct iscsi_snack *hdr;
2327
2328 hdr = (struct iscsi_snack *) buf;
2329 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002330
2331 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2332 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2333 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2334 hdr->begrun, hdr->runlength, conn->cid);
2335
2336 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2337 pr_err("Initiator sent SNACK request while in"
2338 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002339 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2340 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002341 }
2342 /*
2343 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2344 * call from inside iscsi_send_recovery_datain_or_r2t().
2345 */
2346 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2347 case 0:
2348 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002349 hdr->itt,
2350 be32_to_cpu(hdr->ttt),
2351 be32_to_cpu(hdr->begrun),
2352 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002353 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002354 return iscsit_handle_status_snack(conn, hdr->itt,
2355 be32_to_cpu(hdr->ttt),
2356 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002357 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002358 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2359 be32_to_cpu(hdr->begrun),
2360 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002361 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2362 /* FIXME: Support R-Data SNACK */
2363 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002364 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2365 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002366 default:
2367 pr_err("Unknown SNACK type 0x%02x, protocol"
2368 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002369 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2370 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002371 }
2372
2373 return 0;
2374}
2375
2376static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2377{
2378 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2379 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2380 wait_for_completion_interruptible_timeout(
2381 &conn->rx_half_close_comp,
2382 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2383 }
2384}
2385
2386static int iscsit_handle_immediate_data(
2387 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002388 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002389 u32 length)
2390{
2391 int iov_ret, rx_got = 0, rx_size = 0;
2392 u32 checksum, iov_count = 0, padding = 0;
2393 struct iscsi_conn *conn = cmd->conn;
2394 struct kvec *iov;
2395
2396 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2397 if (iov_ret < 0)
2398 return IMMEDIATE_DATA_CANNOT_RECOVER;
2399
2400 rx_size = length;
2401 iov_count = iov_ret;
2402 iov = &cmd->iov_data[0];
2403
2404 padding = ((-length) & 3);
2405 if (padding != 0) {
2406 iov[iov_count].iov_base = cmd->pad_bytes;
2407 iov[iov_count++].iov_len = padding;
2408 rx_size += padding;
2409 }
2410
2411 if (conn->conn_ops->DataDigest) {
2412 iov[iov_count].iov_base = &checksum;
2413 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2414 rx_size += ISCSI_CRC_LEN;
2415 }
2416
2417 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2418
2419 iscsit_unmap_iovec(cmd);
2420
2421 if (rx_got != rx_size) {
2422 iscsit_rx_thread_wait_for_tcp(conn);
2423 return IMMEDIATE_DATA_CANNOT_RECOVER;
2424 }
2425
2426 if (conn->conn_ops->DataDigest) {
2427 u32 data_crc;
2428
2429 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2430 cmd->write_data_done, length, padding,
2431 cmd->pad_bytes);
2432
2433 if (checksum != data_crc) {
2434 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2435 " does not match computed 0x%08x\n", checksum,
2436 data_crc);
2437
2438 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2439 pr_err("Unable to recover from"
2440 " Immediate Data digest failure while"
2441 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002442 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002443 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002444 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002445 return IMMEDIATE_DATA_CANNOT_RECOVER;
2446 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002447 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002448 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002449 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002450 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2451 }
2452 } else {
2453 pr_debug("Got CRC32C DataDigest 0x%08x for"
2454 " %u bytes of Immediate Data\n", checksum,
2455 length);
2456 }
2457 }
2458
2459 cmd->write_data_done += length;
2460
Andy Groverebf1d952012-04-03 15:51:24 -07002461 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002462 spin_lock_bh(&cmd->istate_lock);
2463 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2464 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2465 spin_unlock_bh(&cmd->istate_lock);
2466 }
2467
2468 return IMMEDIATE_DATA_NORMAL_OPERATION;
2469}
2470
2471/*
2472 * Called with sess->conn_lock held.
2473 */
2474/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2475 with active network interface */
2476static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2477{
2478 struct iscsi_cmd *cmd;
2479 struct iscsi_conn *conn_p;
2480
2481 /*
2482 * Only send a Asynchronous Message on connections whos network
2483 * interface is still functional.
2484 */
2485 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2486 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2487 iscsit_inc_conn_usage_count(conn_p);
2488 break;
2489 }
2490 }
2491
2492 if (!conn_p)
2493 return;
2494
Wei Yongjun3c989d72012-11-23 12:07:39 +08002495 cmd = iscsit_allocate_cmd(conn_p, GFP_ATOMIC);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002496 if (!cmd) {
2497 iscsit_dec_conn_usage_count(conn_p);
2498 return;
2499 }
2500
2501 cmd->logout_cid = conn->cid;
2502 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2503 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2504
2505 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002506 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002507 spin_unlock_bh(&conn_p->cmd_lock);
2508
2509 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2510 iscsit_dec_conn_usage_count(conn_p);
2511}
2512
2513static int iscsit_send_conn_drop_async_message(
2514 struct iscsi_cmd *cmd,
2515 struct iscsi_conn *conn)
2516{
2517 struct iscsi_async *hdr;
2518
2519 cmd->tx_size = ISCSI_HDR_LEN;
2520 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2521
2522 hdr = (struct iscsi_async *) cmd->pdu;
2523 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2524 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002525 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002526 cmd->targ_xfer_tag = 0xFFFFFFFF;
2527 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2528 cmd->stat_sn = conn->stat_sn++;
2529 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2530 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2531 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2532 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2533 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2534 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2535 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2536
2537 if (conn->conn_ops->HeaderDigest) {
2538 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2539
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002540 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2541 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002542
2543 cmd->tx_size += ISCSI_CRC_LEN;
2544 pr_debug("Attaching CRC32C HeaderDigest to"
2545 " Async Message 0x%08x\n", *header_digest);
2546 }
2547
2548 cmd->iov_misc[0].iov_base = cmd->pdu;
2549 cmd->iov_misc[0].iov_len = cmd->tx_size;
2550 cmd->iov_misc_count = 1;
2551
2552 pr_debug("Sending Connection Dropped Async Message StatSN:"
2553 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2554 cmd->logout_cid, conn->cid);
2555 return 0;
2556}
2557
Andy Grover6f3c0e62012-04-03 15:51:09 -07002558static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2559{
2560 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2561 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2562 wait_for_completion_interruptible_timeout(
2563 &conn->tx_half_close_comp,
2564 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2565 }
2566}
2567
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002568static void
2569iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2570 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2571 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002572{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002573 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2574 hdr->flags = datain->flags;
2575 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2576 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2577 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2578 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2579 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2580 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2581 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2582 }
2583 }
2584 hton24(hdr->dlength, datain->length);
2585 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2586 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2587 (struct scsi_lun *)&hdr->lun);
2588 else
2589 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2590
2591 hdr->itt = cmd->init_task_tag;
2592
2593 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2594 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2595 else
2596 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2597 if (set_statsn)
2598 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2599 else
2600 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2601
2602 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2603 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2604 hdr->datasn = cpu_to_be32(datain->data_sn);
2605 hdr->offset = cpu_to_be32(datain->offset);
2606
2607 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2608 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2609 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2610 ntohl(hdr->offset), datain->length, conn->cid);
2611}
2612
2613static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2614{
2615 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002616 struct iscsi_datain datain;
2617 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002618 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002619 u32 iov_count = 0, tx_size = 0;
2620 int eodr = 0, ret, iov_ret;
2621 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002622
2623 memset(&datain, 0, sizeof(struct iscsi_datain));
2624 dr = iscsit_get_datain_values(cmd, &datain);
2625 if (!dr) {
2626 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2627 cmd->init_task_tag);
2628 return -1;
2629 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002630 /*
2631 * Be paranoid and double check the logic for now.
2632 */
Andy Groverebf1d952012-04-03 15:51:24 -07002633 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002634 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2635 " datain.length: %u exceeds cmd->data_length: %u\n",
2636 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002637 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002638 return -1;
2639 }
2640
2641 spin_lock_bh(&conn->sess->session_stats_lock);
2642 conn->sess->tx_data_octets += datain.length;
2643 if (conn->sess->se_sess->se_node_acl) {
2644 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2645 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2646 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2647 }
2648 spin_unlock_bh(&conn->sess->session_stats_lock);
2649 /*
2650 * Special case for successfully execution w/ both DATAIN
2651 * and Sense Data.
2652 */
2653 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2654 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2655 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2656 else {
2657 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2658 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2659 iscsit_increment_maxcmdsn(cmd, conn->sess);
2660 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002661 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002662 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002663 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2664 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002665 }
2666
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002667 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002668
2669 iov = &cmd->iov_data[0];
2670 iov[iov_count].iov_base = cmd->pdu;
2671 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2672 tx_size += ISCSI_HDR_LEN;
2673
2674 if (conn->conn_ops->HeaderDigest) {
2675 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2676
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002677 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2678 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002679
2680 iov[0].iov_len += ISCSI_CRC_LEN;
2681 tx_size += ISCSI_CRC_LEN;
2682
2683 pr_debug("Attaching CRC32 HeaderDigest"
2684 " for DataIN PDU 0x%08x\n", *header_digest);
2685 }
2686
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002687 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2688 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002689 if (iov_ret < 0)
2690 return -1;
2691
2692 iov_count += iov_ret;
2693 tx_size += datain.length;
2694
2695 cmd->padding = ((-datain.length) & 3);
2696 if (cmd->padding) {
2697 iov[iov_count].iov_base = cmd->pad_bytes;
2698 iov[iov_count++].iov_len = cmd->padding;
2699 tx_size += cmd->padding;
2700
2701 pr_debug("Attaching %u padding bytes\n",
2702 cmd->padding);
2703 }
2704 if (conn->conn_ops->DataDigest) {
2705 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2706 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2707
2708 iov[iov_count].iov_base = &cmd->data_crc;
2709 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2710 tx_size += ISCSI_CRC_LEN;
2711
2712 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2713 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2714 }
2715
2716 cmd->iov_data_count = iov_count;
2717 cmd->tx_size = tx_size;
2718
Andy Grover6f3c0e62012-04-03 15:51:09 -07002719 /* sendpage is preferred but can't insert markers */
2720 if (!conn->conn_ops->IFMarker)
2721 ret = iscsit_fe_sendpage_sg(cmd, conn);
2722 else
2723 ret = iscsit_send_tx_data(cmd, conn, 0);
2724
2725 iscsit_unmap_iovec(cmd);
2726
2727 if (ret < 0) {
2728 iscsit_tx_thread_wait_for_tcp(conn);
2729 return ret;
2730 }
2731
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002732 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002733 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002734 2 : 1;
2735 iscsit_free_datain_req(cmd, dr);
2736 }
2737
Andy Grover6f3c0e62012-04-03 15:51:09 -07002738 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002739}
2740
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002741int
2742iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2743 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002744{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002745 struct iscsi_conn *logout_conn = NULL;
2746 struct iscsi_conn_recovery *cr = NULL;
2747 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002748 /*
2749 * The actual shutting down of Sessions and/or Connections
2750 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2751 * is done in scsi_logout_post_handler().
2752 */
2753 switch (cmd->logout_reason) {
2754 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2755 pr_debug("iSCSI session logout successful, setting"
2756 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2757 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2758 break;
2759 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2760 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2761 break;
2762 /*
2763 * For CLOSECONNECTION logout requests carrying
2764 * a matching logout CID -> local CID, the reference
2765 * for the local CID will have been incremented in
2766 * iscsi_logout_closeconnection().
2767 *
2768 * For CLOSECONNECTION logout requests carrying
2769 * a different CID than the connection it arrived
2770 * on, the connection responding to cmd->logout_cid
2771 * is stopped in iscsit_logout_post_handler_diffcid().
2772 */
2773
2774 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2775 " successful.\n", cmd->logout_cid, conn->cid);
2776 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2777 break;
2778 case ISCSI_LOGOUT_REASON_RECOVERY:
2779 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2780 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2781 break;
2782 /*
2783 * If the connection is still active from our point of view
2784 * force connection recovery to occur.
2785 */
2786 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2787 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002788 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002789 iscsit_connection_reinstatement_rcfr(logout_conn);
2790 iscsit_dec_conn_usage_count(logout_conn);
2791 }
2792
2793 cr = iscsit_get_inactive_connection_recovery_entry(
2794 conn->sess, cmd->logout_cid);
2795 if (!cr) {
2796 pr_err("Unable to locate CID: %hu for"
2797 " REMOVECONNFORRECOVERY Logout Request.\n",
2798 cmd->logout_cid);
2799 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2800 break;
2801 }
2802
2803 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2804
2805 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2806 " for recovery for CID: %hu on CID: %hu successful.\n",
2807 cmd->logout_cid, conn->cid);
2808 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2809 break;
2810 default:
2811 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2812 cmd->logout_reason);
2813 return -1;
2814 }
2815
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002816 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2817 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2818 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002819 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002820 cmd->stat_sn = conn->stat_sn++;
2821 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2822
2823 iscsit_increment_maxcmdsn(cmd, conn->sess);
2824 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2825 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2826
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002827 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2828 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2829 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2830 cmd->logout_cid, conn->cid);
2831
2832 return 0;
2833}
2834EXPORT_SYMBOL(iscsit_build_logout_rsp);
2835
2836static int
2837iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2838{
2839 struct kvec *iov;
2840 int niov = 0, tx_size, rc;
2841
2842 rc = iscsit_build_logout_rsp(cmd, conn,
2843 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2844 if (rc < 0)
2845 return rc;
2846
2847 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002848 iov = &cmd->iov_misc[0];
2849 iov[niov].iov_base = cmd->pdu;
2850 iov[niov++].iov_len = ISCSI_HDR_LEN;
2851
2852 if (conn->conn_ops->HeaderDigest) {
2853 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2854
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002855 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2856 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002857
2858 iov[0].iov_len += ISCSI_CRC_LEN;
2859 tx_size += ISCSI_CRC_LEN;
2860 pr_debug("Attaching CRC32C HeaderDigest to"
2861 " Logout Response 0x%08x\n", *header_digest);
2862 }
2863 cmd->iov_misc_count = niov;
2864 cmd->tx_size = tx_size;
2865
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002866 return 0;
2867}
2868
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002869void
2870iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2871 struct iscsi_nopin *hdr, bool nopout_response)
2872{
2873 hdr->opcode = ISCSI_OP_NOOP_IN;
2874 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2875 hton24(hdr->dlength, cmd->buf_ptr_size);
2876 if (nopout_response)
2877 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2878 hdr->itt = cmd->init_task_tag;
2879 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2880 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2881 conn->stat_sn;
2882 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2883
2884 if (nopout_response)
2885 iscsit_increment_maxcmdsn(cmd, conn->sess);
2886
2887 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2888 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2889
2890 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2891 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2892 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2893 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2894}
2895EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2896
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002897/*
2898 * Unsolicited NOPIN, either requesting a response or not.
2899 */
2900static int iscsit_send_unsolicited_nopin(
2901 struct iscsi_cmd *cmd,
2902 struct iscsi_conn *conn,
2903 int want_response)
2904{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002905 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2906 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002907
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002908 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002909
2910 if (conn->conn_ops->HeaderDigest) {
2911 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2912
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002913 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2914 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002915
2916 tx_size += ISCSI_CRC_LEN;
2917 pr_debug("Attaching CRC32C HeaderDigest to"
2918 " NopIN 0x%08x\n", *header_digest);
2919 }
2920
2921 cmd->iov_misc[0].iov_base = cmd->pdu;
2922 cmd->iov_misc[0].iov_len = tx_size;
2923 cmd->iov_misc_count = 1;
2924 cmd->tx_size = tx_size;
2925
2926 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2927 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2928
Andy Grover6f3c0e62012-04-03 15:51:09 -07002929 ret = iscsit_send_tx_data(cmd, conn, 1);
2930 if (ret < 0) {
2931 iscsit_tx_thread_wait_for_tcp(conn);
2932 return ret;
2933 }
2934
2935 spin_lock_bh(&cmd->istate_lock);
2936 cmd->i_state = want_response ?
2937 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2938 spin_unlock_bh(&cmd->istate_lock);
2939
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002940 return 0;
2941}
2942
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002943static int
2944iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002945{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002946 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002947 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002948 u32 padding = 0;
2949 int niov = 0, tx_size;
2950
2951 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002952
2953 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002954 iov = &cmd->iov_misc[0];
2955 iov[niov].iov_base = cmd->pdu;
2956 iov[niov++].iov_len = ISCSI_HDR_LEN;
2957
2958 if (conn->conn_ops->HeaderDigest) {
2959 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2960
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002961 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2962 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002963
2964 iov[0].iov_len += ISCSI_CRC_LEN;
2965 tx_size += ISCSI_CRC_LEN;
2966 pr_debug("Attaching CRC32C HeaderDigest"
2967 " to NopIn 0x%08x\n", *header_digest);
2968 }
2969
2970 /*
2971 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2972 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2973 */
2974 if (cmd->buf_ptr_size) {
2975 iov[niov].iov_base = cmd->buf_ptr;
2976 iov[niov++].iov_len = cmd->buf_ptr_size;
2977 tx_size += cmd->buf_ptr_size;
2978
2979 pr_debug("Echoing back %u bytes of ping"
2980 " data.\n", cmd->buf_ptr_size);
2981
2982 padding = ((-cmd->buf_ptr_size) & 3);
2983 if (padding != 0) {
2984 iov[niov].iov_base = &cmd->pad_bytes;
2985 iov[niov++].iov_len = padding;
2986 tx_size += padding;
2987 pr_debug("Attaching %u additional"
2988 " padding bytes.\n", padding);
2989 }
2990 if (conn->conn_ops->DataDigest) {
2991 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2992 cmd->buf_ptr, cmd->buf_ptr_size,
2993 padding, (u8 *)&cmd->pad_bytes,
2994 (u8 *)&cmd->data_crc);
2995
2996 iov[niov].iov_base = &cmd->data_crc;
2997 iov[niov++].iov_len = ISCSI_CRC_LEN;
2998 tx_size += ISCSI_CRC_LEN;
2999 pr_debug("Attached DataDigest for %u"
3000 " bytes of ping data, CRC 0x%08x\n",
3001 cmd->buf_ptr_size, cmd->data_crc);
3002 }
3003 }
3004
3005 cmd->iov_misc_count = niov;
3006 cmd->tx_size = tx_size;
3007
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003008 return 0;
3009}
3010
Andy Grover6f3c0e62012-04-03 15:51:09 -07003011static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003012 struct iscsi_cmd *cmd,
3013 struct iscsi_conn *conn)
3014{
3015 int tx_size = 0;
3016 struct iscsi_r2t *r2t;
3017 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003018 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003019
3020 r2t = iscsit_get_r2t_from_list(cmd);
3021 if (!r2t)
3022 return -1;
3023
3024 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3025 memset(hdr, 0, ISCSI_HDR_LEN);
3026 hdr->opcode = ISCSI_OP_R2T;
3027 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3028 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3029 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003030 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003031 spin_lock_bh(&conn->sess->ttt_lock);
3032 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3033 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3034 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3035 spin_unlock_bh(&conn->sess->ttt_lock);
3036 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3037 hdr->statsn = cpu_to_be32(conn->stat_sn);
3038 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3039 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3040 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3041 hdr->data_offset = cpu_to_be32(r2t->offset);
3042 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3043
3044 cmd->iov_misc[0].iov_base = cmd->pdu;
3045 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3046 tx_size += ISCSI_HDR_LEN;
3047
3048 if (conn->conn_ops->HeaderDigest) {
3049 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3050
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003051 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3052 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003053
3054 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3055 tx_size += ISCSI_CRC_LEN;
3056 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3057 " PDU 0x%08x\n", *header_digest);
3058 }
3059
3060 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3061 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3062 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3063 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3064 r2t->offset, r2t->xfer_len, conn->cid);
3065
3066 cmd->iov_misc_count = 1;
3067 cmd->tx_size = tx_size;
3068
3069 spin_lock_bh(&cmd->r2t_lock);
3070 r2t->sent_r2t = 1;
3071 spin_unlock_bh(&cmd->r2t_lock);
3072
Andy Grover6f3c0e62012-04-03 15:51:09 -07003073 ret = iscsit_send_tx_data(cmd, conn, 1);
3074 if (ret < 0) {
3075 iscsit_tx_thread_wait_for_tcp(conn);
3076 return ret;
3077 }
3078
3079 spin_lock_bh(&cmd->dataout_timeout_lock);
3080 iscsit_start_dataout_timer(cmd, conn);
3081 spin_unlock_bh(&cmd->dataout_timeout_lock);
3082
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003083 return 0;
3084}
3085
3086/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003087 * @recovery: If called from iscsi_task_reassign_complete_write() for
3088 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003089 */
3090int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003091 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003092 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003093 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003094{
3095 int first_r2t = 1;
3096 u32 offset = 0, xfer_len = 0;
3097
3098 spin_lock_bh(&cmd->r2t_lock);
3099 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3100 spin_unlock_bh(&cmd->r2t_lock);
3101 return 0;
3102 }
3103
Andy Grover8b1e1242012-04-03 15:51:12 -07003104 if (conn->sess->sess_ops->DataSequenceInOrder &&
3105 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003106 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003107
3108 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3109 if (conn->sess->sess_ops->DataSequenceInOrder) {
3110 offset = cmd->r2t_offset;
3111
Andy Grover8b1e1242012-04-03 15:51:12 -07003112 if (first_r2t && recovery) {
3113 int new_data_end = offset +
3114 conn->sess->sess_ops->MaxBurstLength -
3115 cmd->next_burst_len;
3116
Andy Groverebf1d952012-04-03 15:51:24 -07003117 if (new_data_end > cmd->se_cmd.data_length)
3118 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003119 else
3120 xfer_len =
3121 conn->sess->sess_ops->MaxBurstLength -
3122 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003123 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003124 int new_data_end = offset +
3125 conn->sess->sess_ops->MaxBurstLength;
3126
Andy Groverebf1d952012-04-03 15:51:24 -07003127 if (new_data_end > cmd->se_cmd.data_length)
3128 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003129 else
3130 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003131 }
3132 cmd->r2t_offset += xfer_len;
3133
Andy Groverebf1d952012-04-03 15:51:24 -07003134 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003135 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3136 } else {
3137 struct iscsi_seq *seq;
3138
3139 seq = iscsit_get_seq_holder_for_r2t(cmd);
3140 if (!seq) {
3141 spin_unlock_bh(&cmd->r2t_lock);
3142 return -1;
3143 }
3144
3145 offset = seq->offset;
3146 xfer_len = seq->xfer_len;
3147
3148 if (cmd->seq_send_order == cmd->seq_count)
3149 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3150 }
3151 cmd->outstanding_r2ts++;
3152 first_r2t = 0;
3153
3154 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3155 spin_unlock_bh(&cmd->r2t_lock);
3156 return -1;
3157 }
3158
3159 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3160 break;
3161 }
3162 spin_unlock_bh(&cmd->r2t_lock);
3163
3164 return 0;
3165}
3166
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003167void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3168 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003169{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003170 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003171 cmd->stat_sn = conn->stat_sn++;
3172
3173 spin_lock_bh(&conn->sess->session_stats_lock);
3174 conn->sess->rsp_pdus++;
3175 spin_unlock_bh(&conn->sess->session_stats_lock);
3176
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003177 memset(hdr, 0, ISCSI_HDR_LEN);
3178 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3179 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3180 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3181 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003182 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003183 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3184 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003185 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003186 }
3187 hdr->response = cmd->iscsi_response;
3188 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003189 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003190 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3191
3192 iscsit_increment_maxcmdsn(cmd, conn->sess);
3193 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3194 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3195
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003196 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3197 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3198 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3199 cmd->se_cmd.scsi_status, conn->cid);
3200}
3201EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3202
3203static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3204{
3205 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3206 struct kvec *iov;
3207 u32 padding = 0, tx_size = 0;
3208 int iov_count = 0;
3209 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3210
3211 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3212
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003213 iov = &cmd->iov_misc[0];
3214 iov[iov_count].iov_base = cmd->pdu;
3215 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3216 tx_size += ISCSI_HDR_LEN;
3217
3218 /*
3219 * Attach SENSE DATA payload to iSCSI Response PDU
3220 */
3221 if (cmd->se_cmd.sense_buffer &&
3222 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3223 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003224 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3225 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3226
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003227 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003228 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003229 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003230 iov[iov_count++].iov_len =
3231 (cmd->se_cmd.scsi_sense_length + padding);
3232 tx_size += cmd->se_cmd.scsi_sense_length;
3233
3234 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003235 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003236 cmd->se_cmd.scsi_sense_length, 0, padding);
3237 tx_size += padding;
3238 pr_debug("Adding %u bytes of padding to"
3239 " SENSE.\n", padding);
3240 }
3241
3242 if (conn->conn_ops->DataDigest) {
3243 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003244 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003245 (cmd->se_cmd.scsi_sense_length + padding),
3246 0, NULL, (u8 *)&cmd->data_crc);
3247
3248 iov[iov_count].iov_base = &cmd->data_crc;
3249 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3250 tx_size += ISCSI_CRC_LEN;
3251
3252 pr_debug("Attaching CRC32 DataDigest for"
3253 " SENSE, %u bytes CRC 0x%08x\n",
3254 (cmd->se_cmd.scsi_sense_length + padding),
3255 cmd->data_crc);
3256 }
3257
3258 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3259 " Response PDU\n",
3260 cmd->se_cmd.scsi_sense_length);
3261 }
3262
3263 if (conn->conn_ops->HeaderDigest) {
3264 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3265
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003266 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3267 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003268
3269 iov[0].iov_len += ISCSI_CRC_LEN;
3270 tx_size += ISCSI_CRC_LEN;
3271 pr_debug("Attaching CRC32 HeaderDigest for Response"
3272 " PDU 0x%08x\n", *header_digest);
3273 }
3274
3275 cmd->iov_misc_count = iov_count;
3276 cmd->tx_size = tx_size;
3277
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003278 return 0;
3279}
3280
3281static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3282{
3283 switch (se_tmr->response) {
3284 case TMR_FUNCTION_COMPLETE:
3285 return ISCSI_TMF_RSP_COMPLETE;
3286 case TMR_TASK_DOES_NOT_EXIST:
3287 return ISCSI_TMF_RSP_NO_TASK;
3288 case TMR_LUN_DOES_NOT_EXIST:
3289 return ISCSI_TMF_RSP_NO_LUN;
3290 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3291 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003292 case TMR_FUNCTION_REJECTED:
3293 default:
3294 return ISCSI_TMF_RSP_REJECTED;
3295 }
3296}
3297
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003298void
3299iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3300 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003301{
3302 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003303
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003304 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003305 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003306 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003307 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003308 cmd->stat_sn = conn->stat_sn++;
3309 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3310
3311 iscsit_increment_maxcmdsn(cmd, conn->sess);
3312 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3313 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3314
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003315 pr_debug("Built Task Management Response ITT: 0x%08x,"
3316 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3317 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3318}
3319EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3320
3321static int
3322iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3323{
3324 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3325 u32 tx_size = 0;
3326
3327 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3328
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003329 cmd->iov_misc[0].iov_base = cmd->pdu;
3330 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3331 tx_size += ISCSI_HDR_LEN;
3332
3333 if (conn->conn_ops->HeaderDigest) {
3334 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3335
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003336 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3337 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003338
3339 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3340 tx_size += ISCSI_CRC_LEN;
3341 pr_debug("Attaching CRC32 HeaderDigest for Task"
3342 " Mgmt Response PDU 0x%08x\n", *header_digest);
3343 }
3344
3345 cmd->iov_misc_count = 1;
3346 cmd->tx_size = tx_size;
3347
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003348 return 0;
3349}
3350
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003351static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3352{
3353 bool ret = false;
3354
3355 if (np->np_sockaddr.ss_family == AF_INET6) {
3356 const struct sockaddr_in6 sin6 = {
3357 .sin6_addr = IN6ADDR_ANY_INIT };
3358 struct sockaddr_in6 *sock_in6 =
3359 (struct sockaddr_in6 *)&np->np_sockaddr;
3360
3361 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3362 sin6.sin6_addr.s6_addr, 16))
3363 ret = true;
3364 } else {
3365 struct sockaddr_in * sock_in =
3366 (struct sockaddr_in *)&np->np_sockaddr;
3367
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003368 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003369 ret = true;
3370 }
3371
3372 return ret;
3373}
3374
Andy Grover8b1e1242012-04-03 15:51:12 -07003375#define SENDTARGETS_BUF_LIMIT 32768U
3376
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003377static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3378{
3379 char *payload = NULL;
3380 struct iscsi_conn *conn = cmd->conn;
3381 struct iscsi_portal_group *tpg;
3382 struct iscsi_tiqn *tiqn;
3383 struct iscsi_tpg_np *tpg_np;
3384 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003385 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003386 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003387
Andy Grover8b1e1242012-04-03 15:51:12 -07003388 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3389 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003390
3391 payload = kzalloc(buffer_len, GFP_KERNEL);
3392 if (!payload) {
3393 pr_err("Unable to allocate memory for sendtargets"
3394 " response.\n");
3395 return -ENOMEM;
3396 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003397 /*
3398 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3399 * explicit case..
3400 */
3401 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) {
3402 text_ptr = strchr(text_in, '=');
3403 if (!text_ptr) {
3404 pr_err("Unable to locate '=' string in text_in:"
3405 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003406 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003407 return -EINVAL;
3408 }
3409 /*
3410 * Skip over '=' character..
3411 */
3412 text_ptr += 1;
3413 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003414
3415 spin_lock(&tiqn_lock);
3416 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003417 if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) &&
3418 strcmp(tiqn->tiqn, text_ptr)) {
3419 continue;
3420 }
3421
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003422 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3423 len += 1;
3424
3425 if ((len + payload_len) > buffer_len) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003426 end_of_buf = 1;
3427 goto eob;
3428 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003429 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003430 payload_len += len;
3431
3432 spin_lock(&tiqn->tiqn_tpg_lock);
3433 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3434
3435 spin_lock(&tpg->tpg_state_lock);
3436 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3437 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3438 spin_unlock(&tpg->tpg_state_lock);
3439 continue;
3440 }
3441 spin_unlock(&tpg->tpg_state_lock);
3442
3443 spin_lock(&tpg->tpg_np_lock);
3444 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3445 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003446 struct iscsi_np *np = tpg_np->tpg_np;
3447 bool inaddr_any = iscsit_check_inaddr_any(np);
3448
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003449 len = sprintf(buf, "TargetAddress="
3450 "%s%s%s:%hu,%hu",
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003451 (np->np_sockaddr.ss_family == AF_INET6) ?
3452 "[" : "", (inaddr_any == false) ?
3453 np->np_ip : conn->local_ip,
3454 (np->np_sockaddr.ss_family == AF_INET6) ?
3455 "]" : "", (inaddr_any == false) ?
3456 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003457 tpg->tpgt);
3458 len += 1;
3459
3460 if ((len + payload_len) > buffer_len) {
3461 spin_unlock(&tpg->tpg_np_lock);
3462 spin_unlock(&tiqn->tiqn_tpg_lock);
3463 end_of_buf = 1;
3464 goto eob;
3465 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003466 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003467 payload_len += len;
3468 }
3469 spin_unlock(&tpg->tpg_np_lock);
3470 }
3471 spin_unlock(&tiqn->tiqn_tpg_lock);
3472eob:
3473 if (end_of_buf)
3474 break;
Nicholas Bellinger66658892013-06-19 22:45:42 -07003475
3476 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE)
3477 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003478 }
3479 spin_unlock(&tiqn_lock);
3480
3481 cmd->buf_ptr = payload;
3482
3483 return payload_len;
3484}
3485
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003486int
3487iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3488 struct iscsi_text_rsp *hdr)
3489{
3490 int text_length, padding;
3491
3492 text_length = iscsit_build_sendtargets_response(cmd);
3493 if (text_length < 0)
3494 return text_length;
3495
3496 hdr->opcode = ISCSI_OP_TEXT_RSP;
3497 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3498 padding = ((-text_length) & 3);
3499 hton24(hdr->dlength, text_length);
3500 hdr->itt = cmd->init_task_tag;
3501 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3502 cmd->stat_sn = conn->stat_sn++;
3503 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3504
3505 iscsit_increment_maxcmdsn(cmd, conn->sess);
3506 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3507 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3508
3509 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3510 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3511 text_length, conn->cid);
3512
3513 return text_length + padding;
3514}
3515EXPORT_SYMBOL(iscsit_build_text_rsp);
3516
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003517/*
3518 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3519 * MaxRecvDataSegmentLength.
3520 */
3521static int iscsit_send_text_rsp(
3522 struct iscsi_cmd *cmd,
3523 struct iscsi_conn *conn)
3524{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003525 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003526 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003527 u32 tx_size = 0;
3528 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003529
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003530 rc = iscsit_build_text_rsp(cmd, conn, hdr);
3531 if (rc < 0)
3532 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003533
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003534 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003535 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003536 iov[iov_count].iov_base = cmd->pdu;
3537 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3538 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003539 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003540
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003541 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003542
3543 if (conn->conn_ops->HeaderDigest) {
3544 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3545
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003546 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3547 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003548
3549 iov[0].iov_len += ISCSI_CRC_LEN;
3550 tx_size += ISCSI_CRC_LEN;
3551 pr_debug("Attaching CRC32 HeaderDigest for"
3552 " Text Response PDU 0x%08x\n", *header_digest);
3553 }
3554
3555 if (conn->conn_ops->DataDigest) {
3556 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003557 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003558 0, NULL, (u8 *)&cmd->data_crc);
3559
3560 iov[iov_count].iov_base = &cmd->data_crc;
3561 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3562 tx_size += ISCSI_CRC_LEN;
3563
3564 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003565 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003566 cmd->data_crc);
3567 }
3568
3569 cmd->iov_misc_count = iov_count;
3570 cmd->tx_size = tx_size;
3571
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003572 return 0;
3573}
3574
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003575void
3576iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3577 struct iscsi_reject *hdr)
3578{
3579 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003580 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003581 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3582 hton24(hdr->dlength, ISCSI_HDR_LEN);
3583 hdr->ffffffff = cpu_to_be32(0xffffffff);
3584 cmd->stat_sn = conn->stat_sn++;
3585 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3586 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3587 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3588
3589}
3590EXPORT_SYMBOL(iscsit_build_reject);
3591
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003592static int iscsit_send_reject(
3593 struct iscsi_cmd *cmd,
3594 struct iscsi_conn *conn)
3595{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003596 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003597 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003598 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003599
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003600 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003601
3602 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003603 iov[iov_count].iov_base = cmd->pdu;
3604 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3605 iov[iov_count].iov_base = cmd->buf_ptr;
3606 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3607
3608 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3609
3610 if (conn->conn_ops->HeaderDigest) {
3611 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3612
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003613 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3614 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003615
3616 iov[0].iov_len += ISCSI_CRC_LEN;
3617 tx_size += ISCSI_CRC_LEN;
3618 pr_debug("Attaching CRC32 HeaderDigest for"
3619 " REJECT PDU 0x%08x\n", *header_digest);
3620 }
3621
3622 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003623 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3624 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003625
3626 iov[iov_count].iov_base = &cmd->data_crc;
3627 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3628 tx_size += ISCSI_CRC_LEN;
3629 pr_debug("Attaching CRC32 DataDigest for REJECT"
3630 " PDU 0x%08x\n", cmd->data_crc);
3631 }
3632
3633 cmd->iov_misc_count = iov_count;
3634 cmd->tx_size = tx_size;
3635
3636 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3637 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3638
3639 return 0;
3640}
3641
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003642void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3643{
3644 struct iscsi_thread_set *ts = conn->thread_set;
3645 int ord, cpu;
3646 /*
3647 * thread_id is assigned from iscsit_global->ts_bitmap from
3648 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3649 *
3650 * Here we use thread_id to determine which CPU that this
3651 * iSCSI connection's iscsi_thread_set will be scheduled to
3652 * execute upon.
3653 */
3654 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003655 for_each_online_cpu(cpu) {
3656 if (ord-- == 0) {
3657 cpumask_set_cpu(cpu, conn->conn_cpumask);
3658 return;
3659 }
3660 }
3661 /*
3662 * This should never be reached..
3663 */
3664 dump_stack();
3665 cpumask_setall(conn->conn_cpumask);
3666}
3667
3668static inline void iscsit_thread_check_cpumask(
3669 struct iscsi_conn *conn,
3670 struct task_struct *p,
3671 int mode)
3672{
3673 char buf[128];
3674 /*
3675 * mode == 1 signals iscsi_target_tx_thread() usage.
3676 * mode == 0 signals iscsi_target_rx_thread() usage.
3677 */
3678 if (mode == 1) {
3679 if (!conn->conn_tx_reset_cpumask)
3680 return;
3681 conn->conn_tx_reset_cpumask = 0;
3682 } else {
3683 if (!conn->conn_rx_reset_cpumask)
3684 return;
3685 conn->conn_rx_reset_cpumask = 0;
3686 }
3687 /*
3688 * Update the CPU mask for this single kthread so that
3689 * both TX and RX kthreads are scheduled to run on the
3690 * same CPU.
3691 */
3692 memset(buf, 0, 128);
3693 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003694 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3695}
3696
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003697static int
3698iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003699{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003700 int ret;
3701
3702 switch (state) {
3703 case ISTATE_SEND_R2T:
3704 ret = iscsit_send_r2t(cmd, conn);
3705 if (ret < 0)
3706 goto err;
3707 break;
3708 case ISTATE_REMOVE:
3709 spin_lock_bh(&conn->cmd_lock);
3710 list_del(&cmd->i_conn_node);
3711 spin_unlock_bh(&conn->cmd_lock);
3712
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003713 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003714 break;
3715 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3716 iscsit_mod_nopin_response_timer(conn);
3717 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3718 if (ret < 0)
3719 goto err;
3720 break;
3721 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3722 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3723 if (ret < 0)
3724 goto err;
3725 break;
3726 default:
3727 pr_err("Unknown Opcode: 0x%02x ITT:"
3728 " 0x%08x, i_state: %d on CID: %hu\n",
3729 cmd->iscsi_opcode, cmd->init_task_tag, state,
3730 conn->cid);
3731 goto err;
3732 }
3733
3734 return 0;
3735
3736err:
3737 return -1;
3738}
3739
3740static int
3741iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3742{
3743 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003744 struct iscsi_queue_req *qr;
3745 struct iscsi_cmd *cmd;
3746 u8 state;
3747 int ret;
3748
3749 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3750 atomic_set(&conn->check_immediate_queue, 0);
3751 cmd = qr->cmd;
3752 state = qr->state;
3753 kmem_cache_free(lio_qr_cache, qr);
3754
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003755 ret = t->iscsit_immediate_queue(conn, cmd, state);
3756 if (ret < 0)
3757 return ret;
3758 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003759
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003760 return 0;
3761}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003762
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003763static int
3764iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3765{
3766 int ret;
3767
3768check_rsp_state:
3769 switch (state) {
3770 case ISTATE_SEND_DATAIN:
3771 ret = iscsit_send_datain(cmd, conn);
3772 if (ret < 0)
3773 goto err;
3774 else if (!ret)
3775 /* more drs */
3776 goto check_rsp_state;
3777 else if (ret == 1) {
3778 /* all done */
3779 spin_lock_bh(&cmd->istate_lock);
3780 cmd->i_state = ISTATE_SENT_STATUS;
3781 spin_unlock_bh(&cmd->istate_lock);
3782
3783 if (atomic_read(&conn->check_immediate_queue))
3784 return 1;
3785
3786 return 0;
3787 } else if (ret == 2) {
3788 /* Still must send status,
3789 SCF_TRANSPORT_TASK_SENSE was set */
3790 spin_lock_bh(&cmd->istate_lock);
3791 cmd->i_state = ISTATE_SEND_STATUS;
3792 spin_unlock_bh(&cmd->istate_lock);
3793 state = ISTATE_SEND_STATUS;
3794 goto check_rsp_state;
3795 }
3796
3797 break;
3798 case ISTATE_SEND_STATUS:
3799 case ISTATE_SEND_STATUS_RECOVERY:
3800 ret = iscsit_send_response(cmd, conn);
3801 break;
3802 case ISTATE_SEND_LOGOUTRSP:
3803 ret = iscsit_send_logout(cmd, conn);
3804 break;
3805 case ISTATE_SEND_ASYNCMSG:
3806 ret = iscsit_send_conn_drop_async_message(
3807 cmd, conn);
3808 break;
3809 case ISTATE_SEND_NOPIN:
3810 ret = iscsit_send_nopin(cmd, conn);
3811 break;
3812 case ISTATE_SEND_REJECT:
3813 ret = iscsit_send_reject(cmd, conn);
3814 break;
3815 case ISTATE_SEND_TASKMGTRSP:
3816 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3817 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003818 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003819 ret = iscsit_tmr_post_handler(cmd, conn);
3820 if (ret != 0)
3821 iscsit_fall_back_to_erl0(conn->sess);
3822 break;
3823 case ISTATE_SEND_TEXTRSP:
3824 ret = iscsit_send_text_rsp(cmd, conn);
3825 break;
3826 default:
3827 pr_err("Unknown Opcode: 0x%02x ITT:"
3828 " 0x%08x, i_state: %d on CID: %hu\n",
3829 cmd->iscsi_opcode, cmd->init_task_tag,
3830 state, conn->cid);
3831 goto err;
3832 }
3833 if (ret < 0)
3834 goto err;
3835
3836 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3837 iscsit_tx_thread_wait_for_tcp(conn);
3838 iscsit_unmap_iovec(cmd);
3839 goto err;
3840 }
3841 iscsit_unmap_iovec(cmd);
3842
3843 switch (state) {
3844 case ISTATE_SEND_LOGOUTRSP:
3845 if (!iscsit_logout_post_handler(cmd, conn))
3846 goto restart;
3847 /* fall through */
3848 case ISTATE_SEND_STATUS:
3849 case ISTATE_SEND_ASYNCMSG:
3850 case ISTATE_SEND_NOPIN:
3851 case ISTATE_SEND_STATUS_RECOVERY:
3852 case ISTATE_SEND_TEXTRSP:
3853 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003854 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003855 spin_lock_bh(&cmd->istate_lock);
3856 cmd->i_state = ISTATE_SENT_STATUS;
3857 spin_unlock_bh(&cmd->istate_lock);
3858 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003859 default:
3860 pr_err("Unknown Opcode: 0x%02x ITT:"
3861 " 0x%08x, i_state: %d on CID: %hu\n",
3862 cmd->iscsi_opcode, cmd->init_task_tag,
3863 cmd->i_state, conn->cid);
3864 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003865 }
3866
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003867 if (atomic_read(&conn->check_immediate_queue))
3868 return 1;
3869
Andy Grover6f3c0e62012-04-03 15:51:09 -07003870 return 0;
3871
3872err:
3873 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003874restart:
3875 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003876}
3877
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003878static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003879{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003880 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003881 struct iscsi_queue_req *qr;
3882 struct iscsi_cmd *cmd;
3883 u8 state;
3884 int ret;
3885
3886 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3887 cmd = qr->cmd;
3888 state = qr->state;
3889 kmem_cache_free(lio_qr_cache, qr);
3890
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003891 ret = t->iscsit_response_queue(conn, cmd, state);
3892 if (ret == 1 || ret < 0)
3893 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003894 }
3895
3896 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003897}
3898
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003899int iscsi_target_tx_thread(void *arg)
3900{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003901 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003902 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003903 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003904 /*
3905 * Allow ourselves to be interrupted by SIGINT so that a
3906 * connection recovery / failure event can be triggered externally.
3907 */
3908 allow_signal(SIGINT);
3909
3910restart:
3911 conn = iscsi_tx_thread_pre_handler(ts);
3912 if (!conn)
3913 goto out;
3914
Andy Grover6f3c0e62012-04-03 15:51:09 -07003915 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003916
3917 while (!kthread_should_stop()) {
3918 /*
3919 * Ensure that both TX and RX per connection kthreads
3920 * are scheduled to run on the same CPU.
3921 */
3922 iscsit_thread_check_cpumask(conn, current, 1);
3923
Roland Dreierd5627ac2012-10-31 09:16:46 -07003924 wait_event_interruptible(conn->queues_wq,
3925 !iscsit_conn_all_queues_empty(conn) ||
3926 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003927
3928 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3929 signal_pending(current))
3930 goto transport_err;
3931
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003932get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003933 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003934 if (ret < 0)
3935 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003936
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003937 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003938 if (ret == 1)
3939 goto get_immediate;
3940 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003941 goto restart;
3942 else if (ret < 0)
3943 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003944 }
3945
3946transport_err:
3947 iscsit_take_action_for_connection_exit(conn);
3948 goto restart;
3949out:
3950 return 0;
3951}
3952
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003953static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3954{
3955 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3956 struct iscsi_cmd *cmd;
3957 int ret = 0;
3958
3959 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3960 case ISCSI_OP_SCSI_CMD:
3961 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3962 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003963 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003964
3965 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3966 break;
3967 case ISCSI_OP_SCSI_DATA_OUT:
3968 ret = iscsit_handle_data_out(conn, buf);
3969 break;
3970 case ISCSI_OP_NOOP_OUT:
3971 cmd = NULL;
3972 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3973 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3974 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003975 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003976 }
3977 ret = iscsit_handle_nop_out(conn, cmd, buf);
3978 break;
3979 case ISCSI_OP_SCSI_TMFUNC:
3980 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3981 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003982 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003983
3984 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3985 break;
3986 case ISCSI_OP_TEXT:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003987 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3988 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003989 goto reject;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003990
3991 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003992 break;
3993 case ISCSI_OP_LOGOUT:
3994 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3995 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003996 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003997
3998 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3999 if (ret > 0)
4000 wait_for_completion_timeout(&conn->conn_logout_comp,
4001 SECONDS_FOR_LOGOUT_COMP * HZ);
4002 break;
4003 case ISCSI_OP_SNACK:
4004 ret = iscsit_handle_snack(conn, buf);
4005 break;
4006 default:
4007 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4008 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4009 pr_err("Cannot recover from unknown"
4010 " opcode while ERL=0, closing iSCSI connection.\n");
4011 return -1;
4012 }
4013 if (!conn->conn_ops->OFMarker) {
4014 pr_err("Unable to recover from unknown"
4015 " opcode while OFMarker=No, closing iSCSI"
4016 " connection.\n");
4017 return -1;
4018 }
4019 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4020 pr_err("Unable to recover from unknown"
4021 " opcode, closing iSCSI connection.\n");
4022 return -1;
4023 }
4024 break;
4025 }
4026
4027 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004028reject:
4029 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004030}
4031
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004032int iscsi_target_rx_thread(void *arg)
4033{
4034 int ret;
4035 u8 buffer[ISCSI_HDR_LEN], opcode;
4036 u32 checksum = 0, digest = 0;
4037 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004038 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004039 struct kvec iov;
4040 /*
4041 * Allow ourselves to be interrupted by SIGINT so that a
4042 * connection recovery / failure event can be triggered externally.
4043 */
4044 allow_signal(SIGINT);
4045
4046restart:
4047 conn = iscsi_rx_thread_pre_handler(ts);
4048 if (!conn)
4049 goto out;
4050
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004051 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4052 struct completion comp;
4053 int rc;
4054
4055 init_completion(&comp);
4056 rc = wait_for_completion_interruptible(&comp);
4057 if (rc < 0)
4058 goto transport_err;
4059
4060 goto out;
4061 }
4062
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004063 while (!kthread_should_stop()) {
4064 /*
4065 * Ensure that both TX and RX per connection kthreads
4066 * are scheduled to run on the same CPU.
4067 */
4068 iscsit_thread_check_cpumask(conn, current, 0);
4069
4070 memset(buffer, 0, ISCSI_HDR_LEN);
4071 memset(&iov, 0, sizeof(struct kvec));
4072
4073 iov.iov_base = buffer;
4074 iov.iov_len = ISCSI_HDR_LEN;
4075
4076 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4077 if (ret != ISCSI_HDR_LEN) {
4078 iscsit_rx_thread_wait_for_tcp(conn);
4079 goto transport_err;
4080 }
4081
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004082 if (conn->conn_ops->HeaderDigest) {
4083 iov.iov_base = &digest;
4084 iov.iov_len = ISCSI_CRC_LEN;
4085
4086 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4087 if (ret != ISCSI_CRC_LEN) {
4088 iscsit_rx_thread_wait_for_tcp(conn);
4089 goto transport_err;
4090 }
4091
4092 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4093 buffer, ISCSI_HDR_LEN,
4094 0, NULL, (u8 *)&checksum);
4095
4096 if (digest != checksum) {
4097 pr_err("HeaderDigest CRC32C failed,"
4098 " received 0x%08x, computed 0x%08x\n",
4099 digest, checksum);
4100 /*
4101 * Set the PDU to 0xff so it will intentionally
4102 * hit default in the switch below.
4103 */
4104 memset(buffer, 0xff, ISCSI_HDR_LEN);
4105 spin_lock_bh(&conn->sess->session_stats_lock);
4106 conn->sess->conn_digest_errors++;
4107 spin_unlock_bh(&conn->sess->session_stats_lock);
4108 } else {
4109 pr_debug("Got HeaderDigest CRC32C"
4110 " 0x%08x\n", checksum);
4111 }
4112 }
4113
4114 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4115 goto transport_err;
4116
4117 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4118
4119 if (conn->sess->sess_ops->SessionType &&
4120 ((!(opcode & ISCSI_OP_TEXT)) ||
4121 (!(opcode & ISCSI_OP_LOGOUT)))) {
4122 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4123 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004124 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4125 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004126 goto transport_err;
4127 }
4128
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004129 ret = iscsi_target_rx_opcode(conn, buffer);
4130 if (ret < 0)
4131 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004132 }
4133
4134transport_err:
4135 if (!signal_pending(current))
4136 atomic_set(&conn->transport_failed, 1);
4137 iscsit_take_action_for_connection_exit(conn);
4138 goto restart;
4139out:
4140 return 0;
4141}
4142
4143static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4144{
4145 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4146 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004147 /*
4148 * We expect this function to only ever be called from either RX or TX
4149 * thread context via iscsit_close_connection() once the other context
4150 * has been reset -> returned sleeping pre-handler state.
4151 */
4152 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004153 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004154
Andy Grover2fbb4712012-04-03 15:51:01 -07004155 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004156 spin_unlock_bh(&conn->cmd_lock);
4157
4158 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004159
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004160 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004161
4162 spin_lock_bh(&conn->cmd_lock);
4163 }
4164 spin_unlock_bh(&conn->cmd_lock);
4165}
4166
4167static void iscsit_stop_timers_for_cmds(
4168 struct iscsi_conn *conn)
4169{
4170 struct iscsi_cmd *cmd;
4171
4172 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004173 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004174 if (cmd->data_direction == DMA_TO_DEVICE)
4175 iscsit_stop_dataout_timer(cmd);
4176 }
4177 spin_unlock_bh(&conn->cmd_lock);
4178}
4179
4180int iscsit_close_connection(
4181 struct iscsi_conn *conn)
4182{
4183 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4184 struct iscsi_session *sess = conn->sess;
4185
4186 pr_debug("Closing iSCSI connection CID %hu on SID:"
4187 " %u\n", conn->cid, sess->sid);
4188 /*
4189 * Always up conn_logout_comp just in case the RX Thread is sleeping
4190 * and the logout response never got sent because the connection
4191 * failed.
4192 */
4193 complete(&conn->conn_logout_comp);
4194
4195 iscsi_release_thread_set(conn);
4196
4197 iscsit_stop_timers_for_cmds(conn);
4198 iscsit_stop_nopin_response_timer(conn);
4199 iscsit_stop_nopin_timer(conn);
4200 iscsit_free_queue_reqs_for_conn(conn);
4201
4202 /*
4203 * During Connection recovery drop unacknowledged out of order
4204 * commands for this connection, and prepare the other commands
4205 * for realligence.
4206 *
4207 * During normal operation clear the out of order commands (but
4208 * do not free the struct iscsi_ooo_cmdsn's) and release all
4209 * struct iscsi_cmds.
4210 */
4211 if (atomic_read(&conn->connection_recovery)) {
4212 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4213 iscsit_prepare_cmds_for_realligance(conn);
4214 } else {
4215 iscsit_clear_ooo_cmdsns_for_conn(conn);
4216 iscsit_release_commands_from_conn(conn);
4217 }
4218
4219 /*
4220 * Handle decrementing session or connection usage count if
4221 * a logout response was not able to be sent because the
4222 * connection failed. Fall back to Session Recovery here.
4223 */
4224 if (atomic_read(&conn->conn_logout_remove)) {
4225 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4226 iscsit_dec_conn_usage_count(conn);
4227 iscsit_dec_session_usage_count(sess);
4228 }
4229 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4230 iscsit_dec_conn_usage_count(conn);
4231
4232 atomic_set(&conn->conn_logout_remove, 0);
4233 atomic_set(&sess->session_reinstatement, 0);
4234 atomic_set(&sess->session_fall_back_to_erl0, 1);
4235 }
4236
4237 spin_lock_bh(&sess->conn_lock);
4238 list_del(&conn->conn_list);
4239
4240 /*
4241 * Attempt to let the Initiator know this connection failed by
4242 * sending an Connection Dropped Async Message on another
4243 * active connection.
4244 */
4245 if (atomic_read(&conn->connection_recovery))
4246 iscsit_build_conn_drop_async_message(conn);
4247
4248 spin_unlock_bh(&sess->conn_lock);
4249
4250 /*
4251 * If connection reinstatement is being performed on this connection,
4252 * up the connection reinstatement semaphore that is being blocked on
4253 * in iscsit_cause_connection_reinstatement().
4254 */
4255 spin_lock_bh(&conn->state_lock);
4256 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4257 spin_unlock_bh(&conn->state_lock);
4258 complete(&conn->conn_wait_comp);
4259 wait_for_completion(&conn->conn_post_wait_comp);
4260 spin_lock_bh(&conn->state_lock);
4261 }
4262
4263 /*
4264 * If connection reinstatement is being performed on this connection
4265 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4266 * connection wait rcfr semaphore that is being blocked on
4267 * an iscsit_connection_reinstatement_rcfr().
4268 */
4269 if (atomic_read(&conn->connection_wait_rcfr)) {
4270 spin_unlock_bh(&conn->state_lock);
4271 complete(&conn->conn_wait_rcfr_comp);
4272 wait_for_completion(&conn->conn_post_wait_comp);
4273 spin_lock_bh(&conn->state_lock);
4274 }
4275 atomic_set(&conn->connection_reinstatement, 1);
4276 spin_unlock_bh(&conn->state_lock);
4277
4278 /*
4279 * If any other processes are accessing this connection pointer we
4280 * must wait until they have completed.
4281 */
4282 iscsit_check_conn_usage_count(conn);
4283
4284 if (conn->conn_rx_hash.tfm)
4285 crypto_free_hash(conn->conn_rx_hash.tfm);
4286 if (conn->conn_tx_hash.tfm)
4287 crypto_free_hash(conn->conn_tx_hash.tfm);
4288
4289 if (conn->conn_cpumask)
4290 free_cpumask_var(conn->conn_cpumask);
4291
4292 kfree(conn->conn_ops);
4293 conn->conn_ops = NULL;
4294
Al Virobf6932f2012-07-21 08:55:18 +01004295 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004296 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004297
4298 if (conn->conn_transport->iscsit_free_conn)
4299 conn->conn_transport->iscsit_free_conn(conn);
4300
4301 iscsit_put_transport(conn->conn_transport);
4302
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004303 conn->thread_set = NULL;
4304
4305 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4306 conn->conn_state = TARG_CONN_STATE_FREE;
4307 kfree(conn);
4308
4309 spin_lock_bh(&sess->conn_lock);
4310 atomic_dec(&sess->nconn);
4311 pr_debug("Decremented iSCSI connection count to %hu from node:"
4312 " %s\n", atomic_read(&sess->nconn),
4313 sess->sess_ops->InitiatorName);
4314 /*
4315 * Make sure that if one connection fails in an non ERL=2 iSCSI
4316 * Session that they all fail.
4317 */
4318 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4319 !atomic_read(&sess->session_logout))
4320 atomic_set(&sess->session_fall_back_to_erl0, 1);
4321
4322 /*
4323 * If this was not the last connection in the session, and we are
4324 * performing session reinstatement or falling back to ERL=0, call
4325 * iscsit_stop_session() without sleeping to shutdown the other
4326 * active connections.
4327 */
4328 if (atomic_read(&sess->nconn)) {
4329 if (!atomic_read(&sess->session_reinstatement) &&
4330 !atomic_read(&sess->session_fall_back_to_erl0)) {
4331 spin_unlock_bh(&sess->conn_lock);
4332 return 0;
4333 }
4334 if (!atomic_read(&sess->session_stop_active)) {
4335 atomic_set(&sess->session_stop_active, 1);
4336 spin_unlock_bh(&sess->conn_lock);
4337 iscsit_stop_session(sess, 0, 0);
4338 return 0;
4339 }
4340 spin_unlock_bh(&sess->conn_lock);
4341 return 0;
4342 }
4343
4344 /*
4345 * If this was the last connection in the session and one of the
4346 * following is occurring:
4347 *
4348 * Session Reinstatement is not being performed, and are falling back
4349 * to ERL=0 call iscsit_close_session().
4350 *
4351 * Session Logout was requested. iscsit_close_session() will be called
4352 * elsewhere.
4353 *
4354 * Session Continuation is not being performed, start the Time2Retain
4355 * handler and check if sleep_on_sess_wait_sem is active.
4356 */
4357 if (!atomic_read(&sess->session_reinstatement) &&
4358 atomic_read(&sess->session_fall_back_to_erl0)) {
4359 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004360 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004361
4362 return 0;
4363 } else if (atomic_read(&sess->session_logout)) {
4364 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4365 sess->session_state = TARG_SESS_STATE_FREE;
4366 spin_unlock_bh(&sess->conn_lock);
4367
4368 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4369 complete(&sess->session_wait_comp);
4370
4371 return 0;
4372 } else {
4373 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4374 sess->session_state = TARG_SESS_STATE_FAILED;
4375
4376 if (!atomic_read(&sess->session_continuation)) {
4377 spin_unlock_bh(&sess->conn_lock);
4378 iscsit_start_time2retain_handler(sess);
4379 } else
4380 spin_unlock_bh(&sess->conn_lock);
4381
4382 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4383 complete(&sess->session_wait_comp);
4384
4385 return 0;
4386 }
4387 spin_unlock_bh(&sess->conn_lock);
4388
4389 return 0;
4390}
4391
4392int iscsit_close_session(struct iscsi_session *sess)
4393{
4394 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4395 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4396
4397 if (atomic_read(&sess->nconn)) {
4398 pr_err("%d connection(s) still exist for iSCSI session"
4399 " to %s\n", atomic_read(&sess->nconn),
4400 sess->sess_ops->InitiatorName);
4401 BUG();
4402 }
4403
4404 spin_lock_bh(&se_tpg->session_lock);
4405 atomic_set(&sess->session_logout, 1);
4406 atomic_set(&sess->session_reinstatement, 1);
4407 iscsit_stop_time2retain_timer(sess);
4408 spin_unlock_bh(&se_tpg->session_lock);
4409
4410 /*
4411 * transport_deregister_session_configfs() will clear the
4412 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4413 * can be setting it again with __transport_register_session() in
4414 * iscsi_post_login_handler() again after the iscsit_stop_session()
4415 * completes in iscsi_np context.
4416 */
4417 transport_deregister_session_configfs(sess->se_sess);
4418
4419 /*
4420 * If any other processes are accessing this session pointer we must
4421 * wait until they have completed. If we are in an interrupt (the
4422 * time2retain handler) and contain and active session usage count we
4423 * restart the timer and exit.
4424 */
4425 if (!in_interrupt()) {
4426 if (iscsit_check_session_usage_count(sess) == 1)
4427 iscsit_stop_session(sess, 1, 1);
4428 } else {
4429 if (iscsit_check_session_usage_count(sess) == 2) {
4430 atomic_set(&sess->session_logout, 0);
4431 iscsit_start_time2retain_handler(sess);
4432 return 0;
4433 }
4434 }
4435
4436 transport_deregister_session(sess->se_sess);
4437
4438 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4439 iscsit_free_connection_recovery_entires(sess);
4440
4441 iscsit_free_all_ooo_cmdsns(sess);
4442
4443 spin_lock_bh(&se_tpg->session_lock);
4444 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4445 sess->session_state = TARG_SESS_STATE_FREE;
4446 pr_debug("Released iSCSI session from node: %s\n",
4447 sess->sess_ops->InitiatorName);
4448 tpg->nsessions--;
4449 if (tpg->tpg_tiqn)
4450 tpg->tpg_tiqn->tiqn_nsessions--;
4451
4452 pr_debug("Decremented number of active iSCSI Sessions on"
4453 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4454
4455 spin_lock(&sess_idr_lock);
4456 idr_remove(&sess_idr, sess->session_index);
4457 spin_unlock(&sess_idr_lock);
4458
4459 kfree(sess->sess_ops);
4460 sess->sess_ops = NULL;
4461 spin_unlock_bh(&se_tpg->session_lock);
4462
4463 kfree(sess);
4464 return 0;
4465}
4466
4467static void iscsit_logout_post_handler_closesession(
4468 struct iscsi_conn *conn)
4469{
4470 struct iscsi_session *sess = conn->sess;
4471
4472 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4473 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4474
4475 atomic_set(&conn->conn_logout_remove, 0);
4476 complete(&conn->conn_logout_comp);
4477
4478 iscsit_dec_conn_usage_count(conn);
4479 iscsit_stop_session(sess, 1, 1);
4480 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004481 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004482}
4483
4484static void iscsit_logout_post_handler_samecid(
4485 struct iscsi_conn *conn)
4486{
4487 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4488 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4489
4490 atomic_set(&conn->conn_logout_remove, 0);
4491 complete(&conn->conn_logout_comp);
4492
4493 iscsit_cause_connection_reinstatement(conn, 1);
4494 iscsit_dec_conn_usage_count(conn);
4495}
4496
4497static void iscsit_logout_post_handler_diffcid(
4498 struct iscsi_conn *conn,
4499 u16 cid)
4500{
4501 struct iscsi_conn *l_conn;
4502 struct iscsi_session *sess = conn->sess;
4503
4504 if (!sess)
4505 return;
4506
4507 spin_lock_bh(&sess->conn_lock);
4508 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4509 if (l_conn->cid == cid) {
4510 iscsit_inc_conn_usage_count(l_conn);
4511 break;
4512 }
4513 }
4514 spin_unlock_bh(&sess->conn_lock);
4515
4516 if (!l_conn)
4517 return;
4518
4519 if (l_conn->sock)
4520 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4521
4522 spin_lock_bh(&l_conn->state_lock);
4523 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4524 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4525 spin_unlock_bh(&l_conn->state_lock);
4526
4527 iscsit_cause_connection_reinstatement(l_conn, 1);
4528 iscsit_dec_conn_usage_count(l_conn);
4529}
4530
4531/*
4532 * Return of 0 causes the TX thread to restart.
4533 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004534int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004535 struct iscsi_cmd *cmd,
4536 struct iscsi_conn *conn)
4537{
4538 int ret = 0;
4539
4540 switch (cmd->logout_reason) {
4541 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4542 switch (cmd->logout_response) {
4543 case ISCSI_LOGOUT_SUCCESS:
4544 case ISCSI_LOGOUT_CLEANUP_FAILED:
4545 default:
4546 iscsit_logout_post_handler_closesession(conn);
4547 break;
4548 }
4549 ret = 0;
4550 break;
4551 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4552 if (conn->cid == cmd->logout_cid) {
4553 switch (cmd->logout_response) {
4554 case ISCSI_LOGOUT_SUCCESS:
4555 case ISCSI_LOGOUT_CLEANUP_FAILED:
4556 default:
4557 iscsit_logout_post_handler_samecid(conn);
4558 break;
4559 }
4560 ret = 0;
4561 } else {
4562 switch (cmd->logout_response) {
4563 case ISCSI_LOGOUT_SUCCESS:
4564 iscsit_logout_post_handler_diffcid(conn,
4565 cmd->logout_cid);
4566 break;
4567 case ISCSI_LOGOUT_CID_NOT_FOUND:
4568 case ISCSI_LOGOUT_CLEANUP_FAILED:
4569 default:
4570 break;
4571 }
4572 ret = 1;
4573 }
4574 break;
4575 case ISCSI_LOGOUT_REASON_RECOVERY:
4576 switch (cmd->logout_response) {
4577 case ISCSI_LOGOUT_SUCCESS:
4578 case ISCSI_LOGOUT_CID_NOT_FOUND:
4579 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4580 case ISCSI_LOGOUT_CLEANUP_FAILED:
4581 default:
4582 break;
4583 }
4584 ret = 1;
4585 break;
4586 default:
4587 break;
4588
4589 }
4590 return ret;
4591}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004592EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004593
4594void iscsit_fail_session(struct iscsi_session *sess)
4595{
4596 struct iscsi_conn *conn;
4597
4598 spin_lock_bh(&sess->conn_lock);
4599 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4600 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4601 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4602 }
4603 spin_unlock_bh(&sess->conn_lock);
4604
4605 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4606 sess->session_state = TARG_SESS_STATE_FAILED;
4607}
4608
4609int iscsit_free_session(struct iscsi_session *sess)
4610{
4611 u16 conn_count = atomic_read(&sess->nconn);
4612 struct iscsi_conn *conn, *conn_tmp = NULL;
4613 int is_last;
4614
4615 spin_lock_bh(&sess->conn_lock);
4616 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4617
4618 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4619 conn_list) {
4620 if (conn_count == 0)
4621 break;
4622
4623 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4624 is_last = 1;
4625 } else {
4626 iscsit_inc_conn_usage_count(conn_tmp);
4627 is_last = 0;
4628 }
4629 iscsit_inc_conn_usage_count(conn);
4630
4631 spin_unlock_bh(&sess->conn_lock);
4632 iscsit_cause_connection_reinstatement(conn, 1);
4633 spin_lock_bh(&sess->conn_lock);
4634
4635 iscsit_dec_conn_usage_count(conn);
4636 if (is_last == 0)
4637 iscsit_dec_conn_usage_count(conn_tmp);
4638
4639 conn_count--;
4640 }
4641
4642 if (atomic_read(&sess->nconn)) {
4643 spin_unlock_bh(&sess->conn_lock);
4644 wait_for_completion(&sess->session_wait_comp);
4645 } else
4646 spin_unlock_bh(&sess->conn_lock);
4647
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004648 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004649 return 0;
4650}
4651
4652void iscsit_stop_session(
4653 struct iscsi_session *sess,
4654 int session_sleep,
4655 int connection_sleep)
4656{
4657 u16 conn_count = atomic_read(&sess->nconn);
4658 struct iscsi_conn *conn, *conn_tmp = NULL;
4659 int is_last;
4660
4661 spin_lock_bh(&sess->conn_lock);
4662 if (session_sleep)
4663 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4664
4665 if (connection_sleep) {
4666 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4667 conn_list) {
4668 if (conn_count == 0)
4669 break;
4670
4671 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4672 is_last = 1;
4673 } else {
4674 iscsit_inc_conn_usage_count(conn_tmp);
4675 is_last = 0;
4676 }
4677 iscsit_inc_conn_usage_count(conn);
4678
4679 spin_unlock_bh(&sess->conn_lock);
4680 iscsit_cause_connection_reinstatement(conn, 1);
4681 spin_lock_bh(&sess->conn_lock);
4682
4683 iscsit_dec_conn_usage_count(conn);
4684 if (is_last == 0)
4685 iscsit_dec_conn_usage_count(conn_tmp);
4686 conn_count--;
4687 }
4688 } else {
4689 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4690 iscsit_cause_connection_reinstatement(conn, 0);
4691 }
4692
4693 if (session_sleep && atomic_read(&sess->nconn)) {
4694 spin_unlock_bh(&sess->conn_lock);
4695 wait_for_completion(&sess->session_wait_comp);
4696 } else
4697 spin_unlock_bh(&sess->conn_lock);
4698}
4699
4700int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4701{
4702 struct iscsi_session *sess;
4703 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4704 struct se_session *se_sess, *se_sess_tmp;
4705 int session_count = 0;
4706
4707 spin_lock_bh(&se_tpg->session_lock);
4708 if (tpg->nsessions && !force) {
4709 spin_unlock_bh(&se_tpg->session_lock);
4710 return -1;
4711 }
4712
4713 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4714 sess_list) {
4715 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4716
4717 spin_lock(&sess->conn_lock);
4718 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4719 atomic_read(&sess->session_logout) ||
4720 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4721 spin_unlock(&sess->conn_lock);
4722 continue;
4723 }
4724 atomic_set(&sess->session_reinstatement, 1);
4725 spin_unlock(&sess->conn_lock);
4726 spin_unlock_bh(&se_tpg->session_lock);
4727
4728 iscsit_free_session(sess);
4729 spin_lock_bh(&se_tpg->session_lock);
4730
4731 session_count++;
4732 }
4733 spin_unlock_bh(&se_tpg->session_lock);
4734
4735 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4736 " Group: %hu\n", session_count, tpg->tpgt);
4737 return 0;
4738}
4739
4740MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4741MODULE_VERSION("4.1.x");
4742MODULE_AUTHOR("nab@Linux-iSCSI.org");
4743MODULE_LICENSE("GPL");
4744
4745module_init(iscsi_target_init_module);
4746module_exit(iscsi_target_cleanup_module);