blob: 2a25bd3b065c333dcf1303f05eb167a4a4d601e0 [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) {
1055 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001056 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1057 if (!cmd->sense_reason)
1058 return 0;
1059
1060 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 Bellinger3e1c81a2013-03-06 22:18:24 -08001086 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1087 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001088 }
1089 /*
1090 * Call directly into transport_generic_new_cmd() to perform
1091 * the backend memory allocation.
1092 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001093 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1094 if (cmd->sense_reason) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001095 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1096 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001097 }
1098
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{
1107 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1108 /*
1109 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1110 */
1111 if (dump_payload == true)
1112 goto after_immediate_data;
1113
1114 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1115 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001116after_immediate_data:
1117 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1118 /*
1119 * A PDU/CmdSN carrying Immediate Data passed
1120 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1121 * Immediate Bit is not set.
1122 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001123 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001124
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001125 if (cmd->sense_reason) {
1126 if (iscsit_dump_data_payload(cmd->conn,
1127 cmd->first_burst_len, 1) < 0)
1128 return -1;
1129 } else if (cmd->unsolicited_data)
1130 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001131
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001132 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1133 /*
1134 * Immediate Data failed DataCRC and ERL>=1,
1135 * silently drop this PDU and let the initiator
1136 * plug the CmdSN gap.
1137 *
1138 * FIXME: Send Unsolicited NOPIN with reserved
1139 * TTT here to help the initiator figure out
1140 * the missing CmdSN, although they should be
1141 * intelligent enough to determine the missing
1142 * CmdSN and issue a retry to plug the sequence.
1143 */
1144 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001145 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001146 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1147 return -1;
1148
1149 return 0;
1150}
1151
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001152static int
1153iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1154 unsigned char *buf)
1155{
1156 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1157 int rc, immed_data;
1158 bool dump_payload = false;
1159
1160 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1161 if (rc < 0)
1162 return rc;
1163 /*
1164 * Allocation iovecs needed for struct socket operations for
1165 * traditional iSCSI block I/O.
1166 */
1167 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001168 return iscsit_add_reject_cmd(cmd,
1169 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001170 }
1171 immed_data = cmd->immediate_data;
1172
1173 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1174 if (rc < 0)
1175 return rc;
1176 else if (rc > 0)
1177 dump_payload = true;
1178
1179 if (!immed_data)
1180 return 0;
1181
1182 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1183}
1184
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001185static u32 iscsit_do_crypto_hash_sg(
1186 struct hash_desc *hash,
1187 struct iscsi_cmd *cmd,
1188 u32 data_offset,
1189 u32 data_length,
1190 u32 padding,
1191 u8 *pad_bytes)
1192{
1193 u32 data_crc;
1194 u32 i;
1195 struct scatterlist *sg;
1196 unsigned int page_off;
1197
1198 crypto_hash_init(hash);
1199
1200 sg = cmd->first_data_sg;
1201 page_off = cmd->first_data_sg_off;
1202
1203 i = 0;
1204 while (data_length) {
1205 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1206
1207 crypto_hash_update(hash, &sg[i], cur_len);
1208
1209 data_length -= cur_len;
1210 page_off = 0;
1211 i++;
1212 }
1213
1214 if (padding) {
1215 struct scatterlist pad_sg;
1216
1217 sg_init_one(&pad_sg, pad_bytes, padding);
1218 crypto_hash_update(hash, &pad_sg, padding);
1219 }
1220 crypto_hash_final(hash, (u8 *) &data_crc);
1221
1222 return data_crc;
1223}
1224
1225static void iscsit_do_crypto_hash_buf(
1226 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001227 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001228 u32 payload_length,
1229 u32 padding,
1230 u8 *pad_bytes,
1231 u8 *data_crc)
1232{
1233 struct scatterlist sg;
1234
1235 crypto_hash_init(hash);
1236
Jörn Engel8359cf42011-11-24 02:05:51 +01001237 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001238 crypto_hash_update(hash, &sg, payload_length);
1239
1240 if (padding) {
1241 sg_init_one(&sg, pad_bytes, padding);
1242 crypto_hash_update(hash, &sg, padding);
1243 }
1244 crypto_hash_final(hash, data_crc);
1245}
1246
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001247int
1248iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1249 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001250{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001251 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001252 struct iscsi_cmd *cmd = NULL;
1253 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001254 u32 payload_length = ntoh24(hdr->dlength);
1255 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001256
1257 if (!payload_length) {
1258 pr_err("DataOUT payload is ZERO, protocol error.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001259 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1260 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001261 }
1262
1263 /* iSCSI write */
1264 spin_lock_bh(&conn->sess->session_stats_lock);
1265 conn->sess->rx_data_octets += payload_length;
1266 if (conn->sess->se_sess->se_node_acl) {
1267 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1268 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1269 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1270 }
1271 spin_unlock_bh(&conn->sess->session_stats_lock);
1272
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001273 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001274 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001275 " MaxXmitDataSegmentLength: %u\n", payload_length,
1276 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001277 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1278 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001279 }
1280
1281 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1282 payload_length);
1283 if (!cmd)
1284 return 0;
1285
1286 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1287 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001288 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001289 payload_length, conn->cid);
1290
1291 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1292 pr_err("Command ITT: 0x%08x received DataOUT after"
1293 " last DataOUT received, dumping payload\n",
1294 cmd->init_task_tag);
1295 return iscsit_dump_data_payload(conn, payload_length, 1);
1296 }
1297
1298 if (cmd->data_direction != DMA_TO_DEVICE) {
1299 pr_err("Command ITT: 0x%08x received DataOUT for a"
1300 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001301 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001302 }
1303 se_cmd = &cmd->se_cmd;
1304 iscsit_mod_dataout_timer(cmd);
1305
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001306 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001307 pr_err("DataOut Offset: %u, Length %u greater than"
1308 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001309 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001310 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001311 }
1312
1313 if (cmd->unsolicited_data) {
1314 int dump_unsolicited_data = 0;
1315
1316 if (conn->sess->sess_ops->InitialR2T) {
1317 pr_err("Received unexpected unsolicited data"
1318 " while InitialR2T=Yes, protocol error.\n");
1319 transport_send_check_condition_and_sense(&cmd->se_cmd,
1320 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1321 return -1;
1322 }
1323 /*
1324 * Special case for dealing with Unsolicited DataOUT
1325 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1326 * failures;
1327 */
1328
1329 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001330 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001331 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001332 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001333
1334 if (dump_unsolicited_data) {
1335 /*
1336 * Check if a delayed TASK_ABORTED status needs to
1337 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1338 * received with the unsolicitied data out.
1339 */
1340 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1341 iscsit_stop_dataout_timer(cmd);
1342
1343 transport_check_aborted_status(se_cmd,
1344 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1345 return iscsit_dump_data_payload(conn, payload_length, 1);
1346 }
1347 } else {
1348 /*
1349 * For the normal solicited data path:
1350 *
1351 * Check for a delayed TASK_ABORTED status and dump any
1352 * incoming data out payload if one exists. Also, when the
1353 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1354 * data out sequence, we decrement outstanding_r2ts. Once
1355 * outstanding_r2ts reaches zero, go ahead and send the delayed
1356 * TASK_ABORTED status.
1357 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001358 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001359 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1360 if (--cmd->outstanding_r2ts < 1) {
1361 iscsit_stop_dataout_timer(cmd);
1362 transport_check_aborted_status(
1363 se_cmd, 1);
1364 }
1365
1366 return iscsit_dump_data_payload(conn, payload_length, 1);
1367 }
1368 }
1369 /*
1370 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1371 * within-command recovery checks before receiving the payload.
1372 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001373 rc = iscsit_check_pre_dataout(cmd, buf);
1374 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001375 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001376 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001377 return -1;
1378
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001379 *out_cmd = cmd;
1380 return 0;
1381}
1382EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1383
1384static int
1385iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1386 struct iscsi_data *hdr)
1387{
1388 struct kvec *iov;
1389 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1390 u32 payload_length = ntoh24(hdr->dlength);
1391 int iov_ret, data_crc_failed = 0;
1392
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001393 rx_size += payload_length;
1394 iov = &cmd->iov_data[0];
1395
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001396 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1397 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001398 if (iov_ret < 0)
1399 return -1;
1400
1401 iov_count += iov_ret;
1402
1403 padding = ((-payload_length) & 3);
1404 if (padding != 0) {
1405 iov[iov_count].iov_base = cmd->pad_bytes;
1406 iov[iov_count++].iov_len = padding;
1407 rx_size += padding;
1408 pr_debug("Receiving %u padding bytes.\n", padding);
1409 }
1410
1411 if (conn->conn_ops->DataDigest) {
1412 iov[iov_count].iov_base = &checksum;
1413 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1414 rx_size += ISCSI_CRC_LEN;
1415 }
1416
1417 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1418
1419 iscsit_unmap_iovec(cmd);
1420
1421 if (rx_got != rx_size)
1422 return -1;
1423
1424 if (conn->conn_ops->DataDigest) {
1425 u32 data_crc;
1426
1427 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001428 be32_to_cpu(hdr->offset),
1429 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001430 cmd->pad_bytes);
1431
1432 if (checksum != data_crc) {
1433 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1434 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1435 " does not match computed 0x%08x\n",
1436 hdr->itt, hdr->offset, payload_length,
1437 hdr->datasn, checksum, data_crc);
1438 data_crc_failed = 1;
1439 } else {
1440 pr_debug("Got CRC32C DataDigest 0x%08x for"
1441 " %u bytes of Data Out\n", checksum,
1442 payload_length);
1443 }
1444 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001445
1446 return data_crc_failed;
1447}
1448
1449int
1450iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1451 bool data_crc_failed)
1452{
1453 struct iscsi_conn *conn = cmd->conn;
1454 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001455 /*
1456 * Increment post receive data and CRC values or perform
1457 * within-command recovery.
1458 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001459 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1460 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001461 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001462 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001463 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001464 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1465 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001466 /*
1467 * Handle extra special case for out of order
1468 * Unsolicited Data Out.
1469 */
1470 spin_lock_bh(&cmd->istate_lock);
1471 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1472 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1473 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1474 spin_unlock_bh(&cmd->istate_lock);
1475
1476 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001477 if (ooo_cmdsn)
1478 return 0;
1479 target_execute_cmd(&cmd->se_cmd);
1480 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001481 } else /* DATAOUT_CANNOT_RECOVER */
1482 return -1;
1483
1484 return 0;
1485}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001486EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001487
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001488static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1489{
1490 struct iscsi_cmd *cmd;
1491 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1492 int rc;
1493 bool data_crc_failed = false;
1494
1495 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1496 if (rc < 0)
1497 return rc;
1498 else if (!cmd)
1499 return 0;
1500
1501 rc = iscsit_get_dataout(conn, cmd, hdr);
1502 if (rc < 0)
1503 return rc;
1504 else if (rc > 0)
1505 data_crc_failed = true;
1506
1507 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1508}
1509
Nicholas Bellinger778de362013-06-14 16:07:47 -07001510int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1511 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001512{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001513 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001514
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001515 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001516 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1517 " not set, protocol error.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001518 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1519 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001520 }
1521
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001522 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001523 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001524 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001525 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001526 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001527 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1528 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001529 }
1530
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001531 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001532 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001533 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001534 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1535 payload_length);
1536 /*
1537 * This is not a response to a Unsolicited NopIN, which means
1538 * it can either be a NOPOUT ping request (with a valid ITT),
1539 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1540 * Either way, make sure we allocate an struct iscsi_cmd, as both
1541 * can contain ping data.
1542 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001543 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001544 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1545 cmd->i_state = ISTATE_SEND_NOPIN;
1546 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1547 1 : 0);
1548 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1549 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001550 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1551 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001552 cmd->data_direction = DMA_NONE;
1553 }
1554
Nicholas Bellinger778de362013-06-14 16:07:47 -07001555 return 0;
1556}
1557EXPORT_SYMBOL(iscsit_setup_nop_out);
1558
1559int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1560 struct iscsi_nopout *hdr)
1561{
1562 struct iscsi_cmd *cmd_p = NULL;
1563 int cmdsn_ret = 0;
1564 /*
1565 * Initiator is expecting a NopIN ping reply..
1566 */
1567 if (hdr->itt != RESERVED_ITT) {
1568 BUG_ON(!cmd);
1569
1570 spin_lock_bh(&conn->cmd_lock);
1571 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1572 spin_unlock_bh(&conn->cmd_lock);
1573
1574 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1575
1576 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1577 iscsit_add_cmd_to_response_queue(cmd, conn,
1578 cmd->i_state);
1579 return 0;
1580 }
1581
1582 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1583 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1584 return 0;
1585
1586 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001587 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001588
1589 return 0;
1590 }
1591 /*
1592 * This was a response to a unsolicited NOPIN ping.
1593 */
1594 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1595 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1596 if (!cmd_p)
1597 return -EINVAL;
1598
1599 iscsit_stop_nopin_response_timer(conn);
1600
1601 cmd_p->i_state = ISTATE_REMOVE;
1602 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1603
1604 iscsit_start_nopin_timer(conn);
1605 return 0;
1606 }
1607 /*
1608 * Otherwise, initiator is not expecting a NOPIN is response.
1609 * Just ignore for now.
1610 */
1611 return 0;
1612}
1613EXPORT_SYMBOL(iscsit_process_nop_out);
1614
1615static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1616 unsigned char *buf)
1617{
1618 unsigned char *ping_data = NULL;
1619 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1620 struct kvec *iov = NULL;
1621 u32 payload_length = ntoh24(hdr->dlength);
1622 int ret;
1623
1624 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1625 if (ret < 0)
1626 return ret;
1627 /*
1628 * Handle NOP-OUT payload for traditional iSCSI sockets
1629 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001630 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001631 u32 checksum, data_crc, padding = 0;
1632 int niov = 0, rx_got, rx_size = payload_length;
1633
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001634 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1635 if (!ping_data) {
1636 pr_err("Unable to allocate memory for"
1637 " NOPOUT ping data.\n");
1638 ret = -1;
1639 goto out;
1640 }
1641
1642 iov = &cmd->iov_misc[0];
1643 iov[niov].iov_base = ping_data;
1644 iov[niov++].iov_len = payload_length;
1645
1646 padding = ((-payload_length) & 3);
1647 if (padding != 0) {
1648 pr_debug("Receiving %u additional bytes"
1649 " for padding.\n", padding);
1650 iov[niov].iov_base = &cmd->pad_bytes;
1651 iov[niov++].iov_len = padding;
1652 rx_size += padding;
1653 }
1654 if (conn->conn_ops->DataDigest) {
1655 iov[niov].iov_base = &checksum;
1656 iov[niov++].iov_len = ISCSI_CRC_LEN;
1657 rx_size += ISCSI_CRC_LEN;
1658 }
1659
1660 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1661 if (rx_got != rx_size) {
1662 ret = -1;
1663 goto out;
1664 }
1665
1666 if (conn->conn_ops->DataDigest) {
1667 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1668 ping_data, payload_length,
1669 padding, cmd->pad_bytes,
1670 (u8 *)&data_crc);
1671
1672 if (checksum != data_crc) {
1673 pr_err("Ping data CRC32C DataDigest"
1674 " 0x%08x does not match computed 0x%08x\n",
1675 checksum, data_crc);
1676 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1677 pr_err("Unable to recover from"
1678 " NOPOUT Ping DataCRC failure while in"
1679 " ERL=0.\n");
1680 ret = -1;
1681 goto out;
1682 } else {
1683 /*
1684 * Silently drop this PDU and let the
1685 * initiator plug the CmdSN gap.
1686 */
1687 pr_debug("Dropping NOPOUT"
1688 " Command CmdSN: 0x%08x due to"
1689 " DataCRC error.\n", hdr->cmdsn);
1690 ret = 0;
1691 goto out;
1692 }
1693 } else {
1694 pr_debug("Got CRC32C DataDigest"
1695 " 0x%08x for %u bytes of ping data.\n",
1696 checksum, payload_length);
1697 }
1698 }
1699
1700 ping_data[payload_length] = '\0';
1701 /*
1702 * Attach ping data to struct iscsi_cmd->buf_ptr.
1703 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001704 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001705 cmd->buf_ptr_size = payload_length;
1706
1707 pr_debug("Got %u bytes of NOPOUT ping"
1708 " data.\n", payload_length);
1709 pr_debug("Ping Data: \"%s\"\n", ping_data);
1710 }
1711
Nicholas Bellinger778de362013-06-14 16:07:47 -07001712 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001713out:
1714 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001715 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001716
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001717 kfree(ping_data);
1718 return ret;
1719}
1720
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001721int
1722iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1723 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001724{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001725 struct se_tmr_req *se_tmr;
1726 struct iscsi_tmr_req *tmr_req;
1727 struct iscsi_tm *hdr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001728 int out_of_order_cmdsn = 0;
1729 int ret;
1730 u8 function;
1731
1732 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001733 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1734 function = hdr->flags;
1735
1736 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1737 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1738 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1739 hdr->rtt, hdr->refcmdsn, conn->cid);
1740
1741 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1742 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001743 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001744 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001745 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001746 }
1747
1748 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1749 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1750 pr_err("Task Management Request TASK_REASSIGN not"
1751 " issued as immediate command, bad iSCSI Initiator"
1752 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001753 return iscsit_add_reject_cmd(cmd,
1754 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 }
1756 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001757 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1758 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001759
Andy Groverd28b11692012-04-03 15:51:22 -07001760 cmd->data_direction = DMA_NONE;
1761
1762 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1763 if (!cmd->tmr_req) {
1764 pr_err("Unable to allocate memory for"
1765 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001766 return iscsit_add_reject_cmd(cmd,
1767 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1768 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001769 }
1770
1771 /*
1772 * TASK_REASSIGN for ERL=2 / connection stays inside of
1773 * LIO-Target $FABRIC_MOD
1774 */
1775 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1776
1777 u8 tcm_function;
1778 int ret;
1779
1780 transport_init_se_cmd(&cmd->se_cmd,
1781 &lio_target_fabric_configfs->tf_ops,
1782 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001783 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001784
1785 switch (function) {
1786 case ISCSI_TM_FUNC_ABORT_TASK:
1787 tcm_function = TMR_ABORT_TASK;
1788 break;
1789 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1790 tcm_function = TMR_ABORT_TASK_SET;
1791 break;
1792 case ISCSI_TM_FUNC_CLEAR_ACA:
1793 tcm_function = TMR_CLEAR_ACA;
1794 break;
1795 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1796 tcm_function = TMR_CLEAR_TASK_SET;
1797 break;
1798 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1799 tcm_function = TMR_LUN_RESET;
1800 break;
1801 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1802 tcm_function = TMR_TARGET_WARM_RESET;
1803 break;
1804 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1805 tcm_function = TMR_TARGET_COLD_RESET;
1806 break;
1807 default:
1808 pr_err("Unknown iSCSI TMR Function:"
1809 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001810 return iscsit_add_reject_cmd(cmd,
1811 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001812 }
1813
1814 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1815 tcm_function, GFP_KERNEL);
1816 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001817 return iscsit_add_reject_cmd(cmd,
1818 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001819
1820 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1821 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001822
1823 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1824 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1825 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1826 cmd->init_task_tag = hdr->itt;
1827 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001828 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1829 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001830 se_tmr = cmd->se_cmd.se_tmr_req;
1831 tmr_req = cmd->tmr_req;
1832 /*
1833 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1834 */
1835 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001836 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1837 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001838 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001839 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1840 goto attach;
1841 }
1842 }
1843
1844 switch (function) {
1845 case ISCSI_TM_FUNC_ABORT_TASK:
1846 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001847 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001848 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001849 break;
1850 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1851 case ISCSI_TM_FUNC_CLEAR_ACA:
1852 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1853 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1854 break;
1855 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1856 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001857 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1858 goto attach;
1859 }
1860 break;
1861 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1862 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001863 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1864 goto attach;
1865 }
1866 break;
1867 case ISCSI_TM_FUNC_TASK_REASSIGN:
1868 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1869 /*
1870 * Perform sanity checks on the ExpDataSN only if the
1871 * TASK_REASSIGN was successful.
1872 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001873 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001874 break;
1875
1876 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001877 return iscsit_add_reject_cmd(cmd,
1878 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001879 break;
1880 default:
1881 pr_err("Unknown TMR function: 0x%02x, protocol"
1882 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001883 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1884 goto attach;
1885 }
1886
1887 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1888 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1889 se_tmr->call_transport = 1;
1890attach:
1891 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001892 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001893 spin_unlock_bh(&conn->cmd_lock);
1894
1895 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1896 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
1897 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1898 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001899 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001900 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001901 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001902 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001903 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001904 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001905
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001906 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001907 return 0;
1908 /*
1909 * Found the referenced task, send to transport for processing.
1910 */
1911 if (se_tmr->call_transport)
1912 return transport_generic_handle_tmr(&cmd->se_cmd);
1913
1914 /*
1915 * Could not find the referenced LUN, task, or Task Management
1916 * command not authorized or supported. Change state and
1917 * let the tx_thread send the response.
1918 *
1919 * For connection recovery, this is also the default action for
1920 * TMR TASK_REASSIGN.
1921 */
1922 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1923 return 0;
1924}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001925EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001926
1927/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001928int
1929iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1930 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001931{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001932 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001933
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001934 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001935 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001936 "greater than MaxXmitDataSegmentLength %u.\n",
1937 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001938 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1939 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001940 }
1941
1942 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1943 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1944 hdr->exp_statsn, payload_length);
1945
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001946 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1947 cmd->i_state = ISTATE_SEND_TEXTRSP;
1948 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1949 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1950 cmd->targ_xfer_tag = 0xFFFFFFFF;
1951 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1952 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1953 cmd->data_direction = DMA_NONE;
1954
1955 return 0;
1956}
1957EXPORT_SYMBOL(iscsit_setup_text_cmd);
1958
1959int
1960iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1961 struct iscsi_text *hdr)
1962{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001963 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001964 int cmdsn_ret;
1965
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001966 if (!text_in) {
1967 pr_err("Unable to locate text_in buffer for sendtargets"
1968 " discovery\n");
1969 goto reject;
1970 }
1971 if (strncmp("SendTargets", text_in, 11) != 0) {
1972 pr_err("Received Text Data that is not"
1973 " SendTargets, cannot continue.\n");
1974 goto reject;
1975 }
1976 text_ptr = strchr(text_in, '=');
1977 if (!text_ptr) {
1978 pr_err("No \"=\" separator found in Text Data,"
1979 " cannot continue.\n");
1980 goto reject;
1981 }
1982 if (!strncmp("=All", text_ptr, 4)) {
1983 cmd->cmd_flags |= IFC_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07001984 } else if (!strncmp("=iqn.", text_ptr, 5) ||
1985 !strncmp("=eui.", text_ptr, 5)) {
1986 cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001987 } else {
1988 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
1989 goto reject;
1990 }
1991
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001992 spin_lock_bh(&conn->cmd_lock);
1993 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1994 spin_unlock_bh(&conn->cmd_lock);
1995
1996 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1997
1998 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1999 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
2000 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002001 return -1;
2002
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002003 return 0;
2004 }
2005
2006 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002007
2008reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002009 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2010 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002011}
2012EXPORT_SYMBOL(iscsit_process_text_cmd);
2013
2014static int
2015iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2016 unsigned char *buf)
2017{
2018 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2019 char *text_in = NULL;
2020 u32 payload_length = ntoh24(hdr->dlength);
2021 int rx_size, rc;
2022
2023 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2024 if (rc < 0)
2025 return rc;
2026
2027 rx_size = payload_length;
2028 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002029 u32 checksum = 0, data_crc = 0;
2030 u32 padding = 0, pad_bytes = 0;
2031 int niov = 0, rx_got;
2032 struct kvec iov[3];
2033
2034 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002035 if (!text_in) {
2036 pr_err("Unable to allocate memory for"
2037 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002038 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002039 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002040 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002041
2042 memset(iov, 0, 3 * sizeof(struct kvec));
2043 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002044 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002045
2046 padding = ((-payload_length) & 3);
2047 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002048 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002049 iov[niov++].iov_len = padding;
2050 rx_size += padding;
2051 pr_debug("Receiving %u additional bytes"
2052 " for padding.\n", padding);
2053 }
2054 if (conn->conn_ops->DataDigest) {
2055 iov[niov].iov_base = &checksum;
2056 iov[niov++].iov_len = ISCSI_CRC_LEN;
2057 rx_size += ISCSI_CRC_LEN;
2058 }
2059
2060 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002061 if (rx_got != rx_size)
2062 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002063
2064 if (conn->conn_ops->DataDigest) {
2065 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002066 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002067 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002068 (u8 *)&data_crc);
2069
2070 if (checksum != data_crc) {
2071 pr_err("Text data CRC32C DataDigest"
2072 " 0x%08x does not match computed"
2073 " 0x%08x\n", checksum, data_crc);
2074 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2075 pr_err("Unable to recover from"
2076 " Text Data digest failure while in"
2077 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002078 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002079 } else {
2080 /*
2081 * Silently drop this PDU and let the
2082 * initiator plug the CmdSN gap.
2083 */
2084 pr_debug("Dropping Text"
2085 " Command CmdSN: 0x%08x due to"
2086 " DataCRC error.\n", hdr->cmdsn);
2087 kfree(text_in);
2088 return 0;
2089 }
2090 } else {
2091 pr_debug("Got CRC32C DataDigest"
2092 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002093 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002094 }
2095 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002096 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002097 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002098 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002099 }
2100
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002101 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002102
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002103reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002104 kfree(cmd->text_in_ptr);
2105 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002106 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002107}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002108EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002109
2110int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2111{
2112 struct iscsi_conn *conn_p;
2113 struct iscsi_session *sess = conn->sess;
2114
2115 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2116 " for SID: %u.\n", conn->cid, conn->sess->sid);
2117
2118 atomic_set(&sess->session_logout, 1);
2119 atomic_set(&conn->conn_logout_remove, 1);
2120 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2121
2122 iscsit_inc_conn_usage_count(conn);
2123 iscsit_inc_session_usage_count(sess);
2124
2125 spin_lock_bh(&sess->conn_lock);
2126 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2127 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2128 continue;
2129
2130 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2131 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2132 }
2133 spin_unlock_bh(&sess->conn_lock);
2134
2135 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2136
2137 return 0;
2138}
2139
2140int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2141{
2142 struct iscsi_conn *l_conn;
2143 struct iscsi_session *sess = conn->sess;
2144
2145 pr_debug("Received logout request CLOSECONNECTION for CID:"
2146 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2147
2148 /*
2149 * A Logout Request with a CLOSECONNECTION reason code for a CID
2150 * can arrive on a connection with a differing CID.
2151 */
2152 if (conn->cid == cmd->logout_cid) {
2153 spin_lock_bh(&conn->state_lock);
2154 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2155 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2156
2157 atomic_set(&conn->conn_logout_remove, 1);
2158 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2159 iscsit_inc_conn_usage_count(conn);
2160
2161 spin_unlock_bh(&conn->state_lock);
2162 } else {
2163 /*
2164 * Handle all different cid CLOSECONNECTION requests in
2165 * iscsit_logout_post_handler_diffcid() as to give enough
2166 * time for any non immediate command's CmdSN to be
2167 * acknowledged on the connection in question.
2168 *
2169 * Here we simply make sure the CID is still around.
2170 */
2171 l_conn = iscsit_get_conn_from_cid(sess,
2172 cmd->logout_cid);
2173 if (!l_conn) {
2174 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2175 iscsit_add_cmd_to_response_queue(cmd, conn,
2176 cmd->i_state);
2177 return 0;
2178 }
2179
2180 iscsit_dec_conn_usage_count(l_conn);
2181 }
2182
2183 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2184
2185 return 0;
2186}
2187
2188int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2189{
2190 struct iscsi_session *sess = conn->sess;
2191
2192 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2193 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2194
2195 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2196 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2197 " while ERL!=2.\n");
2198 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2199 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2200 return 0;
2201 }
2202
2203 if (conn->cid == cmd->logout_cid) {
2204 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2205 " with CID: %hu on CID: %hu, implementation error.\n",
2206 cmd->logout_cid, conn->cid);
2207 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2208 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2209 return 0;
2210 }
2211
2212 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2213
2214 return 0;
2215}
2216
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002217int
2218iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2219 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002220{
2221 int cmdsn_ret, logout_remove = 0;
2222 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002223 struct iscsi_logout *hdr;
2224 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2225
2226 hdr = (struct iscsi_logout *) buf;
2227 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002228
2229 if (tiqn) {
2230 spin_lock(&tiqn->logout_stats.lock);
2231 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2232 tiqn->logout_stats.normal_logouts++;
2233 else
2234 tiqn->logout_stats.abnormal_logouts++;
2235 spin_unlock(&tiqn->logout_stats.lock);
2236 }
2237
2238 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2239 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2240 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2241 hdr->cid, conn->cid);
2242
2243 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2244 pr_err("Received logout request on connection that"
2245 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002246 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002247 return 0;
2248 }
2249
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002250 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2251 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2252 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2253 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2254 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002255 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2256 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2257 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002258 cmd->logout_reason = reason_code;
2259 cmd->data_direction = DMA_NONE;
2260
2261 /*
2262 * We need to sleep in these cases (by returning 1) until the Logout
2263 * Response gets sent in the tx thread.
2264 */
2265 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2266 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002267 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002268 logout_remove = 1;
2269
2270 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002271 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002272 spin_unlock_bh(&conn->cmd_lock);
2273
2274 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002275 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002276
2277 /*
2278 * Immediate commands are executed, well, immediately.
2279 * Non-Immediate Logout Commands are executed in CmdSN order.
2280 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002281 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002282 int ret = iscsit_execute_cmd(cmd, 0);
2283
2284 if (ret < 0)
2285 return ret;
2286 } else {
2287 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002288 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002289 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002290 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2291 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002292 }
2293
2294 return logout_remove;
2295}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002296EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002297
2298static int iscsit_handle_snack(
2299 struct iscsi_conn *conn,
2300 unsigned char *buf)
2301{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002302 struct iscsi_snack *hdr;
2303
2304 hdr = (struct iscsi_snack *) buf;
2305 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002306
2307 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2308 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2309 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2310 hdr->begrun, hdr->runlength, conn->cid);
2311
2312 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2313 pr_err("Initiator sent SNACK request while in"
2314 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002315 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2316 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002317 }
2318 /*
2319 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2320 * call from inside iscsi_send_recovery_datain_or_r2t().
2321 */
2322 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2323 case 0:
2324 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002325 hdr->itt,
2326 be32_to_cpu(hdr->ttt),
2327 be32_to_cpu(hdr->begrun),
2328 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002329 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002330 return iscsit_handle_status_snack(conn, hdr->itt,
2331 be32_to_cpu(hdr->ttt),
2332 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002333 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002334 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2335 be32_to_cpu(hdr->begrun),
2336 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002337 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2338 /* FIXME: Support R-Data SNACK */
2339 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002340 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2341 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002342 default:
2343 pr_err("Unknown SNACK type 0x%02x, protocol"
2344 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002345 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2346 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002347 }
2348
2349 return 0;
2350}
2351
2352static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2353{
2354 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2355 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2356 wait_for_completion_interruptible_timeout(
2357 &conn->rx_half_close_comp,
2358 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2359 }
2360}
2361
2362static int iscsit_handle_immediate_data(
2363 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002364 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002365 u32 length)
2366{
2367 int iov_ret, rx_got = 0, rx_size = 0;
2368 u32 checksum, iov_count = 0, padding = 0;
2369 struct iscsi_conn *conn = cmd->conn;
2370 struct kvec *iov;
2371
2372 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2373 if (iov_ret < 0)
2374 return IMMEDIATE_DATA_CANNOT_RECOVER;
2375
2376 rx_size = length;
2377 iov_count = iov_ret;
2378 iov = &cmd->iov_data[0];
2379
2380 padding = ((-length) & 3);
2381 if (padding != 0) {
2382 iov[iov_count].iov_base = cmd->pad_bytes;
2383 iov[iov_count++].iov_len = padding;
2384 rx_size += padding;
2385 }
2386
2387 if (conn->conn_ops->DataDigest) {
2388 iov[iov_count].iov_base = &checksum;
2389 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2390 rx_size += ISCSI_CRC_LEN;
2391 }
2392
2393 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2394
2395 iscsit_unmap_iovec(cmd);
2396
2397 if (rx_got != rx_size) {
2398 iscsit_rx_thread_wait_for_tcp(conn);
2399 return IMMEDIATE_DATA_CANNOT_RECOVER;
2400 }
2401
2402 if (conn->conn_ops->DataDigest) {
2403 u32 data_crc;
2404
2405 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2406 cmd->write_data_done, length, padding,
2407 cmd->pad_bytes);
2408
2409 if (checksum != data_crc) {
2410 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2411 " does not match computed 0x%08x\n", checksum,
2412 data_crc);
2413
2414 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2415 pr_err("Unable to recover from"
2416 " Immediate Data digest failure while"
2417 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002418 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002419 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002420 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002421 return IMMEDIATE_DATA_CANNOT_RECOVER;
2422 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002423 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002424 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002425 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002426 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2427 }
2428 } else {
2429 pr_debug("Got CRC32C DataDigest 0x%08x for"
2430 " %u bytes of Immediate Data\n", checksum,
2431 length);
2432 }
2433 }
2434
2435 cmd->write_data_done += length;
2436
Andy Groverebf1d952012-04-03 15:51:24 -07002437 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002438 spin_lock_bh(&cmd->istate_lock);
2439 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2440 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2441 spin_unlock_bh(&cmd->istate_lock);
2442 }
2443
2444 return IMMEDIATE_DATA_NORMAL_OPERATION;
2445}
2446
2447/*
2448 * Called with sess->conn_lock held.
2449 */
2450/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2451 with active network interface */
2452static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2453{
2454 struct iscsi_cmd *cmd;
2455 struct iscsi_conn *conn_p;
2456
2457 /*
2458 * Only send a Asynchronous Message on connections whos network
2459 * interface is still functional.
2460 */
2461 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2462 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2463 iscsit_inc_conn_usage_count(conn_p);
2464 break;
2465 }
2466 }
2467
2468 if (!conn_p)
2469 return;
2470
Wei Yongjun3c989d72012-11-23 12:07:39 +08002471 cmd = iscsit_allocate_cmd(conn_p, GFP_ATOMIC);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002472 if (!cmd) {
2473 iscsit_dec_conn_usage_count(conn_p);
2474 return;
2475 }
2476
2477 cmd->logout_cid = conn->cid;
2478 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2479 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2480
2481 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002482 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002483 spin_unlock_bh(&conn_p->cmd_lock);
2484
2485 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2486 iscsit_dec_conn_usage_count(conn_p);
2487}
2488
2489static int iscsit_send_conn_drop_async_message(
2490 struct iscsi_cmd *cmd,
2491 struct iscsi_conn *conn)
2492{
2493 struct iscsi_async *hdr;
2494
2495 cmd->tx_size = ISCSI_HDR_LEN;
2496 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2497
2498 hdr = (struct iscsi_async *) cmd->pdu;
2499 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2500 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002501 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002502 cmd->targ_xfer_tag = 0xFFFFFFFF;
2503 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2504 cmd->stat_sn = conn->stat_sn++;
2505 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2506 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2507 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2508 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2509 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2510 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2511 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2512
2513 if (conn->conn_ops->HeaderDigest) {
2514 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2515
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002516 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2517 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002518
2519 cmd->tx_size += ISCSI_CRC_LEN;
2520 pr_debug("Attaching CRC32C HeaderDigest to"
2521 " Async Message 0x%08x\n", *header_digest);
2522 }
2523
2524 cmd->iov_misc[0].iov_base = cmd->pdu;
2525 cmd->iov_misc[0].iov_len = cmd->tx_size;
2526 cmd->iov_misc_count = 1;
2527
2528 pr_debug("Sending Connection Dropped Async Message StatSN:"
2529 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2530 cmd->logout_cid, conn->cid);
2531 return 0;
2532}
2533
Andy Grover6f3c0e62012-04-03 15:51:09 -07002534static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2535{
2536 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2537 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2538 wait_for_completion_interruptible_timeout(
2539 &conn->tx_half_close_comp,
2540 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2541 }
2542}
2543
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002544static void
2545iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2546 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2547 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002548{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002549 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2550 hdr->flags = datain->flags;
2551 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2552 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2553 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2554 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2555 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2556 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2557 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2558 }
2559 }
2560 hton24(hdr->dlength, datain->length);
2561 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2562 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2563 (struct scsi_lun *)&hdr->lun);
2564 else
2565 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2566
2567 hdr->itt = cmd->init_task_tag;
2568
2569 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2570 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2571 else
2572 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2573 if (set_statsn)
2574 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2575 else
2576 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2577
2578 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2579 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2580 hdr->datasn = cpu_to_be32(datain->data_sn);
2581 hdr->offset = cpu_to_be32(datain->offset);
2582
2583 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2584 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2585 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2586 ntohl(hdr->offset), datain->length, conn->cid);
2587}
2588
2589static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2590{
2591 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002592 struct iscsi_datain datain;
2593 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002594 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002595 u32 iov_count = 0, tx_size = 0;
2596 int eodr = 0, ret, iov_ret;
2597 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002598
2599 memset(&datain, 0, sizeof(struct iscsi_datain));
2600 dr = iscsit_get_datain_values(cmd, &datain);
2601 if (!dr) {
2602 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2603 cmd->init_task_tag);
2604 return -1;
2605 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002606 /*
2607 * Be paranoid and double check the logic for now.
2608 */
Andy Groverebf1d952012-04-03 15:51:24 -07002609 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002610 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2611 " datain.length: %u exceeds cmd->data_length: %u\n",
2612 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002613 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002614 return -1;
2615 }
2616
2617 spin_lock_bh(&conn->sess->session_stats_lock);
2618 conn->sess->tx_data_octets += datain.length;
2619 if (conn->sess->se_sess->se_node_acl) {
2620 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2621 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2622 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2623 }
2624 spin_unlock_bh(&conn->sess->session_stats_lock);
2625 /*
2626 * Special case for successfully execution w/ both DATAIN
2627 * and Sense Data.
2628 */
2629 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2630 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2631 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2632 else {
2633 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2634 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2635 iscsit_increment_maxcmdsn(cmd, conn->sess);
2636 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002637 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002638 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002639 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2640 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002641 }
2642
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002643 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002644
2645 iov = &cmd->iov_data[0];
2646 iov[iov_count].iov_base = cmd->pdu;
2647 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2648 tx_size += ISCSI_HDR_LEN;
2649
2650 if (conn->conn_ops->HeaderDigest) {
2651 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2652
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002653 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2654 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002655
2656 iov[0].iov_len += ISCSI_CRC_LEN;
2657 tx_size += ISCSI_CRC_LEN;
2658
2659 pr_debug("Attaching CRC32 HeaderDigest"
2660 " for DataIN PDU 0x%08x\n", *header_digest);
2661 }
2662
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002663 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2664 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002665 if (iov_ret < 0)
2666 return -1;
2667
2668 iov_count += iov_ret;
2669 tx_size += datain.length;
2670
2671 cmd->padding = ((-datain.length) & 3);
2672 if (cmd->padding) {
2673 iov[iov_count].iov_base = cmd->pad_bytes;
2674 iov[iov_count++].iov_len = cmd->padding;
2675 tx_size += cmd->padding;
2676
2677 pr_debug("Attaching %u padding bytes\n",
2678 cmd->padding);
2679 }
2680 if (conn->conn_ops->DataDigest) {
2681 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2682 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2683
2684 iov[iov_count].iov_base = &cmd->data_crc;
2685 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2686 tx_size += ISCSI_CRC_LEN;
2687
2688 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2689 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2690 }
2691
2692 cmd->iov_data_count = iov_count;
2693 cmd->tx_size = tx_size;
2694
Andy Grover6f3c0e62012-04-03 15:51:09 -07002695 /* sendpage is preferred but can't insert markers */
2696 if (!conn->conn_ops->IFMarker)
2697 ret = iscsit_fe_sendpage_sg(cmd, conn);
2698 else
2699 ret = iscsit_send_tx_data(cmd, conn, 0);
2700
2701 iscsit_unmap_iovec(cmd);
2702
2703 if (ret < 0) {
2704 iscsit_tx_thread_wait_for_tcp(conn);
2705 return ret;
2706 }
2707
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002708 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002709 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002710 2 : 1;
2711 iscsit_free_datain_req(cmd, dr);
2712 }
2713
Andy Grover6f3c0e62012-04-03 15:51:09 -07002714 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002715}
2716
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002717int
2718iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2719 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002720{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002721 struct iscsi_conn *logout_conn = NULL;
2722 struct iscsi_conn_recovery *cr = NULL;
2723 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002724 /*
2725 * The actual shutting down of Sessions and/or Connections
2726 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2727 * is done in scsi_logout_post_handler().
2728 */
2729 switch (cmd->logout_reason) {
2730 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2731 pr_debug("iSCSI session logout successful, setting"
2732 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2733 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2734 break;
2735 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2736 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2737 break;
2738 /*
2739 * For CLOSECONNECTION logout requests carrying
2740 * a matching logout CID -> local CID, the reference
2741 * for the local CID will have been incremented in
2742 * iscsi_logout_closeconnection().
2743 *
2744 * For CLOSECONNECTION logout requests carrying
2745 * a different CID than the connection it arrived
2746 * on, the connection responding to cmd->logout_cid
2747 * is stopped in iscsit_logout_post_handler_diffcid().
2748 */
2749
2750 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2751 " successful.\n", cmd->logout_cid, conn->cid);
2752 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2753 break;
2754 case ISCSI_LOGOUT_REASON_RECOVERY:
2755 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2756 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2757 break;
2758 /*
2759 * If the connection is still active from our point of view
2760 * force connection recovery to occur.
2761 */
2762 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2763 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002764 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002765 iscsit_connection_reinstatement_rcfr(logout_conn);
2766 iscsit_dec_conn_usage_count(logout_conn);
2767 }
2768
2769 cr = iscsit_get_inactive_connection_recovery_entry(
2770 conn->sess, cmd->logout_cid);
2771 if (!cr) {
2772 pr_err("Unable to locate CID: %hu for"
2773 " REMOVECONNFORRECOVERY Logout Request.\n",
2774 cmd->logout_cid);
2775 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2776 break;
2777 }
2778
2779 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2780
2781 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2782 " for recovery for CID: %hu on CID: %hu successful.\n",
2783 cmd->logout_cid, conn->cid);
2784 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2785 break;
2786 default:
2787 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2788 cmd->logout_reason);
2789 return -1;
2790 }
2791
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002792 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2793 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2794 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002795 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002796 cmd->stat_sn = conn->stat_sn++;
2797 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2798
2799 iscsit_increment_maxcmdsn(cmd, conn->sess);
2800 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2801 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2802
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002803 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2804 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2805 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2806 cmd->logout_cid, conn->cid);
2807
2808 return 0;
2809}
2810EXPORT_SYMBOL(iscsit_build_logout_rsp);
2811
2812static int
2813iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2814{
2815 struct kvec *iov;
2816 int niov = 0, tx_size, rc;
2817
2818 rc = iscsit_build_logout_rsp(cmd, conn,
2819 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2820 if (rc < 0)
2821 return rc;
2822
2823 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002824 iov = &cmd->iov_misc[0];
2825 iov[niov].iov_base = cmd->pdu;
2826 iov[niov++].iov_len = ISCSI_HDR_LEN;
2827
2828 if (conn->conn_ops->HeaderDigest) {
2829 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2830
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002831 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2832 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002833
2834 iov[0].iov_len += ISCSI_CRC_LEN;
2835 tx_size += ISCSI_CRC_LEN;
2836 pr_debug("Attaching CRC32C HeaderDigest to"
2837 " Logout Response 0x%08x\n", *header_digest);
2838 }
2839 cmd->iov_misc_count = niov;
2840 cmd->tx_size = tx_size;
2841
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002842 return 0;
2843}
2844
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002845void
2846iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2847 struct iscsi_nopin *hdr, bool nopout_response)
2848{
2849 hdr->opcode = ISCSI_OP_NOOP_IN;
2850 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2851 hton24(hdr->dlength, cmd->buf_ptr_size);
2852 if (nopout_response)
2853 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2854 hdr->itt = cmd->init_task_tag;
2855 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2856 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2857 conn->stat_sn;
2858 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2859
2860 if (nopout_response)
2861 iscsit_increment_maxcmdsn(cmd, conn->sess);
2862
2863 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2864 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2865
2866 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2867 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2868 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2869 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2870}
2871EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2872
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002873/*
2874 * Unsolicited NOPIN, either requesting a response or not.
2875 */
2876static int iscsit_send_unsolicited_nopin(
2877 struct iscsi_cmd *cmd,
2878 struct iscsi_conn *conn,
2879 int want_response)
2880{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002881 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2882 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002883
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002884 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002885
2886 if (conn->conn_ops->HeaderDigest) {
2887 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2888
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002889 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2890 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002891
2892 tx_size += ISCSI_CRC_LEN;
2893 pr_debug("Attaching CRC32C HeaderDigest to"
2894 " NopIN 0x%08x\n", *header_digest);
2895 }
2896
2897 cmd->iov_misc[0].iov_base = cmd->pdu;
2898 cmd->iov_misc[0].iov_len = tx_size;
2899 cmd->iov_misc_count = 1;
2900 cmd->tx_size = tx_size;
2901
2902 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2903 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2904
Andy Grover6f3c0e62012-04-03 15:51:09 -07002905 ret = iscsit_send_tx_data(cmd, conn, 1);
2906 if (ret < 0) {
2907 iscsit_tx_thread_wait_for_tcp(conn);
2908 return ret;
2909 }
2910
2911 spin_lock_bh(&cmd->istate_lock);
2912 cmd->i_state = want_response ?
2913 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2914 spin_unlock_bh(&cmd->istate_lock);
2915
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002916 return 0;
2917}
2918
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002919static int
2920iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002921{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002922 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002923 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002924 u32 padding = 0;
2925 int niov = 0, tx_size;
2926
2927 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002928
2929 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002930 iov = &cmd->iov_misc[0];
2931 iov[niov].iov_base = cmd->pdu;
2932 iov[niov++].iov_len = ISCSI_HDR_LEN;
2933
2934 if (conn->conn_ops->HeaderDigest) {
2935 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2936
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002937 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2938 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002939
2940 iov[0].iov_len += ISCSI_CRC_LEN;
2941 tx_size += ISCSI_CRC_LEN;
2942 pr_debug("Attaching CRC32C HeaderDigest"
2943 " to NopIn 0x%08x\n", *header_digest);
2944 }
2945
2946 /*
2947 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2948 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2949 */
2950 if (cmd->buf_ptr_size) {
2951 iov[niov].iov_base = cmd->buf_ptr;
2952 iov[niov++].iov_len = cmd->buf_ptr_size;
2953 tx_size += cmd->buf_ptr_size;
2954
2955 pr_debug("Echoing back %u bytes of ping"
2956 " data.\n", cmd->buf_ptr_size);
2957
2958 padding = ((-cmd->buf_ptr_size) & 3);
2959 if (padding != 0) {
2960 iov[niov].iov_base = &cmd->pad_bytes;
2961 iov[niov++].iov_len = padding;
2962 tx_size += padding;
2963 pr_debug("Attaching %u additional"
2964 " padding bytes.\n", padding);
2965 }
2966 if (conn->conn_ops->DataDigest) {
2967 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2968 cmd->buf_ptr, cmd->buf_ptr_size,
2969 padding, (u8 *)&cmd->pad_bytes,
2970 (u8 *)&cmd->data_crc);
2971
2972 iov[niov].iov_base = &cmd->data_crc;
2973 iov[niov++].iov_len = ISCSI_CRC_LEN;
2974 tx_size += ISCSI_CRC_LEN;
2975 pr_debug("Attached DataDigest for %u"
2976 " bytes of ping data, CRC 0x%08x\n",
2977 cmd->buf_ptr_size, cmd->data_crc);
2978 }
2979 }
2980
2981 cmd->iov_misc_count = niov;
2982 cmd->tx_size = tx_size;
2983
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002984 return 0;
2985}
2986
Andy Grover6f3c0e62012-04-03 15:51:09 -07002987static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002988 struct iscsi_cmd *cmd,
2989 struct iscsi_conn *conn)
2990{
2991 int tx_size = 0;
2992 struct iscsi_r2t *r2t;
2993 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002994 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002995
2996 r2t = iscsit_get_r2t_from_list(cmd);
2997 if (!r2t)
2998 return -1;
2999
3000 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3001 memset(hdr, 0, ISCSI_HDR_LEN);
3002 hdr->opcode = ISCSI_OP_R2T;
3003 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3004 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3005 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003006 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003007 spin_lock_bh(&conn->sess->ttt_lock);
3008 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3009 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3010 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3011 spin_unlock_bh(&conn->sess->ttt_lock);
3012 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3013 hdr->statsn = cpu_to_be32(conn->stat_sn);
3014 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3015 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3016 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3017 hdr->data_offset = cpu_to_be32(r2t->offset);
3018 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3019
3020 cmd->iov_misc[0].iov_base = cmd->pdu;
3021 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3022 tx_size += ISCSI_HDR_LEN;
3023
3024 if (conn->conn_ops->HeaderDigest) {
3025 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3026
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003027 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3028 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003029
3030 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3031 tx_size += ISCSI_CRC_LEN;
3032 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3033 " PDU 0x%08x\n", *header_digest);
3034 }
3035
3036 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3037 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3038 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3039 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3040 r2t->offset, r2t->xfer_len, conn->cid);
3041
3042 cmd->iov_misc_count = 1;
3043 cmd->tx_size = tx_size;
3044
3045 spin_lock_bh(&cmd->r2t_lock);
3046 r2t->sent_r2t = 1;
3047 spin_unlock_bh(&cmd->r2t_lock);
3048
Andy Grover6f3c0e62012-04-03 15:51:09 -07003049 ret = iscsit_send_tx_data(cmd, conn, 1);
3050 if (ret < 0) {
3051 iscsit_tx_thread_wait_for_tcp(conn);
3052 return ret;
3053 }
3054
3055 spin_lock_bh(&cmd->dataout_timeout_lock);
3056 iscsit_start_dataout_timer(cmd, conn);
3057 spin_unlock_bh(&cmd->dataout_timeout_lock);
3058
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003059 return 0;
3060}
3061
3062/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003063 * @recovery: If called from iscsi_task_reassign_complete_write() for
3064 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003065 */
3066int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003067 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003068 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003069 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003070{
3071 int first_r2t = 1;
3072 u32 offset = 0, xfer_len = 0;
3073
3074 spin_lock_bh(&cmd->r2t_lock);
3075 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3076 spin_unlock_bh(&cmd->r2t_lock);
3077 return 0;
3078 }
3079
Andy Grover8b1e1242012-04-03 15:51:12 -07003080 if (conn->sess->sess_ops->DataSequenceInOrder &&
3081 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003082 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003083
3084 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3085 if (conn->sess->sess_ops->DataSequenceInOrder) {
3086 offset = cmd->r2t_offset;
3087
Andy Grover8b1e1242012-04-03 15:51:12 -07003088 if (first_r2t && recovery) {
3089 int new_data_end = offset +
3090 conn->sess->sess_ops->MaxBurstLength -
3091 cmd->next_burst_len;
3092
Andy Groverebf1d952012-04-03 15:51:24 -07003093 if (new_data_end > cmd->se_cmd.data_length)
3094 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003095 else
3096 xfer_len =
3097 conn->sess->sess_ops->MaxBurstLength -
3098 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003099 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003100 int new_data_end = offset +
3101 conn->sess->sess_ops->MaxBurstLength;
3102
Andy Groverebf1d952012-04-03 15:51:24 -07003103 if (new_data_end > cmd->se_cmd.data_length)
3104 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003105 else
3106 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003107 }
3108 cmd->r2t_offset += xfer_len;
3109
Andy Groverebf1d952012-04-03 15:51:24 -07003110 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003111 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3112 } else {
3113 struct iscsi_seq *seq;
3114
3115 seq = iscsit_get_seq_holder_for_r2t(cmd);
3116 if (!seq) {
3117 spin_unlock_bh(&cmd->r2t_lock);
3118 return -1;
3119 }
3120
3121 offset = seq->offset;
3122 xfer_len = seq->xfer_len;
3123
3124 if (cmd->seq_send_order == cmd->seq_count)
3125 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3126 }
3127 cmd->outstanding_r2ts++;
3128 first_r2t = 0;
3129
3130 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3131 spin_unlock_bh(&cmd->r2t_lock);
3132 return -1;
3133 }
3134
3135 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3136 break;
3137 }
3138 spin_unlock_bh(&cmd->r2t_lock);
3139
3140 return 0;
3141}
3142
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003143void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3144 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003145{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003146 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003147 cmd->stat_sn = conn->stat_sn++;
3148
3149 spin_lock_bh(&conn->sess->session_stats_lock);
3150 conn->sess->rsp_pdus++;
3151 spin_unlock_bh(&conn->sess->session_stats_lock);
3152
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003153 memset(hdr, 0, ISCSI_HDR_LEN);
3154 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3155 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3156 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3157 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003158 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003159 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3160 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003161 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003162 }
3163 hdr->response = cmd->iscsi_response;
3164 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003165 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003166 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3167
3168 iscsit_increment_maxcmdsn(cmd, conn->sess);
3169 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3170 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3171
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003172 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3173 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3174 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3175 cmd->se_cmd.scsi_status, conn->cid);
3176}
3177EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3178
3179static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3180{
3181 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3182 struct kvec *iov;
3183 u32 padding = 0, tx_size = 0;
3184 int iov_count = 0;
3185 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3186
3187 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3188
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003189 iov = &cmd->iov_misc[0];
3190 iov[iov_count].iov_base = cmd->pdu;
3191 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3192 tx_size += ISCSI_HDR_LEN;
3193
3194 /*
3195 * Attach SENSE DATA payload to iSCSI Response PDU
3196 */
3197 if (cmd->se_cmd.sense_buffer &&
3198 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3199 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003200 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3201 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3202
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003203 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003204 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003205 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003206 iov[iov_count++].iov_len =
3207 (cmd->se_cmd.scsi_sense_length + padding);
3208 tx_size += cmd->se_cmd.scsi_sense_length;
3209
3210 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003211 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003212 cmd->se_cmd.scsi_sense_length, 0, padding);
3213 tx_size += padding;
3214 pr_debug("Adding %u bytes of padding to"
3215 " SENSE.\n", padding);
3216 }
3217
3218 if (conn->conn_ops->DataDigest) {
3219 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003220 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003221 (cmd->se_cmd.scsi_sense_length + padding),
3222 0, NULL, (u8 *)&cmd->data_crc);
3223
3224 iov[iov_count].iov_base = &cmd->data_crc;
3225 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3226 tx_size += ISCSI_CRC_LEN;
3227
3228 pr_debug("Attaching CRC32 DataDigest for"
3229 " SENSE, %u bytes CRC 0x%08x\n",
3230 (cmd->se_cmd.scsi_sense_length + padding),
3231 cmd->data_crc);
3232 }
3233
3234 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3235 " Response PDU\n",
3236 cmd->se_cmd.scsi_sense_length);
3237 }
3238
3239 if (conn->conn_ops->HeaderDigest) {
3240 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3241
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003242 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3243 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003244
3245 iov[0].iov_len += ISCSI_CRC_LEN;
3246 tx_size += ISCSI_CRC_LEN;
3247 pr_debug("Attaching CRC32 HeaderDigest for Response"
3248 " PDU 0x%08x\n", *header_digest);
3249 }
3250
3251 cmd->iov_misc_count = iov_count;
3252 cmd->tx_size = tx_size;
3253
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003254 return 0;
3255}
3256
3257static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3258{
3259 switch (se_tmr->response) {
3260 case TMR_FUNCTION_COMPLETE:
3261 return ISCSI_TMF_RSP_COMPLETE;
3262 case TMR_TASK_DOES_NOT_EXIST:
3263 return ISCSI_TMF_RSP_NO_TASK;
3264 case TMR_LUN_DOES_NOT_EXIST:
3265 return ISCSI_TMF_RSP_NO_LUN;
3266 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3267 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3268 case TMR_FUNCTION_AUTHORIZATION_FAILED:
3269 return ISCSI_TMF_RSP_AUTH_FAILED;
3270 case TMR_FUNCTION_REJECTED:
3271 default:
3272 return ISCSI_TMF_RSP_REJECTED;
3273 }
3274}
3275
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003276void
3277iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3278 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003279{
3280 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003281
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003282 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003283 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003284 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003285 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003286 cmd->stat_sn = conn->stat_sn++;
3287 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3288
3289 iscsit_increment_maxcmdsn(cmd, conn->sess);
3290 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3291 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3292
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003293 pr_debug("Built Task Management Response ITT: 0x%08x,"
3294 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3295 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3296}
3297EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3298
3299static int
3300iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3301{
3302 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3303 u32 tx_size = 0;
3304
3305 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3306
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003307 cmd->iov_misc[0].iov_base = cmd->pdu;
3308 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3309 tx_size += ISCSI_HDR_LEN;
3310
3311 if (conn->conn_ops->HeaderDigest) {
3312 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3313
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003314 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3315 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003316
3317 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3318 tx_size += ISCSI_CRC_LEN;
3319 pr_debug("Attaching CRC32 HeaderDigest for Task"
3320 " Mgmt Response PDU 0x%08x\n", *header_digest);
3321 }
3322
3323 cmd->iov_misc_count = 1;
3324 cmd->tx_size = tx_size;
3325
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003326 return 0;
3327}
3328
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003329static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3330{
3331 bool ret = false;
3332
3333 if (np->np_sockaddr.ss_family == AF_INET6) {
3334 const struct sockaddr_in6 sin6 = {
3335 .sin6_addr = IN6ADDR_ANY_INIT };
3336 struct sockaddr_in6 *sock_in6 =
3337 (struct sockaddr_in6 *)&np->np_sockaddr;
3338
3339 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3340 sin6.sin6_addr.s6_addr, 16))
3341 ret = true;
3342 } else {
3343 struct sockaddr_in * sock_in =
3344 (struct sockaddr_in *)&np->np_sockaddr;
3345
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003346 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003347 ret = true;
3348 }
3349
3350 return ret;
3351}
3352
Andy Grover8b1e1242012-04-03 15:51:12 -07003353#define SENDTARGETS_BUF_LIMIT 32768U
3354
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003355static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3356{
3357 char *payload = NULL;
3358 struct iscsi_conn *conn = cmd->conn;
3359 struct iscsi_portal_group *tpg;
3360 struct iscsi_tiqn *tiqn;
3361 struct iscsi_tpg_np *tpg_np;
3362 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003363 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003364 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003365
Andy Grover8b1e1242012-04-03 15:51:12 -07003366 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3367 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003368
3369 payload = kzalloc(buffer_len, GFP_KERNEL);
3370 if (!payload) {
3371 pr_err("Unable to allocate memory for sendtargets"
3372 " response.\n");
3373 return -ENOMEM;
3374 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003375 /*
3376 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3377 * explicit case..
3378 */
3379 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) {
3380 text_ptr = strchr(text_in, '=');
3381 if (!text_ptr) {
3382 pr_err("Unable to locate '=' string in text_in:"
3383 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003384 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003385 return -EINVAL;
3386 }
3387 /*
3388 * Skip over '=' character..
3389 */
3390 text_ptr += 1;
3391 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003392
3393 spin_lock(&tiqn_lock);
3394 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003395 if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) &&
3396 strcmp(tiqn->tiqn, text_ptr)) {
3397 continue;
3398 }
3399
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003400 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3401 len += 1;
3402
3403 if ((len + payload_len) > buffer_len) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003404 end_of_buf = 1;
3405 goto eob;
3406 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003407 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003408 payload_len += len;
3409
3410 spin_lock(&tiqn->tiqn_tpg_lock);
3411 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3412
3413 spin_lock(&tpg->tpg_state_lock);
3414 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3415 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3416 spin_unlock(&tpg->tpg_state_lock);
3417 continue;
3418 }
3419 spin_unlock(&tpg->tpg_state_lock);
3420
3421 spin_lock(&tpg->tpg_np_lock);
3422 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3423 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003424 struct iscsi_np *np = tpg_np->tpg_np;
3425 bool inaddr_any = iscsit_check_inaddr_any(np);
3426
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003427 len = sprintf(buf, "TargetAddress="
3428 "%s%s%s:%hu,%hu",
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003429 (np->np_sockaddr.ss_family == AF_INET6) ?
3430 "[" : "", (inaddr_any == false) ?
3431 np->np_ip : conn->local_ip,
3432 (np->np_sockaddr.ss_family == AF_INET6) ?
3433 "]" : "", (inaddr_any == false) ?
3434 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003435 tpg->tpgt);
3436 len += 1;
3437
3438 if ((len + payload_len) > buffer_len) {
3439 spin_unlock(&tpg->tpg_np_lock);
3440 spin_unlock(&tiqn->tiqn_tpg_lock);
3441 end_of_buf = 1;
3442 goto eob;
3443 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003444 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003445 payload_len += len;
3446 }
3447 spin_unlock(&tpg->tpg_np_lock);
3448 }
3449 spin_unlock(&tiqn->tiqn_tpg_lock);
3450eob:
3451 if (end_of_buf)
3452 break;
Nicholas Bellinger66658892013-06-19 22:45:42 -07003453
3454 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE)
3455 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003456 }
3457 spin_unlock(&tiqn_lock);
3458
3459 cmd->buf_ptr = payload;
3460
3461 return payload_len;
3462}
3463
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003464int
3465iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3466 struct iscsi_text_rsp *hdr)
3467{
3468 int text_length, padding;
3469
3470 text_length = iscsit_build_sendtargets_response(cmd);
3471 if (text_length < 0)
3472 return text_length;
3473
3474 hdr->opcode = ISCSI_OP_TEXT_RSP;
3475 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3476 padding = ((-text_length) & 3);
3477 hton24(hdr->dlength, text_length);
3478 hdr->itt = cmd->init_task_tag;
3479 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3480 cmd->stat_sn = conn->stat_sn++;
3481 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3482
3483 iscsit_increment_maxcmdsn(cmd, conn->sess);
3484 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3485 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3486
3487 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3488 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3489 text_length, conn->cid);
3490
3491 return text_length + padding;
3492}
3493EXPORT_SYMBOL(iscsit_build_text_rsp);
3494
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003495/*
3496 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3497 * MaxRecvDataSegmentLength.
3498 */
3499static int iscsit_send_text_rsp(
3500 struct iscsi_cmd *cmd,
3501 struct iscsi_conn *conn)
3502{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003503 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003504 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003505 u32 tx_size = 0;
3506 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003507
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003508 rc = iscsit_build_text_rsp(cmd, conn, hdr);
3509 if (rc < 0)
3510 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003511
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003512 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003513 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003514 iov[iov_count].iov_base = cmd->pdu;
3515 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3516 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003517 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003518
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003519 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003520
3521 if (conn->conn_ops->HeaderDigest) {
3522 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3523
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003524 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3525 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003526
3527 iov[0].iov_len += ISCSI_CRC_LEN;
3528 tx_size += ISCSI_CRC_LEN;
3529 pr_debug("Attaching CRC32 HeaderDigest for"
3530 " Text Response PDU 0x%08x\n", *header_digest);
3531 }
3532
3533 if (conn->conn_ops->DataDigest) {
3534 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003535 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003536 0, NULL, (u8 *)&cmd->data_crc);
3537
3538 iov[iov_count].iov_base = &cmd->data_crc;
3539 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3540 tx_size += ISCSI_CRC_LEN;
3541
3542 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003543 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003544 cmd->data_crc);
3545 }
3546
3547 cmd->iov_misc_count = iov_count;
3548 cmd->tx_size = tx_size;
3549
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003550 return 0;
3551}
3552
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003553void
3554iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3555 struct iscsi_reject *hdr)
3556{
3557 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003558 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003559 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3560 hton24(hdr->dlength, ISCSI_HDR_LEN);
3561 hdr->ffffffff = cpu_to_be32(0xffffffff);
3562 cmd->stat_sn = conn->stat_sn++;
3563 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3564 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3565 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3566
3567}
3568EXPORT_SYMBOL(iscsit_build_reject);
3569
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003570static int iscsit_send_reject(
3571 struct iscsi_cmd *cmd,
3572 struct iscsi_conn *conn)
3573{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003574 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003575 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003576 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003577
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003578 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003579
3580 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003581 iov[iov_count].iov_base = cmd->pdu;
3582 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3583 iov[iov_count].iov_base = cmd->buf_ptr;
3584 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3585
3586 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3587
3588 if (conn->conn_ops->HeaderDigest) {
3589 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3590
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003591 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3592 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003593
3594 iov[0].iov_len += ISCSI_CRC_LEN;
3595 tx_size += ISCSI_CRC_LEN;
3596 pr_debug("Attaching CRC32 HeaderDigest for"
3597 " REJECT PDU 0x%08x\n", *header_digest);
3598 }
3599
3600 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003601 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3602 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003603
3604 iov[iov_count].iov_base = &cmd->data_crc;
3605 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3606 tx_size += ISCSI_CRC_LEN;
3607 pr_debug("Attaching CRC32 DataDigest for REJECT"
3608 " PDU 0x%08x\n", cmd->data_crc);
3609 }
3610
3611 cmd->iov_misc_count = iov_count;
3612 cmd->tx_size = tx_size;
3613
3614 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3615 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3616
3617 return 0;
3618}
3619
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003620void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3621{
3622 struct iscsi_thread_set *ts = conn->thread_set;
3623 int ord, cpu;
3624 /*
3625 * thread_id is assigned from iscsit_global->ts_bitmap from
3626 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3627 *
3628 * Here we use thread_id to determine which CPU that this
3629 * iSCSI connection's iscsi_thread_set will be scheduled to
3630 * execute upon.
3631 */
3632 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003633 for_each_online_cpu(cpu) {
3634 if (ord-- == 0) {
3635 cpumask_set_cpu(cpu, conn->conn_cpumask);
3636 return;
3637 }
3638 }
3639 /*
3640 * This should never be reached..
3641 */
3642 dump_stack();
3643 cpumask_setall(conn->conn_cpumask);
3644}
3645
3646static inline void iscsit_thread_check_cpumask(
3647 struct iscsi_conn *conn,
3648 struct task_struct *p,
3649 int mode)
3650{
3651 char buf[128];
3652 /*
3653 * mode == 1 signals iscsi_target_tx_thread() usage.
3654 * mode == 0 signals iscsi_target_rx_thread() usage.
3655 */
3656 if (mode == 1) {
3657 if (!conn->conn_tx_reset_cpumask)
3658 return;
3659 conn->conn_tx_reset_cpumask = 0;
3660 } else {
3661 if (!conn->conn_rx_reset_cpumask)
3662 return;
3663 conn->conn_rx_reset_cpumask = 0;
3664 }
3665 /*
3666 * Update the CPU mask for this single kthread so that
3667 * both TX and RX kthreads are scheduled to run on the
3668 * same CPU.
3669 */
3670 memset(buf, 0, 128);
3671 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003672 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3673}
3674
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003675static int
3676iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003677{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003678 int ret;
3679
3680 switch (state) {
3681 case ISTATE_SEND_R2T:
3682 ret = iscsit_send_r2t(cmd, conn);
3683 if (ret < 0)
3684 goto err;
3685 break;
3686 case ISTATE_REMOVE:
3687 spin_lock_bh(&conn->cmd_lock);
3688 list_del(&cmd->i_conn_node);
3689 spin_unlock_bh(&conn->cmd_lock);
3690
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003691 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003692 break;
3693 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3694 iscsit_mod_nopin_response_timer(conn);
3695 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3696 if (ret < 0)
3697 goto err;
3698 break;
3699 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3700 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3701 if (ret < 0)
3702 goto err;
3703 break;
3704 default:
3705 pr_err("Unknown Opcode: 0x%02x ITT:"
3706 " 0x%08x, i_state: %d on CID: %hu\n",
3707 cmd->iscsi_opcode, cmd->init_task_tag, state,
3708 conn->cid);
3709 goto err;
3710 }
3711
3712 return 0;
3713
3714err:
3715 return -1;
3716}
3717
3718static int
3719iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3720{
3721 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003722 struct iscsi_queue_req *qr;
3723 struct iscsi_cmd *cmd;
3724 u8 state;
3725 int ret;
3726
3727 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3728 atomic_set(&conn->check_immediate_queue, 0);
3729 cmd = qr->cmd;
3730 state = qr->state;
3731 kmem_cache_free(lio_qr_cache, qr);
3732
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003733 ret = t->iscsit_immediate_queue(conn, cmd, state);
3734 if (ret < 0)
3735 return ret;
3736 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003737
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003738 return 0;
3739}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003740
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003741static int
3742iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3743{
3744 int ret;
3745
3746check_rsp_state:
3747 switch (state) {
3748 case ISTATE_SEND_DATAIN:
3749 ret = iscsit_send_datain(cmd, conn);
3750 if (ret < 0)
3751 goto err;
3752 else if (!ret)
3753 /* more drs */
3754 goto check_rsp_state;
3755 else if (ret == 1) {
3756 /* all done */
3757 spin_lock_bh(&cmd->istate_lock);
3758 cmd->i_state = ISTATE_SENT_STATUS;
3759 spin_unlock_bh(&cmd->istate_lock);
3760
3761 if (atomic_read(&conn->check_immediate_queue))
3762 return 1;
3763
3764 return 0;
3765 } else if (ret == 2) {
3766 /* Still must send status,
3767 SCF_TRANSPORT_TASK_SENSE was set */
3768 spin_lock_bh(&cmd->istate_lock);
3769 cmd->i_state = ISTATE_SEND_STATUS;
3770 spin_unlock_bh(&cmd->istate_lock);
3771 state = ISTATE_SEND_STATUS;
3772 goto check_rsp_state;
3773 }
3774
3775 break;
3776 case ISTATE_SEND_STATUS:
3777 case ISTATE_SEND_STATUS_RECOVERY:
3778 ret = iscsit_send_response(cmd, conn);
3779 break;
3780 case ISTATE_SEND_LOGOUTRSP:
3781 ret = iscsit_send_logout(cmd, conn);
3782 break;
3783 case ISTATE_SEND_ASYNCMSG:
3784 ret = iscsit_send_conn_drop_async_message(
3785 cmd, conn);
3786 break;
3787 case ISTATE_SEND_NOPIN:
3788 ret = iscsit_send_nopin(cmd, conn);
3789 break;
3790 case ISTATE_SEND_REJECT:
3791 ret = iscsit_send_reject(cmd, conn);
3792 break;
3793 case ISTATE_SEND_TASKMGTRSP:
3794 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3795 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003796 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003797 ret = iscsit_tmr_post_handler(cmd, conn);
3798 if (ret != 0)
3799 iscsit_fall_back_to_erl0(conn->sess);
3800 break;
3801 case ISTATE_SEND_TEXTRSP:
3802 ret = iscsit_send_text_rsp(cmd, conn);
3803 break;
3804 default:
3805 pr_err("Unknown Opcode: 0x%02x ITT:"
3806 " 0x%08x, i_state: %d on CID: %hu\n",
3807 cmd->iscsi_opcode, cmd->init_task_tag,
3808 state, conn->cid);
3809 goto err;
3810 }
3811 if (ret < 0)
3812 goto err;
3813
3814 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3815 iscsit_tx_thread_wait_for_tcp(conn);
3816 iscsit_unmap_iovec(cmd);
3817 goto err;
3818 }
3819 iscsit_unmap_iovec(cmd);
3820
3821 switch (state) {
3822 case ISTATE_SEND_LOGOUTRSP:
3823 if (!iscsit_logout_post_handler(cmd, conn))
3824 goto restart;
3825 /* fall through */
3826 case ISTATE_SEND_STATUS:
3827 case ISTATE_SEND_ASYNCMSG:
3828 case ISTATE_SEND_NOPIN:
3829 case ISTATE_SEND_STATUS_RECOVERY:
3830 case ISTATE_SEND_TEXTRSP:
3831 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003832 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003833 spin_lock_bh(&cmd->istate_lock);
3834 cmd->i_state = ISTATE_SENT_STATUS;
3835 spin_unlock_bh(&cmd->istate_lock);
3836 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003837 default:
3838 pr_err("Unknown Opcode: 0x%02x ITT:"
3839 " 0x%08x, i_state: %d on CID: %hu\n",
3840 cmd->iscsi_opcode, cmd->init_task_tag,
3841 cmd->i_state, conn->cid);
3842 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003843 }
3844
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003845 if (atomic_read(&conn->check_immediate_queue))
3846 return 1;
3847
Andy Grover6f3c0e62012-04-03 15:51:09 -07003848 return 0;
3849
3850err:
3851 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003852restart:
3853 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003854}
3855
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003856static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003857{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003858 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003859 struct iscsi_queue_req *qr;
3860 struct iscsi_cmd *cmd;
3861 u8 state;
3862 int ret;
3863
3864 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3865 cmd = qr->cmd;
3866 state = qr->state;
3867 kmem_cache_free(lio_qr_cache, qr);
3868
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003869 ret = t->iscsit_response_queue(conn, cmd, state);
3870 if (ret == 1 || ret < 0)
3871 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003872 }
3873
3874 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003875}
3876
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003877int iscsi_target_tx_thread(void *arg)
3878{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003879 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003880 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003881 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003882 /*
3883 * Allow ourselves to be interrupted by SIGINT so that a
3884 * connection recovery / failure event can be triggered externally.
3885 */
3886 allow_signal(SIGINT);
3887
3888restart:
3889 conn = iscsi_tx_thread_pre_handler(ts);
3890 if (!conn)
3891 goto out;
3892
Andy Grover6f3c0e62012-04-03 15:51:09 -07003893 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003894
3895 while (!kthread_should_stop()) {
3896 /*
3897 * Ensure that both TX and RX per connection kthreads
3898 * are scheduled to run on the same CPU.
3899 */
3900 iscsit_thread_check_cpumask(conn, current, 1);
3901
Roland Dreierd5627ac2012-10-31 09:16:46 -07003902 wait_event_interruptible(conn->queues_wq,
3903 !iscsit_conn_all_queues_empty(conn) ||
3904 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003905
3906 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3907 signal_pending(current))
3908 goto transport_err;
3909
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003910get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003911 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003912 if (ret < 0)
3913 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003914
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003915 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003916 if (ret == 1)
3917 goto get_immediate;
3918 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003919 goto restart;
3920 else if (ret < 0)
3921 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003922 }
3923
3924transport_err:
3925 iscsit_take_action_for_connection_exit(conn);
3926 goto restart;
3927out:
3928 return 0;
3929}
3930
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003931static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3932{
3933 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3934 struct iscsi_cmd *cmd;
3935 int ret = 0;
3936
3937 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3938 case ISCSI_OP_SCSI_CMD:
3939 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3940 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003941 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003942
3943 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3944 break;
3945 case ISCSI_OP_SCSI_DATA_OUT:
3946 ret = iscsit_handle_data_out(conn, buf);
3947 break;
3948 case ISCSI_OP_NOOP_OUT:
3949 cmd = NULL;
3950 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3951 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3952 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003953 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003954 }
3955 ret = iscsit_handle_nop_out(conn, cmd, buf);
3956 break;
3957 case ISCSI_OP_SCSI_TMFUNC:
3958 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3959 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003960 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003961
3962 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3963 break;
3964 case ISCSI_OP_TEXT:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003965 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3966 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003967 goto reject;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003968
3969 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003970 break;
3971 case ISCSI_OP_LOGOUT:
3972 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3973 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003974 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003975
3976 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3977 if (ret > 0)
3978 wait_for_completion_timeout(&conn->conn_logout_comp,
3979 SECONDS_FOR_LOGOUT_COMP * HZ);
3980 break;
3981 case ISCSI_OP_SNACK:
3982 ret = iscsit_handle_snack(conn, buf);
3983 break;
3984 default:
3985 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3986 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3987 pr_err("Cannot recover from unknown"
3988 " opcode while ERL=0, closing iSCSI connection.\n");
3989 return -1;
3990 }
3991 if (!conn->conn_ops->OFMarker) {
3992 pr_err("Unable to recover from unknown"
3993 " opcode while OFMarker=No, closing iSCSI"
3994 " connection.\n");
3995 return -1;
3996 }
3997 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
3998 pr_err("Unable to recover from unknown"
3999 " opcode, closing iSCSI connection.\n");
4000 return -1;
4001 }
4002 break;
4003 }
4004
4005 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004006reject:
4007 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004008}
4009
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004010int iscsi_target_rx_thread(void *arg)
4011{
4012 int ret;
4013 u8 buffer[ISCSI_HDR_LEN], opcode;
4014 u32 checksum = 0, digest = 0;
4015 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004016 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004017 struct kvec iov;
4018 /*
4019 * Allow ourselves to be interrupted by SIGINT so that a
4020 * connection recovery / failure event can be triggered externally.
4021 */
4022 allow_signal(SIGINT);
4023
4024restart:
4025 conn = iscsi_rx_thread_pre_handler(ts);
4026 if (!conn)
4027 goto out;
4028
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004029 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4030 struct completion comp;
4031 int rc;
4032
4033 init_completion(&comp);
4034 rc = wait_for_completion_interruptible(&comp);
4035 if (rc < 0)
4036 goto transport_err;
4037
4038 goto out;
4039 }
4040
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004041 while (!kthread_should_stop()) {
4042 /*
4043 * Ensure that both TX and RX per connection kthreads
4044 * are scheduled to run on the same CPU.
4045 */
4046 iscsit_thread_check_cpumask(conn, current, 0);
4047
4048 memset(buffer, 0, ISCSI_HDR_LEN);
4049 memset(&iov, 0, sizeof(struct kvec));
4050
4051 iov.iov_base = buffer;
4052 iov.iov_len = ISCSI_HDR_LEN;
4053
4054 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4055 if (ret != ISCSI_HDR_LEN) {
4056 iscsit_rx_thread_wait_for_tcp(conn);
4057 goto transport_err;
4058 }
4059
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004060 if (conn->conn_ops->HeaderDigest) {
4061 iov.iov_base = &digest;
4062 iov.iov_len = ISCSI_CRC_LEN;
4063
4064 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4065 if (ret != ISCSI_CRC_LEN) {
4066 iscsit_rx_thread_wait_for_tcp(conn);
4067 goto transport_err;
4068 }
4069
4070 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4071 buffer, ISCSI_HDR_LEN,
4072 0, NULL, (u8 *)&checksum);
4073
4074 if (digest != checksum) {
4075 pr_err("HeaderDigest CRC32C failed,"
4076 " received 0x%08x, computed 0x%08x\n",
4077 digest, checksum);
4078 /*
4079 * Set the PDU to 0xff so it will intentionally
4080 * hit default in the switch below.
4081 */
4082 memset(buffer, 0xff, ISCSI_HDR_LEN);
4083 spin_lock_bh(&conn->sess->session_stats_lock);
4084 conn->sess->conn_digest_errors++;
4085 spin_unlock_bh(&conn->sess->session_stats_lock);
4086 } else {
4087 pr_debug("Got HeaderDigest CRC32C"
4088 " 0x%08x\n", checksum);
4089 }
4090 }
4091
4092 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4093 goto transport_err;
4094
4095 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4096
4097 if (conn->sess->sess_ops->SessionType &&
4098 ((!(opcode & ISCSI_OP_TEXT)) ||
4099 (!(opcode & ISCSI_OP_LOGOUT)))) {
4100 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4101 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004102 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4103 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004104 goto transport_err;
4105 }
4106
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004107 ret = iscsi_target_rx_opcode(conn, buffer);
4108 if (ret < 0)
4109 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004110 }
4111
4112transport_err:
4113 if (!signal_pending(current))
4114 atomic_set(&conn->transport_failed, 1);
4115 iscsit_take_action_for_connection_exit(conn);
4116 goto restart;
4117out:
4118 return 0;
4119}
4120
4121static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4122{
4123 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4124 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004125 /*
4126 * We expect this function to only ever be called from either RX or TX
4127 * thread context via iscsit_close_connection() once the other context
4128 * has been reset -> returned sleeping pre-handler state.
4129 */
4130 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004131 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004132
Andy Grover2fbb4712012-04-03 15:51:01 -07004133 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004134 spin_unlock_bh(&conn->cmd_lock);
4135
4136 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004137
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004138 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004139
4140 spin_lock_bh(&conn->cmd_lock);
4141 }
4142 spin_unlock_bh(&conn->cmd_lock);
4143}
4144
4145static void iscsit_stop_timers_for_cmds(
4146 struct iscsi_conn *conn)
4147{
4148 struct iscsi_cmd *cmd;
4149
4150 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004151 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004152 if (cmd->data_direction == DMA_TO_DEVICE)
4153 iscsit_stop_dataout_timer(cmd);
4154 }
4155 spin_unlock_bh(&conn->cmd_lock);
4156}
4157
4158int iscsit_close_connection(
4159 struct iscsi_conn *conn)
4160{
4161 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4162 struct iscsi_session *sess = conn->sess;
4163
4164 pr_debug("Closing iSCSI connection CID %hu on SID:"
4165 " %u\n", conn->cid, sess->sid);
4166 /*
4167 * Always up conn_logout_comp just in case the RX Thread is sleeping
4168 * and the logout response never got sent because the connection
4169 * failed.
4170 */
4171 complete(&conn->conn_logout_comp);
4172
4173 iscsi_release_thread_set(conn);
4174
4175 iscsit_stop_timers_for_cmds(conn);
4176 iscsit_stop_nopin_response_timer(conn);
4177 iscsit_stop_nopin_timer(conn);
4178 iscsit_free_queue_reqs_for_conn(conn);
4179
4180 /*
4181 * During Connection recovery drop unacknowledged out of order
4182 * commands for this connection, and prepare the other commands
4183 * for realligence.
4184 *
4185 * During normal operation clear the out of order commands (but
4186 * do not free the struct iscsi_ooo_cmdsn's) and release all
4187 * struct iscsi_cmds.
4188 */
4189 if (atomic_read(&conn->connection_recovery)) {
4190 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4191 iscsit_prepare_cmds_for_realligance(conn);
4192 } else {
4193 iscsit_clear_ooo_cmdsns_for_conn(conn);
4194 iscsit_release_commands_from_conn(conn);
4195 }
4196
4197 /*
4198 * Handle decrementing session or connection usage count if
4199 * a logout response was not able to be sent because the
4200 * connection failed. Fall back to Session Recovery here.
4201 */
4202 if (atomic_read(&conn->conn_logout_remove)) {
4203 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4204 iscsit_dec_conn_usage_count(conn);
4205 iscsit_dec_session_usage_count(sess);
4206 }
4207 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4208 iscsit_dec_conn_usage_count(conn);
4209
4210 atomic_set(&conn->conn_logout_remove, 0);
4211 atomic_set(&sess->session_reinstatement, 0);
4212 atomic_set(&sess->session_fall_back_to_erl0, 1);
4213 }
4214
4215 spin_lock_bh(&sess->conn_lock);
4216 list_del(&conn->conn_list);
4217
4218 /*
4219 * Attempt to let the Initiator know this connection failed by
4220 * sending an Connection Dropped Async Message on another
4221 * active connection.
4222 */
4223 if (atomic_read(&conn->connection_recovery))
4224 iscsit_build_conn_drop_async_message(conn);
4225
4226 spin_unlock_bh(&sess->conn_lock);
4227
4228 /*
4229 * If connection reinstatement is being performed on this connection,
4230 * up the connection reinstatement semaphore that is being blocked on
4231 * in iscsit_cause_connection_reinstatement().
4232 */
4233 spin_lock_bh(&conn->state_lock);
4234 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4235 spin_unlock_bh(&conn->state_lock);
4236 complete(&conn->conn_wait_comp);
4237 wait_for_completion(&conn->conn_post_wait_comp);
4238 spin_lock_bh(&conn->state_lock);
4239 }
4240
4241 /*
4242 * If connection reinstatement is being performed on this connection
4243 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4244 * connection wait rcfr semaphore that is being blocked on
4245 * an iscsit_connection_reinstatement_rcfr().
4246 */
4247 if (atomic_read(&conn->connection_wait_rcfr)) {
4248 spin_unlock_bh(&conn->state_lock);
4249 complete(&conn->conn_wait_rcfr_comp);
4250 wait_for_completion(&conn->conn_post_wait_comp);
4251 spin_lock_bh(&conn->state_lock);
4252 }
4253 atomic_set(&conn->connection_reinstatement, 1);
4254 spin_unlock_bh(&conn->state_lock);
4255
4256 /*
4257 * If any other processes are accessing this connection pointer we
4258 * must wait until they have completed.
4259 */
4260 iscsit_check_conn_usage_count(conn);
4261
4262 if (conn->conn_rx_hash.tfm)
4263 crypto_free_hash(conn->conn_rx_hash.tfm);
4264 if (conn->conn_tx_hash.tfm)
4265 crypto_free_hash(conn->conn_tx_hash.tfm);
4266
4267 if (conn->conn_cpumask)
4268 free_cpumask_var(conn->conn_cpumask);
4269
4270 kfree(conn->conn_ops);
4271 conn->conn_ops = NULL;
4272
Al Virobf6932f2012-07-21 08:55:18 +01004273 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004274 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004275
4276 if (conn->conn_transport->iscsit_free_conn)
4277 conn->conn_transport->iscsit_free_conn(conn);
4278
4279 iscsit_put_transport(conn->conn_transport);
4280
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004281 conn->thread_set = NULL;
4282
4283 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4284 conn->conn_state = TARG_CONN_STATE_FREE;
4285 kfree(conn);
4286
4287 spin_lock_bh(&sess->conn_lock);
4288 atomic_dec(&sess->nconn);
4289 pr_debug("Decremented iSCSI connection count to %hu from node:"
4290 " %s\n", atomic_read(&sess->nconn),
4291 sess->sess_ops->InitiatorName);
4292 /*
4293 * Make sure that if one connection fails in an non ERL=2 iSCSI
4294 * Session that they all fail.
4295 */
4296 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4297 !atomic_read(&sess->session_logout))
4298 atomic_set(&sess->session_fall_back_to_erl0, 1);
4299
4300 /*
4301 * If this was not the last connection in the session, and we are
4302 * performing session reinstatement or falling back to ERL=0, call
4303 * iscsit_stop_session() without sleeping to shutdown the other
4304 * active connections.
4305 */
4306 if (atomic_read(&sess->nconn)) {
4307 if (!atomic_read(&sess->session_reinstatement) &&
4308 !atomic_read(&sess->session_fall_back_to_erl0)) {
4309 spin_unlock_bh(&sess->conn_lock);
4310 return 0;
4311 }
4312 if (!atomic_read(&sess->session_stop_active)) {
4313 atomic_set(&sess->session_stop_active, 1);
4314 spin_unlock_bh(&sess->conn_lock);
4315 iscsit_stop_session(sess, 0, 0);
4316 return 0;
4317 }
4318 spin_unlock_bh(&sess->conn_lock);
4319 return 0;
4320 }
4321
4322 /*
4323 * If this was the last connection in the session and one of the
4324 * following is occurring:
4325 *
4326 * Session Reinstatement is not being performed, and are falling back
4327 * to ERL=0 call iscsit_close_session().
4328 *
4329 * Session Logout was requested. iscsit_close_session() will be called
4330 * elsewhere.
4331 *
4332 * Session Continuation is not being performed, start the Time2Retain
4333 * handler and check if sleep_on_sess_wait_sem is active.
4334 */
4335 if (!atomic_read(&sess->session_reinstatement) &&
4336 atomic_read(&sess->session_fall_back_to_erl0)) {
4337 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004338 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004339
4340 return 0;
4341 } else if (atomic_read(&sess->session_logout)) {
4342 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4343 sess->session_state = TARG_SESS_STATE_FREE;
4344 spin_unlock_bh(&sess->conn_lock);
4345
4346 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4347 complete(&sess->session_wait_comp);
4348
4349 return 0;
4350 } else {
4351 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4352 sess->session_state = TARG_SESS_STATE_FAILED;
4353
4354 if (!atomic_read(&sess->session_continuation)) {
4355 spin_unlock_bh(&sess->conn_lock);
4356 iscsit_start_time2retain_handler(sess);
4357 } else
4358 spin_unlock_bh(&sess->conn_lock);
4359
4360 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4361 complete(&sess->session_wait_comp);
4362
4363 return 0;
4364 }
4365 spin_unlock_bh(&sess->conn_lock);
4366
4367 return 0;
4368}
4369
4370int iscsit_close_session(struct iscsi_session *sess)
4371{
4372 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4373 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4374
4375 if (atomic_read(&sess->nconn)) {
4376 pr_err("%d connection(s) still exist for iSCSI session"
4377 " to %s\n", atomic_read(&sess->nconn),
4378 sess->sess_ops->InitiatorName);
4379 BUG();
4380 }
4381
4382 spin_lock_bh(&se_tpg->session_lock);
4383 atomic_set(&sess->session_logout, 1);
4384 atomic_set(&sess->session_reinstatement, 1);
4385 iscsit_stop_time2retain_timer(sess);
4386 spin_unlock_bh(&se_tpg->session_lock);
4387
4388 /*
4389 * transport_deregister_session_configfs() will clear the
4390 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4391 * can be setting it again with __transport_register_session() in
4392 * iscsi_post_login_handler() again after the iscsit_stop_session()
4393 * completes in iscsi_np context.
4394 */
4395 transport_deregister_session_configfs(sess->se_sess);
4396
4397 /*
4398 * If any other processes are accessing this session pointer we must
4399 * wait until they have completed. If we are in an interrupt (the
4400 * time2retain handler) and contain and active session usage count we
4401 * restart the timer and exit.
4402 */
4403 if (!in_interrupt()) {
4404 if (iscsit_check_session_usage_count(sess) == 1)
4405 iscsit_stop_session(sess, 1, 1);
4406 } else {
4407 if (iscsit_check_session_usage_count(sess) == 2) {
4408 atomic_set(&sess->session_logout, 0);
4409 iscsit_start_time2retain_handler(sess);
4410 return 0;
4411 }
4412 }
4413
4414 transport_deregister_session(sess->se_sess);
4415
4416 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4417 iscsit_free_connection_recovery_entires(sess);
4418
4419 iscsit_free_all_ooo_cmdsns(sess);
4420
4421 spin_lock_bh(&se_tpg->session_lock);
4422 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4423 sess->session_state = TARG_SESS_STATE_FREE;
4424 pr_debug("Released iSCSI session from node: %s\n",
4425 sess->sess_ops->InitiatorName);
4426 tpg->nsessions--;
4427 if (tpg->tpg_tiqn)
4428 tpg->tpg_tiqn->tiqn_nsessions--;
4429
4430 pr_debug("Decremented number of active iSCSI Sessions on"
4431 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4432
4433 spin_lock(&sess_idr_lock);
4434 idr_remove(&sess_idr, sess->session_index);
4435 spin_unlock(&sess_idr_lock);
4436
4437 kfree(sess->sess_ops);
4438 sess->sess_ops = NULL;
4439 spin_unlock_bh(&se_tpg->session_lock);
4440
4441 kfree(sess);
4442 return 0;
4443}
4444
4445static void iscsit_logout_post_handler_closesession(
4446 struct iscsi_conn *conn)
4447{
4448 struct iscsi_session *sess = conn->sess;
4449
4450 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4451 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4452
4453 atomic_set(&conn->conn_logout_remove, 0);
4454 complete(&conn->conn_logout_comp);
4455
4456 iscsit_dec_conn_usage_count(conn);
4457 iscsit_stop_session(sess, 1, 1);
4458 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004459 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004460}
4461
4462static void iscsit_logout_post_handler_samecid(
4463 struct iscsi_conn *conn)
4464{
4465 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4466 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4467
4468 atomic_set(&conn->conn_logout_remove, 0);
4469 complete(&conn->conn_logout_comp);
4470
4471 iscsit_cause_connection_reinstatement(conn, 1);
4472 iscsit_dec_conn_usage_count(conn);
4473}
4474
4475static void iscsit_logout_post_handler_diffcid(
4476 struct iscsi_conn *conn,
4477 u16 cid)
4478{
4479 struct iscsi_conn *l_conn;
4480 struct iscsi_session *sess = conn->sess;
4481
4482 if (!sess)
4483 return;
4484
4485 spin_lock_bh(&sess->conn_lock);
4486 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4487 if (l_conn->cid == cid) {
4488 iscsit_inc_conn_usage_count(l_conn);
4489 break;
4490 }
4491 }
4492 spin_unlock_bh(&sess->conn_lock);
4493
4494 if (!l_conn)
4495 return;
4496
4497 if (l_conn->sock)
4498 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4499
4500 spin_lock_bh(&l_conn->state_lock);
4501 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4502 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4503 spin_unlock_bh(&l_conn->state_lock);
4504
4505 iscsit_cause_connection_reinstatement(l_conn, 1);
4506 iscsit_dec_conn_usage_count(l_conn);
4507}
4508
4509/*
4510 * Return of 0 causes the TX thread to restart.
4511 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004512int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004513 struct iscsi_cmd *cmd,
4514 struct iscsi_conn *conn)
4515{
4516 int ret = 0;
4517
4518 switch (cmd->logout_reason) {
4519 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4520 switch (cmd->logout_response) {
4521 case ISCSI_LOGOUT_SUCCESS:
4522 case ISCSI_LOGOUT_CLEANUP_FAILED:
4523 default:
4524 iscsit_logout_post_handler_closesession(conn);
4525 break;
4526 }
4527 ret = 0;
4528 break;
4529 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4530 if (conn->cid == cmd->logout_cid) {
4531 switch (cmd->logout_response) {
4532 case ISCSI_LOGOUT_SUCCESS:
4533 case ISCSI_LOGOUT_CLEANUP_FAILED:
4534 default:
4535 iscsit_logout_post_handler_samecid(conn);
4536 break;
4537 }
4538 ret = 0;
4539 } else {
4540 switch (cmd->logout_response) {
4541 case ISCSI_LOGOUT_SUCCESS:
4542 iscsit_logout_post_handler_diffcid(conn,
4543 cmd->logout_cid);
4544 break;
4545 case ISCSI_LOGOUT_CID_NOT_FOUND:
4546 case ISCSI_LOGOUT_CLEANUP_FAILED:
4547 default:
4548 break;
4549 }
4550 ret = 1;
4551 }
4552 break;
4553 case ISCSI_LOGOUT_REASON_RECOVERY:
4554 switch (cmd->logout_response) {
4555 case ISCSI_LOGOUT_SUCCESS:
4556 case ISCSI_LOGOUT_CID_NOT_FOUND:
4557 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4558 case ISCSI_LOGOUT_CLEANUP_FAILED:
4559 default:
4560 break;
4561 }
4562 ret = 1;
4563 break;
4564 default:
4565 break;
4566
4567 }
4568 return ret;
4569}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004570EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004571
4572void iscsit_fail_session(struct iscsi_session *sess)
4573{
4574 struct iscsi_conn *conn;
4575
4576 spin_lock_bh(&sess->conn_lock);
4577 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4578 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4579 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4580 }
4581 spin_unlock_bh(&sess->conn_lock);
4582
4583 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4584 sess->session_state = TARG_SESS_STATE_FAILED;
4585}
4586
4587int iscsit_free_session(struct iscsi_session *sess)
4588{
4589 u16 conn_count = atomic_read(&sess->nconn);
4590 struct iscsi_conn *conn, *conn_tmp = NULL;
4591 int is_last;
4592
4593 spin_lock_bh(&sess->conn_lock);
4594 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4595
4596 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4597 conn_list) {
4598 if (conn_count == 0)
4599 break;
4600
4601 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4602 is_last = 1;
4603 } else {
4604 iscsit_inc_conn_usage_count(conn_tmp);
4605 is_last = 0;
4606 }
4607 iscsit_inc_conn_usage_count(conn);
4608
4609 spin_unlock_bh(&sess->conn_lock);
4610 iscsit_cause_connection_reinstatement(conn, 1);
4611 spin_lock_bh(&sess->conn_lock);
4612
4613 iscsit_dec_conn_usage_count(conn);
4614 if (is_last == 0)
4615 iscsit_dec_conn_usage_count(conn_tmp);
4616
4617 conn_count--;
4618 }
4619
4620 if (atomic_read(&sess->nconn)) {
4621 spin_unlock_bh(&sess->conn_lock);
4622 wait_for_completion(&sess->session_wait_comp);
4623 } else
4624 spin_unlock_bh(&sess->conn_lock);
4625
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004626 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004627 return 0;
4628}
4629
4630void iscsit_stop_session(
4631 struct iscsi_session *sess,
4632 int session_sleep,
4633 int connection_sleep)
4634{
4635 u16 conn_count = atomic_read(&sess->nconn);
4636 struct iscsi_conn *conn, *conn_tmp = NULL;
4637 int is_last;
4638
4639 spin_lock_bh(&sess->conn_lock);
4640 if (session_sleep)
4641 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4642
4643 if (connection_sleep) {
4644 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4645 conn_list) {
4646 if (conn_count == 0)
4647 break;
4648
4649 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4650 is_last = 1;
4651 } else {
4652 iscsit_inc_conn_usage_count(conn_tmp);
4653 is_last = 0;
4654 }
4655 iscsit_inc_conn_usage_count(conn);
4656
4657 spin_unlock_bh(&sess->conn_lock);
4658 iscsit_cause_connection_reinstatement(conn, 1);
4659 spin_lock_bh(&sess->conn_lock);
4660
4661 iscsit_dec_conn_usage_count(conn);
4662 if (is_last == 0)
4663 iscsit_dec_conn_usage_count(conn_tmp);
4664 conn_count--;
4665 }
4666 } else {
4667 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4668 iscsit_cause_connection_reinstatement(conn, 0);
4669 }
4670
4671 if (session_sleep && atomic_read(&sess->nconn)) {
4672 spin_unlock_bh(&sess->conn_lock);
4673 wait_for_completion(&sess->session_wait_comp);
4674 } else
4675 spin_unlock_bh(&sess->conn_lock);
4676}
4677
4678int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4679{
4680 struct iscsi_session *sess;
4681 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4682 struct se_session *se_sess, *se_sess_tmp;
4683 int session_count = 0;
4684
4685 spin_lock_bh(&se_tpg->session_lock);
4686 if (tpg->nsessions && !force) {
4687 spin_unlock_bh(&se_tpg->session_lock);
4688 return -1;
4689 }
4690
4691 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4692 sess_list) {
4693 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4694
4695 spin_lock(&sess->conn_lock);
4696 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4697 atomic_read(&sess->session_logout) ||
4698 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4699 spin_unlock(&sess->conn_lock);
4700 continue;
4701 }
4702 atomic_set(&sess->session_reinstatement, 1);
4703 spin_unlock(&sess->conn_lock);
4704 spin_unlock_bh(&se_tpg->session_lock);
4705
4706 iscsit_free_session(sess);
4707 spin_lock_bh(&se_tpg->session_lock);
4708
4709 session_count++;
4710 }
4711 spin_unlock_bh(&se_tpg->session_lock);
4712
4713 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4714 " Group: %hu\n", session_count, tpg->tpgt);
4715 return 0;
4716}
4717
4718MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4719MODULE_VERSION("4.1.x");
4720MODULE_AUTHOR("nab@Linux-iSCSI.org");
4721MODULE_LICENSE("GPL");
4722
4723module_init(iscsi_target_init_module);
4724module_exit(iscsi_target_cleanup_module);